From: Martin Pieuchot Subject: tog(1) log by default++ To: gameoftrees@openbsd.org Date: Fri, 14 Feb 2020 16:39:48 +0100 If the second argument on the command line doesn't match a command name let the tool to assume it's a path. In other words the following: $ tog kern/kern_sync.c Becomes an alias for: $ tog log kern/kern_sync.c This is similar to what tig(1) does and it helps me being lazy :o) While here remove make_argv(), or is the need to fake a command name necessary instead of just rewinding ``argv''? Comments? Oks? diff --git tog/tog.c tog/tog.c index ca4cead..7b087f7 100644 --- tog/tog.c +++ tog/tog.c @@ -5303,34 +5303,12 @@ usage(int hflag) exit(1); } -static char ** -make_argv(const char *arg0, const char *arg1) -{ - char **argv; - int argc = (arg1 == NULL ? 1 : 2); - - argv = calloc(argc, sizeof(char *)); - if (argv == NULL) - err(1, "calloc"); - argv[0] = strdup(arg0); - if (argv[0] == NULL) - err(1, "strdup"); - if (arg1) { - argv[1] = strdup(arg1); - if (argv[1] == NULL) - err(1, "strdup"); - } - - return argv; -} - int main(int argc, char *argv[]) { const struct got_error *error = NULL; struct tog_cmd *cmd = NULL; int ch, hflag = 0, Vflag = 0; - char **cmd_argv = NULL; static struct option longopts[] = { { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0} @@ -5365,14 +5343,10 @@ main(int argc, char *argv[]) if (argc == 0) { if (hflag) usage(hflag); - /* Build an argument vector which runs a default command. */ - cmd = &tog_commands[0]; - cmd_argv = make_argv(cmd->name, NULL); - argc = 1; } else { int i; - /* Did the user specific a command? */ + /* Did the user specify a command? */ for (i = 0; i < nitems(tog_commands); i++) { if (strncmp(tog_commands[i].name, argv[0], strlen(argv[0])) == 0) { @@ -5380,22 +5354,21 @@ main(int argc, char *argv[]) break; } } + } - if (cmd == NULL) { - fprintf(stderr, "%s: unknown command '%s'\n", - getprogname(), argv[0]); - list_commands(); - return 1; - } + if (cmd == NULL) { + /* No command specified, default to 'log' */ + cmd = &tog_commands[0]; + argv--; + argc++; } if (hflag) cmd->cmd_usage(); else - error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv); + error = cmd->cmd_main(argc, argv); endwin(); - free(cmd_argv); if (error && error->code != GOT_ERR_CANCELLED) fprintf(stderr, "%s: %s\n", getprogname(), error->msg); return 0;