w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

utility.c
Go to the documentation of this file.
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 
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 {
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  while (V.matching && V.p <= strlen(V.pattern) && V.s <= strlen(V.source))
311  subgrep(&V);
312  *product = '\0';
313  FORLIM = strlen(target);
314  for (t = 0; t <= FORLIM - 1; t++) {
315  if (trigger) {
316  reg = digit(target[t]);
317  if ((unsigned)reg <= 9) {
318  FORLIM1 = V.p2[reg];
319  for (j = V.p1[reg] - 1; j <= FORLIM1 - 1; j++)
320  sprintf(product + strlen(product), "%c", V.source[j]);
321  } else if (target[t] == '\\')
322  strcat(product, "\\");
323  else
324  sprintf(product + strlen(product), "\\%c", target[t]);
325  trigger = false;
326  } else if (target[t] == '\\' && t + 1 < strlen(target))
327  trigger = true;
328  else
329  sprintf(product + strlen(product), "%c", target[t]);
330  }
331  strcpy(V.source, substr_(STR2, V.source, V.s, strlen(V.source)));
332  strcpy(V.pattern, substr_(STR2, V.pattern, V.p, strlen(V.pattern)));
333  strcpy(target, product);
334 }
335 
336 
337 /* "match" tests whether the source matches the pattern exactly */
338 boolean match(Char *source_, Char *pattern_)
339 {
340  Char source[256], pattern[256];
341  static Char target[256] = "";
342 
343  strcpy(source, source_);
344  strcpy(pattern, pattern_);
346  return (*source == '\0' && *pattern == '\0');
347 }
348 
349 
350 /* "translate" replaces the pattern by the target in the source. */
351 Char *translate(Char *Result, Char *source_, Char *pattern_, Char *target_)
352 {
353  Char source[256], pattern[256], target[256];
354 
355  strcpy(source, source_);
356  strcpy(pattern, pattern_);
357  strcpy(target, target_);
359  return strcpy(Result, target);
360 }
361 
362 
363 
364 
365 /* End. */
#define P_(s)
Definition: basicdefs.h:27
short posNot(char c, char *s)
Definition: cfuncs.c:50
char * substr_(char *Result, char *s, short start, short count)
Definition: cfuncs.c:83
int _Escape(int exitcode)
Definition: cfuncs.c:11
void predelete(char *s, short l)
Definition: cfuncs.c:40
boolean startsWith(char *s1, char *s2)
Definition: cfuncs.c:72
void toUpper(char *s)
Definition: cfuncs.c:68
#define Local
Definition: cfuncs.h:10
#define n
Definition: t4ht.c:1290
static int shorten(int h, int m)
int dummy
Definition: dummy.c:29
int w
Definition: dviconv.c:26
int strcmp()
Definition: coll.cpp:143
char * strcpy()
#define s
Definition: afcover.h:80
#define t
Definition: afcover.h:96
paragraph P
#define c(n)
Definition: gpos-common.c:150
#define reg
Definition: hbf.c:83
small capitals from c petite p scientific i
Definition: afcover.h:80
#define sprintf
Definition: snprintf.c:44
#define isdigit(c)
Definition: snprintf.c:177
#define target(code, i)
Definition: lpeg.c:1165
int k
Definition: otp-parser.c:70
#define V
Definition: pgmcrater.c:68
static int32_t last
Definition: ppagelist.c:29
#define STR2(tok)
Definition: t1part.c:39
char * pattern
Definition: utility.c:175
short i
Definition: utility.c:177
boolean matching
Definition: utility.c:178
short p1[10]
Definition: utility.c:176
short s
Definition: utility.c:177
char * source
Definition: utility.c:175
short index
Definition: utility.c:177
short p
Definition: utility.c:177
short p2[10]
Definition: utility.c:176
Definition: sh.h:1345
Definition: dvips.h:235
int j
Definition: t4ht.c:1589
s1
Definition: t4ht.c:1059
char * s2
Definition: t4ht.c:1062
*job_name strlen((char *) job_name) - 4)
#define s0
Definition: tokst.h:45
char * GetNextWord(char *Result, char *s, char Delim, char Term)
Definition: utility.c:40
char * plural(char *Result, short n)
Definition: utility.c:73
void trim(char *s)
Definition: utility.c:146
static void subgrep(struct LOC_grep *LINK)
Definition: utility.c:246
void grep(char *source_, char *pattern_, char *target)
Definition: utility.c:292
short wordCount(char *s)
Definition: utility.c:21
boolean match(char *source_, char *pattern_)
Definition: utility.c:338
boolean endsWith(char *s1_, char *s2)
Definition: utility.c:158
static void remember(short s1, short s2, struct LOC_grep *LINK)
Definition: utility.c:181
char * translate(char *Result, char *source_, char *pattern_, char *target_)
Definition: utility.c:351
boolean equalsIgnoreCase(char *s1_, char *s2_)
Definition: utility.c:111
short digit(char c)
Definition: utility.c:105
static void matchnum(struct LOC_grep *LINK)
Definition: utility.c:191
#define blank
Definition: utility.c:17
char * toString(char *Result, short n)
Definition: utility.c:96
char * NextWord(char *Result, char *s_, char Delim, char Term)
Definition: utility.c:64
short curtail(char *s, char c)
Definition: utility.c:82
boolean startsWithIgnoreCase(char *s1_, char *s2_)
Definition: utility.c:123
static void matchmeta(struct LOC_grep *LINK)
Definition: utility.c:235
boolean startsWithBracedWord(char *P_)
Definition: utility.c:135
@ start
Definition: preamble.c:52