Просмотр исходного кода

libdpkg: Change pkg_format_parse() to take a dpkg_error argument

This fixes two issues at once: the more detailed inner message is
relied back to the more general outter printer which allows a clearer
error message, and it makes the pkg-format module stop printing things
at all which is bad for a library,
Guillem Jover лет назад: 14
Родитель
Сommit
bae98fb22f
4 измененных файлов с 25 добавлено и 15 удалено
  1. 4 2
      dpkg-deb/info.c
  2. 13 11
      lib/dpkg/pkg-format.c
  3. 3 1
      lib/dpkg/pkg-format.h
  4. 5 1
      src/querycmd.c

+ 4 - 2
dpkg-deb/info.c

@@ -271,11 +271,13 @@ do_showinfo(const char *const *argv)
 {
   const char *debar, *dir;
   char *controlfile;
+  struct dpkg_error err;
   struct pkginfo *pkg;
-  struct pkg_format_node *fmt = pkg_format_parse(showformat);
+  struct pkg_format_node *fmt;
 
+  fmt = pkg_format_parse(showformat, &err);
   if (!fmt)
-    ohshit(_("Error in format"));
+    ohshit(_("error in show format: %s"), err.str);
 
   info_prepare(&argv, &debar, &dir, 1);
 

+ 13 - 11
lib/dpkg/pkg-format.c

@@ -29,6 +29,7 @@
 #include <stdio.h>
 
 #include <dpkg/i18n.h>
+#include <dpkg/error.h>
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
 #include <dpkg/parsedump.h>
@@ -66,7 +67,8 @@ pkg_format_node_new(void)
 }
 
 static bool
-parsefield(struct pkg_format_node *cur, const char *fmt, const char *fmtend)
+parsefield(struct pkg_format_node *cur, const char *fmt, const char *fmtend,
+           struct dpkg_error *err)
 {
 	int len;
 	const char *ws;
@@ -81,13 +83,13 @@ parsefield(struct pkg_format_node *cur, const char *fmt, const char *fmtend)
 		errno = 0;
 		w = strtol(ws + 1, &endptr, 0);
 		if (endptr[0] != '}') {
-			fprintf(stderr,
-			        _("invalid character `%c' in field width\n"),
-			       *endptr);
+			dpkg_put_error(err,
+			               _("invalid character '%c' in field width"),
+			               *endptr);
 			return false;
 		}
 		if (w < INT_MIN || w > INT_MAX || errno == ERANGE) {
-			fprintf(stderr, _("field width is out of range\n"));
+			dpkg_put_error(err, _("field width is out of range"));
 			return false;
 		}
 
@@ -109,7 +111,8 @@ parsefield(struct pkg_format_node *cur, const char *fmt, const char *fmtend)
 }
 
 static bool
-parsestring(struct pkg_format_node *cur, const char *fmt, const char *fmtend)
+parsestring(struct pkg_format_node *cur, const char *fmt, const char *fmtend,
+            struct dpkg_error *err)
 {
 	int len;
 	char *write;
@@ -160,7 +163,7 @@ pkg_format_free(struct pkg_format_node *head)
 }
 
 struct pkg_format_node *
-pkg_format_parse(const char *fmt)
+pkg_format_parse(const char *fmt, struct dpkg_error *err)
 {
 	struct pkg_format_node *head;
 	struct pkg_format_node *cur;
@@ -177,13 +180,12 @@ pkg_format_parse(const char *fmt)
 		if (fmt[0] == '$' && fmt[1] == '{') {
 			fmtend = strchr(fmt, '}');
 			if (!fmtend) {
-				fprintf(stderr,
-				      _("Closing brace missing in format\n"));
+				dpkg_put_error(err, _("missing closing brace"));
 				pkg_format_free(head);
 				return NULL;
 			}
 
-			if (!parsefield(cur, fmt + 2, fmtend - 1)) {
+			if (!parsefield(cur, fmt + 2, fmtend - 1, err)) {
 				pkg_format_free(head);
 				return NULL;
 			}
@@ -198,7 +200,7 @@ pkg_format_parse(const char *fmt)
 			if (!fmtend)
 				fmtend = fmt + strlen(fmt);
 
-			if (!parsestring(cur, fmt, fmtend - 1)) {
+			if (!parsestring(cur, fmt, fmtend - 1, err)) {
 				pkg_format_free(head);
 				return NULL;
 			}

+ 3 - 1
lib/dpkg/pkg-format.h

@@ -22,6 +22,7 @@
 #define LIBDPKG_PKG_FORMAT_H
 
 #include <dpkg/macros.h>
+#include <dpkg/error.h>
 #include <dpkg/dpkg-db.h>
 
 DPKG_BEGIN_DECLS
@@ -34,7 +35,8 @@ DPKG_BEGIN_DECLS
 
 struct pkg_format_node;
 
-struct pkg_format_node *pkg_format_parse(const char *fmt);
+struct pkg_format_node *pkg_format_parse(const char *fmt,
+                                         struct dpkg_error *err);
 void pkg_format_free(struct pkg_format_node *head);
 void pkg_format_show(const struct pkg_format_node *head,
                      struct pkginfo *pkg, struct pkgbin *pkgbin);

+ 5 - 1
src/querycmd.c

@@ -494,13 +494,17 @@ enqperpackage(const char *const *argv)
 static int
 showpackages(const char *const *argv)
 {
+  struct dpkg_error err;
   struct pkg_array array;
   struct pkginfo *pkg;
-  struct pkg_format_node *fmt = pkg_format_parse(showformat);
+  struct pkg_format_node *fmt;
   int i;
   int failures = 0;
 
+  fmt = pkg_format_parse(showformat, &err);
   if (!fmt) {
+    notice(_("error in show format: %s"), err.str);
+    dpkg_error_destroy(&err);
     failures++;
     return failures;
   }