diff 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
line wrap: on
line diff
--- a/pwm.c	Mon Aug 07 19:11:56 2017 +0200
+++ b/pwm.c	Tue Aug 08 10:47:04 2017 +0200
@@ -57,6 +57,24 @@
 	fprintf(stderr, "usage: %s [-P file] [filename]\n", getprogname());
 }
 
+void
+pwm_err(struct pwm_ctx *ctx, char *fmt, ...)
+{
+	va_list	args;
+
+	free(ctx->errmsg);
+
+	if (fmt != NULL) {
+		va_start(args, fmt);
+		xvasprintf(&ctx->errmsg, fmt, args);
+		va_end(args);
+
+		fprintf(stderr, "%s\n", ctx->errmsg);
+	} else {
+		ctx->errmsg = NULL;
+	}
+}
+
 static int
 run_input_loop(struct pwm_ctx *ctx, int is_interactive)
 {
@@ -125,7 +143,7 @@
 		/* find and execute the command */
 		cmd = cmd_match(argv[0]);
 		if (cmd == NULL) {
-			fprintf(stderr, "unknown command: %s\n", argv[0]);
+			pwm_err(ctx, "unknown command: %s", argv[0]);
 			if (is_interactive) {
 				goto next;
 			} else {
@@ -134,6 +152,7 @@
 		}
 		switch (cmd->cmd_func(ctx, argc, argv)) {
 		case CMD_OK:
+			pwm_err(ctx, NULL); /* clear error */
 			break;
 		case CMD_USAGE:
 			fprintf(stderr, "usage: %s\n", cmd->usage);
@@ -145,12 +164,9 @@
 		case CMD_QUIT:
 			goto quit;
 		}
+		ctx->prev_cmd = cmd->full_cmd;
 
 next:
-		if (cmd != NULL) {
-			ctx->prev_cmd = cmd->full_cmd;
-		}
-
 		for (i = 0; i < argc; i++) {
 			free(argv[i]);
 		}
@@ -366,6 +382,7 @@
 		fclose(fp);
 	}
 	free(ctx.filename);
+	free(ctx.errmsg);
 	free(pwm_dirname);
 
 	exit(status);