]> git.cameronkatri.com Git - mandoc.git/blob - mandoc.c
fix -Tascii .Fd line breaking
[mandoc.git] / mandoc.c
1 /* $Id: mandoc.c,v 1.66 2012/06/12 20:21:04 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012 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 #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>
26 #include <errno.h>
27 #include <limits.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <time.h>
32
33 #include "mandoc.h"
34 #include "libmandoc.h"
35
36 #define DATESIZE 32
37
38 static int a2time(time_t *, const char *, const char *);
39 static char *time2a(time_t);
40
41
42 enum mandoc_esc
43 mandoc_escape(const char **end, const char **start, int *sz)
44 {
45 const char *local_start;
46 int local_sz;
47 char term;
48 enum mandoc_esc gly;
49
50 /*
51 * When the caller doesn't provide return storage,
52 * use local storage.
53 */
54
55 if (NULL == start)
56 start = &local_start;
57 if (NULL == sz)
58 sz = &local_sz;
59
60 /*
61 * Beyond the backslash, at least one input character
62 * is part of the escape sequence. With one exception
63 * (see below), that character won't be returned.
64 */
65
66 gly = ESCAPE_ERROR;
67 *start = ++*end;
68 *sz = 0;
69 term = '\0';
70
71 switch ((*start)[-1]) {
72 /*
73 * First the glyphs. There are several different forms of
74 * these, but each eventually returns a substring of the glyph
75 * name.
76 */
77 case ('('):
78 gly = ESCAPE_SPECIAL;
79 *sz = 2;
80 break;
81 case ('['):
82 gly = ESCAPE_SPECIAL;
83 /*
84 * Unicode escapes are defined in groff as \[uXXXX] to
85 * \[u10FFFF], where the contained value must be a valid
86 * Unicode codepoint. Here, however, only check whether
87 * it's not a zero-width escape.
88 */
89 if ('u' == (*start)[0] && ']' != (*start)[1])
90 gly = ESCAPE_UNICODE;
91 term = ']';
92 break;
93 case ('C'):
94 if ('\'' != **start)
95 return(ESCAPE_ERROR);
96 gly = ESCAPE_SPECIAL;
97 *start = ++*end;
98 term = '\'';
99 break;
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 /* FALLTHROUGH */
116 case ('g'):
117 /* FALLTHROUGH */
118 case ('k'):
119 /* FALLTHROUGH */
120 case ('M'):
121 /* FALLTHROUGH */
122 case ('m'):
123 /* FALLTHROUGH */
124 case ('n'):
125 /* FALLTHROUGH */
126 case ('V'):
127 /* FALLTHROUGH */
128 case ('Y'):
129 gly = ESCAPE_IGNORE;
130 /* FALLTHROUGH */
131 case ('f'):
132 if (ESCAPE_ERROR == gly)
133 gly = ESCAPE_FONT;
134 switch (**start) {
135 case ('('):
136 *start = ++*end;
137 *sz = 2;
138 break;
139 case ('['):
140 *start = ++*end;
141 term = ']';
142 break;
143 default:
144 *sz = 1;
145 break;
146 }
147 break;
148
149 /*
150 * These escapes are of the form \X'Y', where 'X' is the trigger
151 * and 'Y' is any string. These have opaque sub-strings.
152 */
153 case ('A'):
154 /* FALLTHROUGH */
155 case ('b'):
156 /* FALLTHROUGH */
157 case ('D'):
158 /* FALLTHROUGH */
159 case ('o'):
160 /* FALLTHROUGH */
161 case ('R'):
162 /* FALLTHROUGH */
163 case ('X'):
164 /* FALLTHROUGH */
165 case ('Z'):
166 if ('\'' != **start)
167 return(ESCAPE_ERROR);
168 gly = ESCAPE_IGNORE;
169 *start = ++*end;
170 term = '\'';
171 break;
172
173 /*
174 * These escapes are of the form \X'N', where 'X' is the trigger
175 * and 'N' resolves to a numerical expression.
176 */
177 case ('B'):
178 /* FALLTHROUGH */
179 case ('h'):
180 /* FALLTHROUGH */
181 case ('H'):
182 /* FALLTHROUGH */
183 case ('L'):
184 /* FALLTHROUGH */
185 case ('l'):
186 gly = ESCAPE_NUMBERED;
187 /* FALLTHROUGH */
188 case ('S'):
189 /* FALLTHROUGH */
190 case ('v'):
191 /* FALLTHROUGH */
192 case ('w'):
193 /* FALLTHROUGH */
194 case ('x'):
195 if ('\'' != **start)
196 return(ESCAPE_ERROR);
197 if (ESCAPE_ERROR == gly)
198 gly = ESCAPE_IGNORE;
199 *start = ++*end;
200 term = '\'';
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 (*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 default:
247 *sz = 1;
248 break;
249 }
250
251 break;
252
253 /*
254 * Anything else is assumed to be a glyph.
255 * In this case, pass back the character after the backslash.
256 */
257 default:
258 gly = ESCAPE_SPECIAL;
259 *start = --*end;
260 *sz = 1;
261 break;
262 }
263
264 assert(ESCAPE_ERROR != gly);
265
266 /*
267 * Read up to the terminating character,
268 * paying attention to nested escapes.
269 */
270
271 if ('\0' != term) {
272 while (**end != term) {
273 switch (**end) {
274 case ('\0'):
275 return(ESCAPE_ERROR);
276 case ('\\'):
277 (*end)++;
278 if (ESCAPE_ERROR ==
279 mandoc_escape(end, NULL, NULL))
280 return(ESCAPE_ERROR);
281 break;
282 default:
283 (*end)++;
284 break;
285 }
286 }
287 *sz = (*end)++ - *start;
288 } else {
289 assert(*sz > 0);
290 if ((size_t)*sz > strlen(*start))
291 return(ESCAPE_ERROR);
292 *end += *sz;
293 }
294
295 /* Run post-processors. */
296
297 switch (gly) {
298 case (ESCAPE_FONT):
299 /*
300 * Pretend that the constant-width font modes are the
301 * same as the regular font modes.
302 */
303 if (2 == *sz && 'C' == **start) {
304 (*start)++;
305 (*sz)--;
306 } else if (1 != *sz)
307 break;
308
309 switch (**start) {
310 case ('3'):
311 /* FALLTHROUGH */
312 case ('B'):
313 gly = ESCAPE_FONTBOLD;
314 break;
315 case ('2'):
316 /* FALLTHROUGH */
317 case ('I'):
318 gly = ESCAPE_FONTITALIC;
319 break;
320 case ('P'):
321 gly = ESCAPE_FONTPREV;
322 break;
323 case ('1'):
324 /* FALLTHROUGH */
325 case ('R'):
326 gly = ESCAPE_FONTROMAN;
327 break;
328 }
329 break;
330 case (ESCAPE_SPECIAL):
331 if (1 == *sz && 'c' == **start)
332 gly = ESCAPE_NOSPACE;
333 break;
334 default:
335 break;
336 }
337
338 return(gly);
339 }
340
341 void *
342 mandoc_calloc(size_t num, size_t size)
343 {
344 void *ptr;
345
346 ptr = calloc(num, size);
347 if (NULL == ptr) {
348 perror(NULL);
349 exit((int)MANDOCLEVEL_SYSERR);
350 }
351
352 return(ptr);
353 }
354
355
356 void *
357 mandoc_malloc(size_t size)
358 {
359 void *ptr;
360
361 ptr = malloc(size);
362 if (NULL == ptr) {
363 perror(NULL);
364 exit((int)MANDOCLEVEL_SYSERR);
365 }
366
367 return(ptr);
368 }
369
370
371 void *
372 mandoc_realloc(void *ptr, size_t size)
373 {
374
375 ptr = realloc(ptr, size);
376 if (NULL == ptr) {
377 perror(NULL);
378 exit((int)MANDOCLEVEL_SYSERR);
379 }
380
381 return(ptr);
382 }
383
384 char *
385 mandoc_strndup(const char *ptr, size_t sz)
386 {
387 char *p;
388
389 p = mandoc_malloc(sz + 1);
390 memcpy(p, ptr, sz);
391 p[(int)sz] = '\0';
392 return(p);
393 }
394
395 char *
396 mandoc_strdup(const char *ptr)
397 {
398 char *p;
399
400 p = strdup(ptr);
401 if (NULL == p) {
402 perror(NULL);
403 exit((int)MANDOCLEVEL_SYSERR);
404 }
405
406 return(p);
407 }
408
409 /*
410 * Parse a quoted or unquoted roff-style request or macro argument.
411 * Return a pointer to the parsed argument, which is either the original
412 * pointer or advanced by one byte in case the argument is quoted.
413 * Null-terminate the argument in place.
414 * Collapse pairs of quotes inside quoted arguments.
415 * Advance the argument pointer to the next argument,
416 * or to the null byte terminating the argument line.
417 */
418 char *
419 mandoc_getarg(struct mparse *parse, char **cpp, int ln, int *pos)
420 {
421 char *start, *cp;
422 int quoted, pairs, white;
423
424 /* Quoting can only start with a new word. */
425 start = *cpp;
426 quoted = 0;
427 if ('"' == *start) {
428 quoted = 1;
429 start++;
430 }
431
432 pairs = 0;
433 white = 0;
434 for (cp = start; '\0' != *cp; cp++) {
435 /* Move left after quoted quotes and escaped backslashes. */
436 if (pairs)
437 cp[-pairs] = cp[0];
438 if ('\\' == cp[0]) {
439 if ('\\' == cp[1]) {
440 /* Poor man's copy mode. */
441 pairs++;
442 cp++;
443 } else if (0 == quoted && ' ' == cp[1])
444 /* Skip escaped blanks. */
445 cp++;
446 } else if (0 == quoted) {
447 if (' ' == cp[0]) {
448 /* Unescaped blanks end unquoted args. */
449 white = 1;
450 break;
451 }
452 } else if ('"' == cp[0]) {
453 if ('"' == cp[1]) {
454 /* Quoted quotes collapse. */
455 pairs++;
456 cp++;
457 } else {
458 /* Unquoted quotes end quoted args. */
459 quoted = 2;
460 break;
461 }
462 }
463 }
464
465 /* Quoted argument without a closing quote. */
466 if (1 == quoted)
467 mandoc_msg(MANDOCERR_BADQUOTE, parse, ln, *pos, NULL);
468
469 /* Null-terminate this argument and move to the next one. */
470 if (pairs)
471 cp[-pairs] = '\0';
472 if ('\0' != *cp) {
473 *cp++ = '\0';
474 while (' ' == *cp)
475 cp++;
476 }
477 *pos += (int)(cp - start) + (quoted ? 1 : 0);
478 *cpp = cp;
479
480 if ('\0' == *cp && (white || ' ' == cp[-1]))
481 mandoc_msg(MANDOCERR_EOLNSPACE, parse, ln, *pos, NULL);
482
483 return(start);
484 }
485
486 static int
487 a2time(time_t *t, const char *fmt, const char *p)
488 {
489 struct tm tm;
490 char *pp;
491
492 memset(&tm, 0, sizeof(struct tm));
493
494 pp = NULL;
495 #ifdef HAVE_STRPTIME
496 pp = strptime(p, fmt, &tm);
497 #endif
498 if (NULL != pp && '\0' == *pp) {
499 *t = mktime(&tm);
500 return(1);
501 }
502
503 return(0);
504 }
505
506 static char *
507 time2a(time_t t)
508 {
509 struct tm *tm;
510 char *buf, *p;
511 size_t ssz;
512 int isz;
513
514 tm = localtime(&t);
515
516 /*
517 * Reserve space:
518 * up to 9 characters for the month (September) + blank
519 * up to 2 characters for the day + comma + blank
520 * 4 characters for the year and a terminating '\0'
521 */
522 p = buf = mandoc_malloc(10 + 4 + 4 + 1);
523
524 if (0 == (ssz = strftime(p, 10 + 1, "%B ", tm)))
525 goto fail;
526 p += (int)ssz;
527
528 if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)))
529 goto fail;
530 p += isz;
531
532 if (0 == strftime(p, 4 + 1, "%Y", tm))
533 goto fail;
534 return(buf);
535
536 fail:
537 free(buf);
538 return(NULL);
539 }
540
541 char *
542 mandoc_normdate(struct mparse *parse, char *in, int ln, int pos)
543 {
544 char *out;
545 time_t t;
546
547 if (NULL == in || '\0' == *in ||
548 0 == strcmp(in, "$" "Mdocdate$")) {
549 mandoc_msg(MANDOCERR_NODATE, parse, ln, pos, NULL);
550 time(&t);
551 }
552 else if (a2time(&t, "%Y-%m-%d", in))
553 t = 0;
554 else if (!a2time(&t, "$" "Mdocdate: %b %d %Y $", in) &&
555 !a2time(&t, "%b %d, %Y", in)) {
556 mandoc_msg(MANDOCERR_BADDATE, parse, ln, pos, NULL);
557 t = 0;
558 }
559 out = t ? time2a(t) : NULL;
560 return(out ? out : mandoc_strdup(in));
561 }
562
563 int
564 mandoc_eos(const char *p, size_t sz, int enclosed)
565 {
566 const char *q;
567 int found;
568
569 if (0 == sz)
570 return(0);
571
572 /*
573 * End-of-sentence recognition must include situations where
574 * some symbols, such as `)', allow prior EOS punctuation to
575 * propagate outward.
576 */
577
578 found = 0;
579 for (q = p + (int)sz - 1; q >= p; q--) {
580 switch (*q) {
581 case ('\"'):
582 /* FALLTHROUGH */
583 case ('\''):
584 /* FALLTHROUGH */
585 case (']'):
586 /* FALLTHROUGH */
587 case (')'):
588 if (0 == found)
589 enclosed = 1;
590 break;
591 case ('.'):
592 /* FALLTHROUGH */
593 case ('!'):
594 /* FALLTHROUGH */
595 case ('?'):
596 found = 1;
597 break;
598 default:
599 return(found && (!enclosed || isalnum((unsigned char)*q)));
600 }
601 }
602
603 return(found && !enclosed);
604 }
605
606 /*
607 * Convert a string to a long that may not be <0.
608 * If the string is invalid, or is less than 0, return -1.
609 */
610 int
611 mandoc_strntoi(const char *p, size_t sz, int base)
612 {
613 char buf[32];
614 char *ep;
615 long v;
616
617 if (sz > 31)
618 return(-1);
619
620 memcpy(buf, p, sz);
621 buf[(int)sz] = '\0';
622
623 errno = 0;
624 v = strtol(buf, &ep, base);
625
626 if (buf[0] == '\0' || *ep != '\0')
627 return(-1);
628
629 if (v > INT_MAX)
630 v = INT_MAX;
631 if (v < INT_MIN)
632 v = INT_MIN;
633
634 return((int)v);
635 }