showpkg.c 5.0 KB

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