|
| 1 | +#define _GNU_SOURCE |
| 2 | +#include <confuse.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <string.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <errno.h> |
| 7 | + |
| 8 | +#include "genimage.h" |
| 9 | + |
| 10 | +static LIST_HEAD(optlist); |
| 11 | + |
| 12 | +struct config { |
| 13 | + const char *name; |
| 14 | + cfg_opt_t opt; |
| 15 | + const char *env; |
| 16 | + char cmdlineopt; |
| 17 | + struct list_head list; |
| 18 | + char *value; |
| 19 | + char *def; |
| 20 | +}; |
| 21 | + |
| 22 | +const char *get_opt(const char *name) |
| 23 | +{ |
| 24 | + struct config *c; |
| 25 | + |
| 26 | + list_for_each_entry(c, &optlist, list) { |
| 27 | + if (!strcmp(c->name, name)) |
| 28 | + return c->value; |
| 29 | + } |
| 30 | + |
| 31 | + return NULL; |
| 32 | +} |
| 33 | + |
| 34 | +int add_opt(const char *name, char cmdline, const char *env, cfg_opt_t *opt, char *def) |
| 35 | +{ |
| 36 | + struct config *c = xzalloc(sizeof(*c)); |
| 37 | + |
| 38 | + c->name = strdup(name); |
| 39 | + c->cmdlineopt = cmdline; |
| 40 | + c->env = env; |
| 41 | + c->def = def; |
| 42 | + memcpy(&c->opt, opt, sizeof(cfg_opt_t)); |
| 43 | + |
| 44 | + list_add_tail(&c->list, &optlist); |
| 45 | + |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +cfg_opt_t *get_config_opts(void) |
| 50 | +{ |
| 51 | + struct config *c; |
| 52 | + int num_opts = 0;; |
| 53 | + cfg_opt_t *opts; |
| 54 | + int i = 0; |
| 55 | + cfg_opt_t cfg_end[] = { |
| 56 | + CFG_END() |
| 57 | + }; |
| 58 | + |
| 59 | + list_for_each_entry(c, &optlist, list) |
| 60 | + num_opts++; |
| 61 | + |
| 62 | + opts = xzalloc(sizeof(cfg_opt_t) * (num_opts + 1)); |
| 63 | + |
| 64 | + list_for_each_entry(c, &optlist, list) { |
| 65 | + memcpy(&opts[i], &c->opt, sizeof(cfg_opt_t)); |
| 66 | + i++; |
| 67 | + } |
| 68 | + |
| 69 | + memcpy(&opts[i], cfg_end, sizeof(cfg_opt_t)); |
| 70 | + |
| 71 | + return opts; |
| 72 | +} |
| 73 | + |
| 74 | +int set_config_opts(int argc, char *argv[], cfg_t *cfg) |
| 75 | +{ |
| 76 | + struct config *c; |
| 77 | + cfg_t *cfgsec; |
| 78 | + |
| 79 | + list_for_each_entry(c, &optlist, list) { |
| 80 | + char *str = c->def; |
| 81 | + if (str) |
| 82 | + c->value = strdup(str); |
| 83 | + } |
| 84 | + |
| 85 | + list_for_each_entry(c, &optlist, list) { |
| 86 | + char *str = getenv(c->env); |
| 87 | + if (str) { |
| 88 | + if (c->value) |
| 89 | + free(c->value); |
| 90 | + c->value = strdup(str); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + cfgsec = cfg_getsec(cfg, "config"); |
| 95 | + if (cfgsec) { |
| 96 | + list_for_each_entry(c, &optlist, list) { |
| 97 | + char *str = cfg_getstr(cfgsec, c->opt.name); |
| 98 | + if (str) { |
| 99 | + if (c->value) |
| 100 | + free(c->value); |
| 101 | + c->value = strdup(str); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return 0; |
| 107 | +} |
| 108 | + |
| 109 | +const char *imagepath(void) |
| 110 | +{ |
| 111 | + return get_opt("outputpath"); |
| 112 | +} |
| 113 | + |
| 114 | +const char *inputpath(void) |
| 115 | +{ |
| 116 | + return get_opt("inputpath"); |
| 117 | +} |
| 118 | + |
| 119 | +const char *rootpath(void) |
| 120 | +{ |
| 121 | + return get_opt("rootpath"); |
| 122 | +} |
| 123 | + |
| 124 | +const char *tmppath(void) |
| 125 | +{ |
| 126 | + return get_opt("tmppath"); |
| 127 | +} |
| 128 | + |
| 129 | +static struct config opts[] = { |
| 130 | + { |
| 131 | + .name = "rootpath", |
| 132 | + .opt = CFG_STR("rootpath", NULL, CFGF_NONE), |
| 133 | + .cmdlineopt = 'r', |
| 134 | + .env = "PTXMKIMAGE_ROOTPATH", |
| 135 | + }, { |
| 136 | + .name = "tmppath", |
| 137 | + .opt = CFG_STR("tmppath", NULL, CFGF_NONE), |
| 138 | + .cmdlineopt = 't', |
| 139 | + .env = "PTXMKIMAGE_TMPPATH", |
| 140 | + }, { |
| 141 | + .name = "inputpath", |
| 142 | + .opt = CFG_STR("inputpath", NULL, CFGF_NONE), |
| 143 | + .cmdlineopt = 'i', |
| 144 | + .env = "PTXMKIMAGE_INPUTPATH", |
| 145 | + }, { |
| 146 | + .name = "outputpath", |
| 147 | + .opt = CFG_STR("outputpath", NULL, CFGF_NONE), |
| 148 | + .cmdlineopt = 'o', |
| 149 | + .env = "PTXMKIMAGE_OUTPUTPATH", |
| 150 | + }, { |
| 151 | + .name = "mkfsubifs", |
| 152 | + .opt = CFG_STR("mkfsubifs", NULL, CFGF_NONE), |
| 153 | + .cmdlineopt = 'o', |
| 154 | + .env = "PTXMKIMAGE_MKFSUBIFS", |
| 155 | + .def = "mkfs.ubifs", |
| 156 | + }, { |
| 157 | + .name = "mkfsjffs2", |
| 158 | + .opt = CFG_STR("mkfsjffs2", NULL, CFGF_NONE), |
| 159 | + .cmdlineopt = 'o', |
| 160 | + .env = "PTXMKIMAGE_MKFJFFS2", |
| 161 | + .def = "mkfs.jffs2", |
| 162 | + }, { |
| 163 | + .name = "ubinize", |
| 164 | + .opt = CFG_STR("ubinize", NULL, CFGF_NONE), |
| 165 | + .cmdlineopt = 'o', |
| 166 | + .env = "PTXMKIMAGE_UBINIZE", |
| 167 | + .def = "ubinize", |
| 168 | + }, { |
| 169 | + .name = "genext2fs", |
| 170 | + .opt = CFG_STR("genext2fs", NULL, CFGF_NONE), |
| 171 | + .cmdlineopt = 'o', |
| 172 | + .env = "PTXMKIMAGE_GENEXT2FS", |
| 173 | + .def = "genext2fs", |
| 174 | + }, { |
| 175 | + .name = "tar", |
| 176 | + .opt = CFG_STR("tar", NULL, CFGF_NONE), |
| 177 | + .cmdlineopt = 'o', |
| 178 | + .env = "PTXMKIMAGE_TAR", |
| 179 | + .def = "tar", |
| 180 | + }, |
| 181 | +}; |
| 182 | + |
| 183 | +int init_config(void) |
| 184 | +{ |
| 185 | + int i, ret; |
| 186 | + |
| 187 | + for (i = 0; i < ARRAY_SIZE(opts); i++) { |
| 188 | + ret = add_opt(opts[i].name, opts[i].cmdlineopt, opts[i].env, &opts[i].opt, opts[i].def); |
| 189 | + if (ret) |
| 190 | + return ret; |
| 191 | + } |
| 192 | + |
| 193 | + return 0; |
| 194 | +} |
0 commit comments