From fff0ca295b7d63c3f4c49bb3e6f8932458216680 Mon Sep 17 00:00:00 2001 From: Laurent Vivier Date: Wed, 16 Apr 2008 18:32:17 +0000 Subject: [PATCH] use libconfig --- tools/Makefile | 11 +- tools/emile-install.c | 252 ++++++++++++++++------------- tools/emile.c | 301 ++++++++-------------------------- tools/emile_config.c | 363 ------------------------------------------ tools/emile_config.h | 41 ----- 5 files changed, 213 insertions(+), 755 deletions(-) delete mode 100644 tools/emile_config.c delete mode 100644 tools/emile_config.h diff --git a/tools/Makefile b/tools/Makefile index 6d72cb0..ff7561e 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -15,9 +15,9 @@ SOURCES = emile-set-cmdline.c Makefile emile-first-tune.c \ emile-install.c emile-set-output.c emile.c \ emile_scanbus.c emile-map-set.c iso9660_ls.c \ iso9660_cat.c minigzip.c read_vmlinuz.c device.c gzio.c \ - emile-mkisofs.c emile_config.c ext2_ls.c ext2_cat.c + emile-mkisofs.c ext2_ls.c ext2_cat.c -HEADERS = device.h emile_config.h +HEADERS = device.h DISTFILES =$(HEADERS) $(SOURCES) Makefile @@ -27,14 +27,13 @@ CPPFLAGS = $(CROSS_COMPILE_CPPFLAGS) -DSIGNATURE="\"$(SIGNATURE)\"" -DPREFIX=\"$ -I../libconfig -I../libmap -I../libext2 CFLAGS = -Wall -Werror -g -LDLIBS = $(CROSS_COMPILE_LDFLAGS) -L../libemile -lemile -L../libiso9660/native -liso9660 -L../libgzip/native -lgzip -L../libconfig/native -lconfig -L../libmap/native -lmap \ --L../libext2/native -lext2 +LDLIBS = $(CROSS_COMPILE_LDFLAGS) -L../libemile -lemile -L../libiso9660/native -liso9660 -L../libgzip/native -lgzip -L../libconfig/native -lconfig -L../libmap/native -lmap -L../libext2/native -lext2 all: $(PROGRAMS) -emile-install: emile-install.o emile_config.o +emile-install: emile-install.o -emile: emile.o emile_scanbus.o emile_config.o device.o +emile: emile.o emile_scanbus.o device.o emile-map-set: emile-map-set.o emile_scanbus.o device.o diff --git a/tools/emile-install.c b/tools/emile-install.c index ba7eb81..23496db 100644 --- a/tools/emile-install.c +++ b/tools/emile-install.c @@ -1,6 +1,6 @@ /* * - * (c) 2004,2005 Laurent Vivier + * (c) 2004-2008 Laurent Vivier * */ @@ -15,7 +15,6 @@ #include "libemile.h" #include "libconfig.h" -#include "emile_config.h" enum { ARG_NONE = 0, @@ -71,7 +70,6 @@ static int get_info(char *image, int verbose) int index; char *known_properties[] = { "kernel", - "parameters", "initrd", "chainloader", NULL @@ -189,159 +187,182 @@ static int get_info(char *image, int verbose) static int set_config(char *image, int verbose, char *config_path, char *first_level, char *second_level) { + char property[1024]; + char property2[1024]; char *ramdisk_ondisk, *kernel_ondisk; - char* kernel_image = NULL; - char* ramdisk = NULL; - char* chainloader = NULL; - emile_config* config; int8_t *configuration; - int timeout; - int gestaltid; - int default_entry; - char *title, *args; - char *output; + int8_t *conffile; char buf[64]; int fd; + struct stat st; + int i; + int current; + int res; + static char *prolog[] = { + "gestaltID", + "default", + "timeout", + "vga", + "modem", + "printer" + }; + static char *known_properties[] ={ + "kernel", + "initrd", + "chainloader" + }; - if (kernel_image || ramdisk) - { - fprintf(stderr, - "ERROR: don't use --kernel or --ramdisk with --config\n"); - return 4; - } - config = emile_config_open(config_path); - if (config == NULL) + /* open configuration file */ + + fd = open(config_path, O_RDONLY); + if (fd == -1) { fprintf(stderr, "ERROR: cannot open %s\n", config_path); return 5; } + if (fstat(fd, &st) == -1) + { + fprintf(stderr, "ERROR: cannot fstat %s\n", config_path); + return 5; + } + conffile = (int8_t*)malloc(st.st_size); + if (conffile == NULL) + { + fprintf(stderr, "ERROR: cannot malloc() %s\n", config_path); + return 5; + } - if ((first_level == NULL) && - (emile_config_get(config, CONFIG_FIRST_LEVEL, &first_level))) - first_level = PREFIX "/lib/emile/first_floppy"; + if (read(fd, conffile, st.st_size) != st.st_size) + { + fprintf(stderr, "ERROR: cannot read() %s\n", config_path); + return 5; + } + close(fd); - if ((second_level == NULL) && - emile_config_get(config, CONFIG_SECOND_LEVEL, &second_level)) - second_level = PREFIX "/lib/emile/second_floppy"; + /* extract properties */ + + if (first_level == NULL) { + if (config_get_property(conffile, + "first_level", property) == -1) + first_level = PREFIX "/lib/emile/first_floppy"; + else + first_level = property; + } + + if (second_level == NULL) { + if (config_get_property(conffile, + "second_level", property2) == -1) + second_level = PREFIX "/lib/emile/second_floppy"; + else + second_level = property2; + } + + /* create floppy, set first and second level position */ fd = emile_floppy_create(image, first_level, second_level); if (fd < 0) { fprintf(stderr, "ERROR: cannot create %s\n", image); + free(conffile); return 6; } + /* create configuration information of the floppy image */ + configuration = malloc(65536); if (configuration == NULL) { fprintf(stderr, "ERROR: cannot initalize configuration\n"); + free(conffile); return 7; } memset(configuration, 0, 65536); - if (!emile_config_get(config, CONFIG_GESTALTID, &gestaltid)) + /* copy prolog */ + + for (i = 0; i < sizeof(prolog) / sizeof(char*); i++) { - sprintf(buf, "%d", gestaltid); - config_set_property(configuration, "gestaltID", buf); + if (config_get_property(conffile, + prolog[i], property) != -1) + { + sprintf(buf, "%s", property); + config_set_property(configuration, prolog[i], buf); + } } - if (!emile_config_get(config, CONFIG_DEFAULT, &default_entry)) - { - sprintf(buf, "%d", default_entry); - config_set_property(configuration, "default", buf); - } - - if (!emile_config_get(config, CONFIG_TIMEOUT, &timeout)) - { - sprintf(buf, "%d", timeout); - config_set_property(configuration, "timeout", buf); - } - - if (!emile_config_get(config, CONFIG_VGA, &output)) - config_set_property(configuration, "vga", output); - - if (!emile_config_get(config, CONFIG_MODEM, &output)) - config_set_property(configuration, "modem", output); - - if (!emile_config_get(config, CONFIG_PRINTER, &output)) - config_set_property(configuration, "printer", output); + /* get kernel properties */ kernel_ondisk = NULL; ramdisk_ondisk = NULL; - emile_config_read_first_entry(config); - do { - if (!emile_config_get(config, CONFIG_TITLE, &title)) - config_add_property(configuration, "title", title); + current = 0; + while(1) + { + res = config_get_property(conffile + current, + "title", property); + if (res == -1) + break; + + config_add_property(configuration, "title", property); + if (verbose) - printf("title %s\n", title); - if (!emile_config_get(config, CONFIG_CHAINLOADER, &chainloader)) + printf("title %s\n", property); + + current += res; + current = config_get_next_property(conffile, current, + NULL, NULL); + + for (i = 0; i < sizeof(known_properties) / sizeof(char*); i++) { - if (emile_is_url(chainloader)) + res = config_get_indexed_property(conffile, + "title", + property, + known_properties[i], + property2); + if (res == -1) + continue; + + if (emile_is_url(property2)) { if (verbose) - printf(" chainloader %s\n", chainloader); + printf(" %s %s\n", + known_properties[i], property2); + config_set_indexed_property(configuration, - "title", title, - "chainloader", chainloader); - } - continue; - } - if (!emile_config_get(config, CONFIG_KERNEL, &kernel_image)) - { - if (emile_is_url(kernel_image)) - { - if (verbose) - printf(" kernel %s\n", kernel_image); - config_set_indexed_property(configuration, - "title", title, - "kernel", kernel_image); + "title", property, + known_properties[i], + property2); } else { - if (kernel_ondisk == NULL) - kernel_ondisk = emile_floppy_add(fd, - kernel_image); + char *url; + if (strcmp(known_properties[i], "kernel") == 0) + { + if (kernel_ondisk == NULL) + kernel_ondisk = + emile_floppy_add(fd, + property2); + url = kernel_ondisk; + } else + if (strcmp(known_properties[i], "initrd") == 0) + { + if (ramdisk_ondisk == NULL) + ramdisk_ondisk = + emile_floppy_add(fd, + property2); + url = ramdisk_ondisk; + } else + url = property2; + config_set_indexed_property(configuration, - "title", title, - "kernel", kernel_ondisk); + "title", property, + known_properties[i], url); if (verbose) - printf(" kernel %s (%s)\n", - kernel_image, kernel_ondisk); + printf(" %s %s (%s)\n", + known_properties[i], + property2, url); } } - if (!emile_config_get(config, CONFIG_INITRD, &ramdisk)) - { - if (emile_is_url(ramdisk)) - { - if (verbose) - printf(" initrd %s\n", ramdisk); - config_set_indexed_property(configuration, - "title", title, - "initrd", ramdisk); - } - else - { - if (ramdisk_ondisk == NULL) - ramdisk_ondisk = emile_floppy_add(fd, - ramdisk); - config_set_indexed_property(configuration, - "title", title, - "initrd", ramdisk_ondisk); - if (verbose) - printf(" initrd %s (%s)\n", - ramdisk, ramdisk_ondisk); - } - } - if (!emile_config_get(config, CONFIG_ARGS, &args)) - { - config_set_indexed_property(configuration, - "title", title, - "parameters", args); - if (verbose) - printf(" parameters %s\n", args); - } - } while (!emile_config_read_next(config)); - emile_config_close(config); + } if (ramdisk_ondisk != NULL) free(ramdisk_ondisk); if (kernel_ondisk != NULL) @@ -377,6 +398,7 @@ static int set_config(char *image, int verbose, char *config_path, emile_second_set_configuration(fd, configuration); emile_floppy_close(fd); + free(conffile); free(configuration); return 0; @@ -446,8 +468,16 @@ int main(int argc, char** argv) return get_info(image, verbose); if (config_path != NULL) + { + if (kernel_image || ramdisk) + { + fprintf(stderr, "ERROR: don't use --kernel" + " or --ramdisk with --config\n"); + return 4; + } return set_config(image, verbose, config_path, first_level, second_level); + } if (first_level == NULL) first_level = PREFIX "/lib/emile/first_floppy"; diff --git a/tools/emile.c b/tools/emile.c index a48d8c2..2cc5326 100644 --- a/tools/emile.c +++ b/tools/emile.c @@ -19,7 +19,6 @@ #include "libemile.h" #include "libmap.h" -#include "emile_config.h" #include "device.h" int verbose = 0; @@ -417,24 +416,12 @@ static int add_file(int8_t *configuration, return 0; } -static int8_t *set_config(emile_config *config, int drive) +static int8_t *set_config(char *config_path) { - int default_entry; - int gestaltid; - int timeout; - char *kernel_path; - char *initrd_path; - char *kernel_map_path; - char *initrd_map_path; - char *output; - char *append_string; - char *title; - char buf[16]; - int ret; int8_t *configuration; - char *chainloader_path; + int ret; - configuration = malloc(65536); + configuration = (int8_t*)malloc(65536); if (configuration == NULL) { fprintf(stderr, @@ -443,218 +430,33 @@ static int8_t *set_config(emile_config *config, int drive) } configuration[0] = 0; - - if (!emile_config_get(config, CONFIG_GESTALTID, &gestaltid)) - { - sprintf(buf, "%d", gestaltid); - config_set_property(configuration, "gestaltID", buf); - } - - if (!emile_config_get(config, CONFIG_DEFAULT, &default_entry)) - { - sprintf(buf, "%d", default_entry); - config_set_property(configuration, "default", buf); - } - - if (!emile_config_get(config, CONFIG_TIMEOUT, &timeout)) - { - sprintf(buf, "%d", timeout); - config_set_property(configuration, "timeout", buf); - } - - if (!emile_config_get(config, CONFIG_VGA, &output)) - config_set_property(configuration, "vga", output); - - if (!emile_config_get(config, CONFIG_MODEM, &output)) - config_set_property(configuration, "modem", output); - - if (!emile_config_get(config, CONFIG_PRINTER, &output)) - config_set_property(configuration, "printer", output); - - emile_config_read_first_entry(config); - - do { - if (!emile_config_get(config, CONFIG_TITLE, &title)) - config_add_property(configuration, "title", title); - if (verbose) - printf("title %s\n", title); - - if (!emile_config_get(config, - CONFIG_CHAINLOADER, - &chainloader_path)) - { - if (emile_is_url(chainloader_path)) - { - config_set_indexed_property(configuration, - "title", title, - "chainloader", - chainloader_path); - } - else - { - int fd; - unsigned short unit_id; - struct emile_container *container; - struct stat st; - char *chainloader; - - fd = open(chainloader_path, O_RDONLY); - if (fd == -1) - { - fprintf(stderr, - "ERROR: cannot open %s\n", - chainloader_path); - return NULL; - } - fstat(fd, &st); - - container = malloc( - sizeof(struct emile_container) + - sizeof(struct emile_block)); - if (container == NULL) - { - fprintf(stderr, - "ERROR: cannot malloc container" - "\n"); - close(fd); - return NULL; - } - ret = emile_scsi_create_container(fd, - &unit_id, - container, - 2); - close(fd); - if (ret == -1) - { - fprintf(stderr, - "ERROR: cannot create container" - "\n"); - free(container); - return NULL; - } - chainloader = malloc(32); - if (chainloader == NULL) - { - fprintf(stderr, - "ERROR: cannot malloc chainloader\n"); - free(container); - return NULL; - } - sprintf(chainloader, - "block:(sd%d)0x%x,0x%lx", unit_id, - container->blocks[0].offset, - st.st_size); - free(container); - config_set_indexed_property(configuration, - "title", title, - "chainloader", chainloader); - free(chainloader); - } - } - else if (!emile_config_get(config, CONFIG_KERNEL, &kernel_path)) - { - ret = emile_config_get(config, CONFIG_KERNEL_MAP, - &kernel_map_path); - - ret = add_file(configuration, title, - "kernel", kernel_path, - ret == -1 ? NULL : kernel_map_path); - if (ret == -1) - { - fprintf(stderr, - "ERROR: cannot add kernel %s\n", - kernel_path); - free(configuration); - return NULL; - } - } - else - fprintf(stderr, - "WARNING: missing kernel entry for %s\n", title); - - if (!emile_config_get(config, CONFIG_INITRD, &initrd_path)) - { - ret = emile_config_get(config, CONFIG_INITRD_MAP, &initrd_map_path); - - ret = add_file(configuration, title, "initrd", initrd_path, - ret == -1 ? NULL : initrd_map_path); - if (ret == -1) - { - free(configuration); - fprintf(stderr, - "ERROR: cannot add initrd %s\n", - initrd_path); - fprintf(stderr, - "ERROR: missing kernel entry for %s\n", title); - return NULL; - } - } - - if (!emile_config_get(config, CONFIG_ARGS, &append_string)) - { - config_set_indexed_property(configuration, - "title", title, - "parameters", append_string); - if (verbose) - printf(" parameters %s\n", append_string); - } - } while (!emile_config_read_next(config)); - - if (strlen((char*)configuration) > 1023) + ret = add_file(configuration, NULL, "configuration", config_path, NULL); + if (ret == -1) { - int fd; - char* bootconfig = "/boot/emile/.bootconfig"; - - /* do not fit in second paramstring */ - - fd = creat(bootconfig, S_IWUSR); - if (fd == -1) - { - free(configuration); - fprintf(stderr, - "ERROR: cannot create /boot/emile/.bootconfig\n"); - return NULL; - - } - - write(fd, configuration, strlen((char*)configuration) + 1); - close(fd); free(configuration); - - configuration = malloc(1024); - if (configuration == NULL) - { - fprintf(stderr, - "ERROR: cannot allocate memory for configuration\n"); - return NULL; - } - ret = add_file(configuration, NULL, - "configuration", bootconfig, NULL); - if (ret == -1) - { - free(configuration); - fprintf(stderr, - "ERROR: cannot add %s to configuration\n", bootconfig); - return NULL; - } + fprintf(stderr, + "ERROR: cannot set configuration in %s\n", config_path); + return NULL; } return configuration; } int main(int argc, char **argv) { + char property[1024]; char *backup_path = PREFIX "/boot/emile/bootblock.backup"; char *config_path = PREFIX "/boot/emile/emile.conf"; char *first_path; char *second_path; - char *partition; int ret; int c; + char *partition; int option_index = 0; - emile_config *config; int drive, second, size; int fd; int8_t *configuration; + int8_t *config; + struct stat st; while(1) { @@ -713,23 +515,46 @@ int main(int argc, char **argv) /* read config file */ - config = emile_config_open(config_path); - if (config == NULL) + fd = open(config_path, O_RDONLY); + if (fd == -1) { - fprintf(stderr, "ERROR: cannot open config file %s\n", config_path); + fprintf(stderr, + "ERROR: cannot open config file %s\n", config_path); return 2; } + if (fstat(fd, &st) == -1) + { + fprintf(stderr, + "ERROR: cannot fstat file %s\n", config_path); + return 2; + } + config = (int8_t*)malloc(st.st_size); + if (config == NULL) + { + fprintf(stderr, + "ERROR: cannot malloc %s\n", config_path); + return 2; + } + if (read(fd, config, st.st_size) != st.st_size) + { + fprintf(stderr, "ERROR: cannot read() %s\n", config_path); + return 5; + } + close(fd); - ret = emile_config_get(config, CONFIG_PARTITION, &partition); + /* get partition */ + + ret = config_get_property(config, "partition", property); if (ret == -1) { fprintf(stderr, "ERROR: you must specify in %s a partition to set\n" " EMILE bootblock\n", config_path); fprintf(stderr, " you can have the list of available partitions with \"--scanbus\".\n"); - emile_config_close(config); + free(config); return 3; } + partition = strdup(property); if (action & ACTION_RESTORE) { @@ -739,7 +564,7 @@ int main(int argc, char **argv) { fprintf(stderr, "ERROR: \"--restore\" cannot be used with other arguments\n"); - emile_config_close(config); + free(config); return 13; } @@ -749,7 +574,7 @@ int main(int argc, char **argv) fprintf(stderr, "ERROR: cannot restore bootblock %s from %s\n", partition, backup_path); - emile_config_close(config); + free(config); return 14; } printf("Bootblock restore successfully done.\n"); @@ -765,7 +590,7 @@ int main(int argc, char **argv) free(new_name); - emile_config_close(config); + free(config); return 0; } @@ -778,7 +603,7 @@ int main(int argc, char **argv) fprintf(stderr, " you should try as root\n"); if ((action & ACTION_TEST) == 0) { - emile_config_close(config); + free(config); return 4; } } @@ -792,7 +617,7 @@ int main(int argc, char **argv) " or wait a release of EMILE allowing you to add this driver\n"); if ((action & ACTION_TEST) == 0) { - emile_config_close(config); + free(config); return 5; } } @@ -805,7 +630,7 @@ int main(int argc, char **argv) fprintf(stderr, " you should try as root\n"); if ((action & ACTION_TEST) == 0) { - emile_config_close(config); + free(config); return 6; } } @@ -817,7 +642,7 @@ int main(int argc, char **argv) " you can change it to Apple_HFS using \"--set-hfs\" argument\n"); if ((action & ACTION_TEST) == 0) { - emile_config_close(config); + free(config); return 7; } } @@ -829,7 +654,7 @@ int main(int argc, char **argv) fprintf(stderr, " you should try as root\n"); if ((action & ACTION_TEST) == 0) { - emile_config_close(config); + free(config); return 8; } } @@ -842,7 +667,7 @@ int main(int argc, char **argv) " you must use \"--backup\" to save it\n"); if ((action & ACTION_TEST) == 0) { - emile_config_close(config); + free(config); return 9; } } @@ -853,7 +678,7 @@ int main(int argc, char **argv) { fprintf(stderr, "ERROR: \"--backup\" cannot be used with \"--test\"\n"); - emile_config_close(config); + free(config); return 13; } @@ -863,26 +688,28 @@ int main(int argc, char **argv) fprintf(stderr, "ERROR: cannot backup bootblock %s to %s\n", partition, backup_path); - emile_config_close(config); + free(config); return 14; } printf("Bootblock backup successfully done.\n"); } - ret = emile_config_get(config, CONFIG_FIRST_LEVEL, &first_path); + ret = config_get_property(config, "first_level", property); if (ret == -1) return 2; + first_path = strdup(property); - ret = emile_config_get(config, CONFIG_SECOND_LEVEL, &second_path); + ret = config_get_property(config, "second_level", property); if (ret == -1) return 2; + second_path = strdup(property); fd = open(first_path, O_RDONLY); if (fd == -1) { fprintf(stderr, "ERROR: cannot open \"%s\".\n", first_path); - emile_config_close(config); + free(config); return 20; } @@ -890,7 +717,7 @@ int main(int argc, char **argv) close(fd); - configuration = set_config(config, drive); + configuration = set_config(config_path); if (ret) return ret; @@ -912,10 +739,13 @@ int main(int argc, char **argv) fprintf(stderr, "ERROR: cannot set \"%s\" information into \"%s\".\n", second_path, first_path); - emile_config_close(config); + free(first_path); + free(second_path); + free(config); free(configuration); return 21; } + free(second_path); close(fd); @@ -929,10 +759,12 @@ int main(int argc, char **argv) first_path, partition); fprintf(stderr, " %s\n", strerror(errno)); - emile_config_close(config); + free(first_path); + free(config); free(configuration); return 22; } + free(first_path); /* set HFS if needed */ @@ -944,14 +776,15 @@ int main(int argc, char **argv) fprintf( stderr, "ERROR: cannot set partition type of \"%s\" to Apple_HFS.\n" , partition); - emile_config_close(config); + free(config); free(configuration); return 23; } } } - emile_config_close(config); + free(partition); + free(config); free(configuration); return 0; } diff --git a/tools/emile_config.c b/tools/emile_config.c deleted file mode 100644 index 6ffe1d6..0000000 --- a/tools/emile_config.c +++ /dev/null @@ -1,363 +0,0 @@ -/* - * - * (c) 2007 Laurent Vivier - * - */ - -#include -#include -#include -#include - -#include "emile_config.h" - -static int read_char(FILE* fd) -{ - int c; - - c = getc(fd); - - if (c == '#') - while ( (c = getc(fd)) != '\n'); - - return c; -} - -static void skip_blank(FILE* fd) -{ - int c; - - do { - c = read_char(fd); - } while ( (c == ' ') || (c == '\t') ); - - ungetc(c, fd); -} - -static void skip_blank_line(FILE* fd) -{ - int c; - - do { - skip_blank(fd); - c = read_char(fd); - } while (c == '\n'); - - ungetc(c, fd); -} - -static int read_word(FILE* fd, char* word) -{ - int index = 0; - int c; - - skip_blank(fd); - - c = read_char(fd); - if (c == -1) - return -1; - - if (c == '\n') - { - word[index++] = c; - word[index] = 0; - return index; - } - while ( (c != ' ') && (c != '\t') && (c != '\n') && (c != -1) ) - { - word[index++] = c; - c = read_char(fd); - } - ungetc(c, fd); - word[index] = 0; - return index; -} - -static int read_line(FILE* fd, char *name, char *value) -{ - int c; - int index = 0; - - skip_blank_line(fd); - - if (read_word(fd, name) == -1) - return -1; - - skip_blank(fd); - - c = read_char(fd); - while ( (c != '\n') && (c != -1) ) - { - value[index++] = c; - c = read_char(fd); - } - while ( (value[index-1] == ' ') || (value[index-1] == '\t') ) - index--; - value[index] = 0; - - return 0; -} - -static char* set_tag(char* string, int tag, int len, void* data) -{ - *string++ = tag; - *string++ = len + 1; - memcpy(string, data, len); - string += len; - - *string = CONFIG_END; /* mark end of string */ - - return string; -} - -static char* get_tag(char* string, int tag) -{ - while (*string != tag) - { - if (*string == CONFIG_END) - return NULL; - string++; - string += *string; - } - - return string + 2; -} - -static int read_header(FILE* fd, char* header, int size) -{ - int offset = 0; - char name[256]; - char value[1024]; - - rewind(fd); - - while (read_line(fd, name, value) != -1) - { - if (strcmp("gestaltid", name) == 0) - { - int v = atoi(value); - header = set_tag(header, CONFIG_GESTALTID, sizeof(int), &v); - } - else if (strcmp("vga", name) == 0) - { - header = set_tag(header, CONFIG_VGA, strlen(value) + 1, value); - } - else if (strcmp("modem", name) == 0) - { - header = set_tag(header, CONFIG_MODEM, strlen(value) + 1, value); - } - else if (strcmp("printer", name) == 0) - { - header = set_tag(header, CONFIG_PRINTER, strlen(value) + 1, value); - } - else if (strcmp("partition", name) == 0) - { - header = set_tag(header, CONFIG_PARTITION, strlen(value) + 1, value); - } - else if (strcmp("first_level", name) == 0) - { - header = set_tag(header, CONFIG_FIRST_LEVEL, strlen(value) + 1, value); - } - else if (strcmp("second_level", name) == 0) - { - header = set_tag(header, CONFIG_SECOND_LEVEL, strlen(value) + 1, value); - } - else if (strcmp("timeout", name) == 0) - { - int v = atoi(value); - header = set_tag(header, CONFIG_TIMEOUT, sizeof(int), &v); - } - else if (strcmp("default", name) == 0) - { - int v = atoi(value); - header = set_tag(header, CONFIG_DEFAULT, sizeof(int), &v); - } - else if (strcmp("title", name) == 0) - { - fseek(fd, offset, SEEK_SET); - break; - } - else - { - fprintf(stderr, "ERROR: syntax error on word %s\n", name); - return -1; - } - offset = ftell(fd); - } - return 0; -} - -emile_config* emile_config_open(char* name) -{ - int ret; - FILE* fd; - emile_config* config; - - fd = fopen(name, "r"); - if (fd == NULL) - return NULL; - - config = (emile_config*)malloc(sizeof(emile_config)); - if (config == NULL) - goto close_file; - - memset(config, 0, sizeof(emile_config)); - - config->fd = fd; - config->header_size = 1024; - config->header = (char*)malloc(config->header_size); - if (config->header == NULL) - goto free_config; - config->current_size = 1024; - config->current = (char*)malloc(config->current_size); - if (config->current == NULL) - goto free_header; - - ret = read_header(config->fd, config->header, config->header_size); - if (ret == -1) - goto free_current; - - return config; -free_current: - free(config->current); -free_header: - free(config->header); -free_config: - free(config); -close_file: - fclose(fd); - return NULL; -} - -void emile_config_close(emile_config* config) -{ - free(config->current); - free(config->header); - fclose(config->fd); - free(config); -} - -static int read_description(FILE* fd, char* desc, int size) -{ - int offset = 0; - char name[256]; - char value[1024]; - int found = 0; - - while (read_line(fd, name, value) != -1) - { - if (strcmp("title", name) == 0) - { - if (found) - { - fseek(fd, offset, SEEK_SET); - break; - } - found = 1; - desc = set_tag(desc, CONFIG_TITLE, strlen(value) + 1, value); - } - else if (strcmp("kernel", name) == 0) - { - desc = set_tag(desc, CONFIG_KERNEL, strlen(value) + 1, value); - } - else if (strcmp("kernel_map", name) == 0) - { - desc = set_tag(desc, CONFIG_KERNEL_MAP, strlen(value) + 1, value); - } - else if (strcmp("args", name) == 0) - { - desc = set_tag(desc, CONFIG_ARGS, strlen(value) + 1, value); - } - else if (strcmp("initrd", name) == 0) - { - desc = set_tag(desc, CONFIG_INITRD, strlen(value) + 1, value); - } - else if (strcmp("initrd_map", name) == 0) - { - desc = set_tag(desc, CONFIG_INITRD_MAP, strlen(value) + 1, value); - } - else if (strcmp("chainloader", name) == 0) - { - desc = set_tag(desc, CONFIG_CHAINLOADER, strlen(value) + 1, value); - } - else - { - fprintf(stderr, "ERROR: syntax error on word %s\n", name); - return -1; - } - offset = ftell(fd); - } - return found ? 0 : -1; -} - -int emile_config_read_next(emile_config* config) -{ - return read_description(config->fd, config->current, config->current_size); -} - -int emile_config_read_first_entry(emile_config* config) -{ - int offset = 0; - char name[256]; - char value[1024]; - - rewind(config->fd); - while (read_line(config->fd, name, value) != -1) - { - if (strcmp("title", name) == 0) - { - fseek(config->fd, offset, SEEK_SET); - break; - } - offset = ftell(config->fd); - } - return read_description(config->fd, config->current, config->current_size); -} - -int emile_config_get(emile_config* config, int tag, ...) -{ - int ret = -1; - va_list arg; - char **s; - int *v; - int *p; - - va_start(arg, tag); - switch(tag) - { - case CONFIG_PARTITION: - case CONFIG_FIRST_LEVEL: - case CONFIG_SECOND_LEVEL: - case CONFIG_VGA: - case CONFIG_MODEM: - case CONFIG_PRINTER: - s = va_arg(arg, char**); - *s = get_tag(config->header, tag); - ret = (*s == NULL) ? -1 : 0; - break; - case CONFIG_TIMEOUT: - case CONFIG_DEFAULT: - case CONFIG_GESTALTID: - v = va_arg(arg, int*); - p = (int*)get_tag(config->header, tag); - if (p != NULL) - { - ret = 0; - *v = *p; - } - break; - case CONFIG_TITLE: - case CONFIG_KERNEL: - case CONFIG_KERNEL_MAP: - case CONFIG_ARGS: - case CONFIG_INITRD: - case CONFIG_INITRD_MAP: - case CONFIG_CHAINLOADER: - s = va_arg(arg, char**); - *s = get_tag(config->current, tag); - ret = (*s == NULL) ? -1 : 0; - break; - } - va_end(arg); - return ret; -} diff --git a/tools/emile_config.h b/tools/emile_config.h deleted file mode 100644 index 41d7fc8..0000000 --- a/tools/emile_config.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * - * (c) 2007 Laurent Vivier - * - */ - -typedef struct emile_config { - FILE* fd; - char* header; - int header_size; - char* current; - int current_size; -} emile_config; - -enum { - CONFIG_END, - CONFIG_GESTALTID, - CONFIG_VGA, - CONFIG_MODEM, - CONFIG_PRINTER, - CONFIG_PARTITION, - CONFIG_FIRST_LEVEL, - CONFIG_SECOND_LEVEL, - CONFIG_TIMEOUT, - CONFIG_DEFAULT, - CONFIG_TITLE, - CONFIG_KERNEL, - CONFIG_KERNEL_MAP, - CONFIG_ARGS, - CONFIG_INITRD, - CONFIG_INITRD_MAP, - CONFIG_CHAINLOADER, -}; - -extern emile_config* emile_config_open(char* name); -extern void emile_config_close(emile_config* config); - -extern int emile_config_read_first_entry(emile_config* config); -extern int emile_config_read_next(emile_config* config); - -extern int emile_config_get(emile_config* config, int tag, ...);