diff options
author | Tom Smeding <tom@tomsmeding.com> | 2024-07-14 09:48:55 +0200 |
---|---|---|
committer | Tom Smeding <tom@tomsmeding.com> | 2024-07-14 09:48:55 +0200 |
commit | 723828d1d473b70f77fa7878f855c2956ddcacc4 (patch) | |
tree | 5fc0e1b7b9e73679732a528d321ab9411b26ccd5 | |
parent | 7700192c257bff62a324cd9b12237d17f1238e59 (diff) |
Gebruik option parsing in kat en tak
-rw-r--r-- | src/kat.c | 75 | ||||
-rw-r--r-- | src/tak.c | 97 |
2 files changed, 119 insertions, 53 deletions
@@ -1,35 +1,68 @@ -#include <fcntl.h> -#include <stdio.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> -#include <sys/mman.h> -#include <sys/stat.h> #include <unistd.h> #include "util/map.h" +#include "util/versie.h" -int entry_kat(int argc, char **argv) { - if (argc == 2 && strcmp(argv[1], "-h") == 0) { - fprintf(stderr, "Gebruik: kat [BESTAND]...\n" - "\n" - "Schakel BESTAND(en) aaneen naar standaard uitvoer.\n"); - return 0; - } +static void usage(FILE *f) { + fprintf(f, + "Gebruik: kat [-hV] <bestand...>\n" + "\n" + "Schakel bestanden aaneen naar standaard uitvoer.\n" + "\n" + " -h Toon deze hulptekst\n" + " -V Toon versienummer\n"); +} - for (int i = 1; i < argc; i++) { - const char *fname = argv[i]; - struct map *map = open_map(fname); - if (map == NULL) { - return 1; +// Returns pointer to argument array containing the file names +static char** parse_options(int argc, char **argv) { + int opt; + while ((opt = getopt(argc, argv, "hV")) != -1) { + switch (opt) { + case 'h': + usage(stdout); + exit(0); + + case 'V': + drukkedoos_print_versie(stdout, "kat"); + exit(0); + + case '?': + usage(stderr); + exit(1); } + } + + return argv + optind; +} + +static void process(const char *fname) { + struct map *map = open_map(fname); + if (map == NULL) { + exit(1); + } - if (map->sb.st_size >= 0) { - for (size_t i = 0; i < (size_t)map->sb.st_size; i++) { - putchar(map->addr[i]); - } + if (map->sb.st_size >= 0) { + for (size_t i = 0; i < (size_t)map->sb.st_size; i++) { + putchar(map->addr[i]); } + } - close_map(map); + close_map(map); +} + +int entry_kat(int argc, char **argv) { + char **args = parse_options(argc, argv); + if (*args == NULL) { + fprintf(stderr, "kat: Standaard invoer niet ondersteund in deze projectie-gebaseerde implementatie\n"); + return 1; + } else { + while (*args != NULL) { + process(*args); + args++; + } } return 0; @@ -1,50 +1,83 @@ -#include <fcntl.h> -#include <stdio.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> -#include <sys/mman.h> -#include <sys/stat.h> #include <unistd.h> #include "util/map.h" +#include "util/versie.h" -int entry_tak(int argc, char **argv) { - if (argc == 2 && strcmp(argv[1], "-h") == 0) { - fprintf(stderr, "Gebruik: tak [BESTAND]...\n" - "\n" - "Schakel BESTAND(en) aaneen naar standaard uitvoer omgekeerd.\n"); - return 0; - } +static void usage(FILE *f) { + fprintf(f, + "Gebruik: tak [-hV] <bestand...>\n" + "\n" + "Schakel bestanden aaneen naar standaard uitvoer omgekeerd.\n" + "\n" + " -h Toon deze hulptekst\n" + " -V Toon versienummer\n"); +} + +// Returns pointer to argument array containing the file names +static char** parse_options(int argc, char **argv) { + int opt; + while ((opt = getopt(argc, argv, "hV")) != -1) { + switch (opt) { + case 'h': + usage(stdout); + exit(0); - for (int i = argc-1; i >= 1; i--) { - const char *fname = argv[i]; - struct map *map = open_map(fname); - if (map == NULL) { - return 1; + case 'V': + drukkedoos_print_versie(stdout, "tak"); + exit(0); + + case '?': + usage(stderr); + exit(1); } + } - char *lstart, *lend; - lend = &map->addr[map->sb.st_size - 1]; + return argv + optind; +} - while (lend > map->addr) { - if (*lend == '\n') { - lend--; - } +static void process(const char *fname) { + struct map *map = open_map(fname); + if (map == NULL) { + exit(1); + } - lstart = lend; - while (*lstart != '\n' && lstart != map->addr) { - lstart--; - } - if (lstart != map->addr) { - lstart++; - } + char *lstart, *lend; + lend = &map->addr[map->sb.st_size - 1]; - fwrite(lstart, 1, lend - lstart + 2, stdout); + while (lend > map->addr) { + if (*lend == '\n') { + lend--; + } - lend = lstart-1; + lstart = lend; + while (*lstart != '\n' && lstart != map->addr) { + lstart--; + } + if (lstart != map->addr) { + lstart++; } - close_map(map); + fwrite(lstart, 1, lend - lstart + 2, stdout); + + lend = lstart-1; + } + + close_map(map); +} + +int entry_tak(int argc, char **argv) { + char **args = parse_options(argc, argv); + if (*args == NULL) { + fprintf(stderr, "tak: Standaard invoer niet ondersteund in deze projectie-gebaseerde implementatie\n"); + return 1; + } else { + while (*args != NULL) { + process(*args); + args++; + } } return 0; |