source: trunk/dll/literal.c@ 985

Last change on this file since 985 was 985, checked in by Gregg Young, 17 years ago

Update sizes dialog (ticket 44); Make max command line length user settable (ticket 199); use xfree for free in most cases (ticket 212); initial code to check for valid ini file (ticket 102); Some additional refactoring and structure rework; Some documentation updates;

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