]>
git.cameronkatri.com Git - mandoc.git/blob - argv.c
1 /* $Id: argv.c,v 1.17 2009/01/19 17:02:58 kristaps Exp $ */
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
29 /* FIXME: .It called with -column and quoted arguments. */
31 static int lookup(int, const char *);
32 static int parse(struct mdoc
*, int,
33 struct mdoc_arg
*, int *, char *);
34 static int parse_single(struct mdoc
*, int,
35 struct mdoc_arg
*, int *, char *);
36 static int parse_multi(struct mdoc
*, int,
37 struct mdoc_arg
*, int *, char *);
38 static int postparse(struct mdoc
*, int,
39 const struct mdoc_arg
*, int);
43 mdoc_args(struct mdoc
*mdoc
, int line
, int *pos
, char *buf
, int fl
, char **v
)
50 if ('\"' == buf
[*pos
] && ! (fl
& ARGS_QUOTED
))
51 if ( ! mdoc_pwarn(mdoc
, line
, *pos
, WARN_SYNTAX
, "unexpected quoted parameter"))
55 if ( ! mdoc_pwarn(mdoc
, line
, *pos
, WARN_SYNTAX
, "argument-like parameter"))
58 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
60 * If ARGS_DELIM, return ARGS_PUNCT if only space-separated
61 * punctuation remains.
63 for (i
= *pos
; buf
[i
]; ) {
64 if ( ! mdoc_iscdelim(buf
[i
]))
67 if (0 == buf
[i
] || ! isspace(buf
[i
]))
70 while (buf
[i
] && isspace(buf
[i
]))
79 /* Parse routine for non-quoted string. */
82 if ('\"' != buf
[*pos
] || ! (ARGS_QUOTED
& fl
)) {
85 /* FIXME: UGLY tab-sep processing. */
89 if ('\t' == buf
[*pos
])
91 if ('T' == buf
[*pos
]) {
95 if ('a' == buf
[*pos
]) {
104 if (isspace(buf
[*pos
]))
105 if ('\\' != buf
[*pos
- 1])
119 if ( ! (ARGS_TABSEP
& fl
))
120 while (buf
[*pos
] && isspace(buf
[*pos
]))
126 if ( ! mdoc_pwarn(mdoc
, line
, *pos
, WARN_COMPAT
, "whitespace at end-of-line"))
133 * If we're a quoted string (and quoted strings are allowed),
134 * then parse ahead to the next quote. If none's found, it's an
135 * error. After, parse to the next word.
140 while (buf
[*pos
] && '\"' != buf
[*pos
])
143 if (0 == buf
[*pos
]) {
144 (void)mdoc_perr(mdoc
, line
, *pos
, "unterminated quoted parameter");
152 while (buf
[*pos
] && isspace(buf
[*pos
]))
158 if ( ! mdoc_pwarn(mdoc
, line
, *pos
, WARN_COMPAT
, "whitespace at end-of-line"))
166 lookup(int tok
, const char *argv
)
171 if (xstrcmp(argv
, "split"))
173 else if (xstrcmp(argv
, "nosplit"))
174 return(MDOC_Nosplit
);
178 if (xstrcmp(argv
, "ragged"))
180 else if (xstrcmp(argv
, "unfilled"))
181 return(MDOC_Unfilled
);
182 else if (xstrcmp(argv
, "filled"))
184 else if (xstrcmp(argv
, "literal"))
185 return(MDOC_Literal
);
186 else if (xstrcmp(argv
, "file"))
188 else if (xstrcmp(argv
, "offset"))
193 if (xstrcmp(argv
, "emphasis"))
194 return(MDOC_Emphasis
);
195 else if (xstrcmp(argv
, "literal"))
196 return(MDOC_Literal
);
197 else if (xstrcmp(argv
, "symbolic"))
198 return(MDOC_Symbolic
);
202 if (xstrcmp(argv
, "words"))
207 if (xstrcmp(argv
, "bullet"))
209 else if (xstrcmp(argv
, "dash"))
211 else if (xstrcmp(argv
, "hyphen"))
213 else if (xstrcmp(argv
, "item"))
215 else if (xstrcmp(argv
, "enum"))
217 else if (xstrcmp(argv
, "tag"))
219 else if (xstrcmp(argv
, "diag"))
221 else if (xstrcmp(argv
, "hang"))
223 else if (xstrcmp(argv
, "ohang"))
225 else if (xstrcmp(argv
, "inset"))
227 else if (xstrcmp(argv
, "column"))
229 else if (xstrcmp(argv
, "width"))
231 else if (xstrcmp(argv
, "offset"))
233 else if (xstrcmp(argv
, "compact"))
234 return(MDOC_Compact
);
240 if (xstrcmp(argv
, "std"))
245 if (xstrcmp(argv
, "p1003.1-88"))
246 return(MDOC_p1003_1_88
);
247 else if (xstrcmp(argv
, "p1003.1-90"))
248 return(MDOC_p1003_1_90
);
249 else if (xstrcmp(argv
, "p1003.1-96"))
250 return(MDOC_p1003_1_96
);
251 else if (xstrcmp(argv
, "p1003.1-2001"))
252 return(MDOC_p1003_1_2001
);
253 else if (xstrcmp(argv
, "p1003.1-2004"))
254 return(MDOC_p1003_1_2004
);
255 else if (xstrcmp(argv
, "p1003.1"))
256 return(MDOC_p1003_1
);
257 else if (xstrcmp(argv
, "p1003.1b"))
258 return(MDOC_p1003_1b
);
259 else if (xstrcmp(argv
, "p1003.1b-93"))
260 return(MDOC_p1003_1b_93
);
261 else if (xstrcmp(argv
, "p1003.1c-95"))
262 return(MDOC_p1003_1c_95
);
263 else if (xstrcmp(argv
, "p1003.1g-2000"))
264 return(MDOC_p1003_1g_2000
);
265 else if (xstrcmp(argv
, "p1003.2-92"))
266 return(MDOC_p1003_2_92
);
267 else if (xstrcmp(argv
, "p1003.2-95"))
268 return(MDOC_p1387_2_95
);
269 else if (xstrcmp(argv
, "p1003.2"))
270 return(MDOC_p1003_2
);
271 else if (xstrcmp(argv
, "p1387.2-95"))
272 return(MDOC_p1387_2
);
273 else if (xstrcmp(argv
, "isoC-90"))
274 return(MDOC_isoC_90
);
275 else if (xstrcmp(argv
, "isoC-amd1"))
276 return(MDOC_isoC_amd1
);
277 else if (xstrcmp(argv
, "isoC-tcor1"))
278 return(MDOC_isoC_tcor1
);
279 else if (xstrcmp(argv
, "isoC-tcor2"))
280 return(MDOC_isoC_tcor2
);
281 else if (xstrcmp(argv
, "isoC-99"))
282 return(MDOC_isoC_99
);
283 else if (xstrcmp(argv
, "ansiC"))
285 else if (xstrcmp(argv
, "ansiC-89"))
286 return(MDOC_ansiC_89
);
287 else if (xstrcmp(argv
, "ansiC-99"))
288 return(MDOC_ansiC_99
);
289 else if (xstrcmp(argv
, "ieee754"))
290 return(MDOC_ieee754
);
291 else if (xstrcmp(argv
, "iso8802-3"))
292 return(MDOC_iso8802_3
);
293 else if (xstrcmp(argv
, "xpg3"))
295 else if (xstrcmp(argv
, "xpg4"))
297 else if (xstrcmp(argv
, "xpg4.2"))
299 else if (xstrcmp(argv
, "xpg4.3"))
301 else if (xstrcmp(argv
, "xbd5"))
303 else if (xstrcmp(argv
, "xcu5"))
305 else if (xstrcmp(argv
, "xsh5"))
307 else if (xstrcmp(argv
, "xns5"))
309 else if (xstrcmp(argv
, "xns5.2d2.0"))
310 return(MDOC_xns5_2d2_0
);
311 else if (xstrcmp(argv
, "xcurses4.2"))
312 return(MDOC_xcurses4_2
);
313 else if (xstrcmp(argv
, "susv2"))
315 else if (xstrcmp(argv
, "susv3"))
317 else if (xstrcmp(argv
, "svid4"))
325 return(MDOC_ARG_MAX
);
330 postparse(struct mdoc
*mdoc
, int line
, const struct mdoc_arg
*v
, int pos
)
337 if (xstrcmp(v
->value
[0], "left"))
339 if (xstrcmp(v
->value
[0], "right"))
341 if (xstrcmp(v
->value
[0], "center"))
343 if (xstrcmp(v
->value
[0], "indent"))
345 if (xstrcmp(v
->value
[0], "indent-two"))
347 return(mdoc_perr(mdoc
, line
, pos
, "invalid offset value"));
357 parse_multi(struct mdoc
*mdoc
, int line
,
358 struct mdoc_arg
*v
, int *pos
, char *buf
)
364 v
->value
= xcalloc(MDOC_LINEARG_MAX
, sizeof(char *));
368 for (v
->sz
= 0; v
->sz
< MDOC_LINEARG_MAX
; v
->sz
++) {
369 if ('-' == buf
[*pos
])
371 c
= mdoc_args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
372 if (ARGS_ERROR
== c
) {
375 } else if (ARGS_EOLN
== c
)
380 if (0 < v
->sz
&& v
->sz
< MDOC_LINEARG_MAX
)
384 return(mdoc_perr(mdoc
, line
, ppos
, 0 == v
->sz
?
385 "argument requires a value" :
386 "too many values to argument"));
391 parse_single(struct mdoc
*mdoc
, int line
,
392 struct mdoc_arg
*v
, int *pos
, char *buf
)
399 c
= mdoc_args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
403 return(mdoc_perr(mdoc
, line
, ppos
, "argument requires a value"));
406 v
->value
= xcalloc(1, sizeof(char *));
413 parse(struct mdoc
*mdoc
, int line
,
414 struct mdoc_arg
*v
, int *pos
, char *buf
)
426 return(parse_single(mdoc
, line
, v
, pos
, buf
));
428 return(parse_multi(mdoc
, line
, v
, pos
, buf
));
438 mdoc_argv(struct mdoc
*mdoc
, int line
, int tok
,
439 struct mdoc_arg
*v
, int *pos
, char *buf
)
444 (void)memset(v
, 0, sizeof(struct mdoc_arg
));
449 assert( ! isspace(buf
[*pos
]));
451 if ('-' != buf
[*pos
])
455 argv
= &buf
[++(*pos
)];
462 if (isspace(buf
[*pos
]))
463 if ('\\' != buf
[*pos
- 1])
471 if (MDOC_ARG_MAX
== (v
->arg
= lookup(tok
, argv
))) {
472 if ( ! mdoc_pwarn(mdoc
, line
, i
, WARN_SYNTAX
, "argument-like parameter"))
477 while (buf
[*pos
] && isspace(buf
[*pos
]))
480 /* FIXME: whitespace if no value. */
483 if ( ! parse(mdoc
, line
, v
, pos
, buf
))
485 if ( ! postparse(mdoc
, line
, v
, ppos
))
493 mdoc_argv_free(int sz
, struct mdoc_arg
*arg
)
497 for (i
= 0; i
< sz
; i
++) {
498 if (0 == arg
[i
].sz
) {
499 assert(NULL
== arg
[i
].value
);
502 assert(arg
[i
].value
);