aboutsummaryrefslogtreecommitdiff
path: root/pkg/utilities/nttools/lib/tctexp.x
blob: 263b18bc92a26e147d75ec6bd90a5012e31d9557 (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
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
include <tbset.h>
include	<ctype.h>
include	"reloperr.h"

define	MAX_STACK	8
define	BLANK		' '
define	DELIM		','
define  ESCAPE		'\\'
define	NEGCHAR		'~'	# negation character
define	ALT_NEGCHAR	'!'	# alternate negation character

.help tctexp
.nf___________________________________________________________________________
Column template package

This package contains subroutines to expand a column name template into
an array of column pointers which match the template.  The template is a
list of column patterns separated by commas or whitespace.  The column
pattern is either a column name, a file name containing a list of column
names, or a pattern using the usual IRAF pattern matching syntax. For
example, the string

	a[1-9], b, time*, @column.lis

would be expanded as the column names a1 through a9, b, any column name
beginning with "time", and all the column names in the file column.lis.
If the column template is entirely whitespace, the array of column pointers
will include all the columns in the table, as this seems the most reasonable
default. If the first non-white character is the negation character (~),
the array of column pointers will include all columns not matched by the
template. The negation character only has this meaning as the first character
in the column template, and is interpreted as part of a column name if
found later in the template or in a file.

.endhelp______________________________________________________________________

# TCTEXP -- Expand a column template into an array of column pointers
#
# Given a table pointed to by a table descriptor and a column name template,
# return an array of column pointers. The size of the column pointer array
# is given by numcol and should be greater than or equal to the number of
# columns in the table. The actual number of columns found that match the
# template is returned as numptr.
#
# B.Simon	24-Jul-1987	First Code
# Phil Hodge	 1-Jun-1989	make search for columns case insensitive
# Phil Hodge	28-Jan-1999	add ! as an alternate negation character

procedure tctexp (tp, template, numcol, numptr, colptr)

pointer	tp		# i: pointer to table descriptor
char	template[ARB]	# i: column template
int	numcol		# i: size of column pointer array
int	numptr		# o: number of columns matched
pointer	colptr[ARB]	# o: array of column pointers
#--

bool	nometa		# true if pattern does not contain metacharacters
bool	negate		# true if template starts with negation character

int	fd_ptr		# pointer to stack of open list file descriptors
int	ic		# first non-white character in template

pointer fd_stack[MAX_STACK]
			# stack of file descriptors for open list files

pointer	sp, colpat, pattern, auxcol, fd

string	stkovflerr	"List files are nested too deeply, stack overflow"

int	strlen(), tctgetpat()
pointer	stropen(), open()

errchk	salloc, stropen, open, close
errchk	tctgetpat, tctmakpat, tctstrmatch, tctpatmatch

begin
	numptr = 0
	negate = false

	call smark (sp)
	call salloc (colpat, SZ_FNAME, TY_CHAR)
	call salloc (pattern, SZ_FNAME, TY_CHAR)

	# Check the column name template to find the first non-white character.

 	for (ic = 1; IS_WHITE (template[ic]); ic = ic + 1)
		;

	if (template[ic] == EOS) {

	    # If the template is blank, include all columns in the array

	    call allcolumns (tp, numptr, auxcol)
	    call amovi (Memi[auxcol], colptr, numptr)
	    call mfree (auxcol, TY_INT)
	    fd_ptr = 0

	} else {

	    # If the first non-white character is the negation character
	    # (either ~ or !), the meaning of the column name template is
	    # negated, that is, the array of column pointers will include
	    # those columns whose names were not matched by the column template

	    if (template[ic] == NEGCHAR || template[ic] == ALT_NEGCHAR) {
		negate = true
		ic = ic + 1
	    }

	    # Open the column name template as a file and push on
	    # the list file stack

	    fd_ptr = 1
	    fd_stack[1] =
		stropen (template[ic], strlen(template[ic]), READ_ONLY)

	}

	while (fd_ptr > 0) {

	    # Pop file descriptor off of the list file stack

	    fd = fd_stack[fd_ptr]
	    fd_ptr = fd_ptr - 1

	    # Loop over all column patterns in the file

	    while (tctgetpat (fd, Memc[colpat], SZ_FNAME) > 0) {

		if (Memc[colpat] == '@') {

		    # If this pattern is a list file name, push the
		    # current descriptor on the stack and open the file

		    if (fd_ptr == MAX_STACK)
			call error (BOUNDS, stkovflerr)
		    fd_ptr = fd_ptr + 1
		    fd_stack[fd_ptr] = fd
		    fd = open (Memc[colpat+1], READ_ONLY, TEXT_FILE)

		} else {

		    # Otherwise, encode the pattern and search the table
		    # for matching column names. To speed the search, use
		    # a special routine if the pattern does not include
		    # metacharacters

		    call strlwr (Memc[colpat])	# for case insensitivity
		    call tctmakpat (Memc[colpat], Memc[pattern], SZ_FNAME,
				    nometa)
		    if (nometa)
			call tctstrmatch (tp, Memc[pattern], numcol,
					  numptr, colptr)
		    else
			call tctpatmatch (tp, Memc[pattern], numcol,
					  numptr, colptr)
		}
	    }
	    call close (fd)
	}

	if (negate)
	    call invert (tp, numptr, colptr)

	call sfree (sp)
end

# TCTGETPAT -- Get next comma or whitespace delimeted pattern from file
#
# Copy characters into colpat until a field delimeter or the maximum number of
# characters is reached. The number of characters in colpat is returned as the
# value of the function, so the procedure which calls this one can test for
# the last field in the template.
#
# B. Simon	24-Jul-87	First Code

int procedure tctgetpat (fd, colpat, maxch)

pointer	fd		# i: template file descriptor
char	colpat[ARB]	# o: pattern from column name template
int	maxch		# i: maximum number of characters in field
#--
char	ch		# next character from template
int	iq		# pointer to character in colpat

char	getc()

begin
	# Skip leading whitespace or commas

	ch = getc (fd, ch)
	while (IS_CNTRL(ch) || ch == BLANK || ch == DELIM)
	    ch = getc (fd, ch)

	# Copy characters to colpat. End when maxch is reached, or
	# when comma, whitespace, or EOF is found

	for (iq = 1; iq <= maxch; iq = iq + 1) {

	    if (IS_CNTRL(ch) || ch == BLANK || ch == DELIM || ch == EOF)
		break

	    colpat[iq] = ch
	    ch = getc (fd, ch)
	}
	colpat[iq] = EOS

	# If loop is terminated because of maxch, eat remaining characters
	# in field

	while (! IS_CNTRL(ch) && ch != BLANK && ch != DELIM && ch != EOF)
	    ch = getc (fd, ch)

	# Return number of characters in colpat

	return (iq-1)
end

# TCTMAKPAT -- Encode the column pattern
#
# Create the pattern used by the matching routines. Check for metacharacters
# (unescaped pattern matching characters) to see if the faster constant
# pattern routine can be used.
#
# B.Simon	24-Jul-87	First Code

procedure tctmakpat (colpat, pattern, maxch, nometa)

char	colpat[ARB]	# i: Column pattern string
char	pattern[ARB]	# o: Encoded pattern string
int	maxch		# i: Maximum length of encoded pattern string
bool	nometa		# o: True if no metacharacters in string
#--
int	ic, ip
pointer	sp, buffer, buffer2, errtxt, ib

int	stridx(), strlen(), patmake()

string	patovflerr "Column pattern too long (%s)"
string	badpaterr  "Column pattern has bad syntax (%s)"

begin
	call smark (sp)
	call salloc (buffer, maxch, TY_CHAR)
	call salloc (buffer2, maxch, TY_CHAR)
	call salloc (errtxt, SZ_LINE, TY_CHAR)

	nometa = true
	ib = buffer

	# Copy the column pattern to a temporary buffer

	for (ic = 1; colpat[ic] != EOS ; ic = ic + 1) {

	    # Copy escape sequences, but do not count as metacharacters

	    if (colpat[ic] == ESCAPE && colpat[ic+1] != EOS) {
		Memc[ib] = ESCAPE
		ib = ib + 1
		ic = ic + 1

	    # Covert '*' to '?*', count as a metacharacter

	    } else if (colpat[ic] == '*') {
		nometa = false
		Memc[ib] = '?'
		ib = ib + 1

	    # Check for the other metacharacters

	    } else if (stridx (colpat[ic], "[?{") > 0)
		nometa = false

	    Memc[ib] = colpat[ic]
	    ib = ib + 1
	}
	Memc[ib] = EOS

	# Check the buffer length against maximum pattern length

	if (strlen (Memc[buffer]) > maxch) {
	    call sprintf (Memc[errtxt], SZ_LINE, patovflerr)
	    call pargstr (colpat)
	    call error (BOUNDS, Memc[errtxt])
	}

	# If no metacharacters, strip escape sequences

	if (nometa) {
	    ip = 1
	    for (ib = buffer; Memc[ib] != EOS; ib = ib + 1) {
		if (Memc[ib] == ESCAPE && Memc[ib+1] != EOS)
		    ib = ib + 1
		pattern[ip] = Memc[ib]
		ip = ip + 1
	    }
	    pattern[ip] = EOS

	# Otherwise, encode with patmake

	} else {
	    call sprintf (Memc[buffer2], maxch, "^%s$")
	    call pargstr (Memc[buffer])

	    if (patmake (Memc[buffer2], pattern, SZ_LINE) == ERR) {
		call sprintf (Memc[errtxt], SZ_LINE, badpaterr)
		call pargstr (colpat)
		call error (SYNTAX, Memc[errtxt])
	    }
	}

	call sfree (sp)
end

# TCTSTRMATCH -- Add a column pointer for a column name to the array
#
# Used to match column names when the column pattern contains no
# metacharacters.
#
# B. Simon	24-Jul-87	First Code

procedure tctstrmatch (tp, pattern, numcol, numptr, colptr)

pointer	tp		# i: pointer to table descriptor
char	pattern[ARB]	# i: column pattern
int	numcol		# i: size of column pointer array
int	numptr		# o: number of columns matched
pointer	colptr[ARB]	# o: array of column pointers
#--
int	iptr
pointer	sp, errtxt, cp

string	maxcolerr "Maximum number of columns in table exceeded (%d)"

errchk	tbcfnd

begin
	call smark (sp)
	call salloc (errtxt, SZ_LINE, TY_CHAR)

	# Find the column pointer corresponding to the column name

	call tbcfnd (tp, pattern, cp, 1)

	# Pointer is null if column not found in table

	if (cp == NULL)
	    return

	# See if the column name has already been matched

	for (iptr = 1; iptr <= numptr; iptr = iptr +1) {
	    if (cp == colptr[iptr])
		break
	    }

	# If not, add its pointer in the array of pointers
	# after checking for array overflow

	if (iptr > numptr) {
	    if (numptr >= numcol) {
		call sprintf (Memc[errtxt], SZ_LINE, maxcolerr)
		call pargi (numcol)
		call error (BOUNDS, Memc[errtxt])
	    }
	    numptr = numptr + 1
	    colptr[numptr] = cp
	}

	call sfree (sp)
end

# TCTPATMATCH -- Find column pointers for columns matching a pattern
#
# This routine is called when the column pattern contains metacharacters.
#
# B.Simon	27-Jul-87	First Code

procedure tctpatmatch (tp, pattern, numcol, numptr, colptr)

pointer	tp		# i: pointer to table descriptor
char	pattern[ARB]	# i: column pattern
int	numcol		# i: size of column pointer array
int	numptr		# o: number of columns matched
pointer	colptr[ARB]	# o: array of column pointers
#--
int	maxcol, icol, iptr
pointer	sp, errtxt, cp
pointer	colname

string	maxcolerr "Maximum number of columns in table exceeded (%d)"

int	tbpsta(), tbcnum(), patmatch()

errchk	tbpsta, tbcnum, tbcinf, patmatch

begin
	call smark (sp)
	call salloc (colname, SZ_COLNAME, TY_CHAR)
	call salloc (errtxt, SZ_LINE, TY_CHAR)

	maxcol = tbpsta (tp, TBL_NCOLS)

	# Compare the column pattern to each column name in the table

	do icol = 1, maxcol {

	    # Get the next column name in the table

	    cp = tbcnum (tp, icol)
	    call tbcigt (cp, TBL_COL_NAME, Memc[colname], SZ_COLNAME)
	    call strlwr (Memc[colname])		# for case insensitivity

	    # Check the column name for a match

	    if (patmatch (Memc[colname], pattern) > 0) {
		# See if the column name has already been matched

		for (iptr = 1; iptr <= numptr; iptr = iptr +1) {
		    if (cp == colptr[iptr])
			break
		}

		# If not, add its pointer in the array of pointers
		# after checking for array overflow

		if (iptr > numptr) {
		    if (numptr >= numcol) {
			call sprintf (Memc[errtxt], SZ_LINE, maxcolerr)
			call pargi (numcol)
			call error (BOUNDS, Memc[errtxt])
		    }
		    numptr = numptr + 1
		    colptr[numptr] = cp
		}
	    }
	}

	call sfree (sp)

end