commit a7b8ab1c3a951cec9c8ccd2f4db3ec6ef4071ddb
parent b39747133860cfedc975ba3d794bf85d8a1a482f
Author: Louis Burda <quent.burda@gmail.com>
Date: Tue, 23 May 2023 16:56:00 +0200
Add usage and fix sigint handler
Diffstat:
M | tquery.c | | | 39 | +++++++++++++++++++++++++++++++++------ |
1 file changed, 33 insertions(+), 6 deletions(-)
diff --git a/tquery.c b/tquery.c
@@ -61,6 +61,7 @@ sigint(int sig)
kill(locate_pid, SIGKILL);
waitpid(locate_pid, NULL, 0);
locate_pid = 0;
+ signal(SIGINT, sigint);
} else {
exit(0);
}
@@ -226,6 +227,10 @@ spawn(const char **prefix, const char *query)
dvec_init(&argv, sizeof(char *), 0, ga);
for (arg = prefix; *arg; arg++) {
+ if (!strcmp(*arg, "{}")) {
+ arg += 1;
+ break;
+ }
dvec_add_back(&argv, 1);
*(const char **)dvec_back(&argv) = *arg;
}
@@ -245,6 +250,11 @@ spawn(const char **prefix, const char *query)
*(const char **)dvec_back(&argv) = query;
}
+ for (; *arg; arg++) {
+ dvec_add_back(&argv, 1);
+ *(const char **)dvec_back(&argv) = *arg;
+ }
+
dvec_add_back(&argv, 1);
*(const char **)dvec_back(&argv) = NULL;
@@ -425,19 +435,37 @@ input(void)
}
void
+usage(int rc, bool full)
+{
+ fprintf(stderr, "Usage: tquery [OPT..] -- CMD [ARG..]\n");
+ if (full) {
+ fprintf(stderr, "\n");
+ fprintf(stderr, " -h, --help Show this message\n");
+ fprintf(stderr, " -s, --single Exit after single selection\n");
+ fprintf(stderr, " -d, --debug Enable debug mode\n");
+ fprintf(stderr, " -a, --args Split the query into args\n");
+ fprintf(stderr, " -c, --delim Set input entry delim\n");
+ fprintf(stderr, "\n");
+ }
+ exit(rc);
+}
+
+void
parse(int argc, const char **argv)
{
const char **arg;
const char *cmd_path;
for (arg = &argv[1]; *arg; arg++) {
- if (!strcmp(*arg, "-s")) {
+ if (!strcmp(*arg, "-h") || !strcmp(*arg, "--help")) {
+ usage(0, true);
+ } else if (!strcmp(*arg, "-s") || !strcmp(*arg, "--single")) {
single_out = true;
- } else if (!strcmp(*arg, "-d")) {
+ } else if (!strcmp(*arg, "-d") || !strcmp(*arg, "--debug")) {
debug = true;
- } else if (!strcmp(*arg, "-a")) {
+ } else if (!strcmp(*arg, "-a") || !strcmp(*arg, "--args")) {
separate_args = true;
- } else if (!strcmp(*arg, "-c")) {
+ } else if (!strcmp(*arg, "-c") || !strcmp(*arg, "--delim")) {
delim = **++arg;
} else if (!strcmp(*arg, "--")) {
cmd_argv = arg + 1;
@@ -449,8 +477,7 @@ parse(int argc, const char **argv)
}
if (!cmd_argv || !*cmd_argv) {
- fprintf(stderr, "tquery: empty command args\n");
- exit(1);
+ usage(0, false);
}
if (*cmd_argv[0] != '/' && strncmp(cmd_argv[0], "./", 2)) {