-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu.c
263 lines (238 loc) · 6.31 KB
/
menu.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details. */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#define DEFAULT_NUM 15
#define CURRENT_MAX 150
#define INPUT_MAX 50
int delim='\n'; /*Delimiter for input/output items*/
char **lines; /*Lines read from stdin*/
size_t nlines; /*Number of lines in lines[]*/
/* This function slurps all the data from stdin, and stuffs it into lines[] */
void slurp (void) {
size_t maxlines;
char *readbuf;
size_t maxread,nread;
int c;
nlines=maxlines=0;
readbuf=malloc(maxread=128);
if (readbuf == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
nread=0;
while ((c=getchar()) != EOF) {
if (c == delim) {
/*End of line*/
readbuf[nread++]='\0';
if (nlines >= maxlines) {
/*Grow lines[]*/
maxread *= 2;
lines = realloc(lines,maxread*sizeof *lines);
if (!lines) {
perror("realloc");
exit(EXIT_FAILURE);
}
}
lines[nlines++] = strdup(readbuf);
nread=0;
}
else
{
readbuf[nread++] = c;
if (nread >= maxread) {
/* Grow after stuffing another character in.
This ensures there's room for the '\0'. */
maxread *= 2;
readbuf = realloc(readbuf,maxread);
if (!readbuf) {
perror("realloc");
exit(EXIT_FAILURE);
}
}
}
}
if (ferror(stdin)) {
perror("<stdin>");
exit(EXIT_FAILURE);
}
if (nread) {
/* Last item read didn't end with a newline. Don't lose it. */
if (nlines >= maxlines) {
/*Grow lines[]*/
maxread *= 2;
lines = realloc(lines,maxread*sizeof *lines);
if (!lines) {
perror("realloc");
exit(EXIT_FAILURE);
}
}
lines[nlines++] = strdup(readbuf);
}
free(readbuf);
}
/* This function takes in a string and prints it out, escaping spaces and quotes */
void printWithEscape (char * inp) {
int i=0;
int length= strlen(inp);
for (;i<length;i++){
if (inp[i]==' ' || inp[i]=='"' || inp[i]=='\'') {
putchar('\\');
}
putchar(inp[i]);
}
}
/* Print a usage message to stderr */
void usage() {
fprintf(stderr, "This function is used as a filter in a pipe from stdin to stdout.\n\
Options:\n\
-n NUM Specifies the number of entries to display\n\
-r Raw output. Doesn't escape any characters\n\
-0 Use NUL instead of newline as delimiter (implies -r)\n");
}
int main (int argc, char ** argv ) {
/* manymany decls */
char inp[INPUT_MAX];
char * end;
char * iterator;
/* This variable is used to hold the character that will start a line*/
/* Used in the ',' causing newline and '+' causing space case */
char lineStarter=-1;
FILE * tty;
int num=DEFAULT_NUM;
int i;
int selection;
char optFlag;
int Escape=1;
int need_newline=0;
/* Process command line arguments */
while ((optFlag=getopt(argc, argv, "n:r0"))!=-1){
switch(optFlag) {
case 'n':
/* Get the number to display */
num=atoi(optarg);
if (num==0) {
fprintf(stderr,"Invalid Input: %s", argv[i]);
exit(EXIT_FAILURE);
}
break;
case '0':
/* NUL instead of newline as delimiter */
delim='\0';
/* fall through - -0 implies -r */
case 'r':
/* Raw ouput */
Escape=0;
break;
case '?':
default:
usage();
return 0;
break;
}
}
/* Open the terminal for input/output so we don't
mess with stdin/stdout */
tty = fopen("/dev/tty","r+");
if(tty == NULL || errno == EINVAL) {
perror("fopen: /dev/tty");
exit(EXIT_FAILURE);
}
/* Slurp all input from stdin */
slurp();
if(nlines > num)
nlines=num;
/* Print out all lines from STDIN */
for(i=0; i < nlines; i++) {
fprintf(tty, "%d: %s\n",i,lines[i]);
}
/* Prompt */
fprintf(tty, "Selection: ");
/* Get one line of input from the user and put it in inp */
fgets(inp,INPUT_MAX,tty);
fclose(tty);
/* Initialize the iterator */
iterator=inp;
/* This is what I seem to get on a blank line */
if(*iterator != (char)10) {
while(*iterator != '\0') {
/* Try to convert user input to int */
selection=strtol(iterator,&end,10);
if(end == iterator) {
/* Not an int */
/* Consume White Space */
while(*end == ' ') end++;
/* Commands Go Here */
if(*end == '\0' || *end == (char)10) {
/* We're Done Here */
break;
/* I love the null command */
} else if(*end == 'a' && lineStarter==-1) {
/* The 'a' command outputs every line*/
/* 'a' is only valid if it's the first, thus the lineStarter check*/
for(i=0;i<nlines; i++){
if (Escape) {
printWithEscape(lines[i]);
} else {
printf("%s",lines[i]);
}
putchar(delim);
}
/* After a, no more input is accepted */
break;
} else if (*end == ',') {
/* The ',' command is one of those things that I love */
/* At first it doesn't seem like a command, but really it is. */
lineStarter=delim;
/* Set iterator to the next char */
iterator=end+1;
} else if (*end == '+') {
/* The '+' command I love too. See ',' command. */
lineStarter=' ';
/* Set iterator to the next char */
iterator=end+1;
} else {
/* If I don't recognize it, end */
fprintf(stderr,"Unrecognized Command %c\n", *end);
break;
}
} else {
/* Selection is the index the user chose
check if the index is valid. */
if(selection < nlines && selection >= 0) {
/* print selected item to STDOUT */
if (lineStarter!=-1) {
putchar(lineStarter);
}
if (Escape) {
printWithEscape(lines[selection]);
} else {
printf(lines[selection]);
}
need_newline=1;
} else {
/* User selected a bad index */
fprintf(stderr, "There is no item with the index %d\n", selection);
}
/*
This is actually kind of a hack
If I were to select 1+2, which I get is "1" and "+2", which is parsed as "2".
So, the default behaviour will actaully be "+", but it's only accessed if you break the numbers up with something that gets consumed without changing the value of the number
This mostly leaves '+'. Maybe '.', I'm not sure.
*/
lineStarter=' ';
iterator=end;
}
}
}
if(need_newline)
putchar(delim);
/* Boilerplate */
return 0;
}