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