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