21 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
#include <config.h> |
23 |
#endif |
#endif |
24 |
|
#include <fcntl.h> |
25 |
#include <stdio.h> |
#include <stdio.h> |
26 |
#include <stdlib.h> |
#include <stdlib.h> |
27 |
#include <sys/param.h> |
#include <sys/param.h> |
28 |
|
|
29 |
#include "die.h" |
#include "die.h" |
30 |
#include "gparam.h" |
#include "gparam.h" |
31 |
|
#include "dbop.h" |
32 |
#include "split.h" |
#include "split.h" |
33 |
#include "strbuf.h" |
#include "strbuf.h" |
34 |
#include "strlimcpy.h" |
#include "strlimcpy.h" |
38 |
/* |
/* |
39 |
* A special version of sort command. |
* A special version of sort command. |
40 |
* |
* |
41 |
|
* 1. ctags [-x] format |
42 |
|
* |
43 |
* As long as the input meets the undermentioned requirement, |
* As long as the input meets the undermentioned requirement, |
44 |
* you can use this special sort command as a sort filter for |
* you can use this special sort command as a sort filter for |
45 |
* global(1) instead of external sort command. |
* global(1) instead of external sort command. |
55 |
* |
* |
56 |
* usage: read from stdin, sort and write it to stdout. |
* usage: read from stdin, sort and write it to stdout. |
57 |
* |
* |
58 |
* tagsort(0, stdin, stdout); |
* tagsort(TAGSORT_CTAGS, stdin, stdout); |
59 |
|
* |
60 |
|
* 2. path name only format |
61 |
|
* |
62 |
|
* 'gtags --sort --pathname' is equivalent with |
63 |
|
* 'sort -u'. |
64 |
|
* The --pathname option always includes --unique option. |
65 |
*/ |
*/ |
66 |
/* |
/* |
67 |
* This entry corresponds to one record of ctags [-x] format. |
* This entry corresponds to one record of ctags [-x] format. |
94 |
* |
* |
95 |
* i) unique unique or not |
* i) unique unique or not |
96 |
* 0: sort, 1: sort -u |
* 0: sort, 1: sort -u |
97 |
* i) ctags format of the records |
* i) format tag format |
98 |
* 0: ctags -x format, 1: ctags format |
* TAGSORT_CTAGS_X: ctags -x format |
99 |
|
* TAGSORT_CTAGS: ctags format |
100 |
|
* TAGSORT_PATH: path name |
101 |
* i) lines ctags stream |
* i) lines ctags stream |
102 |
* i) entries sort target |
* i) entries sort target |
103 |
* i) entry_count number of entry of the entries |
* i) entry_count number of entry of the entries |
104 |
* i) op output file |
* i) op output file |
105 |
*/ |
*/ |
106 |
static void |
static void |
107 |
put_lines(int unique, int ctags, char *lines, struct dup_entry *entries, int entry_count, FILE *op) |
put_lines(int unique, int format, char *lines, struct dup_entry *entries, int entry_count, FILE *op) |
108 |
{ |
{ |
109 |
int i; |
int i; |
110 |
char last_path[MAXPATHLEN+1]; |
char last_path[MAXPATHLEN+1]; |
111 |
int last_lineno; |
int last_lineno; |
112 |
|
int splits, part_lno, part_path; |
113 |
/* |
/* |
114 |
* ctags format. |
* ctags format. |
115 |
* |
* |
124 |
* |main 227 src/main main() |
* |main 227 src/main main() |
125 |
* |
* |
126 |
*/ |
*/ |
127 |
int splits = ctags ? 3 : 4; |
if (format == TAGSORT_CTAGS) { |
128 |
int part_lno = ctags ? PART_CTAGS_LNO : PART_LNO; |
splits = 3; |
129 |
int part_path = ctags ? PART_CTAGS_PATH : PART_PATH; |
part_lno = PART_CTAGS_LNO; |
130 |
|
part_path = PART_CTAGS_PATH; |
131 |
|
} else if (format == TAGSORT_CTAGS_X) { |
132 |
|
splits = 4; |
133 |
|
part_lno = PART_LNO; |
134 |
|
part_path = PART_PATH; |
135 |
|
} else { |
136 |
|
die("internal error in put_lines."); |
137 |
|
} |
138 |
/* |
/* |
139 |
* Parse and sort ctags [-x] format records. |
* Parse and sort ctags [-x] format records. |
140 |
*/ |
*/ |
184 |
} |
} |
185 |
|
|
186 |
/* |
/* |
187 |
* main body of sorting. |
* ctags_sort: sort ctags [-x] format records |
188 |
* |
* |
189 |
* i) unique 0: sort, 1: sort -u |
* i) unique 0: sort, 1: sort -u |
190 |
* i) ctags 0: ctags -x format, 1: ctags format |
* i) format tag format |
191 |
|
* TAGSORT_CTAGS_X: ctags -x format |
192 |
|
* TAGSORT_CTAGS: ctags format |
193 |
|
* TAGSORT_PATH: path name |
194 |
* i) ip input |
* i) ip input |
195 |
* i) op output |
* i) op output |
196 |
*/ |
*/ |
197 |
void |
static void |
198 |
tagsort(int unique, int ctags, FILE *ip, FILE *op) |
ctags_sort(int unique, int format, FILE *ip, FILE *op) |
199 |
{ |
{ |
200 |
STRBUF *ib = strbuf_open(MAXBUFLEN); |
STRBUF *ib = strbuf_open(MAXBUFLEN); |
201 |
STRBUF *sb = strbuf_open(MAXBUFLEN); |
STRBUF *sb = strbuf_open(MAXBUFLEN); |
224 |
fputc('\n', op); |
fputc('\n', op); |
225 |
} else |
} else |
226 |
put_lines(unique, |
put_lines(unique, |
227 |
ctags, |
format, |
228 |
strbuf_value(sb), |
strbuf_value(sb), |
229 |
varray_assign(vb, 0, 0), |
varray_assign(vb, 0, 0), |
230 |
vb->length, |
vb->length, |
245 |
fputc('\n', op); |
fputc('\n', op); |
246 |
} else |
} else |
247 |
put_lines(unique, |
put_lines(unique, |
248 |
ctags, |
format, |
249 |
strbuf_value(sb), |
strbuf_value(sb), |
250 |
varray_assign(vb, 0, 0), |
varray_assign(vb, 0, 0), |
251 |
vb->length, |
vb->length, |
255 |
strbuf_close(sb); |
strbuf_close(sb); |
256 |
varray_close(vb); |
varray_close(vb); |
257 |
} |
} |
258 |
|
/* |
259 |
|
* path_sort: sort path name only format records |
260 |
|
* |
261 |
|
* i) ip input |
262 |
|
* i) op output |
263 |
|
* |
264 |
|
* This function is applicable in the output of find(1) etc. |
265 |
|
*/ |
266 |
|
static void |
267 |
|
path_sort(FILE *ip, FILE *op) |
268 |
|
{ |
269 |
|
DBOP *dbop; |
270 |
|
STRBUF *ib = strbuf_open(MAXBUFLEN); |
271 |
|
const char *path; |
272 |
|
const char *null = ""; |
273 |
|
|
274 |
|
dbop = dbop_open(NULL, 1, 0600, 0); |
275 |
|
if (!dbop) |
276 |
|
die("cannot make temporary file in path_sort()."); |
277 |
|
while ((path = strbuf_fgets(ib, ip, 0)) != NULL) { |
278 |
|
dbop_put(dbop, path, null); |
279 |
|
} |
280 |
|
for (path = dbop_first(dbop, NULL, NULL, DBOP_KEY); |
281 |
|
path != NULL; |
282 |
|
path = dbop_next(dbop)) { |
283 |
|
fputs(path, op); |
284 |
|
} |
285 |
|
dbop_close(dbop); |
286 |
|
strbuf_close(ib); |
287 |
|
} |
288 |
|
/* |
289 |
|
* tagsort: |
290 |
|
* |
291 |
|
* i) unique 0: sort, 1: sort -u |
292 |
|
* In TAGSORT_PATH format, it is always considered 1. |
293 |
|
* i) format tag format |
294 |
|
* TAGSORT_CTAGS_X: ctags -x format |
295 |
|
* TAGSORT_CTAGS: ctags format |
296 |
|
* TAGSORT_PATH: path name |
297 |
|
* i) ip input |
298 |
|
* i) op output |
299 |
|
*/ |
300 |
|
void |
301 |
|
tagsort(int unique, int format, FILE *ip, FILE *op) |
302 |
|
{ |
303 |
|
if (format == TAGSORT_PATH) |
304 |
|
path_sort(ip, op); |
305 |
|
else |
306 |
|
ctags_sort(unique, format, ip, op); |
307 |
|
} |