]> mj.ucw.cz Git - pciutils.git/commitdiff
Better error messages
authorMartin Mares <mj@ucw.cz>
Fri, 26 Dec 2003 21:39:23 +0000 (21:39 +0000)
committerMartin Mares <mj@ucw.cz>
Fri, 5 May 2006 12:18:10 +0000 (14:18 +0200)
setpci now emits better error messages.
git-archimport-id: mj@ucw.cz--public/pciutils--main--2.2--patch-11

ChangeLog
setpci.c

index 0a7a20458b9be72f4224d7e6223037ea5e9fa11e..cc15ea59ecb244275a649fac7ff3d036c1f8b1e2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
 2003-12-26  Martin Mares  <mj@ucw.cz>
 
+       * setpci.c (main): Better error messages.
+
        * pciutils.h, common.c, setpci.c: Introduced a NONRET macro encapsulating
        the GCC specific __attribute__((noreturn)) and killed the hack with
        redefining __attribute__ on non-GCC compilers.
index 8ee6f3a9be58dc7ce7c1dd460bf6ea82b649764a..5af3589359e54af5b1f4cf4214c52aef442edff8 100644 (file)
--- a/setpci.c
+++ b/setpci.c
@@ -176,8 +176,8 @@ scan_ops(struct op *op)
 }
 
 struct reg_name {
-  int offset;
-  int width;
+  unsigned int offset;
+  unsigned int width;
   const char *name;
 };
 
@@ -250,8 +250,16 @@ static const struct reg_name pci_reg_names[] = {
 };
 
 static void NONRET
-usage(void)
+usage(char *msg, ...)
 {
+  va_list args;
+  va_start(args, msg);
+  if (msg)
+    {
+      fprintf(stderr, "setpci: ");
+      vfprintf(stderr, msg, args);
+      fprintf(stderr, "\n\n");
+    }
   fprintf(stderr,
 "Usage: setpci [<options>] (<device>+ <reg>[=<values>]*)*\n\
 -f\t\tDon't complain if there's nothing to do\n\
@@ -324,18 +332,18 @@ main(int argc, char **argv)
                        argc--; argv++;
                      }
                    else
-                     usage();
+                     usage(NULL);
                    c = "";
                  }
                else
                  arg = NULL;
                if (!parse_generic_option(*e, pacc, arg))
-                 usage();
+                 usage(NULL);
              }
            else
              {
                if (c != d)
-                 usage();
+                 usage(NULL);
                goto next;
              }
          }
@@ -359,7 +367,7 @@ next:
       if (*c == '-')
        {
          if (!c[1] || !strchr("sd", c[1]))
-           usage();
+           usage(NULL);
          if (c[2])
            d = (c[2] == '=') ? c+3 : c+2;
          else if (argc > 1)
@@ -369,7 +377,7 @@ next:
              d = argv[0];
            }
          else
-           usage();
+           usage(NULL);
          if (state != STATE_GOT_FILTER)
            {
              pci_filter_init(pacc, &filter);
@@ -386,11 +394,11 @@ next:
                die("-d: %s", d);
              break;
            default:
-             usage();
+             usage(NULL);
            }
        }
       else if (state == STATE_INIT)
-       usage();
+       usage(NULL);
       else
        {
          if (state == STATE_GOT_FILTER)
@@ -404,7 +412,7 @@ next:
            {
              *d++ = 0;
              if (!*d)
-               usage();
+               usage("Missing value");
              for(e=d, n=1; *e; e++)
                if (*e == ',')
                  n++;
@@ -422,7 +430,7 @@ next:
            {
              *e++ = 0;
              if (e[1])
-               usage();
+               usage("Missing width");
              switch (*e & 0xdf)
                {
                case 'B':
@@ -432,7 +440,7 @@ next:
                case 'L':
                  op->width = 4; break;
                default:
-                 usage();
+                 usage("Invalid width \"%s\"", *e);
                }
            }
          else
@@ -444,8 +452,10 @@ next:
              for(r = pci_reg_names; r->name; r++)
                if (!strcasecmp(r->name, c))
                  break;
-             if (!r->name || e)
-               usage();
+             if (!r->name)
+               usage("Unknown register \"%s\"", c);
+             if (e && op->width != r->width)
+               usage("Explicit width doesn't correspond with the named register \"%s\"", c);
              ll = r->offset;
              op->width = r->width;
            }
@@ -462,31 +472,24 @@ next:
                *e++ = 0;
              ll = strtoul(d, &f, 16);
              lim = max_values[op->width];
-             if (f && *f && (*f != ':') ||
-                 (ll > lim && ll < ~0UL - lim))
-               {
-                 fprintf(stderr, "bad value \"%s\"\n\n", d);
-                 usage();
-               }
+             if (f && *f && *f != ':')
+               usage("Invalid value \"%s\"", d);
+             if (ll > lim && ll < ~0UL - lim)
+               usage("Value \"%s\" is out of range", d);
+             op->values[i].value = ll;
              if (f && *f == ':')
                {
-                 op->values[i].value = ll;
                  d = ++f;
                  ll = strtoul(d, &f, 16);
-                 if (f && *f ||
-                     (ll > lim && ll < ~0UL - lim))
-                   {
-                     fprintf(stderr, "bad value:mask pair \"%s\"\n\n", d);
-                     usage();
-                   }
+                 if (f && *f)
+                   usage("Invalid mask \"%s\"", d);
+                 if (ll > lim && ll < ~0UL - lim)
+                   usage("Mask \"%s\" is out of range", d);
                  op->values[i].mask = ll;
-                 op->values[i].value &= op->values[i].mask;
+                 op->values[i].value &= ll;
                }
              else
-               {
-                 op->values[i].value = ll;
-                 op->values[i].mask = ~0U;
-               }
+               op->values[i].mask = ~0U;
              d = e;
            }
          *last_op = op;
@@ -497,7 +500,7 @@ next:
       argv++;
     }
   if (state == STATE_INIT)
-    usage();
+    usage("No operation specified");
 
   scan_ops(first_op);
   execute(first_op);