File: parse.c

package info (click to toggle)
mgt 2.31-7
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 460 kB
  • ctags: 582
  • sloc: ansic: 4,852; sh: 411; pascal: 329; makefile: 88; csh: 3
file content (502 lines) | stat: -rw-r--r-- 8,295 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/* "mgt" Copyright (c) 1991 Shodan  */

#include <string.h>
#include "mgt.h"


struct {
   char *str;
   Token val;
}  tokens[] = {
   {
      "W", t_White
   },
   {
      "B", t_Black
   },
   {
      "C", t_Comment
   },
   {
      "AW", t_AddWhite
   },
   {
      "AB", t_AddBlack
   },
   {
      "L", t_Letter
   },
   {
      "AE", t_AddEmpty
   },
   {
      "N", t_Name
   },
   {
      "M", t_Mark
   },
   {
      "SZ", t_Size
   },
   {
      "GN", t_GameName
   },
   {
      "GC", t_GameComment
   },
   {
      "EV", t_Event
   },
   {
      "RO", t_Round
   },
   {
      "DT", t_Date
   },
   {
      "PC", t_Place
   },
   {
      "PB", t_PlayerBlack
   },
   {
      "PW", t_PlayerWhite
   },
   {
      "RE", t_Result
   },
   {
      "US", t_User
   },
   {
      "TM", t_TimeLimit
   },
   {
      "SO", t_Source
   },
   {
      "BR", t_BlackRank
   },
   {
      "WR", t_WhiteRank
   },
   {
      "HA", t_Handicap
   },
   {
      "KM", t_Komi
   }, {
      "PL", t_Player
   }

};


char buf[1028], *curin, started;

FUNCTION void readInit()
{
   int p;
   buf[0] = '\0';
   curin = &buf[0];
   started = 1;
   boardsize = 19;
   handicap = 0;
   for (p = 0; p <= LASTINFO - FIRSTINFO + 1; p++) {
      if (info[p])
	 free(info[p]);
      info[p] = 0;
   }
}


static char *readLine()
{
   curin = &buf[0];
   buf[0] = '\0';
   return fgets(&buf[0], 1023, input);
}


static char readChar()
{
   if (*curin)
      return *(curin++);
   curin = &buf[0];
   buf[0] = '\0';
   fgets(&buf[0], 1023, input);
   return 0;
}



static int getCoordStr(co)
coord *co;
{
   if (strlen(curin) < 4 && curin[strlen(curin) - 1] != '\n') {
      char *dest;
      for (dest = buf; *curin; curin++, dest++)
	 *dest = *curin;
      *dest = 0;
      fgets(dest, 1023, input);
      curin = buf;
   }
   if (curin[0] == '[' && curin[1] >= 'a' && curin[1] <= 's'
       && curin[2] >= 'a' && curin[2] <= 's' && curin[3] == ']') {
      co->x = curin[1] - 'a';
      co->y = curin[2] - 'a';
      curin += 4;
      while (*curin == ' ' || *curin == '\t')
	 curin++;
      if (*curin == '\n')
	 readLine();
      return 1;
   } else
      return 0;
}


static void getCoordList(clp)
coordList **clp;
{
   coord co;
   while (getCoordStr(&co)) {
      setCoord(co.x, co.y, clp);
   }
}

static void addPass(n, t)
nodep n;
Token t;
{
   property *p;
   if (!(p = (property *) calloc(1, sizeof(property))))
      barf("Memory allocation error (addPass)");
   p->t = t_Pass;
   p->data.player = t;
   addprop(n, p);

}



static void doPlayer(n)
nodep n;

{
   property *p;

   if (curin[0] == '[' && curin[2] == ']' && (curin[1] == 'B' || curin[1] == 'W')) {
      p = (property *) calloc(1, sizeof(property));
      if (!p)
	 barf("Memory allocation failure (doPlayer)");
      p->t = t_Player;
      p->data.player = curin[1] == 'B' ? t_Black : t_White;
      curin += 3;
      addprop(n, p);
   }
}


static Token tokenize()
{
   int i, len;
   char buf[4];

   len = 0;
   do {
      if (!*curin || *curin == '\n') {
	 if (!readLine())
	    return t_EOF;
      }
      if (*curin == ')') {
	 curin++;
	 return t_Close;
      }
      if (*curin == '(') {
	 curin++;
	 return t_Open;
      }
      if (*curin == ';') {
	 curin++;
	 return t_NewNode;
      }
      if (*curin >= 'A' && *curin <= 'Z')
	 buf[len++] = *curin;
      curin++;
   } while (len < 3 && *curin != '[');
   buf[len] = 0;
   if (len == 1 || len == 2)
      for (i = 0; i < sizeof(tokens) / sizeof(tokens[0]); i++)
	 if (!strcmp(buf, tokens[i].str))
	    return tokens[i].val;
   while (*curin != ']') {
      if (!*++curin) {
	 if (!readLine())
	    return t_EOF;
      }
   }
   return t_WS;
}



static void addMoveArrayProp(t, n)
Token t;
nodep n;
{
   property *p;

#ifdef DEBUG
   totalmemory += sizeof(property);
   fprintf(debug, "%ld %ld\n", totalmemory, coreleft());
#endif
   if (!(p = getprop(n, t))) {
      p = (property *) calloc(1, sizeof(property));

#ifdef DEBUG
      totalmemory += sizeof(coordList);
      fprintf(debug, "%ld %ld\n", totalmemory, coreleft());
#endif

      if (!p)
	 barf("Memory allocation failure (addMoveArrayProp)");

      p->data.stones = 0;
      p->t = t;
      addprop(n, p);
   }
   getCoordList(&(p->data.stones));
}


static void doSize()
{
   int size;
   char *s, c, b[25];
   s = &b[0];
   readChar();
   while ((c = readChar()) != ']')
      if (c && c != '\\')
	 *s++ = c;
   *s = 0;
   size = atoi(b);
   if ((size > 1) && (size <= 19))
      boardsize = size;
}



static void doHandicap()
{
   int size;
   char *s, c, b[25];
   s = &b[0];
   readChar();
   while ((c = readChar()) != ']')
      if (c && c != '\\')
	 *s++ = c;
   *s = 0;
   size = atoi(b);
   if ((size > 1) && (size <= 17))
      handicap = size;
}


/* gets text.  Assumes we start on a '['.  Returns pointer to a buffer which
 * should be freed */

static char *getText()
{
   int c;
   char *s;
   char *buffer;
   int space = COMMENTALLOC;
   int blocks = 1;

   buffer = (char *) malloc(COMMENTALLOC);
   if (!buffer)
      barf("Memory Allocation Failure (getText)");
   s = buffer;

   (void) readChar();
   while ((c = readChar()) != ']') {
      if (!c) {
	 c = readChar();
	 if (!c || c == ']')
	    break;
      }
      if (space <= 1) {
	 buffer = (char *) realloc(buffer, ++blocks * COMMENTALLOC);
	 if (!buffer)
	    barf("Memory Allocation Failure (getText)");
	 s = buffer - space + (blocks - 1) * COMMENTALLOC;
	 space += COMMENTALLOC;
      }
      if (c == '\\')
	 c = readChar();
      if (c) {
	 *s++ = c;
	 space--;
      }
   }
   if (s != buffer && *(s - 1) == '\n')
      s--;
   *s = 0;
   return buffer;
}


static void doComment(n, t)
nodep n;
Token t;
{
   property *p;
   char *buffer;

   p = (property *) calloc(1, sizeof(property));
   if (!p)
      barf("Memory Allocation Failure (doComment)");
   buffer = getText();
   p->t = t;
   p->data.comment = dupStr(buffer);
   addprop(n, p);
   free(buffer);
}


static void doInfo(t)
Token t;
{
   char *buffer;

   buffer = getText();
   info[(int) t - FIRSTINFO] = dupStr(buffer);
   free(buffer);
}


FUNCTION void addChild(n, c)	/* add child c to node n */
nodep n, c;
{
   if (n) {
      if (n->child) {
	 nodep s;
	 s = treeLastSibling(child(n));
	 s->nextSibling = c;
	 c->lastSibling = s;
	 c->parent = n;
      } else {
	 n->child = c;
	 c->parent = n;
      }
   }
}


FUNCTION nodep parse(lev)
int lev;
{
   nodep r, n, new;
   Token t;

   if (started) {
      started = 0;

      while (*curin != '(') {
	 if (!readLine())
	    break;
      }
   }
   r = n = 0;
   for (;;) {
      t = tokenize();
      switch (t) {
	 case t_Size:
	    doSize();
	    break;
	 case t_White:
	 case t_Black:
	    if (curin[1] == 't' && curin[2] == 't' && curin[0] == '[' && curin[3] == ']') {
	       addPass(n, t);
	       break;
	    }
	 case t_AddWhite:
	 case t_AddBlack:
	 case t_AddEmpty:
	 case t_Mark:
	 case t_Letter:
	    if (n)
	       addMoveArrayProp(t, n);
	    else
	       fprintf(stderr, "Error - property w/o node in data\n");
	    break;
	 case t_Player:
	    if (n)
	       doPlayer(n);
	    else
	       fprintf(stderr, "Error - property w/o node in data\n");
	    break;
	 case t_Name:
	 case t_Comment:
	    if (n)
	       doComment(n, t);
	    else
	       fprintf(stderr, "Error - property w/o node in data\n");
	    break;
	 case t_NewNode:
	    new = newNode();
	    if (!r)
	       r = new;
	    else
	       addChild(n, new);
	    n = new;
	    break;
	 case t_Open:
	    if (lev == 1 && !r)
	       n = r = newNode();
	    if (new = parse(lev + 1)) {
	       addChild(n, new);
	       if (!r)
		  r = new;
	    }
	    break;
	 case t_Close:
	 case t_EOF:
	    return r;

	 case t_GameName:
	 case t_GameComment:
	 case t_Event:
	 case t_Round:
	 case t_Date:
	 case t_Place:
	 case t_PlayerBlack:
	 case t_PlayerWhite:
	 case t_Result:
	 case t_User:
	 case t_TimeLimit:
	 case t_Source:
	 case t_BlackRank:
	 case t_WhiteRank:

	    doInfo(t);
	    break;
	 case t_Komi:{
	       char *ch, *end;
	       doInfo(t_Komi);
	       end = ch = info[(int) t_Komi - FIRSTINFO];
	       while (*end)
		  end++;
	       for (end--; *end == '0' && (end != ch); end--);
	       *++end = 0;
	    }
	    break;
	 case t_Handicap:
	    doHandicap();
	    break;
	 default:
	    break;
      }
   }
}