[117] | 1 |
|
---|
| 2 | /***********************************************************************
|
---|
| 3 |
|
---|
| 4 | $Id: literal.c 959 2008-02-18 00:47:31Z gyoung $
|
---|
| 5 |
|
---|
| 6 | string quoting utilities
|
---|
| 7 | wildcarding utilities
|
---|
| 8 |
|
---|
| 9 | Copyright (c) 1993-98 M. Kimes
|
---|
[871] | 10 | Copyright (c) 2004, 2007 Steven H.Levine
|
---|
[117] | 11 |
|
---|
| 12 | Archive containers
|
---|
| 13 |
|
---|
[298] | 14 | 01 Aug 04 SHL Rework fixup to avoid overflows
|
---|
| 15 | 16 Jun 06 SHL liternal: comments
|
---|
[351] | 16 | 22 Jul 06 SHL Check more run time errors
|
---|
[795] | 17 | 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
|
---|
[871] | 18 | 16 Nov 07 SHL Report fixup buffer overflow
|
---|
[117] | 19 |
|
---|
| 20 | ***********************************************************************/
|
---|
| 21 |
|
---|
[907] | 22 | #include <stdlib.h>
|
---|
| 23 | #include <string.h>
|
---|
| 24 | #include <ctype.h>
|
---|
| 25 |
|
---|
[2] | 26 | #define INCL_OS2
|
---|
| 27 | #define INCL_WIN
|
---|
[907] | 28 | #define INCL_LONGLONG // dircnrs.h
|
---|
[2] | 29 |
|
---|
[907] | 30 | #include "errutil.h" // Dos_Error...
|
---|
[2] | 31 | #include "fm3dll.h"
|
---|
| 32 |
|
---|
[351] | 33 | static PSZ pszSrcFile = __FILE__;
|
---|
| 34 |
|
---|
[551] | 35 | static INT index(const CHAR * s, const CHAR c);
|
---|
[117] | 36 |
|
---|
| 37 | /* Get index of char in string
|
---|
| 38 | * @parm s string to search
|
---|
| 39 | * @parm c char to search for
|
---|
| 40 | * @return 0 relative index of c in s or -1
|
---|
| 41 | */
|
---|
[2] | 42 |
|
---|
[551] | 43 | static INT index(const CHAR * s, const CHAR c)
|
---|
[117] | 44 | {
|
---|
[551] | 45 | CHAR *p;
|
---|
[2] | 46 |
|
---|
[551] | 47 | p = strchr(s, c);
|
---|
| 48 | if (p == NULL || !*p)
|
---|
| 49 | return -1;
|
---|
| 50 | return (INT) (p - s);
|
---|
[2] | 51 | }
|
---|
| 52 |
|
---|
[409] | 53 | /* literal()
|
---|
| 54 | * Translate a string with \ escape tokens to binary equivalent
|
---|
[117] | 55 | * Translates in place
|
---|
| 56 | *
|
---|
[2] | 57 | * 1. \x1b translates to CHAR(0x1b)
|
---|
| 58 | * 2. \27 translates to CHAR(27)
|
---|
| 59 | * 3. \" translates to "
|
---|
| 60 | * 4. \' translates to '
|
---|
| 61 | * 5. \\ translates to \
|
---|
| 62 | * 6. \r translates to carriage return
|
---|
| 63 | * 7. \n translates to linefeed
|
---|
| 64 | * 8. \b translates to backspace
|
---|
| 65 | * 9. \t translates to tab
|
---|
| 66 | * 10. \a translates to bell
|
---|
| 67 | * 11. \f translates to formfeed
|
---|
| 68 | *
|
---|
| 69 | * Synopsis
|
---|
| 70 | * *s = "this\x20is\32a test of \\MSC\\CSM\7"
|
---|
| 71 | * literal(s);
|
---|
| 72 | *
|
---|
| 73 | * ( s now equals "this is a test of \MSC\CSM")
|
---|
[298] | 74 | *
|
---|
[409] | 75 | * Return converted character count like strlen()
|
---|
| 76 | * Count does not include terminating nul
|
---|
[2] | 77 | */
|
---|
| 78 |
|
---|
| 79 | #define HEX "0123456789ABCDEF"
|
---|
| 80 | #define DEC "0123456789"
|
---|
| 81 |
|
---|
[117] | 82 | UINT literal(PSZ pszBuf)
|
---|
| 83 | {
|
---|
[551] | 84 | INT wpos;
|
---|
| 85 | INT iBuf;
|
---|
| 86 | UINT cBufBytes;
|
---|
| 87 | INT iBufSave;
|
---|
| 88 | PSZ pszOut;
|
---|
| 89 | PSZ pszWork;
|
---|
| 90 | CHAR wchar;
|
---|
[2] | 91 |
|
---|
[551] | 92 | if (!pszBuf || !*pszBuf)
|
---|
[2] | 93 | return 0;
|
---|
[117] | 94 | cBufBytes = strlen(pszBuf) + 1;
|
---|
[551] | 95 | pszWork = pszOut = xmalloc(cBufBytes + 1, pszSrcFile, __LINE__);
|
---|
[959] | 96 | if (!pszWork)
|
---|
| 97 | return 0;
|
---|
[2] | 98 |
|
---|
[757] | 99 | iBuf = 0; /* set index to first character */
|
---|
[551] | 100 | while (pszBuf[iBuf]) {
|
---|
| 101 | switch (pszBuf[iBuf]) {
|
---|
| 102 | case '\\':
|
---|
| 103 | switch (pszBuf[iBuf + 1]) {
|
---|
[757] | 104 | case 'x': /* hexadecimal */
|
---|
[871] | 105 | wchar = 0;
|
---|
| 106 | iBuf += 2; /* get past "\x" */
|
---|
| 107 | if (index(HEX, (CHAR) toupper(pszBuf[iBuf])) != -1) {
|
---|
| 108 | iBufSave = iBuf;
|
---|
| 109 | while (((wpos = index(HEX, (CHAR) toupper(pszBuf[iBuf]))) != -1) &&
|
---|
| 110 | iBuf < iBufSave + 2) {
|
---|
| 111 | wchar = (CHAR) (wchar << 4) + (CHAR) wpos;
|
---|
| 112 | iBuf++;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | else
|
---|
| 116 | wchar = 'x'; /* just an x */
|
---|
| 117 | iBuf--;
|
---|
| 118 | *pszOut++ = wchar;
|
---|
| 119 | break;
|
---|
[2] | 120 |
|
---|
[757] | 121 | case '\\': /* we want a "\" */
|
---|
[871] | 122 | iBuf++;
|
---|
| 123 | *pszOut++ = '\\';
|
---|
| 124 | break;
|
---|
[2] | 125 |
|
---|
[757] | 126 | case 't': /* tab CHAR */
|
---|
[871] | 127 | iBuf++;
|
---|
| 128 | *pszOut++ = '\t';
|
---|
| 129 | break;
|
---|
[2] | 130 |
|
---|
[757] | 131 | case 'n': /* new line */
|
---|
[871] | 132 | iBuf++;
|
---|
| 133 | *pszOut++ = '\n';
|
---|
| 134 | break;
|
---|
[2] | 135 |
|
---|
[757] | 136 | case 'r': /* carr return */
|
---|
[871] | 137 | iBuf++;
|
---|
| 138 | *pszOut++ = '\r';
|
---|
| 139 | break;
|
---|
[2] | 140 |
|
---|
[757] | 141 | case 'b': /* back space */
|
---|
[871] | 142 | iBuf++;
|
---|
| 143 | *pszOut++ = '\b';
|
---|
| 144 | break;
|
---|
[2] | 145 |
|
---|
[757] | 146 | case 'f': /* formfeed */
|
---|
[871] | 147 | iBuf++;
|
---|
| 148 | *pszOut++ = '\x0c';
|
---|
| 149 | break;
|
---|
[2] | 150 |
|
---|
[757] | 151 | case 'a': /* bell */
|
---|
[871] | 152 | iBuf++;
|
---|
| 153 | *pszOut++ = '\07';
|
---|
| 154 | break;
|
---|
[2] | 155 |
|
---|
[757] | 156 | case '\'': /* single quote */
|
---|
[871] | 157 | iBuf++;
|
---|
| 158 | *pszOut++ = '\'';
|
---|
| 159 | break;
|
---|
[2] | 160 |
|
---|
[757] | 161 | case '\"': /* double quote */
|
---|
[2] | 162 |
|
---|
[871] | 163 | iBuf++;
|
---|
| 164 | *pszOut++ = '\"';
|
---|
| 165 | break;
|
---|
[757] | 166 |
|
---|
| 167 | default: /* decimal */
|
---|
[871] | 168 | iBuf++; /* get past "\" */
|
---|
| 169 | wchar = 0;
|
---|
| 170 | if (index(DEC, pszBuf[iBuf]) != -1) {
|
---|
| 171 | iBufSave = iBuf;
|
---|
| 172 | do { /* cvt to binary */
|
---|
| 173 | wchar = (CHAR) (wchar * 10 + (pszBuf[iBuf++] - 48));
|
---|
| 174 | } while (index(DEC, pszBuf[iBuf]) != -1 && iBuf < iBufSave + 3);
|
---|
| 175 | iBuf--;
|
---|
| 176 | }
|
---|
| 177 | else
|
---|
| 178 | wchar = pszBuf[iBuf];
|
---|
| 179 | *pszOut++ = wchar;
|
---|
| 180 | break;
|
---|
[757] | 181 | } // switch
|
---|
[551] | 182 | break;
|
---|
| 183 |
|
---|
| 184 | default:
|
---|
| 185 | *pszOut++ = pszBuf[iBuf];
|
---|
| 186 | break;
|
---|
[757] | 187 | } // switch
|
---|
[551] | 188 | iBuf++;
|
---|
[757] | 189 | } // while
|
---|
| 190 | *pszOut = 0; /* Always terminate, even if not string */
|
---|
[2] | 191 |
|
---|
[757] | 192 | cBufBytes = pszOut - pszWork; /* Calc string length excluding terminator */
|
---|
| 193 | memcpy(pszBuf, pszWork, cBufBytes + 1); /* Overwrite including terminator */
|
---|
[117] | 194 | free(pszWork);
|
---|
[2] | 195 |
|
---|
[757] | 196 | return cBufBytes; /* Return string length */
|
---|
[2] | 197 | }
|
---|
| 198 |
|
---|
[117] | 199 | /* Check wildcard match
|
---|
| 200 | * @parm pszBuf Buffer to check
|
---|
| 201 | * @parm pszWildCard wildcard to match
|
---|
| 202 | * @parm fNotFileSpec TRUE if generic match else filespec match
|
---|
| 203 | * @return TRUE if matched else FALSE
|
---|
| 204 | */
|
---|
[2] | 205 |
|
---|
[551] | 206 | BOOL wildcard(const PSZ pszBuf, const PSZ pszWildCard,
|
---|
[871] | 207 | const BOOL fNotFileSpec)
|
---|
[117] | 208 | {
|
---|
[834] | 209 | const CHAR *fstr = pszBuf;
|
---|
| 210 | PSZ fcard = pszWildCard;
|
---|
| 211 | CHAR *tcard;
|
---|
| 212 | INT wmatch = TRUE;
|
---|
| 213 | BOOL reverse = FALSE;
|
---|
[832] | 214 |
|
---|
[551] | 215 | while (wmatch && *fcard && *fstr) {
|
---|
[834] | 216 | if (*fcard == '*' && fcard[strlen(fcard) - 1] == '*' && !reverse){
|
---|
| 217 | tcard = xstrdup(fcard + 1, __FILE__, __LINE__);
|
---|
| 218 | tcard[strlen(tcard) - 1] = 0;
|
---|
| 219 | if (!(strchr(tcard, '?')) && !(strchr(tcard, '*'))){
|
---|
[871] | 220 | if (strstr(fstr, tcard)){ //strstr match for *stuff* pattern no wildcards in "stuff"
|
---|
| 221 | xfree(tcard);
|
---|
| 222 | return TRUE;
|
---|
| 223 | }
|
---|
| 224 | else{
|
---|
| 225 | xfree(tcard);
|
---|
| 226 | return FALSE;
|
---|
| 227 | }
|
---|
[834] | 228 | }
|
---|
| 229 | xfree(tcard);
|
---|
| 230 | }
|
---|
| 231 | else //reverse search for *stuff pattern "stuff" can contain wildcards
|
---|
| 232 | if (*fcard == '*' && fcard[strlen(fcard) - 1] != '*'){
|
---|
[871] | 233 | fstr = strrev(pszBuf);
|
---|
| 234 | fcard = strrev(pszWildCard);
|
---|
| 235 | reverse = TRUE;
|
---|
[832] | 236 | }
|
---|
[834] | 237 | switch (*fcard) { //fm2 standard forward search for all other cases
|
---|
| 238 | case '?': /* character substitution */
|
---|
[871] | 239 | fcard++;
|
---|
| 240 | if (fNotFileSpec || (*fstr != '.' && *fstr != '/' && *fstr != '\\'))
|
---|
| 241 | fstr++; /* skip (match) next character */
|
---|
| 242 | break;
|
---|
[834] | 243 |
|
---|
| 244 | case '*':
|
---|
[871] | 245 | /* find next non-wild character in wildcard */
|
---|
| 246 | while (*fcard && (*fcard == '?' || *fcard == '*'))
|
---|
| 247 | fcard++;
|
---|
| 248 | if (!*fcard){ /* if last char of wildcard is *, it matches */
|
---|
| 249 | if (reverse){
|
---|
| 250 | fstr = strrev(pszBuf);
|
---|
| 251 | fcard = strrev(pszWildCard);
|
---|
| 252 | }
|
---|
| 253 | return TRUE;
|
---|
| 254 | }
|
---|
| 255 | /* skip until partition, match, or eos */
|
---|
| 256 | while (*fstr && toupper(*fstr) != toupper(*fcard) &&
|
---|
| 257 | (fNotFileSpec || (*fstr != '\\' &&
|
---|
| 258 | *fstr != '/' && *fstr != '.')))
|
---|
| 259 | fstr++;
|
---|
| 260 | if (!fNotFileSpec && !*fstr) /* implicit '.' */
|
---|
| 261 | if (*fcard == '.')
|
---|
| 262 | fcard++;
|
---|
| 263 | break;
|
---|
[2] | 264 |
|
---|
[834] | 265 | default:
|
---|
[871] | 266 | if (!fNotFileSpec && ((*fstr == '/' || *fstr == '\\') &&
|
---|
| 267 | (*fcard == '/' || *fcard == '\\')))
|
---|
| 268 | wmatch = TRUE;
|
---|
| 269 | else
|
---|
| 270 | wmatch = (toupper(*fstr) == toupper(*fcard));
|
---|
| 271 | fstr++;
|
---|
| 272 | fcard++;
|
---|
| 273 | break;
|
---|
[834] | 274 | }
|
---|
| 275 | } //while
|
---|
| 276 |
|
---|
| 277 | if ((*fcard && *fcard != '*') || *fstr){
|
---|
| 278 | if (reverse){
|
---|
| 279 | fstr = strrev(pszBuf);
|
---|
| 280 | fcard = strrev(pszWildCard);
|
---|
[2] | 281 | }
|
---|
[832] | 282 | return 0;
|
---|
| 283 | }
|
---|
[834] | 284 | else {
|
---|
| 285 | if (reverse){
|
---|
| 286 | fstr = strrev(pszBuf);
|
---|
| 287 | fcard = strrev(pszWildCard);
|
---|
| 288 | }
|
---|
[832] | 289 | return wmatch;
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
[2] | 292 |
|
---|
[832] | 293 |
|
---|
[117] | 294 | // fixup - quote literal character array
|
---|
[2] | 295 |
|
---|
[551] | 296 | PSZ fixup(const PCH pachIn, PSZ pszOutBuf, const UINT cBufBytes,
|
---|
[871] | 297 | const UINT cInBytes)
|
---|
[117] | 298 | {
|
---|
[551] | 299 | PCH pchIn = pachIn;
|
---|
| 300 | PCH pchOut = pszOutBuf;
|
---|
[2] | 301 |
|
---|
[117] | 302 | // input is a character array, not a string - may not be null terminated
|
---|
| 303 | // cBufBytes is buffer size
|
---|
| 304 | if (pachIn) {
|
---|
[871] | 305 | // 16 Nov 07 SHL fixme to optimize counting and speed
|
---|
[117] | 306 | // Ensure room for null and possible \ escape
|
---|
[871] | 307 | while (pchIn - pachIn < cInBytes) {
|
---|
| 308 | if (pchOut - pszOutBuf + 4 >= cBufBytes) {
|
---|
| 309 | *pchOut = 0;
|
---|
| 310 | Runtime_Error(pszSrcFile, __LINE__, "buffer too small for %s", pszOutBuf);
|
---|
| 311 | break;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
[551] | 314 | if (!isprint(*pchIn)) {
|
---|
[871] | 315 | if (*pchIn == '\r') {
|
---|
| 316 | *pchOut++ = '\\';
|
---|
| 317 | *pchOut++ = 'r';
|
---|
| 318 | }
|
---|
| 319 | else if (*pchIn == '\n') {
|
---|
| 320 | *pchOut++ = '\\';
|
---|
| 321 | *pchOut++ = 'n';
|
---|
| 322 | }
|
---|
| 323 | else if (*pchIn == '\b') {
|
---|
| 324 | *pchOut++ = '\\';
|
---|
| 325 | *pchOut++ = 'b';
|
---|
| 326 | }
|
---|
| 327 | else {
|
---|
| 328 | sprintf(pchOut, "\\x%02x", (UCHAR)*pchIn);
|
---|
| 329 | pchOut += 4;
|
---|
| 330 | }
|
---|
| 331 | pchIn++;
|
---|
[2] | 332 | }
|
---|
[551] | 333 | else if (*pchIn == '\\') {
|
---|
[871] | 334 | *pchOut++ = '\\';
|
---|
| 335 | *pchOut++ = '\\';
|
---|
| 336 | pchIn++;
|
---|
[2] | 337 | }
|
---|
[117] | 338 | else
|
---|
[871] | 339 | *pchOut++ = *pchIn++;
|
---|
| 340 | } // while
|
---|
| 341 |
|
---|
| 342 | } // if pachIn
|
---|
[117] | 343 | *pchOut = 0;
|
---|
| 344 | return pszOutBuf;
|
---|
[2] | 345 | }
|
---|
[795] | 346 |
|
---|
[834] | 347 | #pragma alloc_text(LITERAL,literal,index,fixup,wildcard)
|
---|
[795] | 348 |
|
---|