~ubuntu-branches/ubuntu/warty/swish-e/warty

« back to all changes in this revision

Viewing changes to src/string.c

  • Committer: Bazaar Package Importer
  • Author(s): Ludovic Drolez
  • Date: 2004-03-11 08:41:07 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040311084107-7vp0mu82blq1qjvo
Tags: 2.4.1-3
Oops ! A comment was not removed to disable interactive compilation.
Closes: Bug#237332

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
** Copyright (C) 1995, 1996, 1997, 1998 Hewlett-Packard Company
3
 
** Originally by Kevin Hughes, kev@kevcom.com, 3/11/94
4
 
**
5
 
** This program and library is free software; you can redistribute it and/or
6
 
** modify it under the terms of the GNU (Library) General Public License
7
 
** as published by the Free Software Foundation; either version 2
8
 
** of the License, or any later version.
9
 
**
10
 
** This program is distributed in the hope that it will be useful,
11
 
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 
** GNU (Library) General Public License for more details.
14
 
**
15
 
** You should have received a copy of the GNU (Library) General Public License
16
 
** along with this program; if not, write to the Free Software
17
 
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
 
**---------------------------------------------------------
19
 
** ** ** PATCHED 5/13/96, CJC
20
 
** Added MatchAndChange for regex in replace rule G.Hill 2/10/98
21
 
**
22
 
** change sprintf to snprintf to avoid corruption
23
 
** added safestrcpy() macro to avoid corruption from strcpy overflow
24
 
** SRE 11/17/99
25
 
**
26
 
** fixed cast to int problems pointed out by "gcc -Wall"
27
 
** SRE 2/22/00
28
 
**
29
 
**
30
 
*/
31
 
 
32
 
#include "swish.h"
33
 
#include "string.h"
34
 
#include "mem.h"
35
 
 
36
 
/* My own case-insensitive strstr().
37
 
*/
38
 
 
39
 
char *lstrstr(s, t)
40
 
char *s;
41
 
char *t;
42
 
{
43
 
        int i, j, k, l;
44
 
        
45
 
        for (i = 0; s[i]; i++) {
46
 
                for (j = 0, l = k = i; s[k] && t[j] &&
47
 
                        tolower(s[k]) == tolower(t[j]); j++, k++)
48
 
                        ;
49
 
                if (t[j] == '\0')
50
 
                        return s + l;
51
 
        }
52
 
        return NULL;
53
 
}
54
 
 
55
 
/* Gets the next word in a line. If the word's in quotes,
56
 
** include blank spaces in the word or phrase.
57
 
*/
58
 
 
59
 
char *getword(line, skiplen)
60
 
char *line;
61
 
int *skiplen;
62
 
{
63
 
        int i, inquotes;
64
 
        char *start,*p;
65
 
        static int lenword=0;
66
 
        static char *word=NULL;
67
 
        
68
 
        if(!lenword) word = (char *)emalloc((lenword=MAXWORDLEN) + 1);
69
 
        start = line;
70
 
        if (!(*line))
71
 
                return "\0";
72
 
        while (isspace((int)*line)) line++;
73
 
        if (!(*line))
74
 
                return "\0";
75
 
        if (*line == '\"') {
76
 
                inquotes = 1;
77
 
                line++;
78
 
        }
79
 
        else
80
 
                inquotes = 0;
81
 
        for (i = 0; *line && ((inquotes) ? (*line != '\"') : (!isspace((int)*line))); line++) {
82
 
                if(i==lenword) {
83
 
                        lenword *=2;
84
 
                        word = (char *)erealloc(word,lenword+1);
85
 
                }
86
 
                word[i++] = *line;
87
 
        }
88
 
        word[i] = '\0';
89
 
 
90
 
        if (!(*line) && i)
91
 
                 if ((p=strpbrk(word,"\r\n"))) *p='\0';
92
 
 
93
 
        if (*line == '\"')
94
 
                line++;
95
 
        
96
 
        *skiplen = line - start;
97
 
        
98
 
        return word;
99
 
}
100
 
 
101
 
/* Gets the value of a variable in a line of the configuration file.
102
 
** Basically, anything in quotes or an argument to a variable.
103
 
*/
104
 
 
105
 
char *getconfvalue(line, var)
106
 
char *line;
107
 
char *var;
108
 
{
109
 
        int i;
110
 
        char *c;
111
 
        static int lentmpvalue=0;
112
 
        static char *tmpvalue=NULL;
113
 
        
114
 
        if(!lentmpvalue) tmpvalue = (char *) emalloc((lentmpvalue=MAXSTRLEN) + 1);
115
 
        if ((c = (char *) lstrstr(line, var)) != NULL) {
116
 
                if (c != line)
117
 
                        return NULL;
118
 
                c += strlen(var);
119
 
                while (isspace((int)*c) || *c == '\"')
120
 
                        c++;
121
 
                if (*c == '\0')
122
 
                        return NULL;
123
 
                for (i = 0; *c != '\0' && *c != '\"' && *c != '\n' && *c!= '\r' ; c++) {
124
 
                        if(i==lentmpvalue) {
125
 
                                lentmpvalue *=2;
126
 
                                tmpvalue= (char *) erealloc(tmpvalue,lentmpvalue +1);
127
 
                        }
128
 
                        tmpvalue[i++] = *c;
129
 
                }
130
 
                tmpvalue[i] = '\0';
131
 
                return tmpvalue;
132
 
        }
133
 
        else
134
 
                return NULL;
135
 
}
136
 
 
137
 
/* Extracts anything in <title> tags from an HTML file and returns it.
138
 
** Otherwise, only the file name without its path is returned.
139
 
*/
140
 
 
141
 
char *parsetitle(filename, alttitle)
142
 
char *filename;
143
 
char *alttitle;
144
 
{
145
 
        register int c, d;
146
 
        register char *p,*q;
147
 
        char *tag;
148
 
        static int lentitle=0;
149
 
        static char *title=NULL;
150
 
        static int lenshorttitle=0;
151
 
        static char *shorttitle=NULL;
152
 
        int i, j, lines, status, tagbuflen, totaltaglen, curlentitle;
153
 
        FILE *fp;
154
 
        
155
 
        if(!lentitle) title = (char *) emalloc((lentitle=MAXTITLELEN) +1);
156
 
        if(!lenshorttitle) shorttitle = (char *) emalloc((lenshorttitle=MAXTITLELEN) +1);
157
 
        tag = (char *) emalloc(1);
158
 
        tag[0] = '\0';
159
 
        lines = status = 0;
160
 
        p = title;
161
 
        *p = '\0';
162
 
        
163
 
        if ((q=strrchr(alttitle, '/'))) 
164
 
                q++;
165
 
        else
166
 
                q=alttitle;
167
 
        shorttitle = SafeStrCopy(shorttitle,q,&lenshorttitle);
168
 
        
169
 
        fp = fopen(filename, "r");
170
 
        if (fp == NULL) {
171
 
                efree(tag);
172
 
                return shorttitle;
173
 
        }
174
 
        for (; lines < TITLETOPLINES ; ) {
175
 
                c = getc(fp);
176
 
                if (c == '\n')
177
 
                        lines++;
178
 
                if (feof(fp)) {
179
 
                        fclose(fp);
180
 
                        efree(tag);
181
 
                        return shorttitle;
182
 
                }
183
 
                switch(c) {
184
 
                case '<':
185
 
                        
186
 
                        efree(tag);
187
 
                        tag = (char *) emalloc((tagbuflen=MAXSTRLEN)+1);
188
 
                        totaltaglen = 0;
189
 
                        tag[totaltaglen++] = '<';
190
 
                        status = TI_OPEN;
191
 
                        
192
 
                        while (1) {
193
 
                                d = getc(fp);
194
 
                                if (d == EOF) {
195
 
                                        fclose(fp);
196
 
                                        efree(tag);
197
 
                                        return shorttitle;
198
 
                                }
199
 
                                if(totaltaglen==tagbuflen) {
200
 
                                        tagbuflen+=200;
201
 
                                        tag=erealloc(tag,tagbuflen+1);
202
 
                                }
203
 
                                tag[totaltaglen++] = d;
204
 
                                if (d == '>') {
205
 
                                        tag[totaltaglen]='\0';  
206
 
                                        break;
207
 
                                }
208
 
                        }
209
 
                        
210
 
                        if (lstrstr(tag, "</title>")) {
211
 
                                status = TI_CLOSE;
212
 
                                *p = '\0';
213
 
                                fclose(fp);
214
 
                                for (i = 0; title[i]; i++)
215
 
                                        if (title[i] == '\n')
216
 
                                        title[i] = ' ';
217
 
                                for (i = 0; isspace((int)title[i]) ||
218
 
                                        title[i] == '\"'; i++)
219
 
                                        ;
220
 
                                for (j = 0; title[i]; j++)
221
 
                                        title[j] = title[i++];
222
 
                                title[j] = '\0';
223
 
                                for (j = strlen(title) - 1;
224
 
                                (j && isspace((int)title[j]))
225
 
                                        || title[j] == '\0' || title[j] == '\"'; j--)
226
 
                                        title[j] = '\0';
227
 
                                for (j = 0; title[j]; j++)
228
 
                                        if (title[j] == '\"')
229
 
                                        title[j] = '\'';
230
 
                                efree(tag);
231
 
                                return *title ? title : shorttitle;
232
 
                        }
233
 
                        else {
234
 
                                if (lstrstr(tag, "<title>"))
235
 
                                        status = TI_FOUND;
236
 
                        }
237
 
                        break;
238
 
                default:
239
 
                        if (status == TI_FOUND) {
240
 
                                curlentitle=p-title;
241
 
                                if(curlentitle==lentitle) {
242
 
                                        lentitle *=2;
243
 
                                        title = (char *)erealloc(title,lentitle +1);
244
 
                                        p = title + curlentitle;
245
 
                                }
246
 
                                *p = c;
247
 
                                p++;
248
 
                        }
249
 
                        else {
250
 
                                if (status == TI_CLOSE) {
251
 
                                        fclose(fp);
252
 
                                        efree(tag);
253
 
                                        return shorttitle;
254
 
                                }
255
 
                        }
256
 
                }
257
 
        }
258
 
        fclose(fp);
259
 
        efree(tag);
260
 
        return shorttitle;
261
 
}
262
 
 
263
 
/* Is a character a valid word character?
264
 
*/
265
 
 
266
 
/* Old version - Now is a macro with a lookuptable for better performance
267
 
int iswordchar(char c)
268
 
{
269
 
        int d, i;
270
 
        
271
 
        d = tolower(c);
272
 
 
273
 
        for (i = 0; wordchars[i] != '\0'; i++)
274
 
        {
275
 
                if ((char)d == wordchars[i])
276
 
                        return 1;
277
 
        }
278
 
        return 0;
279
 
}
280
 
*/
281
 
 
282
 
/* In a string, replaces all occurrences of "oldpiece" with "newpiece".
283
 
** This is not really bulletproof yet.
284
 
*/
285
 
/* 05/00 Jose Ruiz 
286
 
** Totally rewritten
287
 
*/
288
 
char *replace(string, oldpiece, newpiece)
289
 
     char *string;
290
 
     char *oldpiece;
291
 
     char *newpiece;
292
 
{
293
 
        int limit, curpos, lennewpiece, lenoldpiece, curnewlen;
294
 
        char *c, *p, *q;
295
 
        static int lennewstring=0;
296
 
        static char *newstring=NULL;
297
 
 
298
 
        if(!lennewstring) newstring = (char *) emalloc((lennewstring=MAXSTRLEN) + 1);
299
 
        
300
 
        lennewpiece = strlen(newpiece);
301
 
        lenoldpiece = strlen(oldpiece);
302
 
        c = string;
303
 
        q = newstring;
304
 
        curnewlen = 0;
305
 
        while ((p = (char *) strstr(c, oldpiece))) {
306
 
                limit = p - c;
307
 
                curnewlen += (limit + lennewpiece);
308
 
                if(curnewlen > lennewstring) {
309
 
                        curpos = q - newstring;
310
 
                        lennewstring = curnewlen + 200;
311
 
                        newstring = (char *) erealloc(newstring,lennewstring+1);
312
 
                        q=newstring+curpos;
313
 
                }
314
 
                memcpy(q,c,limit);
315
 
                q+=limit;
316
 
                memcpy(q,newpiece,lennewpiece);
317
 
                q+=lennewpiece;
318
 
                c=p+lenoldpiece;
319
 
        }
320
 
        curnewlen +=strlen(c);
321
 
        if(curnewlen > lennewstring) {
322
 
                curpos = q - newstring;
323
 
                lennewstring = curnewlen + 200;
324
 
                newstring = (char *) erealloc(newstring,lennewstring+1);
325
 
                q=newstring+curpos;
326
 
        }
327
 
        strcpy(q,c);
328
 
        return newstring;
329
 
}
330
 
 
331
 
/* Just for A.P. and K.H. 2/5/98 by G.Hill - not really used now */
332
 
char* replaceWild (char* fileName, char* pattern, char* subs)
333
 
{
334
 
        int i;
335
 
        for (i = 0; pattern[i] != '*' && fileName[i] != '\0'; i++) 
336
 
    {
337
 
                if (fileName[i] != pattern[i])
338
 
                        return fileName;
339
 
    }
340
 
        return subs;
341
 
}
342
 
 
343
 
/* Like strcmp(), but the order of sorting the first char is
344
 
** determined by the order of the characters in the wordchars array.
345
 
*/
346
 
 
347
 
/* Obsolete and slow
348
 
int wordcompare(s1, s2)
349
 
     char *s1;
350
 
     char *s2;
351
 
{
352
 
        register int i, j;
353
 
        
354
 
        if (s1[0] != s2[0]) {
355
 
                for (i = 0; wordchars[i] != '\0'; i++)
356
 
                        if (s1[0] == wordchars[i])
357
 
                        break;
358
 
                for (j = 0; wordchars[j] != '\0'; j++)
359
 
                {
360
 
                        if (s2[0] == wordchars[j])
361
 
                                break;
362
 
                }
363
 
                if (i < j)
364
 
                        return -1;
365
 
                else
366
 
                        return 1;
367
 
        }
368
 
        else
369
 
                return strcmp(s1, s2);
370
 
}
371
 
*/
372
 
 
373
 
/* This converts HTML numbered entities (such as &#169;)
374
 
** to strings (like &copy;). Much is this function is
375
 
** simply adding semicolons in the right places.
376
 
** This and the functions it calls are not very fast
377
 
** and could be made faster.
378
 
*/
379
 
 
380
 
char *convertentities(s)
381
 
char *s;
382
 
{
383
 
        int i, lens, skip;
384
 
        static int lenent=0;
385
 
        static char *ent=NULL;
386
 
        static int lennewword=0;
387
 
        static char *newword=NULL;
388
 
        char *newwordconvert;
389
 
        char *p,*q;
390
 
 
391
 
        if(!lennewword) newword = (char *) emalloc((lennewword=MAXWORDLEN) +1);
392
 
        if(!lenent) ent = (char *) emalloc((lenent=MAXENTLEN) +1);
393
 
 
394
 
        if (!(p=strchr(s, '&'))) return s;
395
 
        lens = strlen(s);
396
 
        if(lens>lennewword) {
397
 
                lennewword = lens + 200;
398
 
                newword = (char *) erealloc(newword,lennewword + 1);
399
 
        }
400
 
        if ((int)strlen(s) > maxwordlimit) return s;
401
 
        q=newword;
402
 
        memcpy(q,s,p-s);
403
 
        q +=(p-s);
404
 
 
405
 
        for (s=p; *s != '\0'; s++) {
406
 
                if (*s == '&') {
407
 
                        ent = SafeStrCopy(ent,getent(s, &skip),&lenent);
408
 
                        if (ent[0] == '\0') {
409
 
                                *q++='&';                       
410
 
                                continue;
411
 
                        }
412
 
                        s += skip;
413
 
                        if (*s == ';') s++;
414
 
                        i=strlen(ent);
415
 
                        memcpy(q,ent,i);
416
 
                        q+=i;
417
 
                        *q++=';';
418
 
                        s--;
419
 
                }
420
 
                else
421
 
                        *q++=*s;
422
 
        }
423
 
        *q='\0';
424
 
 
425
 
        if (ASCIIENTITIES) { 
426
 
                        /* Jose Ruiz 06/00 Do not call to converttonamed
427
 
                        ** here. convertoascii do all the work 
428
 
                        */
429
 
                newwordconvert = (char *) converttoascii(newword);
430
 
                newword = SafeStrCopy(newword,newwordconvert,&lennewword);
431
 
        } else {
432
 
                newwordconvert = (char *) converttonamed(newword);
433
 
                newword = SafeStrCopy(newword,newwordconvert,&lennewword);
434
 
        }
435
 
        
436
 
        return newword;
437
 
}
438
 
 
439
 
/* Returns a matching entity that matches the beginning of a string, if any.
440
 
*/
441
 
 
442
 
char *getent(s, skip)
443
 
char *s;
444
 
int *skip;
445
 
{
446
 
        int i;
447
 
        static int lenent=0;
448
 
        static char *ent=NULL;
449
 
        static int lentestent=0;
450
 
        static char *testent=NULL;
451
 
 
452
 
        if(!lenent) ent = (char *)emalloc((lenent=MAXENTLEN) +1);
453
 
        if(!lentestent) testent = (char *)emalloc((lentestent=MAXENTLEN) +1);
454
 
 
455
 
        *skip = 0;
456
 
        ent = SafeStrCopy(ent,s,&lenent);
457
 
        if (ent[1] == '#') {
458
 
                if (isdigit((int)ent[5]))
459
 
                        return "\0";
460
 
                for (i = 2; ent[i] != '\0' && isdigit((int)ent[i]); i++)
461
 
                        ;
462
 
                while (ent[i] != '\0' && !isdigit((int)ent[i]))
463
 
                        ent[i++] = '\0';
464
 
                *skip = strlen(ent);
465
 
                return ent;
466
 
        }
467
 
        else {
468
 
                for (i = 0; entities[i] != NULL; i += 3) {
469
 
                        testent = SafeStrCopy(testent, entities[i],&lentestent);
470
 
                        if (testent[0] != '\0') {
471
 
                                if (!strncmp(testent, ent, strlen(testent))) {
472
 
                                        ent = SafeStrCopy(ent, testent,&lenent);
473
 
                                        *skip = strlen(ent);
474
 
                                        return ent;
475
 
                                }
476
 
                        }
477
 
                }
478
 
        }
479
 
        
480
 
        return "\0";
481
 
}
482
 
 
483
 
/* This is the real function called by convertentities() that
484
 
** changes numbered to named entities.
485
 
*/
486
 
 
487
 
char *converttonamed(s)
488
 
char *s;
489
 
{
490
 
        int i, hasnumbered, ilen;
491
 
        static int lentestent=0;
492
 
        static char *testent=NULL;
493
 
        static int lennewent=0;
494
 
        static char *newent=NULL;
495
 
        static int lennewword=0;
496
 
        static char *newword=NULL;
497
 
        char *newwordreplaced;
498
 
        
499
 
        if(!lennewword) newword = (char *) emalloc((lennewword=MAXWORDLEN) + 1);
500
 
        if(!lentestent) testent = (char *) emalloc((lentestent=MAXENTLEN) + 1);
501
 
        if(!lennewent) newent = (char *) emalloc((lennewent=MAXENTLEN) + 1);
502
 
        
503
 
        newword=SafeStrCopy(newword, s,&lennewword);
504
 
        do {
505
 
           for (i = 0, hasnumbered = 0; entities[i] != NULL; i += 3) {
506
 
                ilen=strlen(entities[i+1]);
507
 
                if((ilen+1)>=lentestent) {
508
 
                        lentestent=ilen+1+100;
509
 
                        testent=erealloc(testent,lentestent+1);
510
 
                }
511
 
                memcpy(testent, entities[i + 1],ilen);
512
 
                testent[ilen]=';';
513
 
                testent[ilen+1]='\0';
514
 
                if (strstr(newword, testent) != NULL &&
515
 
                        (entities[i])[0] != '\0') {
516
 
                        hasnumbered=1;
517
 
                        ilen=strlen(entities[i]);
518
 
                        if((ilen+1)>=lennewent) {
519
 
                                lennewent=ilen+1+100;
520
 
                                newent=erealloc(newent,lennewent+1);
521
 
                        }
522
 
                        memcpy(newent,entities[i],ilen);
523
 
                        newent[ilen]=';';
524
 
                        newent[ilen+1]='\0';
525
 
                        newwordreplaced = (char *) replace(newword, testent, newent);
526
 
                        newword=SafeStrCopy(newword,newwordreplaced,&lennewword);
527
 
                        strcpy(newword,newwordreplaced);
528
 
                }
529
 
           }
530
 
        } while (hasnumbered);
531
 
        return newword;
532
 
}
533
 
 
534
 
/* This function converts all convertable named and numbered
535
 
** entities to their ASCII equivalents, if they exist.
536
 
*/
537
 
 
538
 
char *converttoascii(s)
539
 
char *s;
540
 
{
541
 
        int i, hasnonascii,ilen;
542
 
        char *c, *d;
543
 
        static int lenwrdent=0;
544
 
        static char *wrdent=NULL;
545
 
        static int lennument=0;
546
 
        static char *nument=NULL;
547
 
        static int lennewword=0;
548
 
        static char *newword=NULL;
549
 
        char *newwordreplaced;
550
 
 
551
 
        if(!lennewword) newword = (char *) emalloc((lennewword=MAXWORDLEN) + 1);
552
 
        if(!lenwrdent) wrdent = (char *) emalloc((lenwrdent=MAXENTLEN) + 1);
553
 
        if(!lennument) nument = (char *) emalloc((lennument=MAXENTLEN) + 1);
554
 
        
555
 
        newword=SafeStrCopy(newword, s, &lennewword);
556
 
        
557
 
        do {
558
 
           for (i = 0, hasnonascii = 0; entities[i] != NULL; i += 3) {
559
 
                ilen=strlen(entities[i]);
560
 
                if((ilen+1)>=lenwrdent) {
561
 
                        lenwrdent=ilen+1+200;
562
 
                        wrdent=erealloc(wrdent,lenwrdent+1);
563
 
                }
564
 
                memcpy(wrdent,entities[i],ilen);
565
 
                wrdent[ilen]=';';
566
 
                wrdent[ilen+1]='\0';
567
 
                ilen=strlen(entities[i+1]);
568
 
                if((ilen+1)>=lennument) {
569
 
                        lennument=ilen+1+200;
570
 
                        nument=erealloc(nument,lennument+1);
571
 
                }
572
 
                memcpy(nument,entities[i+1],ilen);
573
 
                nument[ilen]=';';
574
 
                nument[ilen+1]='\0';
575
 
                
576
 
                c = d = NULL;
577
 
                if ((entities[i])[0] != '\0')
578
 
                        c = (char *) strstr(newword, wrdent);
579
 
                if ((entities[i + 1])[0] != '\0')
580
 
                        d = (char *) strstr(newword, nument);
581
 
                if ((entities[i + 2])[0] != '\0' && (c!=NULL || d!=NULL)) {
582
 
                        hasnonascii=1;
583
 
                        if (c != NULL && d==NULL) { 
584
 
                                newwordreplaced = (char *) replace(newword, wrdent, entities[i + 2]); 
585
 
                                newword=SafeStrCopy(newword,newwordreplaced,&lennewword);
586
 
                        } else if (d != NULL && c==NULL) { 
587
 
                                newwordreplaced = (char *) replace(newword, nument, entities[i + 2]); 
588
 
                                newword=SafeStrCopy(newword,newwordreplaced,&lennewword);
589
 
                        } else {
590
 
                                newwordreplaced = (char *) replace(newword, wrdent, entities[i + 2]); 
591
 
                                newword=SafeStrCopy(newword,newwordreplaced,&lennewword);
592
 
                                newwordreplaced = (char *) replace(newword, nument, entities[i + 2]); 
593
 
                                newword=SafeStrCopy(newword,newwordreplaced,&lennewword);
594
 
                        }
595
 
                }
596
 
           }
597
 
        } while (hasnonascii);
598
 
        
599
 
        return newword;
600
 
}
601
 
 
602
 
/* That regular expression matching and replacing thing */
603
 
char * matchAndChange (char *str, char *pattern, char *subs) 
604
 
{
605
 
        int  status, lenSub, lenBeg, lenTmp;
606
 
        regex_t re;
607
 
        regmatch_t pmatch[MAXPAR];
608
 
        char *tmpstr;
609
 
        char begin[MAXSTRLEN];
610
 
        static int lennewstr=0;
611
 
        static char *newstr=NULL;
612
 
        static int lenoldStr=0;
613
 
        static char *oldStr=NULL;
614
 
 
615
 
        if(!lenoldStr) oldStr = (char *)emalloc((lenoldStr=MAXSTRLEN) +1);
616
 
        if(!lennewstr) newstr = (char *)emalloc((lennewstr=MAXSTRLEN) +1);
617
 
        
618
 
        /* Save the old string just in case */
619
 
        oldStr = SafeStrCopy(oldStr,str,&lenoldStr);
620
 
        
621
 
        status = regcomp(&re, pattern, REG_EXTENDED);
622
 
        if ( status != 0) {
623
 
                regfree(&re); /* Richard Beebe */
624
 
                return oldStr;
625
 
        }
626
 
        
627
 
        status = regexec(&re,str,(size_t)MAXPAR,pmatch,0);
628
 
        if (status != 0) {
629
 
                regfree(&re); /* Richard Beebe */
630
 
                return oldStr;
631
 
        }
632
 
        
633
 
        tmpstr = str;
634
 
        while (!status) {
635
 
                /* Stuff the new piece were needed */
636
 
                strncpy(begin,tmpstr,pmatch[0].rm_so); /* get the beginning */
637
 
                begin[pmatch[0].rm_so] = '\0';  /* Null terminate */
638
 
                
639
 
                lenBeg = strlen(begin);
640
 
                lenSub = strlen(subs);
641
 
                lenTmp = strlen(&(tmpstr[pmatch[0].rm_eo]));
642
 
                if ( (lenTmp + lenSub + lenBeg) >= lennewstr)
643
 
                {
644
 
                        lennewstr = lenTmp + lenSub + lenBeg + 200;
645
 
                        newstr = (char *)erealloc(newstr,lennewstr +1);
646
 
                }
647
 
 
648
 
                memcpy(newstr,begin,lenBeg);
649
 
                memcpy(newstr+lenBeg,subs,lenSub);
650
 
                memcpy(newstr+lenBeg+lenSub,&(tmpstr[pmatch[0].rm_eo]),lenTmp);
651
 
                newstr[lenBeg+lenSub+lenTmp]='\0';
652
 
 
653
 
                /* Copy the newstr into the tmpstr */
654
 
 
655
 
                safestrcpy(MAXSTRLEN,tmpstr,newstr); /* CAREFUL! tmpstr points to an arg of unknown length */
656
 
                /* Position the pointer to the end of the subs string */
657
 
                tmpstr = &(tmpstr[pmatch[0].rm_so+lenSub]);
658
 
                status = regexec(&re,tmpstr,(size_t)5,pmatch,0);
659
 
        }
660
 
        regfree(&re);
661
 
        return str;
662
 
}
663
 
/*---------------------------------------------------------*/
664
 
/* Match a regex and a string */
665
 
 
666
 
int matchARegex( char *str, char *pattern) 
667
 
{
668
 
        int  status;
669
 
        regex_t re;
670
 
        
671
 
        status = regcomp(&re, pattern, REG_EXTENDED);
672
 
        if ( status != 0) {
673
 
                regfree(&re); /* Richard Beebe */
674
 
                return 0;
675
 
        }
676
 
        
677
 
        status = regexec(&re,str,(size_t)0,NULL,0);
678
 
        regfree(&re); /** Marc Perrin ## 18Jan99 **/
679
 
        if (status != 0)
680
 
                return 0;
681
 
        
682
 
        return 1;
683
 
}
684
 
/*-----------------------------------------------------*/
685
 
void makeItLow (char *str)
686
 
{
687
 
  int i;
688
 
  int len = strlen(str);
689
 
  for (i = 0; i < len; i++)
690
 
    str[i] = tolower(str[i]);
691
 
}
692
 
/*----------------------------------------------------*/
693
 
 
694
 
/* Check if a file with a particular suffix should be indexed
695
 
** according to the settings in the configuration file.
696
 
*/
697
 
 
698
 
int isoksuffix(filename, rulelist)
699
 
char *filename;
700
 
struct swline *rulelist;
701
 
{
702
 
        int badfile;
703
 
        char *c;
704
 
        static int lensuffix=0;
705
 
        static char *suffix=NULL;
706
 
        static int lenchecksuffix=0;
707
 
        static char *checksuffix=NULL;
708
 
        struct swline *tmplist;
709
 
 
710
 
        if(!lensuffix) suffix = (char *) emalloc((lensuffix=MAXSUFFIXLEN) + 1);
711
 
        if(!lenchecksuffix) checksuffix = (char *) emalloc((lenchecksuffix=MAXSUFFIXLEN) + 1);
712
 
 
713
 
        tmplist = rulelist;
714
 
        if (tmplist == NULL)
715
 
                return 1; 
716
 
 
717
 
        if ((c = (char *) strrchr(filename, '.')) == NULL)
718
 
                return 0; 
719
 
        
720
 
        if ((int)strlen(c+1) >= MAXSUFFIXLEN)
721
 
          return 0;
722
 
 
723
 
        badfile = 1;
724
 
        checksuffix = SafeStrCopy(checksuffix, c + 1,&lenchecksuffix);
725
 
        while (tmplist != NULL) {
726
 
                if ((c = (char *) strrchr(tmplist->line, '.')) == NULL)
727
 
                        { suffix = SafeStrCopy(suffix, tmplist->line,&lensuffix); }
728
 
                else
729
 
                        { suffix = SafeStrCopy(suffix, c + 1,&lensuffix); }
730
 
                if (lstrstr(suffix, checksuffix) && strlen(suffix) ==
731
 
                        strlen(checksuffix))
732
 
                        badfile = 0;
733
 
                tmplist = tmplist->next;
734
 
        }
735
 
        return !(badfile);
736
 
}
737
 
 
738
 
/* 05/00 Jose Ruiz
739
 
** Function to copy strings 
740
 
** Reallocate memory if needed
741
 
** Returns the string copied
742
 
*/
743
 
char *SafeStrCopy(dest, orig, initialsize)
744
 
char *dest;
745
 
char *orig;
746
 
int *initialsize;
747
 
{
748
 
int len,oldlen;
749
 
        len=strlen(orig);
750
 
        oldlen=*initialsize;
751
 
        if(len > oldlen) {
752
 
                *initialsize=len + 200;
753
 
                if(oldlen)
754
 
                        dest = (char *) erealloc(dest,*initialsize + 1);
755
 
                else
756
 
                        dest = (char *) emalloc(*initialsize + 1);
757
 
        }
758
 
        strcpy(dest,orig);
759
 
        return(dest);
760
 
}
761
 
 
762
 
 
763
 
/* Comparison routine to sort a string - See sortstring */
764
 
int ccomp(const void *s1,const void *s2)
765
 
{
766
 
        return (*(unsigned char *)s1 - *(unsigned char *)s2);
767
 
}
768
 
 
769
 
/* Sort a string  removing dups */
770
 
void sortstring(char *s)
771
 
{
772
 
int i,j,len;
773
 
        len=strlen(s);
774
 
        qsort(s,len,1,&ccomp);
775
 
        for(i=1,j=1;i<len;i++) if(s[i]!=s[j-1]) s[j++]=s[i];
776
 
        s[j]='\0';
777
 
 
778
 
}
779
 
 
780
 
/* Merges two strings removing dups and ordering results */
781
 
char *mergestrings(char *s1, char *s2)
782
 
{
783
 
int i,j,ilen1,ilen2,ilent;
784
 
char *s,*p;
785
 
        ilen1=strlen(s1);
786
 
        ilen2=strlen(s2);
787
 
        ilent=ilen1+ilen2;
788
 
        s= emalloc(ilent+1);
789
 
        p= emalloc(ilent+1);
790
 
        if (ilen1) memcpy(s,s1,ilen1);
791
 
        if (ilen2) memcpy(s+ilen1,s2,ilen2);
792
 
        if (ilent) qsort(s,ilent,1,&ccomp);
793
 
        for(i=1,j=1,p[0]=s[0];i<ilent;i++) if(s[i]!=p[j-1]) p[j++]=s[i];
794
 
        p[j]='\0';
795
 
        efree(s);
796
 
        return(p);
797
 
}
798
 
 
799
 
void makelookuptable(char* s,int *l)
800
 
{
801
 
int i;
802
 
for(i=0;i<256;i++) l[i]=0;
803
 
for(;*s;s++)l[(int)((unsigned char)*s)]=1;
804
 
}
805
 
 
806
 
void makeallstringlookuptables(void)
807
 
{
808
 
        makelookuptable("aeiouAEIOU",isvowellookuptable);
809
 
}
810
 
 
811
 
/* 06/00 Jose Ruiz- Parses a line into a StringList
812
 
*/
813
 
StringList *parse_line(char *line)
814
 
{
815
 
StringList *sl;
816
 
int cursize,skiplen,maxsize;
817
 
char *p;
818
 
        if(!line) return(NULL);
819
 
        if((p=strchr(line,'\n'))) 
820
 
                *p='\0';
821
 
        cursize=0;
822
 
        sl=(StringList *)emalloc(sizeof(StringList));
823
 
        sl->word=(char **)emalloc((maxsize=1)*sizeof(char *));
824
 
        p=line;
825
 
        skiplen=1;
826
 
        while(skiplen && *(p=(char *)getword(line,&skiplen))) 
827
 
        {
828
 
                if(cursize==maxsize) 
829
 
                        sl->word=(char **)erealloc(sl->word,(maxsize*=2)*sizeof(char *));
830
 
                sl->word[cursize++]=(char *)estrdup(p);
831
 
                line+=skiplen;
832
 
        }
833
 
        sl->n=cursize;
834
 
        return sl;
835
 
}
836
 
 
837
 
/* Frees memory used by a StringList
838
 
*/
839
 
void freeStringList(StringList *sl)
840
 
{
841
 
        if(sl) {
842
 
                while(sl->n)
843
 
                        efree(sl->word[--sl->n]);
844
 
                efree(sl->word);
845
 
                efree(sl);
846
 
        }
847
 
}