[Pdbg] [PATCH 12/29] main: Prepare to change command handling

Cyril Bur cyrilbur at gmail.com
Fri Feb 9 15:38:40 AEDT 2018


Signed-off-by: Cyril Bur <cyrilbur at gmail.com>
---
 src/main.c | 48 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 44 insertions(+), 4 deletions(-)

diff --git a/src/main.c b/src/main.c
index cdf075d..ca060d6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -26,6 +26,8 @@
 #include <limits.h>
 #include <inttypes.h>
 
+#include <ccan/array_size/array_size.h>
+
 #include <config.h>
 
 #include <libpdbg.h>
@@ -76,6 +78,15 @@ static int **processorsel[MAX_PROCESSORS];
 static int *chipsel[MAX_PROCESSORS][MAX_CHIPS];
 static int threadsel[MAX_PROCESSORS][MAX_CHIPS][MAX_THREADS];
 
+static struct {
+	const char *name;
+	const char *args;
+	const char *desc;
+	int (*fn)(int, int, char **);
+} actions[] = {
+	{ "none yet", "nothing", "placeholder", NULL },
+};
+
 static void print_usage(char *pname)
 {
 	printf("Usage: %s [options] command ...\n\n", pname);
@@ -365,8 +376,17 @@ static bool parse_command(int argc, char *argv[])
 	bool opt_error = true;
 	char *endptr;
 
+	/*
+	 * In order to make the series changing this code cleaner,
+	 * this is going to happen gradually.
+	 * Eventually parse_command() will be deleted but so that this
+	 * can be done over multiple patches neatly remove the error
+	 * check here and print the usage later.
+	 * We'll just have a lie a bit for now.
+	 */
+	if (i >= argc || !parse_cmd(argv[i]))
+		return false;
 
-	opt_error = i == argc || !parse_cmd(argv[i]);
 	i++;
 	while (i < argc && !opt_error) {
 		if (cmd_arg_idx >= MAX_CMD_ARGS ||
@@ -582,13 +602,19 @@ void print_target(struct pdbg_target *target, int level)
 
 int main(int argc, char *argv[])
 {
-	int rc = 0;
-	uint8_t *buf;
 	struct pdbg_target *target;
+	bool found = true;
+	int i, rc = 0;
+	uint8_t *buf;
 
 	if (parse_options(argc, argv))
 		return 1;
 
+	if (optind >= argc) {
+		print_usage(argv[0]);
+		return 1;
+	}
+
 	/* Disable unselected targets */
 	if (target_select())
 		return 1;
@@ -711,10 +737,24 @@ int main(int argc, char *argv[])
 		rc = run_htm_analyse();
 		break;
 	default:
-		PR_ERROR("Unsupported command\n");
+		found = false;
 		break;
 	}
 
+	for (i = 0; i < ARRAY_SIZE(actions); i++) {
+		if (strcmp(argv[optind], actions[i].name) == 0) {
+			found = true;
+			rc = actions[i].fn(optind, argc, argv);
+			break;
+		}
+	}
+
+	if (!found) {
+		PR_ERROR("Unsupported command: %s\n", argv[optind]);
+		print_usage(argv[0]);
+		return 1;
+	}
+
 	if (rc <= 0) {
                 printf("No valid targets found or specified. Try adding -p/-c/-t options to specify a target.\n");
                 printf("Alternatively run %s -a probe to get a list of all valid targets\n", argv[0]);
-- 
2.16.1



More information about the Pdbg mailing list