Changeset 3460

Show
Ignore:
Timestamp:
08/09/07 16:05:23 (1 year ago)
Author:
tg
Message:

fix these bugs and add line numbers to syntax errors as bonus

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/common-nfo/tools/nfotiser/parser.c

    r3459 r3460  
    3636#include <err.h> 
    3737#include <errno.h> 
     38#include <stdarg.h> 
    3839#include <stdint.h> 
    3940#include <stdio.h> 
    4041#include <stdlib.h> 
    4142#include <string.h> 
    42 //#include <unistd.h> 
    4343 
    4444#include "nfotiser.h" 
     45 
     46static void syntaxerr_(size_t, const char *, ...) 
     47    __attribute__((format (printf, 2, 3))) 
     48    __attribute__((noreturn)); 
     49#define syntaxerr(fmt, ...)     syntaxerr_(lineno, (fmt), ##__VA_ARGS__) 
    4550 
    4651/* 
     
    9095        const struct parser_keywords *kwp; 
    9196        char *cp, *t, *tp, *buf, *buf_base; 
    92         size_t len, n, lineno
     97        size_t len, n, lineno = 0
    9398        struct stat sb; 
    9499        char *entry_multi; 
     
    118123        /* now we can operate on the NUL-terminated R/W string “buf” */ 
    119124        if (buf[len - 1] != '\n') 
    120                 errx(1, "syntax error: file does not end with a newline!"); 
     125                syntaxerr("file does not end with a newline!"); 
    121126 
    122127        D(2, "entire string: «%s» (%zu)\n", buf, strlen(buf)); 
     
    153158                D(2, "D: [%4zu] read trail line\n", lineno); 
    154159                if (cp == NULL) 
    155                         errx(1, "syntax error: expected lead line," 
    156                             " got trail line!"); 
     160                        syntaxerr("expected lead line, got trail line!"); 
    157161                t = ++buf; 
    158162                goto loop_storeline; 
     
    160164                D(2, "D: [%4zu] read head line (%02X)\n", lineno, *buf); 
    161165        } 
    162         if (cp != NULL) 
     166        if (cp != NULL) { 
     167                --lineno; 
    163168                goto process_line; 
     169        } 
    164170        if ((*buf >= 'A' && *buf <= 'Z') || 
    165171            (*buf >= 'a' && *buf <= 'z') || *buf == '_') 
    166172                t = buf; 
    167173        else 
    168                 errx(1, "syntax error: line must begin with" 
    169                     " a letter or underscore!"); 
     174                syntaxerr("line must begin with a letter or an underscore!"); 
    170175 loop_storeline: 
    171         while (*t != '\n') 
    172                 ++t; 
    173         t = str_nsave(buf, t - buf + /* NL */ 1); 
     176        while (*t++ != '\n') 
     177                ; 
     178        t = str_nsave(buf, (tp = t) - buf); 
     179        buf = tp; 
    174180        if (cp != NULL) { 
    175181                if (*(tp = cp + strlen(cp) - 2) == '\\') 
    176182                        *tp = '\0'; 
    177183        } 
     184        tp = t + strlen(t) - 1; 
     185        *tp = '\0'; 
     186        D(2, "D: [%4zu] storing string '%s'\n", lineno, t); 
     187        *tp = '\n'; 
    178188        tp = str_add(cp, t); 
    179189        if (cp != NULL) 
     
    181191        free(t); 
    182192        cp = tp; 
     193        ++lineno; 
    183194        goto loop_getline; 
    184195 process_line: 
     
    186197        /* buf points to first byte of next line */ 
    187198        if (*(tp = cp + strlen(cp) - 2) == '\\') 
    188                 errx(1, "syntax error: expected trail line," 
    189                     " got lead line!"); 
     199                syntaxerr("expected trail line, got lead line!"); 
    190200 process_lastline: 
    191201        /* cut off final newline */ 
    192202        *++tp = '\0'; 
     203        D(2, "D: [%4zu] processing «%s»\n", lineno, cp); 
    193204 
    194205        /* parse the meat out of there */ 
    195206        if ((tp = strchr(cp, '\t')) == NULL) 
    196                 errx(1, "syntax error: expected keyword +" 
    197                     " tab + value!"); 
     207                syntaxerr("expected keyword + tab + value!"); 
    198208        *tp++ = '\0'; 
    199209        /* cp points to keyword, tp points to value */ 
     
    225235                                ++zp; 
    226236                        if (zp == np) 
    227                                 errx(1, "syntax error: iterator expected"); 
     237                                syntaxerr("iterator expected"); 
    228238                        if (*zp != (char)(kwp->kwtype == KWT_ITERATED ? 
    229239                            '\0' : '_')) 
    230                                 errx(1, "syntax error: %s expected," 
    231                                     " got 0x%02X", kwp->kwtype == KWT_ITERATED ? 
     240                                syntaxerr("%s expected, got 0x%02X", 
     241                                    kwp->kwtype == KWT_ITERATED ? 
    232242                                    "tab" : "underscore", *zp); 
    233243                        *zp++ = '\0'; 
     
    251261        CIRCLEQ_INSERT_TAIL(res, entry, e); 
    252262        free(cp); 
     263        ++lineno; 
    253264        goto loop_newline; 
    254265 loop_eof: 
    255266        if (cp != NULL) { 
    256267                if (*(tp = cp + strlen(cp) - 2) == '\\') 
    257                         errx(1, "syntax error: expected trail line," 
    258                             " read end of file!"); 
     268                        syntaxerr("expected trail line, read end of file!"); 
    259269                goto process_lastline; 
    260270        } 
     
    319329        free(head); 
    320330} 
     331 
     332static void 
     333syntaxerr_(size_t lno, const char *fmt, ...) 
     334{ 
     335        va_list args; 
     336 
     337        va_start(args, fmt); 
     338        fflush(NULL); 
     339        fprintf(stderr, "syntax error [%4zu]: ", lno); 
     340        fflush(NULL); 
     341        verrx(1, fmt, args); 
     342        va_end(args); 
     343}