summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/kat.c75
-rw-r--r--src/tak.c97
2 files changed, 119 insertions, 53 deletions
diff --git a/src/kat.c b/src/kat.c
index 7efa7e1..399e466 100644
--- a/src/kat.c
+++ b/src/kat.c
@@ -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;
diff --git a/src/tak.c b/src/tak.c
index efdc806..64a07a7 100644
--- a/src/tak.c
+++ b/src/tak.c
@@ -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;