showpkg.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 <compat.h>
  22. #include <dpkg-i18n.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <dpkg.h>
  27. #include <dpkg-db.h>
  28. #include <parsedump.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(void) {
  38. struct lstitem* buf;
  39. buf = m_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 = 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 = m_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 = m_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. 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. #define dumpchain(head) {\
  154. const struct lstitem* ptr = head;\
  155. while (ptr) {\
  156. printf("Type: %s\n", (ptr->type==string) ? "string" : "field");\
  157. printf("Width: %d\n", ptr->width);\
  158. printf("Data: %s\n", ptr->data);\
  159. printf("\n");\
  160. ptr=ptr->next;\
  161. }\
  162. }\
  163. void show1package(const struct lstitem* head, struct pkginfo *pkg) {
  164. struct varbuf vb = VARBUF_INIT, fb = VARBUF_INIT, wb = VARBUF_INIT;
  165. /* Make sure we have package info available, even if it's all empty. */
  166. if (!pkg->installed.valid)
  167. blankpackageperfile(&pkg->installed);
  168. while (head) {
  169. int ok;
  170. char fmt[16];
  171. ok=0;
  172. if (head->width>0)
  173. snprintf(fmt,16,"%%%s%zds",
  174. ((head->pad) ? "-" : ""), head->width);
  175. else
  176. strcpy(fmt, "%s");
  177. if (head->type==string) {
  178. varbufprintf(&fb, fmt, head->data);
  179. ok=1;
  180. } else if (head->type==field) {
  181. const struct fieldinfo* fip;
  182. for (fip=fieldinfos; fip->name; fip++)
  183. if (strcasecmp(head->data, fip->name)==0) {
  184. fip->wcall(&wb,pkg,&pkg->installed,0,fip);
  185. varbufaddc(&wb, '\0');
  186. varbufprintf(&fb, fmt, wb.buf);
  187. varbufreset(&wb);
  188. ok=1;
  189. break;
  190. }
  191. if (!fip->name && pkg->installed.valid) {
  192. const struct arbitraryfield* afp;
  193. for (afp=pkg->installed.arbs; afp; afp=afp->next)
  194. if (strcasecmp(head->data, afp->name)==0) {
  195. varbufprintf(&fb, fmt, afp->value);
  196. ok=1;
  197. break;
  198. }
  199. }
  200. }
  201. if (ok) {
  202. size_t len=strlen(fb.buf);
  203. if ((head->width>0) && (len>head->width))
  204. len=head->width;
  205. varbufaddbuf(&vb, fb.buf, len);
  206. }
  207. varbufreset(&fb);
  208. head=head->next;
  209. }
  210. if (vb.buf) {
  211. varbufaddc(&vb, '\0');
  212. fputs(vb.buf,stdout);
  213. }
  214. varbuffree(&wb);
  215. varbuffree(&fb);
  216. varbuffree(&vb);
  217. }