1 /* $Id: mdoc_argv.c,v 1.82 2012/03/23 05:50:24 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "mdoc.h"
29 #include "mandoc.h"
30 #include "libmdoc.h"
31 #include "libmandoc.h"
32
33 #define MULTI_STEP 5 /* pre-allocate argument values */
34 #define DELIMSZ 6 /* max possible size of a delimiter */
35
36 enum argsflag {
37 ARGSFL_NONE = 0,
38 ARGSFL_DELIM, /* handle delimiters of [[::delim::][ ]+]+ */
39 ARGSFL_TABSEP /* handle tab/`Ta' separated phrases */
40 };
41
42 enum argvflag {
43 ARGV_NONE, /* no args to flag (e.g., -split) */
44 ARGV_SINGLE, /* one arg to flag (e.g., -file xxx) */
45 ARGV_MULTI, /* multiple args (e.g., -column xxx yyy) */
46 ARGV_OPT_SINGLE /* optional arg (e.g., -offset [xxx]) */
47 };
48
49 struct mdocarg {
50 enum argsflag flags;
51 const enum mdocargt *argvs;
52 };
53
54 static void argn_free(struct mdoc_arg *, int);
55 static enum margserr args(struct mdoc *, int, int *,
56 char *, enum argsflag, char **);
57 static int args_checkpunct(const char *, int);
58 static int argv_multi(struct mdoc *, int,
59 struct mdoc_argv *, int *, char *);
60 static int argv_opt_single(struct mdoc *, int,
61 struct mdoc_argv *, int *, char *);
62 static int argv_single(struct mdoc *, int,
63 struct mdoc_argv *, int *, char *);
64
65 static const enum argvflag argvflags[MDOC_ARG_MAX] = {
66 ARGV_NONE, /* MDOC_Split */
67 ARGV_NONE, /* MDOC_Nosplit */
68 ARGV_NONE, /* MDOC_Ragged */
69 ARGV_NONE, /* MDOC_Unfilled */
70 ARGV_NONE, /* MDOC_Literal */
71 ARGV_SINGLE, /* MDOC_File */
72 ARGV_OPT_SINGLE, /* MDOC_Offset */
73 ARGV_NONE, /* MDOC_Bullet */
74 ARGV_NONE, /* MDOC_Dash */
75 ARGV_NONE, /* MDOC_Hyphen */
76 ARGV_NONE, /* MDOC_Item */
77 ARGV_NONE, /* MDOC_Enum */
78 ARGV_NONE, /* MDOC_Tag */
79 ARGV_NONE, /* MDOC_Diag */
80 ARGV_NONE, /* MDOC_Hang */
81 ARGV_NONE, /* MDOC_Ohang */
82 ARGV_NONE, /* MDOC_Inset */
83 ARGV_MULTI, /* MDOC_Column */
84 ARGV_OPT_SINGLE, /* MDOC_Width */
85 ARGV_NONE, /* MDOC_Compact */
86 ARGV_NONE, /* MDOC_Std */
87 ARGV_NONE, /* MDOC_Filled */
88 ARGV_NONE, /* MDOC_Words */
89 ARGV_NONE, /* MDOC_Emphasis */
90 ARGV_NONE, /* MDOC_Symbolic */
91 ARGV_NONE /* MDOC_Symbolic */
92 };
93
94 static const enum mdocargt args_Ex[] = {
95 MDOC_Std,
96 MDOC_ARG_MAX
97 };
98
99 static const enum mdocargt args_An[] = {
100 MDOC_Split,
101 MDOC_Nosplit,
102 MDOC_ARG_MAX
103 };
104
129 static const enum mdocargt args_Bl[] = {
130 MDOC_Bullet,
131 MDOC_Dash,
132 MDOC_Hyphen,
133 MDOC_Item,
134 MDOC_Enum,
135 MDOC_Tag,
136 MDOC_Diag,
137 MDOC_Hang,
138 MDOC_Ohang,
139 MDOC_Inset,
140 MDOC_Column,
141 MDOC_Width,
142 MDOC_Offset,
143 MDOC_Compact,
144 MDOC_Nested,
145 MDOC_ARG_MAX
146 };
147
148 static const struct mdocarg mdocargs[MDOC_MAX] = {
149 { ARGSFL_NONE, NULL }, /* Ap */
150 { ARGSFL_NONE, NULL }, /* Dd */
151 { ARGSFL_NONE, NULL }, /* Dt */
152 { ARGSFL_NONE, NULL }, /* Os */
153 { ARGSFL_NONE, NULL }, /* Sh */
154 { ARGSFL_NONE, NULL }, /* Ss */
155 { ARGSFL_NONE, NULL }, /* Pp */
156 { ARGSFL_DELIM, NULL }, /* D1 */
157 { ARGSFL_DELIM, NULL }, /* Dl */
158 { ARGSFL_NONE, args_Bd }, /* Bd */
159 { ARGSFL_NONE, NULL }, /* Ed */
160 { ARGSFL_NONE, args_Bl }, /* Bl */
161 { ARGSFL_NONE, NULL }, /* El */
162 { ARGSFL_NONE, NULL }, /* It */
163 { ARGSFL_DELIM, NULL }, /* Ad */
164 { ARGSFL_DELIM, args_An }, /* An */
165 { ARGSFL_DELIM, NULL }, /* Ar */
166 { ARGSFL_NONE, NULL }, /* Cd */
167 { ARGSFL_DELIM, NULL }, /* Cm */
168 { ARGSFL_DELIM, NULL }, /* Dv */
169 { ARGSFL_DELIM, NULL }, /* Er */
170 { ARGSFL_DELIM, NULL }, /* Ev */
171 { ARGSFL_NONE, args_Ex }, /* Ex */
172 { ARGSFL_DELIM, NULL }, /* Fa */
173 { ARGSFL_NONE, NULL }, /* Fd */
174 { ARGSFL_DELIM, NULL }, /* Fl */
175 { ARGSFL_DELIM, NULL }, /* Fn */
176 { ARGSFL_DELIM, NULL }, /* Ft */
177 { ARGSFL_DELIM, NULL }, /* Ic */
178 { ARGSFL_NONE, NULL }, /* In */
179 { ARGSFL_DELIM, NULL }, /* Li */
180 { ARGSFL_NONE, NULL }, /* Nd */
181 { ARGSFL_DELIM, NULL }, /* Nm */
182 { ARGSFL_DELIM, NULL }, /* Op */
183 { ARGSFL_NONE, NULL }, /* Ot */
184 { ARGSFL_DELIM, NULL }, /* Pa */
185 { ARGSFL_NONE, args_Ex }, /* Rv */
186 { ARGSFL_DELIM, NULL }, /* St */
187 { ARGSFL_DELIM, NULL }, /* Va */
188 { ARGSFL_DELIM, NULL }, /* Vt */
189 { ARGSFL_DELIM, NULL }, /* Xr */
190 { ARGSFL_NONE, NULL }, /* %A */
191 { ARGSFL_NONE, NULL }, /* %B */
192 { ARGSFL_NONE, NULL }, /* %D */
193 { ARGSFL_NONE, NULL }, /* %I */
194 { ARGSFL_NONE, NULL }, /* %J */
195 { ARGSFL_NONE, NULL }, /* %N */
196 { ARGSFL_NONE, NULL }, /* %O */
197 { ARGSFL_NONE, NULL }, /* %P */
198 { ARGSFL_NONE, NULL }, /* %R */
226 { ARGSFL_DELIM, NULL }, /* Pf */
227 { ARGSFL_NONE, NULL }, /* Po */
228 { ARGSFL_DELIM, NULL }, /* Pq */
229 { ARGSFL_DELIM, NULL }, /* Qc */
230 { ARGSFL_DELIM, NULL }, /* Ql */
231 { ARGSFL_NONE, NULL }, /* Qo */
232 { ARGSFL_DELIM, NULL }, /* Qq */
233 { ARGSFL_NONE, NULL }, /* Re */
234 { ARGSFL_NONE, NULL }, /* Rs */
235 { ARGSFL_DELIM, NULL }, /* Sc */
236 { ARGSFL_NONE, NULL }, /* So */
237 { ARGSFL_DELIM, NULL }, /* Sq */
238 { ARGSFL_NONE, NULL }, /* Sm */
239 { ARGSFL_DELIM, NULL }, /* Sx */
240 { ARGSFL_DELIM, NULL }, /* Sy */
241 { ARGSFL_DELIM, NULL }, /* Tn */
242 { ARGSFL_DELIM, NULL }, /* Ux */
243 { ARGSFL_DELIM, NULL }, /* Xc */
244 { ARGSFL_NONE, NULL }, /* Xo */
245 { ARGSFL_NONE, NULL }, /* Fo */
246 { ARGSFL_NONE, NULL }, /* Fc */
247 { ARGSFL_NONE, NULL }, /* Oo */
248 { ARGSFL_DELIM, NULL }, /* Oc */
249 { ARGSFL_NONE, args_Bk }, /* Bk */
250 { ARGSFL_NONE, NULL }, /* Ek */
251 { ARGSFL_NONE, NULL }, /* Bt */
252 { ARGSFL_NONE, NULL }, /* Hf */
253 { ARGSFL_NONE, NULL }, /* Fr */
254 { ARGSFL_NONE, NULL }, /* Ud */
255 { ARGSFL_NONE, NULL }, /* Lb */
256 { ARGSFL_NONE, NULL }, /* Lp */
257 { ARGSFL_DELIM, NULL }, /* Lk */
258 { ARGSFL_DELIM, NULL }, /* Mt */
259 { ARGSFL_DELIM, NULL }, /* Brq */
260 { ARGSFL_NONE, NULL }, /* Bro */
261 { ARGSFL_DELIM, NULL }, /* Brc */
262 { ARGSFL_NONE, NULL }, /* %C */
263 { ARGSFL_NONE, NULL }, /* Es */
264 { ARGSFL_NONE, NULL }, /* En */
265 { ARGSFL_NONE, NULL }, /* Dx */
266 { ARGSFL_NONE, NULL }, /* %Q */
267 { ARGSFL_NONE, NULL }, /* br */
268 { ARGSFL_NONE, NULL }, /* sp */
269 { ARGSFL_NONE, NULL }, /* %U */
270 { ARGSFL_NONE, NULL }, /* Ta */
271 };
272
273
274 /*
275 * Parse an argument from line text. This comes in the form of -key
276 * [value0...], which may either have a single mandatory value, at least
277 * one mandatory value, an optional single value, or no value.
278 */
279 enum margverr
280 mdoc_argv(struct mdoc *m, int line, enum mdoct tok,
281 struct mdoc_arg **v, int *pos, char *buf)
282 {
283 char *p, sv;
284 struct mdoc_argv tmp;
285 struct mdoc_arg *arg;
286 const enum mdocargt *ap;
287
288 if ('\0' == buf[*pos])
289 return(ARGV_EOLN);
290 else if (NULL == (ap = mdocargs[tok].argvs))
291 return(ARGV_WORD);
292 else if ('-' != buf[*pos])
293 return(ARGV_WORD);
294
295 /* Seek to the first unescaped space. */
296
297 p = &buf[++(*pos)];
298
299 assert(*pos > 0);
300
327 if (0 == strcmp(p, mdoc_argnames[tmp.arg]))
328 break;
329
330 if (MDOC_ARG_MAX == tmp.arg) {
331 /*
332 * The flag was not found.
333 * Restore saved zeroed byte and return as a word.
334 */
335 if (sv)
336 buf[*pos - 1] = sv;
337 return(ARGV_WORD);
338 }
339
340 /* Read to the next word (the argument). */
341
342 while (buf[*pos] && ' ' == buf[*pos])
343 (*pos)++;
344
345 switch (argvflags[tmp.arg]) {
346 case (ARGV_SINGLE):
347 if ( ! argv_single(m, line, &tmp, pos, buf))
348 return(ARGV_ERROR);
349 break;
350 case (ARGV_MULTI):
351 if ( ! argv_multi(m, line, &tmp, pos, buf))
352 return(ARGV_ERROR);
353 break;
354 case (ARGV_OPT_SINGLE):
355 if ( ! argv_opt_single(m, line, &tmp, pos, buf))
356 return(ARGV_ERROR);
357 break;
358 case (ARGV_NONE):
359 break;
360 }
361
362 if (NULL == (arg = *v))
363 arg = *v = mandoc_calloc(1, sizeof(struct mdoc_arg));
364
365 arg->argc++;
366 arg->argv = mandoc_realloc
367 (arg->argv, arg->argc * sizeof(struct mdoc_argv));
368
369 memcpy(&arg->argv[(int)arg->argc - 1],
370 &tmp, sizeof(struct mdoc_argv));
371
372 return(ARGV_ARG);
373 }
374
375 void
376 mdoc_argv_free(struct mdoc_arg *p)
377 {
396
397 static void
398 argn_free(struct mdoc_arg *p, int iarg)
399 {
400 struct mdoc_argv *arg;
401 int j;
402
403 arg = &p->argv[iarg];
404
405 if (arg->sz && arg->value) {
406 for (j = (int)arg->sz - 1; j >= 0; j--)
407 free(arg->value[j]);
408 free(arg->value);
409 }
410
411 for (--p->argc; iarg < (int)p->argc; iarg++)
412 p->argv[iarg] = p->argv[iarg+1];
413 }
414
415 enum margserr
416 mdoc_zargs(struct mdoc *m, int line, int *pos, char *buf, char **v)
417 {
418
419 return(args(m, line, pos, buf, ARGSFL_NONE, v));
420 }
421
422 enum margserr
423 mdoc_args(struct mdoc *m, int line, int *pos,
424 char *buf, enum mdoct tok, char **v)
425 {
426 enum argsflag fl;
427 struct mdoc_node *n;
428
429 fl = mdocargs[tok].flags;
430
431 if (MDOC_It != tok)
432 return(args(m, line, pos, buf, fl, v));
433
434 /*
435 * We know that we're in an `It', so it's reasonable to expect
436 * us to be sitting in a `Bl'. Someday this may not be the case
437 * (if we allow random `It's sitting out there), so provide a
438 * safe fall-back into the default behaviour.
439 */
440
441 for (n = m->last; n; n = n->parent)
442 if (MDOC_Bl == n->tok)
443 if (LIST_column == n->norm->Bl.type) {
444 fl = ARGSFL_TABSEP;
445 break;
446 }
447
448 return(args(m, line, pos, buf, fl, v));
449 }
450
451 static enum margserr
452 args(struct mdoc *m, int line, int *pos,
453 char *buf, enum argsflag fl, char **v)
454 {
455 char *p, *pp;
456 enum margserr rc;
457
458 if ('\0' == buf[*pos]) {
459 if (MDOC_PPHRASE & m->flags)
460 return(ARGS_EOLN);
461 /*
462 * If we're not in a partial phrase and the flag for
463 * being a phrase literal is still set, the punctuation
464 * is unterminated.
465 */
466 if (MDOC_PHRASELIT & m->flags)
467 mdoc_pmsg(m, line, *pos, MANDOCERR_BADQUOTE);
468
469 m->flags &= ~MDOC_PHRASELIT;
470 return(ARGS_EOLN);
471 }
472
473 *v = &buf[*pos];
474
475 if (ARGSFL_DELIM == fl)
476 if (args_checkpunct(buf, *pos))
477 return(ARGS_PUNCT);
478
479 /*
480 * First handle TABSEP items, restricted to `Bl -column'. This
481 * ignores conventional token parsing and instead uses tabs or
482 * `Ta' macros to separate phrases. Phrases are parsed again
483 * for arguments at a later phase.
484 */
485
486 if (ARGSFL_TABSEP == fl) {
487 /* Scan ahead to tab (can't be escaped). */
488 p = strchr(*v, '\t');
489 pp = NULL;
490
491 /* Scan ahead to unescaped `Ta'. */
492 if ( ! (MDOC_PHRASELIT & m->flags))
493 for (pp = *v; ; pp++) {
494 if (NULL == (pp = strstr(pp, "Ta")))
495 break;
496 if (pp > *v && ' ' != *(pp - 1))
497 continue;
498 if (' ' == *(pp + 2) || '\0' == *(pp + 2))
499 break;
500 }
501
502 /* By default, assume a phrase. */
503 rc = ARGS_PHRASE;
504
505 /*
506 * Adjust new-buffer position to be beyond delimiter
507 * mark (e.g., Ta -> end + 2).
508 */
509 if (p && pp) {
510 *pos += pp < p ? 2 : 1;
511 rc = pp < p ? ARGS_PHRASE : ARGS_PPHRASE;
512 p = pp < p ? pp : p;
513 } else if (p && ! pp) {
514 rc = ARGS_PPHRASE;
515 *pos += 1;
516 } else if (pp && ! p) {
517 p = pp;
518 *pos += 2;
519 } else {
520 rc = ARGS_PEND;
521 p = strchr(*v, 0);
522 }
523
524 /* Whitespace check for eoln case... */
525 if ('\0' == *p && ' ' == *(p - 1))
526 mdoc_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE);
527
528 *pos += (int)(p - *v);
529
530 /* Strip delimiter's preceding whitespace. */
531 pp = p - 1;
532 while (pp > *v && ' ' == *pp) {
533 if (pp > *v && '\\' == *(pp - 1))
534 break;
535 pp--;
536 }
537 *(pp + 1) = 0;
538
539 /* Strip delimiter's proceeding whitespace. */
540 for (pp = &buf[*pos]; ' ' == *pp; pp++, (*pos)++)
541 /* Skip ahead. */ ;
542
543 return(rc);
544 }
545
546 /*
547 * Process a quoted literal. A quote begins with a double-quote
548 * and ends with a double-quote NOT preceded by a double-quote.
549 * Whitespace is NOT involved in literal termination.
550 */
551
552 if (MDOC_PHRASELIT & m->flags || '\"' == buf[*pos]) {
553 if ( ! (MDOC_PHRASELIT & m->flags))
554 *v = &buf[++(*pos)];
555
556 if (MDOC_PPHRASE & m->flags)
557 m->flags |= MDOC_PHRASELIT;
558
559 for ( ; buf[*pos]; (*pos)++) {
560 if ('\"' != buf[*pos])
561 continue;
562 if ('\"' != buf[*pos + 1])
563 break;
564 (*pos)++;
565 }
566
567 if ('\0' == buf[*pos]) {
568 if (MDOC_PPHRASE & m->flags)
569 return(ARGS_QWORD);
570 mdoc_pmsg(m, line, *pos, MANDOCERR_BADQUOTE);
571 return(ARGS_QWORD);
572 }
573
574 m->flags &= ~MDOC_PHRASELIT;
575 buf[(*pos)++] = '\0';
576
577 if ('\0' == buf[*pos])
578 return(ARGS_QWORD);
579
580 while (' ' == buf[*pos])
581 (*pos)++;
582
583 if ('\0' == buf[*pos])
584 mdoc_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE);
585
586 return(ARGS_QWORD);
587 }
588
589 p = &buf[*pos];
590 *v = mandoc_getarg(m->parse, &p, line, pos);
591
592 return(ARGS_WORD);
593 }
594
595 /*
596 * Check if the string consists only of space-separated closing
597 * delimiters. This is a bit of a dance: the first must be a close
598 * delimiter, but it may be followed by middle delimiters. Arbitrary
599 * whitespace may separate these tokens.
600 */
601 static int
602 args_checkpunct(const char *buf, int i)
603 {
604 int j;
605 char dbuf[DELIMSZ];
606 enum mdelim d;
607
608 /* First token must be a close-delimiter. */
609
610 for (j = 0; buf[i] && ' ' != buf[i] && j < DELIMSZ; j++, i++)
626 j = 0;
627 while (buf[i] && ' ' != buf[i] && j < DELIMSZ)
628 dbuf[j++] = buf[i++];
629
630 if (DELIMSZ == j)
631 return(0);
632
633 dbuf[j] = '\0';
634 d = mdoc_isdelim(dbuf);
635 if (DELIM_NONE == d || DELIM_OPEN == d)
636 return(0);
637
638 while (' ' == buf[i])
639 i++;
640 }
641
642 return('\0' == buf[i]);
643 }
644
645 static int
646 argv_multi(struct mdoc *m, int line,
647 struct mdoc_argv *v, int *pos, char *buf)
648 {
649 enum margserr ac;
650 char *p;
651
652 for (v->sz = 0; ; v->sz++) {
653 if ('-' == buf[*pos])
654 break;
655 ac = args(m, line, pos, buf, ARGSFL_NONE, &p);
656 if (ARGS_ERROR == ac)
657 return(0);
658 else if (ARGS_EOLN == ac)
659 break;
660
661 if (0 == v->sz % MULTI_STEP)
662 v->value = mandoc_realloc(v->value,
663 (v->sz + MULTI_STEP) * sizeof(char *));
664
665 v->value[(int)v->sz] = mandoc_strdup(p);
666 }
667
668 return(1);
669 }
670
671 static int
672 argv_opt_single(struct mdoc *m, int line,
673 struct mdoc_argv *v, int *pos, char *buf)
674 {
675 enum margserr ac;
676 char *p;
677
678 if ('-' == buf[*pos])
679 return(1);
680
681 ac = args(m, line, pos, buf, ARGSFL_NONE, &p);
682 if (ARGS_ERROR == ac)
683 return(0);
684 if (ARGS_EOLN == ac)
685 return(1);
686
687 v->sz = 1;
688 v->value = mandoc_malloc(sizeof(char *));
689 v->value[0] = mandoc_strdup(p);
690
691 return(1);
692 }
693
694 static int
695 argv_single(struct mdoc *m, int line,
696 struct mdoc_argv *v, int *pos, char *buf)
697 {
698 int ppos;
699 enum margserr ac;
700 char *p;
701
702 ppos = *pos;
703
704 ac = args(m, line, pos, buf, ARGSFL_NONE, &p);
705 if (ARGS_EOLN == ac) {
706 mdoc_pmsg(m, line, ppos, MANDOCERR_SYNTARGVCOUNT);
707 return(0);
708 } else if (ARGS_ERROR == ac)
709 return(0);
710
711 v->sz = 1;
712 v->value = mandoc_malloc(sizeof(char *));
713 v->value[0] = mandoc_strdup(p);
714
715 return(1);
716 }
|
1 /* $Id: mdoc_argv.c,v 1.89 2013/12/25 00:50:05 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "mdoc.h"
30 #include "mandoc.h"
31 #include "libmdoc.h"
32 #include "libmandoc.h"
33
34 #define MULTI_STEP 5 /* pre-allocate argument values */
35 #define DELIMSZ 6 /* max possible size of a delimiter */
36
37 enum argsflag {
38 ARGSFL_NONE = 0,
39 ARGSFL_DELIM, /* handle delimiters of [[::delim::][ ]+]+ */
40 ARGSFL_TABSEP /* handle tab/`Ta' separated phrases */
41 };
42
43 enum argvflag {
44 ARGV_NONE, /* no args to flag (e.g., -split) */
45 ARGV_SINGLE, /* one arg to flag (e.g., -file xxx) */
46 ARGV_MULTI /* multiple args (e.g., -column xxx yyy) */
47 };
48
49 struct mdocarg {
50 enum argsflag flags;
51 const enum mdocargt *argvs;
52 };
53
54 static void argn_free(struct mdoc_arg *, int);
55 static enum margserr args(struct mdoc *, int, int *,
56 char *, enum argsflag, char **);
57 static int args_checkpunct(const char *, int);
58 static int argv_multi(struct mdoc *, int,
59 struct mdoc_argv *, int *, char *);
60 static int argv_single(struct mdoc *, int,
61 struct mdoc_argv *, int *, char *);
62
63 static const enum argvflag argvflags[MDOC_ARG_MAX] = {
64 ARGV_NONE, /* MDOC_Split */
65 ARGV_NONE, /* MDOC_Nosplit */
66 ARGV_NONE, /* MDOC_Ragged */
67 ARGV_NONE, /* MDOC_Unfilled */
68 ARGV_NONE, /* MDOC_Literal */
69 ARGV_SINGLE, /* MDOC_File */
70 ARGV_SINGLE, /* MDOC_Offset */
71 ARGV_NONE, /* MDOC_Bullet */
72 ARGV_NONE, /* MDOC_Dash */
73 ARGV_NONE, /* MDOC_Hyphen */
74 ARGV_NONE, /* MDOC_Item */
75 ARGV_NONE, /* MDOC_Enum */
76 ARGV_NONE, /* MDOC_Tag */
77 ARGV_NONE, /* MDOC_Diag */
78 ARGV_NONE, /* MDOC_Hang */
79 ARGV_NONE, /* MDOC_Ohang */
80 ARGV_NONE, /* MDOC_Inset */
81 ARGV_MULTI, /* MDOC_Column */
82 ARGV_SINGLE, /* MDOC_Width */
83 ARGV_NONE, /* MDOC_Compact */
84 ARGV_NONE, /* MDOC_Std */
85 ARGV_NONE, /* MDOC_Filled */
86 ARGV_NONE, /* MDOC_Words */
87 ARGV_NONE, /* MDOC_Emphasis */
88 ARGV_NONE, /* MDOC_Symbolic */
89 ARGV_NONE /* MDOC_Symbolic */
90 };
91
92 static const enum mdocargt args_Ex[] = {
93 MDOC_Std,
94 MDOC_ARG_MAX
95 };
96
97 static const enum mdocargt args_An[] = {
98 MDOC_Split,
99 MDOC_Nosplit,
100 MDOC_ARG_MAX
101 };
102
127 static const enum mdocargt args_Bl[] = {
128 MDOC_Bullet,
129 MDOC_Dash,
130 MDOC_Hyphen,
131 MDOC_Item,
132 MDOC_Enum,
133 MDOC_Tag,
134 MDOC_Diag,
135 MDOC_Hang,
136 MDOC_Ohang,
137 MDOC_Inset,
138 MDOC_Column,
139 MDOC_Width,
140 MDOC_Offset,
141 MDOC_Compact,
142 MDOC_Nested,
143 MDOC_ARG_MAX
144 };
145
146 static const struct mdocarg mdocargs[MDOC_MAX] = {
147 { ARGSFL_DELIM, NULL }, /* Ap */
148 { ARGSFL_NONE, NULL }, /* Dd */
149 { ARGSFL_NONE, NULL }, /* Dt */
150 { ARGSFL_NONE, NULL }, /* Os */
151 { ARGSFL_NONE, NULL }, /* Sh */
152 { ARGSFL_NONE, NULL }, /* Ss */
153 { ARGSFL_NONE, NULL }, /* Pp */
154 { ARGSFL_DELIM, NULL }, /* D1 */
155 { ARGSFL_DELIM, NULL }, /* Dl */
156 { ARGSFL_NONE, args_Bd }, /* Bd */
157 { ARGSFL_NONE, NULL }, /* Ed */
158 { ARGSFL_NONE, args_Bl }, /* Bl */
159 { ARGSFL_NONE, NULL }, /* El */
160 { ARGSFL_NONE, NULL }, /* It */
161 { ARGSFL_DELIM, NULL }, /* Ad */
162 { ARGSFL_DELIM, args_An }, /* An */
163 { ARGSFL_DELIM, NULL }, /* Ar */
164 { ARGSFL_DELIM, NULL }, /* Cd */
165 { ARGSFL_DELIM, NULL }, /* Cm */
166 { ARGSFL_DELIM, NULL }, /* Dv */
167 { ARGSFL_DELIM, NULL }, /* Er */
168 { ARGSFL_DELIM, NULL }, /* Ev */
169 { ARGSFL_NONE, args_Ex }, /* Ex */
170 { ARGSFL_DELIM, NULL }, /* Fa */
171 { ARGSFL_NONE, NULL }, /* Fd */
172 { ARGSFL_DELIM, NULL }, /* Fl */
173 { ARGSFL_DELIM, NULL }, /* Fn */
174 { ARGSFL_DELIM, NULL }, /* Ft */
175 { ARGSFL_DELIM, NULL }, /* Ic */
176 { ARGSFL_DELIM, NULL }, /* In */
177 { ARGSFL_DELIM, NULL }, /* Li */
178 { ARGSFL_NONE, NULL }, /* Nd */
179 { ARGSFL_DELIM, NULL }, /* Nm */
180 { ARGSFL_DELIM, NULL }, /* Op */
181 { ARGSFL_NONE, NULL }, /* Ot */
182 { ARGSFL_DELIM, NULL }, /* Pa */
183 { ARGSFL_NONE, args_Ex }, /* Rv */
184 { ARGSFL_DELIM, NULL }, /* St */
185 { ARGSFL_DELIM, NULL }, /* Va */
186 { ARGSFL_DELIM, NULL }, /* Vt */
187 { ARGSFL_DELIM, NULL }, /* Xr */
188 { ARGSFL_NONE, NULL }, /* %A */
189 { ARGSFL_NONE, NULL }, /* %B */
190 { ARGSFL_NONE, NULL }, /* %D */
191 { ARGSFL_NONE, NULL }, /* %I */
192 { ARGSFL_NONE, NULL }, /* %J */
193 { ARGSFL_NONE, NULL }, /* %N */
194 { ARGSFL_NONE, NULL }, /* %O */
195 { ARGSFL_NONE, NULL }, /* %P */
196 { ARGSFL_NONE, NULL }, /* %R */
224 { ARGSFL_DELIM, NULL }, /* Pf */
225 { ARGSFL_NONE, NULL }, /* Po */
226 { ARGSFL_DELIM, NULL }, /* Pq */
227 { ARGSFL_DELIM, NULL }, /* Qc */
228 { ARGSFL_DELIM, NULL }, /* Ql */
229 { ARGSFL_NONE, NULL }, /* Qo */
230 { ARGSFL_DELIM, NULL }, /* Qq */
231 { ARGSFL_NONE, NULL }, /* Re */
232 { ARGSFL_NONE, NULL }, /* Rs */
233 { ARGSFL_DELIM, NULL }, /* Sc */
234 { ARGSFL_NONE, NULL }, /* So */
235 { ARGSFL_DELIM, NULL }, /* Sq */
236 { ARGSFL_NONE, NULL }, /* Sm */
237 { ARGSFL_DELIM, NULL }, /* Sx */
238 { ARGSFL_DELIM, NULL }, /* Sy */
239 { ARGSFL_DELIM, NULL }, /* Tn */
240 { ARGSFL_DELIM, NULL }, /* Ux */
241 { ARGSFL_DELIM, NULL }, /* Xc */
242 { ARGSFL_NONE, NULL }, /* Xo */
243 { ARGSFL_NONE, NULL }, /* Fo */
244 { ARGSFL_DELIM, NULL }, /* Fc */
245 { ARGSFL_NONE, NULL }, /* Oo */
246 { ARGSFL_DELIM, NULL }, /* Oc */
247 { ARGSFL_NONE, args_Bk }, /* Bk */
248 { ARGSFL_NONE, NULL }, /* Ek */
249 { ARGSFL_NONE, NULL }, /* Bt */
250 { ARGSFL_NONE, NULL }, /* Hf */
251 { ARGSFL_NONE, NULL }, /* Fr */
252 { ARGSFL_NONE, NULL }, /* Ud */
253 { ARGSFL_DELIM, NULL }, /* Lb */
254 { ARGSFL_NONE, NULL }, /* Lp */
255 { ARGSFL_DELIM, NULL }, /* Lk */
256 { ARGSFL_DELIM, NULL }, /* Mt */
257 { ARGSFL_DELIM, NULL }, /* Brq */
258 { ARGSFL_NONE, NULL }, /* Bro */
259 { ARGSFL_DELIM, NULL }, /* Brc */
260 { ARGSFL_NONE, NULL }, /* %C */
261 { ARGSFL_NONE, NULL }, /* Es */
262 { ARGSFL_NONE, NULL }, /* En */
263 { ARGSFL_DELIM, NULL }, /* Dx */
264 { ARGSFL_NONE, NULL }, /* %Q */
265 { ARGSFL_NONE, NULL }, /* br */
266 { ARGSFL_NONE, NULL }, /* sp */
267 { ARGSFL_NONE, NULL }, /* %U */
268 { ARGSFL_NONE, NULL }, /* Ta */
269 };
270
271
272 /*
273 * Parse an argument from line text. This comes in the form of -key
274 * [value0...], which may either have a single mandatory value, at least
275 * one mandatory value, an optional single value, or no value.
276 */
277 enum margverr
278 mdoc_argv(struct mdoc *mdoc, int line, enum mdoct tok,
279 struct mdoc_arg **v, int *pos, char *buf)
280 {
281 char *p, sv;
282 struct mdoc_argv tmp;
283 struct mdoc_arg *arg;
284 const enum mdocargt *ap;
285
286 if ('\0' == buf[*pos])
287 return(ARGV_EOLN);
288 else if (NULL == (ap = mdocargs[tok].argvs))
289 return(ARGV_WORD);
290 else if ('-' != buf[*pos])
291 return(ARGV_WORD);
292
293 /* Seek to the first unescaped space. */
294
295 p = &buf[++(*pos)];
296
297 assert(*pos > 0);
298
325 if (0 == strcmp(p, mdoc_argnames[tmp.arg]))
326 break;
327
328 if (MDOC_ARG_MAX == tmp.arg) {
329 /*
330 * The flag was not found.
331 * Restore saved zeroed byte and return as a word.
332 */
333 if (sv)
334 buf[*pos - 1] = sv;
335 return(ARGV_WORD);
336 }
337
338 /* Read to the next word (the argument). */
339
340 while (buf[*pos] && ' ' == buf[*pos])
341 (*pos)++;
342
343 switch (argvflags[tmp.arg]) {
344 case (ARGV_SINGLE):
345 if ( ! argv_single(mdoc, line, &tmp, pos, buf))
346 return(ARGV_ERROR);
347 break;
348 case (ARGV_MULTI):
349 if ( ! argv_multi(mdoc, line, &tmp, pos, buf))
350 return(ARGV_ERROR);
351 break;
352 case (ARGV_NONE):
353 break;
354 }
355
356 if (NULL == (arg = *v))
357 arg = *v = mandoc_calloc(1, sizeof(struct mdoc_arg));
358
359 arg->argc++;
360 arg->argv = mandoc_realloc
361 (arg->argv, arg->argc * sizeof(struct mdoc_argv));
362
363 memcpy(&arg->argv[(int)arg->argc - 1],
364 &tmp, sizeof(struct mdoc_argv));
365
366 return(ARGV_ARG);
367 }
368
369 void
370 mdoc_argv_free(struct mdoc_arg *p)
371 {
390
391 static void
392 argn_free(struct mdoc_arg *p, int iarg)
393 {
394 struct mdoc_argv *arg;
395 int j;
396
397 arg = &p->argv[iarg];
398
399 if (arg->sz && arg->value) {
400 for (j = (int)arg->sz - 1; j >= 0; j--)
401 free(arg->value[j]);
402 free(arg->value);
403 }
404
405 for (--p->argc; iarg < (int)p->argc; iarg++)
406 p->argv[iarg] = p->argv[iarg+1];
407 }
408
409 enum margserr
410 mdoc_zargs(struct mdoc *mdoc, int line, int *pos, char *buf, char **v)
411 {
412
413 return(args(mdoc, line, pos, buf, ARGSFL_NONE, v));
414 }
415
416 enum margserr
417 mdoc_args(struct mdoc *mdoc, int line, int *pos,
418 char *buf, enum mdoct tok, char **v)
419 {
420 enum argsflag fl;
421 struct mdoc_node *n;
422
423 fl = mdocargs[tok].flags;
424
425 if (MDOC_It != tok)
426 return(args(mdoc, line, pos, buf, fl, v));
427
428 /*
429 * We know that we're in an `It', so it's reasonable to expect
430 * us to be sitting in a `Bl'. Someday this may not be the case
431 * (if we allow random `It's sitting out there), so provide a
432 * safe fall-back into the default behaviour.
433 */
434
435 for (n = mdoc->last; n; n = n->parent)
436 if (MDOC_Bl == n->tok)
437 if (LIST_column == n->norm->Bl.type) {
438 fl = ARGSFL_TABSEP;
439 break;
440 }
441
442 return(args(mdoc, line, pos, buf, fl, v));
443 }
444
445 static enum margserr
446 args(struct mdoc *mdoc, int line, int *pos,
447 char *buf, enum argsflag fl, char **v)
448 {
449 char *p, *pp;
450 int pairs;
451 enum margserr rc;
452
453 if ('\0' == buf[*pos]) {
454 if (MDOC_PPHRASE & mdoc->flags)
455 return(ARGS_EOLN);
456 /*
457 * If we're not in a partial phrase and the flag for
458 * being a phrase literal is still set, the punctuation
459 * is unterminated.
460 */
461 if (MDOC_PHRASELIT & mdoc->flags)
462 mdoc_pmsg(mdoc, line, *pos, MANDOCERR_BADQUOTE);
463
464 mdoc->flags &= ~MDOC_PHRASELIT;
465 return(ARGS_EOLN);
466 }
467
468 *v = &buf[*pos];
469
470 if (ARGSFL_DELIM == fl)
471 if (args_checkpunct(buf, *pos))
472 return(ARGS_PUNCT);
473
474 /*
475 * First handle TABSEP items, restricted to `Bl -column'. This
476 * ignores conventional token parsing and instead uses tabs or
477 * `Ta' macros to separate phrases. Phrases are parsed again
478 * for arguments at a later phase.
479 */
480
481 if (ARGSFL_TABSEP == fl) {
482 /* Scan ahead to tab (can't be escaped). */
483 p = strchr(*v, '\t');
484 pp = NULL;
485
486 /* Scan ahead to unescaped `Ta'. */
487 if ( ! (MDOC_PHRASELIT & mdoc->flags))
488 for (pp = *v; ; pp++) {
489 if (NULL == (pp = strstr(pp, "Ta")))
490 break;
491 if (pp > *v && ' ' != *(pp - 1))
492 continue;
493 if (' ' == *(pp + 2) || '\0' == *(pp + 2))
494 break;
495 }
496
497 /* By default, assume a phrase. */
498 rc = ARGS_PHRASE;
499
500 /*
501 * Adjust new-buffer position to be beyond delimiter
502 * mark (e.g., Ta -> end + 2).
503 */
504 if (p && pp) {
505 *pos += pp < p ? 2 : 1;
506 rc = pp < p ? ARGS_PHRASE : ARGS_PPHRASE;
507 p = pp < p ? pp : p;
508 } else if (p && ! pp) {
509 rc = ARGS_PPHRASE;
510 *pos += 1;
511 } else if (pp && ! p) {
512 p = pp;
513 *pos += 2;
514 } else {
515 rc = ARGS_PEND;
516 p = strchr(*v, 0);
517 }
518
519 /* Whitespace check for eoln case... */
520 if ('\0' == *p && ' ' == *(p - 1))
521 mdoc_pmsg(mdoc, line, *pos, MANDOCERR_EOLNSPACE);
522
523 *pos += (int)(p - *v);
524
525 /* Strip delimiter's preceding whitespace. */
526 pp = p - 1;
527 while (pp > *v && ' ' == *pp) {
528 if (pp > *v && '\\' == *(pp - 1))
529 break;
530 pp--;
531 }
532 *(pp + 1) = 0;
533
534 /* Strip delimiter's proceeding whitespace. */
535 for (pp = &buf[*pos]; ' ' == *pp; pp++, (*pos)++)
536 /* Skip ahead. */ ;
537
538 return(rc);
539 }
540
541 /*
542 * Process a quoted literal. A quote begins with a double-quote
543 * and ends with a double-quote NOT preceded by a double-quote.
544 * NUL-terminate the literal in place.
545 * Collapse pairs of quotes inside quoted literals.
546 * Whitespace is NOT involved in literal termination.
547 */
548
549 if (MDOC_PHRASELIT & mdoc->flags || '\"' == buf[*pos]) {
550 if ( ! (MDOC_PHRASELIT & mdoc->flags))
551 *v = &buf[++(*pos)];
552
553 if (MDOC_PPHRASE & mdoc->flags)
554 mdoc->flags |= MDOC_PHRASELIT;
555
556 pairs = 0;
557 for ( ; buf[*pos]; (*pos)++) {
558 /* Move following text left after quoted quotes. */
559 if (pairs)
560 buf[*pos - pairs] = buf[*pos];
561 if ('\"' != buf[*pos])
562 continue;
563 /* Unquoted quotes end quoted args. */
564 if ('\"' != buf[*pos + 1])
565 break;
566 /* Quoted quotes collapse. */
567 pairs++;
568 (*pos)++;
569 }
570 if (pairs)
571 buf[*pos - pairs] = '\0';
572
573 if ('\0' == buf[*pos]) {
574 if (MDOC_PPHRASE & mdoc->flags)
575 return(ARGS_QWORD);
576 mdoc_pmsg(mdoc, line, *pos, MANDOCERR_BADQUOTE);
577 return(ARGS_QWORD);
578 }
579
580 mdoc->flags &= ~MDOC_PHRASELIT;
581 buf[(*pos)++] = '\0';
582
583 if ('\0' == buf[*pos])
584 return(ARGS_QWORD);
585
586 while (' ' == buf[*pos])
587 (*pos)++;
588
589 if ('\0' == buf[*pos])
590 mdoc_pmsg(mdoc, line, *pos, MANDOCERR_EOLNSPACE);
591
592 return(ARGS_QWORD);
593 }
594
595 p = &buf[*pos];
596 *v = mandoc_getarg(mdoc->parse, &p, line, pos);
597
598 return(ARGS_WORD);
599 }
600
601 /*
602 * Check if the string consists only of space-separated closing
603 * delimiters. This is a bit of a dance: the first must be a close
604 * delimiter, but it may be followed by middle delimiters. Arbitrary
605 * whitespace may separate these tokens.
606 */
607 static int
608 args_checkpunct(const char *buf, int i)
609 {
610 int j;
611 char dbuf[DELIMSZ];
612 enum mdelim d;
613
614 /* First token must be a close-delimiter. */
615
616 for (j = 0; buf[i] && ' ' != buf[i] && j < DELIMSZ; j++, i++)
632 j = 0;
633 while (buf[i] && ' ' != buf[i] && j < DELIMSZ)
634 dbuf[j++] = buf[i++];
635
636 if (DELIMSZ == j)
637 return(0);
638
639 dbuf[j] = '\0';
640 d = mdoc_isdelim(dbuf);
641 if (DELIM_NONE == d || DELIM_OPEN == d)
642 return(0);
643
644 while (' ' == buf[i])
645 i++;
646 }
647
648 return('\0' == buf[i]);
649 }
650
651 static int
652 argv_multi(struct mdoc *mdoc, int line,
653 struct mdoc_argv *v, int *pos, char *buf)
654 {
655 enum margserr ac;
656 char *p;
657
658 for (v->sz = 0; ; v->sz++) {
659 if ('-' == buf[*pos])
660 break;
661 ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);
662 if (ARGS_ERROR == ac)
663 return(0);
664 else if (ARGS_EOLN == ac)
665 break;
666
667 if (0 == v->sz % MULTI_STEP)
668 v->value = mandoc_realloc(v->value,
669 (v->sz + MULTI_STEP) * sizeof(char *));
670
671 v->value[(int)v->sz] = mandoc_strdup(p);
672 }
673
674 return(1);
675 }
676
677 static int
678 argv_single(struct mdoc *mdoc, int line,
679 struct mdoc_argv *v, int *pos, char *buf)
680 {
681 enum margserr ac;
682 char *p;
683
684 ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);
685 if (ARGS_ERROR == ac)
686 return(0);
687 if (ARGS_EOLN == ac)
688 return(1);
689
690 v->sz = 1;
691 v->value = mandoc_malloc(sizeof(char *));
692 v->value[0] = mandoc_strdup(p);
693
694 return(1);
695 }
|