]>
git.cameronkatri.com Git - mandoc.git/blob - argv.c
1 /* $Id: argv.c,v 1.2 2008/12/28 00:34:20 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 static int lookup(int, const char *);
30 static int parse(struct mdoc
*, int,
31 struct mdoc_arg
*, int *, char *);
32 static int postparse(struct mdoc
*, int,
33 const struct mdoc_arg
*, int);
37 mdoc_args(struct mdoc
*mdoc
, int tok
, int *pos
, char *buf
, int fl
, char **v
)
44 if ('\"' == buf
[*pos
] && ! (fl
& ARGS_QUOTED
))
45 if ( ! mdoc_warn(mdoc
, tok
, *pos
, WARN_SYNTAX_QUOTED
))
49 if ( ! mdoc_warn(mdoc
, tok
, *pos
, WARN_SYNTAX_ARGLIKE
))
52 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
53 for (i
= *pos
; buf
[i
]; ) {
54 if ( ! mdoc_iscdelim(buf
[i
]))
57 if (0 == buf
[i
] || ! isspace(buf
[i
]))
60 while (buf
[i
] && isspace(buf
[i
]))
70 * Parse routine for non-quoted string.
73 if ('\"' != buf
[*pos
]) {
76 while (buf
[*pos
] && ! isspace(buf
[*pos
]))
86 while (buf
[*pos
] && isspace(buf
[*pos
]))
92 if ( ! mdoc_warn(mdoc
, tok
, *pos
, WARN_SYNTAX_WS_EOLN
))
99 * If we're a quoted string (and quoted strings are allowed),
100 * then parse ahead to the next quote. If none's found, it's an
101 * error. After, parse to the next word. We're not allowed to
102 * also be DELIM requests (for now).
104 assert( ! (fl
& ARGS_DELIM
));
108 while (buf
[*pos
] && '\"' != buf
[*pos
])
111 if (0 == buf
[*pos
]) {
112 (void)mdoc_err(mdoc
, tok
, *pos
, ERR_SYNTAX_UNQUOTE
);
120 while (buf
[*pos
] && isspace(buf
[*pos
]))
126 if ( ! mdoc_warn(mdoc
, tok
, *pos
, WARN_SYNTAX_WS_EOLN
))
134 lookup(int tok
, const char *argv
)
139 if (xstrcmp(argv
, "ragged"))
141 else if (xstrcmp(argv
, "unfilled"))
142 return(MDOC_Unfilled
);
143 else if (xstrcmp(argv
, "literal"))
144 return(MDOC_Literal
);
145 else if (xstrcmp(argv
, "file"))
147 else if (xstrcmp(argv
, "offset"))
152 if (xstrcmp(argv
, "bullet"))
154 else if (xstrcmp(argv
, "dash"))
156 else if (xstrcmp(argv
, "hyphen"))
158 else if (xstrcmp(argv
, "item"))
160 else if (xstrcmp(argv
, "enum"))
162 else if (xstrcmp(argv
, "tag"))
164 else if (xstrcmp(argv
, "diag"))
166 else if (xstrcmp(argv
, "hang"))
168 else if (xstrcmp(argv
, "ohang"))
170 else if (xstrcmp(argv
, "inset"))
172 else if (xstrcmp(argv
, "column"))
174 else if (xstrcmp(argv
, "width"))
176 else if (xstrcmp(argv
, "offset"))
178 else if (xstrcmp(argv
, "compact"))
179 return(MDOC_Compact
);
187 return(MDOC_ARG_MAX
);
192 postparse(struct mdoc
*mdoc
, int tok
, const struct mdoc_arg
*v
, int pos
)
199 if (xstrcmp(v
->value
[0], "left"))
201 if (xstrcmp(v
->value
[0], "right"))
203 if (xstrcmp(v
->value
[0], "center"))
205 if (xstrcmp(v
->value
[0], "indent"))
207 if (xstrcmp(v
->value
[0], "indent-two"))
209 return(mdoc_err(mdoc
, tok
, pos
, ERR_SYNTAX_ARGBAD
));
219 parse(struct mdoc
*mdoc
, int tok
,
220 struct mdoc_arg
*v
, int *pos
, char *buf
)
265 * This has a single value for an argument.
267 c
= mdoc_args(mdoc
, tok
, pos
, buf
, ARGS_QUOTED
, &p
);
270 else if (ARGS_EOLN
== c
)
271 return(mdoc_err(mdoc
, tok
, ppos
, ERR_SYNTAX_ARGVAL
));
274 v
->value
= xcalloc(1, sizeof(char *));
280 * This has several value for a single argument. We
281 * pre-allocate a pointer array and don't let it exceed
285 v
->value
= xcalloc(MDOC_LINEARG_MAX
, sizeof(char *));
286 for (i
= 0; i
< MDOC_LINEARG_MAX
; i
++) {
287 c
= mdoc_args(mdoc
, tok
, pos
, buf
, ARGS_QUOTED
, &p
);
288 if (ARGS_ERROR
== c
) {
291 } else if (ARGS_EOLN
== c
)
297 return(mdoc_err(mdoc
, tok
, ppos
, ERR_SYNTAX_ARGVAL
));
298 } else if (MDOC_LINEARG_MAX
== i
)
299 return(mdoc_err(mdoc
, tok
, ppos
, ERR_SYNTAX_ARGMANY
));
313 mdoc_argv(struct mdoc
*mdoc
, int tok
,
314 struct mdoc_arg
*v
, int *pos
, char *buf
)
319 (void)memset(v
, 0, sizeof(struct mdoc_arg
));
324 assert( ! isspace(buf
[*pos
]));
326 if ('-' != buf
[*pos
]) {
327 (void)mdoc_err(mdoc
, tok
, *pos
, ERR_SYNTAX_ARGFORM
);
332 argv
= &buf
[++(*pos
)];
334 while (buf
[*pos
] && ! isspace(buf
[*pos
]))
340 if (MDOC_ARG_MAX
== (v
->arg
= lookup(tok
, argv
))) {
341 (void)mdoc_err(mdoc
, tok
, i
, ERR_SYNTAX_ARG
);
345 while (buf
[*pos
] && isspace(buf
[*pos
]))
348 /* FIXME: whitespace if no value. */
351 if ( ! parse(mdoc
, tok
, v
, pos
, buf
))
353 if ( ! postparse(mdoc
, tok
, v
, ppos
))
361 mdoc_argv_free(int sz
, struct mdoc_arg
*arg
)
365 for (i
= 0; i
< sz
; i
++) {
366 if (0 == arg
[i
].sz
) {
367 assert(NULL
== arg
[i
].value
);
370 assert(arg
[i
].value
);