1 | /* |
---|
2 | * Copyright 1994 Christopher Seiwald. All rights reserved. |
---|
3 | * |
---|
4 | * This file is part of Jam - see jam.c for Copyright information. |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * glob.c - match a string against a simple pattern |
---|
9 | * |
---|
10 | * Understands the following patterns: |
---|
11 | * |
---|
12 | * * any number of characters |
---|
13 | * ? any single character |
---|
14 | * [a-z] any single character in the range a-z |
---|
15 | * [^a-z] any single character not in the range a-z |
---|
16 | * \x match x |
---|
17 | * |
---|
18 | * External functions: |
---|
19 | * |
---|
20 | * glob() - match a string against a simple pattern |
---|
21 | * |
---|
22 | * Internal functions: |
---|
23 | * |
---|
24 | * globchars() - build a bitlist to check for character group match |
---|
25 | */ |
---|
26 | |
---|
27 | # include "jam.h" |
---|
28 | |
---|
29 | # define CHECK_BIT( tab, bit ) ( tab[ (bit)/8 ] & (1<<( (bit)%8 )) ) |
---|
30 | # define BITLISTSIZE 16 /* bytes used for [chars] in compiled expr */ |
---|
31 | |
---|
32 | static void globchars( char *s, char *e, char *b ); |
---|
33 | |
---|
34 | /* |
---|
35 | * glob() - match a string against a simple pattern |
---|
36 | */ |
---|
37 | |
---|
38 | int |
---|
39 | glob( |
---|
40 | register char *c, |
---|
41 | register char *s ) |
---|
42 | { |
---|
43 | char bitlist[ BITLISTSIZE ]; |
---|
44 | char *here; |
---|
45 | |
---|
46 | for( ;; ) |
---|
47 | switch( *c++ ) |
---|
48 | { |
---|
49 | case '\0': |
---|
50 | return *s ? -1 : 0; |
---|
51 | |
---|
52 | case '?': |
---|
53 | if( !*s++ ) |
---|
54 | return 1; |
---|
55 | break; |
---|
56 | |
---|
57 | case '[': |
---|
58 | /* scan for matching ] */ |
---|
59 | |
---|
60 | here = c; |
---|
61 | do if( !*c++ ) |
---|
62 | return 1; |
---|
63 | while( here == c || *c != ']' ); |
---|
64 | c++; |
---|
65 | |
---|
66 | /* build character class bitlist */ |
---|
67 | |
---|
68 | globchars( here, c, bitlist ); |
---|
69 | |
---|
70 | if( !CHECK_BIT( bitlist, *(unsigned char *)s ) ) |
---|
71 | return 1; |
---|
72 | s++; |
---|
73 | break; |
---|
74 | |
---|
75 | case '*': |
---|
76 | here = s; |
---|
77 | |
---|
78 | while( *s ) |
---|
79 | s++; |
---|
80 | |
---|
81 | /* Try to match the rest of the pattern in a recursive */ |
---|
82 | /* call. If the match fails we'll back up chars, retrying. */ |
---|
83 | |
---|
84 | while( s != here ) |
---|
85 | { |
---|
86 | int r; |
---|
87 | |
---|
88 | /* A fast path for the last token in a pattern */ |
---|
89 | |
---|
90 | r = *c ? glob( c, s ) : *s ? -1 : 0; |
---|
91 | |
---|
92 | if( !r ) |
---|
93 | return 0; |
---|
94 | else if( r < 0 ) |
---|
95 | return 1; |
---|
96 | |
---|
97 | --s; |
---|
98 | } |
---|
99 | break; |
---|
100 | |
---|
101 | case '\\': |
---|
102 | /* Force literal match of next char. */ |
---|
103 | |
---|
104 | if( !*c || *s++ != *c++ ) |
---|
105 | return 1; |
---|
106 | break; |
---|
107 | |
---|
108 | default: |
---|
109 | if( *s++ != c[-1] ) |
---|
110 | return 1; |
---|
111 | break; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | /* |
---|
116 | * globchars() - build a bitlist to check for character group match |
---|
117 | */ |
---|
118 | |
---|
119 | static void |
---|
120 | globchars( |
---|
121 | char *s, |
---|
122 | char *e, |
---|
123 | char *b ) |
---|
124 | { |
---|
125 | int neg = 0; |
---|
126 | |
---|
127 | memset( b, '\0', BITLISTSIZE ); |
---|
128 | |
---|
129 | if( *s == '^') |
---|
130 | neg++, s++; |
---|
131 | |
---|
132 | while( s < e ) |
---|
133 | { |
---|
134 | int c; |
---|
135 | |
---|
136 | if( s+2 < e && s[1] == '-' ) |
---|
137 | { |
---|
138 | for( c = s[0]; c <= s[2]; c++ ) |
---|
139 | b[ c/8 ] |= (1<<(c%8)); |
---|
140 | s += 3; |
---|
141 | } else { |
---|
142 | c = *s++; |
---|
143 | b[ c/8 ] |= (1<<(c%8)); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | if( neg ) |
---|
148 | { |
---|
149 | int i; |
---|
150 | for( i = 0; i < BITLISTSIZE; i++ ) |
---|
151 | b[ i ] ^= 0377; |
---|
152 | } |
---|
153 | |
---|
154 | /* Don't include \0 in either $[chars] or $[^chars] */ |
---|
155 | |
---|
156 | b[0] &= 0376; |
---|
157 | } |
---|