comparison sidinfo.c @ 78:9fb70b7b34ff

Clean up error handling for field parsing.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 01 Jan 2016 06:14:00 +0200
parents 92a4065c41d0
children 5709d0beb394
comparison
equal deleted inserted replaced
77:d14c82880141 78:9fb70b7b34ff
153 "can be used: -F \"hash=@hash@\\ncopy=@copyright@\\n\"\n" 153 "can be used: -F \"hash=@hash@\\ncopy=@copyright@\\n\"\n"
154 , th_prog_name); 154 , th_prog_name);
155 } 155 }
156 156
157 157
158 int argMatchPSField(const char *field, const size_t len) 158 int argMatchPSField(const char *field)
159 { 159 {
160 int index, found = -1; 160 int index, found = -1;
161 for (index = 0; index < noptPSFlags; index++) 161 for (index = 0; index < noptPSFlags; index++)
162 { 162 {
163 const PSFOption *opt = &optPSFlags[index]; 163 const PSFOption *opt = &optPSFlags[index];
164 if (th_strncasecmp(opt->name, field, len) == 0) 164 if (th_strcasecmp(opt->name, field) == 0)
165 { 165 {
166 if (found >= 0) 166 if (found >= 0)
167 return -2; 167 return -2;
168 found = index; 168 found = index;
169 } 169 }
171 171
172 return found; 172 return found;
173 } 173 }
174 174
175 175
176 int argMatchPSFieldError(const char *field)
177 {
178 int found = argMatchPSField(field);
179 switch (found)
180 {
181 case -1:
182 THERR("No such field '%s'.\n", field);
183 break;
184
185 case -2:
186 THERR("Field '%s' is ambiguous.\n", field);
187 break;
188 }
189 return found;
190 }
191
192
176 BOOL argParsePSField(char *opt, char *end, uint32_t *fields) 193 BOOL argParsePSField(char *opt, char *end, uint32_t *fields)
177 { 194 {
178 // Trim whitespace 195 // Trim whitespace
179 if (end != NULL) 196 if (end != NULL)
180 while (end > opt && *end && th_isspace(*end)) end--; 197 while (end > opt && *end && th_isspace(*end)) end--;
181 198
182 while (*opt && th_isspace(*opt)) opt++; 199 while (*opt && th_isspace(*opt)) opt++;
183 200
184 // Match field name 201 // Match field name
185 int found = argMatchPSField(opt, (end != NULL) ? end - opt : strlen(opt)); 202 char *field = (end != NULL) ? th_strndup(opt, end - opt) : th_strdup(opt);
186 switch (found) 203 int found = argMatchPSFieldError(field);
187 { 204 if (found >= 0)
188 case -1: 205 *fields |= optPSFlags[found].flag;
189 THERR("No such field '%s'.\n", opt); 206
190 return FALSE; 207 th_free(field);
191 208 return FALSE;
192 case -2:
193 THERR("Field '%s' is ambiguous.\n", opt);
194 return FALSE;
195
196 default:
197 *fields |= optPSFlags[found].flag;
198 return TRUE;
199 }
200 } 209 }
201 210
202 211
203 BOOL siStackAddItem(PSFStack *stack, const PSFStackItem *item) 212 BOOL siStackAddItem(PSFStack *stack, const PSFStackItem *item)
204 { 213 {
273 if (!siStackAddItem(&optFormat, &item)) 282 if (!siStackAddItem(&optFormat, &item))
274 return FALSE; 283 return FALSE;
275 } 284 }
276 else 285 else
277 { 286 {
278 int ret = argMatchPSField(start, fmt - start); 287 char *field = th_strndup(start, fmt - start);
279 if (ret >= 0) 288 int ret = argMatchPSFieldError(field);
289 if (found >= 0)
280 { 290 {
281 item.cmd = ret; 291 item.cmd = ret;
282 item.str = NULL; 292 item.str = NULL;
283 if (!siStackAddItem(&optFormat, &item)) 293 if (!siStackAddItem(&optFormat, &item))
294 {
295 th_free(field);
284 return FALSE; 296 return FALSE;
297 }
285 } 298 }
286 else 299 th_free(field);
287 {
288 THERR("Foobar %d\n", ret);
289 return FALSE;
290 }
291 } 300 }
292 mode = 0; 301 mode = 0;
293 } 302 }
294 else 303 else
295 if (*fmt == 0) 304 if (*fmt == 0)