]> git.cameronkatri.com Git - mandoc.git/blob - tbl.c
Use `Dl' instead of `D1' for code examples.
[mandoc.git] / tbl.c
1 /* $Id: tbl.c,v 1.7 2010/12/29 14:53:31 kristaps Exp $ */
2 /*
3 * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <time.h>
22
23 #include "mandoc.h"
24 #include "roff.h"
25 #include "libmandoc.h"
26 #include "libroff.h"
27
28 static void tbl_init(struct tbl *);
29 static void tbl_clear(struct tbl *);
30
31 static void
32 tbl_clear(struct tbl *tbl)
33 {
34 struct tbl_row *rp;
35 struct tbl_cell *cp;
36
37 while (tbl->first) {
38 rp = tbl->first;
39 tbl->first = rp->next;
40 while (rp->first) {
41 cp = rp->first;
42 rp->first = cp->next;
43 free(cp);
44 }
45 free(rp);
46 }
47
48 tbl->last = NULL;
49 }
50
51 static void
52 tbl_init(struct tbl *tbl)
53 {
54
55 tbl->part = TBL_PART_OPTS;
56 tbl->tab = '\t';
57 tbl->linesize = 12;
58 tbl->decimal = '.';
59 }
60
61 enum rofferr
62 tbl_read(struct tbl *tbl, int ln, const char *p, int offs)
63 {
64 int len;
65 const char *cp;
66
67 cp = &p[offs];
68 len = (int)strlen(cp);
69
70 /*
71 * If we're in the options section and we don't have a
72 * terminating semicolon, assume we've moved directly into the
73 * layout section. No need to report a warning: this is,
74 * apparently, standard behaviour.
75 */
76
77 if (TBL_PART_OPTS == tbl->part && len)
78 if (';' != cp[len - 1])
79 tbl->part = TBL_PART_LAYOUT;
80
81 /* Now process each logical section of the table. */
82
83 switch (tbl->part) {
84 case (TBL_PART_OPTS):
85 return(tbl_option(tbl, ln, p) ? ROFF_IGN : ROFF_ERR);
86 case (TBL_PART_LAYOUT):
87 return(tbl_layout(tbl, ln, p) ? ROFF_IGN : ROFF_ERR);
88 default:
89 break;
90 }
91
92 return(ROFF_CONT);
93 }
94
95 struct tbl *
96 tbl_alloc(void *data, const mandocmsg msg)
97 {
98 struct tbl *p;
99
100 p = mandoc_calloc(1, sizeof(struct tbl));
101 p->data = data;
102 p->msg = msg;
103 tbl_init(p);
104 return(p);
105 }
106
107 void
108 tbl_free(struct tbl *p)
109 {
110
111 tbl_clear(p);
112 free(p);
113 }
114
115 void
116 tbl_reset(struct tbl *tbl)
117 {
118
119 tbl_clear(tbl);
120 tbl_init(tbl);
121 }
122
123 void
124 tbl_restart(struct tbl *tbl)
125 {
126
127 tbl_clear(tbl);
128 tbl->part = TBL_PART_LAYOUT;
129 }
130