This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sg.c
203 lines (161 loc) · 5.08 KB
/
sg.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
/* SuperGrep. fast "kg". version 1.01 */
/* modification history in "sg-history" */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXLINE 200
#define DATABASE "/repo/mine"
int main(int argc, char *argv[])
{
FILE *f;
FILE *fup;
char c, pwd[MAXLINE], line[MAXLINE], uline[MAXLINE], lineS[50][MAXLINE], filename[MAXLINE]="", str2[MAXLINE], oldfilename[MAXLINE]="", v[MAXLINE];
int
AFTER=4, /* lines before, minimum 1*/
i, /* loop index*/
search, /* search in current file?*/
next, /* remainning lines to print AFTER*/
first_print, /* print header?*/
index, /* index of cyclic queue*/
current, /* current line no.*/
offset, /* help var for BEFORE print*/
last_line, /* last printed line no.*/
current_index=0,
and=0,
nand=0,
casesens=0; /* -c option */
AFTER=0;
while (--argc>0 && (*++argv)[0]=='-')
while (c=*++argv[0])
switch (toupper(c)) {
case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':
AFTER=AFTER*10+c-'0';
break;
case 'C':
if (casesens) { printf("sg: not twice %c\n", c); return 0; }
casesens=1;
break;
case 'A':
if (and || nand) { printf("sg: not twice %c\n", c); return 0; }
and=1;
break;
case 'N':
if (nand || and) { printf("sg: not twice %c\n", c); return 0; }
nand=1;
break;
case 'H':
system("cat ~/sg-history");
return 0;
break;
default:
printf("sg: illegal option %c\n", c);
argc=0;
break;
}
if (AFTER<=0 || AFTER>45)
AFTER=4;
if (argc!=1+and+nand) {
printf("Super Grep v1.01\n");
printf("Usage: sg [-n] [-c] [-a|-n] [-v] string [string2]\n");
printf(" -n Number of surrounded lines. max 45, default 4\n");
printf(" -c Case sensitive search\n");
printf(" -a search string And string2\n");
printf(" -n search string Not string2\n");
printf(" -h show modification History\n");
return 0;
}
f= popen("zcat "DATABASE"/filecode", "r");
fup=popen("zcat "DATABASE"/filecodeup","r");
getcwd(pwd+100, MAXLINE);
strcpy(pwd, strrchr(pwd+100,'/')+1);
if (!strstr(pwd, "_lib") && !strstr(pwd, "main"))
strcpy(pwd,".");
else
printf("Searching in %s\n", pwd);
if (and==1) {
strcpy(str2, *argv);
argv++;
if (!casesens)
for (i=0; i<strlen(str2); i++)
str2[i]=toupper(str2[i]);
}
if (nand==1) {
strcpy(str2, *argv);
argv++;
if (!casesens)
for (i=0; i<strlen(str2); i++)
str2[i]=toupper(str2[i]);
}
if (!casesens)
for (i=0; i<strlen(*argv); i++)
(*argv)[i]=toupper((*argv)[i]);
while (!feof(f)) {
fgets( line, MAXLINE, f);
fgets(uline, MAXLINE, fup);
/* new header?*/
if (line[0]=='_') {
strcpy(oldfilename, "");
fgets( line, MAXLINE, f);
fgets(uline, MAXLINE, fup);
sscanf(line, "%s", filename);
if (strstr(filename,pwd))
search=1;
else if (search==1)
return 0;
else
search=0;
next =0;
current =0;
index =0;
first_print=1;
last_line =0;
}
/* pattern found?*/
else if (search) {
strcpy(lineS[index++], line);
index%=AFTER;
current++;
/* print AFTER lines*/
if (next>0) {
if (strstr(casesens?line:uline, nand?str2:*argv) &&
(and+nand==0 || (and==1 && strstr(casesens?line:uline, str2)) || (nand==1 && !strstr(casesens?line:uline, *argv) ))) {
printf("*");
next=AFTER;
}
else {
printf(" ");
next--;
}
printf(" %d\t%s", current, line);
last_line=current;
}
/* print BEFORE lines*/
else if (strstr(casesens?line:uline, nand?str2:*argv) &&
(and+nand==0 || (and==1 && strstr(casesens?line:uline, str2)) || (nand==1 && !strstr(casesens?line:uline, *argv) ))) {
/* print header?*/
if (first_print) {
current_index++;
printf("\n%d\n%s\n\n", current_index, (strstr(pwd, "_lib") || strstr(pwd, "main"))?strstr(filename,"/")+1:filename);
strcpy(oldfilename, filename);
first_print=0;
}
/* in "print AFTER-1 lines"?*/
if (next==0) {
if (current-AFTER+1>last_line)
printf("\n");
i=index;
for (offset=-(AFTER-1); offset<0; offset++) {
if (1<=current+offset && current+offset>last_line)
printf(" %d\t%s", current+offset, lineS[i]);
i++;
i%=AFTER;
}
printf("* %d\t%s", current, line);
next=AFTER;
}
}
}
}
strcpy(oldfilename, "");
return 0;
}