]> git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
Cleanup, no functional change:
[mandoc.git] / mandoc.c
1 /* $Id: mandoc.c,v 1.110 2018/12/14 06:33:14 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2015, 2017, 2018 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "mandoc_aux.h"
32 #include "mandoc.h"
33 #include "roff.h"
34 #include "libmandoc.h"
35
36 static int a2time(time_t *, const char *, const char *);
37 static char *time2a(time_t);
38
39
40 enum mandoc_esc
41 mandoc_escape(const char **end, const char **start, int *sz)
42 {
43 const char *local_start;
44 int local_sz, c, i;
45 char term;
46 enum mandoc_esc gly;
47
48 /*
49 * When the caller doesn't provide return storage,
50 * use local storage.
51 */
52
53 if (NULL == start)
54 start = &local_start;
55 if (NULL == sz)
56 sz = &local_sz;
57
58 /*
59 * Beyond the backslash, at least one input character
60 * is part of the escape sequence. With one exception
61 * (see below), that character won't be returned.
62 */
63
64 gly = ESCAPE_ERROR;
65 *start = ++*end;
66 *sz = 0;
67 term = '\0';
68
69 switch ((*start)[-1]) {
70 /*
71 * First the glyphs. There are several different forms of
72 * these, but each eventually returns a substring of the glyph
73 * name.
74 */
75 case '(':
76 gly = ESCAPE_SPECIAL;
77 *sz = 2;
78 break;
79 case '[':
80 gly = ESCAPE_SPECIAL;
81 term = ']';
82 break;
83 case 'C':
84 if ('\'' != **start)
85 return ESCAPE_ERROR;
86 *start = ++*end;
87 gly = ESCAPE_SPECIAL;
88 term = '\'';
89 break;
90
91 /*
92 * Escapes taking no arguments at all.
93 */
94 case 'd':
95 case 'u':
96 case ',':
97 case '/':
98 return ESCAPE_IGNORE;
99 case 'p':
100 return ESCAPE_BREAK;
101
102 /*
103 * The \z escape is supposed to output the following
104 * character without advancing the cursor position.
105 * Since we are mostly dealing with terminal mode,
106 * let us just skip the next character.
107 */
108 case 'z':
109 return ESCAPE_SKIPCHAR;
110
111 /*
112 * Handle all triggers matching \X(xy, \Xx, and \X[xxxx], where
113 * 'X' is the trigger. These have opaque sub-strings.
114 */
115 case 'F':
116 case 'g':
117 case 'k':
118 case 'M':
119 case 'm':
120 case 'n':
121 case 'V':
122 case 'Y':
123 gly = ESCAPE_IGNORE;
124 /* FALLTHROUGH */
125 case 'f':
126 if (ESCAPE_ERROR == gly)
127 gly = ESCAPE_FONT;
128 switch (**start) {
129 case '(':
130 *start = ++*end;
131 *sz = 2;
132 break;
133 case '[':
134 *start = ++*end;
135 term = ']';
136 break;
137 default:
138 *sz = 1;
139 break;
140 }
141 break;
142 case '*':
143 if (strncmp(*start, "(.T", 3) != 0)
144 abort();
145 gly = ESCAPE_DEVICE;
146 *start = ++*end;
147 *sz = 2;
148 break;
149
150 /*
151 * These escapes are of the form \X'Y', where 'X' is the trigger
152 * and 'Y' is any string. These have opaque sub-strings.
153 * The \B and \w escapes are handled in roff.c, roff_res().
154 */
155 case 'A':
156 case 'b':
157 case 'D':
158 case 'R':
159 case 'X':
160 case 'Z':
161 gly = ESCAPE_IGNORE;
162 /* FALLTHROUGH */
163 case 'o':
164 if (**start == '\0')
165 return ESCAPE_ERROR;
166 if (gly == ESCAPE_ERROR)
167 gly = ESCAPE_OVERSTRIKE;
168 term = **start;
169 *start = ++*end;
170 break;
171
172 /*
173 * These escapes are of the form \X'N', where 'X' is the trigger
174 * and 'N' resolves to a numerical expression.
175 */
176 case 'h':
177 case 'H':
178 case 'L':
179 case 'l':
180 case 'S':
181 case 'v':
182 case 'x':
183 if (strchr(" %&()*+-./0123456789:<=>", **start)) {
184 if ('\0' != **start)
185 ++*end;
186 return ESCAPE_ERROR;
187 }
188 switch ((*start)[-1]) {
189 case 'h':
190 gly = ESCAPE_HORIZ;
191 break;
192 case 'l':
193 gly = ESCAPE_HLINE;
194 break;
195 default:
196 gly = ESCAPE_IGNORE;
197 break;
198 }
199 term = **start;
200 *start = ++*end;
201 break;
202
203 /*
204 * Special handling for the numbered character escape.
205 * XXX Do any other escapes need similar handling?
206 */
207 case 'N':
208 if ('\0' == **start)
209 return ESCAPE_ERROR;
210 (*end)++;
211 if (isdigit((unsigned char)**start)) {
212 *sz = 1;
213 return ESCAPE_IGNORE;
214 }
215 (*start)++;
216 while (isdigit((unsigned char)**end))
217 (*end)++;
218 *sz = *end - *start;
219 if ('\0' != **end)
220 (*end)++;
221 return ESCAPE_NUMBERED;
222
223 /*
224 * Sizes get a special category of their own.
225 */
226 case 's':
227 gly = ESCAPE_IGNORE;
228
229 /* See +/- counts as a sign. */
230 if ('+' == **end || '-' == **end || ASCII_HYPH == **end)
231 *start = ++*end;
232
233 switch (**end) {
234 case '(':
235 *start = ++*end;
236 *sz = 2;
237 break;
238 case '[':
239 *start = ++*end;
240 term = ']';
241 break;
242 case '\'':
243 *start = ++*end;
244 term = '\'';
245 break;
246 case '3':
247 case '2':
248 case '1':
249 *sz = (*end)[-1] == 's' &&
250 isdigit((unsigned char)(*end)[1]) ? 2 : 1;
251 break;
252 default:
253 *sz = 1;
254 break;
255 }
256
257 break;
258
259 /*
260 * Anything else is assumed to be a glyph.
261 * In this case, pass back the character after the backslash.
262 */
263 default:
264 gly = ESCAPE_SPECIAL;
265 *start = --*end;
266 *sz = 1;
267 break;
268 }
269
270 assert(ESCAPE_ERROR != gly);
271
272 /*
273 * Read up to the terminating character,
274 * paying attention to nested escapes.
275 */
276
277 if ('\0' != term) {
278 while (**end != term) {
279 switch (**end) {
280 case '\0':
281 return ESCAPE_ERROR;
282 case '\\':
283 (*end)++;
284 if (ESCAPE_ERROR ==
285 mandoc_escape(end, NULL, NULL))
286 return ESCAPE_ERROR;
287 break;
288 default:
289 (*end)++;
290 break;
291 }
292 }
293 *sz = (*end)++ - *start;
294 } else {
295 assert(*sz > 0);
296 if ((size_t)*sz > strlen(*start))
297 return ESCAPE_ERROR;
298 *end += *sz;
299 }
300
301 /* Run post-processors. */
302
303 switch (gly) {
304 case ESCAPE_FONT:
305 if (*sz == 2) {
306 if (**start == 'C') {
307 if ((*start)[1] == 'W' ||
308 (*start)[1] == 'R') {
309 gly = ESCAPE_FONTCW;
310 break;
311 }
312 /*
313 * Treat other constant-width font modes
314 * just like regular font modes.
315 */
316 (*start)++;
317 (*sz)--;
318 } else {
319 if ((*start)[0] == 'B' && (*start)[1] == 'I')
320 gly = ESCAPE_FONTBI;
321 break;
322 }
323 } else if (*sz != 1) {
324 if (*sz == 0)
325 gly = ESCAPE_FONTPREV;
326 break;
327 }
328
329 switch (**start) {
330 case '3':
331 case 'B':
332 gly = ESCAPE_FONTBOLD;
333 break;
334 case '2':
335 case 'I':
336 gly = ESCAPE_FONTITALIC;
337 break;
338 case 'P':
339 gly = ESCAPE_FONTPREV;
340 break;
341 case '1':
342 case 'R':
343 gly = ESCAPE_FONTROMAN;
344 break;
345 }
346 break;
347 case ESCAPE_SPECIAL:
348 if (**start == 'c') {
349 if (*sz == 1) {
350 gly = ESCAPE_NOSPACE;
351 break;
352 }
353 if (*sz < 6 || *sz > 7 ||
354 strncmp(*start, "char", 4) != 0 ||
355 (int)strspn(*start + 4, "0123456789") + 4 < *sz)
356 break;
357 c = 0;
358 for (i = 4; i < *sz; i++)
359 c = 10 * c + ((*start)[i] - '0');
360 if (c < 0x21 || (c > 0x7e && c < 0xa0) || c > 0xff)
361 break;
362 *start += 4;
363 *sz -= 4;
364 gly = ESCAPE_NUMBERED;
365 break;
366 }
367
368 /*
369 * Unicode escapes are defined in groff as \[u0000]
370 * to \[u10FFFF], where the contained value must be
371 * a valid Unicode codepoint. Here, however, only
372 * check the length and range.
373 */
374 if (**start != 'u' || *sz < 5 || *sz > 7)
375 break;
376 if (*sz == 7 && ((*start)[1] != '1' || (*start)[2] != '0'))
377 break;
378 if (*sz == 6 && (*start)[1] == '0')
379 break;
380 if (*sz == 5 && (*start)[1] == 'D' &&
381 strchr("89ABCDEF", (*start)[2]) != NULL)
382 break;
383 if ((int)strspn(*start + 1, "0123456789ABCDEFabcdef")
384 + 1 == *sz)
385 gly = ESCAPE_UNICODE;
386 break;
387 default:
388 break;
389 }
390
391 return gly;
392 }
393
394 /*
395 * Parse a quoted or unquoted roff-style request or macro argument.
396 * Return a pointer to the parsed argument, which is either the original
397 * pointer or advanced by one byte in case the argument is quoted.
398 * NUL-terminate the argument in place.
399 * Collapse pairs of quotes inside quoted arguments.
400 * Advance the argument pointer to the next argument,
401 * or to the NUL byte terminating the argument line.
402 */
403 char *
404 mandoc_getarg(char **cpp, int ln, int *pos)
405 {
406 char *start, *cp;
407 int quoted, pairs, white;
408
409 /* Quoting can only start with a new word. */
410 start = *cpp;
411 quoted = 0;
412 if ('"' == *start) {
413 quoted = 1;
414 start++;
415 }
416
417 pairs = 0;
418 white = 0;
419 for (cp = start; '\0' != *cp; cp++) {
420
421 /*
422 * Move the following text left
423 * after quoted quotes and after "\\" and "\t".
424 */
425 if (pairs)
426 cp[-pairs] = cp[0];
427
428 if ('\\' == cp[0]) {
429 /*
430 * In copy mode, translate double to single
431 * backslashes and backslash-t to literal tabs.
432 */
433 switch (cp[1]) {
434 case 't':
435 cp[0] = '\t';
436 /* FALLTHROUGH */
437 case '\\':
438 pairs++;
439 cp++;
440 break;
441 case ' ':
442 /* Skip escaped blanks. */
443 if (0 == quoted)
444 cp++;
445 break;
446 default:
447 break;
448 }
449 } else if (0 == quoted) {
450 if (' ' == cp[0]) {
451 /* Unescaped blanks end unquoted args. */
452 white = 1;
453 break;
454 }
455 } else if ('"' == cp[0]) {
456 if ('"' == cp[1]) {
457 /* Quoted quotes collapse. */
458 pairs++;
459 cp++;
460 } else {
461 /* Unquoted quotes end quoted args. */
462 quoted = 2;
463 break;
464 }
465 }
466 }
467
468 /* Quoted argument without a closing quote. */
469 if (1 == quoted)
470 mandoc_msg(MANDOCERR_ARG_QUOTE, ln, *pos, NULL);
471
472 /* NUL-terminate this argument and move to the next one. */
473 if (pairs)
474 cp[-pairs] = '\0';
475 if ('\0' != *cp) {
476 *cp++ = '\0';
477 while (' ' == *cp)
478 cp++;
479 }
480 *pos += (int)(cp - start) + (quoted ? 1 : 0);
481 *cpp = cp;
482
483 if ('\0' == *cp && (white || ' ' == cp[-1]))
484 mandoc_msg(MANDOCERR_SPACE_EOL, ln, *pos, NULL);
485
486 return start;
487 }
488
489 static int
490 a2time(time_t *t, const char *fmt, const char *p)
491 {
492 struct tm tm;
493 char *pp;
494
495 memset(&tm, 0, sizeof(struct tm));
496
497 pp = NULL;
498 #if HAVE_STRPTIME
499 pp = strptime(p, fmt, &tm);
500 #endif
501 if (NULL != pp && '\0' == *pp) {
502 *t = mktime(&tm);
503 return 1;
504 }
505
506 return 0;
507 }
508
509 static char *
510 time2a(time_t t)
511 {
512 struct tm *tm;
513 char *buf, *p;
514 size_t ssz;
515 int isz;
516
517 tm = localtime(&t);
518 if (tm == NULL)
519 return NULL;
520
521 /*
522 * Reserve space:
523 * up to 9 characters for the month (September) + blank
524 * up to 2 characters for the day + comma + blank
525 * 4 characters for the year and a terminating '\0'
526 */
527
528 p = buf = mandoc_malloc(10 + 4 + 4 + 1);
529
530 if ((ssz = strftime(p, 10 + 1, "%B ", tm)) == 0)
531 goto fail;
532 p += (int)ssz;
533
534 /*
535 * The output format is just "%d" here, not "%2d" or "%02d".
536 * That's also the reason why we can't just format the
537 * date as a whole with "%B %e, %Y" or "%B %d, %Y".
538 * Besides, the present approach is less prone to buffer
539 * overflows, in case anybody should ever introduce the bug
540 * of looking at LC_TIME.
541 */
542
543 if ((isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)) == -1)
544 goto fail;
545 p += isz;
546
547 if (strftime(p, 4 + 1, "%Y", tm) == 0)
548 goto fail;
549 return buf;
550
551 fail:
552 free(buf);
553 return NULL;
554 }
555
556 char *
557 mandoc_normdate(struct roff_man *man, char *in, int ln, int pos)
558 {
559 char *cp;
560 time_t t;
561
562 /* No date specified: use today's date. */
563
564 if (in == NULL || *in == '\0' || strcmp(in, "$" "Mdocdate$") == 0) {
565 mandoc_msg(MANDOCERR_DATE_MISSING, ln, pos, NULL);
566 return time2a(time(NULL));
567 }
568
569 /* Valid mdoc(7) date format. */
570
571 if (a2time(&t, "$" "Mdocdate: %b %d %Y $", in) ||
572 a2time(&t, "%b %d, %Y", in)) {
573 cp = time2a(t);
574 if (t > time(NULL) + 86400)
575 mandoc_msg(MANDOCERR_DATE_FUTURE, ln, pos, "%s", cp);
576 else if (*in != '$' && strcmp(in, cp) != 0)
577 mandoc_msg(MANDOCERR_DATE_NORM, ln, pos, "%s", cp);
578 return cp;
579 }
580
581 /* In man(7), do not warn about the legacy format. */
582
583 if (a2time(&t, "%Y-%m-%d", in) == 0)
584 mandoc_msg(MANDOCERR_DATE_BAD, ln, pos, "%s", in);
585 else if (t > time(NULL) + 86400)
586 mandoc_msg(MANDOCERR_DATE_FUTURE, ln, pos, "%s", in);
587 else if (man->macroset == MACROSET_MDOC)
588 mandoc_msg(MANDOCERR_DATE_LEGACY, ln, pos, "Dd %s", in);
589
590 /* Use any non-mdoc(7) date verbatim. */
591
592 return mandoc_strdup(in);
593 }
594
595 int
596 mandoc_eos(const char *p, size_t sz)
597 {
598 const char *q;
599 int enclosed, found;
600
601 if (0 == sz)
602 return 0;
603
604 /*
605 * End-of-sentence recognition must include situations where
606 * some symbols, such as `)', allow prior EOS punctuation to
607 * propagate outward.
608 */
609
610 enclosed = found = 0;
611 for (q = p + (int)sz - 1; q >= p; q--) {
612 switch (*q) {
613 case '\"':
614 case '\'':
615 case ']':
616 case ')':
617 if (0 == found)
618 enclosed = 1;
619 break;
620 case '.':
621 case '!':
622 case '?':
623 found = 1;
624 break;
625 default:
626 return found &&
627 (!enclosed || isalnum((unsigned char)*q));
628 }
629 }
630
631 return found && !enclosed;
632 }
633
634 /*
635 * Convert a string to a long that may not be <0.
636 * If the string is invalid, or is less than 0, return -1.
637 */
638 int
639 mandoc_strntoi(const char *p, size_t sz, int base)
640 {
641 char buf[32];
642 char *ep;
643 long v;
644
645 if (sz > 31)
646 return -1;
647
648 memcpy(buf, p, sz);
649 buf[(int)sz] = '\0';
650
651 errno = 0;
652 v = strtol(buf, &ep, base);
653
654 if (buf[0] == '\0' || *ep != '\0')
655 return -1;
656
657 if (v > INT_MAX)
658 v = INT_MAX;
659 if (v < INT_MIN)
660 v = INT_MIN;
661
662 return (int)v;
663 }