]> git.cameronkatri.com Git - mandoc.git/blob - prologue.c
b9dac28a67fe3032689f87196c1c2e935b7dee85
[mandoc.git] / prologue.c
1 /* $Id: prologue.c,v 1.4 2009/01/05 17:57:08 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
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
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include <assert.h>
20 #include <stdlib.h>
21 #ifdef __linux__
22 #include <time.h>
23 #endif
24
25 #include "private.h"
26
27 /* FIXME: deprecate into actions.c! */
28
29 static int prologue_dt(MACRO_PROT_ARGS);
30 static int prologue_dd(MACRO_PROT_ARGS);
31 static int prologue_os(MACRO_PROT_ARGS);
32
33 static int
34 prologue_dt(MACRO_PROT_ARGS)
35 {
36 int lastarg, j;
37 char *args[MDOC_LINEARG_MAX];
38
39 if (SEC_PROLOGUE != mdoc->sec_lastn)
40 return(mdoc_err(mdoc, tok, ppos, ERR_SEC_NPROLOGUE));
41 if (0 == mdoc->meta.date)
42 return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_OO));
43 if (mdoc->meta.title[0])
44 return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_REP));
45
46 j = -1;
47 lastarg = ppos;
48
49 again:
50 if (j == MDOC_LINEARG_MAX)
51 return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY));
52
53 lastarg = *pos;
54
55 switch (mdoc_args(mdoc, tok, pos, buf, 0, &args[++j])) {
56 case (ARGS_EOLN):
57 if (mdoc->meta.title)
58 return(1);
59 if ( ! mdoc_warn(mdoc, tok, ppos, WARN_ARGS_GE1))
60 return(0);
61 (void)xstrlcpy(mdoc->meta.title,
62 "UNTITLED", META_TITLE_SZ);
63 return(1);
64 case (ARGS_ERROR):
65 return(0);
66 default:
67 break;
68 }
69
70 if (0 == j) {
71 if (xstrlcpy(mdoc->meta.title, args[0], META_TITLE_SZ))
72 goto again;
73 return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM));
74
75 } else if (1 == j) {
76 mdoc->meta.msec = mdoc_atomsec(args[1]);
77 if (MSEC_DEFAULT != mdoc->meta.msec)
78 goto again;
79 return(mdoc_err(mdoc, tok, -1, ERR_SYNTAX_ARGFORM));
80
81 } else if (2 == j) {
82 mdoc->meta.vol = mdoc_atovol(args[2]);
83 if (VOL_DEFAULT != mdoc->meta.vol)
84 goto again;
85 mdoc->meta.arch = mdoc_atoarch(args[2]);
86 if (ARCH_DEFAULT != mdoc->meta.arch)
87 goto again;
88 return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM));
89 }
90
91 return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY));
92 }
93
94
95 static int
96 prologue_os(MACRO_PROT_ARGS)
97 {
98 int lastarg, j;
99 char *args[MDOC_LINEARG_MAX];
100
101 /* FIXME: if we use `Os' again... ? */
102
103 if (SEC_PROLOGUE != mdoc->sec_lastn)
104 return(mdoc_err(mdoc, tok, ppos, ERR_SEC_NPROLOGUE));
105 if (0 == mdoc->meta.title[0])
106 return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_OO));
107 if (mdoc->meta.os[0])
108 return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_REP));
109
110 j = -1;
111 lastarg = ppos;
112
113 again:
114 if (j == MDOC_LINEARG_MAX)
115 return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY));
116
117 lastarg = *pos;
118
119 switch (mdoc_args(mdoc, tok, pos, buf,
120 ARGS_QUOTED, &args[++j])) {
121 case (ARGS_EOLN):
122 mdoc->sec_lastn = mdoc->sec_last = SEC_BODY;
123 return(1);
124 case (ARGS_ERROR):
125 return(0);
126 default:
127 break;
128 }
129
130 if ( ! xstrlcat(mdoc->meta.os, args[j], sizeof(mdoc->meta.os)))
131 return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM));
132 if ( ! xstrlcat(mdoc->meta.os, " ", sizeof(mdoc->meta.os)))
133 return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM));
134
135 goto again;
136 /* NOTREACHED */
137 }
138
139
140 static int
141 prologue_dd(MACRO_PROT_ARGS)
142 {
143 int lastarg, j;
144 char *args[MDOC_LINEARG_MAX], date[64];
145
146 if (SEC_PROLOGUE != mdoc->sec_lastn)
147 return(mdoc_err(mdoc, tok, ppos, ERR_SEC_NPROLOGUE));
148 if (mdoc->meta.title[0])
149 return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_OO));
150 if (mdoc->meta.date)
151 return(mdoc_err(mdoc, tok, ppos, ERR_SEC_PROLOGUE_REP));
152
153 j = -1;
154 date[0] = 0;
155 lastarg = ppos;
156
157 again:
158 if (j == MDOC_LINEARG_MAX)
159 return(mdoc_err(mdoc, tok, lastarg, ERR_ARGS_MANY));
160
161 lastarg = *pos;
162 switch (mdoc_args(mdoc, tok, pos, buf, 0, &args[++j])) {
163 case (ARGS_EOLN):
164 if (mdoc->meta.date)
165 return(1);
166 mdoc->meta.date = mdoc_atotime(date);
167 if (mdoc->meta.date)
168 return(1);
169 return(mdoc_err(mdoc, tok, ppos, ERR_SYNTAX_ARGFORM));
170 case (ARGS_ERROR):
171 return(0);
172 default:
173 break;
174 }
175
176 if (MDOC_MAX != mdoc_find(mdoc, args[j]) && ! mdoc_warn
177 (mdoc, tok, lastarg, WARN_SYNTAX_MACLIKE))
178 return(0);
179
180 if (0 == j) {
181 if (xstrcmp("$Mdocdate: January 5 2009 $", args[j])) {
182 mdoc->meta.date = time(NULL);
183 goto again;
184 } else if (xstrcmp("$Mdocdate:", args[j]))
185 goto again;
186 } else if (4 == j)
187 if ( ! xstrcmp("$", args[j]))
188 goto again;
189
190 if ( ! xstrlcat(date, args[j], sizeof(date)))
191 return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM));
192 if ( ! xstrlcat(date, " ", sizeof(date)))
193 return(mdoc_err(mdoc, tok, lastarg, ERR_SYNTAX_ARGFORM));
194
195 goto again;
196 /* NOTREACHED */
197 }
198
199
200 int
201 macro_prologue(MACRO_PROT_ARGS)
202 {
203
204 switch (tok) {
205 case (MDOC_Dt):
206 return(prologue_dt(mdoc, tok, ppos, pos, buf));
207 case (MDOC_Dd):
208 return(prologue_dd(mdoc, tok, ppos, pos, buf));
209 case (MDOC_Os):
210 return(prologue_os(mdoc, tok, ppos, pos, buf));
211 default:
212 break;
213 }
214
215 abort();
216 /* NOTREACHED */
217 }
218