Move to getopt

- everything is single-letter now
This commit is contained in:
Shav Kinderlehrer 2023-04-20 20:31:21 -04:00
parent efd1d76428
commit b7b16197f9
3 changed files with 43 additions and 115 deletions

View File

@ -13,6 +13,7 @@ struct config {
int force_binary; int force_binary;
bool literal; bool literal;
bool pager; bool pager;
char *name;
bool has_read_stdin; bool has_read_stdin;
}; };

View File

@ -1,30 +1,34 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include "arg.h" #include "arg.h"
#include "util.h" #include "util.h"
#define LAT_USAGE "usage: lat [-cntblpVh] [file...]" #define LAT_SHORT_ARGS "cltbrpn:Vh"
#define LAT_USAGE "usage: lat [-cltbrpnVh] [file...]"
struct config conf;
void help(void) { void help(void) {
printf("lat | lazy cat - a cat clone with some quality-of-life " printf("lat | lazy cat - a cat clone with some quality-of-life "
"embellishments\n\n"); "embellishments\n\n");
printf("%s\n\n", LAT_USAGE); printf("%s\n\n", LAT_USAGE);
printf("options:\n" printf(
"\t-c, --color\t toggle color\n" "options:\n"
"\t-n, --lines\t toggle line numbers\n" "\t-c\t toggle color\n"
"\t-t, --headers\t toggle file info headers\n" "\t-l\t toggle line numbers\n"
"\t-b, --binary\t toggle binary mode, -b forces binary and -bb forces " "\t-t\t toggle file info headers\n"
"NOT binary\n" "\t-b\t toggle binary mode, -b forces binary and -bb forces NOT binary\n"
"\t-l, --literal\t print everything to stdout (or equivalent)\n" "\t-r\t print everything to stdout (or equivalent)\n"
"\t-p, --pager\t print file with the pager (uses less)\n" "\t-p\t print file with the pager (uses less)\n"
"\t-V, --version\t show program version\n" "\t-n\t set the name of the file in the title\n"
"\t-h, --help\t display this help text (--help shows additional " "\t-V\t show program version\n"
"info)\n\n"); "\t-h\t display this help text\n\n");
printf("environment:\n" printf("environment:\n"
"\tNO_COLOR, see https://no-color.org/\n\n"); "\tNO_COLOR, see https://no-color.org/\n");
} }
void examples(void) { void examples(void) {
@ -44,82 +48,27 @@ void examples(void) {
"as a binary file and print it in the pager\n" "as a binary file and print it in the pager\n"
"\tcurl example.com | lat\n\t\t pipe the results of 'curl example.com' " "\tcurl example.com | lat\n\t\t pipe the results of 'curl example.com' "
"into lat\n" "into lat\n"
"\tfzf --preview 'lat -l {}'\n\t\t use lat as the file viewer in fzf\n" "\tfzf --preview 'lat -l {}'\n\t\t use lat as the file viewer in fzf\n");
);
} }
void version(void) { void version(void) {
printf("lat - v%s built %s at %s\n", LAT_VERSION, __DATE__, __TIME__); printf("lat - v%s built %s at %s\n", LAT_VERSION, __DATE__, __TIME__);
} }
struct config conf; void argerr(void) {
void argerr(char *r, char *arg) { printf("\n%s\n", LAT_USAGE);
printf("lat: %s '%s'\n\n", r, arg);
printf("%s\n", LAT_USAGE);
printf("run '--help' for more information\n"); printf("run '--help' for more information\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
void parselongarg(char *arg) { int parseargs(int argc, char *argv[]) {
if (strcmp(arg, "--color") == 0) { char opt;
conf.color = !conf.color; while ((opt = getopt(argc, argv, LAT_SHORT_ARGS)) != -1) {
return; switch (opt) {
}
if (strcmp(arg, "--lines") == 0) {
conf.lines = !conf.lines;
return;
}
if (strcmp(arg, "--headers") == 0) {
conf.headers = !conf.headers;
return;
}
if (strcmp(arg, "--binary") == 0) {
if (conf.force_binary < 0)
conf.force_binary = 1;
else
conf.force_binary = !conf.force_binary;
return;
}
if (strcmp(arg, "--literal") == 0) {
conf.literal = !conf.literal;
return;
}
if (strcmp(arg, "--pager") == 0) {
conf.pager = !conf.pager;
return;
}
if (strcmp(arg, "--help") == 0) {
help();
examples();
exit(EXIT_SUCCESS);
return;
}
if (strcmp(arg, "--version") == 0) {
version();
exit(EXIT_SUCCESS);
return;
}
argerr("unrecognized arg", arg);
}
void parseshortarg(char *arg) {
size_t i = 1;
while (arg[i] != '\0') {
char c = arg[i];
switch (c) {
case 'c': case 'c':
conf.color = !conf.color; conf.color = !conf.color;
break; break;
case 'n': case 'l':
conf.lines = !conf.lines; conf.lines = !conf.lines;
break; break;
case 't': case 't':
@ -131,12 +80,15 @@ void parseshortarg(char *arg) {
else else
conf.force_binary = !conf.force_binary; conf.force_binary = !conf.force_binary;
break; break;
case 'l': case 'r':
conf.literal = !conf.literal; conf.literal = !conf.literal;
break; break;
case 'p': case 'p':
conf.pager = !conf.pager; conf.pager = !conf.pager;
break; break;
case 'n':
conf.name = optarg;
break;
case 'V': case 'V':
version(); version();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
@ -145,39 +97,10 @@ void parseshortarg(char *arg) {
help(); help();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
default: { default:
char *str = malloc(2); argerr();
str[0] = c;
str[1] = '\0';
argerr("unrecognized flag", str);
free(str);
break; break;
} }
}
i++;
} }
} return optind;
int parseargs(int argc, char *argv[]) {
int i;
for (i = 1; i < argc; i++) { // offset for argv[0]
char *arg = argv[i];
if (arg[0] == '-' && arg[1] == '\0') {
return i;
}
if (arg[0] == '-') {
if (arg[1] == '-') {
parselongarg(arg);
continue;
}
parseshortarg(arg);
} else {
return i;
}
}
return i;
} }

View File

@ -1,7 +1,5 @@
#include <libgen.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include "arg.h" #include "arg.h"
@ -44,13 +42,13 @@ void run(FILE *fp, char *filename, bool tty) {
f.binary = false; f.binary = false;
} }
char *name = conf.name == NULL ? filename : conf.name;
if (conf.headers) { if (conf.headers) {
char *addon = f.binary ? "<binary>" : ""; char *addon = f.binary ? "<binary>" : "";
if (conf.stdin && !conf.pager) if (conf.stdin && !conf.pager)
fprintf(err, "\x1b[2K\r%s%s%s%s\n", invert_t, basename(filename), addon, fprintf(err, "\x1b[2K\r%s%s%s%s\n", invert_t, name, addon, reset);
reset);
else else
fprintf(err, "%s%s%s%s\n", invert_t, basename(filename), addon, reset); fprintf(err, "%s%s%s%s\n", invert_t, name, addon, reset);
} }
conf.process = (tty && !f.binary); conf.process = (tty && !f.binary);
@ -101,10 +99,13 @@ void initconf(void) {
conf.has_read_stdin = false; conf.has_read_stdin = false;
conf.pager = false; conf.pager = false;
conf.literal = false; conf.literal = false;
conf.process = true; conf.process = true;
conf.headers = true; conf.headers = true;
conf.color = true; conf.color = true;
conf.lines = true; conf.lines = true;
conf.name = NULL;
} }
void clearstdin(void) { void clearstdin(void) {
@ -141,6 +142,10 @@ int main(int argc, char *argv[]) {
conf.has_read_stdin = true; conf.has_read_stdin = true;
conf.stdin = true; conf.stdin = true;
run(stdin, "stdin", tty); run(stdin, "stdin", tty);
if (tty && (i + 1 != argc)) {
fprintf(err, "\n"); // separate concurrent files in tty
}
continue; continue;
} }
@ -151,7 +156,6 @@ int main(int argc, char *argv[]) {
run(fp, argv[i], tty); run(fp, argv[i], tty);
fclose(fp); fclose(fp);
if (tty && (i + 1 != argc)) { if (tty && (i + 1 != argc)) {
printf("offset: %d argc: %d\n", i, argc);
fprintf(err, "\n"); // separate concurrent files in tty fprintf(err, "\n"); // separate concurrent files in tty
} }
} }