]> git.mdlowis.com Git - proto/labwc.git/commitdiff
find-banned: support reading multiple files from stdin
authorJohan Malm <jgm323@gmail.com>
Sun, 7 May 2023 21:14:35 +0000 (22:14 +0100)
committerJohan Malm <johanmalm@users.noreply.github.com>
Thu, 6 Jul 2023 17:04:55 +0000 (18:04 +0100)
scripts/helper/find-idents.c

index 655aba2825bef24f60a5f4efbe7f41d134fcdc96..b581f2dd4fa4899e35001b51a303151d6f00bae1 100644 (file)
@@ -60,7 +60,52 @@ enum {
 static char *current_buffer_position;
 static struct token *tokens;
 static int nr_tokens, alloc_tokens;
-static int current_line = 1;
+static int current_line;
+static char **argv_tokens;
+static int found_token;
+
+static const char find_banned_usage[] =
+"Usage: find-banned [OPTIONS...] FILE\n"
+"When FILE is -, read stdin\n"
+"OPTIONS:\n"
+"  --tokens=<tokens>     Comma-separated string of idents to grep for\n";
+
+static void
+usage(void)
+{
+       printf("%s", find_banned_usage);
+       exit(0);
+}
+
+char **
+split(char *str, char delim)
+{
+       if (!str) {
+               return NULL;
+       }
+       int argc = 1;
+       char *p = str;
+       while (*p) {
+               if (*p == delim) {
+                       argc++;
+               }
+               ++p;
+       }
+
+       char **argv = calloc(argc + 1, sizeof(*argv));
+       char **argvp = argv;
+       p = str;
+       while (*str) {
+               if (*str == delim) {
+                       *argvp++ = strndup(p, str-p);
+                       p = str + 1;
+               }
+               ++str;
+       }
+       *argvp++ = strndup(p, str-p);
+       *argvp = NULL;
+       return argv;
+}
 
 void
 buf_init(struct buf *s)
@@ -369,33 +414,66 @@ grep(struct token *tokens, const char *filename, const char *pattern)
        return found;
 }
 
-int
-main(int argc, char **argv)
+static void
+process_one_file(const char *filename)
 {
        struct token *tokens;
-       int found = false;
-
-       if (argc < 2) {
-               fprintf(stderr, "usage: %s <file> [<patterns>...]\n", argv[0]);
-               return EXIT_FAILURE;
-       }
-
-       char *buffer = read_file(argv[1]);
+       char *buffer = read_file(filename);
        if (!buffer) {
-               return EXIT_FAILURE;
+               exit(EXIT_FAILURE);
        }
+       current_line = 1;
        tokens = lex(buffer);
        free(buffer);
 
-       if (argc == 2) {
+       if (!argv_tokens) {
                /* Dump all idents */
-               grep(tokens, argv[1], NULL);
+               grep(tokens, filename, NULL);
        } else {
-               for (int i = 2; i < argc; ++i) {
-                       found |= grep(tokens, argv[1], argv[i]);
+               for (char **p = argv_tokens; *p; p++) {
+                       found_token |= grep(tokens, filename, *p);
+               }
+       }
+}
+
+int
+main(int argc, char **argv)
+{
+       if (argc < 2) {
+               usage();
+       }
+       for (int i = 1; i < argc; ++i) {
+               char *arg = argv[i];
+
+               if (!strncmp(arg, "--tokens=", 9)) {
+                       argv_tokens = split(arg + 9, ',');
+               }
+               if (!strcmp(arg, "-")) {
+                       char *line = NULL;
+                       size_t len = 0;
+                       while ((getline(&line, &len, stdin) != -1)) {
+                               char *p = strrchr(line, '\n');
+                               if (p) {
+                                       *p = '\0';
+                               }
+                               process_one_file(line);
+                       }
+                       free(line);
+                       break;
+               }
+               if (arg[0] != '-') {
+                       process_one_file(arg);
+                       break;
                }
        }
 
+       if (argv_tokens) {
+               for (char **p = argv_tokens; *p; p++) {
+                       free(*p);
+               }
+               free(argv_tokens);
+       }
+
        /* return failure (1) if we have found a banned identifier */
-       return found;
+       return found_token;
 }