1 /* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */
2 /* From input file "utility.pas" */
3
4
5 #include "p2c.h"
6
7
8 #define UTILITY_G
9 #include "utility.h"
10
11
12 #ifndef STRINGS_H
13 #include "strings.h"
14 #endif
15
16
17 #define blank ' '
18 #define dummy '\0'
19
20
wordCount(s)21 short wordCount(s)
22 Char *s;
23 {
24 short i, n, FORLIM;
25
26 if (*s == '\0')
27 return 0;
28 if (s[0] == blank)
29 n = 0;
30 else
31 n = 1;
32 FORLIM = strlen(s);
33 for (i = 1; i <= FORLIM - 1; i++) {
34 if (s[i-1] == blank && s[i] != blank)
35 n++;
36 }
37 return n;
38 }
39
40
GetNextWord(Result,s,Delim,Term)41 Char *GetNextWord(Result, s, Delim, Term)
42 Char *Result;
43 Char *s;
44 Char Delim, Term;
45 {
46 /* A delimiter is a character that separates words, but forms no part
47 of them. A terminator is a character that appears at the end of
48 a word. */
49 short n = 1;
50 short start, last;
51
52 last = strlen(s);
53 while (n <= last && s[n-1] == Delim)
54 n++;
55 start = n;
56 while (n <= last && s[n-1] != Term && s[n-1] != Delim)
57 n++;
58 if (n <= last && s[n-1] == Term)
59 n++;
60 substr_(Result, s, start, n - start);
61 while (n <= last && s[n-1] == Delim)
62 n++;
63 predelete(s, n - 1);
64 return Result;
65 }
66
67
NextWord(Result,s_,Delim,Term)68 Char *NextWord(Result, s_, Delim, Term)
69 Char *Result;
70 Char *s_;
71 Char Delim, Term;
72 {
73 Char s[256];
74
75 strcpy(s, s_);
76 return (GetNextWord(Result, s, Delim, Term));
77 }
78
79
plural(Result,n)80 Char *plural(Result, n)
81 Char *Result;
82 short n;
83 {
84 if (n == 1)
85 return strcpy(Result, "");
86 else
87 return strcpy(Result, "s");
88 }
89
90
curtail(s,c)91 short curtail(s, c)
92 Char *s;
93 Char c;
94 {
95 short Result = 0;
96 short l;
97
98 l = strlen(s);
99 if (s[l-1] == c) {
100 shorten(s, l - 1);
101 return l;
102 }
103 return Result;
104 }
105
106
toString(Result,n)107 Char *toString(Result, n)
108 Char *Result;
109 short n;
110 {
111 Char s[256];
112
113 sprintf(s, "%d", n);
114 return strcpy(Result, s);
115 }
116
117
digit(c)118 short digit(c)
119 Char c;
120 {
121 return (c - '0');
122 }
123
124
equalsIgnoreCase(s1_,s2_)125 boolean equalsIgnoreCase(s1_, s2_)
126 Char *s1_, *s2_;
127 {
128 Char s1[256], s2[256];
129
130 strcpy(s1, s1_);
131 strcpy(s2, s2_);
132 toUpper(s1);
133 toUpper(s2);
134 return (strcmp(s1, s2) == 0);
135 }
136
137
startsWithIgnoreCase(s1_,s2_)138 boolean startsWithIgnoreCase(s1_, s2_)
139 Char *s1_, *s2_;
140 {
141 Char s1[256], s2[256];
142
143 strcpy(s1, s1_);
144 strcpy(s2, s2_);
145 toUpper(s1);
146 toUpper(s2);
147 return (startsWith(s1, s2));
148 }
149
150
startsWithBracedWord(P_)151 boolean startsWithBracedWord(P_)
152 Char *P_;
153 {
154 Char P[256];
155 Char w[256];
156
157 strcpy(P, P_);
158 GetNextWord(w, P, blank, dummy);
159 return (w[0] == '{' && w[strlen(w) - 1] == '}');
160 }
161
162
trim(s)163 Void trim(s)
164 Char *s;
165 {
166 short k;
167
168 k = posNot(blank, s);
169 if (k > 1)
170 predelete(s, k - 1);
171 else if (k == 0)
172 *s = '\0';
173 }
174
175
endsWith(s1_,s2)176 boolean endsWith(s1_, s2)
177 Char *s1_, *s2;
178 {
179 Char s1[256];
180 short l1, l2;
181
182 strcpy(s1, s1_);
183 l1 = strlen(s1);
184 l2 = strlen(s2);
185 if (l1 < l2)
186 return false;
187 predelete(s1, l1 - l2);
188 return (strcmp(s1, s2) == 0);
189 }
190
191
192 /* Local variables for grep: */
193 struct LOC_grep {
194 Char *source, *pattern;
195 short p1[10], p2[10];
196 short i, p, s, index;
197 boolean matching;
198 } ;
199
remember(s1,s2,LINK)200 Local Void remember(s1, s2, LINK)
201 short s1, s2;
202 struct LOC_grep *LINK;
203 {
204 if (LINK->index > 9)
205 _Escape(9999);
206 LINK->p1[LINK->index] = s1;
207 LINK->p2[LINK->index] = s2;
208 LINK->s = s2 + 1;
209 LINK->index++;
210 }
211
matchnum(LINK)212 Local Void matchnum(LINK)
213 struct LOC_grep *LINK;
214 {
215 boolean allowsign = false, allowpoint = false, quit = false;
216 short s0;
217
218 LINK->matching = false;
219 s0 = LINK->s;
220 if (LINK->p < strlen(LINK->pattern)) {
221 if (LINK->pattern[LINK->p] == '#') {
222 LINK->p++;
223 allowsign = true;
224 if (LINK->p < strlen(LINK->pattern)) {
225 if (LINK->pattern[LINK->p] == '#') {
226 LINK->p++;
227 allowpoint = true;
228 }
229 }
230 }
231 }
232 if (allowsign &&
233 (LINK->source[LINK->s-1] == '-' || LINK->source[LINK->s-1] == '+')) {
234 LINK->s++;
235 if (LINK->s > strlen(LINK->source))
236 return;
237 }
238 while (!quit && LINK->s <= strlen(LINK->source)) {
239 if (LINK->source[LINK->s-1] == '.') {
240 if (!allowpoint)
241 quit = true;
242 else {
243 LINK->s++;
244 allowpoint = false;
245 }
246 }
247 if (isdigit(LINK->source[LINK->i-1])) {
248 LINK->s++;
249 LINK->matching = true;
250 } else
251 quit = true;
252 }
253 if (LINK->matching)
254 remember(s0, LINK->s - 1, LINK);
255 }
256
matchmeta(LINK)257 Local Void matchmeta(LINK)
258 struct LOC_grep *LINK;
259 {
260 if (LINK->p < strlen(LINK->pattern))
261 LINK->p++;
262 if (LINK->source[LINK->s-1] == LINK->pattern[LINK->p-1]) {
263 LINK->s++;
264 LINK->p++;
265 } else
266 LINK->matching = false;
267 }
268
subgrep(LINK)269 Local Void subgrep(LINK)
270 struct LOC_grep *LINK;
271 {
272 LINK->matching = true;
273 if (LINK->pattern[LINK->p-1] == '*') {
274 remember(LINK->s, strlen(LINK->source), LINK);
275 LINK->p++;
276 return;
277 }
278 if (LINK->pattern[LINK->p-1] == '?') {
279 remember(LINK->s, LINK->s, LINK);
280 LINK->p++;
281 return;
282 }
283 if (LINK->pattern[LINK->p-1] == '#') {
284 matchnum(LINK);
285 return;
286 }
287 if (LINK->pattern[LINK->p-1] == '\\') {
288 matchmeta(LINK);
289 return;
290 }
291 if (LINK->source[LINK->s-1] == LINK->pattern[LINK->p-1]) {
292 LINK->s++;
293 LINK->p++;
294 } else
295 LINK->matching = false;
296 }
297
298
299 /*--- Match/Replace package --- */
300
301 /* Search and replace. Stops when pattern no longer matches source.
302 Pattern wildcards:
303 ? Any single character
304 * Any string
305 # An unsigned integer
306 ## A signed integer
307 ### A signed number maybe with a decimal part
308 Pattern metacharacters:
309 \x where x is any character, stands for that character
310 Target wildcards
311 \0 to \9 Value of corresponding source wildcard
312 Target metacharacters
313 \ When not followed by 0..9 or \, stands for itself
314 \\ Backslash
315 */
grep(source_,pattern_,target)316 Void grep(source_, pattern_, target)
317 Char *source_, *pattern_, *target;
318 {
319 struct LOC_grep V;
320 short j, t, reg;
321 Char product[256];
322 boolean trigger = false;
323 short FORLIM, FORLIM1;
324 Char STR2[256];
325
326 V.source = source_;
327 V.pattern = pattern_;
328 V.index = 0;
329 V.s = 1;
330 V.p = 1;
331 for (V.i = 0; V.i <= 9; V.i++) {
332 V.p1[V.i] = 1;
333 V.p2[V.i] = 0;
334 }
335 while (V.matching && V.p <= strlen(V.pattern) && V.s <= strlen(V.source))
336 subgrep(&V);
337 *product = '\0';
338 FORLIM = strlen(target);
339 for (t = 0; t <= FORLIM - 1; t++) {
340 if (trigger) {
341 reg = digit(target[t]);
342 if ((unsigned)reg <= 9) {
343 FORLIM1 = V.p2[reg];
344 for (j = V.p1[reg] - 1; j <= FORLIM1 - 1; j++)
345 sprintf(product + strlen(product), "%c", V.source[j]);
346 } else if (target[t] == '\\')
347 strcat(product, "\\");
348 else
349 sprintf(product + strlen(product), "\\%c", target[t]);
350 trigger = false;
351 } else if (target[t] == '\\' && t + 1 < strlen(target))
352 trigger = true;
353 else
354 sprintf(product + strlen(product), "%c", target[t]);
355 }
356 strcpy(V.source, substr_(STR2, V.source, V.s, strlen(V.source)));
357 strcpy(V.pattern, substr_(STR2, V.pattern, V.p, strlen(V.pattern)));
358 strcpy(target, product);
359 }
360
361
362 /* "match" tests whether the source matches the pattern exactly */
match(source_,pattern_)363 boolean match(source_, pattern_)
364 Char *source_, *pattern_;
365 {
366 Char source[256], pattern[256];
367 static Char target[256] = "";
368
369 strcpy(source, source_);
370 strcpy(pattern, pattern_);
371 grep(source, pattern, target);
372 return (*source == '\0' && *pattern == '\0');
373 }
374
375
376 /* "translate" replaces the pattern by the target in the source. */
translate(Result,source_,pattern_,target_)377 Char *translate(Result, source_, pattern_, target_)
378 Char *Result;
379 Char *source_, *pattern_, *target_;
380 {
381 Char source[256], pattern[256], target[256];
382
383 strcpy(source, source_);
384 strcpy(pattern, pattern_);
385 strcpy(target, target_);
386 grep(source, pattern, target);
387 return strcpy(Result, target);
388 }
389
390
391
392
393 /* End. */
394