1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/
#include <stdio.h>
#define import_kernel
#define import_knames
#define import_spp
#include <iraf.h>
/*
* KUTIL -- Miscellaneous utilities required by the network interface.
* Most of these are either portable (no i/o) or are built upon the kernel
* i/o routines.
*/
/* KU_FOPEN -- Open a text file.
*/
ku_fopen (fname, mode)
char *fname;
char *mode;
{
PKCHAR osfn[SZ_PATHNAME+1];
XINT fmode, chan;
strcpy ((char *)osfn, fname);
if (mode[0] == 'r')
fmode = READ_ONLY;
else
return (ERR);
ZOPNTX (osfn, &fmode, &chan);
return (chan);
}
/* KU_FCLOSE -- Close a text file.
*/
ku_fclose (fd)
int fd;
{
XINT chan=fd, status;
ZCLSTX (&chan, &status);
return (status);
}
/* KU_FGETS -- Get a newline delimited line from a text file. The semantics of
* this procedure are like the unix FGETS.
*/
char *
ku_fgets (obuf, maxch, fd)
char *obuf;
int maxch;
int fd;
{
register XCHAR *ip;
register char *op;
register int n;
XCHAR lbuf[SZ_LINE+1];
XINT maxchars, status, chan;
maxchars = (maxch > SZ_LINE) ? SZ_LINE : maxch;
chan = fd;
ZGETTX (&chan, lbuf, &maxchars, &status);
if (status <= 0)
return (NULL);
for (ip=lbuf, op=obuf, n=status; --n >= 0; )
*op++ = *ip++;
*op++ = EOS;
return (obuf);
}
/* KU_GPASSWD -- Read a line from the terminal in raw mode (no echo), e.g.,
* when reading a password.
*/
ku_gpasswd (prompt, passwd, maxch)
char *prompt; /* user prompt string */
char *passwd; /* receives password */
int maxch;
{
XCHAR text[SZ_LINE+1], ch;
XINT mode=READ_WRITE, chan, status, nchars;
register char *ip;
register XCHAR *op;
register int n;
/* Open terminal. */
strcpy ((char *)text, TTYNAME);
ZOPNTY (text, &mode, &chan);
if (chan < 0) {
passwd[0] = EOS;
return (ERR);
}
/* Write prompt string. */
for (ip=prompt, op=text, nchars=0; (*op++ = *ip++) != EOS; )
nchars++;
ZPUTTY (&chan, text, &nchars, &status);
ZFLSTY (&chan, &status);
/* Read line in raw mode. */
nchars = 1;
for (n=0; n < maxch; n++) {
ZGETTY (&chan, &text, &nchars, &status);
ch = text[0];
if (status <= 0 || ch == '\n' || ch == '\r')
break;
passwd[n] = ch;
}
passwd[n] = EOS;
/* Echo the newline. */
ch = '\n';
ZPUTTY (&chan, &ch, &nchars, &status);
/* Disable raw mode. */
nchars = LEN_RAWCMD;
for (ip=RAWOFF, op=text, n=LEN_RAWCMD; --n > 0 && (*op++ = *ip++); )
;
ZPUTTY (&chan, text, &nchars, &status);
ZCLSTY (&chan, &status);
return (n);
}
/* KU_MKFNAME -- Make an OSFN, given a logical directory name (either "iraf"
* or "home"), a subdirectory name, and a filename.
*/
ku_mkfname (ldir, subdir, fname, osfn, maxch)
char *ldir; /* logical directory name */
char *subdir; /* subdirectory */
char *fname; /* filename */
char *osfn; /* receives pathname */
int maxch;
{
PKCHAR pkname[SZ_PATHNAME+1];
PKCHAR temp[SZ_FNAME+1];
XINT maxchars=SZ_PATHNAME, nchars;
if (ku_mapdir (ldir, (char *)pkname, SZ_PATHNAME) == ERR)
return (ERR);
strcpy ((char *)temp, subdir);
ku_strupk (pkname, pkname, &maxchars);
ku_strupk (temp, temp, &maxchars);
ZFSUBD (pkname, &maxchars, temp, &nchars);
ku_strpak (pkname, pkname, &maxchars);
strcat ((char *)pkname, fname);
strncpy (osfn, (char *)pkname, maxch);
osfn[maxch-1] = EOS;
return (OK);
}
/* KU_ITOC -- Encode a simple positive integer in a decimal radix, returning
* a pointer to the encoded numeric string.
*/
char *
ku_itoc (num)
int num;
{
register int dig, n;
register char *op;
static char buf[15];
op = &buf[15];
*--op = '\0';
for (n=num; dig = n % 10; n /= 10)
*--op = dig + '0';
return (op);
}
/* KU_BCOPY -- Copy a byte array.
*/
ku_bcopy (a, b, nbytes)
char *a; /* input byte array */
char *b; /* output byte array */
int nbytes; /* number of bytes to move */
{
register char *ip, *op;
register int n = nbytes;
/* If the two arrays are the same return immediately. If the move is
* to the left then copy left to right, else copy right to left.
*/
if (a == b) {
return;
} else if (b < a) {
for (ip=a, op=b; --n >= 0; )
*op++ = *ip++;
} else {
for (ip = &a[n], op = &b[n]; --n >= 0; )
*--op = *--ip;
}
}
/* KU_SLEEP -- Suspend process execution.
*/
ku_sleep (nseconds)
int nseconds;
{
int mseconds = nseconds*1000;
ZWMSEC (&mseconds);
}
/* KU_ERROR -- [MACHDEP] Print an error message somewhere where the user can
* see it (but do not abort or interrupt execution).
*/
ku_error (message)
char *message;
{
write (2, message, strlen(message));
write (2, "\n", 1);
}
/* KU_MAPDIR -- Return the OSFN of the named logical directory, which can
* be either "iraf" or "home". The IRAF root directory is either given in
* the host system environment and returned by ZGTENV, or is defined as
* IRAF in <iraf.h>. The user's login directory "home" is the host
* system login directory, not the IRAF login directory. On a UNIX system
* the pathname of this directory is given in the UNIX file /etc/passwd.
* On other systems, e.g., VMS, the ZGTENV mechanism can be used to define
* the user's home directory.
*/
ku_mapdir (ldir, osfn, maxch)
char *ldir; /* logical directory name */
char *osfn; /* receives filename */
int maxch;
{
PKCHAR pkname[SZ_FNAME+1];
PKCHAR valstr[SZ_PATHNAME+1];
XINT maxchars=SZ_PATHNAME, status;
/* Look in the host environment first.
*/
strcpy ((char *)pkname, ldir);
ZGTENV (pkname, valstr, &maxchars, &status);
if (status > 0) {
strncpy (osfn, (char *)valstr, maxch);
osfn[maxch-1] = EOS;
return (OK);
} else if (strncmp (ldir, "iraf", 4) == 0) {
strncpy (osfn, IRAF, maxch);
osfn[maxch-1] = EOS;
return (OK);
} else if (strncmp (ldir, "home", 4) != 0) {
osfn[0] = EOS;
return (ERR);
}
/* If we get here the ldir is "home" and no definition was found in the
* host environment. Determine host login directory by some system
* dependent means. [MACHDEP].
*/
strcpy ((char *)pkname, "LOGNAME");
ZGTENV (pkname, valstr, &maxchars, &status);
if (status <= 0) {
osfn[0] = EOS;
return (ERR);
} else {
strcpy (osfn, ":udd:");
strcat (osfn, (char *)valstr);
strcat (osfn, ":");
return (OK);
}
}
/* STRPAK -- Pack an SPP character string into a C string, i.e., a sequence
* of characters stored one per byte, delimited by EOS='\0'. The operation
* may be performed in place. This version assumes that the host character
* set is ASCII and hence no lookup table reference to map character sets is
* needed. If this is not the case, code must be added to convert to the host
* character set.
*
* N.B.: If sizeof(XCHAR)=1, XEOS=EOS, and the host character set is ASCII,
* and the operation is being performed in place, then this procedure should
* do nothing.
*/
ku_strpak (instr, outstr, maxch)
XCHAR *instr;
PKCHAR *outstr;
XINT *maxch;
{
register XCHAR *ip = instr;
register char *op = (char *)outstr;
register int n = *maxch;
while ((*op++ = *ip++) != XEOS && --n >= 0)
;
*--op = EOS;
}
/* STRUPK -- Unpack a kernel (C style) string into an SPP string. The unpacking * operation can be performed in place. A kernel string consists of a sequence
* of host characters stored one character per byte, delimited by EOS='\0'.
* We assume here that the host character set is ASCII. If this is not the
* case code must be added to convert from the host character set to ASCII in
* the unpacked string.
*
* N.B.: If sizeof(XCHAR)=1, XEOS=EOS, and the host character set is ASCII,
* and the operation is being performed in place, then this procedure should
* do nothing.
*/
ku_strupk (instr, outstr, maxch)
PKCHAR *instr;
XCHAR *outstr;
XINT *maxch;
{
register char *ip = (char *)instr;
register XCHAR *op = outstr;
register int n;
/* Is is necessary to determine the length of the string in order to
* be able to unpack the string in place, i.e., from right to left.
*/
n = strlen (ip);
n = (n < *maxch) ? n : *maxch;
op[n] = XEOS;
while (--n >= 0)
op[n] = ip[n];
}
|