1*4d131170SRobert Mustacchi /* $Id: mandoc.c,v 1.119 2021/08/10 12:55:03 schwarze Exp $ */
295c635efSGarrett D'Amore /*
3260e9a87SYuri Pankov * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4*4d131170SRobert Mustacchi * Copyright (c) 2011-2015, 2017-2021 Ingo Schwarze <schwarze@openbsd.org>
595c635efSGarrett D'Amore *
695c635efSGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
795c635efSGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
895c635efSGarrett D'Amore * copyright notice and this permission notice appear in all copies.
995c635efSGarrett D'Amore *
1095c635efSGarrett D'Amore * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1195c635efSGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1295c635efSGarrett D'Amore * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1395c635efSGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1495c635efSGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1595c635efSGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1695c635efSGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1795c635efSGarrett D'Amore */
1895c635efSGarrett D'Amore #include "config.h"
1995c635efSGarrett D'Amore
2095c635efSGarrett D'Amore #include <sys/types.h>
2195c635efSGarrett D'Amore
2295c635efSGarrett D'Amore #include <assert.h>
2395c635efSGarrett D'Amore #include <ctype.h>
2495c635efSGarrett D'Amore #include <errno.h>
2595c635efSGarrett D'Amore #include <limits.h>
2695c635efSGarrett D'Amore #include <stdlib.h>
2795c635efSGarrett D'Amore #include <stdio.h>
2895c635efSGarrett D'Amore #include <string.h>
2995c635efSGarrett D'Amore #include <time.h>
3095c635efSGarrett D'Amore
31260e9a87SYuri Pankov #include "mandoc_aux.h"
32c66b8046SYuri Pankov #include "mandoc.h"
33c66b8046SYuri Pankov #include "roff.h"
3495c635efSGarrett D'Amore #include "libmandoc.h"
35cec8643bSMichal Nowak #include "roff_int.h"
3695c635efSGarrett D'Amore
3795c635efSGarrett D'Amore static int a2time(time_t *, const char *, const char *);
3895c635efSGarrett D'Amore static char *time2a(time_t);
3995c635efSGarrett D'Amore
4095c635efSGarrett D'Amore
41cec8643bSMichal Nowak enum mandoc_esc
mandoc_font(const char * cp,int sz)42cec8643bSMichal Nowak mandoc_font(const char *cp, int sz)
43cec8643bSMichal Nowak {
44cec8643bSMichal Nowak switch (sz) {
45cec8643bSMichal Nowak case 0:
46cec8643bSMichal Nowak return ESCAPE_FONTPREV;
47cec8643bSMichal Nowak case 1:
48cec8643bSMichal Nowak switch (cp[0]) {
49cec8643bSMichal Nowak case 'B':
50cec8643bSMichal Nowak case '3':
51cec8643bSMichal Nowak return ESCAPE_FONTBOLD;
52cec8643bSMichal Nowak case 'I':
53cec8643bSMichal Nowak case '2':
54cec8643bSMichal Nowak return ESCAPE_FONTITALIC;
55cec8643bSMichal Nowak case 'P':
56cec8643bSMichal Nowak return ESCAPE_FONTPREV;
57cec8643bSMichal Nowak case 'R':
58cec8643bSMichal Nowak case '1':
59cec8643bSMichal Nowak return ESCAPE_FONTROMAN;
60cec8643bSMichal Nowak case '4':
61cec8643bSMichal Nowak return ESCAPE_FONTBI;
62cec8643bSMichal Nowak default:
63cec8643bSMichal Nowak return ESCAPE_ERROR;
64cec8643bSMichal Nowak }
65cec8643bSMichal Nowak case 2:
66cec8643bSMichal Nowak switch (cp[0]) {
67cec8643bSMichal Nowak case 'B':
68cec8643bSMichal Nowak switch (cp[1]) {
69cec8643bSMichal Nowak case 'I':
70cec8643bSMichal Nowak return ESCAPE_FONTBI;
71cec8643bSMichal Nowak default:
72cec8643bSMichal Nowak return ESCAPE_ERROR;
73cec8643bSMichal Nowak }
74cec8643bSMichal Nowak case 'C':
75cec8643bSMichal Nowak switch (cp[1]) {
76cec8643bSMichal Nowak case 'B':
77*4d131170SRobert Mustacchi return ESCAPE_FONTCB;
78cec8643bSMichal Nowak case 'I':
79*4d131170SRobert Mustacchi return ESCAPE_FONTCI;
80cec8643bSMichal Nowak case 'R':
81cec8643bSMichal Nowak case 'W':
82*4d131170SRobert Mustacchi return ESCAPE_FONTCR;
83cec8643bSMichal Nowak default:
84cec8643bSMichal Nowak return ESCAPE_ERROR;
85cec8643bSMichal Nowak }
86cec8643bSMichal Nowak default:
87cec8643bSMichal Nowak return ESCAPE_ERROR;
88cec8643bSMichal Nowak }
89cec8643bSMichal Nowak default:
90cec8643bSMichal Nowak return ESCAPE_ERROR;
91cec8643bSMichal Nowak }
92cec8643bSMichal Nowak }
93cec8643bSMichal Nowak
94698f87a4SGarrett D'Amore enum mandoc_esc
mandoc_escape(const char ** end,const char ** start,int * sz)95698f87a4SGarrett D'Amore mandoc_escape(const char **end, const char **start, int *sz)
96698f87a4SGarrett D'Amore {
97698f87a4SGarrett D'Amore const char *local_start;
98cec8643bSMichal Nowak int local_sz, c, i;
99698f87a4SGarrett D'Amore char term;
100260e9a87SYuri Pankov enum mandoc_esc gly;
10195c635efSGarrett D'Amore
10295c635efSGarrett D'Amore /*
103698f87a4SGarrett D'Amore * When the caller doesn't provide return storage,
104698f87a4SGarrett D'Amore * use local storage.
10595c635efSGarrett D'Amore */
10695c635efSGarrett D'Amore
107698f87a4SGarrett D'Amore if (NULL == start)
108698f87a4SGarrett D'Amore start = &local_start;
109698f87a4SGarrett D'Amore if (NULL == sz)
110698f87a4SGarrett D'Amore sz = &local_sz;
11195c635efSGarrett D'Amore
112cec8643bSMichal Nowak /*
113cec8643bSMichal Nowak * Treat "\E" just like "\";
114cec8643bSMichal Nowak * it only makes a difference in copy mode.
115cec8643bSMichal Nowak */
116cec8643bSMichal Nowak
117cec8643bSMichal Nowak if (**end == 'E')
118cec8643bSMichal Nowak ++*end;
119cec8643bSMichal Nowak
120698f87a4SGarrett D'Amore /*
121698f87a4SGarrett D'Amore * Beyond the backslash, at least one input character
122698f87a4SGarrett D'Amore * is part of the escape sequence. With one exception
123698f87a4SGarrett D'Amore * (see below), that character won't be returned.
124698f87a4SGarrett D'Amore */
12595c635efSGarrett D'Amore
12695c635efSGarrett D'Amore gly = ESCAPE_ERROR;
127698f87a4SGarrett D'Amore *start = ++*end;
128698f87a4SGarrett D'Amore *sz = 0;
129698f87a4SGarrett D'Amore term = '\0';
13095c635efSGarrett D'Amore
131698f87a4SGarrett D'Amore switch ((*start)[-1]) {
13295c635efSGarrett D'Amore /*
13395c635efSGarrett D'Amore * First the glyphs. There are several different forms of
13495c635efSGarrett D'Amore * these, but each eventually returns a substring of the glyph
13595c635efSGarrett D'Amore * name.
13695c635efSGarrett D'Amore */
137260e9a87SYuri Pankov case '(':
13895c635efSGarrett D'Amore gly = ESCAPE_SPECIAL;
139698f87a4SGarrett D'Amore *sz = 2;
14095c635efSGarrett D'Amore break;
141260e9a87SYuri Pankov case '[':
142cec8643bSMichal Nowak if (**start == ' ') {
143cec8643bSMichal Nowak ++*end;
144cec8643bSMichal Nowak return ESCAPE_ERROR;
145cec8643bSMichal Nowak }
14695c635efSGarrett D'Amore gly = ESCAPE_SPECIAL;
14795c635efSGarrett D'Amore term = ']';
14895c635efSGarrett D'Amore break;
149260e9a87SYuri Pankov case 'C':
150698f87a4SGarrett D'Amore if ('\'' != **start)
151371584c2SYuri Pankov return ESCAPE_ERROR;
152698f87a4SGarrett D'Amore *start = ++*end;
153260e9a87SYuri Pankov gly = ESCAPE_SPECIAL;
15495c635efSGarrett D'Amore term = '\'';
15595c635efSGarrett D'Amore break;
15695c635efSGarrett D'Amore
157698f87a4SGarrett D'Amore /*
158698f87a4SGarrett D'Amore * Escapes taking no arguments at all.
159698f87a4SGarrett D'Amore */
160cec8643bSMichal Nowak case '!':
161cec8643bSMichal Nowak case '?':
162cec8643bSMichal Nowak return ESCAPE_UNSUPP;
163cec8643bSMichal Nowak case '%':
164cec8643bSMichal Nowak case '&':
165cec8643bSMichal Nowak case ')':
166371584c2SYuri Pankov case ',':
167371584c2SYuri Pankov case '/':
168cec8643bSMichal Nowak case '^':
169cec8643bSMichal Nowak case 'a':
170cec8643bSMichal Nowak case 'd':
171cec8643bSMichal Nowak case 'r':
172cec8643bSMichal Nowak case 't':
173cec8643bSMichal Nowak case 'u':
174cec8643bSMichal Nowak case '{':
175cec8643bSMichal Nowak case '|':
176cec8643bSMichal Nowak case '}':
177371584c2SYuri Pankov return ESCAPE_IGNORE;
178cec8643bSMichal Nowak case 'c':
179cec8643bSMichal Nowak return ESCAPE_NOSPACE;
180c66b8046SYuri Pankov case 'p':
181c66b8046SYuri Pankov return ESCAPE_BREAK;
182698f87a4SGarrett D'Amore
183698f87a4SGarrett D'Amore /*
184698f87a4SGarrett D'Amore * The \z escape is supposed to output the following
185260e9a87SYuri Pankov * character without advancing the cursor position.
186698f87a4SGarrett D'Amore * Since we are mostly dealing with terminal mode,
187698f87a4SGarrett D'Amore * let us just skip the next character.
188698f87a4SGarrett D'Amore */
189260e9a87SYuri Pankov case 'z':
190371584c2SYuri Pankov return ESCAPE_SKIPCHAR;
191698f87a4SGarrett D'Amore
19295c635efSGarrett D'Amore /*
19395c635efSGarrett D'Amore * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
19495c635efSGarrett D'Amore * 'X' is the trigger. These have opaque sub-strings.
19595c635efSGarrett D'Amore */
196260e9a87SYuri Pankov case 'F':
197cec8643bSMichal Nowak case 'f':
198260e9a87SYuri Pankov case 'g':
199260e9a87SYuri Pankov case 'k':
200260e9a87SYuri Pankov case 'M':
201260e9a87SYuri Pankov case 'm':
202260e9a87SYuri Pankov case 'n':
203cec8643bSMichal Nowak case 'O':
204260e9a87SYuri Pankov case 'V':
205260e9a87SYuri Pankov case 'Y':
206*4d131170SRobert Mustacchi case '*':
207*4d131170SRobert Mustacchi switch ((*start)[-1]) {
208*4d131170SRobert Mustacchi case 'f':
209*4d131170SRobert Mustacchi gly = ESCAPE_FONT;
210*4d131170SRobert Mustacchi break;
211*4d131170SRobert Mustacchi case '*':
212*4d131170SRobert Mustacchi gly = ESCAPE_DEVICE;
213*4d131170SRobert Mustacchi break;
214*4d131170SRobert Mustacchi default:
215*4d131170SRobert Mustacchi gly = ESCAPE_IGNORE;
216*4d131170SRobert Mustacchi break;
217*4d131170SRobert Mustacchi }
218698f87a4SGarrett D'Amore switch (**start) {
219260e9a87SYuri Pankov case '(':
220cec8643bSMichal Nowak if ((*start)[-1] == 'O')
221cec8643bSMichal Nowak gly = ESCAPE_ERROR;
222698f87a4SGarrett D'Amore *start = ++*end;
223698f87a4SGarrett D'Amore *sz = 2;
22495c635efSGarrett D'Amore break;
225260e9a87SYuri Pankov case '[':
226cec8643bSMichal Nowak if ((*start)[-1] == 'O')
227cec8643bSMichal Nowak gly = (*start)[1] == '5' ?
228cec8643bSMichal Nowak ESCAPE_UNSUPP : ESCAPE_ERROR;
229698f87a4SGarrett D'Amore *start = ++*end;
23095c635efSGarrett D'Amore term = ']';
23195c635efSGarrett D'Amore break;
23295c635efSGarrett D'Amore default:
233cec8643bSMichal Nowak if ((*start)[-1] == 'O') {
234cec8643bSMichal Nowak switch (**start) {
235cec8643bSMichal Nowak case '0':
236cec8643bSMichal Nowak gly = ESCAPE_UNSUPP;
237cec8643bSMichal Nowak break;
238cec8643bSMichal Nowak case '1':
239cec8643bSMichal Nowak case '2':
240cec8643bSMichal Nowak case '3':
241cec8643bSMichal Nowak case '4':
242cec8643bSMichal Nowak break;
243cec8643bSMichal Nowak default:
244cec8643bSMichal Nowak gly = ESCAPE_ERROR;
245cec8643bSMichal Nowak break;
246cec8643bSMichal Nowak }
247cec8643bSMichal Nowak }
248698f87a4SGarrett D'Amore *sz = 1;
24995c635efSGarrett D'Amore break;
25095c635efSGarrett D'Amore }
25195c635efSGarrett D'Amore break;
25295c635efSGarrett D'Amore
25395c635efSGarrett D'Amore /*
25495c635efSGarrett D'Amore * These escapes are of the form \X'Y', where 'X' is the trigger
25595c635efSGarrett D'Amore * and 'Y' is any string. These have opaque sub-strings.
256260e9a87SYuri Pankov * The \B and \w escapes are handled in roff.c, roff_res().
25795c635efSGarrett D'Amore */
258260e9a87SYuri Pankov case 'A':
259260e9a87SYuri Pankov case 'b':
260260e9a87SYuri Pankov case 'D':
261260e9a87SYuri Pankov case 'R':
262260e9a87SYuri Pankov case 'X':
263260e9a87SYuri Pankov case 'Z':
264260e9a87SYuri Pankov gly = ESCAPE_IGNORE;
26595c635efSGarrett D'Amore /* FALLTHROUGH */
266260e9a87SYuri Pankov case 'o':
267260e9a87SYuri Pankov if (**start == '\0')
268371584c2SYuri Pankov return ESCAPE_ERROR;
269260e9a87SYuri Pankov if (gly == ESCAPE_ERROR)
270260e9a87SYuri Pankov gly = ESCAPE_OVERSTRIKE;
271260e9a87SYuri Pankov term = **start;
272698f87a4SGarrett D'Amore *start = ++*end;
27395c635efSGarrett D'Amore break;
27495c635efSGarrett D'Amore
27595c635efSGarrett D'Amore /*
27695c635efSGarrett D'Amore * These escapes are of the form \X'N', where 'X' is the trigger
27795c635efSGarrett D'Amore * and 'N' resolves to a numerical expression.
27895c635efSGarrett D'Amore */
279260e9a87SYuri Pankov case 'h':
280260e9a87SYuri Pankov case 'H':
281260e9a87SYuri Pankov case 'L':
282260e9a87SYuri Pankov case 'l':
283260e9a87SYuri Pankov case 'S':
284260e9a87SYuri Pankov case 'v':
285260e9a87SYuri Pankov case 'x':
286260e9a87SYuri Pankov if (strchr(" %&()*+-./0123456789:<=>", **start)) {
287260e9a87SYuri Pankov if ('\0' != **start)
288260e9a87SYuri Pankov ++*end;
289371584c2SYuri Pankov return ESCAPE_ERROR;
290260e9a87SYuri Pankov }
291c66b8046SYuri Pankov switch ((*start)[-1]) {
292c66b8046SYuri Pankov case 'h':
293c66b8046SYuri Pankov gly = ESCAPE_HORIZ;
294c66b8046SYuri Pankov break;
295c66b8046SYuri Pankov case 'l':
296c66b8046SYuri Pankov gly = ESCAPE_HLINE;
297c66b8046SYuri Pankov break;
298c66b8046SYuri Pankov default:
299c66b8046SYuri Pankov gly = ESCAPE_IGNORE;
300c66b8046SYuri Pankov break;
301c66b8046SYuri Pankov }
302260e9a87SYuri Pankov term = **start;
303698f87a4SGarrett D'Amore *start = ++*end;
30495c635efSGarrett D'Amore break;
30595c635efSGarrett D'Amore
30695c635efSGarrett D'Amore /*
30795c635efSGarrett D'Amore * Special handling for the numbered character escape.
30895c635efSGarrett D'Amore * XXX Do any other escapes need similar handling?
30995c635efSGarrett D'Amore */
310260e9a87SYuri Pankov case 'N':
311698f87a4SGarrett D'Amore if ('\0' == **start)
312371584c2SYuri Pankov return ESCAPE_ERROR;
313698f87a4SGarrett D'Amore (*end)++;
314698f87a4SGarrett D'Amore if (isdigit((unsigned char)**start)) {
315698f87a4SGarrett D'Amore *sz = 1;
316371584c2SYuri Pankov return ESCAPE_IGNORE;
317698f87a4SGarrett D'Amore }
318698f87a4SGarrett D'Amore (*start)++;
31995c635efSGarrett D'Amore while (isdigit((unsigned char)**end))
32095c635efSGarrett D'Amore (*end)++;
321698f87a4SGarrett D'Amore *sz = *end - *start;
32295c635efSGarrett D'Amore if ('\0' != **end)
32395c635efSGarrett D'Amore (*end)++;
324371584c2SYuri Pankov return ESCAPE_NUMBERED;
32595c635efSGarrett D'Amore
326260e9a87SYuri Pankov /*
32795c635efSGarrett D'Amore * Sizes get a special category of their own.
32895c635efSGarrett D'Amore */
329260e9a87SYuri Pankov case 's':
33095c635efSGarrett D'Amore gly = ESCAPE_IGNORE;
33195c635efSGarrett D'Amore
33295c635efSGarrett D'Amore /* See +/- counts as a sign. */
333698f87a4SGarrett D'Amore if ('+' == **end || '-' == **end || ASCII_HYPH == **end)
334260e9a87SYuri Pankov *start = ++*end;
33595c635efSGarrett D'Amore
336698f87a4SGarrett D'Amore switch (**end) {
337260e9a87SYuri Pankov case '(':
338698f87a4SGarrett D'Amore *start = ++*end;
339698f87a4SGarrett D'Amore *sz = 2;
34095c635efSGarrett D'Amore break;
341260e9a87SYuri Pankov case '[':
342698f87a4SGarrett D'Amore *start = ++*end;
343698f87a4SGarrett D'Amore term = ']';
34495c635efSGarrett D'Amore break;
345260e9a87SYuri Pankov case '\'':
346698f87a4SGarrett D'Amore *start = ++*end;
347698f87a4SGarrett D'Amore term = '\'';
34895c635efSGarrett D'Amore break;
349260e9a87SYuri Pankov case '3':
350260e9a87SYuri Pankov case '2':
351260e9a87SYuri Pankov case '1':
352260e9a87SYuri Pankov *sz = (*end)[-1] == 's' &&
353260e9a87SYuri Pankov isdigit((unsigned char)(*end)[1]) ? 2 : 1;
354260e9a87SYuri Pankov break;
35595c635efSGarrett D'Amore default:
356698f87a4SGarrett D'Amore *sz = 1;
35795c635efSGarrett D'Amore break;
35895c635efSGarrett D'Amore }
35995c635efSGarrett D'Amore
36095c635efSGarrett D'Amore break;
36195c635efSGarrett D'Amore
36295c635efSGarrett D'Amore /*
363cec8643bSMichal Nowak * Several special characters can be encoded as
364cec8643bSMichal Nowak * one-byte escape sequences without using \[].
36595c635efSGarrett D'Amore */
366cec8643bSMichal Nowak case ' ':
367cec8643bSMichal Nowak case '\'':
368cec8643bSMichal Nowak case '-':
369cec8643bSMichal Nowak case '.':
370cec8643bSMichal Nowak case '0':
371cec8643bSMichal Nowak case ':':
372cec8643bSMichal Nowak case '_':
373cec8643bSMichal Nowak case '`':
374cec8643bSMichal Nowak case 'e':
375cec8643bSMichal Nowak case '~':
37695c635efSGarrett D'Amore gly = ESCAPE_SPECIAL;
377cec8643bSMichal Nowak /* FALLTHROUGH */
378cec8643bSMichal Nowak default:
379cec8643bSMichal Nowak if (gly == ESCAPE_ERROR)
380cec8643bSMichal Nowak gly = ESCAPE_UNDEF;
381698f87a4SGarrett D'Amore *start = --*end;
382698f87a4SGarrett D'Amore *sz = 1;
38395c635efSGarrett D'Amore break;
38495c635efSGarrett D'Amore }
38595c635efSGarrett D'Amore
38695c635efSGarrett D'Amore /*
387698f87a4SGarrett D'Amore * Read up to the terminating character,
388698f87a4SGarrett D'Amore * paying attention to nested escapes.
38995c635efSGarrett D'Amore */
39095c635efSGarrett D'Amore
39195c635efSGarrett D'Amore if ('\0' != term) {
392698f87a4SGarrett D'Amore while (**end != term) {
393698f87a4SGarrett D'Amore switch (**end) {
394260e9a87SYuri Pankov case '\0':
395371584c2SYuri Pankov return ESCAPE_ERROR;
396260e9a87SYuri Pankov case '\\':
397698f87a4SGarrett D'Amore (*end)++;
398698f87a4SGarrett D'Amore if (ESCAPE_ERROR ==
399698f87a4SGarrett D'Amore mandoc_escape(end, NULL, NULL))
400371584c2SYuri Pankov return ESCAPE_ERROR;
401698f87a4SGarrett D'Amore break;
402698f87a4SGarrett D'Amore default:
403698f87a4SGarrett D'Amore (*end)++;
404698f87a4SGarrett D'Amore break;
405698f87a4SGarrett D'Amore }
406698f87a4SGarrett D'Amore }
407698f87a4SGarrett D'Amore *sz = (*end)++ - *start;
408cec8643bSMichal Nowak
409cec8643bSMichal Nowak /*
410cec8643bSMichal Nowak * The file chars.c only provides one common list
411cec8643bSMichal Nowak * of character names, but \[-] == \- is the only
412cec8643bSMichal Nowak * one of the characters with one-byte names that
413cec8643bSMichal Nowak * allows enclosing the name in brackets.
414cec8643bSMichal Nowak */
415cec8643bSMichal Nowak if (gly == ESCAPE_SPECIAL && *sz == 1 && **start != '-')
416cec8643bSMichal Nowak return ESCAPE_ERROR;
417698f87a4SGarrett D'Amore } else {
418698f87a4SGarrett D'Amore assert(*sz > 0);
419698f87a4SGarrett D'Amore if ((size_t)*sz > strlen(*start))
420371584c2SYuri Pankov return ESCAPE_ERROR;
421698f87a4SGarrett D'Amore *end += *sz;
42295c635efSGarrett D'Amore }
42395c635efSGarrett D'Amore
42495c635efSGarrett D'Amore /* Run post-processors. */
42595c635efSGarrett D'Amore
42695c635efSGarrett D'Amore switch (gly) {
427260e9a87SYuri Pankov case ESCAPE_FONT:
428cec8643bSMichal Nowak gly = mandoc_font(*start, *sz);
429cec8643bSMichal Nowak break;
430cec8643bSMichal Nowak case ESCAPE_SPECIAL:
431cec8643bSMichal Nowak if (**start == 'c') {
432cec8643bSMichal Nowak if (*sz < 6 || *sz > 7 ||
433cec8643bSMichal Nowak strncmp(*start, "char", 4) != 0 ||
434cec8643bSMichal Nowak (int)strspn(*start + 4, "0123456789") + 4 < *sz)
435698f87a4SGarrett D'Amore break;
436cec8643bSMichal Nowak c = 0;
437cec8643bSMichal Nowak for (i = 4; i < *sz; i++)
438cec8643bSMichal Nowak c = 10 * c + ((*start)[i] - '0');
439cec8643bSMichal Nowak if (c < 0x21 || (c > 0x7e && c < 0xa0) || c > 0xff)
440cec8643bSMichal Nowak break;
441cec8643bSMichal Nowak *start += 4;
442cec8643bSMichal Nowak *sz -= 4;
443cec8643bSMichal Nowak gly = ESCAPE_NUMBERED;
44495c635efSGarrett D'Amore break;
44595c635efSGarrett D'Amore }
446cec8643bSMichal Nowak
447260e9a87SYuri Pankov /*
448260e9a87SYuri Pankov * Unicode escapes are defined in groff as \[u0000]
449260e9a87SYuri Pankov * to \[u10FFFF], where the contained value must be
450260e9a87SYuri Pankov * a valid Unicode codepoint. Here, however, only
451260e9a87SYuri Pankov * check the length and range.
452260e9a87SYuri Pankov */
453260e9a87SYuri Pankov if (**start != 'u' || *sz < 5 || *sz > 7)
454260e9a87SYuri Pankov break;
455260e9a87SYuri Pankov if (*sz == 7 && ((*start)[1] != '1' || (*start)[2] != '0'))
456260e9a87SYuri Pankov break;
457260e9a87SYuri Pankov if (*sz == 6 && (*start)[1] == '0')
458260e9a87SYuri Pankov break;
459371584c2SYuri Pankov if (*sz == 5 && (*start)[1] == 'D' &&
460371584c2SYuri Pankov strchr("89ABCDEF", (*start)[2]) != NULL)
461371584c2SYuri Pankov break;
462260e9a87SYuri Pankov if ((int)strspn(*start + 1, "0123456789ABCDEFabcdef")
463260e9a87SYuri Pankov + 1 == *sz)
464260e9a87SYuri Pankov gly = ESCAPE_UNICODE;
46595c635efSGarrett D'Amore break;
466*4d131170SRobert Mustacchi case ESCAPE_DEVICE:
467*4d131170SRobert Mustacchi assert(*sz == 2 && (*start)[0] == '.' && (*start)[1] == 'T');
468*4d131170SRobert Mustacchi break;
46995c635efSGarrett D'Amore default:
47095c635efSGarrett D'Amore break;
47195c635efSGarrett D'Amore }
47295c635efSGarrett D'Amore
473371584c2SYuri Pankov return gly;
47495c635efSGarrett D'Amore }
47595c635efSGarrett D'Amore
47695c635efSGarrett D'Amore static int
a2time(time_t * t,const char * fmt,const char * p)47795c635efSGarrett D'Amore a2time(time_t *t, const char *fmt, const char *p)
47895c635efSGarrett D'Amore {
47995c635efSGarrett D'Amore struct tm tm;
48095c635efSGarrett D'Amore char *pp;
48195c635efSGarrett D'Amore
48295c635efSGarrett D'Amore memset(&tm, 0, sizeof(struct tm));
48395c635efSGarrett D'Amore
48495c635efSGarrett D'Amore pp = NULL;
485260e9a87SYuri Pankov #if HAVE_STRPTIME
48695c635efSGarrett D'Amore pp = strptime(p, fmt, &tm);
48795c635efSGarrett D'Amore #endif
48895c635efSGarrett D'Amore if (NULL != pp && '\0' == *pp) {
48995c635efSGarrett D'Amore *t = mktime(&tm);
490371584c2SYuri Pankov return 1;
49195c635efSGarrett D'Amore }
49295c635efSGarrett D'Amore
493371584c2SYuri Pankov return 0;
49495c635efSGarrett D'Amore }
49595c635efSGarrett D'Amore
49695c635efSGarrett D'Amore static char *
time2a(time_t t)49795c635efSGarrett D'Amore time2a(time_t t)
49895c635efSGarrett D'Amore {
49995c635efSGarrett D'Amore struct tm *tm;
50095c635efSGarrett D'Amore char *buf, *p;
50195c635efSGarrett D'Amore size_t ssz;
50295c635efSGarrett D'Amore int isz;
50395c635efSGarrett D'Amore
504*4d131170SRobert Mustacchi buf = NULL;
50595c635efSGarrett D'Amore tm = localtime(&t);
506260e9a87SYuri Pankov if (tm == NULL)
507*4d131170SRobert Mustacchi goto fail;
50895c635efSGarrett D'Amore
50995c635efSGarrett D'Amore /*
51095c635efSGarrett D'Amore * Reserve space:
51195c635efSGarrett D'Amore * up to 9 characters for the month (September) + blank
51295c635efSGarrett D'Amore * up to 2 characters for the day + comma + blank
51395c635efSGarrett D'Amore * 4 characters for the year and a terminating '\0'
51495c635efSGarrett D'Amore */
515371584c2SYuri Pankov
51695c635efSGarrett D'Amore p = buf = mandoc_malloc(10 + 4 + 4 + 1);
51795c635efSGarrett D'Amore
518371584c2SYuri Pankov if ((ssz = strftime(p, 10 + 1, "%B ", tm)) == 0)
51995c635efSGarrett D'Amore goto fail;
52095c635efSGarrett D'Amore p += (int)ssz;
52195c635efSGarrett D'Amore
522371584c2SYuri Pankov /*
523371584c2SYuri Pankov * The output format is just "%d" here, not "%2d" or "%02d".
524371584c2SYuri Pankov * That's also the reason why we can't just format the
525371584c2SYuri Pankov * date as a whole with "%B %e, %Y" or "%B %d, %Y".
526371584c2SYuri Pankov * Besides, the present approach is less prone to buffer
527371584c2SYuri Pankov * overflows, in case anybody should ever introduce the bug
528371584c2SYuri Pankov * of looking at LC_TIME.
529371584c2SYuri Pankov */
530371584c2SYuri Pankov
531*4d131170SRobert Mustacchi isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday);
532*4d131170SRobert Mustacchi if (isz < 0 || isz > 4)
53395c635efSGarrett D'Amore goto fail;
53495c635efSGarrett D'Amore p += isz;
53595c635efSGarrett D'Amore
536371584c2SYuri Pankov if (strftime(p, 4 + 1, "%Y", tm) == 0)
53795c635efSGarrett D'Amore goto fail;
538371584c2SYuri Pankov return buf;
53995c635efSGarrett D'Amore
54095c635efSGarrett D'Amore fail:
54195c635efSGarrett D'Amore free(buf);
542*4d131170SRobert Mustacchi return mandoc_strdup("");
54395c635efSGarrett D'Amore }
54495c635efSGarrett D'Amore
54595c635efSGarrett D'Amore char *
mandoc_normdate(struct roff_node * nch,struct roff_node * nbl)546*4d131170SRobert Mustacchi mandoc_normdate(struct roff_node *nch, struct roff_node *nbl)
54795c635efSGarrett D'Amore {
548c66b8046SYuri Pankov char *cp;
54995c635efSGarrett D'Amore time_t t;
55095c635efSGarrett D'Amore
551*4d131170SRobert Mustacchi /* No date specified. */
552371584c2SYuri Pankov
553*4d131170SRobert Mustacchi if (nch == NULL) {
554*4d131170SRobert Mustacchi if (nbl == NULL)
555*4d131170SRobert Mustacchi mandoc_msg(MANDOCERR_DATE_MISSING, 0, 0, NULL);
556*4d131170SRobert Mustacchi else
557*4d131170SRobert Mustacchi mandoc_msg(MANDOCERR_DATE_MISSING, nbl->line,
558*4d131170SRobert Mustacchi nbl->pos, "%s", roff_name[nbl->tok]);
559*4d131170SRobert Mustacchi return mandoc_strdup("");
56095c635efSGarrett D'Amore }
561*4d131170SRobert Mustacchi if (*nch->string == '\0') {
562*4d131170SRobert Mustacchi mandoc_msg(MANDOCERR_DATE_MISSING, nch->line,
563*4d131170SRobert Mustacchi nch->pos, "%s", roff_name[nbl->tok]);
564*4d131170SRobert Mustacchi return mandoc_strdup("");
565*4d131170SRobert Mustacchi }
566*4d131170SRobert Mustacchi if (strcmp(nch->string, "$" "Mdocdate$") == 0)
567*4d131170SRobert Mustacchi return time2a(time(NULL));
568371584c2SYuri Pankov
569371584c2SYuri Pankov /* Valid mdoc(7) date format. */
570371584c2SYuri Pankov
571*4d131170SRobert Mustacchi if (a2time(&t, "$" "Mdocdate: %b %d %Y $", nch->string) ||
572*4d131170SRobert Mustacchi a2time(&t, "%b %d, %Y", nch->string)) {
573c66b8046SYuri Pankov cp = time2a(t);
574c66b8046SYuri Pankov if (t > time(NULL) + 86400)
575*4d131170SRobert Mustacchi mandoc_msg(MANDOCERR_DATE_FUTURE, nch->line,
576*4d131170SRobert Mustacchi nch->pos, "%s %s", roff_name[nbl->tok], cp);
577*4d131170SRobert Mustacchi else if (*nch->string != '$' &&
578*4d131170SRobert Mustacchi strcmp(nch->string, cp) != 0)
579*4d131170SRobert Mustacchi mandoc_msg(MANDOCERR_DATE_NORM, nch->line,
580*4d131170SRobert Mustacchi nch->pos, "%s %s", roff_name[nbl->tok], cp);
581c66b8046SYuri Pankov return cp;
582c66b8046SYuri Pankov }
583371584c2SYuri Pankov
584c66b8046SYuri Pankov /* In man(7), do not warn about the legacy format. */
585371584c2SYuri Pankov
586*4d131170SRobert Mustacchi if (a2time(&t, "%Y-%m-%d", nch->string) == 0)
587*4d131170SRobert Mustacchi mandoc_msg(MANDOCERR_DATE_BAD, nch->line, nch->pos,
588*4d131170SRobert Mustacchi "%s %s", roff_name[nbl->tok], nch->string);
589c66b8046SYuri Pankov else if (t > time(NULL) + 86400)
590*4d131170SRobert Mustacchi mandoc_msg(MANDOCERR_DATE_FUTURE, nch->line, nch->pos,
591*4d131170SRobert Mustacchi "%s %s", roff_name[nbl->tok], nch->string);
592*4d131170SRobert Mustacchi else if (nbl->tok == MDOC_Dd)
593*4d131170SRobert Mustacchi mandoc_msg(MANDOCERR_DATE_LEGACY, nch->line, nch->pos,
594*4d131170SRobert Mustacchi "Dd %s", nch->string);
595371584c2SYuri Pankov
596371584c2SYuri Pankov /* Use any non-mdoc(7) date verbatim. */
597371584c2SYuri Pankov
598*4d131170SRobert Mustacchi return mandoc_strdup(nch->string);
59995c635efSGarrett D'Amore }
60095c635efSGarrett D'Amore
60195c635efSGarrett D'Amore int
mandoc_eos(const char * p,size_t sz)602260e9a87SYuri Pankov mandoc_eos(const char *p, size_t sz)
60395c635efSGarrett D'Amore {
604260e9a87SYuri Pankov const char *q;
605260e9a87SYuri Pankov int enclosed, found;
60695c635efSGarrett D'Amore
60795c635efSGarrett D'Amore if (0 == sz)
608371584c2SYuri Pankov return 0;
60995c635efSGarrett D'Amore
61095c635efSGarrett D'Amore /*
61195c635efSGarrett D'Amore * End-of-sentence recognition must include situations where
61295c635efSGarrett D'Amore * some symbols, such as `)', allow prior EOS punctuation to
61395c635efSGarrett D'Amore * propagate outward.
61495c635efSGarrett D'Amore */
61595c635efSGarrett D'Amore
616260e9a87SYuri Pankov enclosed = found = 0;
61795c635efSGarrett D'Amore for (q = p + (int)sz - 1; q >= p; q--) {
61895c635efSGarrett D'Amore switch (*q) {
619260e9a87SYuri Pankov case '\"':
620260e9a87SYuri Pankov case '\'':
621260e9a87SYuri Pankov case ']':
622260e9a87SYuri Pankov case ')':
62395c635efSGarrett D'Amore if (0 == found)
62495c635efSGarrett D'Amore enclosed = 1;
62595c635efSGarrett D'Amore break;
626260e9a87SYuri Pankov case '.':
627260e9a87SYuri Pankov case '!':
628260e9a87SYuri Pankov case '?':
62995c635efSGarrett D'Amore found = 1;
63095c635efSGarrett D'Amore break;
63195c635efSGarrett D'Amore default:
632371584c2SYuri Pankov return found &&
633371584c2SYuri Pankov (!enclosed || isalnum((unsigned char)*q));
63495c635efSGarrett D'Amore }
63595c635efSGarrett D'Amore }
63695c635efSGarrett D'Amore
637371584c2SYuri Pankov return found && !enclosed;
63895c635efSGarrett D'Amore }
63995c635efSGarrett D'Amore
64095c635efSGarrett D'Amore /*
64195c635efSGarrett D'Amore * Convert a string to a long that may not be <0.
64295c635efSGarrett D'Amore * If the string is invalid, or is less than 0, return -1.
64395c635efSGarrett D'Amore */
64495c635efSGarrett D'Amore int
mandoc_strntoi(const char * p,size_t sz,int base)64595c635efSGarrett D'Amore mandoc_strntoi(const char *p, size_t sz, int base)
64695c635efSGarrett D'Amore {
64795c635efSGarrett D'Amore char buf[32];
64895c635efSGarrett D'Amore char *ep;
64995c635efSGarrett D'Amore long v;
65095c635efSGarrett D'Amore
65195c635efSGarrett D'Amore if (sz > 31)
652371584c2SYuri Pankov return -1;
65395c635efSGarrett D'Amore
65495c635efSGarrett D'Amore memcpy(buf, p, sz);
65595c635efSGarrett D'Amore buf[(int)sz] = '\0';
65695c635efSGarrett D'Amore
65795c635efSGarrett D'Amore errno = 0;
65895c635efSGarrett D'Amore v = strtol(buf, &ep, base);
65995c635efSGarrett D'Amore
66095c635efSGarrett D'Amore if (buf[0] == '\0' || *ep != '\0')
661371584c2SYuri Pankov return -1;
66295c635efSGarrett D'Amore
66395c635efSGarrett D'Amore if (v > INT_MAX)
66495c635efSGarrett D'Amore v = INT_MAX;
66595c635efSGarrett D'Amore if (v < INT_MIN)
66695c635efSGarrett D'Amore v = INT_MIN;
66795c635efSGarrett D'Amore
668371584c2SYuri Pankov return (int)v;
66995c635efSGarrett D'Amore }
670