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