~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/modules/libpref/src/prefread.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
4
 * The contents of this file are subject to the Mozilla Public License Version
 
5
 * 1.1 (the "License"); you may not use this file except in compliance with
 
6
 * the License. You may obtain a copy of the License at
 
7
 * http://www.mozilla.org/MPL/
 
8
 *
 
9
 * Software distributed under the License is distributed on an "AS IS" basis,
 
10
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
11
 * for the specific language governing rights and limitations under the
 
12
 * License.
 
13
 *
 
14
 * The Original Code is Mozilla.
 
15
 *
 
16
 * The Initial Developer of the Original Code is Darin Fisher.
 
17
 * Portions created by the Initial Developer are Copyright (C) 2003
 
18
 * the Initial Developer. All Rights Reserved.
 
19
 *
 
20
 * Contributor(s):
 
21
 *   Darin Fisher <darin@meer.net>
 
22
 *
 
23
 * Alternatively, the contents of this file may be used under the terms of
 
24
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
25
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
26
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
27
 * of those above. If you wish to allow use of your version of this file only
 
28
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
29
 * use your version of this file under the terms of the MPL, indicate your
 
30
 * decision by deleting the provisions above and replace them with the notice
 
31
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
32
 * the provisions above, a recipient may use your version of this file under
 
33
 * the terms of any one of the MPL, the GPL or the LGPL.
 
34
 *
 
35
 * ***** END LICENSE BLOCK ***** */
 
36
 
 
37
#include <stdlib.h>
 
38
#include <string.h>
 
39
#include <ctype.h>
 
40
#include "prefread.h"
 
41
 
 
42
#ifdef TEST_PREFREAD
 
43
#include <stdio.h>
 
44
#define NS_WARNING(_s) printf(">>> " _s "!\n")
 
45
#define NS_NOTREACHED(_s) NS_WARNING(_s)
 
46
#else
 
47
#include "nsDebug.h" // for NS_WARNING
 
48
#endif
 
49
 
 
50
/* pref parser states */
 
51
enum {
 
52
    PREF_PARSE_INIT,
 
53
    PREF_PARSE_MATCH_STRING,
 
54
    PREF_PARSE_UNTIL_NAME,
 
55
    PREF_PARSE_QUOTED_STRING,
 
56
    PREF_PARSE_UNTIL_COMMA,
 
57
    PREF_PARSE_UNTIL_VALUE,
 
58
    PREF_PARSE_INT_VALUE,
 
59
    PREF_PARSE_COMMENT_MAYBE_START,
 
60
    PREF_PARSE_COMMENT_BLOCK,
 
61
    PREF_PARSE_COMMENT_BLOCK_MAYBE_END,
 
62
    PREF_PARSE_ESC_SEQUENCE,
 
63
    PREF_PARSE_UNTIL_OPEN_PAREN,
 
64
    PREF_PARSE_UNTIL_CLOSE_PAREN,
 
65
    PREF_PARSE_UNTIL_SEMICOLON,
 
66
    PREF_PARSE_UNTIL_EOL
 
67
};
 
68
 
 
69
static const char kUserPref[] = "user_pref";
 
70
static const char kPref[] = "pref";
 
71
static const char kTrue[] = "true";
 
72
static const char kFalse[] = "false";
 
73
 
 
74
/**
 
75
 * pref_GrowBuf
 
76
 * 
 
77
 * this function will increase the size of the buffer owned
 
78
 * by the given pref parse state.  the only requirement is
 
79
 * that it increase the buffer by at least one byte, but we
 
80
 * use a simple doubling algorithm.
 
81
 * 
 
82
 * this buffer is used to store partial pref lines.  it is
 
83
 * freed when the parse state is destroyed.
 
84
 *
 
85
 * @param ps
 
86
 *        parse state instance
 
87
 *
 
88
 * this function updates all pointers that reference an 
 
89
 * address within lb since realloc may relocate the buffer.
 
90
 *
 
91
 * @return PR_FALSE if insufficient memory.
 
92
 */
 
93
static PRBool
 
94
pref_GrowBuf(PrefParseState *ps)
 
95
{
 
96
    int bufLen, curPos, valPos;
 
97
 
 
98
    bufLen = ps->lbend - ps->lb;
 
99
    curPos = ps->lbcur - ps->lb;
 
100
    valPos = ps->vb    - ps->lb;
 
101
 
 
102
    if (bufLen == 0)
 
103
        bufLen = 128;  /* default buffer size */
 
104
    else
 
105
        bufLen <<= 1;  /* double buffer size */
 
106
 
 
107
#ifdef TEST_PREFREAD
 
108
    fprintf(stderr, ">>> realloc(%d)\n", bufLen);
 
109
#endif
 
110
 
 
111
    ps->lb = (char*) realloc(ps->lb, bufLen);
 
112
    if (!ps->lb)
 
113
        return PR_FALSE;
 
114
 
 
115
    ps->lbcur = ps->lb + curPos;
 
116
    ps->lbend = ps->lb + bufLen;
 
117
    ps->vb    = ps->lb + valPos;
 
118
 
 
119
    return PR_TRUE;
 
120
}
 
121
 
 
122
/**
 
123
 * pref_DoCallback
 
124
 *
 
125
 * this function is called when a complete pref name-value pair has
 
126
 * been extracted from the input data.
 
127
 *
 
128
 * @param ps
 
129
 *        parse state instance
 
130
 *
 
131
 * @return PR_FALSE to indicate a fatal error.
 
132
 */
 
133
static PRBool
 
134
pref_DoCallback(PrefParseState *ps)
 
135
{
 
136
    PrefValue  value;
 
137
 
 
138
    switch (ps->vtype) {
 
139
    case PREF_STRING:
 
140
        value.stringVal = ps->vb;
 
141
        break;
 
142
    case PREF_INT:
 
143
        if ((ps->vb[0] == '-' || ps->vb[0] == '+') && ps->vb[1] == '\0') {
 
144
            NS_WARNING("malformed integer value");
 
145
            return PR_FALSE;
 
146
        }
 
147
        value.intVal = atoi(ps->vb);
 
148
        break;
 
149
    case PREF_BOOL:
 
150
        value.boolVal = (ps->vb == kTrue);
 
151
        break;
 
152
    default:
 
153
        break;
 
154
    }
 
155
    (*ps->reader)(ps->closure, ps->lb, value, ps->vtype, ps->fdefault);
 
156
    return PR_TRUE;
 
157
}
 
158
 
 
159
void
 
160
PREF_InitParseState(PrefParseState *ps, PrefReader reader, void *closure)
 
161
{
 
162
    memset(ps, 0, sizeof(*ps));
 
163
    ps->reader = reader;
 
164
    ps->closure = closure;
 
165
}
 
166
 
 
167
void
 
168
PREF_FinalizeParseState(PrefParseState *ps)
 
169
{
 
170
    if (ps->lb)
 
171
        free(ps->lb);
 
172
}
 
173
 
 
174
/**
 
175
 * Pseudo-BNF
 
176
 * ----------
 
177
 * function      = LJUNK function-name JUNK function-args
 
178
 * function-name = "user_pref" | "pref"
 
179
 * function-args = "(" JUNK pref-name JUNK "," JUNK pref-value JUNK ")" JUNK ";"
 
180
 * pref-name     = quoted-string
 
181
 * pref-value    = quoted-string | "true" | "false" | integer-value
 
182
 * JUNK          = *(WS | comment-block | comment-line)
 
183
 * LJUNK         = *(WS | comment-block | comment-line | bcomment-line)
 
184
 * WS            = SP | HT | LF | VT | FF | CR
 
185
 * SP            = <US-ASCII SP, space (32)>
 
186
 * HT            = <US-ASCII HT, horizontal-tab (9)>
 
187
 * LF            = <US-ASCII LF, linefeed (10)>
 
188
 * VT            = <US-ASCII HT, vertical-tab (11)>
 
189
 * FF            = <US-ASCII FF, form-feed (12)>
 
190
 * CR            = <US-ASCII CR, carriage return (13)>
 
191
 * comment-block = <C/C++ style comment block>
 
192
 * comment-line  = <C++ style comment line>
 
193
 * bcomment-line = <bourne-shell style comment line>
 
194
 */
 
195
PRBool
 
196
PREF_ParseBuf(PrefParseState *ps, const char *buf, int bufLen)
 
197
{
 
198
    const char *end;
 
199
    char c;
 
200
    int state;
 
201
 
 
202
    state = ps->state;
 
203
    for (end = buf + bufLen; buf != end; ++buf) {
 
204
        c = *buf;
 
205
        switch (state) {
 
206
        /* initial state */
 
207
        case PREF_PARSE_INIT:
 
208
            if (ps->lbcur != ps->lb) { /* reset state */
 
209
                ps->lbcur = ps->lb;
 
210
                ps->vb    = NULL;
 
211
                ps->vtype = PREF_INVALID;
 
212
                ps->fdefault = PR_FALSE;
 
213
            }
 
214
            switch (c) {
 
215
            case '/':       /* begin comment block or line? */
 
216
                state = PREF_PARSE_COMMENT_MAYBE_START;
 
217
                break;
 
218
            case '#':       /* accept shell style comments */
 
219
                state = PREF_PARSE_UNTIL_EOL;
 
220
                break;
 
221
            case 'u':       /* indicating user_pref */
 
222
            case 'p':       /* indicating pref */
 
223
                ps->smatch = (c == 'u' ? kUserPref : kPref);
 
224
                ps->sindex = 1;
 
225
                ps->nextstate = PREF_PARSE_UNTIL_OPEN_PAREN;
 
226
                state = PREF_PARSE_MATCH_STRING;
 
227
                break;
 
228
            /* else skip char */
 
229
            }
 
230
            break;
 
231
 
 
232
        /* string matching */
 
233
        case PREF_PARSE_MATCH_STRING:
 
234
            if (c == ps->smatch[ps->sindex++]) {
 
235
                /* if we've matched all characters, then move to next state. */
 
236
                if (ps->smatch[ps->sindex] == '\0') {
 
237
                    state = ps->nextstate;
 
238
                    ps->nextstate = PREF_PARSE_INIT; /* reset next state */
 
239
                }
 
240
                /* else wait for next char */
 
241
            }
 
242
            else {
 
243
                NS_WARNING("malformed pref file");
 
244
                return PR_FALSE;
 
245
            }
 
246
            break;
 
247
 
 
248
        /* quoted string parsing */
 
249
        case PREF_PARSE_QUOTED_STRING:
 
250
            /* we assume that the initial quote has already been consumed */
 
251
            if (ps->lbcur == ps->lbend && !pref_GrowBuf(ps))
 
252
                return PR_FALSE; /* out of memory */
 
253
            if (c == '\\')
 
254
                state = PREF_PARSE_ESC_SEQUENCE;
 
255
            else if (c == ps->quotechar) {
 
256
                *ps->lbcur++ = '\0';
 
257
                state = ps->nextstate;
 
258
                ps->nextstate = PREF_PARSE_INIT; /* reset next state */
 
259
            }
 
260
            else
 
261
                *ps->lbcur++ = c;
 
262
            break;
 
263
 
 
264
        /* name parsing */
 
265
        case PREF_PARSE_UNTIL_NAME:
 
266
            if (c == '\"' || c == '\'') {
 
267
                ps->fdefault = (ps->smatch == kPref);
 
268
                ps->quotechar = c;
 
269
                ps->nextstate = PREF_PARSE_UNTIL_COMMA; /* return here when done */
 
270
                state = PREF_PARSE_QUOTED_STRING;
 
271
            }
 
272
            else if (c == '/') {       /* allow embedded comment */
 
273
                ps->nextstate = state; /* return here when done with comment */
 
274
                state = PREF_PARSE_COMMENT_MAYBE_START;
 
275
            }
 
276
            else if (!isspace(c)) {
 
277
                NS_WARNING("malformed pref file");
 
278
                return PR_FALSE;
 
279
            }
 
280
            break;
 
281
 
 
282
        /* parse until we find a comma separating name and value */
 
283
        case PREF_PARSE_UNTIL_COMMA:
 
284
            if (c == ',') {
 
285
                ps->vb = ps->lbcur;
 
286
                state = PREF_PARSE_UNTIL_VALUE;
 
287
            }
 
288
            else if (c == '/') {       /* allow embedded comment */
 
289
                ps->nextstate = state; /* return here when done with comment */
 
290
                state = PREF_PARSE_COMMENT_MAYBE_START;
 
291
            }
 
292
            else if (!isspace(c)) {
 
293
                NS_WARNING("malformed pref file");
 
294
                return PR_FALSE;
 
295
            }
 
296
            break;
 
297
 
 
298
        /* value parsing */
 
299
        case PREF_PARSE_UNTIL_VALUE:
 
300
            /* the pref value type is unknown.  so, we scan for the first
 
301
             * character of the value, and determine the type from that. */
 
302
            if (c == '\"' || c == '\'') {
 
303
                ps->vtype = PREF_STRING;
 
304
                ps->quotechar = c;
 
305
                ps->nextstate = PREF_PARSE_UNTIL_CLOSE_PAREN;
 
306
                state = PREF_PARSE_QUOTED_STRING;
 
307
            }
 
308
            else if (c == 't' || c == 'f') {
 
309
                ps->vb = (char *) (c == 't' ? kTrue : kFalse);
 
310
                ps->vtype = PREF_BOOL;
 
311
                ps->smatch = ps->vb;
 
312
                ps->sindex = 1;
 
313
                ps->nextstate = PREF_PARSE_UNTIL_CLOSE_PAREN;
 
314
                state = PREF_PARSE_MATCH_STRING;
 
315
            }
 
316
            else if (isdigit(c) || (c == '-') || (c == '+')) {
 
317
                ps->vtype = PREF_INT;
 
318
                /* write c to line buffer... */
 
319
                if (ps->lbcur == ps->lbend && !pref_GrowBuf(ps))
 
320
                    return PR_FALSE; /* out of memory */
 
321
                *ps->lbcur++ = c;
 
322
                state = PREF_PARSE_INT_VALUE;
 
323
            }
 
324
            else if (c == '/') {       /* allow embedded comment */
 
325
                ps->nextstate = state; /* return here when done with comment */
 
326
                state = PREF_PARSE_COMMENT_MAYBE_START;
 
327
            }
 
328
            else if (!isspace(c)) {
 
329
                NS_WARNING("malformed pref file");
 
330
                return PR_FALSE;
 
331
            }
 
332
            break;
 
333
        case PREF_PARSE_INT_VALUE:
 
334
            /* grow line buffer if necessary... */
 
335
            if (ps->lbcur == ps->lbend && !pref_GrowBuf(ps))
 
336
                return PR_FALSE; /* out of memory */
 
337
            if (isdigit(c))
 
338
                *ps->lbcur++ = c;
 
339
            else {
 
340
                *ps->lbcur++ = '\0'; /* stomp null terminator; we are done. */
 
341
                if (c == ')')
 
342
                    state = PREF_PARSE_UNTIL_SEMICOLON;
 
343
                else if (c == '/') { /* allow embedded comment */
 
344
                    ps->nextstate = PREF_PARSE_UNTIL_CLOSE_PAREN;
 
345
                    state = PREF_PARSE_COMMENT_MAYBE_START;
 
346
                }
 
347
                else if (isspace(c))
 
348
                    state = PREF_PARSE_UNTIL_CLOSE_PAREN;
 
349
                else {
 
350
                    NS_WARNING("malformed pref file");
 
351
                    return PR_FALSE;
 
352
                }
 
353
            }
 
354
            break;
 
355
 
 
356
        /* comment parsing */
 
357
        case PREF_PARSE_COMMENT_MAYBE_START:
 
358
            switch (c) {
 
359
            case '*': /* comment block */
 
360
                state = PREF_PARSE_COMMENT_BLOCK;
 
361
                break;
 
362
            case '/': /* comment line */
 
363
                state = PREF_PARSE_UNTIL_EOL;
 
364
                break;
 
365
            default:
 
366
                /* pref file is malformed */
 
367
                NS_WARNING("malformed pref file");
 
368
                return PR_FALSE;
 
369
            }
 
370
            break;
 
371
        case PREF_PARSE_COMMENT_BLOCK:
 
372
            if (c == '*') 
 
373
                state = PREF_PARSE_COMMENT_BLOCK_MAYBE_END;
 
374
            break;
 
375
        case PREF_PARSE_COMMENT_BLOCK_MAYBE_END:
 
376
            switch (c) {
 
377
            case '/':
 
378
                state = ps->nextstate;
 
379
                ps->nextstate = PREF_PARSE_INIT;
 
380
                break;
 
381
            case '*':       /* stay in this state */
 
382
                break;
 
383
            default:
 
384
                state = PREF_PARSE_COMMENT_BLOCK;
 
385
            }
 
386
            break;
 
387
 
 
388
        /* string escape sequence parsing */
 
389
        case PREF_PARSE_ESC_SEQUENCE:
 
390
            /* not necessary to resize buffer here since we should be writing
 
391
             * only one character and the resize check would have been done
 
392
             * for us in the previous state */
 
393
            switch (c) {
 
394
            case '\"':
 
395
            case '\\':
 
396
                break;
 
397
            case 'r':
 
398
                c = '\r';
 
399
                break;
 
400
            case 'n':
 
401
                c = '\n';
 
402
                break;
 
403
            default:
 
404
                NS_WARNING("preserving unexpected JS escape sequence");
 
405
                /* grow line buffer if necessary... */
 
406
                if (ps->lbcur == ps->lbend && !pref_GrowBuf(ps))
 
407
                    return PR_FALSE; /* out of memory */
 
408
                *ps->lbcur++ = '\\'; /* preserve the escape sequence */
 
409
                break;
 
410
            }
 
411
            *ps->lbcur++ = c;
 
412
            state = PREF_PARSE_QUOTED_STRING;
 
413
            break;
 
414
 
 
415
        /* function open and close parsing */
 
416
        case PREF_PARSE_UNTIL_OPEN_PAREN:
 
417
            /* tolerate only whitespace and embedded comments */
 
418
            if (c == '(')
 
419
                state = PREF_PARSE_UNTIL_NAME;
 
420
            else if (c == '/') {
 
421
                ps->nextstate = state; /* return here when done with comment */
 
422
                state = PREF_PARSE_COMMENT_MAYBE_START;
 
423
            }
 
424
            else if (!isspace(c)) {
 
425
                NS_WARNING("malformed pref file");
 
426
                return PR_FALSE;
 
427
            }
 
428
            break;
 
429
        case PREF_PARSE_UNTIL_CLOSE_PAREN:
 
430
            /* tolerate only whitespace and embedded comments  */
 
431
            if (c == ')')
 
432
                state = PREF_PARSE_UNTIL_SEMICOLON;
 
433
            else if (c == '/') {
 
434
                ps->nextstate = state; /* return here when done with comment */
 
435
                state = PREF_PARSE_COMMENT_MAYBE_START;
 
436
            }
 
437
            else if (!isspace(c)) {
 
438
                NS_WARNING("malformed pref file");
 
439
                return PR_FALSE;
 
440
            }
 
441
            break;
 
442
 
 
443
        /* function terminator ';' parsing */
 
444
        case PREF_PARSE_UNTIL_SEMICOLON:
 
445
            /* tolerate only whitespace and embedded comments */
 
446
            if (c == ';') {
 
447
                if (!pref_DoCallback(ps))
 
448
                    return PR_FALSE;
 
449
                state = PREF_PARSE_INIT;
 
450
            }
 
451
            else if (c == '/') {
 
452
                ps->nextstate = state; /* return here when done with comment */
 
453
                state = PREF_PARSE_COMMENT_MAYBE_START;
 
454
            }
 
455
            else if (!isspace(c)) {
 
456
                NS_WARNING("malformed pref file");
 
457
                return PR_FALSE;
 
458
            }
 
459
            break;
 
460
 
 
461
        /* eol parsing */
 
462
        case PREF_PARSE_UNTIL_EOL:
 
463
            /* need to handle mac, unix, or dos line endings.
 
464
             * PREF_PARSE_INIT will eat the next \n in case
 
465
             * we have \r\n. */
 
466
            if (c == '\r' || c == '\n' || c == 0x1A) {
 
467
                state = ps->nextstate;
 
468
                ps->nextstate = PREF_PARSE_INIT; /* reset next state */
 
469
            }
 
470
            break;
 
471
        }
 
472
    }
 
473
    ps->state = state;
 
474
    return PR_TRUE;
 
475
}
 
476
 
 
477
#ifdef TEST_PREFREAD
 
478
 
 
479
static void
 
480
pref_reader(void       *closure, 
 
481
            const char *pref,
 
482
            PrefValue   val,
 
483
            PrefType    type,
 
484
            PRBool      defPref)
 
485
{
 
486
    printf("%spref(\"%s\", ", defPref ? "" : "user_", pref);
 
487
    switch (type) {
 
488
    case PREF_STRING:
 
489
        printf("\"%s\");\n", val.stringVal);
 
490
        break;
 
491
    case PREF_INT:
 
492
        printf("%i);\n", val.intVal);
 
493
        break;
 
494
    case PREF_BOOL:
 
495
        printf("%s);\n", val.boolVal == PR_FALSE ? "false" : "true");
 
496
        break;
 
497
    }
 
498
}
 
499
 
 
500
int
 
501
main(int argc, char **argv)
 
502
{
 
503
    PrefParseState ps;
 
504
    char buf[4096];     /* i/o buffer */
 
505
    FILE *fp;
 
506
    int n;
 
507
 
 
508
    if (argc == 1) {
 
509
        printf("usage: prefread file.js\n");
 
510
        return -1;
 
511
    }
 
512
 
 
513
    fp = fopen(argv[1], "r");
 
514
    if (!fp) {
 
515
        printf("failed to open file\n");
 
516
        return -1;
 
517
    }
 
518
 
 
519
    PREF_InitParseState(&ps, pref_reader, NULL);
 
520
 
 
521
    while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
 
522
        PREF_ParseBuf(&ps, buf, n);
 
523
 
 
524
    PREF_FinalizeParseState(&ps);
 
525
 
 
526
    fclose(fp);
 
527
    return 0;
 
528
}
 
529
 
 
530
#endif /* TEST_PREFREAD */