aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShav Kinderlehrer <[email protected]>2023-04-12 23:46:53 -0400
committerShav Kinderlehrer <[email protected]>2023-04-12 23:46:53 -0400
commit58e0d435c38c0b4e6c59244436556dba6ed96abf (patch)
tree8876d6c5d2d7bdbb4e0bfef1eda33e49bf1f1da3
parentc760493f48e4f8c6fd9f3effb5082d263128548c (diff)
downloadlat-58e0d435c38c0b4e6c59244436556dba6ed96abf.tar.gz
lat-58e0d435c38c0b4e6c59244436556dba6ed96abf.zip
Add argsv0.4.0
- add color arg - add lines arg
-rw-r--r--include/arg.h14
-rw-r--r--include/file.h4
-rw-r--r--include/util.h1
-rw-r--r--src/lib/arg.c64
-rw-r--r--src/lib/util.c2
-rw-r--r--src/main.c24
6 files changed, 94 insertions, 15 deletions
diff --git a/include/arg.h b/include/arg.h
new file mode 100644
index 0000000..be7ca0e
--- /dev/null
+++ b/include/arg.h
@@ -0,0 +1,14 @@
+#ifndef ARG_H
+#define ARG_H
+#include <stdbool.h>
+
+struct config {
+ bool color;
+ bool lines;
+};
+
+extern struct config conf;
+
+int parseargs(int argc, char *argv[]);
+
+#endif
diff --git a/include/file.h b/include/file.h
index 8dbaf86..df3b515 100644
--- a/include/file.h
+++ b/include/file.h
@@ -1,7 +1,7 @@
-#include <stdio.h>
-
#ifndef FILE_H
#define FILE_H
+
+#include <stdio.h>
struct filedata {
int lc;
size_t len;
diff --git a/include/util.h b/include/util.h
index ddb86fc..1b0243c 100644
--- a/include/util.h
+++ b/include/util.h
@@ -1,5 +1,6 @@
#ifndef LIB_H
#define LIB_H
+#include <stdbool.h>
#include <stddef.h>
void die(const char *message);
diff --git a/src/lib/arg.c b/src/lib/arg.c
new file mode 100644
index 0000000..6178156
--- /dev/null
+++ b/src/lib/arg.c
@@ -0,0 +1,64 @@
+#include "arg.h"
+#include "util.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct config conf;
+void argerr(char *r, char *arg) {
+ fprintf(stderr, "lat: %s '%s'\n", r, arg);
+ exit(EXIT_FAILURE);
+}
+
+void parselongarg(char *arg) {
+ if (strcmp(arg, "--color") == 0) {
+ conf.color = !conf.color;
+ return;
+ }
+
+ if (strcmp(arg, "--lines") == 0) {
+ conf.lines = !conf.lines;
+ 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':
+ conf.color = !conf.color;
+ break;
+ case 'n':
+ conf.lines = !conf.lines;
+ break;
+ default:
+ argerr("unrecognized flag", &c);
+ break;
+ }
+ i++;
+ }
+}
+
+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] == '-') {
+ if (arg[1] == '-') {
+ parselongarg(arg);
+ continue;
+ }
+
+ parseshortarg(arg);
+ } else {
+ return i;
+ }
+ }
+
+ return i;
+}
diff --git a/src/lib/util.c b/src/lib/util.c
index d2d4f6a..d66579d 100644
--- a/src/lib/util.c
+++ b/src/lib/util.c
@@ -4,7 +4,7 @@
void die(const char *message) {
perror(message);
- exit(1);
+ exit(EXIT_FAILURE);
}
char *formatbytes(size_t bytes, float *rounded) {
diff --git a/src/main.c b/src/main.c
index db1be8e..dc4d5f8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,6 +4,7 @@
#include <string.h>
#include <unistd.h>
+#include "arg.h"
#include "file.h"
#include "util.h"
@@ -12,12 +13,6 @@
#define GREY "\x1b[90m"
#define RESET "\x1b[0m"
-struct config {
- int color;
-};
-
-struct config conf;
-
void run(FILE *fp, char *filename, int tty) {
const char *invert_t = conf.color ? INVERT_T : "";
const char *uinvert_t = conf.color ? UINVERT_T : "";
@@ -41,7 +36,7 @@ void run(FILE *fp, char *filename, int tty) {
for (size_t i = 0; i < f.len; i++) {
c = f.buf[i];
- if (tty && !f.binary && (pc == '\n' || i == 0)) {
+ if ((conf.lines && tty && !f.binary) && (pc == '\n' || i == 0)) {
f.lc++;
int padlen = lcpad - intlen(f.lc);
@@ -70,24 +65,29 @@ void run(FILE *fp, char *filename, int tty) {
}
}
-void initconf(void) { conf.color = 1; }
+void initconf(void) {
+ conf.color = true;
+ conf.lines = true;
+}
int main(int argc, char *argv[]) {
initconf();
+ // init no_color first so that args take priority
char *no_color = getenv("NO_COLOR");
if (no_color != NULL && no_color[0] != '\0') {
- conf.color = 0;
+ conf.color = false;
}
if (argc > 1) {
- for (int i = 1; i < argc; i++) { // start at one to offset argv[0]
+ int offset = parseargs(argc, argv);
+ for (int i = offset; i < argc; i++) { // start at one to offset argv[0]
FILE *fp = fopen(argv[i], "rb");
if (fp == NULL)
die(argv[i]);
- int tty = isatty(STDOUT_FILENO);
+ bool tty = isatty(STDOUT_FILENO);
run(fp, argv[i], tty);
fclose(fp);
@@ -99,5 +99,5 @@ int main(int argc, char *argv[]) {
run(stdin, "stdin", 1); // for piped-input or repl-like behavior
}
- return 0;
+ return EXIT_SUCCESS;
}