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