-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.c
334 lines (291 loc) · 9.37 KB
/
output.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Results sort and output
* Copyright © 2007-2011 Andrew Savchenko
*
* This file is part of symlookup.
*
* symlookup is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation
*
* symlookup is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License version 3 for more details.
*
* You should have received a copy of the GNU General Public License version 3
* along with symlookup. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <string.h>
#include <regex.h>
#include "symlookup.h"
#include "safemem.h"
#include "rpmutils.h"
#include "output.h"
const char* mtypes_str[M_TYPES]; /* match field names */
const char* const str_not_found = "No matches found.";
/* create header for sorted output */
static void construct_header()
{
for (unsigned int i=0; i<opt.sort.cnt; i++)
{
fputs(mtypes_str[opt.sort.seq[i]],stdout);
if (i != opt.sort.cnt-1)
putchar('\t');
else
putchar('\n');
}
}
/* Comparison function for ordinary matches.
It must be called only if ordinary sort is enabled (opt.sort.cnt),
opt.sort.seq must NOT contain mtype.rpm if (!opt.rpm) */
static int compare_matches(const void* const a, const void* const b)
{
// statically initialize to shut up gcc
static int result = 0;
const char** const s1 = *((const char*** const)a);
const char** const s2 = *((const char*** const)b);
/* compare match results subsequently */
for (unsigned int i=0; i < opt.sort.cnt; i++)
/* match is an array (char**) with 3 elements,
their order is in concordance with mtype indexes
*/
if (( result = strcmp( s1[opt.sort.seq[i]],
s2[opt.sort.seq[i]] ) ))
break;
return result;
}
/* Comparison function for matches within fixed level.
It must be called only from terse output subsystem,
opt.sort.seq must NOT contain mtype.rpm if (!opt.rpm).
External variable comparision_level is used to denote level to compare,
as it can't be passed to the function directly */
unsigned int comparision_level;
static int compare_level(const void* const a, const void* const b)
{
const char** const s1 = *((const char*** const)a);
const char** const s2 = *((const char*** const)b);
/* compare single level match results */
return strcmp(s1[opt.sort.seq[comparision_level]],
s2[opt.sort.seq[comparision_level]]);
}
/* Print subtree in a terse mode:
* at each level there're no duplicated nodes.
* match -- match array to process
* count -- number of elements in match array
* start -- index of the first element to process
* level -- highest level of a tree to process (should be valid) */
static unsigned int
terse_output(char*** const match, const unsigned int count,
const unsigned int start, const unsigned int level)
{
unsigned int end;
unsigned int *type = opt.sort.seq;
// find the end of the terse block
for (end = start; end < count - 1; end++)
if (strcmp(match[end][type[level-1]], match[end+1][type[level-1]]))
break;
for (unsigned int j = level; j < opt.sort.cnt; j++)
{
// all terse levels with (depth > 1) must be resorted
if (j > level)
{
comparision_level = j;
qsort(match + start, end - start + 1, sizeof(char**), compare_level);
}
for (unsigned int i = start; i <= end; i++)
{
// skip previously printed element
if (i > start && !strcmp(match[i][type[j]], match[i-1][type[j]]))
continue;
for (unsigned int z=0; z < j; z++)
putchar('\t');
puts(match[i][type[j]]);
}
}
return end;
}
/* Output sorted results.
* match -- match structure
* count -- number of matches
* pattern -- match pattern, only != NULL when match
* search field engaged */
static void print_result(char*** const match, const unsigned int count,
const char* const pattern)
{
//empty set?
if (!count) {
if (opt.verb)
puts(str_not_found);
return;
}
//header for match created separately
if (opt.hdr && !opt.sort.match)
construct_header();
/* sort results */
qsort(match, count, sizeof(char**), compare_matches);
unsigned int *type = opt.sort.seq;
/* output sorted table */
if (opt.tbl)
{
for (unsigned int i=0; i < count; i++)
{
if (pattern) {
fputs(pattern, stdout);
putchar('\t');
}
//cycle through the fields,
//number depends on CLI options
for (unsigned int j=0; j < opt.sort.cnt; j++)
{
fputs(match[i][type[j]], stdout);
if (j != opt.sort.cnt-1)
putchar('\t');
else
putchar('\n');
}
}
return;
}
char *prev_str[M_TYPES], // array of pointers to previously printed strings
*str; // string for temporal reference
// top prev str layer should be cleaned outside the main loop
prev_str[0] = NULL;
/* tree output */
for (unsigned int i=0; i < count; i++)
for (unsigned int j=0; j < opt.sort.cnt; j++)
{
str = match[i][type[j]];
// skip previously printed element
if (prev_str[j] && !strcmp(prev_str[j], str))
continue;
// clear previous string flag from inner layer
if (j < opt.sort.cnt - 1)
prev_str[j+1] = NULL;
for (unsigned int z=0; z < j; z++)
putchar('\t');
puts(str);
prev_str[j] = str; // save current string
/* terse output section */
if (type[j] == mtype.file && j < opt.sort.cnt - 1)
{
i = terse_output(match, count, i, j+1);
break;
}
}
}
/* sort if required and output results */
void sort_output()
{
if (opt.verb >= V_VERBOSE)
puts("--> Preparing for output results");
/* sort by match is done separately */
if (opt.sort.match)
{
if (opt.hdr)
{
fputs("PATTERN\t\t",stdout);
construct_header();
}
for (unsigned int i=0; i < symbol.size; i++) {
if (!symbol.match_count[i] && !opt.verb)
continue;
if (!opt.tbl)
printf("===> match(es) for pattern '%s':\n", symbol.str[i]);
/* use standard output facility */
print_result(symbol.match[i], symbol.match_count[i], symbol.str[i]);
}
} // opt.sort.match
/* ordinary sort */
else
print_result(match_arr.match, match_arr.count, NULL);
}
/* Output unsorted results for ebuild search,
* take care of special case when ebuild was enabled by user,
* but disabled later due to an error. */
#ifdef HAVE_PORTAGE
void ebuild_unsorted_output()
{
if (opt.verb >= V_VERBOSE)
puts("--> Unsorted ebuild output");
if (!match_arr.count) {
if (opt.verb)
puts(str_not_found);
return;
}
for (unsigned int i=0; i < match_arr.count; i++)
{
fputs(match_arr.match[i][mtype.file], stdout);
if (opt.ebuild == 1
#ifdef HAVE_RPM
|| opt.rpm
#endif //HAVE_RPM
)
fputs(" (", stdout);
if (opt.ebuild == 1) {
fputs("ebuild: ", stdout);
fputs(match_arr.match[i][mtype.ebuild], stdout);
}
#ifdef HAVE_RPM
if (opt.ebuild == 1
&& opt.rpm
)
fputs(", ", stdout);
if (opt.rpm) {
fputs("rpm: ", stdout);
fputs(match_arr.match[i][mtype.rpm], stdout);
}
#endif //HAVE_RPM
if (opt.ebuild == 1
#ifdef HAVE_RPM
|| opt.rpm
#endif //HAVE_RPM
)
putchar(')');
fputs(": ", stdout);
puts(match_arr.match[i][mtype.symbol]);
}
}
#endif //HAVE_PORTAGE
/* initialize output (header, formats) */
void init_output()
{
/* init mtypes_str, required only for headers */
if (opt.hdr)
{
mtypes_str[mtype.symbol] = alloc_str("SYMBOL");
mtypes_str[mtype.file] = alloc_str("FILE");
#ifdef HAVE_RPM
if (opt.rpm)
mtypes_str[mtype.rpm] = alloc_str("RPM");
#endif //HAVE_RPM
#ifdef HAVE_PORTAGE
if (opt.ebuild)
mtypes_str[mtype.ebuild] = alloc_str("EBUILD");
#endif //HAVE_PORTAGE
}
/* show unsorted output header for the first time */
if (opt.hdr && !opt.sort.cnt)
{
// output as: file [ebuild] [rpm] symbol
fputs(mtypes_str[mtype.file], stdout);
putchar('\t');
#ifdef HAVE_PORTAGE
if (opt.ebuild) {
fputs(mtypes_str[mtype.ebuild], stdout);
putchar('\t');
}
#endif //HAVE_PORTAGE
#ifdef HAVE_RPM
if (opt.rpm) {
fputs(mtypes_str[mtype.rpm], stdout);
putchar('\t');
}
#endif //HAVE_RPM
puts(mtypes_str[mtype.symbol]);
}
}