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