]> git.pond.sub.org Git - empserver/blob - src/lib/common/xundump.c
(xundump): Eat whitespace after table, so that caller can check for
[empserver] / src / lib / common / xundump.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  xundump.c: Load back xdump output
29  * 
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2005
32  *     Markus Armbruster, 2005
33  */
34
35 /* FIXME normalize terminology: table/rows/columns or file/records/fields */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 #include <ctype.h>
41 #include <string.h>
42 #include <stdarg.h>
43 #include <time.h>
44
45 #include "prototypes.h"
46 #include "file.h"
47 #include "nsc.h"
48 #include "match.h"
49
50 #define MAX_NUM_COLUMNS 256
51
52 static char *fname = "";
53 static int lineno = 0;
54
55 /*
56  * TODO
57  * This structure could be replaced with struct valstr.
58  */
59 enum enum_value {
60     VAL_NOTUSED,
61     VAL_STRING,
62     VAL_SYMBOL,
63     VAL_SYMBOL_SET,
64     VAL_DOUBLE
65 };
66
67 struct value {
68     enum enum_value v_type;
69     union {
70         char *v_string;
71         double v_double;
72     } v_field;
73 };
74
75 static int gripe(char *fmt, ...) ATTRIBUTE((format (printf, 1, 2)));
76
77 static int
78 gripe(char *fmt, ...)
79 {
80     va_list ap;
81
82     fprintf(stderr, "%s:%d: ", fname, lineno);
83     va_start(ap, fmt);
84     vfprintf(stderr, fmt, ap);
85     va_end(ap);
86     putc('\n', stderr);
87
88     return -1;
89 }
90
91 static int
92 skipfs(FILE *fp)
93 {
94     int ch;
95
96     do {
97         ch = getc(fp);
98     } while (ch == ' ' || ch == '\t');
99
100     if (ch == '#') {
101         do {
102             ch = getc(fp);
103         } while (ch != EOF && ch != '\n');
104     }
105
106     return ch;
107 }
108
109 static int
110 getid(FILE *fp, char *buf)
111 {
112     int n;
113     if (fscanf(fp, "%1023[^#() \t\n]%n", buf, &n) != 1 || !isalpha(buf[0]))
114         return -1;
115     return n;
116 }
117
118 static char *
119 xuesc(char *buf)
120 {
121     char *src, *dst;
122     int octal_chr, n;
123
124     dst = buf;
125     src = buf;
126     while (*src) {
127         if (*src == '\\') {
128             if (sscanf(++src, "%3o%n", &octal_chr, &n) != 1 || n != 3)
129                 return NULL;
130             *dst++ = (char)octal_chr;
131             src += 3;
132         } else
133             *dst++ = *src++;
134     }
135     *dst = '\0';
136     return buf;
137 }
138
139 static int
140 xuflds(FILE *fp, struct value values[])
141 {
142     int i, ch;
143     char buf[1024];
144     char *p;
145     int len, l1;
146
147     for (i = 0; ; i++) {
148         values[i].v_type = VAL_NOTUSED;
149         if (i >= MAX_NUM_COLUMNS)
150             return gripe("Too many columns");
151
152         ch = skipfs(fp);
153         switch (ch) {
154         case EOF:
155             return gripe("Unexpected EOF");
156         case '\n':
157             return i;
158         case '+': case '-': case '.':
159         case '0': case '1': case '2': case '3': case '4':
160         case '5': case '6': case '7': case '8': case '9':
161             ungetc(ch, fp);
162             if (fscanf(fp, "%lg", &values[i].v_field.v_double) != 1)
163                 return gripe("Malformed number in field %d", i + 1);
164             values[i].v_type = VAL_DOUBLE;
165             break;
166         case '"':
167             ch = getc(fp);
168             if (ch == '"')
169                 buf[0] = 0;
170             else {
171                 ungetc(ch, fp);
172                 if (fscanf(fp, "%1023[^\"\n]", buf) != 1 || getc(fp) != '"')
173                     return gripe("Malformed string in field %d", i + 1);
174                 if (!xuesc(buf))
175                     return gripe("Invalid escape sequence in field %d",
176                                  i + 1);
177             }
178             values[i].v_type = VAL_STRING;
179             values[i].v_field.v_string = strdup(buf);
180             break;
181         case '(':
182             p = strdup("");
183             len = 0;
184             for (;;) {
185                 ch = skipfs(fp);
186                 if (ch == EOF || ch == '\n')
187                     return gripe("Unmatched '(' in field %d", i + 1);
188                 if (ch == ')')
189                     break;
190                 ungetc(ch, fp);
191                 l1 = getid(fp, buf);
192                 if (l1 < 0)
193                     return gripe("Junk in field %d", i + 1);
194                 p = realloc(p, len + l1 + 2);
195                 strcpy(p + len, buf);
196                 strcpy(p + len + l1, " ");
197                 len += l1 + 1;
198             }
199             if (len)
200                 p[len - 1] = 0;
201             values[i].v_type = VAL_SYMBOL_SET;
202             values[i].v_field.v_string = p;
203             break;
204         default:
205             ungetc(ch, fp);
206             if (getid(fp, buf) < 0) {
207                 return gripe("Junk in field %d", i + 1);
208             }
209             if (!strcmp(buf, "nil")) {
210                 values[i].v_type = VAL_STRING;
211                 values[i].v_field.v_string = NULL;
212             }
213             else {
214                 values[i].v_type = VAL_SYMBOL;
215                 values[i].v_field.v_string = strdup(buf);
216             }
217         }
218         ch = getc(fp);
219         if (ch == '\n')
220             ungetc(ch, fp);
221         else if (ch != ' ' && ch != '\t')
222             return gripe("Bad field separator after field %d", i + 1);
223     }
224 }
225
226 static void
227 freeflds(struct value values[])
228 {
229     struct value *vp;
230
231     for (vp = values; vp->v_type != VAL_NOTUSED; vp++) {
232         if (vp->v_type != VAL_DOUBLE)
233             free(vp->v_field.v_string);
234     }
235 }
236
237 static int
238 xunsymbol1(char *id, struct symbol *symtab, struct castr *ca, int n)
239 {
240     int i = stmtch(id, symtab, offsetof(struct symbol, name),
241                    sizeof(struct symbol));
242     if (i < 0)
243         return gripe("%s %s symbol `%s' in field %d",
244                      i == M_NOTUNIQUE ? "Ambiguous" : "Unknown",
245                      ca->ca_name, id, n);
246     return i;
247 }
248
249 static int
250 xunsymbol(struct castr *ca, struct value *vp, int n)
251 {
252     struct symbol *symtab = (struct symbol *)empfile[ca->ca_table].cache;
253     char *buf = vp->v_field.v_string;
254     int i;
255     int value;
256     char *token;
257
258     if (vp->v_type == VAL_SYMBOL_SET) {
259         if (!(ca->ca_flags & NSC_BITS) || ca->ca_table == EF_BAD)
260             return gripe("%s doesn't take a symbol set in field %d",
261                          ca->ca_name, n);
262         value = 0;
263         for (token = strtok(buf, " "); token; token = strtok(NULL, " ")) {
264             i = xunsymbol1(token, symtab, ca, n);
265             if (i < 0)
266                 return -1;
267             value |= symtab[i].value;
268         }
269     } else if (vp->v_type == VAL_SYMBOL) {
270         if ((ca->ca_flags & NSC_BITS) || ca->ca_table == EF_BAD)
271             return gripe("%s doesn't take a symbol in field %d",
272                          ca->ca_name, n);
273         i = xunsymbol1(buf, symtab, ca, n);
274         if (i < 0)
275             return -1;
276         value = symtab[i].value;
277     } else
278         return 0;
279
280     vp->v_type = VAL_DOUBLE;
281     vp->v_field.v_double = value;
282     return 0;
283 }
284
285 static int
286 has_const(struct castr ca[])
287 {
288     int i;
289
290     for (i = 0; ca[i].ca_name; i++) {
291         if (ca[i].ca_flags & NSC_CONST)
292             return 1;
293     }
294     return 0;
295 }
296
297 static void
298 xuinitrow(int type, int row)
299 {
300     struct empfile *ep = &empfile[type];
301     char *ptr = ep->cache + ep->size * row;
302
303     memset(ptr, 0, ep->size);
304
305     if (ep->init)
306         ep->init(row, ptr);
307 }
308
309 static int
310 xuloadrow(int type, int row, struct value values[])
311 {
312     int i,j,k;
313     struct empfile *ep = &empfile[type];
314     char *ptr = ep->cache + ep->size * row;
315     struct castr *ca = ep->cadef;
316     void *row_ref;
317
318     i = 0;
319     j = 0;
320     while (ca[i].ca_type != NSC_NOTYPE &&
321            values[j].v_type != VAL_NOTUSED) {
322         if (ca[i].ca_flags & NSC_EXTRA) {
323             i++;
324             continue;
325         }
326         row_ref = (char *)ptr + ca[i].ca_off;
327         k = 0;
328         do {
329             /*
330              * TODO
331              * factor out NSC_CONST comparsion
332              */
333             switch (values[j].v_type) {
334             case VAL_SYMBOL:
335             case VAL_SYMBOL_SET:
336                 if (xunsymbol(&ca[i], &values[j], j) < 0)
337                     return -1;
338                 /* fall through */
339             case VAL_DOUBLE:
340                 switch (ca[i].ca_type) {
341                 case NSC_INT:
342                     if (ca[i].ca_flags & NSC_CONST) {
343                         if (((int *)row_ref)[k] != (int)
344                              values[j].v_field.v_double)
345                             gripe("Field %s must be same, "
346                                 "read %d != expected %d",
347                                 ca[i].ca_name,
348                                 ((int *)row_ref)[k],
349                                 (int)values[j].v_field.v_double);
350
351                     } else
352                         ((int *)row_ref)[k] =
353                             (int)values[j].v_field.v_double;
354                     break;
355                 case NSC_LONG:
356                     if (ca[i].ca_flags & NSC_CONST) {
357                         if (((long *)row_ref)[k] != (long)
358                              values[j].v_field.v_double)
359                             gripe("Field %s must be same, "
360                                 "read %ld != expected %ld",
361                                 ca[i].ca_name,
362                                 ((long *)row_ref)[k],
363                                 (long)values[j].v_field.v_double);
364                     } else
365                         ((long *)row_ref)[k] = (long)
366                             values[j].v_field.v_double;
367                     break;
368                 case NSC_USHORT:
369                     if (ca[i].ca_flags & NSC_CONST) {
370                         if (((unsigned short *)row_ref)[k] !=
371                              (unsigned short)values[j].v_field.v_double)
372                             gripe("Field %s must be same, "
373                                 "read %d != expected %d",
374                                 ca[i].ca_name,
375                                 ((unsigned short *)row_ref)[k],
376                                 (unsigned short)values[j].v_field.v_double);
377                     } else
378                         ((unsigned short *)row_ref)[k] = (unsigned short)
379                             values[j].v_field.v_double;
380                     break;
381                 case NSC_UCHAR:
382                     if (ca[i].ca_flags & NSC_CONST) {
383                         if (((unsigned char *)row_ref)[k] != (unsigned char)
384                              values[j].v_field.v_double)
385                             gripe("Field %s must be same, "
386                                 "read %d != expected %d",
387                                 ca[i].ca_name,
388                                 ((unsigned char *)row_ref)[k],
389                                 (unsigned char)values[j].v_field.v_double);
390                     } else
391                         ((unsigned char *)row_ref)[k] = (unsigned char)
392                             values[j].v_field.v_double;
393                     break;
394                 case NSC_FLOAT:
395                     if (ca[i].ca_flags & NSC_CONST) {
396                         if (((float *)row_ref)[k] != (float)
397                              values[j].v_field.v_double)
398                             gripe("Field %s must be same, "
399                                 "read %g != expected %g",
400                                 ca[i].ca_name,
401                                 ((float *)row_ref)[k],
402                                 (float)values[j].v_field.v_double);
403                     } else
404                         ((float *)row_ref)[k] = (float)
405                             values[j].v_field.v_double;
406                     break;
407                 case NSC_STRING:
408                     return gripe("Field %s is a string type, "
409                         "but %lg was read which is a number",
410                         ca[i].ca_name, values[j].v_field.v_double);
411                 default:
412                     return gripe("Field %s's type %d is not supported",
413                         ca[i].ca_name, ca[i].ca_type);
414                 }
415                 break;
416             case VAL_STRING:
417                 switch(ca[i].ca_type) {
418                 case NSC_STRING:
419                     if (ca[i].ca_flags & NSC_CONST) {
420                         if (strcmp(((char **)row_ref)[k],
421                                    values[j].v_field.v_string) != 0)
422                             gripe("Field %s must be same, "
423                                 "read %s != expected %s",
424                                 ca[i].ca_name,
425                                 ((char **)row_ref)[k],
426                                 values[j].v_field.v_string);
427                     } else
428                         ((char **)row_ref)[k]
429                             = strdup(values[j].v_field.v_string);
430                     break;
431                 case NSC_INT:
432                 case NSC_LONG:
433                 case NSC_USHORT:
434                 case NSC_UCHAR:
435                 case NSC_FLOAT:
436                     return gripe("Field %s is a number type %d, "
437                         "but %s was read which is a string",
438                         ca[i].ca_name, ca[i].ca_type,
439                         values[j].v_field.v_string);
440                 default:
441                     return gripe("Field %s's type %d is not supported",
442                             ca[i].ca_name, ca[i].ca_type);
443                 }
444                 break;
445             case VAL_NOTUSED:
446                 return gripe("Missing column %s in file", ca[i].ca_name);
447             default:
448                 return gripe("Unknown value type %d", values[j].v_type);
449             }
450             k++;
451             j++;
452         } while (k < ca[i].ca_len);
453         i++;
454     }
455     if (ca[i].ca_type != NSC_NOTYPE)
456         return gripe("Missing column %s in file", ca[i].ca_name);
457     switch  (values[j].v_type) {
458     case VAL_NOTUSED:
459         break;
460     case VAL_STRING:
461     case VAL_SYMBOL:
462     case VAL_SYMBOL_SET:
463         return gripe("Extra junk after the last column, read %s",
464             values[j].v_field.v_string);
465     case VAL_DOUBLE:
466         return gripe("Extra junk after the last column, read %lg",
467             values[j].v_field.v_double);
468     default:
469         return gripe("Extra junk after the last column, "
470             "unknown value type %d", values[j].v_type);
471     }
472     return 0;
473 }
474
475 int
476 xundump(FILE *fp, char *file, int expected_table)
477 {
478     char name[64];
479     struct empfile *ep;
480     int row, res, rows, ch;
481     struct value values[MAX_NUM_COLUMNS + 1];
482     int type;
483     int fixed_rows, need_sentinel;
484
485     if (strcmp(fname, file) != 0) {
486         fname = file;
487         lineno = 1;
488     } else
489         lineno++;
490
491     while ((ch = skipfs(fp)) == '\n')
492         lineno++;
493     if (ch == EOF && expected_table == EF_BAD)
494         return -1;
495     ungetc(ch, fp);
496
497     res = -1;
498     if (fscanf(fp, "XDUMP%*[ \t]%63[^ \t#\n]%*[ \t]%*[^ \t#\n]%n",
499                name, &res) != 1
500         || res < 0)
501         return gripe("Expected XDUMP header");
502     if (skipfs(fp) != '\n')
503         return gripe("Junk after XDUMP header");
504
505     type = ef_byname(name);
506     if (type < 0)
507         return gripe("Unknown table `%s'", name);
508     ep = &empfile[type];
509     if (CANT_HAPPEN(!(ep->flags & EFF_MEM)))
510         return -1;              /* not implemented */
511
512     if (expected_table != EF_BAD && expected_table != type)
513         return gripe("Expected table `%s', not `%s'",
514                      ef_nameof(expected_table), name);
515
516     fixed_rows = has_const(ef_cadef(type));
517     need_sentinel = !fixed_rows; /* FIXME only approximation */
518
519     row = 0;
520     for (;;) {
521         lineno++;
522         ch = skipfs(fp);
523         if (ch == '/')
524             break;
525         ungetc(ch, fp);
526         /*
527          * TODO
528          * Add column count check to the return value of xuflds()
529          */
530         res = xuflds(fp, values);
531         if (res > 0 && row >= ep->csize - 1) {
532             /* TODO grow cache unless EFF_STATIC */
533             gripe("Too many rows for table %s", name);
534             res = -1;
535         }
536         if (res > 0) {
537             if (!fixed_rows)
538                 xuinitrow(type, row);
539             res = xuloadrow(type, row, values);
540             row++;
541         }
542         freeflds(values);
543         if (res < 0)
544             return -1;
545     }
546
547     ch = getc(fp);
548     ungetc(ch, fp);
549     if (!isdigit(ch) || fscanf(fp, "%d", &rows) != 1)
550         return gripe("Malformed table footer");
551     if (skipfs(fp) != '\n')
552         return gripe("Junk after table footer");
553     if (row != rows)
554         return gripe("Read %d rows, which doesn't match footer",
555                      row);
556     if (fixed_rows && row != ep->csize -1)
557         return gripe("Table %s requires %d rows, got %d",
558                      name, ep->csize - 1, row);
559     if (need_sentinel)
560         xuinitrow(type, row);
561
562     while ((ch = skipfs(fp)) == '\n') ;
563     ungetc(ch, fp);
564
565     ep->fids = ep->cids = row;
566     return type;
567 }