Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 1 | |
| 2 | #include <err.h> |
| 3 | #include <fcntl.h> |
| 4 | #include <stdint.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <strings.h> |
| 9 | #include <unistd.h> |
| 10 | |
| 11 | #include <sys/mman.h> |
| 12 | #include <sys/stat.h> |
| 13 | |
Jeremy Kerr | e440a40 | 2016-03-17 16:34:14 +0800 | [diff] [blame] | 14 | static const char *config_default_filename = SYSCONFDIR "/obmc-console.conf"; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 15 | |
| 16 | struct config_item { |
| 17 | char *name; |
| 18 | char *value; |
| 19 | struct config_item *next; |
| 20 | }; |
| 21 | |
| 22 | struct config { |
| 23 | struct config_item *items; |
| 24 | }; |
| 25 | |
| 26 | const char *config_get_value(struct config *config, const char *name) |
| 27 | { |
| 28 | struct config_item *item; |
| 29 | |
| 30 | for (item = config->items; item; item = item->next) |
| 31 | if (!strcasecmp(item->name, name)) |
| 32 | return item->value; |
| 33 | |
| 34 | return NULL; |
| 35 | } |
| 36 | |
| 37 | static void config_parse(struct config *config, char *buf) |
| 38 | { |
| 39 | struct config_item *item; |
| 40 | char *name, *value; |
| 41 | char *p, *line; |
| 42 | int rc; |
| 43 | |
| 44 | for (p = NULL, line = strtok_r(buf, "\n", &p); line; |
| 45 | line = strtok_r(NULL, "\n", &p)) { |
| 46 | |
| 47 | /* trim leading space */ |
| 48 | for (;*line == ' ' || *line == '\t'; line++) |
| 49 | ; |
| 50 | |
| 51 | /* skip comments */ |
| 52 | if (*line == '#') |
| 53 | continue; |
| 54 | |
| 55 | name = value = NULL; |
| 56 | |
| 57 | rc = sscanf(line, "%m[^ =] = %ms ", &name, &value); |
| 58 | if (rc != 2 || !strlen(name) || !strlen(value)) { |
| 59 | free(name); |
| 60 | free(value); |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | /* create a new item and add to our list */ |
| 65 | item = malloc(sizeof(*item)); |
| 66 | item->name = name; |
| 67 | item->value = value; |
| 68 | item->next = config->items; |
| 69 | config->items = item; |
| 70 | } |
| 71 | } |
| 72 | |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 73 | static struct config *config_init_fd(int fd, const char *filename) |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 74 | { |
| 75 | struct config *config; |
Jeremy Kerr | 2eb56f5 | 2016-03-17 14:55:14 +0800 | [diff] [blame] | 76 | size_t size, len; |
Jeremy Kerr | 2eb56f5 | 2016-03-17 14:55:14 +0800 | [diff] [blame] | 77 | char *buf; |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 78 | int rc; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 79 | |
Jeremy Kerr | 2eb56f5 | 2016-03-17 14:55:14 +0800 | [diff] [blame] | 80 | size = 4096; |
| 81 | len = 0; |
| 82 | buf = malloc(size + 1); |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 83 | config = NULL; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 84 | |
Jeremy Kerr | 2eb56f5 | 2016-03-17 14:55:14 +0800 | [diff] [blame] | 85 | for (;;) { |
| 86 | rc = read(fd, buf + len, size - len); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 87 | if (rc < 0) { |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 88 | warn("Can't read from configuration file %s", filename); |
| 89 | goto out_free; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 90 | |
| 91 | } else if (!rc) { |
| 92 | break; |
| 93 | } |
Jeremy Kerr | 2eb56f5 | 2016-03-17 14:55:14 +0800 | [diff] [blame] | 94 | len += rc; |
| 95 | if (len == size) { |
| 96 | size <<= 1; |
| 97 | buf = realloc(buf, size + 1); |
| 98 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 99 | |
| 100 | } |
| 101 | buf[len] = '\0'; |
| 102 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 103 | config = malloc(sizeof(*config)); |
| 104 | config->items = NULL; |
| 105 | |
| 106 | config_parse(config, buf); |
| 107 | |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 108 | out_free: |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 109 | free(buf); |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 110 | return config; |
| 111 | } |
| 112 | |
| 113 | struct config *config_init(const char *filename) |
| 114 | { |
| 115 | struct config *config; |
| 116 | int fd; |
| 117 | |
| 118 | if (!filename) |
| 119 | filename = config_default_filename; |
| 120 | |
| 121 | fd = open(filename, O_RDONLY); |
| 122 | if (fd < 0) { |
| 123 | warn("Can't open configuration file %s", filename); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | config = config_init_fd(fd, filename); |
| 128 | |
| 129 | close(fd); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 130 | |
| 131 | return config; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void config_fini(struct config *config) |
| 135 | { |
| 136 | struct config_item *item, *next; |
| 137 | |
| 138 | for (item = config->items; item; item = next) { |
| 139 | next = item->next; |
| 140 | free(item->name); |
| 141 | free(item->value); |
| 142 | free(item); |
| 143 | } |
| 144 | |
| 145 | free(config); |
| 146 | } |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 147 | |
| 148 | #ifdef CONFIG_TEST |
| 149 | int main(void) |
| 150 | { |
| 151 | struct config_item *item; |
| 152 | struct config *config; |
| 153 | |
| 154 | config = config_init_fd(STDIN_FILENO, "<stdin>"); |
| 155 | if (!config) |
| 156 | return EXIT_FAILURE; |
| 157 | |
| 158 | for (item = config->items; item; item = item->next) |
| 159 | printf("%s: %s\n", item->name, item->value); |
| 160 | |
| 161 | config_fini(config); |
| 162 | |
| 163 | return EXIT_SUCCESS; |
| 164 | |
| 165 | } |
| 166 | #endif |