aboutsummaryrefslogtreecommitdiff
path: root/noao/astutil/t_astcalc.x
blob: 65f649f6b0b25ec2f2e8946642407a264c6b7132 (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
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
include	<error.h>
include	<fset.h>
include	<evvexpr.h>
include	<ctype.h>
include	<ctotok.h>
include	<lexnum.h>
include	<time.h>
include	"astfunc.h"

define	SZ_KEY		32


# T_ASTCALC -- Calculator including astronomical routines.

procedure t_astcalc()

pointer	cmd			# command file
pointer	imlist			# image list
pointer	table			# data table
pointer	col			# column names
pointer	prompt			# prompt for STDIN
bool	verbose			# verbose output?

bool	eval
int	i, ip, sz_cmd, fdcmd, ncmds, nim, tm[LEN_TMSTRUCT]
long	pos
pointer	sp, image, key, expr, keys, exprs, ast
pointer	stopen(), immap()
int	open(), fscan(), fstati(), nowhite(), ctotok()
int	imtopenp(), imtlen(), imtgetim()
int	strlen(), stridxs()
long	note(), clktime(), lsttogmt()
bool	clgetb(), streq()
errchk	open, stopen, immap

begin
	call smark (sp)
	call salloc (image, SZ_FNAME, TY_CHAR)
	call salloc (table, SZ_FNAME, TY_CHAR)
	call salloc (prompt, SZ_FNAME, TY_CHAR)
	call salloc (col, SZ_LINE, TY_CHAR)
	call salloc (key, SZ_KEY, TY_CHAR)

	sz_cmd = SZ_LINE
	call malloc (cmd, sz_cmd, TY_CHAR)
	call malloc (expr, sz_cmd, TY_CHAR)

	# Create ast_func data structure.
	call calloc (ast, LEN_AST, TY_STRUCT)

	# Open symbol table for storing results.
	AST_STP(ast) = stopen ("astcalc", 20, 1024, 20*SZ_KEY)

	# Open the command file and initialize
	ncmds = 0
	call clgstr ("commands", Memc[cmd], sz_cmd)
	if (nowhite (Memc[cmd], Memc[cmd], sz_cmd) > 0) {
	    fdcmd = open (Memc[cmd], READ_ONLY, TEXT_FILE)
	    Memc[prompt] = EOS
	} else {
	    fdcmd = STDIN
	    call clgstr ("prompt", Memc[prompt], SZ_FNAME)
	}
	eval = TRUE
	verbose = clgetb ("verbose")
	call fseti (STDOUT, F_FLUSHNL, YES)

	# Open the image list and first image.
	imlist = imtopenp ("images")
	nim = imtlen (imlist)
	if (nim > 0) {
	    i = imtgetim (imlist, Memc[image], SZ_FNAME)
	    iferr (AST_IM(ast) = immap (Memc[image], READ_WRITE, 0))
		AST_IM(ast) = immap (Memc[image], READ_ONLY, 0)
	    call sprintf (Memc[expr], sz_cmd, "\"%s\"")
		call pargstr (Memc[image])
	    call ac_evaluate (ast, "$I", Memc[expr], eval, verbose)
	}

	# Open the table file.
	call clgstr ("table", Memc[table], SZ_FNAME)
	if (nowhite (Memc[table], Memc[table], SZ_FNAME) > 0) {
	    AST_TFD(ast) = open (Memc[table], READ_ONLY, TEXT_FILE)
	    pos = note (AST_TFD(ast))
	}

	# Set special operands.
	pos = clktime(0)
	call brktime (pos, tm)
	call sprintf (Memc[expr], sz_cmd, "\"%04d-%02d-%02d\"")
	    call pargi (TM_YEAR(tm))
	    call pargi (TM_MDAY(tm))
	    call pargi (TM_MONTH(tm))
	call ac_evaluate (ast, "$D", Memc[expr], eval, verbose)
	call sprintf (Memc[expr], sz_cmd, "\"%02d:%02d:%02d\"")
	    call pargi (TM_HOUR(tm))
	    call pargi (TM_MIN(tm))
	    call pargi (TM_SEC(tm))
	call ac_evaluate (ast, "$T", Memc[expr], eval, verbose)
	call brktime (lsttogmt(pos), tm)
	call sprintf (Memc[expr], sz_cmd, "\"%04d-%02d-%02d\"")
	    call pargi (TM_YEAR(tm))
	    call pargi (TM_MONTH(tm))
	    call pargi (TM_MDAY(tm))
	call ac_evaluate (ast, "$GMD", Memc[expr], eval, verbose)
	call sprintf (Memc[expr], sz_cmd, "\"%02d:%02d:%02d\"")
	    call pargi (TM_HOUR(tm))
	    call pargi (TM_MIN(tm))
	    call pargi (TM_SEC(tm))
	call ac_evaluate (ast, "$GMT", Memc[expr], eval, verbose)
	call sprintf (Memc[expr], sz_cmd, "\"%04d-%02d-%02dT%02d:%02d:%02d\"")
	    call pargi (TM_YEAR(tm))
	    call pargi (TM_MONTH(tm))
	    call pargi (TM_MDAY(tm))
	    call pargi (TM_HOUR(tm))
	    call pargi (TM_MIN(tm))
	    call pargi (TM_SEC(tm))
	call ac_evaluate (ast, "$GMDT", Memc[expr], eval, verbose)

	# Read and evaluate commands.
	repeat {
	    if (Memc[prompt] != EOS) {
		call printf (Memc[prompt])
		call flush (STDOUT)
	    }

	    # Get next command.  Allow for continuation lines.
	    if (fscan (fdcmd) == EOF)
		break
	    ip = 1
	    repeat {
		call gargstr (Memc[cmd+ip-1], sz_cmd)
		ip = strlen (Memc[cmd])
		if (Memc[cmd+ip-1] != '\\')
		    break
		if (fscan (fdcmd) == EOF)
		    break
		if (ip + SZ_LINE >= sz_cmd) {
		    sz_cmd = sz_cmd + SZ_LINE
		    call realloc (cmd, sz_cmd, TY_CHAR)
		    call realloc (expr, sz_cmd, TY_CHAR)
		}
	    }

	    # Eliminate comments, leading/trailing whitespace, and blank lines.
	    ip = stridxs ("#", Memc[cmd])
	    if (ip > 0)
		Memc[cmd+ip-1] = EOS
	    ip = strlen (Memc[cmd])
	    while (IS_WHITE(Memc[cmd+ip-1]))
		ip = ip - 1
	    Memc[cmd+ip] = EOS
	    ip = 1
	    while (IS_WHITE(Memc[cmd+ip-1]))
		ip = ip + 1
	    call strcpy (Memc[cmd+ip-1], Memc[cmd], sz_cmd)
	    if (Memc[cmd] == EOS)
		next

	    # Parse variable.
	    ip = 1
	    if (Memc[cmd+ip-1] == '$') {
	       ip = ip + 1
	       Memc[key] = '$'
	       i = ctotok (Memc[cmd], ip, Memc[key+1], SZ_KEY)
	    } else if (Memc[cmd+ip-1] == '@') {
		ip = ip + 2
		i = 0
                while (Memc[cmd+ip-1]!=Memc[cmd+1] && Memc[cmd+ip-1]!=EOS) {
		    Memc[key+i] = Memc[cmd+ip-1]
		    i = i + 1
                    ip = ip + 1
		}
		Memc[key+i] = EOS
		if (Memc[cmd+ip-1] != Memc[cmd+1]) {
		    call sprintf (Memc[expr], sz_cmd,
			"Syntax error `%s'")
			call pargstr (Memc[cmd])
		    call error (1, Memc[cmd])
		}
		ip = ip + 1
		i = TOK_IDENTIFIER
	    } else
	       i = ctotok (Memc[cmd], ip, Memc[key], SZ_KEY)

	    switch (i) {
	    case TOK_IDENTIFIER:
		while (IS_WHITE(Memc[cmd+ip-1]))
		    ip = ip + 1
		if (Memc[cmd+ip-1] == '=')
		    ip = ip + 1
		else {
		    ip = 1
		    Memc[key] = EOS
		}
	    default:
		ip = 1
		Memc[key] = EOS
	    }

	    # Parse expression.
	    while (IS_WHITE(Memc[cmd+ip-1]) || Memc[cmd+ip-1] == '=')
		ip = ip + 1
	    call strcpy (Memc[cmd+ip-1], Memc[expr], sz_cmd)

	    if (streq (Memc[expr], "quit"))
		break

	    # Save command.
	    if (ncmds == 0) {
		call malloc (keys, 100, TY_POINTER)
		call malloc (exprs, 100, TY_POINTER)
	    } else if (mod (ncmds, 100) == 0) {
		call realloc (keys, ncmds+100, TY_POINTER)
		call realloc (exprs, ncmds+100, TY_POINTER)
	    }

	    call salloc (Memi[keys+ncmds], SZ_KEY, TY_CHAR)
	    call strcpy (Memc[key], Memc[Memi[keys+ncmds]], SZ_KEY)
	    ip = strlen (Memc[expr])
	    call salloc (Memi[exprs+ncmds], ip, TY_CHAR)
	    call strcpy (Memc[expr], Memc[Memi[exprs+ncmds]], ip)

	    # Evaluate expression.
	    call ac_evaluate (ast, Memc[key], Memc[expr], eval, verbose)

	    ncmds = ncmds + 1
	}
	if (AST_IM(ast) != NULL)
	    call imunmap (AST_IM(ast))
	call close (fdcmd)

	# Repeat commands for other lines in the table and other images.
	# Note must be called in order to check for EOF in table.

	if (ncmds > 0 && (nim > 1 || AST_TFD(ast) != NULL)) {
	    repeat {
		eval = TRUE
		if (AST_TFD(ast) != NULL) {
		    if (pos == note (AST_TFD(ast)))
			break
		    if (fstati (AST_TFD(ast), F_EOF) == YES)
			break
		}
		if (nim > 0) {
		    if (imtgetim (imlist, Memc[image], SZ_FNAME) == EOF)
			break
		    iferr (AST_IM(ast) = immap (Memc[image], READ_WRITE, 0))
			AST_IM(ast) = immap (Memc[image], READ_ONLY, 0)
		    call sprintf (Memc[expr], sz_cmd, "\"%s\"")
			call pargstr (Memc[image])
		    call ac_evaluate (ast, "$I", Memc[expr], eval, verbose)
		}

		do i = 1, ncmds
		    call ac_evaluate (ast, Memc[Memi[keys+i-1]],
			Memc[Memi[exprs+i-1]], eval, verbose)

		if (AST_IM(ast) != NULL)
		    call imunmap (AST_IM(ast))
	    }
	}

	if (AST_TFD(ast) != NULL)
	    call close (AST_TFD(ast))
	if (AST_STP(ast) != NULL)
	    call stclose (AST_STP(ast))
	call mfree (cmd, TY_CHAR)
	call mfree (expr, TY_CHAR)
	call sfree (sp)
end


# AC_EVALUATE -- Evaluate the value of the key and add it to symbol table.

procedure ac_evaluate (ast, key, expr, eval, verbose)

pointer	ast			#I Data structure
char	key[ARB]		#I name of key to be edited
char	expr[ARB]		#I value expression
bool	eval			#U Conditional flag
bool	verbose			#I verbose output?

bool	streq()
pointer	o, sym, evvexpr(), stfind(), stenter()
int	locpr(), strncmp()
extern	ac_getop(), ast_func()
errchk	evvexpr

begin
	# Check conditional evaluation.
	if (streq (expr, "endif")) {
	    eval = TRUE
	    return
	} else if (streq (expr, "else")) {
	    eval = (!eval)
	    return
	}
	if (!eval)
	    return

	# Evaluate the expression.
	o = NULL
	if (expr[1] != EOS)
	    o = evvexpr (expr, locpr (ac_getop), ast, locpr (ast_func), ast, 0)
	if (o == NULL)
	    return

	# Set conditional evalution.
	if (key[1] == EOS) {
	    if (strncmp (expr, "if ", 3) == 0 || strncmp (expr, "if(", 3) == 0)
		eval = (O_VALI(o) != 0)

	} else {
	    # Print the verbose output.
	    if (verbose) {
		switch (O_TYPE(o)) {
		case TY_BOOL:
		    call printf ("  %s = %b\n")
			call pargstr (key)
			call pargi (O_VALI(o))
		case TY_CHAR:
		    call printf ("  %s = %s\n")
			call pargstr (key)
			call pargstr (O_VALC(o))
		case TY_INT:
		    call printf ("  %s = %d\n")
			call pargstr (key)
			call pargi (O_VALI(o))
		case TY_REAL:
		    call printf ("  %s = %g\n")
			call pargstr (key)
			call pargr (O_VALR(o))
		case TY_DOUBLE:
		    call printf ("  %s = %g\n")
			call pargstr (key)
			call pargd (O_VALD(o))
		}
	    }

	    sym = stfind (AST_STP(ast), key)
	    if (sym == NULL)
		sym = stenter (AST_STP(ast), key, SZ_LINE)
	    Memi[sym] = O_TYPE(o)
	    switch (O_TYPE(o)) {
	    case TY_BOOL:
		Memi[sym+2] = O_VALI(o)
	    case TY_CHAR:
		call strcpy (O_VALC(o), Memc[P2C(sym+2)], SZ_LINE)
	    case TY_INT:
		Memi[sym+2] = O_VALI(o)
	    case TY_REAL:
		Memd[P2D(sym+2)] = O_VALR(o)
	    case TY_DOUBLE:
		Memd[P2D(sym+2)] = O_VALD(o)
	    }
	}

	call mfree (o, TY_STRUCT)
end


# AC_GETOP -- Satisfy an operand request from EVEXPR.  In this context,
# operand names refer to entries in the symbol table.

procedure ac_getop (ast, operand, o)

pointer	ast			#I Data structure
char	operand[ARB]		#I name of operand to be returned
pointer	o			#O pointer to output operand

pointer	sym, stfind()

begin
	# Get operand value from symbol table.
	sym = stfind (AST_STP(ast), operand)
	if (sym == NULL)
	    call xvv_error1 ("variable `%s' not found", operand[1])

	switch (Memi[sym]) {
	case TY_BOOL, TY_SHORT, TY_INT, TY_LONG:
	    call xvv_initop (o, 0, TY_INT)
	    O_VALI(o) = Memi[sym+2]

	case TY_REAL, TY_DOUBLE, TY_COMPLEX:
	    call xvv_initop (o, 0, TY_DOUBLE)
	    O_VALD(o) = Memd[P2D(sym+2)]

	default:
	    call xvv_initop (o, SZ_LINE, TY_CHAR)
	    call strcpy (Memc[P2C(sym+2)], O_VALC(o), SZ_LINE)
	}
end