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