]>
git.cameronkatri.com Git - mandoc.git/blob - argv.c
1 /* $Id: argv.c,v 1.15 2009/01/16 12:23:25 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, 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. */
81 if ('\"' != buf
[*pos
]) {
84 /* FIXME: UGLY tab-sep processing. */
88 if ('\t' == buf
[*pos
])
90 if ('T' == buf
[*pos
]) {
94 if ('a' == buf
[*pos
]) {
102 while (buf
[*pos
] && ! isspace(buf
[*pos
]))
113 if ( ! (ARGS_TABSEP
& fl
))
114 while (buf
[*pos
] && isspace(buf
[*pos
]))
120 if ( ! mdoc_pwarn(mdoc
, line
, *pos
, WARN_COMPAT
, "whitespace at end-of-line"))
127 * If we're a quoted string (and quoted strings are allowed),
128 * then parse ahead to the next quote. If none's found, it's an
129 * error. After, parse to the next word.
134 while (buf
[*pos
] && '\"' != buf
[*pos
])
137 if (0 == buf
[*pos
]) {
138 (void)mdoc_perr(mdoc
, line
, *pos
, "unterminated quoted parameter");
146 while (buf
[*pos
] && isspace(buf
[*pos
]))
152 if ( ! mdoc_pwarn(mdoc
, line
, *pos
, WARN_COMPAT
, "whitespace at end-of-line"))
160 lookup(int tok
, const char *argv
)
165 if (xstrcmp(argv
, "split"))
167 else if (xstrcmp(argv
, "nosplit"))
168 return(MDOC_Nosplit
);
172 if (xstrcmp(argv
, "ragged"))
174 else if (xstrcmp(argv
, "unfilled"))
175 return(MDOC_Unfilled
);
176 else if (xstrcmp(argv
, "filled"))
178 else if (xstrcmp(argv
, "literal"))
179 return(MDOC_Literal
);
180 else if (xstrcmp(argv
, "file"))
182 else if (xstrcmp(argv
, "offset"))
187 if (xstrcmp(argv
, "emphasis"))
188 return(MDOC_Emphasis
);
189 else if (xstrcmp(argv
, "literal"))
190 return(MDOC_Literal
);
191 else if (xstrcmp(argv
, "symbolic"))
192 return(MDOC_Symbolic
);
196 if (xstrcmp(argv
, "words"))
201 if (xstrcmp(argv
, "bullet"))
203 else if (xstrcmp(argv
, "dash"))
205 else if (xstrcmp(argv
, "hyphen"))
207 else if (xstrcmp(argv
, "item"))
209 else if (xstrcmp(argv
, "enum"))
211 else if (xstrcmp(argv
, "tag"))
213 else if (xstrcmp(argv
, "diag"))
215 else if (xstrcmp(argv
, "hang"))
217 else if (xstrcmp(argv
, "ohang"))
219 else if (xstrcmp(argv
, "inset"))
221 else if (xstrcmp(argv
, "column"))
223 else if (xstrcmp(argv
, "width"))
225 else if (xstrcmp(argv
, "offset"))
227 else if (xstrcmp(argv
, "compact"))
228 return(MDOC_Compact
);
234 if (xstrcmp(argv
, "std"))
239 if (xstrcmp(argv
, "p1003.1-88"))
240 return(MDOC_p1003_1_88
);
241 else if (xstrcmp(argv
, "p1003.1-90"))
242 return(MDOC_p1003_1_90
);
243 else if (xstrcmp(argv
, "p1003.1-96"))
244 return(MDOC_p1003_1_96
);
245 else if (xstrcmp(argv
, "p1003.1-2001"))
246 return(MDOC_p1003_1_2001
);
247 else if (xstrcmp(argv
, "p1003.1-2004"))
248 return(MDOC_p1003_1_2004
);
249 else if (xstrcmp(argv
, "p1003.1"))
250 return(MDOC_p1003_1
);
251 else if (xstrcmp(argv
, "p1003.1b"))
252 return(MDOC_p1003_1b
);
253 else if (xstrcmp(argv
, "p1003.1b-93"))
254 return(MDOC_p1003_1b_93
);
255 else if (xstrcmp(argv
, "p1003.1c-95"))
256 return(MDOC_p1003_1c_95
);
257 else if (xstrcmp(argv
, "p1003.1g-2000"))
258 return(MDOC_p1003_1g_2000
);
259 else if (xstrcmp(argv
, "p1003.2-92"))
260 return(MDOC_p1003_2_92
);
261 else if (xstrcmp(argv
, "p1003.2-95"))
262 return(MDOC_p1387_2_95
);
263 else if (xstrcmp(argv
, "p1003.2"))
264 return(MDOC_p1003_2
);
265 else if (xstrcmp(argv
, "p1387.2-95"))
266 return(MDOC_p1387_2
);
267 else if (xstrcmp(argv
, "isoC-90"))
268 return(MDOC_isoC_90
);
269 else if (xstrcmp(argv
, "isoC-amd1"))
270 return(MDOC_isoC_amd1
);
271 else if (xstrcmp(argv
, "isoC-tcor1"))
272 return(MDOC_isoC_tcor1
);
273 else if (xstrcmp(argv
, "isoC-tcor2"))
274 return(MDOC_isoC_tcor2
);
275 else if (xstrcmp(argv
, "isoC-99"))
276 return(MDOC_isoC_99
);
277 else if (xstrcmp(argv
, "ansiC"))
279 else if (xstrcmp(argv
, "ansiC-89"))
280 return(MDOC_ansiC_89
);
281 else if (xstrcmp(argv
, "ansiC-99"))
282 return(MDOC_ansiC_99
);
283 else if (xstrcmp(argv
, "ieee754"))
284 return(MDOC_ieee754
);
285 else if (xstrcmp(argv
, "iso8802-3"))
286 return(MDOC_iso8802_3
);
287 else if (xstrcmp(argv
, "xpg3"))
289 else if (xstrcmp(argv
, "xpg4"))
291 else if (xstrcmp(argv
, "xpg4.2"))
293 else if (xstrcmp(argv
, "xpg4.3"))
295 else if (xstrcmp(argv
, "xbd5"))
297 else if (xstrcmp(argv
, "xcu5"))
299 else if (xstrcmp(argv
, "xsh5"))
301 else if (xstrcmp(argv
, "xns5"))
303 else if (xstrcmp(argv
, "xns5.2d2.0"))
304 return(MDOC_xns5_2d2_0
);
305 else if (xstrcmp(argv
, "xcurses4.2"))
306 return(MDOC_xcurses4_2
);
307 else if (xstrcmp(argv
, "susv2"))
309 else if (xstrcmp(argv
, "susv3"))
311 else if (xstrcmp(argv
, "svid4"))
319 return(MDOC_ARG_MAX
);
324 postparse(struct mdoc
*mdoc
, int line
, const struct mdoc_arg
*v
, int pos
)
331 if (xstrcmp(v
->value
[0], "left"))
333 if (xstrcmp(v
->value
[0], "right"))
335 if (xstrcmp(v
->value
[0], "center"))
337 if (xstrcmp(v
->value
[0], "indent"))
339 if (xstrcmp(v
->value
[0], "indent-two"))
341 return(mdoc_perr(mdoc
, line
, pos
, "invalid offset value"));
351 parse_multi(struct mdoc
*mdoc
, int line
,
352 struct mdoc_arg
*v
, int *pos
, char *buf
)
358 v
->value
= xcalloc(MDOC_LINEARG_MAX
, sizeof(char *));
362 for (v
->sz
= 0; v
->sz
< MDOC_LINEARG_MAX
; v
->sz
++) {
363 if ('-' == buf
[*pos
])
365 c
= mdoc_args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
366 if (ARGS_ERROR
== c
) {
369 } else if (ARGS_EOLN
== c
)
374 if (0 < v
->sz
&& v
->sz
< MDOC_LINEARG_MAX
)
378 return(mdoc_perr(mdoc
, line
, ppos
, 0 == v
->sz
?
379 "argument requires a value" :
380 "too many values to argument"));
385 parse_single(struct mdoc
*mdoc
, int line
,
386 struct mdoc_arg
*v
, int *pos
, char *buf
)
393 c
= mdoc_args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
397 return(mdoc_perr(mdoc
, line
, ppos
, "argument requires a value"));
400 v
->value
= xcalloc(1, sizeof(char *));
407 parse(struct mdoc
*mdoc
, int line
, int tok
,
408 struct mdoc_arg
*v
, int *pos
, char *buf
)
420 return(parse_single(mdoc
, line
, v
, pos
, buf
));
422 return(parse_multi(mdoc
, line
, v
, pos
, buf
));
432 mdoc_argv(struct mdoc
*mdoc
, int line
, int tok
,
433 struct mdoc_arg
*v
, int *pos
, char *buf
)
438 (void)memset(v
, 0, sizeof(struct mdoc_arg
));
443 assert( ! isspace(buf
[*pos
]));
445 if ('-' != buf
[*pos
])
449 argv
= &buf
[++(*pos
)];
454 while (buf
[*pos
] && ! isspace(buf
[*pos
]))
460 if (MDOC_ARG_MAX
== (v
->arg
= lookup(tok
, argv
))) {
461 if ( ! mdoc_pwarn(mdoc
, line
, i
, WARN_SYNTAX
, "argument-like parameter"))
466 while (buf
[*pos
] && isspace(buf
[*pos
]))
469 /* FIXME: whitespace if no value. */
472 if ( ! parse(mdoc
, line
, tok
, v
, pos
, buf
))
474 if ( ! postparse(mdoc
, line
, v
, ppos
))
482 mdoc_argv_free(int sz
, struct mdoc_arg
*arg
)
486 for (i
= 0; i
< sz
; i
++) {
487 if (0 == arg
[i
].sz
) {
488 assert(NULL
== arg
[i
].value
);
491 assert(arg
[i
].value
);