showpkg.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  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 version 2 of the GNU General Public
  9. * License version 2 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 <config.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <dpkg.h>
  25. #include <dpkg-db.h>
  26. #include <parsedump.h>
  27. typedef enum { invalid, string, field } itemtype_t;
  28. struct lstitem {
  29. itemtype_t type;
  30. size_t width;
  31. int pad;
  32. char* data;
  33. struct lstitem* next;
  34. };
  35. static struct lstitem* alloclstitem(void) {
  36. struct lstitem* buf;
  37. buf=(struct lstitem*)malloc(sizeof(struct lstitem));
  38. buf->type=invalid;
  39. buf->next=NULL;
  40. buf->data=NULL;
  41. buf->width=0;
  42. buf->pad=0;
  43. return buf;
  44. }
  45. static int parsefield(struct lstitem* cur, const char* fmt, const char* fmtend) {
  46. int len;
  47. const char* ws;
  48. len=fmtend-fmt+1;
  49. ws=(const char*)memchr(fmt, ';', len);
  50. if (ws) {
  51. char* endptr;
  52. long w;
  53. w=strtol(ws+1,&endptr,0);
  54. if (endptr[0]!='}') {
  55. fprintf(stderr, _("invalid character `%c' in field width\n"), *endptr);
  56. return 0;
  57. }
  58. if (w<0) {
  59. cur->pad=1;
  60. cur->width=(size_t)-w;
  61. } else
  62. cur->width=(size_t)w;
  63. len=ws-fmt;
  64. }
  65. cur->type=field;
  66. cur->data=(char*)malloc(len+1);
  67. memcpy(cur->data, fmt, len);
  68. cur->data[len]='\0';
  69. return 1;
  70. }
  71. static int parsestring(struct lstitem* cur, const char* fmt, const char* fmtend) {
  72. int len;
  73. char* write;
  74. len=fmtend-fmt+1;
  75. cur->type=string;
  76. write=cur->data=(char*)malloc(len+1);
  77. while (fmt<=fmtend) {
  78. if (*fmt=='\\') {
  79. fmt++;
  80. switch (*fmt) {
  81. case 'n':
  82. *write='\n';
  83. break;
  84. case 't':
  85. *write='\t';
  86. break;
  87. case 'r':
  88. *write='\r';
  89. break;
  90. case '\\':
  91. default:
  92. *write=*fmt;
  93. break;
  94. }
  95. } else
  96. *write=*fmt;
  97. write++;
  98. fmt++;
  99. }
  100. *write='\0';
  101. return 1;
  102. }
  103. void freeformat(struct lstitem* head) {
  104. struct lstitem* next;
  105. while (head) {
  106. next=head->next;
  107. free(head->data);
  108. free(head);
  109. head=next;
  110. }
  111. }
  112. struct lstitem* parseformat(const char* fmt) {
  113. struct lstitem* head;
  114. struct lstitem* cur;
  115. const char* fmtend;
  116. head=cur=NULL;
  117. while (*fmt) {
  118. if (cur)
  119. cur=cur->next=alloclstitem();
  120. else
  121. head=cur=alloclstitem();
  122. if (fmt[0]=='$' && fmt[1]=='{') {
  123. fmtend=strchr(fmt, '}');
  124. if (!fmtend) {
  125. fprintf(stderr, _("Closing brace missing in format\n"));
  126. freeformat(head);
  127. return NULL;
  128. }
  129. cur->type=field;
  130. if (!parsefield(cur, fmt+2, fmtend-1)) {
  131. freeformat(head);
  132. return NULL;
  133. }
  134. fmt=fmtend+1;
  135. } else {
  136. fmtend=fmt;
  137. do {
  138. fmtend+=1;
  139. fmtend=strchr(fmtend, '$');
  140. } while (fmtend && fmtend[1]!='{');
  141. if (!fmtend)
  142. fmtend=fmt+strlen(fmt);
  143. if (!parsestring(cur, fmt, fmtend-1)) {
  144. freeformat(head);
  145. return NULL;
  146. }
  147. fmt=fmtend;
  148. }
  149. }
  150. return head;
  151. }
  152. #define dumpchain(head) {\
  153. const struct lstitem* ptr = head;\
  154. while (ptr) {\
  155. printf("Type: %s\n", (ptr->type==string) ? "string" : "field");\
  156. printf("Width: %d\n", ptr->width);\
  157. printf("Data: %s\n", ptr->data);\
  158. printf("\n");\
  159. ptr=ptr->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. }