comparison pwm.c @ 16:a07665727c19

Add status command Add status command to redisplay an error message of the previous command and unsaved changes. Add pwm_err function to display and save error messages.
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Tue, 08 Aug 2017 10:47:04 +0200
parents cf81eb0c2d5a
children 1e39a251cbe9
comparison
equal deleted inserted replaced
15:3380c8fd9776 16:a07665727c19
55 usage(void) 55 usage(void)
56 { 56 {
57 fprintf(stderr, "usage: %s [-P file] [filename]\n", getprogname()); 57 fprintf(stderr, "usage: %s [-P file] [filename]\n", getprogname());
58 } 58 }
59 59
60 void
61 pwm_err(struct pwm_ctx *ctx, char *fmt, ...)
62 {
63 va_list args;
64
65 free(ctx->errmsg);
66
67 if (fmt != NULL) {
68 va_start(args, fmt);
69 xvasprintf(&ctx->errmsg, fmt, args);
70 va_end(args);
71
72 fprintf(stderr, "%s\n", ctx->errmsg);
73 } else {
74 ctx->errmsg = NULL;
75 }
76 }
77
60 static int 78 static int
61 run_input_loop(struct pwm_ctx *ctx, int is_interactive) 79 run_input_loop(struct pwm_ctx *ctx, int is_interactive)
62 { 80 {
63 int retval = -1; 81 int retval = -1;
64 char buf[PWM_LINE_MAX]; 82 char buf[PWM_LINE_MAX];
123 } 141 }
124 142
125 /* find and execute the command */ 143 /* find and execute the command */
126 cmd = cmd_match(argv[0]); 144 cmd = cmd_match(argv[0]);
127 if (cmd == NULL) { 145 if (cmd == NULL) {
128 fprintf(stderr, "unknown command: %s\n", argv[0]); 146 pwm_err(ctx, "unknown command: %s", argv[0]);
129 if (is_interactive) { 147 if (is_interactive) {
130 goto next; 148 goto next;
131 } else { 149 } else {
132 goto out; 150 goto out;
133 } 151 }
134 } 152 }
135 switch (cmd->cmd_func(ctx, argc, argv)) { 153 switch (cmd->cmd_func(ctx, argc, argv)) {
136 case CMD_OK: 154 case CMD_OK:
155 pwm_err(ctx, NULL); /* clear error */
137 break; 156 break;
138 case CMD_USAGE: 157 case CMD_USAGE:
139 fprintf(stderr, "usage: %s\n", cmd->usage); 158 fprintf(stderr, "usage: %s\n", cmd->usage);
140 case CMD_ERR: /* FALLTHROUGH */ 159 case CMD_ERR: /* FALLTHROUGH */
141 if (!is_interactive) { 160 if (!is_interactive) {
143 } 162 }
144 break; 163 break;
145 case CMD_QUIT: 164 case CMD_QUIT:
146 goto quit; 165 goto quit;
147 } 166 }
167 ctx->prev_cmd = cmd->full_cmd;
148 168
149 next: 169 next:
150 if (cmd != NULL) {
151 ctx->prev_cmd = cmd->full_cmd;
152 }
153
154 for (i = 0; i < argc; i++) { 170 for (i = 0; i < argc; i++) {
155 free(argv[i]); 171 free(argv[i]);
156 } 172 }
157 free(argv); 173 free(argv);
158 argc = 0; 174 argc = 0;
364 pwfile_destroy(&ctx); 380 pwfile_destroy(&ctx);
365 if (fp != NULL) { 381 if (fp != NULL) {
366 fclose(fp); 382 fclose(fp);
367 } 383 }
368 free(ctx.filename); 384 free(ctx.filename);
385 free(ctx.errmsg);
369 free(pwm_dirname); 386 free(pwm_dirname);
370 387
371 exit(status); 388 exit(status);
372 } 389 }