]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
implement tagging for .Er
[mandoc.git] / mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.104 2015/04/19 14:00:19 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012, 2014, 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 <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "mandoc_aux.h"
28 #include "mandoc.h"
29 #include "roff.h"
30 #include "mdoc.h"
31 #include "libmandoc.h"
32 #include "libmdoc.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 roff_man *, int, int *,
56 char *, enum argsflag, char **);
57 static int args_checkpunct(const char *, int);
58 static void argv_multi(struct roff_man *, int,
59 struct mdoc_argv *, int *, char *);
60 static void argv_single(struct roff_man *, 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
103 static const enum mdocargt args_Bd[] = {
104 MDOC_Ragged,
105 MDOC_Unfilled,
106 MDOC_Filled,
107 MDOC_Literal,
108 MDOC_File,
109 MDOC_Offset,
110 MDOC_Compact,
111 MDOC_Centred,
112 MDOC_ARG_MAX
113 };
114
115 static const enum mdocargt args_Bf[] = {
116 MDOC_Emphasis,
117 MDOC_Literal,
118 MDOC_Symbolic,
119 MDOC_ARG_MAX
120 };
121
122 static const enum mdocargt args_Bk[] = {
123 MDOC_Words,
124 MDOC_ARG_MAX
125 };
126
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_DELIM, 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 */
197 { ARGSFL_NONE, NULL }, /* %T */
198 { ARGSFL_NONE, NULL }, /* %V */
199 { ARGSFL_DELIM, NULL }, /* Ac */
200 { ARGSFL_NONE, NULL }, /* Ao */
201 { ARGSFL_DELIM, NULL }, /* Aq */
202 { ARGSFL_DELIM, NULL }, /* At */
203 { ARGSFL_DELIM, NULL }, /* Bc */
204 { ARGSFL_NONE, args_Bf }, /* Bf */
205 { ARGSFL_NONE, NULL }, /* Bo */
206 { ARGSFL_DELIM, NULL }, /* Bq */
207 { ARGSFL_DELIM, NULL }, /* Bsx */
208 { ARGSFL_DELIM, NULL }, /* Bx */
209 { ARGSFL_NONE, NULL }, /* Db */
210 { ARGSFL_DELIM, NULL }, /* Dc */
211 { ARGSFL_NONE, NULL }, /* Do */
212 { ARGSFL_DELIM, NULL }, /* Dq */
213 { ARGSFL_DELIM, NULL }, /* Ec */
214 { ARGSFL_NONE, NULL }, /* Ef */
215 { ARGSFL_DELIM, NULL }, /* Em */
216 { ARGSFL_NONE, NULL }, /* Eo */
217 { ARGSFL_DELIM, NULL }, /* Fx */
218 { ARGSFL_DELIM, NULL }, /* Ms */
219 { ARGSFL_DELIM, NULL }, /* No */
220 { ARGSFL_DELIM, NULL }, /* Ns */
221 { ARGSFL_DELIM, NULL }, /* Nx */
222 { ARGSFL_DELIM, NULL }, /* Ox */
223 { ARGSFL_DELIM, NULL }, /* Pc */
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_DELIM, 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_DELIM, 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 { ARGSFL_NONE, NULL }, /* ll */
270 };
271
272
273 /*
274 * Parse flags and their arguments from the input line.
275 * These come in the form -flag [argument ...].
276 * Some flags take no argument, some one, some multiple.
277 */
278 void
279 mdoc_argv(struct roff_man *mdoc, int line, int tok,
280 struct mdoc_arg **reta, int *pos, char *buf)
281 {
282 struct mdoc_argv tmpv;
283 struct mdoc_argv **retv;
284 const enum mdocargt *argtable;
285 char *argname;
286 int ipos, retc;
287 char savechar;
288
289 *reta = NULL;
290
291 /* Which flags does this macro support? */
292
293 argtable = mdocargs[tok].argvs;
294 if (argtable == NULL)
295 return;
296
297 /* Loop over the flags on the input line. */
298
299 ipos = *pos;
300 while (buf[ipos] == '-') {
301
302 /* Seek to the first unescaped space. */
303
304 for (argname = buf + ++ipos; buf[ipos] != '\0'; ipos++)
305 if (buf[ipos] == ' ' && buf[ipos - 1] != '\\')
306 break;
307
308 /*
309 * We want to nil-terminate the word to look it up.
310 * But we may not have a flag, in which case we need
311 * to restore the line as-is. So keep around the
312 * stray byte, which we'll reset upon exiting.
313 */
314
315 if ((savechar = buf[ipos]) != '\0')
316 buf[ipos++] = '\0';
317
318 /*
319 * Now look up the word as a flag. Use temporary
320 * storage that we'll copy into the node's flags.
321 */
322
323 while ((tmpv.arg = *argtable++) != MDOC_ARG_MAX)
324 if ( ! strcmp(argname, mdoc_argnames[tmpv.arg]))
325 break;
326
327 /* If it isn't a flag, restore the saved byte. */
328
329 if (tmpv.arg == MDOC_ARG_MAX) {
330 if (savechar != '\0')
331 buf[ipos - 1] = savechar;
332 break;
333 }
334
335 /* Read to the next word (the first argument). */
336
337 while (buf[ipos] == ' ')
338 ipos++;
339
340 /* Parse the arguments of the flag. */
341
342 tmpv.line = line;
343 tmpv.pos = *pos;
344 tmpv.sz = 0;
345 tmpv.value = NULL;
346
347 switch (argvflags[tmpv.arg]) {
348 case ARGV_SINGLE:
349 argv_single(mdoc, line, &tmpv, &ipos, buf);
350 break;
351 case ARGV_MULTI:
352 argv_multi(mdoc, line, &tmpv, &ipos, buf);
353 break;
354 case ARGV_NONE:
355 break;
356 }
357
358 /* Append to the return values. */
359
360 if (*reta == NULL)
361 *reta = mandoc_calloc(1, sizeof(**reta));
362
363 retc = ++(*reta)->argc;
364 retv = &(*reta)->argv;
365 *retv = mandoc_reallocarray(*retv, retc, sizeof(**retv));
366 memcpy(*retv + retc - 1, &tmpv, sizeof(**retv));
367
368 /* Prepare for parsing the next flag. */
369
370 *pos = ipos;
371 argtable = mdocargs[tok].argvs;
372 }
373 }
374
375 void
376 mdoc_argv_free(struct mdoc_arg *p)
377 {
378 int i;
379
380 if (NULL == p)
381 return;
382
383 if (p->refcnt) {
384 --(p->refcnt);
385 if (p->refcnt)
386 return;
387 }
388 assert(p->argc);
389
390 for (i = (int)p->argc - 1; i >= 0; i--)
391 argn_free(p, i);
392
393 free(p->argv);
394 free(p);
395 }
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_args(struct roff_man *mdoc, int line, int *pos,
417 char *buf, int tok, char **v)
418 {
419 struct roff_node *n;
420 char *v_local;
421 enum argsflag fl;
422
423 if (v == NULL)
424 v = &v_local;
425 fl = tok == TOKEN_NONE ? ARGSFL_NONE : mdocargs[tok].flags;
426 if (tok != MDOC_It)
427 return(args(mdoc, line, pos, buf, fl, v));
428
429 /*
430 * We know that we're in an `It', so it's reasonable to expect
431 * us to be sitting in a `Bl'. Someday this may not be the case
432 * (if we allow random `It's sitting out there), so provide a
433 * safe fall-back into the default behaviour.
434 */
435
436 for (n = mdoc->last; n; n = n->parent)
437 if (MDOC_Bl == n->tok)
438 if (LIST_column == n->norm->Bl.type) {
439 fl = ARGSFL_TABSEP;
440 break;
441 }
442
443 return(args(mdoc, line, pos, buf, fl, v));
444 }
445
446 static enum margserr
447 args(struct roff_man *mdoc, int line, int *pos,
448 char *buf, enum argsflag fl, char **v)
449 {
450 char *p, *pp;
451 int pairs;
452 enum margserr rc;
453
454 if ('\0' == buf[*pos]) {
455 if (MDOC_PPHRASE & mdoc->flags)
456 return(ARGS_EOLN);
457 /*
458 * If we're not in a partial phrase and the flag for
459 * being a phrase literal is still set, the punctuation
460 * is unterminated.
461 */
462 if (MDOC_PHRASELIT & mdoc->flags)
463 mandoc_msg(MANDOCERR_ARG_QUOTE,
464 mdoc->parse, line, *pos, NULL);
465
466 mdoc->flags &= ~MDOC_PHRASELIT;
467 return(ARGS_EOLN);
468 }
469
470 *v = &buf[*pos];
471
472 if (ARGSFL_DELIM == fl)
473 if (args_checkpunct(buf, *pos))
474 return(ARGS_PUNCT);
475
476 /*
477 * First handle TABSEP items, restricted to `Bl -column'. This
478 * ignores conventional token parsing and instead uses tabs or
479 * `Ta' macros to separate phrases. Phrases are parsed again
480 * for arguments at a later phase.
481 */
482
483 if (ARGSFL_TABSEP == fl) {
484 /* Scan ahead to tab (can't be escaped). */
485 p = strchr(*v, '\t');
486 pp = NULL;
487
488 /* Scan ahead to unescaped `Ta'. */
489 if ( ! (MDOC_PHRASELIT & mdoc->flags))
490 for (pp = *v; ; pp++) {
491 if (NULL == (pp = strstr(pp, "Ta")))
492 break;
493 if (pp > *v && ' ' != *(pp - 1))
494 continue;
495 if (' ' == *(pp + 2) || '\0' == *(pp + 2))
496 break;
497 }
498
499 /* By default, assume a phrase. */
500 rc = ARGS_PHRASE;
501
502 /*
503 * Adjust new-buffer position to be beyond delimiter
504 * mark (e.g., Ta -> end + 2).
505 */
506 if (p && pp) {
507 *pos += pp < p ? 2 : 1;
508 rc = pp < p ? ARGS_PHRASE : ARGS_PPHRASE;
509 p = pp < p ? pp : p;
510 } else if (p && ! pp) {
511 rc = ARGS_PPHRASE;
512 *pos += 1;
513 } else if (pp && ! p) {
514 p = pp;
515 *pos += 2;
516 } else {
517 rc = ARGS_PEND;
518 p = strchr(*v, 0);
519 }
520
521 /* Whitespace check for eoln case... */
522 if ('\0' == *p && ' ' == *(p - 1))
523 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
524 line, *pos, NULL);
525
526 *pos += (int)(p - *v);
527
528 /* Strip delimiter's preceding whitespace. */
529 pp = p - 1;
530 while (pp > *v && ' ' == *pp) {
531 if (pp > *v && '\\' == *(pp - 1))
532 break;
533 pp--;
534 }
535 *(pp + 1) = 0;
536
537 /* Strip delimiter's proceeding whitespace. */
538 for (pp = &buf[*pos]; ' ' == *pp; pp++, (*pos)++)
539 /* Skip ahead. */ ;
540
541 return(rc);
542 }
543
544 /*
545 * Process a quoted literal. A quote begins with a double-quote
546 * and ends with a double-quote NOT preceded by a double-quote.
547 * NUL-terminate the literal in place.
548 * Collapse pairs of quotes inside quoted literals.
549 * Whitespace is NOT involved in literal termination.
550 */
551
552 if (MDOC_PHRASELIT & mdoc->flags || '\"' == buf[*pos]) {
553 if ( ! (MDOC_PHRASELIT & mdoc->flags))
554 *v = &buf[++(*pos)];
555
556 if (MDOC_PPHRASE & mdoc->flags)
557 mdoc->flags |= MDOC_PHRASELIT;
558
559 pairs = 0;
560 for ( ; buf[*pos]; (*pos)++) {
561 /* Move following text left after quoted quotes. */
562 if (pairs)
563 buf[*pos - pairs] = buf[*pos];
564 if ('\"' != buf[*pos])
565 continue;
566 /* Unquoted quotes end quoted args. */
567 if ('\"' != buf[*pos + 1])
568 break;
569 /* Quoted quotes collapse. */
570 pairs++;
571 (*pos)++;
572 }
573 if (pairs)
574 buf[*pos - pairs] = '\0';
575
576 if ('\0' == buf[*pos]) {
577 if (MDOC_PPHRASE & mdoc->flags)
578 return(ARGS_QWORD);
579 mandoc_msg(MANDOCERR_ARG_QUOTE,
580 mdoc->parse, line, *pos, NULL);
581 return(ARGS_QWORD);
582 }
583
584 mdoc->flags &= ~MDOC_PHRASELIT;
585 buf[(*pos)++] = '\0';
586
587 if ('\0' == buf[*pos])
588 return(ARGS_QWORD);
589
590 while (' ' == buf[*pos])
591 (*pos)++;
592
593 if ('\0' == buf[*pos])
594 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
595 line, *pos, NULL);
596
597 return(ARGS_QWORD);
598 }
599
600 p = &buf[*pos];
601 *v = mandoc_getarg(mdoc->parse, &p, line, pos);
602
603 return(ARGS_WORD);
604 }
605
606 /*
607 * Check if the string consists only of space-separated closing
608 * delimiters. This is a bit of a dance: the first must be a close
609 * delimiter, but it may be followed by middle delimiters. Arbitrary
610 * whitespace may separate these tokens.
611 */
612 static int
613 args_checkpunct(const char *buf, int i)
614 {
615 int j;
616 char dbuf[DELIMSZ];
617 enum mdelim d;
618
619 /* First token must be a close-delimiter. */
620
621 for (j = 0; buf[i] && ' ' != buf[i] && j < DELIMSZ; j++, i++)
622 dbuf[j] = buf[i];
623
624 if (DELIMSZ == j)
625 return(0);
626
627 dbuf[j] = '\0';
628 if (DELIM_CLOSE != mdoc_isdelim(dbuf))
629 return(0);
630
631 while (' ' == buf[i])
632 i++;
633
634 /* Remaining must NOT be open/none. */
635
636 while (buf[i]) {
637 j = 0;
638 while (buf[i] && ' ' != buf[i] && j < DELIMSZ)
639 dbuf[j++] = buf[i++];
640
641 if (DELIMSZ == j)
642 return(0);
643
644 dbuf[j] = '\0';
645 d = mdoc_isdelim(dbuf);
646 if (DELIM_NONE == d || DELIM_OPEN == d)
647 return(0);
648
649 while (' ' == buf[i])
650 i++;
651 }
652
653 return('\0' == buf[i]);
654 }
655
656 static void
657 argv_multi(struct roff_man *mdoc, int line,
658 struct mdoc_argv *v, int *pos, char *buf)
659 {
660 enum margserr ac;
661 char *p;
662
663 for (v->sz = 0; ; v->sz++) {
664 if (buf[*pos] == '-')
665 break;
666 ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);
667 if (ac == ARGS_EOLN)
668 break;
669
670 if (v->sz % MULTI_STEP == 0)
671 v->value = mandoc_reallocarray(v->value,
672 v->sz + MULTI_STEP, sizeof(char *));
673
674 v->value[(int)v->sz] = mandoc_strdup(p);
675 }
676 }
677
678 static void
679 argv_single(struct roff_man *mdoc, int line,
680 struct mdoc_argv *v, int *pos, char *buf)
681 {
682 enum margserr ac;
683 char *p;
684
685 ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);
686 if (ac == ARGS_EOLN)
687 return;
688
689 v->sz = 1;
690 v->value = mandoc_malloc(sizeof(char *));
691 v->value[0] = mandoc_strdup(p);
692 }