annotate tok.c @ 33:fa93d2ff9c62

Prevent potential division by zero Add safeguard ensuring that there is at least one possible character to generate passwords from in order to exclude the possibility of a division by zero error in rand_uniform.
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Tue, 30 Jul 2019 20:38:08 +0200
parents 722a45b4028b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1 /*
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
2 * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name>
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
3 *
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
4 * Permission is hereby granted, free of charge, to any person obtaining
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
5 * a copy of this software and associated documentation files (the
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
6 * "Software"), to deal in the Software without restriction, including
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
7 * without limitation the rights to use, copy, modify, merge, publish,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
8 * distribute, sublicense, and/or sell copies of the Software, and to
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
9 * permit persons to whom the Software is furnished to do so, subject to
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
10 * the following conditions:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
11 *
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
12 * The above copyright notice and this permission notice shall be included
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
13 * in all copies or substantial portions of the Software.
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
14 *
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
22 */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
23
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
24 #include "compat.h"
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
25
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
26 #include <ctype.h>
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
27 #include <stdio.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
28 #include <stdlib.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
29 #include <string.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
30
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
31 #include "tok.h"
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
32 #include "util.h"
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
33
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
34 enum tok_states {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
35 STATE_INITIAL,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
36 STATE_IN_WORD,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
37 STATE_IN_QUOTE,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
38 STATE_IN_WORD_ESCAPE,
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
39 STATE_IN_QUOTE_ESCAPE,
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
40 STATE_IN_MACRO
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
41 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
42
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
43 static inline void
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
44 strbuf_appendc(char **bufp, size_t *buf_sizep, int c)
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
45 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
46 char *buf = *bufp;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
47 size_t buf_size = *buf_sizep;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
48 size_t len;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
49
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
50 len = ((buf != NULL) && (c >= 0)) ? strlen(buf) : 0;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
51
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
52 /* allocate buffer if *bufp is NULL and *buf_sizep is 0 */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
53 if (buf_size < len + (c >= 0) + 1) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
54 buf_size = (buf_size * 2 > BUFSIZ) ? buf_size * 2 : BUFSIZ;
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
55 buf = xrealloc(buf, buf_size);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
56 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
57
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
58 /* append character to string buffer or reset buffer if c is -1 */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
59 if (c >= 0) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
60 buf[len++] = c;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
61 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
62 buf[len] = '\0';
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
63
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
64 *bufp = buf;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
65 *buf_sizep = buf_size;
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
66 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
67
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
68 void
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
69 tok_free(union tok **tokenv)
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
70 {
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
71 size_t i;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
72
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
73 if (tokenv == NULL) {
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
74 return;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
75 }
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
76
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
77 for (i = 0; tokenv[i] != NULL; i++) {
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
78 switch (tokenv[i]->any.type) {
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
79 case TOK_MACRO:
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
80 free(tokenv[i]->macro.name);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
81 break;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
82 case TOK_ARG:
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
83 free(tokenv[i]->arg.value);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
84 break;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
85 }
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
86 free(tokenv[i]);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
87 }
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
88 free(tokenv);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
89 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
90
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
91 enum tok_err
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
92 tok_tokenize(const char *s, size_t *tokencp, union tok ***tokenvp)
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
93 {
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
94 int retval = TOK_ERR_OK;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
95 union tok **tokenv;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
96 size_t tokenc = 0;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
97 const char *p = s;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
98 enum tok_states state = STATE_INITIAL;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
99 char quote;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
100 char *buf = NULL;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
101 size_t buf_size = 0;
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
102 char *value;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
103 char *name;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
104
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
105 /* allocate maximum number of tokens: ceil(length / 2) */
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
106 tokenv = xmalloc((((strlen(s) + 2 - 1) / 2) + 1) *
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
107 sizeof (union tok *));
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
108
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
109 for (;;) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
110 switch (state) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
111 case STATE_INITIAL:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
112 switch (*p) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
113 case ' ': /* FALLTHROUGH */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
114 case '\t': /* FALLTHROUGH */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
115 case '\n':
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
116 /* skip initial whitespace */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
117 break;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
118 case '"': /* FALLTHROUGH */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
119 case '\'':
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
120 /* start quoted part of token */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
121 state = STATE_IN_QUOTE;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
122 quote = *p;
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
123 strbuf_appendc(&buf, &buf_size, -1);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
124 break;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
125 case '\\':
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
126 /* start token with a backslash escape */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
127 state = STATE_IN_WORD_ESCAPE;
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
128 strbuf_appendc(&buf, &buf_size, -1);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
129 break;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
130 case '$':
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
131 /* start macro token */
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
132 state = STATE_IN_MACRO;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
133 strbuf_appendc(&buf, &buf_size, -1);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
134 break;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
135 case '\0':
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
136 /* end of input */
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
137 retval = TOK_ERR_OK;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
138 goto out;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
139 default:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
140 /* start token with a word */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
141 state = STATE_IN_WORD;
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
142 strbuf_appendc(&buf, &buf_size, -1);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
143 strbuf_appendc(&buf, &buf_size, *p);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
144 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
145 break;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
146 case STATE_IN_WORD:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
147 switch (*p) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
148 case ' ': /* FALLTHROUGH */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
149 case '\t': /* FALLTHROUGH */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
150 case '\n': /* FALLTHROUGH */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
151 case '\0':
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
152 /* end of token */
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
153 value = xstrdup(buf);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
154 tokenv[tokenc] = xmalloc(sizeof (union tok));
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
155 tokenv[tokenc]->arg.type = TOK_ARG;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
156 tokenv[tokenc]->arg.value = value;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
157 tokenc++;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
158
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
159 if (*p == '\0') {
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
160 retval = TOK_ERR_OK;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
161 goto out;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
162 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
163 state = STATE_INITIAL;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
164 break;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
165 case '"': /* FALLTHROUGH */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
166 case '\'':
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
167 /* start quoted part of token */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
168 state = STATE_IN_QUOTE;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
169 quote = *p;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
170 break;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
171 case '\\':
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
172 /* start backslash escape */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
173 state = STATE_IN_WORD_ESCAPE;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
174 break;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
175 default:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
176 /* regular character */
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
177 strbuf_appendc(&buf, &buf_size, *p);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
178 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
179 break;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
180 case STATE_IN_QUOTE:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
181 switch (*p) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
182 case '"': /* FALLTHROUGH */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
183 case '\'':
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
184 if (*p == quote) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
185 /* end quoted part of token */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
186 state = STATE_IN_WORD;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
187 } else {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
188 /* quote quote character */
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
189 strbuf_appendc(&buf, &buf_size, *p);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
190 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
191 break;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
192 case '\\':
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
193 /* start quoted backslash escape */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
194 state = STATE_IN_QUOTE_ESCAPE;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
195 break;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
196 case '\0':
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
197 /* unclosed quote */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
198 retval = TOK_ERR_UNTERMINATED_QUOTE;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
199 goto out;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
200 default:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
201 /* regular character */
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
202 strbuf_appendc(&buf, &buf_size, *p);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
203 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
204 break;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
205 case STATE_IN_WORD_ESCAPE: /* FALLTHROUGH */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
206 case STATE_IN_QUOTE_ESCAPE:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
207 if (*p == '\0') {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
208 /* trailing backslash */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
209 retval = TOK_ERR_TRAILING_BACKSLASH;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
210 goto out;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
211 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
212 /* escaped character */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
213 state = (state == STATE_IN_WORD_ESCAPE) ?
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
214 STATE_IN_WORD : STATE_IN_QUOTE;
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
215 strbuf_appendc(&buf, &buf_size, *p);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
216 break;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
217 case STATE_IN_MACRO:
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
218 switch (*p) {
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
219 case ' ': /* FALLTHROUGH */
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
220 case '\t': /* FALLTHROUGH */
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
221 case '\n': /* FALLTHROUGH */
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
222 case '\0':
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
223 /* end of token */
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
224 name = xstrdup(buf);
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
225 tokenv[tokenc] = xmalloc(sizeof (union tok));
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
226 tokenv[tokenc]->macro.type = TOK_MACRO;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
227 tokenv[tokenc]->macro.name = name;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
228 tokenc++;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
229
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
230 if (*p == '\0') {
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
231 retval = TOK_ERR_OK;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
232 goto out;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
233 }
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
234 state = STATE_INITIAL;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
235 break;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
236 default:
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
237 /*
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
238 * macro names must only contain alphanumeric
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
239 * characters and underscores
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
240 */
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
241 if (!isascii(*p) || (!isalnum(*p) &&
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
242 (*p != '_'))) {
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
243 retval = TOK_ERR_INVALID_MACRO_NAME;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
244 goto out;
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
245 }
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
246 strbuf_appendc(&buf, &buf_size, *p);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
247 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
248 break;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
249 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
250 p++;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
251 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
252
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
253 out:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
254 if (retval < 0) {
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
255 tok_free(tokenv);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
256 } else {
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
257 tokenv[tokenc] = NULL;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
258 *tokencp = tokenc;
27
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
259 *tokenvp = xrealloc(tokenv, (tokenc + 1) *
722a45b4028b Add define command for defining macros
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 0
diff changeset
260 sizeof (union tok *));
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
261 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
262 free(buf);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
263
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
264 return (retval);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
265 }