1 /* $Id: tbl_opts.c,v 1.21 2015/09/26 00:54:04 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
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.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
20 #include <sys/types.h>
28 #include "libmandoc.h"
33 #define KEY_LINESIZE 2
41 static const struct tbl_phrase keys
[] = {
46 {"allbox", TBL_OPT_ALLBOX
| TBL_OPT_BOX
},
48 {"frame", TBL_OPT_BOX
},
49 {"center", TBL_OPT_CENTRE
},
50 {"centre", TBL_OPT_CENTRE
},
51 {"doublebox", TBL_OPT_DBOX
},
52 {"doubleframe", TBL_OPT_DBOX
},
53 {"expand", TBL_OPT_EXPAND
},
54 {"nokeep", TBL_OPT_NOKEEP
},
55 {"nospaces", TBL_OPT_NOSPACE
},
56 {"nowarn", TBL_OPT_NOWARN
},
59 #define KEY_MAXKEYS ((int)(sizeof(keys)/sizeof(keys[0])))
61 static void arg(struct tbl_node
*, int, const char *, int *, int);
65 arg(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
, int key
)
69 while (p
[*pos
] == ' ' || p
[*pos
] == '\t')
72 /* Arguments are enclosed in parentheses. */
77 while (p
[*pos
+ len
] != ')')
83 mandoc_vmsg(MANDOCERR_TBLOPT_EQN
, tbl
->parse
,
84 ln
, *pos
, "%.*s", len
, p
+ *pos
);
90 tbl
->opts
.tab
= p
[*pos
];
98 tbl
->opts
.decimal
= p
[*pos
];
105 mandoc_msg(MANDOCERR_TBLOPT_NOARG
,
106 tbl
->parse
, ln
, *pos
, keys
[key
].name
);
107 else if (want
&& len
!= want
)
108 mandoc_vmsg(MANDOCERR_TBLOPT_ARGSZ
,
109 tbl
->parse
, ln
, *pos
, "%s want %d have %d",
110 keys
[key
].name
, want
, len
);
118 * Parse one line of options up to the semicolon.
119 * Each option can be preceded by blanks and/or commas,
120 * and some options are followed by arguments.
123 tbl_option(struct tbl_node
*tbl
, int ln
, const char *p
, int *offs
)
129 while (p
[pos
] == ' ' || p
[pos
] == '\t' || p
[pos
] == ',')
137 /* Parse one option name. */
140 while (isalpha((unsigned char)p
[pos
+ len
]))
144 mandoc_vmsg(MANDOCERR_TBLOPT_ALPHA
,
145 tbl
->parse
, ln
, pos
, "%c", p
[pos
]);
150 /* Look up the option name. */
153 while (i
< KEY_MAXKEYS
&&
154 (strncasecmp(p
+ pos
, keys
[i
].name
, len
) ||
155 keys
[i
].name
[len
] != '\0'))
158 if (i
== KEY_MAXKEYS
) {
159 mandoc_vmsg(MANDOCERR_TBLOPT_BAD
, tbl
->parse
,
160 ln
, pos
, "%.*s", len
, p
+ pos
);
165 /* Handle the option. */
169 tbl
->opts
.opts
|= keys
[i
].key
;
171 arg(tbl
, ln
, p
, &pos
, i
);