comparison cmd.c @ 28:e3db02d7f1f4

Add set command for setting or retrieving option values
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Wed, 27 Sep 2017 19:44:05 +0200
parents 722a45b4028b
children 2552eec9b913
comparison
equal deleted inserted replaced
27:722a45b4028b 28:e3db02d7f1f4
82 CHARCLASS_ALNUM, 82 CHARCLASS_ALNUM,
83 CHARCLASS_XDIGIT, 83 CHARCLASS_XDIGIT,
84 CHARCLASS_GRAPH 84 CHARCLASS_GRAPH
85 }; 85 };
86 86
87 enum option_type {
88 OPTION_UNKNOWN = -1,
89 OPTION_FILENAME,
90 OPTION_PIPECOMMAND
91 };
92
93 static enum cmd_return cmd_set(struct pwm_ctx *, int, char *[]);
87 static enum cmd_return cmd_define(struct pwm_ctx *, int, char *[]); 94 static enum cmd_return cmd_define(struct pwm_ctx *, int, char *[]);
88 static enum cmd_return cmd_status(struct pwm_ctx *, int, char *[]); 95 static enum cmd_return cmd_status(struct pwm_ctx *, int, char *[]);
89 static enum cmd_return cmd_info(struct pwm_ctx *, int, char *[]); 96 static enum cmd_return cmd_info(struct pwm_ctx *, int, char *[]);
90 static enum cmd_return cmd_list(struct pwm_ctx *, int, char *[]); 97 static enum cmd_return cmd_list(struct pwm_ctx *, int, char *[]);
91 static enum cmd_return cmd_create(struct pwm_ctx *, int, char *[]); 98 static enum cmd_return cmd_create(struct pwm_ctx *, int, char *[]);
152 CHARS_DIGIT CHARS_UPPER CHARS_LOWER, 159 CHARS_DIGIT CHARS_UPPER CHARS_LOWER,
153 CHARS_DIGIT "abcdef", 160 CHARS_DIGIT "abcdef",
154 CHARS_DIGIT CHARS_UPPER CHARS_LOWER CHARS_PUNCT 161 CHARS_DIGIT CHARS_UPPER CHARS_LOWER CHARS_PUNCT
155 }; 162 };
156 163
164 static const char *optionv[] = {
165 "filename",
166 "pipecommand",
167 NULL
168 };
169
157 static struct cmd cmds[] = { 170 static struct cmd cmds[] = {
171 { "S", "set", "set [option=value]", "Set an option or show option values",
172 cmd_set },
158 { "D", "define", "define name=value", "Define a macro", cmd_define }, 173 { "D", "define", "define name=value", "Define a macro", cmd_define },
159 { "t", "status", "status", "Redisplay an error message of the previous " 174 { "t", "status", "status", "Redisplay an error message of the previous "
160 "command and unsaved changes", cmd_status }, 175 "command and unsaved changes", cmd_status },
161 { "i", "info", "info", "Show metadata information about the current file", 176 { "i", "info", "info", "Show metadata information about the current file",
162 cmd_info }, 177 cmd_info },
219 234
220 return (0); 235 return (0);
221 } 236 }
222 237
223 static enum cmd_return 238 static enum cmd_return
239 cmd_set(struct pwm_ctx *ctx, int argc, char *argv[])
240 {
241 enum option_type type;
242 char *value;
243
244 if (argc == 1) {
245 /* show options */
246 if ((io_printf("%s: %s\n", optionv[OPTION_FILENAME],
247 ctx->filename) == IO_SIGNAL) ||
248 (io_printf("%s: %s\n", optionv[OPTION_PIPECOMMAND],
249 (ctx->pipecmd != NULL) ? ctx->pipecmd : "") == IO_SIGNAL)) {
250 return (CMD_SIGNAL);
251 }
252
253 return (CMD_OK);
254 } else if (argc != 2) {
255 return (CMD_USAGE);
256 }
257
258 type = parse_arg(argv[1], optionv, '=', &value);
259 switch (type) {
260 case OPTION_FILENAME:
261 free(ctx->filename);
262 ctx->filename = (value[0] != '\0') ? xstrdup(value) :
263 xasprintf(&ctx->filename, "%s/pwm.psafe3", ctx->dirname);
264 break;
265 case OPTION_PIPECOMMAND:
266 free(ctx->pipecmd);
267 ctx->pipecmd = (value[0] != '\0') ? xstrdup(value) : NULL;
268 break;
269 default:
270 pwm_err(ctx, "bad option name \"%s\"", argv[1]);
271 return (CMD_ERR);
272 }
273
274 return (CMD_OK);
275 }
276
277 static enum cmd_return
224 cmd_define(struct pwm_ctx *ctx, int argc, char *argv[]) 278 cmd_define(struct pwm_ctx *ctx, int argc, char *argv[])
225 { 279 {
226 int retval = CMD_ERR; 280 int retval = CMD_ERR;
227 const char *value; 281 const char *value;
228 char *name = NULL; 282 char *name = NULL;
996 enum cmd_return retval = CMD_ERR; 1050 enum cmd_return retval = CMD_ERR;
997 unsigned int id; 1051 unsigned int id;
998 struct record *record = NULL; 1052 struct record *record = NULL;
999 enum field_type type; 1053 enum field_type type;
1000 int fields[COUNTOF(field_namev) - 1] = { 0 }; 1054 int fields[COUNTOF(field_namev) - 1] = { 0 };
1001 struct proc proc = { 0 }; 1055 char *cmd;
1002 1056 struct proc proc = { 0 };
1003 if (argc != 4) { 1057
1058 /* if pipecommand is set, the last argument is optional */
1059 if (((ctx->pipecmd == NULL) && (argc != 4)) ||
1060 ((ctx->pipecmd != NULL) && ((argc < 3) || (argc > 4)))) {
1004 return (CMD_USAGE); 1061 return (CMD_USAGE);
1005 } 1062 }
1006 1063
1007 if (parse_id(argv[1], &id) != 0) { 1064 if (parse_id(argv[1], &id) != 0) {
1008 pwm_err(ctx, "invalid id %s", argv[1]); 1065 pwm_err(ctx, "invalid id %s", argv[1]);
1014 pwm_err(ctx, "bad field name \"%s\"", argv[2]); 1071 pwm_err(ctx, "bad field name \"%s\"", argv[2]);
1015 return (CMD_ERR); 1072 return (CMD_ERR);
1016 } 1073 }
1017 fields[type] = 1; 1074 fields[type] = 1;
1018 1075
1019 if (proc_open(&proc, argv[3], "w") != IO_OK) { 1076 cmd = (argc == 4) ? argv[3] : ctx->pipecmd;
1020 goto out; 1077 if (proc_open(&proc, cmd, "w") != IO_OK) {
1021 } 1078 goto out;
1079 }
1080
1022 1081
1023 record = pwfile_get_record(ctx, id); 1082 record = pwfile_get_record(ctx, id);
1024 if (record == NULL) { 1083 if (record == NULL) {
1025 pwm_err(ctx, "record %u does not exist", id); 1084 pwm_err(ctx, "record %u does not exist", id);
1026 goto out; 1085 goto out;