aboutsummaryrefslogtreecommitdiff
path: root/pkg/tbtables/tbnopen.x
blob: 3c2ab128b0497c00fd6ffafc1aacca40b3054218 (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
# Table name template package, copied from the image template package imt.x.
#
#          tnt = tbnopen (template)
#          tnt = tbnopenp (clparam)
#                tbnclose (tnt)
#   nchars|EOF = tbnget (tnt, table, maxch)
#          len = tbnlen (tnt)
#                tbnrew (tnt)
#
# Phil Hodge, 8-Sep-1995  Copied from imt.x, names changed.

define	SZ_FNT		512
define	CH_DELIM	20B		# used to flag brackets

# TBNOPENP -- Open a table name template obtained as the string value of a CL
# parameter.

pointer	procedure tbnopenp (param)

char	param[ARB]		# i: CL parameter with string value template
#--
pointer	sp, template, tnt
pointer	tbnopen()
errchk	clgstr

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

	call clgstr (param, Memc[template], SZ_LINE)
	tnt = tbnopen (Memc[template])

	call sfree (sp)
	return (tnt)
end

# TBNOPEN -- Open a table name template.

pointer	procedure tbnopen (template)

char	template[ARB]		# i: table name template
#--
int	sort, level, ip, ch
pointer	sp, listp, fnt, op
define	output {Memc[op]=$1;op=op+1}
int	fntopnb(), strlen()

begin
	call smark (sp)
	call salloc (fnt, strlen(template)*12/10 + SZ_FNT, TY_CHAR)

	# Sorting is disabled as input and output templates, derived from the
	# same database but with string editing used to modify the output list,
	# may be sorted differently as sorting is performed upon the edited
	# output list.

	sort = NO

	op = fnt
	for (ip=1;  template[ip] != EOS;  ip=ip+1) {
	    ch = template[ip]

	    if (ch == '[') {
		if (ip > 1 && template[ip-1] == '!') {
		    # ![ -- Pass a [ to FNT (character class notation).
		    Memc[op-1] = '['

		} else if (ip > 1 && template[ip-1] == '\\') {
		    # \[ -- The [ is part of the filename.  Pass it on as an
		    # escape sequence to get by the FNT.

		    output ('[')

		} else {
		    # [ -- Unescaped [.  This marks the beginning of an
		    # extension name.  Output `%%[...]%' and escape all
		    # pattern matching metacharacters until a comma template
		    # delimiter is encountered.  Note that a comma within []
		    # is not a template delimiter.

		    output ('%')
		    output ('%')
		    output (CH_DELIM)

		    level = 0
		    for (;  template[ip] != EOS;  ip=ip+1) {
			ch = template[ip]
			if (ch == ',') {		# ,
			    if (level <= 0)
				break			# exit loop
			    else {
				output ('\\')
				output (ch)
			    }
			} else if (ch == '[') {		# [
			    output ('\\')
			    output (ch)
			    level = level + 1
			} else if (ch == ']') {		# ]
			    output (ch)
			    level = level - 1
			} else if (ch == '*') {		# *
			    output ('\\')
			    output (ch)
			} else {			# normal chars
			    output (ch)
			}
		    }
		    output ('%')
		    ip = ip - 1
		}

	    } else if (ch == '@') {
		# List file reference.  Output the CH_DELIM code before the @
		# to prevent further translations on the table names
		# returned from the list file, e.g., "CH_DELIM // @listfile".

		output (CH_DELIM)
		output ('/')
		output ('/')
		output (ch)

	    } else {
		output (ch)
	    }
	}

	Memc[op] = EOS

	listp = fntopnb (Memc[fnt], sort)

	call sfree (sp)
	return (listp)
end


# TBNGET -- Get the next table name from the expanded list.
# EOF is returned if there are no more names.

int procedure tbnget (tnt, outstr, maxch)

pointer	tnt			# i: table template descriptor
char	outstr[ARB]		# o: output string
int	maxch			# i: max chars out
#--
int	nchars
pointer	sp, buf
int	fntgfnb(), tbn_mapname()
errchk	fntgfnb

begin
	call smark (sp)
	call salloc (buf, SZ_PATHNAME, TY_CHAR)

	if (fntgfnb (tnt, Memc[buf], SZ_PATHNAME) == EOF) {
	    outstr[1] = EOS
	    call sfree (sp)
	    return (EOF)
	}

	nchars = tbn_mapname (Memc[buf], outstr, maxch)
	call sfree (sp)
	return (nchars)
end


# TBNLEN -- Return the number of table names in the expanded list.

int procedure tbnlen (tnt)

pointer	tnt			# i: table name template descriptor
#--
int	fntlenb()

begin
	return (fntlenb (tnt))
end

    
# TBNREW -- Rewind the expanded table name list.

procedure tbnrew (tnt)

pointer	tnt			# i: table name template descriptor

begin
	call fntrewb (tnt)
end

    
# TBNCLOSE -- Close a table name template.

procedure tbnclose (tnt)

pointer	tnt			# io: table name template descriptor

begin
	call fntclsb (tnt)
end


# TBN_MAPNAME -- Translate the string returned by FNT into a table
# specification suitable for input to the table I/O routines.

int procedure tbn_mapname (fnt, outstr, maxch)

char	fnt[ARB]		# i: FNT string
char	outstr[ARB]		# o: output string
int	maxch

int	ip, op

begin
	op = 1
	for (ip=1;  fnt[ip] != EOS;  ip=ip+1) {
	    if (fnt[ip] == '[') {
		outstr[op] = '\\'
		op = op + 1
		outstr[op] = '['
		op = op + 1

	    } else if (fnt[ip] == CH_DELIM) {
		for (ip=ip+1;  fnt[ip] != EOS;  ip=ip+1) {
		    outstr[op] = fnt[ip]
		    op = op + 1
		    if (op > maxch)
			break
		}
		break

	    } else {
		outstr[op] = fnt[ip]
		op = op + 1
		if (op > maxch)
		    break
	    }
	}
	
	outstr[op] = EOS
	return (op - 1)
end