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
|
/*
* libdpkg - Debian packaging suite library routines
* showpkg.c - customizable package listing
*
* Copyright (C) 2001 Wichert Akkerman <wakkerma@debian.org>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* This 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 for more details.
*
* You should have received a copy of the GNU General Public
* License along with dpkg; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dpkg.h>
#include <dpkg-db.h>
#include <parsedump.h>
typedef enum { invalid, string, field } itemtype_t;
struct lstitem {
itemtype_t type;
size_t width;
int pad;
char* data;
struct lstitem* next;
};
static struct lstitem* alloclstitem(void) {
struct lstitem* buf;
buf=(struct lstitem*)malloc(sizeof(struct lstitem));
buf->type=invalid;
buf->next=NULL;
buf->data=NULL;
buf->width=0;
buf->pad=0;
return buf;
}
static int parsefield(struct lstitem* cur, const char* fmt, const char* fmtend) {
int len;
const char* ws;
len=fmtend-fmt+1;
ws=(const char*)memchr(fmt, ';', len);
if (ws) {
char* endptr;
long w;
w=strtol(ws+1,&endptr,0);
if (endptr[0]!='}') {
fprintf(stderr, _("invalid character `%c' in field width\n"), *endptr);
return 0;
}
if (w<0) {
cur->pad=1;
cur->width=(size_t)-w;
} else
cur->width=(size_t)w;
len=ws-fmt;
}
cur->type=field;
cur->data=(char*)malloc(len+1);
memcpy(cur->data, fmt, len);
cur->data[len]='\0';
return 1;
}
static int parsestring(struct lstitem* cur, const char* fmt, const char* fmtend) {
int len;
char* write;
len=fmtend-fmt+1;
cur->type=string;
write=cur->data=(char*)malloc(len+1);
while (fmt<=fmtend) {
if (*fmt=='\\') {
fmt++;
switch (*fmt) {
case 'n':
*write='\n';
break;
case 't':
*write='\t';
break;
case 'r':
*write='\r';
break;
case '\\':
default:
*write=*fmt;
break;
}
} else
*write=*fmt;
write++;
fmt++;
}
*write='\0';
return 1;
}
void freeformat(struct lstitem* head) {
struct lstitem* next;
while (head) {
next=head->next;
free(head->data);
free(head);
head=next;
}
}
struct lstitem* parseformat(const char* fmt) {
struct lstitem* head;
struct lstitem* cur;
const char* fmtend;
head=cur=NULL;
while (*fmt) {
if (cur)
cur=cur->next=alloclstitem();
else
head=cur=alloclstitem();
if (fmt[0]=='$' && fmt[1]=='{') {
fmtend=strchr(fmt, '}');
if (!fmtend) {
fprintf(stderr, _("Closing brace missing in format\n"));
freeformat(head);
return NULL;
}
cur->type=field;
if (!parsefield(cur, fmt+2, fmtend-1)) {
freeformat(head);
return NULL;
}
fmt=fmtend+1;
} else {
fmtend=fmt;
do {
fmtend+=1;
fmtend=strchr(fmtend, '$');
} while (fmtend && fmtend[1]!='{');
if (!fmtend)
fmtend=fmt+strlen(fmt);
if (!parsestring(cur, fmt, fmtend-1)) {
freeformat(head);
return NULL;
}
fmt=fmtend;
}
}
return head;
}
#define dumpchain(head) {\
const struct lstitem* ptr = head;\
while (ptr) {\
printf("Type: %s\n", (ptr->type==string) ? "string" : "field");\
printf("Width: %d\n", ptr->width);\
printf("Data: %s\n", ptr->data);\
printf("\n");\
ptr=ptr->next;\
}\
}\
void show1package(const struct lstitem* head, struct pkginfo *pkg) {
struct varbuf vb, fb, wb;
/* Make sure we have package info available, even if it's all empty. */
if (!pkg->installed.valid)
blankpackageperfile(&pkg->installed);
varbufinit(&vb);
varbufinit(&fb);
varbufinit(&wb);
while (head) {
int ok;
char fmt[16];
ok=0;
if (head->width>0)
snprintf(fmt,16,"%%%s%ds",
((head->pad) ? "-" : ""), head->width);
else
strcpy(fmt, "%s");
if (head->type==string) {
varbufprintf(&fb, fmt, head->data);
ok=1;
} else if (head->type==field) {
const struct fieldinfo* fip;
for (fip=fieldinfos; fip->name; fip++)
if (strcasecmp(head->data, fip->name)==0) {
fip->wcall(&wb,pkg,&pkg->installed,0,fip);
if (!wb.used)
break;
varbufaddc(&wb, '\0');
varbufprintf(&fb, fmt, wb.buf);
varbufreset(&wb);
ok=1;
break;
}
if (!fip && pkg->installed.valid) {
const struct arbitraryfield* afp;
for (afp=pkg->installed.arbs; afp; afp=afp->next)
if (strcasecmp(head->data, afp->name)==0) {
varbufprintf(&fb, fmt, afp->value);
ok=1;
break;
}
}
}
if (ok) {
size_t len=strlen(fb.buf);
if ((head->width>0) && (len>head->width))
len=head->width;
varbufaddbuf(&vb, fb.buf, len);
}
varbufreset(&fb);
head=head->next;
}
if (vb.buf) {
varbufaddc(&vb, '\0');
fputs(vb.buf,stdout);
}
varbuffree(&wb);
varbuffree(&fb);
varbuffree(&vb);
}
|