aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2021-11-06 17:06:17 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2021-11-06 17:06:17 -0400
commit6536e1ee7c36d856f244bcce08112d463c59b8f9 (patch)
tree3baedb28e0bb528ad7fb4e33d2073af542ffc8c6
parente8db05f86e193e597abca60e997bb17a20a8e0e8 (diff)
downloadjdtalkc-6536e1ee7c36d856f244bcce08112d463c59b8f9.tar.gz
Improvements
* Option parsing is easier on the eyes * Options are validated against a list of possible short options * Replace srand with srandom
-rw-r--r--main.c96
1 files changed, 76 insertions, 20 deletions
diff --git a/main.c b/main.c
index be03113..820a710 100644
--- a/main.c
+++ b/main.c
@@ -11,6 +11,7 @@ static const char *usage_text = \
" -h Show this usage statement\n"
" -H Produce hill-cased strings (hIlL cAsE)\n"
" -l Produce leet speak strings (1337 5|*34|<)\n"
+ " -p str Search for `str` in output\n"
" -r Produce random-case strings (raNdoM CasE)\n"
" -R Produce reversed strings (sgnirts desrever)\n"
" -s num Produce word salad (`num` words per line)\n"
@@ -33,6 +34,19 @@ static void usage(char *name) {
printf(usage_text, begin);
}
+static int argv_validate(const char *possible, char *s) {
+ if (strlen(s) > 1) {
+ for (size_t i = 0; i < strlen(possible); i++) {
+ if (possible[i] == *(s + 1))
+ return 1;
+ }
+ }
+ return 0;
+}
+
+#define ARG(X) strcmp(option, X) == 0
+static const char *args_valid = "abcefhHlprRsSt";
+
int main(int argc, char *argv[]) {
struct Dictionary *dict;
char buf[OUTPUT_SIZE_MAX];
@@ -77,55 +91,97 @@ int main(int argc, char *argv[]) {
buf[0] = '\0';
acronym[0] = '\0';
- srand(time(NULL));
+ srandom(time(NULL));
setvbuf(stdout, NULL, _IONBF, 0);
for (int i = 1; i < argc; i++) {
- if (strcmp(argv[i], "-h") == 0) {
+ char *option;
+ char *option_value;
+ option = argv[i];
+ option_value = argv[i + 1];
+
+ if (!argv_validate(args_valid, option)) {
+ fprintf(stderr, "Unknown argument: %s\n", option);
+ usage(argv[0]);
+ exit(1);
+ }
+ if (ARG( "-h")) {
usage(argv[0]);
exit(0);
}
- if (strcmp(argv[i], "-b") == 0) {
+ if (ARG( "-b")) {
do_benchmark = 1;
}
- if (strcmp(argv[i], "-c") == 0) {
- limit = (int) strtol(argv[i + 1], NULL, 10);
+ if (ARG( "-c")) {
+ if (!option_value || *option_value == '-') {
+ fprintf(stderr, "requires a positive integer option_value\n");
+ exit(1);
+ }
+
+ limit = (int) strtol(option_value, NULL, 10);
+ if (!limit) {
+ limit = 1;
+ }
+ i++;
+ continue;
}
- if (strcmp(argv[i], "-p") == 0) {
+ if (ARG( "-p")) {
+ if (!option_value || *option_value == '-') {
+ fprintf(stderr, "requires a dictionary word\n");
+ exit(1);
+ }
do_pattern = 1;
- strcpy(pattern, argv[i + 1]);
+ strcpy(pattern, option_value);
+ i++;
+ continue;
}
- if (strcmp(argv[i], "-e") == 0) {
+ if (ARG( "-e")) {
do_exact = 1;
}
- if (strcmp(argv[i], "-r") == 0) {
+ if (ARG( "-r")) {
do_random_case = 1;
}
- if (strcmp(argv[i], "-H") == 0) {
+ if (ARG( "-H")) {
do_hill_case = 1;
}
- if (strcmp(argv[i], "-l") == 0) {
+ if (ARG( "-l")) {
do_leet = 1;
}
- if (strcmp(argv[i], "-s") == 0) {
+ if (ARG( "-s")) {
+ if (!option_value || *option_value == '-') {
+ fprintf(stderr, "requires a positive integer option_value\n");
+ exit(1);
+ }
+
do_salad = 1;
- salad_limit = (int) strtol(argv[i + 1], NULL, 10);
+ salad_limit = (int) strtol(option_value, NULL, 10);
+ if (!salad_limit) {
+ salad_limit = 1;
+ }
+ i++;
+ continue;
}
- if (strcmp(argv[i], "-f") == 0) {
- strcpy(format, argv[i + 1]);
+ if (ARG( "-f")) {
+ strcpy(format, option_value);
}
- if (strcmp(argv[i], "-a") == 0) {
+ if (ARG( "-a")) {
+ if (!option_value) {
+ fprintf(stderr, "requires a string\n");
+ exit(1);
+ }
do_acronym = 1;
do_title_case = 1;
- strcpy(acronym, argv[i + 1]);
+ strcpy(acronym, option_value);
+ i++;
+ continue;
}
- if (strcmp(argv[i], "-t") == 0) {
+ if (ARG( "-t")) {
do_title_case = 1;
}
- if (strcmp(argv[i], "-S") == 0) {
+ if (ARG( "-S")) {
do_shuffle = 1;
}
- if (strcmp(argv[i], "-R") == 0) {
+ if (ARG( "-R")) {
do_reverse = 1;
}
}