showpkg.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. 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. return buf;
  44. }
  45. static void parsefield(struct lstitem* cur, const char* fmt, const char* fmtend) {
  46. int len;
  47. len=fmtend-fmt+1;
  48. cur->type=field;
  49. cur->data=(char*)malloc(len+1);
  50. memcpy(cur->data, fmt, len);
  51. cur->data[len]='\0';
  52. }
  53. static void parsestring(struct lstitem* cur, const char* fmt, const char* fmtend) {
  54. int len;
  55. char* write;
  56. len=fmtend-fmt+1;
  57. cur->type=string;
  58. write=cur->data=(char*)malloc(len+1);
  59. while (fmt<fmtend) {
  60. if (*fmt=='\\') {
  61. fmt++;
  62. switch (*fmt) {
  63. case 'n':
  64. *write='\n';
  65. break;
  66. case 't':
  67. *write='\t';
  68. break;
  69. case 'r':
  70. *write='\r';
  71. break;
  72. case '\\':
  73. default:
  74. *write=*fmt;
  75. break;
  76. }
  77. } else
  78. *write=*fmt;
  79. write++;
  80. fmt++;
  81. }
  82. *write='\0';
  83. }
  84. void freeformat(struct lstitem* head) {
  85. struct lstitem* next;
  86. while (head) {
  87. next=head->next;
  88. free(head->data);
  89. free(head);
  90. head=next;
  91. }
  92. }
  93. struct lstitem* parseformat(const char* fmt) {
  94. struct lstitem* head;
  95. struct lstitem* cur;
  96. const char* fmtend;
  97. head=cur=NULL;
  98. while (*fmt) {
  99. if (cur)
  100. cur=cur->next=alloclstitem();
  101. else
  102. head=cur=alloclstitem();
  103. if (fmt[0]=='$' && fmt[1]=='{') {
  104. fmtend=strchr(fmt, '}'); // TODO: check for not-found
  105. if (!fmtend) {
  106. fprintf(stderr, _("Closing brace missing in format\n"));
  107. freeformat(head);
  108. return NULL;
  109. }
  110. cur->type=field;
  111. parsefield(cur, fmt+2, fmtend-1);
  112. fmt=fmtend+1;
  113. } else {
  114. fmtend=fmt;
  115. do {
  116. fmtend+=1;
  117. fmtend=strchr(fmtend, '$');
  118. } while (fmtend && fmtend[1]!='{');
  119. if (!fmtend)
  120. fmtend=fmt+strlen(fmt);
  121. parsestring(cur, fmt, fmtend-1);
  122. fmt=fmtend;
  123. }
  124. }
  125. return head;
  126. }
  127. static void dumpchain(struct lstitem* head) {
  128. while (head) {
  129. printf("Type: %s\n", (head->type==string) ? "string" : "field");
  130. printf("Width: %d\n", head->width);
  131. printf("Data: %s\n", head->data);
  132. printf("\n");
  133. head=head->next;
  134. }
  135. }
  136. void show1package(const struct lstitem* head, struct pkginfo *pkg) {
  137. struct varbuf vb, fb;
  138. /* Make sure we have package info available, even if it's all empty. */
  139. if (!pkg->installed.valid)
  140. blankpackageperfile(&pkg->installed);
  141. varbufinit(&vb);
  142. varbufinit(&fb);
  143. while (head) {
  144. int ok;
  145. ok=0;
  146. if (head->type==string) {
  147. varbufprintf(&fb, "%s", head->data);
  148. ok=1;
  149. } else if (head->type==field) {
  150. const struct fieldinfo* fip;
  151. for (fip=fieldinfos; fip->name; fip++)
  152. if (strcasecmp(head->data, fip->name)==0) {
  153. size_t len;
  154. char* i;
  155. fip->wcall(&fb,pkg,&pkg->installed,fip);
  156. /* Bugger, wcall adds the fieldname and a trailing newline we
  157. * do not need. We should probably improve wcall to only do that
  158. * optionally, but this will do for now (ie this is a TODO)
  159. */
  160. fb.buf[fb.used-1]='\0';
  161. i=strchr(fb.buf, ':')+2;
  162. len=strlen(i)+1;
  163. memmove(fb.buf, i, len);
  164. ok=1;
  165. break;
  166. }
  167. if (!fip && pkg->installed.valid) {
  168. const struct arbitraryfield* afp;
  169. for (afp=pkg->installed.arbs; afp; afp=afp->next)
  170. if (strcasecmp(head->data, afp->name)==0) {
  171. varbufprintf(&fb, "%s", afp->value);
  172. ok=1;
  173. break;
  174. }
  175. }
  176. }
  177. if (ok) {
  178. size_t len=strlen(fb.buf);
  179. if ((head->width>0) && (len>head->width))
  180. len=head->width;
  181. varbufaddbuf(&vb, fb.buf, len);
  182. }
  183. varbufreset(&fb);
  184. head=head->next;
  185. }
  186. if (vb.buf) {
  187. varbufaddc(&vb, '\0');
  188. fputs(vb.buf,stdout);
  189. }
  190. varbuffree(&fb);
  191. varbuffree(&vb);
  192. }