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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define import_spp
#define import_error
#include <iraf.h>
#include "mkpkg.h"
#include "extern.h"
/*
* CHAR.C -- Character functions, character i/o.
*/
/* M_GETC -- Get a (possibly pushed back) character from the mkpkgfile
* associated with the given context. If the sequence $( is encountered
* in the input, fetch the value of the named macro and push it back into
* the input stream and continue scanning. Implementing recursive macro
* expansion at this low level permits the use of macros in any part of
* the input except comments.
*/
int
m_getc (register struct context *cx)
{
register int ch, nch;
register char *op;
char name[SZ_FNAME+1], *val;
char lbuf[SZ_CMD+1];
while ((ch = m_rawgetc (cx)) == '$') {
/* Check for the escape sequence "$$" and return the literal $
* if this is seen. Also return if $ is seen but the next char
* is not left paren ("$(..)" is a macro reference).
*/
nch = m_rawgetc (cx);
if (nch == '$')
return (nch);
else if (nch != '(') {
m_ungetc (nch, cx);
break;
}
/* Extract the name of the macro from the input stream.
*/
for (op=name; (*op = m_rawgetc(cx)) != ')'; op++)
if (*op == '\n' || *op == EOF) {
*op = EOS;
warns ("missing right paren in $(M) macro reference: `%s'",
name);
*op++ = '\n';
*op = EOS;
val = name;
goto push;
break;
}
*op = EOS;
/* If the symbol name is prefixed by a question mark, e.g., $(?sym),
* query for the symbol and read the value from the standard input.
* If the syntax is "$(@file)" return the contents of the named
* file as the value of the macro reference. Otherwise look in
* the symbol table and then in the environment for the named
* symbol. If the symbol cannot be found in either place push
* its name and hope for the best.
*/
if (name[0] == '?') {
/* Interactive query. */
if ((cx->fp == stdin)) {
warns ("`$(%s)': cannot query in -f stdin mode", name);
val = &name[1];
} else {
printf ("%s: ", &name[1]);
fflush (stdout);
if (fgets (lbuf, SZ_CMD, stdin) == NULL)
strcpy (lbuf, name);
if ((val = index (lbuf, '\n')))
*val = EOS;
val = lbuf;
}
} else if (name[0] == '@') {
/* Return contents of a file. */
FILE *fp;
int ch, n;
if ((fp = fopen (&name[1], "r")) == NULL) {
warns ("`$(%s)': cannot open file", name);
val = &name[1];
} else {
for (n=SZ_CMD,op=lbuf; --n >= 0 && (ch=getc(fp)) != EOF; )
*op++ = isspace(ch) ? ' ' : ch;
while (op > lbuf) {
ch = *(op-1);
if (isspace (ch))
--op;
else
break;
}
*op = EOS;
val = lbuf;
fclose (fp);
}
} else if ((val = getsym (name)) == NULL) {
if ((val = os_getenv (name)) == NULL) {
warns ("macro `%s' not found", name);
val = name;
}
}
push:
if (debug > 1) {
printf ("pushback macro `%s' = `%s'\n", name, val);
fflush (stdout);
}
m_pushstr (cx, val);
}
/* Get rid of the tabs once and for all.
*/
return ((ch == '\t') ? ' ' : ch);
}
/* M_RAWGETC -- Get a (possibly pushed back) character from the mkpkgfile
* associated with the given context.
*/
int
m_rawgetc (register struct context *cx)
{
register struct pushback *pb;
register int ch;
for (;;) {
/* Check for single character pushback first. This type of pushback
* occurs at the end of every token.
*/
if ((ch = cx->pbchar)) {
if (debug > 3) {
if (ch <= 040)
printf ("return pushback character 0%o\n", ch);
else
printf ("return pushback character `%c'\n", ch);
fflush (stdout);
}
cx->pbchar = 0;
break;
}
/* Check for string type pushback; return character directly from
* file if no pushback.
*/
if (!cx->pushback) {
ch = k_getc (cx);
break;
}
/* Get pushed back character from pushback buffer.
*/
pb = cx->pb;
if ((ch = *(pb->ip)++) != EOS) {
if (debug > 3) {
if (ch <= 040)
printf ("return pbbuf character 0%o\n", ch);
else
printf ("return pbbuf character `%c'\n", ch);
fflush (stdout);
}
break;
}
/* End of pushed back string; pop pushback stack.
*/
if (debug > 3) {
printf ("pop pushback stack at level=%d\n", pb->npb);
fflush (stdout);
}
pb->op = pb->pbstk[--(pb->npb)];
pb->ip = pb->pbstk[--(pb->npb)];
if (pb->npb <= 0)
cx->pushback = 0;
}
if (ch == '\n')
cx->lineno++;
return (ch);
}
/* M_UNGETC -- Pushback a single character, last in first out. Only a single
* character of this type of pushback is normally allowed, however by using
* PUSHSTR we can provide additional pushback at additional expense (no
* problem provided it is not used a lot).
*/
void
m_ungetc (
int ch,
struct context *cx
)
{
static char ps[2] = "\0";
if (ch == '\n')
--cx->lineno;
if ((ps[0] = cx->pbchar))
m_pushstr (cx, ps);
cx->pbchar = ch;
if (debug > 3) {
if (ch <= 040)
printf ("ungetc 0%o\n", ch);
else
printf ("ungetc `%c'\n", ch);
fflush (stdout);
}
}
/* M_PUSHSTR -- Pushback a string. Pushed strings are read back LIFO, although
* of course the individual characters are returned FIFO.
*/
void
m_pushstr (
struct context *cx,
char *str
)
{
register struct pushback *pb;
register char *ip, *op, *otop, ch;
if (debug > 2) {
if (str[0] <= 040)
printf ("pushback punct char 0x%lx\n", (long) str);
else
printf ("pushback string `%s'\n", str);
fflush (stdout);
}
cx->pushback++;
while ((pb = cx->pb) == NULL)
mk_pbbuf (cx);
pb->pbstk[(pb->npb)++] = pb->ip;
pb->pbstk[(pb->npb)++] = pb->op;
otop = pb->otop;
for (ip=str, op=pb->op; (*op++ = ch = *ip++); ) {
if (ch == '\n')
--cx->lineno;
if (op >= otop)
break;
}
pb->ip = pb->op;
pb->op = op;
if (debug > 2) {
printf ("pb status: ");
printf ("level=%d(%d) nleft=%ld ip=%ld op=%ld bp=%ld otop=%ld\n",
pb->npb, SZ_PBSTK,
(long) (otop-op),
(long) pb->ip,
(long) pb->op,
(long) pb->pbbuf,
(long) otop);
fflush (stdout);
}
if (pb->npb + 2 >= SZ_PBSTK || pb->op >= pb->otop)
fatals ("excessive pushback in `%s'; macro recursion?",
cx->mkpkgfile);
}
/* MK_PBBUF -- Allocate and initialize the pushback descriptor.
*/
void
mk_pbbuf (register struct context *cx)
{
register struct pushback *pb;
pb = cx->pb = (struct pushback *) malloc (sizeof (struct pushback));
if (pb == NULL)
fatals ("out of memory in `%s'", cx->mkpkgfile);
pb->npb = 0;
pb->ip = pb->pbbuf;
pb->op = pb->pbbuf;
pb->otop = &pb->pbbuf[SZ_PBBUF];
}
/* PB_CANCEL -- Cancel any pushback.
*/
void
pb_cancel (register struct context *cx)
{
register struct pushback *pb;
cx->pushback = 0;
cx->pbchar = 0;
if ((pb = cx->pb) != NULL) {
pb->npb = 0;
pb->ip = pb->pbbuf;
pb->op = pb->pbbuf;
pb->otop = &pb->pbbuf[SZ_PBBUF];
}
}
/* PUTSTR -- Add a string to end of the string buffer. It is a fatal error
* if the string buffer overflows.
*/
char *
putstr (char *s)
{
register char *ip, *op, *otop;
char *start;
start = cp;
otop = ctop;
for (ip=s, op=cp; (*op++ = *ip++); )
if (op >= otop)
fatals ("string buffer overflow at `%s'", s);
cp = op;
if (debug > 2) {
printf ("putstr `%s': nleft=%ld\n", s, (long)(otop-op));
fflush (stdout);
}
return (start);
}
/*
* OS Character I/O. This set of routines are provided as a workaround in
* the event that the host system cannot execute FTELL/FSEEK reliably (VMS/C
* could not). The idea here is to keep track of the character offset from
* the beginning of the file. K_FTELL returns the character offset. K_FSEEK
* rewinds the file and reads characters forward to the indicated offset.
* K_GETC keeps a count of the file position. (the k_ stands for kludge).
*/
#ifdef vms
int
k_getc (register struct context *cx)
{
register int ch;
cx->fpos++;
if (debug > 3) {
if ((ch = getc (cx->fp)) > 0)
printf ("%5d %03o %c\n", cx->fpos, ch, ch > 040 ? ch : 040);
return (ch);
} else
return (getc (cx->fp));
}
char *
k_fgets (
char *obuf,
int maxch,
register struct context *cx
)
{
register int ch, n;
register char *op;
for (op=obuf, n=maxch; --n >= 0; )
if ((ch = k_getc(cx)) < 0)
return (NULL);
else {
*op++ = ch;
if (ch == '\n')
break;
}
return (obuf);
}
int
k_fseek (
register struct context *cx,
long offset,
int type
)
{
register FILE *fp = cx->fp;
register int ch;
if (debug > 1)
printf ("seek (%s, %ld, %d)\n", cx->mkpkgfile, offset, type);
if (type == 0) {
fseek (fp, 0L, 0);
cx->fpos = 0;
while (cx->fpos < offset && (ch = getc(fp)) != EOF) {
if (debug > 1)
fputc (ch, stdout);
cx->fpos++;
}
if (debug > 1)
printf ("[]\n");
return (0);
}
if (fseek (fp, offset, type) == ERR)
return (ERR);
else {
cx->fpos = ftell (fp);
return (0);
}
}
long
k_ftell (register struct context *cx)
{
if (debug > 1) {
printf ("ftell returns %d\n", cx->fpos);
fflush (stdout);
}
return (cx->fpos);
}
#else
int
k_getc (struct context *cx)
{
return (getc (cx->fp));
}
char *
k_fgets (
char *op,
int maxch,
register struct context *cx
)
{
return (fgets (op, maxch, cx->fp));
}
int
k_fseek (
struct context *cx,
long offset,
int type
)
{
return (fseek (cx->fp, offset, type));
}
long
k_ftell (struct context *cx)
{
return (ftell (cx->fp));
}
#endif
|