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