showpkg.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * dpkg-query - program to query the package database
  3. * showpkg.c - customizable package listing
  4. *
  5. * Copyright (C) 2001 Wichert Akkerman <wakkerma@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <config.h>
  25. #include <dpkg.h>
  26. #include <dpkg-db.h>
  27. #include <parsedump.h>
  28. #include "main.h"
  29. typedef enum { invalid, string, field } itemtype_t;
  30. struct lstitem {
  31. itemtype_t type;
  32. size_t width;
  33. int pad;
  34. char* data;
  35. struct lstitem* next;
  36. };
  37. static struct lstitem* alloclstitem() {
  38. struct lstitem* buf;
  39. buf=(struct lstitem*)malloc(sizeof(struct lstitem));
  40. buf->type=invalid;
  41. buf->next=NULL;
  42. buf->data=NULL;
  43. buf->width=0;
  44. buf->pad=0;
  45. return buf;
  46. }
  47. static int parsefield(struct lstitem* cur, const char* fmt, const char* fmtend) {
  48. int len;
  49. const char* ws;
  50. len=fmtend-fmt+1;
  51. ws=(const char*)memchr(fmt, ';', len);
  52. if (ws) {
  53. char* endptr;
  54. long w;
  55. w=strtol(ws+1,&endptr,0);
  56. if (endptr[0]!='}') {
  57. fprintf(stderr, _("invalid character `%c' in field width\n"), *endptr);
  58. return 0;
  59. }
  60. if (w<0) {
  61. cur->pad=1;
  62. cur->width=(size_t)-w;
  63. } else
  64. cur->width=(size_t)w;
  65. len=ws-fmt;
  66. }
  67. cur->type=field;
  68. cur->data=(char*)malloc(len+1);
  69. memcpy(cur->data, fmt, len);
  70. cur->data[len]='\0';
  71. return 1;
  72. }
  73. static int parsestring(struct lstitem* cur, const char* fmt, const char* fmtend) {
  74. int len;
  75. char* write;
  76. len=fmtend-fmt+1;
  77. cur->type=string;
  78. write=cur->data=(char*)malloc(len+1);
  79. while (fmt<=fmtend) {
  80. if (*fmt=='\\') {
  81. fmt++;
  82. switch (*fmt) {
  83. case 'n':
  84. *write='\n';
  85. break;
  86. case 't':
  87. *write='\t';
  88. break;
  89. case 'r':
  90. *write='\r';
  91. break;
  92. case '\\':
  93. default:
  94. *write=*fmt;
  95. break;
  96. }
  97. } else
  98. *write=*fmt;
  99. write++;
  100. fmt++;
  101. }
  102. *write='\0';
  103. return 1;
  104. }
  105. void freeformat(struct lstitem* head) {
  106. struct lstitem* next;
  107. while (head) {
  108. next=head->next;
  109. free(head->data);
  110. free(head);
  111. head=next;
  112. }
  113. }
  114. struct lstitem* parseformat(const char* fmt) {
  115. struct lstitem* head;
  116. struct lstitem* cur;
  117. const char* fmtend;
  118. head=cur=NULL;
  119. while (*fmt) {
  120. if (cur)
  121. cur=cur->next=alloclstitem();
  122. else
  123. head=cur=alloclstitem();
  124. if (fmt[0]=='$' && fmt[1]=='{') {
  125. fmtend=strchr(fmt, '}');
  126. if (!fmtend) {
  127. fprintf(stderr, _("Closing brace missing in format\n"));
  128. freeformat(head);
  129. return NULL;
  130. }
  131. cur->type=field;
  132. if (!parsefield(cur, fmt+2, fmtend-1)) {
  133. freeformat(head);
  134. return NULL;
  135. }
  136. fmt=fmtend+1;
  137. } else {
  138. fmtend=fmt;
  139. do {
  140. fmtend+=1;
  141. fmtend=strchr(fmtend, '$');
  142. } while (fmtend && fmtend[1]!='{');
  143. if (!fmtend)
  144. fmtend=fmt+strlen(fmt);
  145. if (!parsestring(cur, fmt, fmtend-1)) {
  146. freeformat(head);
  147. return NULL;
  148. }
  149. fmt=fmtend;
  150. }
  151. }
  152. return head;
  153. }
  154. static void dumpchain(struct lstitem* head) {
  155. while (head) {
  156. printf("Type: %s\n", (head->type==string) ? "string" : "field");
  157. printf("Width: %d\n", head->width);
  158. printf("Data: %s\n", head->data);
  159. printf("\n");
  160. head=head->next;
  161. }
  162. }
  163. void show1package(const struct lstitem* head, struct pkginfo *pkg) {
  164. struct varbuf vb, fb, wb;
  165. /* Make sure we have package info available, even if it's all empty. */
  166. if (!pkg->installed.valid)
  167. blankpackageperfile(&pkg->installed);
  168. varbufinit(&vb);
  169. varbufinit(&fb);
  170. varbufinit(&wb);
  171. while (head) {
  172. int ok;
  173. char fmt[16];
  174. ok=0;
  175. if (head->width>0)
  176. snprintf(fmt,16,"%%%s%ds",
  177. ((head->pad) ? "-" : ""), head->width);
  178. else
  179. strcpy(fmt, "%s");
  180. if (head->type==string) {
  181. varbufprintf(&fb, fmt, head->data);
  182. ok=1;
  183. } else if (head->type==field) {
  184. const struct fieldinfo* fip;
  185. for (fip=fieldinfos; fip->name; fip++)
  186. if (strcasecmp(head->data, fip->name)==0) {
  187. size_t len;
  188. char* i;
  189. fip->wcall(&wb,pkg,&pkg->installed,fip);
  190. if (!wb.used)
  191. break;
  192. /* Bugger, wcall adds the fieldname and a trailing newline we
  193. * do not need. We should probably improve wcall to only do that
  194. * optionally, but this will do for now (ie this is a TODO)
  195. */
  196. wb.buf[wb.used-1]='\0';
  197. i=strchr(wb.buf, ':')+2;
  198. len=strlen(i)+1;
  199. memmove(wb.buf, i, len);
  200. varbufprintf(&fb, fmt, wb.buf);
  201. varbufreset(&wb);
  202. ok=1;
  203. break;
  204. }
  205. if (!fip && pkg->installed.valid) {
  206. const struct arbitraryfield* afp;
  207. for (afp=pkg->installed.arbs; afp; afp=afp->next)
  208. if (strcasecmp(head->data, afp->name)==0) {
  209. varbufprintf(&fb, fmt, afp->value);
  210. ok=1;
  211. break;
  212. }
  213. }
  214. }
  215. if (ok) {
  216. size_t len=strlen(fb.buf);
  217. if ((head->width>0) && (len>head->width))
  218. len=head->width;
  219. varbufaddbuf(&vb, fb.buf, len);
  220. }
  221. varbufreset(&fb);
  222. head=head->next;
  223. }
  224. if (vb.buf) {
  225. varbufaddc(&vb, '\0');
  226. fputs(vb.buf,stdout);
  227. }
  228. varbuffree(&wb);
  229. varbuffree(&fb);
  230. varbuffree(&vb);
  231. }