1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
/*
* cook - file construction tool
* Copyright (C) 1999, 2001, 2006, 2007 Peter Miller;
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* MANIFEST: functions to manipulate gmatchs
*/
#include <common/ac/string.h>
#include <common/gmatch.h>
#include <common/str.h>
#include <common/sub.h>
#include <common/trace.h>
static const char *original;
static gmatch_fp erf;
static void *erf_aux;
void
gmatch_error_handler(gmatch_fp fp, void *aux)
{
erf = fp;
erf_aux = aux;
}
static void
report_error(const char *s)
{
sub_context_ty *scp;
string_ty *message;
if (!erf)
return;
scp = sub_context_new();
sub_var_set(scp, "Name", "%s", original);
message = subst_intl(scp, s);
sub_context_delete(scp);
erf(erf_aux, message);
str_free(message);
}
static void
missing_closing_bracket(void)
{
report_error(i18n("pattern \"$name\" missing closing ']'"));
}
/*
* NAME
* gmatch - match entryname pattern
*
* SYNOPSIS
* int gmatch(char *formal, char *formal_end, char *actual);
*
* DESCRIPTION
* The formal strings is used as a template to match the given actual
* string against.
*
* The pattern elements understood are
* * match zero or more of any character
* ? match any single character
* [^xxx] match any single character not in the set given.
* [xxx] match any single character not in the set given.
* The - character is understood to be a range indicator.
* If the ] character is the first of the set it is considered
* as part of the set, not the terminator.
*
* RETURNS
* the gmatch function returns zero if they do not match,
* and nonzero if they do. Returns -1 on error.
*
* CAVEAT
* This is a limited set of the sh(1) patterns.
* Assumes that the `original' global variable has been initialized, it is
* used for error reporting.
*/
static int
gmatch_inner(const char *formal, const char *formal_end, const char *actual,
const char *actual_end)
{
const char *cp;
int result;
trace(("gmatch_inner(formal = %8.8lX, formal_end = %8.8lX, "
"actual = %8.8lX, actual_end = %8.8lX)\n{\n", (long)formal,
(long)formal_end, (long)actual, (long)actual_end));
while (formal < formal_end)
{
trace(("formal == \"%.*s\";\n", formal_end - formal, formal));
trace(("actual = \"%.*s\";\n", actual_end - actual, actual));
switch (*formal)
{
default:
if (*actual++ != *formal++)
{
result = 0;
goto ret;
}
break;
case '?':
if (actual >= actual_end)
{
result = 0;
goto ret;
}
++actual;
++formal;
break;
case '*':
++formal;
/*
* Look for additional wild-cards or single
* character wilds, and accumulate them. No
* sense recursing twice. This takes care of
* weird patterns like "**" or "*?" etc, and
* allows us to use the trailing wild-card
* optimization.
*/
while (formal < formal_end)
{
if (*formal == '*')
++formal;
else if (*formal == '?')
{
++formal;
if (actual >= actual_end)
{
result = 0;
goto ret;
}
++actual;
}
else
break;
}
/*
* If we are at the end of the formal pattern,
* then we have a trailing wild-card. This
* matches anything, so return success no matter
* how much actual is left.
*/
if (formal >= formal_end)
{
result = 1;
goto ret;
}
/*
* Try to find the longest match possible for
* the wild-card, so start from the right hand
* end of the actual string.
*/
cp = actual_end;
for (;;)
{
if (gmatch_inner(formal, formal_end, cp, actual_end))
{
result = 1;
goto ret;
}
--cp;
if (cp < actual)
{
result = 0;
goto ret;
}
}
case '[':
++formal;
if (*formal == '^')
{
++formal;
for (;;)
{
if (formal >= formal_end)
{
no_close:
missing_closing_bracket();
result = -1;
goto ret;
}
/*
* Note: this allows leading
* ']' elegantly.
*/
if
(
formal_end >= formal + 3
&&
formal[1] == '-'
&&
formal[2] != ']'
)
{
char c1;
char c2;
c1 = formal[0];
c2 = formal[2];
formal += 3;
if
(
c1 <= c2
?
(c1 <= *actual && *actual <= c2)
:
(c2 <= *actual && *actual <= c1)
)
{
result = 0;
goto ret;
}
}
else if (*actual == *formal++)
{
result = 0;
goto ret;
}
if (*formal == ']')
break;
}
++formal;
}
else
{
for (;;)
{
if (formal >= formal_end)
goto no_close;
/*
* Note: this allows leading
* ']' elegantly.
*/
trace(("formal == \"%.*s\";\n",
formal_end - formal, formal));
trace(("actual = \"%.*s\";\n",
actual_end - actual, actual));
if
(
formal_end >= formal + 3
&&
formal[1] == '-'
&&
formal[2] != ']'
)
{
char c1;
char c2;
c1 = formal[0];
c2 = formal[2];
formal += 3;
if
(
c1 <= c2
?
(c1 <= *actual && *actual <= c2)
:
(c2 <= *actual && *actual <= c1)
)
break;
}
else if (*actual == *formal++)
break;
if (*formal == ']')
{
result = 0;
goto ret;
}
}
for (;;)
{
if (formal >= formal_end)
goto no_close;
trace(("formal == \"%.*s\";\n",
formal_end - formal, formal));
trace(("actual = \"%.*s\";\n",
actual_end - actual, actual));
if (*formal++ == ']')
break;
}
}
++actual;
break;
}
}
result = (actual >= actual_end);
ret:
trace(("return %d;\n", result));
trace(("}\n"));
return result;
}
int
gmatch2(const char *formal, const char *formal_end, const char *actual)
{
original = formal;
return gmatch_inner(formal, formal_end, actual, actual + strlen(actual));
}
int
gmatch(const char *formal, const char *actual)
{
return gmatch2(formal, formal + strlen(formal), actual);
}
|