Changeset 3460
- Timestamp:
- 08/09/07 16:05:23 (1 year ago)
- Files:
-
- branches/common-nfo/tools/nfotiser/parser.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/common-nfo/tools/nfotiser/parser.c
r3459 r3460 36 36 #include <err.h> 37 37 #include <errno.h> 38 #include <stdarg.h> 38 39 #include <stdint.h> 39 40 #include <stdio.h> 40 41 #include <stdlib.h> 41 42 #include <string.h> 42 //#include <unistd.h>43 43 44 44 #include "nfotiser.h" 45 46 static void syntaxerr_(size_t, const char *, ...) 47 __attribute__((format (printf, 2, 3))) 48 __attribute__((noreturn)); 49 #define syntaxerr(fmt, ...) syntaxerr_(lineno, (fmt), ##__VA_ARGS__) 45 50 46 51 /* … … 90 95 const struct parser_keywords *kwp; 91 96 char *cp, *t, *tp, *buf, *buf_base; 92 size_t len, n, lineno ;97 size_t len, n, lineno = 0; 93 98 struct stat sb; 94 99 char *entry_multi; … … 118 123 /* now we can operate on the NUL-terminated R/W string “buf” */ 119 124 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!"); 121 126 122 127 D(2, "entire string: «%s» (%zu)\n", buf, strlen(buf)); … … 153 158 D(2, "D: [%4zu] read trail line\n", lineno); 154 159 if (cp == NULL) 155 errx(1, "syntax error: expected lead line," 156 " got trail line!"); 160 syntaxerr("expected lead line, got trail line!"); 157 161 t = ++buf; 158 162 goto loop_storeline; … … 160 164 D(2, "D: [%4zu] read head line (%02X)\n", lineno, *buf); 161 165 } 162 if (cp != NULL) 166 if (cp != NULL) { 167 --lineno; 163 168 goto process_line; 169 } 164 170 if ((*buf >= 'A' && *buf <= 'Z') || 165 171 (*buf >= 'a' && *buf <= 'z') || *buf == '_') 166 172 t = buf; 167 173 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!"); 170 175 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; 174 180 if (cp != NULL) { 175 181 if (*(tp = cp + strlen(cp) - 2) == '\\') 176 182 *tp = '\0'; 177 183 } 184 tp = t + strlen(t) - 1; 185 *tp = '\0'; 186 D(2, "D: [%4zu] storing string '%s'\n", lineno, t); 187 *tp = '\n'; 178 188 tp = str_add(cp, t); 179 189 if (cp != NULL) … … 181 191 free(t); 182 192 cp = tp; 193 ++lineno; 183 194 goto loop_getline; 184 195 process_line: … … 186 197 /* buf points to first byte of next line */ 187 198 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!"); 190 200 process_lastline: 191 201 /* cut off final newline */ 192 202 *++tp = '\0'; 203 D(2, "D: [%4zu] processing «%s»\n", lineno, cp); 193 204 194 205 /* parse the meat out of there */ 195 206 if ((tp = strchr(cp, '\t')) == NULL) 196 errx(1, "syntax error: expected keyword +" 197 " tab + value!"); 207 syntaxerr("expected keyword + tab + value!"); 198 208 *tp++ = '\0'; 199 209 /* cp points to keyword, tp points to value */ … … 225 235 ++zp; 226 236 if (zp == np) 227 errx(1, "syntax error:iterator expected");237 syntaxerr("iterator expected"); 228 238 if (*zp != (char)(kwp->kwtype == KWT_ITERATED ? 229 239 '\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 ? 232 242 "tab" : "underscore", *zp); 233 243 *zp++ = '\0'; … … 251 261 CIRCLEQ_INSERT_TAIL(res, entry, e); 252 262 free(cp); 263 ++lineno; 253 264 goto loop_newline; 254 265 loop_eof: 255 266 if (cp != NULL) { 256 267 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!"); 259 269 goto process_lastline; 260 270 } … … 319 329 free(head); 320 330 } 331 332 static void 333 syntaxerr_(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 }


