1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arg.h"
#include "util.h"
#define LAT_USAGE "usage: lat [-cntbVh] [file...]"
void help(void) {
printf("lat | lazy cat - a cat clone with some quality-of-life "
"embelishments\n\n");
printf("%s\n\n", LAT_USAGE);
printf("options:\n"
"\t-c, --color\t toggle whether to print color or not\n"
"\t-n, --lines\t toggle whether to print line numbers or not\n"
"\t-t, --headers\t toggle whether to print file headers or not\n"
"\t-b, --binary\t toggle whether to force the data to be treated as "
"binary or not\n"
"\t-V, --version\t show program version\n"
"\t-h, --help\t display this help text\n\n");
printf("environment:\n"
"\tNO_COLOR, see https://no-color.org/\n\n");
printf("examples:\n"
"\tlat file1\n\t\t print the content of file1 witht default formatting\n"
"\tlat - file1\n\t\t read from stdin (the '-' character reads from stdin) "
"and then print the contents of stdin and file1\n"
"\tlat -nc file1 file2\n\t\t print the contents of file1 and file2 "
"without printing line numbers or colors\n"
"\tcurl example.com | lat\n\t\t pipe the results of 'curl example.com' "
"into lat\n");
}
void version(void) {
printf("lat - v%s built %s at %s\n", LAT_VERSION, __DATE__, __TIME__);
}
struct config conf;
void argerr(char *r, char *arg) {
printf("lat: %s '%s'\n\n", r, arg);
printf("%s\n", LAT_USAGE);
printf("run '--help' for more information\n");
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;
}
if (strcmp(arg, "--headers") == 0) {
conf.headers = !conf.headers;
return;
}
if (strcmp(arg, "--binary") == 0) {
conf.force_binary = !conf.force_binary;
return;
}
if (strcmp(arg, "--help") == 0) {
help();
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':
conf.color = !conf.color;
break;
case 'n':
conf.lines = !conf.lines;
break;
case 't':
conf.headers = !conf.headers;
break;
case 'b':
conf.force_binary = !conf.force_binary;
break;
case 'V':
version();
exit(EXIT_SUCCESS);
break;
case 'h':
help();
exit(EXIT_SUCCESS);
break;
default: {
char *str = malloc(2);
str[0] = c;
str[1] = '\0';
argerr("unrecognized flag", str);
free(str);
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] == '-' && arg[1] == '\0') {
return i;
}
if (arg[0] == '-') {
if (arg[1] == '-') {
parselongarg(arg);
continue;
}
parseshortarg(arg);
} else {
return i;
}
}
return i;
}
|