~ubuntu-branches/ubuntu/edgy/agrep/edgy

« back to all changes in this revision

Viewing changes to maskgen.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2005-12-27 17:01:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051227170100-nk2hnq0bnlkbk3q3
Tags: 4.17-2
Added patch to fix FTBS on amd64 (Closes: #344909).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 1991 Sun Wu and Udi Manber.  All Rights Reserved. */
 
1
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal.  All Rights Reserved. */
2
2
#include "agrep.h"
3
 
 
 
3
#include <errno.h>
 
4
 
4
5
extern unsigned D_endpos, endposition, Init1, wildmask;
5
6
extern Mask[], Bit[], Init[], NO_ERR_MASK;
6
 
extern int AND, SIMPLEPATTERN, REGEX, NOUPPER, D_length;
 
7
extern int AND, REGEX, NOUPPER, D_length;
7
8
extern unsigned char Progname[];
8
 
           
 
9
extern int agrep_initialfd;
 
10
extern int EXITONERROR;
 
11
extern int errno;
 
12
 
 
13
int
9
14
maskgen(Pattern, D)
10
 
unsigned char *Pattern; int D;
 
15
unsigned char *Pattern; 
 
16
int D;
11
17
{
12
 
struct term { int flag; unsigned char class[WORD];
13
 
            } position[WORD+10];
14
 
unsigned char c;
15
 
 
16
 
int i, j, k, l, M, OR=0, EVEN = 0, base, No_error;
17
 
 
18
 
 
19
 
for(i=0; i<WORD; i++) position[i].class[0] = '\0';
20
 
for(i=0; i<WORD; i++) position[i].flag = 0;
21
 
wildmask = NO_ERR_MASK = endposition = 0;
22
 
No_error = 0;
23
 
M = strlen(Pattern);
24
 
if(NOUPPER) {
25
 
              for(i=0; i<M; i++) if(isalpha(Pattern[i])) 
26
 
                     if (isupper(Pattern[i])) Pattern[i] = tolower(Pattern[i]);
27
 
            }
 
18
        struct term { 
 
19
                int flag; 
 
20
                unsigned char class[WORD];
 
21
        } 
 
22
        position[WORD+10];
 
23
        unsigned char c;
 
24
 
 
25
        int i, j, k, l, M, OR=0, EVEN = 0, base, No_error;
 
26
 
 
27
#ifdef  DEBUG
 
28
        fprintf(stderr, "maskgen: len=%d, pat=%s, D=%d\n", strlen(Pattern), Pattern, D);
 
29
#endif
 
30
        for(i=0; i<WORD; i++) position[i].class[0] = '\0';
 
31
        for(i=0; i<WORD; i++) position[i].flag = 0;
 
32
        wildmask = NO_ERR_MASK = endposition = 0;
 
33
        No_error = 0;
 
34
        if ((M = strlen(Pattern)) <= 0) return 0;
 
35
        if(NOUPPER) {
 
36
                for(i=0; i<M; i++) if(isalpha((int)Pattern[i])) 
 
37
                        if (isupper((int)Pattern[i])) Pattern[i] = tolower((int)Pattern[i]);
 
38
        }
28
39
#ifdef DEBUG
29
40
        for(i=0; i<M; i++) printf(" %d", Pattern[i]);
30
41
        printf("\n");
31
42
#endif
32
 
for (i=0, j=1; i< M; i++)
33
 
{
34
 
  switch (Pattern[i])
35
 
  {
36
 
    case WILDCD : if(REGEX) {
37
 
                     position[j].class[0] = '.';
38
 
                     position[j].class[1] = '.';
39
 
                     position[j++].class[2] = '\0'; 
40
 
                     break;
41
 
                  }
42
 
                  wildmask = wildmask | Bit[j-1]; break;
43
 
    case STAR   : break; 
44
 
    case ORSYM  : break; 
45
 
    case LPARENT: break;
46
 
    case RPARENT: break;
47
 
    case LANGLE : No_error = ON; EVEN++;
48
 
                  break;
49
 
    case RANGLE : No_error = OFF; EVEN--;
50
 
                  if(EVEN < 0) {
51
 
                     fprintf(stderr, "%s: illegal pattern, unmatched '<', '>'\n", Progname);
52
 
                     exit(2);
53
 
                  }
54
 
                  break;
55
 
    case LRANGE : if(No_error == ON) NO_ERR_MASK = NO_ERR_MASK | Bit[j]; 
56
 
                  i=i+1; 
57
 
                  if (Pattern[i] == NOTSYM) { position[j].flag = Compl; i++; }
58
 
                  k=0;
59
 
                  while (Pattern[i] != RRANGE && i < M)
60
 
                  { 
61
 
                    if(Pattern[i] == HYPHEN) 
62
 
                       { position[j].class[k-1] = Pattern[i+1]; i=i+2; }
63
 
                    else { 
64
 
                     position[j].class[k] = position[j].class[k+1] = Pattern[i];
65
 
                     k = k+2; i++;
66
 
                    }
67
 
                  }
68
 
                  if(i == M) {
69
 
                     fprintf(stderr, "%s: illegal pattern, unmatched '[', ']'\n",Progname);
70
 
                     exit(2);
71
 
                  }
72
 
                  position[j].class[k] = '\0';
73
 
                  j++; break;
74
 
    case RRANGE : fprintf(stderr, "%s: illegal pattern, unmatched '[', ']'\n", Progname); 
75
 
                  exit(2);
76
 
                  break;     
77
 
    case ORPAT  : if(REGEX == ON || AND == ON) {
78
 
                     fprintf(stderr, "illegal pattern \n");
79
 
                     exit(2);
80
 
                  }
81
 
                  OR = ON;
82
 
                  position[j].flag = 2; position[j].class[0] = '\0';
83
 
                  endposition = endposition | Bit[j++]; break;
84
 
    case ANDPAT : position[j].flag = 2; position[j].class[0] = '\0'; 
85
 
                  if(j > D_length) AND = ON;
86
 
                  if(OR || (REGEX == ON && j>D_length)) {
87
 
                     fprintf(stderr, "illegal pattern \n");
88
 
                     exit(2);
89
 
                  }
90
 
                  endposition = endposition | Bit[j++]; break;
91
 
/*
92
 
    case ' '    : if (Pattern[i-1] == ORPAT || Pattern[i-1] == ANDPAT) break;
93
 
                  if(No_error == ON) NO_ERR_MASK = NO_ERR_MASK | Bit[j];
94
 
                  position[j].flag = 0;
95
 
                  position[j].class[0] = position[j].class[1] = Pattern[i];
96
 
                  position[j++].class[2] = '\0';  break;
97
 
*/
98
 
    case '\n'   : NO_ERR_MASK = NO_ERR_MASK | Bit[j];
99
 
                  position[j].class[0] = position[j].class[1] = '\n';
100
 
                  position[j++].class[2] = '\0'; 
101
 
                  break;
102
 
    case WORDB  : NO_ERR_MASK = NO_ERR_MASK | Bit[j];
103
 
                  position[j].class[0] = 1;
104
 
                  position[j].class[1] = 47;
105
 
                  position[j].class[2] = 58;
106
 
                  position[j].class[3] = 64;
107
 
                  position[j].class[4] = 91;
108
 
                  position[j].class[5] = 96;
109
 
                  position[j].class[6] = 123;
110
 
                  position[j].class[7] = 127;
111
 
                  position[j++].class[8] = '\0';
112
 
                  break;    
113
 
    case NNLINE : NO_ERR_MASK |= Bit[j];
114
 
                  position[j].class[0] = position[j].class[1] = '\n';
115
 
                  position[j].class[2] = position[j].class[3] = NNLINE;
116
 
                  position[j++].class[4] = '\0';
117
 
                  break;
118
 
    default : if(No_error == ON) NO_ERR_MASK = NO_ERR_MASK | Bit[j];
119
 
                  position[j].flag = 0;
120
 
                  position[j].class[0] = position[j].class[1] = Pattern[i];
121
 
                  position[j++].class[2] = '\0'; 
122
 
  }
123
 
  if(j > WORD) {
124
 
     fprintf(stderr, "%s: pattern too long\n", Progname);
125
 
     exit(2);
126
 
  }
127
 
}
128
 
  if (EVEN != 0) {
129
 
     fprintf(stderr, "%s: illegal pattern, unmatched '<', '>'\n", Progname);
130
 
     exit(2);
131
 
  }
132
 
M = j - 1;
133
 
base = WORD - M;
134
 
wildmask = (wildmask >> base);
135
 
endposition = (endposition >> base);
136
 
NO_ERR_MASK = (NO_ERR_MASK >> 1) & (~Bit[1]);
137
 
NO_ERR_MASK = ~NO_ERR_MASK >> (base-1);
138
 
  for (i=1; i<= WORD - M ; i++) Init[0] = Init[0] | Bit[i];
139
 
  Init[0] = Init[0] | endposition;
140
 
             /* not necessary for INit[i], i>0, */
141
 
             /* but at every begining of the matching process append one
142
 
                no-match character to initialize the error vectors */
143
 
  endposition = ( endposition << 1 ) + 1;
144
 
  Init1 = (Init[0] | wildmask | endposition) ;
145
 
  D_endpos = ( endposition >> ( M - D_length ) ) << ( M - D_length);
146
 
  endposition = endposition ^ D_endpos;
 
43
        for (i=0, j=1; i< M; i++)
 
44
        {
 
45
                switch (Pattern[i])
 
46
                {
 
47
                case WILDCD : 
 
48
                        if(REGEX) {
 
49
                                position[j].class[0] = '.';
 
50
                                position[j].class[1] = '.';
 
51
                                position[j++].class[2] = '\0'; 
 
52
                                break;
 
53
                        }
 
54
                        wildmask = wildmask | Bit[j-1]; 
 
55
                        break;
 
56
                case STAR   : 
 
57
                        break; 
 
58
                case ORSYM  : 
 
59
                        break; 
 
60
                case LPARENT: 
 
61
                        break;
 
62
                case RPARENT: 
 
63
                        break;
 
64
                case LANGLE : 
 
65
                        No_error = ON; 
 
66
                        EVEN++;
 
67
                        break;
 
68
                case RANGLE : 
 
69
                        No_error = OFF; 
 
70
                        EVEN--;
 
71
                        if(EVEN < 0) {
 
72
                                fprintf(stderr, "%s: unmatched '<', '>' (use \\<, \\> to search for <, >)\n", Progname);
 
73
                                if (!EXITONERROR) {
 
74
                                        errno = AGREP_ERROR;
 
75
                                        return -1;
 
76
                                }
 
77
                                else exit(2);
 
78
                        }
 
79
                        break;
 
80
                case LRANGE : 
 
81
                        if(No_error == ON) NO_ERR_MASK = NO_ERR_MASK | Bit[j]; 
 
82
                        i=i+1; 
 
83
                        if (Pattern[i] == NOTSYM) { 
 
84
                                position[j].flag = Compl; 
 
85
                                i++; 
 
86
                        }
 
87
                        k=0;
 
88
                        while (Pattern[i] != RRANGE && i < M)
 
89
                        { 
 
90
                                if(Pattern[i] == HYPHEN) 
 
91
                                { 
 
92
                                        position[j].class[k-1] = Pattern[i+1]; 
 
93
                                        i=i+2; 
 
94
                                }
 
95
                                else { 
 
96
                                        position[j].class[k] = position[j].class[k+1] = Pattern[i];
 
97
                                        k = k+2; 
 
98
                                        i++;
 
99
                                }
 
100
                        }
 
101
                        if(i == M) {
 
102
                                fprintf(stderr, "%s: unmatched '[', ']' (use \\[, \\] to search for [, ])\n", Progname);
 
103
                                if (!EXITONERROR) {
 
104
                                        errno = AGREP_ERROR;
 
105
                                        return -1;
 
106
                                }
 
107
                                else exit(2);
 
108
                        }
 
109
                        position[j].class[k] = '\0';
 
110
                        j++; 
 
111
                        break;
 
112
                case RRANGE : 
 
113
                        fprintf(stderr, "%s: unmatched '[', ']' (use \\[, \\] to search for [, ])\n", Progname);
 
114
                        if (!EXITONERROR) {
 
115
                                errno = AGREP_ERROR;
 
116
                                return -1;
 
117
                        }
 
118
                        else exit(2);
 
119
                        break;     
 
120
                case ORPAT  : 
 
121
                        if(REGEX == ON || AND == ON) {
 
122
                                fprintf(stderr, "illegal pattern: cannot handle OR (',') and AND (';')/regular-expressions simultaneously\n");
 
123
                                if (!EXITONERROR) {
 
124
                                        errno = AGREP_ERROR;
 
125
                                        return -1;
 
126
                                }
 
127
                                else exit(2);
 
128
                        }
 
129
                        OR = ON;
 
130
                        position[j].flag = 2; 
 
131
                        position[j].class[0] = '\0';
 
132
                        endposition = endposition | Bit[j++]; 
 
133
                        break;
 
134
                case ANDPAT : 
 
135
                        position[j].flag = 2; 
 
136
                        position[j].class[0] = '\0'; 
 
137
                        if(j > D_length) AND = ON;
 
138
                        if(OR || (REGEX == ON && j>D_length)) {
 
139
                                fprintf(stderr, "illegal pattern: cannot handle AND (';') and OR (',')/regular-expressions simultaneously\n");
 
140
                                if (!EXITONERROR) {
 
141
                                        errno = AGREP_ERROR;
 
142
                                        return -1;
 
143
                                }
 
144
                                else exit(2);
 
145
                        }
 
146
                        endposition = endposition | Bit[j++]; 
 
147
                        break;
 
148
                        /*
 
149
                                case ' '    : if (Pattern[i-1] == ORPAT || Pattern[i-1] == ANDPAT) break;
 
150
                                                          if(No_error == ON) NO_ERR_MASK = NO_ERR_MASK | Bit[j];
 
151
                                                          position[j].flag = 0;
 
152
                                                          position[j].class[0] = position[j].class[1] = Pattern[i];
 
153
                                                          position[j++].class[2] = '\0';  break;
 
154
                        */
 
155
                case '\n'   : 
 
156
                        NO_ERR_MASK = NO_ERR_MASK | Bit[j];
 
157
                        position[j].class[0] = position[j].class[1] = '\n';
 
158
                        position[j++].class[2] = '\0'; 
 
159
                        break;
 
160
                case WORDB  : 
 
161
                        NO_ERR_MASK = NO_ERR_MASK | Bit[j];
 
162
                        position[j].class[0] = 1;
 
163
                        position[j].class[1] = 47;
 
164
                        position[j].class[2] = 58;
 
165
                        position[j].class[3] = 64;
 
166
                        position[j].class[4] = 91;
 
167
                        position[j].class[5] = 96;
 
168
                        position[j].class[6] = 123;
 
169
                        position[j].class[7] = 127;
 
170
                        position[j++].class[8] = '\0';
 
171
                        break;    
 
172
                case NNLINE : 
 
173
                        NO_ERR_MASK |= Bit[j];
 
174
                        position[j].class[0] = position[j].class[1] = '\n';
 
175
                        position[j].class[2] = position[j].class[3] = NNLINE;
 
176
                        position[j++].class[4] = '\0';
 
177
                        break;
 
178
                default : 
 
179
                        if(No_error == ON) NO_ERR_MASK = NO_ERR_MASK | Bit[j];
 
180
                        position[j].flag = 0;
 
181
                        position[j].class[0] = position[j].class[1] = Pattern[i];
 
182
                        position[j++].class[2] = '\0'; 
 
183
                }
 
184
                if(j > WORD) {
 
185
                        fprintf(stderr, "%s: pattern too long (has > %d chars)\n", Progname, WORD);
 
186
                        if (!EXITONERROR) {
 
187
                                errno = AGREP_ERROR;
 
188
                                return -1;
 
189
                        }
 
190
                        else exit(2);
 
191
                }
 
192
        }
 
193
        if (EVEN != 0) {
 
194
                fprintf(stderr, "%s: unmatched '<', '>' (use \\<, \\> to search for <, >)\n", Progname);
 
195
                if (!EXITONERROR) {
 
196
                        errno = AGREP_ERROR;
 
197
                        return -1;
 
198
                }
 
199
                else exit(2);
 
200
        }
 
201
        M = j - 1;
 
202
        base = WORD - M;
 
203
        wildmask = (wildmask >> base);
 
204
        endposition = (endposition >> base);
 
205
        NO_ERR_MASK = (NO_ERR_MASK >> 1) & (~Bit[1]);
 
206
        NO_ERR_MASK = ~NO_ERR_MASK >> (base-1);
 
207
        for (i=1; i<= WORD - M ; i++) Init[0] = Init[0] | Bit[i];
 
208
        Init[0] = Init[0] | endposition;
 
209
        /* not necessary for INit[i], i>0, */
 
210
        /* but at every begining of the matching process append one
 
211
                                        no-match character to initialize the error vectors */
 
212
        endposition = ( endposition << 1 ) + 1;
 
213
        Init1 = (Init[0] | wildmask | endposition) ;
 
214
        D_endpos = ( endposition >> ( M - D_length ) ) << ( M - D_length);
 
215
        endposition = endposition ^ D_endpos;
147
216
#ifdef DEBUG
148
217
        printf("endposition: %o\n", endposition);
149
218
        printf("no_err_mask: %o\n", NO_ERR_MASK);
150
219
#endif
151
 
  for(c=0, i=0; i < MAXSYM; c++, i++)
152
 
  {
153
 
     for (k=1, l=0; k<=M ; k++, l=0)  {
154
 
         while (position[k].class[l] != '\0') {
155
 
               if (position[k].class[l] == NOCARE && (c != '\n' || REGEX) ) 
156
 
                  {  Mask[c] = Mask[c] | Bit[base + k]; break; }
157
 
               if (c >= position[k].class[l] && c <= position[k].class[l+1])
158
 
                  {  Mask[c] = Mask[c] | Bit[base + k]; break; }
159
 
               l = l + 2;  }
160
 
         if (position[k].flag == Compl) Mask[c] = Mask[c] ^ Bit[base+k];
161
 
     }
162
 
  }
163
 
  if(NOUPPER) for(c='A'; c<='Z'; c=c+1) if (isupper(c)) 
164
 
                  Mask[c] = Mask[tolower(c)]; 
165
 
  return(M);
 
220
        for(c=0, i=0; i < MAXSYM; c++, i++)
 
221
        {
 
222
                for (k=1, l=0; k<=M ; k++, l=0)  {
 
223
                        while (position[k].class[l] != '\0') {
 
224
                                if (position[k].class[l] == NOCARE && (c != '\n' || REGEX) ) 
 
225
                                {  
 
226
                                        Mask[c] = Mask[c] | Bit[base + k]; 
 
227
                                        break; 
 
228
                                }
 
229
                                if (c >= position[k].class[l] && c <= position[k].class[l+1])
 
230
                                {  
 
231
                                        Mask[c] = Mask[c] | Bit[base + k]; 
 
232
                                        break; 
 
233
                                }
 
234
                                l = l + 2;  
 
235
                        }
 
236
                        if (position[k].flag == Compl) Mask[c] = Mask[c] ^ Bit[base+k];
 
237
                }
 
238
        }
 
239
        if(NOUPPER) for(i=0; i<MAXSYM; i++)
 
240
                if (isupper(i)) Mask[i] = Mask[tolower(i)]; 
 
241
        return(M);
166
242
}
167
243