Subversion Repositories wimsdev

Rev

Rev 12258 | Rev 12884 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 12258 Rev 12260
Line 85... Line 85...
85
/* copies src to dest, at most lim bytes. Error if more than
85
/* copies src to dest, at most lim bytes. Error if more than
86
   MAX_LINELEN chars would be copied, including final \0. */
86
   MAX_LINELEN chars would be copied, including final \0. */
87
 
87
 
88
void mystrncpy(char *dest, const char *src, size_t lim)
88
void mystrncpy(char *dest, const char *src, size_t lim)
89
{
89
{
90
      if (lim)
90
  if (lim){
91
      {
-
 
92
        size_t i = strlen(src);
91
    size_t i = strlen(src);
93
        if (i >= lim) i = lim-1;
92
    if (i >= lim) i = lim-1;
94
        if (i >= MAX_LINELEN) error("cmd_output_too_long");
93
    if (i >= MAX_LINELEN) error("cmd_output_too_long");
95
        memmove(dest,src,i); dest[i]=0;
94
    memmove(dest,src,i); dest[i]=0;
96
      }
95
  }
97
}
96
}
98
 
97
 
99
/* find matching parenthesis.
98
/* find matching parenthesis.
100
 * The entrance point should be after the opening
99
 * The entrance point should be after the opening
101
 * parenthesis.
100
 * parenthesis.
102
 * Returns NULL if unmatched.
101
 * Returns NULL if unmatched.
103
 */
102
 */
104
char *find_matching(char *p, char c)
103
char *find_matching(char *p, char c)
105
{
104
{
106
    char *pp;
105
  char *pp;
107
    int parenth, brak, brace;
106
  int parenth, brak, brace;
108
    if(c=='|') {
107
  if(c=='|') {
109
      for(pp=p;*pp!=0;pp++) {
-
 
110
          switch(*pp) {
-
 
111
            case '|': return pp;
-
 
112
            case '(': {
-
 
113
              pp=find_matching(pp+1,')');
-
 
114
              if(pp==NULL) return NULL;
-
 
115
              break;
-
 
116
            }
-
 
117
            case '[': {
-
 
118
                pp=find_matching(pp+1,']');
-
 
119
                if(pp==NULL) return NULL;
-
 
120
                break;
-
 
121
            }
-
 
122
            case '{': {
-
 
123
                pp=find_matching(pp+1,'}');
-
 
124
                if(pp==NULL) return NULL;
-
 
125
                break;
-
 
126
            }
-
 
127
            case ')':
-
 
128
            case ']':
-
 
129
            case '}': return NULL;
-
 
130
 
-
 
131
            default: break;
-
 
132
          }
-
 
133
      }
-
 
134
      return NULL;
-
 
135
    }
-
 
136
    parenth=brak=brace=0;
-
 
137
    for(pp=p; *pp!=0; pp++) {
108
    for(pp=p;*pp!=0;pp++) {
138
      switch(*pp) {
109
      switch(*pp) {
139
          case '[': brak++; break;
110
        case '|': return pp;
140
          case ']': brak--; break;
111
        case '(': {
141
          case '(': parenth++; break;
112
          pp=find_matching(pp+1,')');
142
          case ')': parenth--; break;
113
          if(pp==NULL) return NULL;
-
 
114
          break;
-
 
115
        }
143
          case '{': brace++; break;
116
        case '[': {
-
 
117
          pp=find_matching(pp+1,']');
144
          case '}': brace--; break;
118
          if(pp==NULL) return NULL;
145
          default: continue;
119
          break;
146
      }
120
        }
-
 
121
        case '{': {
147
      if(parenth<0 || brak<0 || brace<0) {
122
          pp=find_matching(pp+1,'}');
148
          if(*pp!=c || parenth>0 || brak>0 || brace>0) return NULL;
123
          if(pp==NULL) return NULL;
149
          else break;
124
          break;
-
 
125
        }
-
 
126
        case ')':
-
 
127
        case ']':
-
 
128
        case '}': return NULL;
-
 
129
 
-
 
130
        default: break;
150
      }
131
      }
151
    }
132
    }
-
 
133
    return NULL;
-
 
134
  }
-
 
135
  parenth=brak=brace=0;
-
 
136
  for(pp=p; *pp!=0; pp++) {
-
 
137
    switch(*pp) {
-
 
138
      case '[': brak++; break;
-
 
139
      case ']': brak--; break;
-
 
140
      case '(': parenth++; break;
-
 
141
      case ')': parenth--; break;
-
 
142
      case '{': brace++; break;
-
 
143
      case '}': brace--; break;
-
 
144
      default: continue;
-
 
145
    }
-
 
146
    if(parenth<0 || brak<0 || brace<0) {
-
 
147
      if(*pp!=c || parenth>0 || brak>0 || brace>0) return NULL;
-
 
148
      else break;
-
 
149
    }
-
 
150
  }
152
    if(*pp!=c) return NULL;
151
  if(*pp!=c) return NULL;
153
    return pp;
152
  return pp;
154
}
153
}
155
 
154
 
156
/* Points to the end of the word */
155
/* Points to the end of the word */
157
char *find_word_end(char *p)
156
char *find_word_end(char *p)
158
{
157
{
159
    while(!myisspace(*p) && *p!=0) p++;
158
  while(!myisspace(*p) && *p!=0) p++;
160
    return p;
159
  return p;
161
}
160
}
162
 
161
 
163
/* Strips leading spaces */
162
/* Strips leading spaces */
164
char *find_word_start(char *p)
163
char *find_word_start(char *p)
165
{
164
{
166
    while(myisspace(*p)) p++;
165
  while(myisspace(*p)) p++;
167
    return p;
166
  return p;
168
}
167
}
169
 
168
 
170
/* find a character in a string, but skipping parentheses. */
169
/* find a character in a string, but skipping parentheses. */
171
char *strparchr(char *p, char c)
170
char *strparchr(char *p, char c)
172
{
171
{
173
    char *pp;
172
  char *pp;
174
 
173
 
175
    for(pp=p;*pp && *pp!=c && pp-p<MAX_LINELEN; pp++) {
174
  for(pp=p;*pp && *pp!=c && pp-p<MAX_LINELEN; pp++) {
176
      switch(*pp) {
175
    switch(*pp) {
177
          case '(': pp=find_matching(pp+1,')'); break;
176
      case '(': pp=find_matching(pp+1,')'); break;
178
          case '[': pp=find_matching(pp+1,']'); break;
177
      case '[': pp=find_matching(pp+1,']'); break;
179
          case '{': pp=find_matching(pp+1,'}'); break;
178
      case '{': pp=find_matching(pp+1,'}'); break;
180
      }
-
 
181
      if(pp==NULL) return NULL;
-
 
182
    }
179
    }
-
 
180
    if(pp==NULL) return NULL;
-
 
181
  }
183
    if(*pp==c) return pp; else return NULL;
182
  if(*pp==c) return pp; else return NULL;
184
}
183
}
185
 
184
 
186
/* search for string, skipping parentheses */
185
/* search for string, skipping parentheses */
187
char *strparstr(char *p, char *fnd)
186
char *strparstr(char *p, char *fnd)
188
{
187
{
189
    char *pp, c=*fnd;
188
  char *pp, c=*fnd;
190
    size_t n = strlen(fnd);
189
  size_t n = strlen(fnd);
191
    /*if(*fnd==0) return p+strlen(p);*/
190
  /*if(*fnd==0) return p+strlen(p);*/
192
    for(pp=p; *pp; pp++) {
191
  for(pp=p; *pp; pp++) {
193
      if(*pp==c && (n==1 || strncmp(pp,fnd,n)==0)) return pp;
192
    if(*pp==c && (n==1 || strncmp(pp,fnd,n)==0)) return pp;
194
      switch(*pp) {
193
    switch(*pp) {
195
          case '(': pp=find_matching(pp+1,')'); break;
194
      case '(': pp=find_matching(pp+1,')'); break;
196
          case '[': pp=find_matching(pp+1,']'); break;
195
      case '[': pp=find_matching(pp+1,']'); break;
197
          case '{': pp=find_matching(pp+1,'}'); break;
196
      case '{': pp=find_matching(pp+1,'}'); break;
198
      }
-
 
199
      if (!pp) { pp = strstr(p,fnd); if (!pp) pp = p+strlen(p); break; }
-
 
200
    }
197
    }
-
 
198
    if (!pp) { pp = strstr(p,fnd); if (!pp) pp = p+strlen(p); break; }
-
 
199
  }
201
    return pp;
200
  return pp;
202
}
201
}
203
 
202
 
204
/* Points to the end of an item */
203
/* Points to the end of an item */
205
char *find_item_end(char *p)
204
char *find_item_end(char *p)
206
{
205
{
207
    return strparstr(p,",");
206
  return strparstr(p,",");
208
}
207
}
209
 
208
 
210
/* Points to the end of an item */
209
/* Points to the end of an item */
211
char *find_line_end(char *p)
210
char *find_line_end(char *p)
212
{
211
{
213
    char *pp=strstr(p,"\n");
212
  char *pp=strstr(p,"\n");
214
    if(pp==NULL) pp=p+strlen(p);
213
  if(pp==NULL) pp=p+strlen(p);
215
    return pp;
214
  return pp;
216
}
215
}
217
 
216
 
218
char *charchr(char *p,char *w)
217
char *charchr(char *p,char *w)
219
{
218
{
220
    return strchr(p,w[0]);
219
  return strchr(p,w[0]);
221
}
220
}
222
 
221
 
223
/* Find first occurrence of word */
222
/* Find first occurrence of word */
224
char *wordchr(char *p, char *w)
223
char *wordchr(char *p, char *w)
225
{
224
{
226
    char *r; int n;
225
  char *r; int n;
227
 
226
 
228
    if(*w==0) return NULL;
227
  if(*w==0) return NULL;
229
    n=strlen(w);
228
  n=strlen(w);
230
    for(r=strstr(p,w);r!=NULL &&
229
  for(r=strstr(p,w);r!=NULL &&
231
      ( (r>p && !myisspace(*(r-1))) || (!myisspace(*(r+n)) && *(r+n)!=0) );
230
      ( (r>p && !myisspace(*(r-1))) || (!myisspace(*(r+n)) && *(r+n)!=0) );
232
      r=strstr(r+1,w)){};
231
    r=strstr(r+1,w)){};
233
    return r;
232
  return r;
234
}
233
}
235
 
234
 
236
/* Find first occurrence of item */
235
/* Find first occurrence of item */
237
char *itemchr(char *p, char *w)
236
char *itemchr(char *p, char *w)
238
{
237
{
239
    char *r, *r1, *r2; int n;
238
  char *r, *r1, *r2; int n;
240
 
239
 
241
    if(*w==0) return NULL;
240
  if(*w==0) return NULL;
242
    n=strlen(w);
241
  n=strlen(w);
243
    for(r=strstr(p,w);r!=NULL; r=strstr(r+1,w)) {
242
  for(r=strstr(p,w);r!=NULL; r=strstr(r+1,w)) {
244
      r1=r-1;while(r1>=p && myisspace(*r1)) r1--;
243
    r1=r-1;while(r1>=p && myisspace(*r1)) r1--;
245
      r2=find_word_start(r+n);
244
    r2=find_word_start(r+n);
246
      if((r1<p || *r1==',') && (*r2==0 || *r2==',')) return r;
245
    if((r1<p || *r1==',') && (*r2==0 || *r2==',')) return r;
247
    }
246
  }
248
    return r;
247
  return r;
249
}
248
}
250
 
249
 
251
/* Find first occurrence of line */
250
/* Find first occurrence of line */
252
char *linechr(char *p, char *w)
251
char *linechr(char *p, char *w)
253
{
252
{
254
    char *r;
253
  char *r;
255
    int n;
254
  int n;
256
 
255
 
257
    if(*w==0) return NULL;
256
  if(*w==0) return NULL;
258
    n=strlen(w);
257
  n=strlen(w);
259
    for(r=strstr(p,w);r!=NULL; r=strstr(r+1,w)) {
258
  for(r=strstr(p,w);r!=NULL; r=strstr(r+1,w)) {
260
      if((r<=p || *(r-1)=='\n') && (*(r+n)==0 || *(r+n)=='\n'))
259
    if((r<=p || *(r-1)=='\n') && (*(r+n)==0 || *(r+n)=='\n'))
261
        break;
260
      break;
262
    }
261
  }
263
    return r;
262
  return r;
264
}
263
}
265
 
264
 
266
/* find a variable in a string (math expression).
265
/* find a variable in a string (math expression).
267
 * Returns the pointer or NULL. */
266
 * Returns the pointer or NULL. */
268
char *varchr(char *p, char *v)
267
char *varchr(char *p, char *v)
269
{
268
{
270
    char *pp; int n;
269
  char *pp; int n;
271
    if(*v==0) return NULL;
270
  if(*v==0) return NULL;
272
    n=strlen(v);
271
  n=strlen(v);
273
    for(pp=strstr(p,v); pp!=NULL; pp=strstr(pp+1,v)) {
272
  for(pp=strstr(p,v); pp!=NULL; pp=strstr(pp+1,v)) {
274
      if((pp==p || (!myisalnum(*(pp-1)) && *(pp-1)!='_')) &&
273
    if((pp==p || (!myisalnum(*(pp-1)) && *(pp-1)!='_')) &&
275
         ((!myisalnum(*(pp+n)) && *(pp+n)!='_' && *(pp+n)!='\'')
274
       ((!myisalnum(*(pp+n)) && *(pp+n)!='_' && *(pp+n)!='\'')
276
          || *(pp+n)==0)) break;
275
        || *(pp+n)==0)) break;
277
    }
276
  }
278
    return pp;
277
  return pp;
279
}
278
}
280
 
279
 
281
int _cutit_(char *p, char *list[], int max, char *end_finder(char *pt), int tag)
280
int _cutit_(char *p, char *list[], int max, char *end_finder(char *pt), int tag)
282
{
281
{
283
    int i;
282
  int i;
284
    char *pp, *pe, *p0;
283
  char *pp, *pe, *p0;
285
    if(tag&1) pp=find_word_start(p); else pp=p; /* strip head space */
284
  if(tag&1) pp=find_word_start(p); else pp=p; /* strip head space */
286
    for(i=0;i<max && *pp;i++) {
285
  for(i=0;i<max && *pp;i++) {
287
      pe=end_finder(pp);
286
    pe=end_finder(pp);
288
      if((tag&2) && myisspace(pe[-1])) { /* strip trailing space */
287
    if((tag&2) && myisspace(pe[-1])) { /* strip trailing space */
289
          for(p0=pe; p0>pp && myisspace(p0[-1]); p0--);
288
      for(p0=pe; p0>pp && myisspace(p0[-1]); p0--);
290
          if(p0<pe) *p0=0;
289
      if(p0<pe) *p0=0;
291
      }
-
 
292
      if(*pe) *pe++=0;
-
 
293
      list[i]=pp;
-
 
294
      if(tag&1) pp=find_word_start(pe); else pp=pe;
-
 
295
    }
290
    }
-
 
291
    if(*pe) *pe++=0;
-
 
292
    list[i]=pp;
-
 
293
    if(tag&1) pp=find_word_start(pe); else pp=pe;
-
 
294
  }
296
    return i;
295
  return i;
297
}
296
}
298
 
297
 
299
int cutitems(char *p, char *list[], int max)
298
int cutitems(char *p, char *list[], int max)
300
{
299
{
301
    return _cutit_(p,list,max,find_item_end,3);
300
  return _cutit_(p,list,max,find_item_end,3);
302
}
301
}
303
 
302
 
304
int cutwords(char *p, char *list[], int max)
303
int cutwords(char *p, char *list[], int max)
305
{
304
{
306
    return _cutit_(find_word_start(p),list,max,find_word_end,1);
305
  return _cutit_(find_word_start(p),list,max,find_word_end,1);
307
}
306
}
308
 
307
 
309
int cutlines(char *p, char *list[], int max)
308
int cutlines(char *p, char *list[], int max)
310
{
309
{
311
    return _cutit_(p,list,max,find_line_end,0);
310
  return _cutit_(p,list,max,find_line_end,0);
312
}
311
}
313
 
312
 
314
int cutchars(char *p, char *list[], int max)
313
int cutchars(char *p, char *list[], int max)
315
{
314
{
316
    int i; char *pp;
315
  int i; char *pp;
317
    for(i=0,pp=p;*pp && i<max;list[i++]=pp++);
316
  for(i=0,pp=p;*pp && i<max;list[i++]=pp++);
318
    return i;
317
  return i;
319
}
318
}
320
 
319
 
321
/* strip trailing spaces; return string end. */
320
/* strip trailing spaces; return string end. */
322
char *strip_trailing_spaces(char *p)
321
char *strip_trailing_spaces(char *p)
323
{
322
{
324
    char *pp;
323
  char *pp;
325
    if(*p==0) return p;
324
  if(*p==0) return p;
326
    for(pp=p+strlen(p)-1; pp>=p && myisspace(*pp); pp--);
325
  for(pp=p+strlen(p)-1; pp>=p && myisspace(*pp); pp--);
327
    if(pp[1]) pp[1]=0;
326
  if(pp[1]) pp[1]=0;
328
    return pp;
327
  return pp;
329
}
328
}
330
 
329
 
331
/*  strip trailing spaces; return string end. */
330
/*  strip trailing spaces; return string end. */
332
char *strip_trailing_spaces2(char *p)
331
char *strip_trailing_spaces2(char *p)
333
{
332
{
334
    char *pp;
333
  char *pp;
335
    if(*p==0) return p;
334
  if(*p==0) return p;
336
    for(pp=p+strlen(p)-1; pp>=p && isspace(*pp); *(pp--)=0);
335
  for(pp=p+strlen(p)-1; pp>=p && isspace(*pp); *(pp--)=0);
337
    return pp;
336
  return pp;
338
}
337
}
339
 
338
 
340
/* Routines for quick search of item in a list. */
339
/* Routines for quick search of item in a list. */
341
 
340
 
342
/* Verify whether a list is well-ordered. For debugging uses.
341
/* Verify whether a list is well-ordered. For debugging uses.
343
 * Returns 0 if order is OK, -1 otherwise. */
342
 * Returns 0 if order is OK, -1 otherwise. */
344
int verify_order(void *list, int items, size_t item_size)
343
int verify_order(void *list, int items, size_t item_size)
345
{
344
{
346
    int i; char *old, **p;
345
  int i; char *old, **p;
347
    p=list; old=*p;
346
  p=list; old=*p;
348
    for(i=item_size;i<items*item_size;i+=item_size) {
347
  for(i=item_size;i<items*item_size;i+=item_size) {
349
      p=list+i;
348
    p=list+i;
350
      if(strcmp(*p,old)<0) {
349
    if(strcmp(*p,old)<0) {
351
          fprintf(stderr,"Table disorder: %s > %s",old,*p);
350
      fprintf(stderr,"Table disorder: %s > %s",old,*p);
352
          exit(1);
351
      exit(1);
353
      }
-
 
354
      old=*p;
-
 
355
    }
352
    }
-
 
353
    old=*p;
-
 
354
  }
356
    return 0;
355
  return 0;
357
}
356
}
358
 
357
 
359
/* searches a list. Returns index if found, (-1-index of insertion) if nomatch.
358
/* searches a list. Returns index if found, (-1-index of insertion) if nomatch.
360
 * Uses binary search, list must be sorted. */
359
 * Uses binary search, list must be sorted. */
361
 
360
 
362
int search_list(void *list, int items, size_t item_size, const char *str)
361
int search_list(void *list, int items, size_t item_size, const char *str)
363
{
362
{
364
 int i = 0;
363
  int i = 0;
365
 while (items > 0)
364
  while (items > 0){
366
   {
-
 
367
     int m = items / 2, j = i + m;
365
    int m = items / 2, j = i + m;
368
     int k = strcmp(*(char **)(list + j * item_size), str);
366
    int k = strcmp(*(char **)(list + j * item_size), str);
369
     if (k == 0) return j;
367
    if (k == 0) return j;
370
     if (k > 0) items = m; else {i = j + 1; items -= (m + 1);}
368
    if (k > 0) items = m; else {i = j + 1; items -= (m + 1);}
371
   }
369
  }
372
 return ~i;
370
  return ~i;
373
}
371
}
374
 
372
 
375
/* Returns number of lines in string p */
373
/* Returns number of lines in string p */
376
unsigned int linenum(char *p)
374
unsigned int linenum(char *p)
377
{
375
{
378
    int i; char *pp;
376
  int i; char *pp;
379
 
377
 
380
    /* Ending blank line will be thus removed. */
378
  /* Ending blank line will be thus removed. */
381
    i=strlen(p); if(i>1 && *(p+i-1)=='\n') *(p+i-1)=0;
379
  i=strlen(p); if(i>1 && *(p+i-1)=='\n') *(p+i-1)=0;
382
    if(*p=='\n') i=1; else i=0;
380
  if(*p=='\n') i=1; else i=0;
383
    for(pp=p; pp!=NULL && *pp!=0; pp=strchr(pp+1,'\n'), i++);
381
  for(pp=p; pp!=NULL && *pp!=0; pp=strchr(pp+1,'\n'), i++);
384
    return i;
382
  return i;
385
}
383
}
386
 
384
 
387
/* Returns number of items in the list p, comma separated */
385
/* Returns number of items in the list p, comma separated */
388
unsigned int itemnum(char *p)
386
unsigned int itemnum(char *p)
389
{
387
{
390
    int i; char *pp;
388
  int i; char *pp;
391
 
389
 
392
    if(*p==0) return 0;
390
  if(*p==0) return 0;
393
    for(i=0,pp=p; pp==p || *(pp-1)!=0; pp=find_item_end(pp)+1, i++);
391
  for(i=0,pp=p; pp==p || *(pp-1)!=0; pp=find_item_end(pp)+1, i++);
394
    return i;
392
  return i;
395
}
393
}
396
 
394
 
397
/* Returns number of words in string p */
395
/* Returns number of words in string p */
398
unsigned int wordnum(char *p)
396
unsigned int wordnum(char *p)
399
{
397
{
400
    int i; char *pp;
398
  int i; char *pp;
401
 
399
 
402
    for(i=0, pp=find_word_start(p); *pp!=0; i++) {
400
  for(i=0, pp=find_word_start(p); *pp!=0; i++) {
403
      while(!myisspace(*pp) && *pp!=0) pp++;
401
    while(!myisspace(*pp) && *pp!=0) pp++;
404
      while(myisspace(*pp)) pp++;
402
    while(myisspace(*pp)) pp++;
405
    }
403
  }
406
    return i;
404
  return i;
407
}
405
}
408
 
406
 
409
/* This is just to suppress an annoying compiler warning message. */
407
/* This is just to suppress an annoying compiler warning message. */
410
unsigned int charnum(char *p)
408
unsigned int charnum(char *p)
411
{
409
{
412
    return strlen(p);
410
  return strlen(p);
413
}
411
}
414
 
412
 
415
/* find n-th line in string p */
413
/* find n-th line in string p */
416
char *fnd_line(char *p, int n, char bf[])
414
char *fnd_line(char *p, int n, char bf[])
417
{
415
{
418
    char *pp;
416
  char *pp;
419
    int i;
417
  int i;
420
 
418
 
421
    /*
419
    /*
422
for(i=1,pp=p; pp-1!=NULL && *pp!=0 && i<n; pp=strchr(pp,'\n')+1, i++);
420
for(i=1,pp=p; pp-1!=NULL && *pp!=0 && i<n; pp=strchr(pp,'\n')+1, i++);
423
    */
421
    */
424
    for(i=1,pp=p; i<n; i++) {
422
  for(i=1,pp=p; i<n; i++) {
425
        pp=strchr(pp,'\n');
423
    pp=strchr(pp,'\n');
426
        if (!pp++) {
424
    if (!pp++) {
427
          fnd_position=NULL; fnd_nextpos=""; *bf=0; return bf;
425
      fnd_position=NULL; fnd_nextpos=""; *bf=0; return bf;
428
        }
-
 
429
    }
426
    }
-
 
427
  }
430
    fnd_position=pp;
428
  fnd_position=pp;
431
    for(i=0; *(pp+i)!=0 && *(pp+i)!='\n'; i++) *(bf+i)=*(pp+i);
429
  for(i=0; *(pp+i)!=0 && *(pp+i)!='\n'; i++) *(bf+i)=*(pp+i);
432
    *(bf+i)=0;
430
  *(bf+i)=0;
433
    if(pp[i]=='\n') i++;
431
  if(pp[i]=='\n') i++;
434
    fnd_nextpos=pp+i;
432
  fnd_nextpos=pp+i;
435
    return bf;
433
  return bf;
436
}
434
}
437
 
435
 
438
/* find n-th item in list p, comma separated */
436
/* find n-th item in list p, comma separated */
439
char *fnd_item(char *p, int n, char bf[])
437
char *fnd_item(char *p, int n, char bf[])
440
{
438
{
441
    char *pp, *pe;
439
  char *pp, *pe;
442
    int i;
440
  int i;
443
 
441
 
444
    for(i=1,pp=p; i<n && (pp==p || *(pp-1)!=0); pp=find_item_end(pp)+1, i++);
442
  for(i=1,pp=p; i<n && (pp==p || *(pp-1)!=0); pp=find_item_end(pp)+1, i++);
445
    fnd_position=pp; if(pp>p && *(pp-1)==0) {
443
  fnd_position=pp; if(pp>p && *(pp-1)==0) {
446
      fnd_position=NULL; *bf=0; return bf;
444
    fnd_position=NULL; *bf=0; return bf;
447
    }
445
  }
448
    pp=find_word_start(pp); pe=find_item_end(pp);
446
  pp=find_word_start(pp); pe=find_item_end(pp);
449
    if(*pe) fnd_nextpos=pe+1; else fnd_nextpos=pe;
447
  if(*pe) fnd_nextpos=pe+1; else fnd_nextpos=pe;
450
    while(pe>pp && myisspace(*(pe-1))) pe--;
448
  while(pe>pp && myisspace(*(pe-1))) pe--;
451
    memmove(bf,pp,pe-pp); bf[pe-pp]=0;
449
  memmove(bf,pp,pe-pp); bf[pe-pp]=0;
452
    return bf;
450
  return bf;
453
}
451
}
454
 
452
 
455
/* find n-th word in string p */
453
/* find n-th word in string p */
456
char *fnd_word(char *p, int n, char bf[])
454
char *fnd_word(char *p, int n, char bf[])
457
{
455
{
458
    char *pp;
456
  char *pp;
459
    int i;
457
  int i;
460
 
458
 
461
    for(i=1, pp=find_word_start(p); *pp!=0 && i<n ; i++) {
459
  for(i=1, pp=find_word_start(p); *pp!=0 && i<n ; i++) {
462
      while(!myisspace(*pp) && *pp!=0) pp++;
460
    while(!myisspace(*pp) && *pp!=0) pp++;
463
      pp=find_word_start(pp);
461
    pp=find_word_start(pp);
464
    }
462
  }
465
    if(*pp) fnd_position=pp; else fnd_position=NULL;
463
  if(*pp) fnd_position=pp; else fnd_position=NULL;
466
    for(i=0; *(pp+i)!=0 && !myisspace(*(pp+i)); i++) *(bf+i)=*(pp+i);
464
  for(i=0; *(pp+i)!=0 && !myisspace(*(pp+i)); i++) *(bf+i)=*(pp+i);
467
    fnd_nextpos=find_word_start(pp+i);
465
  fnd_nextpos=find_word_start(pp+i);
468
    *(bf+i)=0;
466
  *(bf+i)=0;
469
    return bf;
467
  return bf;
470
}
468
}
471
 
469
 
472
/* find n-th char in string p */
470
/* find n-th char in string p */
473
char *fnd_char(char *p, int n, char bf[])
471
char *fnd_char(char *p, int n, char bf[])
474
{
472
{
475
    int t;
473
  int t;
476
 
474
 
477
    t=strlen(p);
475
  t=strlen(p);
478
    if(n>t || n<1) {*bf=0;fnd_position=NULL; fnd_nextpos="";}
476
  if(n>t || n<1) {*bf=0;fnd_position=NULL; fnd_nextpos="";}
479
    else {
477
  else {
480
      *bf=*(p+n-1); *(bf+1)=0;
478
    *bf=*(p+n-1); *(bf+1)=0;
481
      fnd_position=p+n-1;fnd_nextpos=p+n;
479
    fnd_position=p+n-1;fnd_nextpos=p+n;
482
    }
480
  }
483
    return bf;
481
  return bf;
484
}
482
}
485
 
483
 
486
/* Returns 1 if semicolons changed to new lines */
484
/* Returns 1 if semicolons changed to new lines */
487
int rows2lines(char *p)
485
int rows2lines(char *p)
488
{
486
{
489
    char *pp, *p2;
487
  char *pp, *p2;
490
    int t;
488
  int t;
491
    if(strchr(p,'\n')!=NULL) return 0;
489
  if(strchr(p,'\n')!=NULL) return 0;
492
    for(t=0, pp=p; *pp; pp++) {
490
  for(t=0, pp=p; *pp; pp++) {
493
      if(*pp=='(') {p2=find_matching(pp+1,')'); if(p2!=NULL) pp=p2; continue;}
491
    if(*pp=='(') {p2=find_matching(pp+1,')'); if(p2!=NULL) pp=p2; continue;}
494
      if(*pp=='[') {p2=find_matching(pp+1,']'); if(p2!=NULL) pp=p2; continue;}
492
    if(*pp=='[') {p2=find_matching(pp+1,']'); if(p2!=NULL) pp=p2; continue;}
495
      if(*pp=='{') {p2=find_matching(pp+1,'}'); if(p2!=NULL) pp=p2; continue;}
493
    if(*pp=='{') {p2=find_matching(pp+1,'}'); if(p2!=NULL) pp=p2; continue;}
496
      if(*pp==';') {*pp='\n'; t++; continue;}
494
    if(*pp==';') {*pp='\n'; t++; continue;}
497
      if(*pp=='&' && myisalpha(*(pp+1))) {
495
    if(*pp=='&' && myisalpha(*(pp+1))) {
498
          for(p2=pp+1; myisalpha(*p2) && p2-pp<14; p2++);
496
      for(p2=pp+1; myisalpha(*p2) && p2-pp<14; p2++);
499
          pp=p2; continue;
-
 
500
      }
-
 
501
      if(*pp=='&' && *(pp+1)=='#') {
-
 
502
          for(p2=pp+2; myisdigit(*p2) && p2-pp<6; p2++);
-
 
503
          pp=p2; continue;
497
      pp=p2; continue;
504
      }
-
 
505
    }
498
    }
-
 
499
    if(*pp=='&' && *(pp+1)=='#') {
-
 
500
      for(p2=pp+2; myisdigit(*p2) && p2-pp<6; p2++);
-
 
501
      pp=p2; continue;
-
 
502
    }
-
 
503
  }
506
    return t;
504
  return t;
507
}
505
}
508
 
506
 
509
void lines2rows(char *p)
507
void lines2rows(char *p)
510
{
508
{
511
    char *pp;
509
  char *pp;
512
    strip_trailing_spaces(p);
510
  strip_trailing_spaces(p);
513
    for(pp=strchr(find_word_start(p),'\n'); pp!=NULL; pp=strchr(pp+1,'\n'))
511
  for(pp=strchr(find_word_start(p),'\n'); pp!=NULL; pp=strchr(pp+1,'\n'))
514
      *pp=';';
512
    *pp=';';
515
}
513
}
516
 
514
 
517
unsigned int rownum(char *p)
515
unsigned int rownum(char *p)
518
{
516
{
519
    char buf[MAX_LINELEN+1];
517
  char buf[MAX_LINELEN+1];
520
    snprintf(buf,sizeof(buf),"%s",p);
518
  snprintf(buf,sizeof(buf),"%s",p);
521
    rows2lines(buf);
519
  rows2lines(buf);
522
    return linenum(buf);
520
  return linenum(buf);
523
}
521
}
524
 
522
 
525
/* find n-th row in a matrix p */
523
/* find n-th row in a matrix p */
526
char *fnd_row(char *p, int n, char bf[])
524
char *fnd_row(char *p, int n, char bf[])
527
{
525
{
528
    rows2lines(p); return fnd_line(p,n,bf);
526
  rows2lines(p); return fnd_line(p,n,bf);
529
}
527
}
530
 
528
 
531
/* strstr but may have embedded zeros.
529
/* strstr but may have embedded zeros.
532
 * Returns memory end if not found.
530
 * Returns memory end if not found.
533
 * Supposes memory ends with 0.
531
 * Supposes memory ends with 0.
534
 */
532
 */
535
char *memstr(char *s1, char *s2, int len)
533
char *memstr(char *s1, char *s2, int len)
536
{
534
{
537
    char *p, *pp;
535
  char *p, *pp;
538
    pp=s1;
536
  pp=s1;
539
    for(p=s1; p<s1+len; p=pp) {
537
  for(p=s1; p<s1+len; p=pp) {
540
      pp=strstr(p,s2); if(pp!=NULL) break;
538
    pp=strstr(p,s2); if(pp!=NULL) break;
541
      pp=p+strlen(p);
539
    pp=p+strlen(p);
542
      while(pp<s1+len && *pp==0) pp++;
540
    while(pp<s1+len && *pp==0) pp++;
543
    }
541
  }
544
    return pp;
542
  return pp;
545
}
543
}
546
 
544
 
547
/* Check whether parentheses are balanced in a given string.
545
/* Check whether parentheses are balanced in a given string.
548
 * Returns 0 if OK.
546
 * Returns 0 if OK.
549
 */
547
 */
550
/* style=0: simple check. style<>0: strong check. */
548
/* style=0: simple check. style<>0: strong check. */
551
int check_parentheses(char *p, int style)
549
int check_parentheses(char *p, int style)
552
{
550
{
553
    int i,j,k;
551
  int i,j,k;
554
    j=strlen(p);
552
  j=strlen(p);
555
    if(j>=MAX_LINELEN) return 65535;
553
  if(j>=MAX_LINELEN) return 65535;
556
    if(style!=0) {
554
  if(style!=0) {
557
      char buf[MAX_LINELEN+1];
555
    char buf[MAX_LINELEN+1];
558
      char *pp, *pe, c;
556
    char *pp, *pe, c;
559
      for(pp=p;pp<p+j;pp++) {
557
    for(pp=p;pp<p+j;pp++) {
560
          switch (*pp) {
558
      switch (*pp) {
561
            case ')':
559
        case ')':
562
            case ']':
560
        case ']':
563
            case '}': return -1;
561
        case '}': return -1;
564
            case '(': c=')'; goto find;
562
        case '(': c=')'; goto find;
565
            case '[': c=']'; goto find;
563
        case '[': c=']'; goto find;
566
            case '{': c='}';
564
        case '{': c='}';
567
            find: {
565
        find: {
568
                pe=find_matching(pp+1,c);
566
          pe=find_matching(pp+1,c);
569
                if(pe==NULL) return 1;
567
          if(pe==NULL) return 1;
570
                memcpy(buf,pp+1,pe-pp-1);
568
          memcpy(buf,pp+1,pe-pp-1);
571
                buf[pe-pp-1]=0;
569
          buf[pe-pp-1]=0;
572
                if((k=check_parentheses(buf,1))!=0) return k;
570
          if((k=check_parentheses(buf,1))!=0) return k;
573
                else pp=pe;
571
          else pp=pe;
574
            }
572
        }
575
            default: break;
573
        default: break;
576
          }
574
      }
577
      }
575
    }
578
      return 0;
576
    return 0;
579
    }
577
  }
580
    for(i=k=0;i<j && k>=0;i++) {
578
  for(i=k=0;i<j && k>=0;i++) {
581
      if(*(p+i)=='(') k++;
579
    if(*(p+i)=='(') k++;
582
      if(*(p+i)==')') k--;
580
    if(*(p+i)==')') k--;
583
    }
581
  }
584
    return k;
582
  return k;
585
}
583
}
586
 
584
 
587
/* Strip enclosing pairs of parentheses */
585
/* Strip enclosing pairs of parentheses */
588
void strip_enclosing_par(char *p)
586
void strip_enclosing_par(char *p)
589
{
587
{
590
    char *p1;
588
  char *p1;
591
    partest: strip_trailing_spaces(p);
589
  partest: strip_trailing_spaces(p);
592
    if(*p=='(') {
590
  if(*p=='(') {
593
      p1=find_matching(p+1,')');
591
    p1=find_matching(p+1,')');
594
      if(p1 && *(p1+1)==0) {
592
    if(p1 && *(p1+1)==0) {
595
          *p1=0; ovlstrcpy(p,find_word_start(p+1));
593
      *p1=0; ovlstrcpy(p,find_word_start(p+1));
596
          goto partest;
594
      goto partest;
597
      }
595
    }
598
    }
596
  }
599
    if(*p=='[') {
597
  if(*p=='[') {
600
      p1=find_matching(p+1,']');
598
    p1=find_matching(p+1,']');
601
      if(p1 && *(p1+1)==0) {
599
    if(p1 && *(p1+1)==0) {
602
          *p1=0; ovlstrcpy(p,find_word_start(p+1));
600
      *p1=0; ovlstrcpy(p,find_word_start(p+1));
603
          goto partest;
601
      goto partest;
604
      }
-
 
605
    }
602
    }
-
 
603
  }
606
    if(*p=='{') {
604
  if(*p=='{') {
607
      p1=find_matching(p+1,'}');
605
    p1=find_matching(p+1,'}');
608
      if(p1 && *(p1+1)==0) {
606
    if(p1 && *(p1+1)==0) {
609
          *p1=0; ovlstrcpy(p,find_word_start(p+1));
607
      *p1=0; ovlstrcpy(p,find_word_start(p+1));
610
          goto partest;
608
      goto partest;
611
      }
-
 
612
    }
609
    }
-
 
610
  }
613
}
611
}
614
 
612
 
615
/* change all spaces into ' ', and collapse multiple occurences */
613
/* change all spaces into ' ', and collapse multiple occurences */
616
void singlespace(char *p)
614
void singlespace(char *p)
617
{
615
{
618
    char *pp, *pt, *p2;
616
  char *pp, *pt, *p2;
619
    for(pp=pt=p;*pp;pp++) {
617
  for(pp=pt=p;*pp;pp++) {
620
      if(!myisspace(*pp)) {*pt++=*pp; continue; }
618
    if(!myisspace(*pp)) {*pt++=*pp; continue; }
621
      *pt++=' ';
619
    *pt++=' ';
622
      for(p2=pp+1;myisspace(*p2);p2++){};
620
    for(p2=pp+1;myisspace(*p2);p2++){};
623
      pp=--p2;
621
    pp=--p2;
624
    }
622
  }
625
    *pt=0;
623
  *pt=0;
626
}
624
}
627
 
625
 
628
/* collapses all space characters in string. */
626
/* collapses all space characters in string. */
629
void nospace(char *p)
627
void nospace(char *p)
630
{
628
{
631
    char *pp, *pt;
629
  char *pp, *pt;
632
    for(pp=pt=p;*pp;pp++) if(!myismspace(*pp)) *pt++=*pp;
630
  for(pp=pt=p;*pp;pp++) if(!myismspace(*pp)) *pt++=*pp;
633
    *pt=0;
631
  *pt=0;
634
}
632
}
635
 
633
 
636
void _spaces2_(char *p, char c)
634
void _spaces2_(char *p, char c)
637
{
635
{
638
    char *pp; int n;
636
  char *pp; int n;
639
    singlespace(p);
637
  singlespace(p);
640
    n=strlen(p); if(*p==' ') {memmove(p,p+1,n);n--;}
638
  n=strlen(p); if(*p==' ') {memmove(p,p+1,n);n--;}
641
    if(n==0) return;
639
  if(n==0) return;
642
    if(p[n-1]==' ') p[n-1]=0;
640
  if(p[n-1]==' ') p[n-1]=0;
643
    for(pp=strchr(p,' '); pp; pp=strchr(pp,' ')) *pp++=c;
641
  for(pp=strchr(p,' '); pp; pp=strchr(pp,' ')) *pp++=c;
644
}
642
}
645
/* change words to items */
643
/* change words to items */
646
void words2items(char *p)
644
void words2items(char *p)
-
 
645
{
647
{   _spaces2_(p,','); }
646
  _spaces2_(p,',');
-
 
647
}
648
 
648
 
649
/* change words to lines */
649
/* change words to lines */
650
void words2lines(char *p)
650
void words2lines(char *p)
-
 
651
{
651
{   _spaces2_(p,'\n'); }
652
  _spaces2_(p,'\n');
-
 
653
}
652
 
654
 
653
/* change lines to items */
655
/* change lines to items */
654
void lines2items(char *p)
656
void lines2items(char *p)
655
{
657
{
656
    char *pp;
658
  char *pp;
657
    for(pp=strchr(p,'\n'); pp; pp=strchr(pp,'\n')) *pp++=',';
659
  for(pp=strchr(p,'\n'); pp; pp=strchr(pp,'\n')) *pp++=',';
658
}
660
}
659
 
661
 
660
/* change lines to words */
662
/* change lines to words */
661
void lines2words(char *p)
663
void lines2words(char *p)
662
{
664
{
663
    char *pp;
665
  char *pp;
664
    for(pp=strchr(p,'\n'); pp; pp=strchr(pp,'\n')) *pp++=' ';
666
  for(pp=strchr(p,'\n'); pp; pp=strchr(pp,'\n')) *pp++=' ';
665
}
667
}
666
 
668
 
667
/* change items to words */
669
/* change items to words */
668
void items2words(char *p)
670
void items2words(char *p)
669
{
671
{
670
    char *pp;
672
  char *pp;
671
    for(pp=strparstr(p,",");*pp;pp=strparstr(pp+1,",")) *pp=' ';
673
  for(pp=strparstr(p,",");*pp;pp=strparstr(pp+1,",")) *pp=' ';
672
}
674
}
673
 
675
 
674
/* change items to lines */
676
/* change items to lines */
675
void items2lines(char *p)
677
void items2lines(char *p)
676
{
678
{
677
    char *pp;
679
  char *pp;
678
    for(pp=strparstr(p,",");*pp;pp=strparstr(pp+1,",")) *pp='\n';
680
  for(pp=strparstr(p,",");*pp;pp=strparstr(pp+1,",")) *pp='\n';
679
}
681
}
680
#pragma GCC diagnostic push
682
#pragma GCC diagnostic push
681
#pragma GCC diagnostic ignored "-Winvalid-source-encoding"
683
#pragma GCC diagnostic ignored "-Winvalid-source-encoding"
682
char *acctab="çéèêëúùûüáàâäãóòôöõíìïîñýÿÇÉÈÊËÚÙÛÜÁÀÂÃÄÓÒÔÖÕÍÌÏÎÑÝ",
684
char *acctab="çéèêëúùûüáàâäãóòôöõíìïîñýÿÇÉÈÊËÚÙÛÜÁÀÂÃÄÓÒÔÖÕÍÌÏÎÑÝ",
683
     *deatab="ceeeeuuuuaaaaaoooooiiiinyyCEEEEUUUUAAAAAOOOOOIIIINY";
685
     *deatab="ceeeeuuuuaaaaaoooooiiiinyyCEEEEUUUUAAAAAOOOOOIIIINY";
684
 
686
 
685
/* fold accented letters to unaccented */
687
/* fold accented letters to unaccented */
686
void deaccent(char *p)
688
void deaccent(char *p)
687
{
689
{
688
    char *sp;
690
  char *sp;
689
    char *v;
691
  char *v;
690
    for(sp=p;*sp;sp++) {
692
  for(sp=p;*sp;sp++) {
691
      if(*sp<0 && (v=strchr(acctab,*sp))!=NULL)
693
    if(*sp<0 && (v=strchr(acctab,*sp))!=NULL)
692
        *sp=*(deatab+(v-acctab));
694
      *sp=*(deatab+(v-acctab));
693
    }
695
  }
694
}
696
}
695
 
697
 
696
char *reaccents="'`\"^~";
698
char *reaccents="'`\"^~";
697
char *reaccentl="aeiouycnAEIOUYCN";
699
char *reaccentl="aeiouycnAEIOUYCN";
698
char *reaccentab="\
700
char *reaccentab="\
Line 700... Line 702...
700
ÁÀÄÂÃÉÈËÊEÍÌÏÎIÓÒÖÔÕÚÙÜÛUÝYYYYCCCCÇNNNNÑ";
702
ÁÀÄÂÃÉÈËÊEÍÌÏÎIÓÒÖÔÕÚÙÜÛUÝYYYYCCCCÇNNNNÑ";
701
#pragma GCC diagnostic pop
703
#pragma GCC diagnostic pop
702
/* compose accent using symbol keys */
704
/* compose accent using symbol keys */
703
void reaccent(char *p)
705
void reaccent(char *p)
704
{
706
{
705
    char *sp, *ap, c;
707
  char *sp, *ap, c;
706
    int i, k;
708
  int i, k;
707
    if(*p==0) return;
709
  if(*p==0) return;
708
    for(sp=p+1; *sp; sp++) {
710
  for(sp=p+1; *sp; sp++) {
709
      ap=strchr(reaccents,*sp); if(ap==NULL) continue;
711
    ap=strchr(reaccents,*sp); if(ap==NULL) continue;
710
      i=ap-reaccents;
712
    i=ap-reaccents;
711
      sp--; ap=strchr(reaccentl,*sp); if(ap==NULL) {sp++; continue;}
713
    sp--; ap=strchr(reaccentl,*sp); if(ap==NULL) {sp++; continue;}
712
      k=ap-reaccentl;
714
    k=ap-reaccentl;
713
      c=reaccentab[k*strlen(reaccents)+i];
715
    c=reaccentab[k*strlen(reaccents)+i];
714
      if(c!=*sp) {*sp=c; ovlstrcpy(sp+1,sp+2);}
716
    if(c!=*sp) {*sp=c; ovlstrcpy(sp+1,sp+2);}
715
      else sp++;
717
    else sp++;
716
    }
718
  }
717
}
719
}
718
 
720
 
719
/* modify a string. Bufferlen must be at least MAX_LINELEN */
721
/* modify a string. Bufferlen must be at least MAX_LINELEN */
720
void string_modify1(char *start, char *bad_beg, char *bad_end, char *good,...)
722
void string_modify1(char *start, char *bad_beg, char *bad_end, char *good,...)
721
{
723
{
722
    char buf[MAX_LINELEN+1];
724
  char buf[MAX_LINELEN+1];
723
    int ln, le;
725
  int ln, le;
724
    va_list vp;
726
  va_list vp;
725
 
727
 
726
    va_start(vp,good);
728
  va_start(vp,good);
727
    vsnprintf(buf,sizeof(buf),good,vp); va_end(vp);
729
  vsnprintf(buf,sizeof(buf),good,vp); va_end(vp);
728
    ln=strlen(buf); le=strlen(bad_end);
730
  ln=strlen(buf); le=strlen(bad_end);
729
    if(ln+le+(bad_beg-start)>=MAX_LINELEN) {
731
  if(ln+le+(bad_beg-start)>=MAX_LINELEN) {
730
      error("string_too_long"); return;
732
    error("string_too_long"); return;
731
    }
733
  }
732
    if(ln!=bad_end-bad_beg) memmove(bad_beg+ln,bad_end,le+1);
734
  if(ln!=bad_end-bad_beg) memmove(bad_beg+ln,bad_end,le+1);
733
    memmove(bad_beg,buf,ln);
735
  memmove(bad_beg,buf,ln);
734
}
736
}
735
 
737
 
736
/*  modify a string. Bufferlen must be at least MAX_LINELEN */
738
/*  modify a string. Bufferlen must be at least MAX_LINELEN */
737
void string_modify3(char *start, char *bad_beg, char *bad_end, char *good,...)
739
void string_modify3(char *start, char *bad_beg, char *bad_end, char *good,...)
738
{
740
{
739
    char buf[MAX_LINELEN+1];
741
  char buf[MAX_LINELEN+1];
740
    va_list vp;
742
  va_list vp;
741
 
743
 
742
    va_start(vp,good);
744
  va_start(vp,good);
743
    vsnprintf(buf,sizeof(buf),good,vp); va_end(vp);
745
  vsnprintf(buf,sizeof(buf),good,vp); va_end(vp);
744
    if(strlen(start)-(bad_end-bad_beg)+strlen(buf)>=MAX_LINELEN)
746
  if(strlen(start)-(bad_end-bad_beg)+strlen(buf)>=MAX_LINELEN)
745
      return; /* this is an error situation. */
747
    return; /* this is an error situation. */
746
    strcat(buf,bad_end);
748
  strcat(buf,bad_end);
747
    ovlstrcpy(bad_beg,buf);
749
  ovlstrcpy(bad_beg,buf);
748
}
750
}
749
 
751
 
750
/* returns number of bytes written */
752
/* returns number of bytes written */
751
int catfile(FILE *outf, char *fn,...)
753
int catfile(FILE *outf, char *fn,...)
752
{
754
{
753
    char buf[4096];
755
  char buf[4096];
754
    va_list vp;
756
  va_list vp;
755
    int l,tot,fd;
757
  int l,tot,fd;
756
 
758
 
757
    tot=0;
759
  tot=0;
758
    va_start(vp,fn);
760
  va_start(vp,fn);
759
    vsnprintf(buf,sizeof(buf),fn,vp); va_end(vp);
761
  vsnprintf(buf,sizeof(buf),fn,vp); va_end(vp);
760
    fd=open(buf,O_RDONLY); if(fd==-1) return 0;
762
  fd=open(buf,O_RDONLY); if(fd==-1) return 0;
761
    for(l=read(fd,buf,4096);l>0 && l<=4096; l=read(fd,buf,4096)) {
763
  for(l=read(fd,buf,4096);l>0 && l<=4096; l=read(fd,buf,4096)) {
762
      fwrite(buf,1,l,outf); tot+=l;
764
    fwrite(buf,1,l,outf); tot+=l;
763
    }
765
  }
764
    close(fd);
766
  close(fd);
765
    return tot;
767
  return tot;
766
}
768
}
767
 
769
 
768
/* returns -1 if error */
770
/* returns -1 if error */
769
long int filelength(char *fn,...)
771
long int filelength(char *fn,...)
770
{
772
{
771
    char buf[MAX_FNAME+1];
773
  char buf[MAX_FNAME+1];
772
    va_list vp;
774
  va_list vp;
773
    struct stat st;
775
  struct stat st;
774
    int l;
776
  int l;
775
 
777
 
776
    va_start(vp,fn);
778
  va_start(vp,fn);
777
    vsnprintf(buf,sizeof(buf),fn,vp); va_end(vp);
779
  vsnprintf(buf,sizeof(buf),fn,vp); va_end(vp);
778
    l=stat(buf,&st); if(l) return -1;
780
  l=stat(buf,&st); if(l) return -1;
779
    return st.st_size;
781
  return st.st_size;
780
}
782
}
781
 
783
 
782
char *parend(char *p)
784
char *parend(char *p)
783
{
785
{
784
    char *pp; int t;
786
  char *pp; int t;
785
    t=0; for(pp=p;*pp;pp++) {
787
  t=0; for(pp=p;*pp;pp++) {
786
      if(*pp=='(') t++;
788
    if(*pp=='(') t++;
787
      if(*pp==')') {t--; if(t==0) return pp; if(t<0) return NULL;}
789
    if(*pp==')') {t--; if(t==0) return pp; if(t<0) return NULL;}
788
    }
790
  }
789
    return NULL;
791
  return NULL;
790
}
792
}
791
 
793
 
792
char *bufprep(char *p)
794
char *bufprep(char *p)
793
{
795
{
794
/*  singlespace(p); strip_trailing_spaces(p); return find_word_start(p); */
796
/*  singlespace(p); strip_trailing_spaces(p); return find_word_start(p); */
795
    nospace(p); return p;
797
  nospace(p); return p;
796
}
798
}