error: Track locations in configuration files

New LOC_FILE.  Use it for tracking file name and line number in
qemu_config_parse().  We now report errors like

    qemu:foo.conf:42: Did not find I2C bus for smbus-eeprom

In particular, gems like this message:

    -device: no driver specified

become almost nice now:

    qemu:foo.conf:44: -device: no driver specified

(A later commit will get rid of the bogus -device:)
This commit is contained in:
Markus Armbruster 2010-02-18 19:48:33 +01:00
parent 65abca0a34
commit cf5a65aaaf
5 changed files with 49 additions and 18 deletions

View File

@ -425,13 +425,17 @@ void qemu_config_write(FILE *fp)
} }
} }
int qemu_config_parse(FILE *fp) int qemu_config_parse(FILE *fp, const char *fname)
{ {
char line[1024], group[64], id[64], arg[64], value[1024]; char line[1024], group[64], id[64], arg[64], value[1024];
Location loc;
QemuOptsList *list = NULL; QemuOptsList *list = NULL;
QemuOpts *opts = NULL; QemuOpts *opts = NULL;
int res = -1, lno = 0;
loc_push_none(&loc);
while (fgets(line, sizeof(line), fp) != NULL) { while (fgets(line, sizeof(line), fp) != NULL) {
loc_set_file(fname, ++lno);
if (line[0] == '\n') { if (line[0] == '\n') {
/* skip empty lines */ /* skip empty lines */
continue; continue;
@ -444,7 +448,7 @@ int qemu_config_parse(FILE *fp)
/* group with id */ /* group with id */
list = find_list(group); list = find_list(group);
if (list == NULL) if (list == NULL)
return -1; goto out;
opts = qemu_opts_create(list, id, 1); opts = qemu_opts_create(list, id, 1);
continue; continue;
} }
@ -452,25 +456,27 @@ int qemu_config_parse(FILE *fp)
/* group without id */ /* group without id */
list = find_list(group); list = find_list(group);
if (list == NULL) if (list == NULL)
return -1; goto out;
opts = qemu_opts_create(list, NULL, 0); opts = qemu_opts_create(list, NULL, 0);
continue; continue;
} }
if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) { if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
/* arg = value */ /* arg = value */
if (opts == NULL) { if (opts == NULL) {
fprintf(stderr, "no group defined\n"); error_report("no group defined");
return -1; goto out;
} }
if (qemu_opt_set(opts, arg, value) != 0) { if (qemu_opt_set(opts, arg, value) != 0) {
fprintf(stderr, "failed to set \"%s\" for %s\n", error_report("failed to set \"%s\" for %s", arg, group);
arg, group); goto out;
return -1;
} }
continue; continue;
} }
fprintf(stderr, "parse error: %s\n", line); error_report("parse error");
return -1; goto out;
} }
return 0; res = 0;
out:
loc_pop(&loc);
return res;
} }

View File

@ -16,6 +16,6 @@ int qemu_global_option(const char *str);
void qemu_add_globals(void); void qemu_add_globals(void);
void qemu_config_write(FILE *fp); void qemu_config_write(FILE *fp);
int qemu_config_parse(FILE *fp); int qemu_config_parse(FILE *fp, const char *fname);
#endif /* QEMU_CONFIG_H */ #endif /* QEMU_CONFIG_H */

View File

@ -113,6 +113,19 @@ void loc_set_none(void)
cur_loc->kind = LOC_NONE; cur_loc->kind = LOC_NONE;
} }
/*
* Change the current location to file FNAME, line LNO.
*/
void loc_set_file(const char *fname, int lno)
{
assert (fname || cur_loc->kind == LOC_FILE);
cur_loc->kind = LOC_FILE;
cur_loc->num = lno;
if (fname) {
cur_loc->ptr = fname;
}
}
static const char *progname; static const char *progname;
/* /*
@ -136,6 +149,13 @@ void error_print_loc(void)
sep = " "; sep = " ";
} }
switch (cur_loc->kind) { switch (cur_loc->kind) {
case LOC_FILE:
error_printf("%s:", (const char *)cur_loc->ptr);
if (cur_loc->num) {
error_printf("%d:", cur_loc->num);
}
error_printf(" ");
break;
default: default:
error_printf(sep); error_printf(sep);
} }

View File

@ -15,7 +15,7 @@
typedef struct Location { typedef struct Location {
/* all members are private to qemu-error.c */ /* all members are private to qemu-error.c */
enum { LOC_NONE } kind; enum { LOC_NONE, LOC_FILE } kind;
int num; int num;
const void *ptr; const void *ptr;
struct Location *prev; struct Location *prev;
@ -27,6 +27,7 @@ Location *loc_pop(Location *loc);
Location *loc_save(Location *loc); Location *loc_save(Location *loc);
void loc_restore(Location *loc); void loc_restore(Location *loc);
void loc_set_none(void); void loc_set_none(void);
void loc_set_file(const char *fname, int lno);
void error_vprintf(const char *fmt, va_list ap); void error_vprintf(const char *fmt, va_list ap);
void error_printf(const char *fmt, ...) __attribute__ ((format(printf, 1, 2))); void error_printf(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));

14
vl.c
View File

@ -4941,18 +4941,22 @@ int main(int argc, char **argv, char **envp)
} }
if (defconfig) { if (defconfig) {
const char *fname;
FILE *fp; FILE *fp;
fp = fopen(CONFIG_QEMU_CONFDIR "/qemu.conf", "r");
fname = CONFIG_QEMU_CONFDIR "/qemu.conf";
fp = fopen(fname, "r");
if (fp) { if (fp) {
if (qemu_config_parse(fp) != 0) { if (qemu_config_parse(fp, fname) != 0) {
exit(1); exit(1);
} }
fclose(fp); fclose(fp);
} }
fp = fopen(CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", "r"); fname = CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf";
fp = fopen(fname, "r");
if (fp) { if (fp) {
if (qemu_config_parse(fp) != 0) { if (qemu_config_parse(fp, fname) != 0) {
exit(1); exit(1);
} }
fclose(fp); fclose(fp);
@ -5633,7 +5637,7 @@ int main(int argc, char **argv, char **envp)
fprintf(stderr, "open %s: %s\n", optarg, strerror(errno)); fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
exit(1); exit(1);
} }
if (qemu_config_parse(fp) != 0) { if (qemu_config_parse(fp, optarg) != 0) {
exit(1); exit(1);
} }
fclose(fp); fclose(fp);