aboutsummaryrefslogtreecommitdiff
path: root/pkg/ecl/scan.c
blob: db7f26baee96b5de7c357d0a139d97ddc66e2546 (plain) (blame)
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.
 */

#define import_spp
#define import_libc
#define import_stdio
#include <iraf.h>

#include "config.h"
#include "operand.h"
#include "param.h"
#include "grammar.h"
#include "task.h"
#include "errs.h"
#include "proto.h"


/*
 * SCAN -- free-format and formatted scan functions.
 */

extern	int cldebug;
extern	char *nullstr;
extern	char *eofstr;
extern	char *indefstr;
extern	char *indeflc;

#define	MAXARGS 32
static	int nscan_val=0;	/* value returned by NSCAN intrinsic	*/


/* SCAN -- Perform the bulk of the scan,fscan intrinsic functions to do
 * free-formatted reads into nargs params.  Formatting is done by makeop()
 * according to the type of the corresponding destination param.
 * Destination may be "stdout".
 *
 * Nargs is the number of operands on the stack we need to deal with.
 *   They are all strings.  The scan procedure is actually called to
 *   process calls to both the SCAN and FSCAN intrinsics.  If scan was
 *   called, the argument "source" will be the string "stdin".  If source
 *   is null, the source is given by the first operand on the stack; it
 *   may be the special string "stdin".  Thereafter, there are exactly
 *   nargs-1 string operands each of which is the name of a destination
 *   parameter to be assigned.  The operand order must be such that the
 *   first one popped is the name of the parameter to which the first field
 *   of the scan line is to be assigned.
 *
 * EOF or OK is returned as the function value.  The number of items
 *   successfully scanned is returned by a subsequent call to NSCAN().
 *
 * query if readlist yields undefined.
 * error() may be called on various conditions.
 */

void 
cl_scan (int nargs, char *source)
{
	char	buf[SZ_LINE];
	char	*bp, *start, c;
	char	*pk, *t, *p, *f;
	char	field = '\0';
	struct	operand o;
	struct	param *pp;
	int	eoftst;

	eoftst = 0;

	/* Fill buf with the line to be scanned.
	 */
	if (strcmp (source, "stdin") == 0) {
	    /* Read from the standard input (SCAN call).
	     */
	    if (fgets (buf, SZ_LINE, currentask->t_stdin) == NULL)
		eoftst++;
	    else
		lentst (buf);
	    /* First arg is an output param, not source, so increment
	     * nargs.
	     */
	    nargs++;

	} else {
	    /* Get source name from first operand (FSCAN call)
	     */
	    o = popop();
	    if (!strcmp (o.o_val.v_s, "stdin") ||
		!strcmp (o.o_val.v_s, "STDIN")) {

		if (fgets (buf, SZ_LINE, currentask->t_stdin) == NULL)
		    eoftst++;
		else
		    lentst (buf);

	    } else {
		breakout (o.o_val.v_s, &pk, &t, &p, &f);
		pp = paramsrch (pk, t, p);
		paramget (pp, *f);
		opcast (OT_STRING);
		o = popop();

		if (pp->p_flags & P_LEOF)
		    eoftst++;
		else {
		    if (opundef (&o)) {
			query (pp);		/* pushes op */
			opcast (OT_STRING);
			o = popop();
		    }
		    strncpy (buf, o.o_val.v_s, SZ_LINE);
		}
	    }
	}

	if (eoftst) {
	    o.o_type = OT_INT;
	    o.o_val.v_i = CL_EOF;
	    while (nargs-- > 0)
		popop();		/* flush op stack		*/
	    pushop (&o);
	    return;
	}

	/* Take each portion of buf and assign to the given parameter.
	 */
	bp = buf;
	nscan_val = 0;

	while (nargs-- > 0) {		/* get each destination name	*/
	    o = popop();

	    if (!strcmp (o.o_val.v_s, "stdout") ||
		!strcmp (o.o_val.v_s, "STDOUT")) {
		pp = NULL;
	    } else {
		breakout (o.o_val.v_s, &pk, &t, &p, &f);
		field = *f;
		pp = paramsrch (pk, t, p);	/* never returns NULL	*/
	    }

	    /* Assign rest of line if struct type parameter.  For simple
	     * string or filename type params, the next whitespace delimited
	     * word is broken out (see below).
	     */
	    if (pp != NULL &&
		((pp->p_type & (PT_STRUCT|PT_IMCUR|PT_GCUR|PT_UKEY)) &&
		!(pp->p_type & (PT_FILNAM|PT_PSET|PT_LIST)))) {

		if (nargs != 0)
		    cl_error (E_UERR,
			"Struct type param must be final Scan argument");
		start = bp;

	    } else {
		while (*bp == ' ' || *bp == '\t')
		    bp++;
		/* It is not an error if not all params can be filled by scan.
		 * Simply break off scan, pop the unused args off the stack,
		 * and return as the function value the number of items
		 * sucessfully scanned.
		 */
		if (*bp == '\0')
		    break;
		start = bp;
		for (c = *bp;  c!=' ' && c!='\t' && c!='\0';  c = *bp)
		    bp++;
		if (c != '\0')
		    *bp++ = '\0';
	    }

	    if (pp == NULL)
		fputs (start, currentask->t_stdout);
	    else {
		o = makeop (start, pp->p_type & OT_BASIC);
		if (opundef (&o))
		    break;		/* cannot convert as basic type	*/
		pushop (&o);
		paramset (pp, field);
	    }

	    nscan_val++;
	}

	/* If we broke out of the above loop because of an unsuccessful
	 * conversion, we must pop the remaining unused operands off the stack.
	 */
	while (--nargs >= 0)
	    popop();

	o.o_type = OT_INT;
	o.o_val.v_i = nscan_val;
	pushop (&o);
}


/* CL_SCANF -- Formatted scan.  Like SCAN except that a C-scanf like format
 * statement is used to decode the input text.
 */
void 
cl_scanf (char *format, int nargs, char *input)
{
	int	nscan_val, eoftst, n;
	char	*pk, *t, *p, *f;
	struct	operand o;
	char	buf[SZ_LINE];
	char	*v[MAXARGS];
	struct	param *pp;

	eoftst = 0;

	/* Fill buf with the line to be scanned.
	 */
	if (strcmp (input, "stdin") == 0) {
	    /* Read from the standard input (SCANF).
	     */
	    if (fgets (buf, SZ_LINE, currentask->t_stdin) == NULL)
		eoftst++;
	    else
		lentst (buf);
	    /* First arg is an output param, not source, so increment nargs. */
	    nargs++;

	} else {
	    /* Get source name from first operand (FSCANF).
	     */
	    o = popop();

	    if (!strcmp (o.o_val.v_s, "stdin") ||
		!strcmp (o.o_val.v_s, "STDIN")) {

		if (fgets (buf, SZ_LINE, currentask->t_stdin) == NULL)
		    eoftst++;
		else
		    lentst (buf);

	    } else {
		breakout (o.o_val.v_s, &pk, &t, &p, &f);
		pp = paramsrch (pk, t, p);
		paramget (pp, *f);
		opcast (OT_STRING);
		o = popop();

		if (pp->p_flags & P_LEOF)
		    eoftst++;
		else {
		    if (opundef (&o)) {
			query (pp);		/* pushes op */
			opcast (OT_STRING);
			o = popop();
		    }
		    strncpy (buf, o.o_val.v_s, SZ_LINE);
		}
	    }
	}

	/* Check for EOF. */
	if (eoftst) {
	    o.o_type = OT_INT;
	    o.o_val.v_i = CL_EOF;
	    while (nargs-- > 0)
		popop();		/* flush op stack		*/
	    pushop (&o);
	    return;
	}

	/* Process the stacked operands and build the argument list for
	 * the scanf call.  Each argument pointer points directly to the
	 * stored parameter value in the parameter descriptor.
	 */
	for (n=0;  --nargs >= 0;  n++) {
	    /* Stacked operand is parameter name. */
	    o = popop();
	    breakout (o.o_val.v_s, &pk, &t, &p, &f);
	    pp = paramsrch (pk, t, p);

	    /* Add address of parameter value to argument list.  First set
	     * the value with PARAMSET, to make sure that the pset knows
	     * that the value has been modified.
	     */
	    switch (pp->p_valo.o_type & OT_BASIC) {
	    case OT_BOOL:
		o = makeop ("yes", OT_BOOL);  pushop (&o);
		paramset (pp, FN_VALUE);
		v[n] = (char *) &pp->p_valo.o_val.v_i;
		break;
	    case OT_INT:
		o = makeop ("0", OT_INT);  pushop (&o);
		paramset (pp, FN_VALUE);
		v[n] = (char *) &pp->p_valo.o_val.v_i;
		break;
	    case OT_REAL:
		o = makeop ("0", OT_REAL);  pushop (&o);
		paramset (pp, FN_VALUE);
		v[n] = (char *) &pp->p_valo.o_val.v_r;
		break;
	    case OT_STRING:
		o = makeop ("", OT_STRING);  pushop (&o);
		paramset (pp, FN_VALUE);
		v[n] = (char *) pp->p_valo.o_val.v_s;
		break;
	    default:
		cl_error (E_UERR, "scanf: cannot scan into %s\n", o.o_val.v_s);
	    }
	}

	/* Perform the scan. */
	nscan_val = sscanf (buf, format,
	    v[ 0], v[ 1], v[ 2], v[ 3], v[ 4], v[ 5], v[ 6], v[ 7],
	    v[ 8], v[ 9], v[10], v[11], v[12], v[13], v[14], v[15],
	    v[16], v[17], v[18], v[19], v[20], v[21], v[22], v[23],
	    v[24], v[25], v[26], v[27], v[28], v[29], v[30], v[31]);

	o.o_type = OT_INT;
	o.o_val.v_i = nscan_val;
	pushop (&o);
}


/* GET_NSCANVAL -- Return the number of items successfully scanned in the
 * last call to SCAN.
 */
int 
get_nscanval (void)
{
	return (nscan_val);
}


/* LENTST -- Test that the scan line just read did not overflow the line
 * buffer.
 */
void 
lentst (char *buf)
{
	char	*index();
	char	*bp;

	bp = index (buf, '\n');
	if (bp != NULL)
	    *bp = '\0';
	else
	    cl_error (E_UERR, "scan limited to %d char lines", SZ_LINE-1);
}