showpkg.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * showpkg.c - customizable package listing
  4. *
  5. * Copyright © 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 <dpkg-i18n.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <dpkg.h>
  26. #include <dpkg-db.h>
  27. #include <parsedump.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(void) {
  37. struct lstitem* buf;
  38. buf = m_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 = 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 = m_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 = m_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. 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 = VARBUF_INIT, fb = VARBUF_INIT, wb = VARBUF_INIT;
  164. /* Make sure we have package info available, even if it's all empty. */
  165. if (!pkg->installed.valid)
  166. blankpackageperfile(&pkg->installed);
  167. while (head) {
  168. int ok;
  169. char fmt[16];
  170. ok=0;
  171. if (head->width>0)
  172. snprintf(fmt,16,"%%%s%zds",
  173. ((head->pad) ? "-" : ""), head->width);
  174. else
  175. strcpy(fmt, "%s");
  176. if (head->type==string) {
  177. varbufprintf(&fb, fmt, head->data);
  178. ok=1;
  179. } else if (head->type==field) {
  180. const struct fieldinfo* fip;
  181. for (fip=fieldinfos; fip->name; fip++)
  182. if (strcasecmp(head->data, fip->name)==0) {
  183. fip->wcall(&wb,pkg,&pkg->installed,0,fip);
  184. varbufaddc(&wb, '\0');
  185. varbufprintf(&fb, fmt, wb.buf);
  186. varbufreset(&wb);
  187. ok=1;
  188. break;
  189. }
  190. if (!fip->name && pkg->installed.valid) {
  191. const struct arbitraryfield* afp;
  192. for (afp=pkg->installed.arbs; afp; afp=afp->next)
  193. if (strcasecmp(head->data, afp->name)==0) {
  194. varbufprintf(&fb, fmt, afp->value);
  195. ok=1;
  196. break;
  197. }
  198. }
  199. }
  200. if (ok) {
  201. size_t len=strlen(fb.buf);
  202. if ((head->width>0) && (len>head->width))
  203. len=head->width;
  204. varbufaddbuf(&vb, fb.buf, len);
  205. }
  206. varbufreset(&fb);
  207. head=head->next;
  208. }
  209. if (vb.buf) {
  210. varbufaddc(&vb, '\0');
  211. fputs(vb.buf,stdout);
  212. }
  213. varbuffree(&wb);
  214. varbuffree(&fb);
  215. varbuffree(&vb);
  216. }