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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
/* $Id: tbl.c,v 1.1 2010/10/15 19:20:03 schwarze Exp $ */
/*
* Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tbl.h"
#include "tbl_extern.h"
const char *const errnames[ERR_MAX] = {
"bad syntax", /* ERR_SYNTAX */
"bad option" /* ERR_OPTION */
};
static char buf[1024]; /* XXX */
static enum tbl_tok tbl_next_char(char);
static void tbl_init(struct tbl *);
static void tbl_clear(struct tbl *);
static struct tbl_head *tbl_head_alloc(struct tbl *);
static void tbl_span_free(struct tbl_span *);
static void tbl_data_free(struct tbl_data *);
static void tbl_row_free(struct tbl_row *);
static void headadj(const struct tbl_cell *,
struct tbl_head *);
static void
tbl_init(struct tbl *tbl)
{
bzero(tbl, sizeof(struct tbl));
tbl->part = TBL_PART_OPTS;
tbl->tab = '\t';
tbl->linesize = 12;
tbl->decimal = '.';
TAILQ_INIT(&tbl->span);
TAILQ_INIT(&tbl->row);
TAILQ_INIT(&tbl->head);
}
int
tbl_read(struct tbl *tbl, const char *f, int ln, const char *p, int len)
{
if (len && TBL_PART_OPTS == tbl->part)
if (';' != p[len - 1])
tbl->part = TBL_PART_LAYOUT;
switch (tbl->part) {
case (TBL_PART_OPTS):
return(tbl_option(tbl, f, ln, p));
case (TBL_PART_CLAYOUT):
/* FALLTHROUGH */
case (TBL_PART_LAYOUT):
return(tbl_layout(tbl, f, ln, p));
case (TBL_PART_DATA):
return(tbl_data(tbl, f, ln, p));
case (TBL_PART_ERROR):
break;
}
return(0);
}
int
tbl_close(struct tbl *tbl, const char *f, int ln)
{
if (TBL_PART_DATA != tbl->part)
return(tbl_errx(tbl, ERR_SYNTAX, f, ln, 0));
if ( ! tbl_data_close(tbl, f, ln))
return(0);
#if 1
return(tbl_calc_term(tbl));
#else
return(tbl_calc_tree(tbl));
#endif
}
int
tbl_write(const struct tbl *tbl)
{
#if 1
return(tbl_write_term(tbl));
#else
return(tbl_write_tree(tbl));
#endif
}
static enum tbl_tok
tbl_next_char(char c)
{
/*
* These are delimiting tokens. They separate out words in the
* token stream.
*/
switch (c) {
case ('('):
return(TBL_TOK_OPENPAREN);
case (')'):
return(TBL_TOK_CLOSEPAREN);
case (' '):
return(TBL_TOK_SPACE);
case ('\t'):
return(TBL_TOK_TAB);
case (';'):
return(TBL_TOK_SEMICOLON);
case ('.'):
return(TBL_TOK_PERIOD);
case (','):
return(TBL_TOK_COMMA);
case (0):
return(TBL_TOK_NIL);
default:
break;
}
return(TBL_TOK_WORD);
}
const char *
tbl_last(void)
{
return(buf);
}
int
tbl_last_uint(void)
{
char *ep;
long lval;
/* From OpenBSD's strtol(3). Gross. */
errno = 0;
lval = strtol(buf, &ep, 10);
if (buf[0] == 0 || *ep != 0)
return(-1);
if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
return(-1);
if (lval < 0 || lval > INT_MAX)
return(-1);
return((int)lval);
}
enum tbl_tok
tbl_next(const char *p, int *pos)
{
int i;
enum tbl_tok c;
buf[0] = 0;
if (TBL_TOK_WORD != (c = tbl_next_char(p[*pos]))) {
if (TBL_TOK_NIL != c) {
buf[0] = p[*pos];
buf[1] = 0;
(*pos)++;
}
return(c);
}
/*
* Copy words into a nil-terminated buffer. For now, we use a
* static buffer. Eventually this should be made into a dynamic
* one living in struct tbl.
*/
for (i = 0; i < 1023; i++, (*pos)++)
if (TBL_TOK_WORD == tbl_next_char(p[*pos]))
buf[i] = p[*pos];
else
break;
assert(i < 1023);
buf[i] = 0;
return(TBL_TOK_WORD);
}
int
tbl_err(struct tbl *tbl)
{
(void)fprintf(stderr, "%s\n", strerror(errno));
tbl->part = TBL_PART_ERROR;
return(0);
}
/* ARGSUSED */
int
tbl_warnx(struct tbl *tbl, enum tbl_err tok,
const char *f, int line, int pos)
{
(void)fprintf(stderr, "%s:%d:%d: %s\n",
f, line, pos + 1, errnames[tok]);
/* TODO: -Werror */
return(1);
}
int
tbl_errx(struct tbl *tbl, enum tbl_err tok,
const char *f, int line, int pos)
{
(void)fprintf(stderr, "%s:%d:%d: %s\n",
f, line, pos + 1, errnames[tok]);
tbl->part = TBL_PART_ERROR;
return(0);
}
struct tbl *
tbl_alloc(void)
{
struct tbl *p;
if (NULL == (p = malloc(sizeof(struct tbl))))
return(NULL);
tbl_init(p);
return(p);
}
void
tbl_free(struct tbl *p)
{
tbl_clear(p);
free(p);
}
void
tbl_reset(struct tbl *tbl)
{
tbl_clear(tbl);
tbl_init(tbl);
}
struct tbl_span *
tbl_span_alloc(struct tbl *tbl)
{
struct tbl_span *p, *pp;
struct tbl_row *row;
if (NULL == (p = calloc(1, sizeof(struct tbl_span)))) {
(void)tbl_err(tbl);
return(NULL);
}
TAILQ_INIT(&p->data);
TAILQ_INSERT_TAIL(&tbl->span, p, entries);
/* LINTED */
pp = TAILQ_PREV(p, tbl_spanh, entries);
if (pp) {
row = TAILQ_NEXT(pp->row, entries);
if (NULL == row)
row = pp->row;
} else {
row = TAILQ_FIRST(&tbl->row);
}
assert(row);
p->row = row;
p->tbl = tbl;
return(p);
}
struct tbl_row *
tbl_row_alloc(struct tbl *tbl)
{
struct tbl_row *p;
if (NULL == (p = calloc(1, sizeof(struct tbl_row)))) {
(void)tbl_err(tbl);
return(NULL);
}
TAILQ_INIT(&p->cell);
TAILQ_INSERT_TAIL(&tbl->row, p, entries);
p->tbl = tbl;
return(p);
}
static void
headadj(const struct tbl_cell *cell, struct tbl_head *head)
{
if (TBL_CELL_VERT != cell->pos &&
TBL_CELL_DVERT != cell->pos) {
head->pos = TBL_HEAD_DATA;
return;
}
if (TBL_CELL_VERT == cell->pos)
if (TBL_HEAD_DVERT != head->pos)
head->pos = TBL_HEAD_VERT;
if (TBL_CELL_DVERT == cell->pos)
head->pos = TBL_HEAD_DVERT;
}
static struct tbl_head *
tbl_head_alloc(struct tbl *tbl)
{
struct tbl_head *p;
if (NULL == (p = calloc(1, sizeof(struct tbl_head)))) {
(void)tbl_err(tbl);
return(NULL);
}
p->tbl = tbl;
return(p);
}
struct tbl_cell *
tbl_cell_alloc(struct tbl_row *rp, enum tbl_cellt pos)
{
struct tbl_cell *p, *pp;
struct tbl_head *h, *hp;
if (NULL == (p = calloc(1, sizeof(struct tbl_cell)))) {
(void)tbl_err(rp->tbl);
return(NULL);
}
TAILQ_INSERT_TAIL(&rp->cell, p, entries);
p->pos = pos;
p->row = rp;
/*
* This is a little bit complicated. Here we determine the
* header the corresponds to a cell. We add headers dynamically
* when need be or re-use them, otherwise. As an example, given
* the following:
*
* 1 c || l
* 2 | c | l
* 3 l l
* 3 || c | l |.
*
* We first add the new headers (as there are none) in (1); then
* in (2) we insert the first spanner (as it doesn't match up
* with the header); then we re-use the prior data headers,
* skipping over the spanners; then we re-use everything and add
* a last spanner. Note that VERT headers are made into DVERT
* ones.
*/
/* LINTED */
pp = TAILQ_PREV(p, tbl_cellh, entries);
h = pp ? TAILQ_NEXT(pp->head, entries) :
TAILQ_FIRST(&rp->tbl->head);
if (h) {
/* Re-use data header. */
if (TBL_HEAD_DATA == h->pos &&
(TBL_CELL_VERT != p->pos &&
TBL_CELL_DVERT != p->pos)) {
p->head = h;
return(p);
}
/* Re-use spanner header. */
if (TBL_HEAD_DATA != h->pos &&
(TBL_CELL_VERT == p->pos ||
TBL_CELL_DVERT == p->pos)) {
headadj(p, h);
p->head = h;
return(p);
}
/* Right-shift headers with a new spanner. */
if (TBL_HEAD_DATA == h->pos &&
(TBL_CELL_VERT == p->pos ||
TBL_CELL_DVERT == p->pos)) {
if (NULL == (hp = tbl_head_alloc(rp->tbl)))
return(NULL);
TAILQ_INSERT_BEFORE(h, hp, entries);
headadj(p, hp);
p->head = hp;
return(p);
}
h = TAILQ_NEXT(h, entries);
if (h) {
headadj(p, h);
p->head = h;
return(p);
}
/* Fall through to default case... */
}
if (NULL == (hp = tbl_head_alloc(rp->tbl)))
return(NULL);
TAILQ_INSERT_TAIL(&rp->tbl->head, hp, entries);
headadj(p, hp);
p->head = hp;
return(p);
}
struct tbl_data *
tbl_data_alloc(struct tbl_span *sp)
{
struct tbl_data *p;
struct tbl_cell *cp;
struct tbl_data *dp;
if (NULL == (p = calloc(1, sizeof(struct tbl_data)))) {
(void)tbl_err(sp->row->tbl);
return(NULL);
}
cp = NULL;
/* LINTED */
if (NULL == (dp = TAILQ_LAST(&sp->data, tbl_datah)))
cp = TAILQ_FIRST(&sp->row->cell);
else if (dp->cell)
cp = TAILQ_NEXT(dp->cell, entries);
TAILQ_INSERT_TAIL(&sp->data, p, entries);
if (cp && (TBL_CELL_VERT == cp->pos ||
TBL_CELL_DVERT == cp->pos))
cp = TAILQ_NEXT(cp, entries);
p->span = sp;
p->cell = cp;
return(p);
}
static void
tbl_clear(struct tbl *p)
{
struct tbl_span *span;
struct tbl_head *head;
struct tbl_row *row;
/* LINTED */
while ((span = TAILQ_FIRST(&p->span))) {
TAILQ_REMOVE(&p->span, span, entries);
tbl_span_free(span);
}
/* LINTED */
while ((row = TAILQ_FIRST(&p->row))) {
TAILQ_REMOVE(&p->row, row, entries);
tbl_row_free(row);
}
/* LINTED */
while ((head = TAILQ_FIRST(&p->head))) {
TAILQ_REMOVE(&p->head, head, entries);
free(head);
}
}
static void
tbl_span_free(struct tbl_span *p)
{
struct tbl_data *data;
/* LINTED */
while ((data = TAILQ_FIRST(&p->data))) {
TAILQ_REMOVE(&p->data, data, entries);
tbl_data_free(data);
}
free(p);
}
static void
tbl_data_free(struct tbl_data *p)
{
if (p->string)
free(p->string);
free(p);
}
static void
tbl_row_free(struct tbl_row *p)
{
struct tbl_cell *cell;
/* LINTED */
while ((cell = TAILQ_FIRST(&p->cell))) {
TAILQ_REMOVE(&p->cell, cell, entries);
free(cell);
}
free(p);
}
|