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