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