]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - systemv/lpinfo.c
Merge changes from CUPS 1.4svn-r7961.
[thirdparty/cups.git] / systemv / lpinfo.c
index 41ffad2063d61577f7e2d0bb68a723886a2ce263..babe6a2ad3f5b21bcfb28511725aee34763b662b 100644 (file)
@@ -1,29 +1,21 @@
 /*
- * "$Id: lpinfo.c 4906 2006-01-10 20:53:28Z mike $"
+ * "$Id: lpinfo.c 7810 2008-07-29 01:11:15Z mike $"
  *
  *   "lpinfo" command for the Common UNIX Printing System (CUPS).
  *
+ *   Copyright 2007 by Apple Inc.
  *   Copyright 1997-2006 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
- *   "LICENSE.txt" which should have been included with this file.  If this
- *   file is missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  * Contents:
  *
  *   main()         - Parse options and show information.
+ *   device_cb      - Device callback.
  *   show_devices() - Show available devices.
  *   show_models()  - Show available PPDs.
  */
  * Local functions...
  */
 
+static void    device_cb(const char *device_clas, const char *device_id,
+                         const char *device_info,
+                         const char *device_make_and_model,
+                         const char *device_uri, const char *device_location,
+                         void *user_data);
 static int     show_devices(http_t *, int);
 static int     show_models(http_t *, int);
 
@@ -62,6 +59,8 @@ main(int  argc,                               /* I - Number of command-line arguments */
   int          long_status;            /* Long listing? */
 
 
+  _cupsSetLocale(argv);
+
   http        = NULL;
   long_status = 0;
 
@@ -76,7 +75,7 @@ main(int  argc,                               /* I - Number of command-line arguments */
            if (http)
              httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
 #else
-            _cupsLangPrintf(stderr, NULL,
+            _cupsLangPrintf(stderr,
                            _("%s: Sorry, no encryption support compiled in!\n"),
                            argv[0]);
 #endif /* HAVE_SSL */
@@ -94,7 +93,7 @@ main(int  argc,                               /* I - Number of command-line arguments */
 
              if (http == NULL)
              {
-               _cupsLangPrintf(stderr, NULL,
+               _cupsLangPrintf(stderr,
                                _("lpinfo: Unable to connect to server: %s\n"),
                                strerror(errno));
                return (1);
@@ -113,7 +112,7 @@ main(int  argc,                             /* I - Number of command-line arguments */
 
              if (http == NULL)
              {
-               _cupsLangPrintf(stderr, NULL,
+               _cupsLangPrintf(stderr,
                                _("lpinfo: Unable to connect to server: %s\n"),
                                strerror(errno));
                return (1);
@@ -139,7 +138,7 @@ main(int  argc,                             /* I - Number of command-line arguments */
 
              if (i >= argc)
              {
-               _cupsLangPuts(stderr, NULL,
+               _cupsLangPuts(stderr,
                              _("Error: need hostname after \'-h\' option!\n"));
                return (1);
               }
@@ -149,13 +148,13 @@ main(int  argc,                           /* I - Number of command-line arguments */
            break;
 
        default :
-           _cupsLangPrintf(stderr, NULL, _("lpinfo: Unknown option \'%c\'!\n"),
+           _cupsLangPrintf(stderr, _("lpinfo: Unknown option \'%c\'!\n"),
                            argv[i][1]);
            return (1);
       }
     else
     {
-      _cupsLangPrintf(stderr, NULL, _("lpinfo: Unknown argument \'%s\'!\n"),
+      _cupsLangPrintf(stderr, _("lpinfo: Unknown argument \'%s\'!\n"),
                       argv[i]);
       return (1);
     }
@@ -165,150 +164,57 @@ main(int  argc,                          /* I - Number of command-line arguments */
 
 
 /*
- * 'show_devices()' - Show available devices.
+ * 'device_cb()' - Device callback.
  */
 
-static int                             /* O - 0 on success, 1 on failure */
-show_devices(http_t *http,             /* I - HTTP connection to server */
-             int    long_status)       /* I - Long status report? */
+static void
+device_cb(
+    const char *device_class,          /* I - device-class string */
+    const char *device_id,             /* I - device-id string */
+    const char *device_info,           /* I - device-info string */
+    const char *device_make_and_model, /* I - device-make-and-model string */
+    const char *device_uri,            /* I - device-uri string */
+    const char *device_location,       /* I - device-location string */
+    void       *user_data)             /* I - User data */
 {
-  ipp_t                *request,               /* IPP Request */
-               *response;              /* IPP Response */
-  ipp_attribute_t *attr;               /* Current attribute */
-  cups_lang_t  *language;              /* Default language */
-  const char   *device_class,          /* Pointer into device-class */
-               *device_info,           /* Pointer into device-info */
-               *device_make,           /* Pointer into device-make-and-model */
-               *device_uri;            /* Pointer into device-uri */
+  int  *long_status;                   /* Show verbose info? */
 
 
-  if (http == NULL)
-    return (1);
-
  /*
-  * Build a CUPS_GET_DEVICES request, which requires the following
-  * attributes:
-  *
-  *    attributes-charset
-  *    attributes-natural-language
-  *    printer-uri
+  * Display the device...
   */
 
-  request = ippNew();
-
-  request->request.op.operation_id = CUPS_GET_DEVICES;
-  request->request.op.request_id   = 1;
-
-  language = cupsLangDefault();
-
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
-               "attributes-charset", NULL, cupsLangEncoding(language));
-
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
-               "attributes-natural-language", NULL, language->language);
+  long_status = (int *)user_data;
 
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
-               NULL, "ipp://localhost/printers/");
-
- /*
-  * Do the request and get back a response...
-  */
-
-  if ((response = cupsDoRequest(http, request, "/")) != NULL)
+  if (*long_status)
   {
-   /*
-    * Loop through the device list and display them...
-    */
-
-    if (response->request.status.status_code > IPP_OK_CONFLICT)
-    {
-      _cupsLangPrintf(stderr, NULL, _("lpinfo: cups-get-devices failed: %s\n"),
-                     ippErrorString(response->request.status.status_code));
-      ippDelete(response);
-      return (1);
-    }
-
-    for (attr = response->attrs; attr != NULL; attr = attr->next)
-    {
-     /*
-      * Skip leading attributes until we hit a device...
-      */
-
-      while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
-        attr = attr->next;
-
-      if (attr == NULL)
-        break;
-
-     /*
-      * Pull the needed attributes from this device...
-      */
-
-      device_class = NULL;
-      device_info  = NULL;
-      device_make  = NULL;
-      device_uri   = NULL;
-
-      while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
-      {
-        if (!strcmp(attr->name, "device-class") &&
-           attr->value_tag == IPP_TAG_KEYWORD)
-         device_class = attr->values[0].string.text;
-
-        if (!strcmp(attr->name, "device-info") &&
-           attr->value_tag == IPP_TAG_TEXT)
-         device_info = attr->values[0].string.text;
-
-        if (!strcmp(attr->name, "device-make-and-model") &&
-           attr->value_tag == IPP_TAG_TEXT)
-         device_make = attr->values[0].string.text;
-
-        if (!strcmp(attr->name, "device-uri") &&
-           attr->value_tag == IPP_TAG_URI)
-         device_uri = attr->values[0].string.text;
-
-        attr = attr->next;
-      }
-
-     /*
-      * See if we have everything needed...
-      */
-
-      if (device_class == NULL || device_info == NULL ||
-          device_make == NULL || device_uri == NULL)
-      {
-        if (attr == NULL)
-         break;
-       else
-          continue;
-      }
+    _cupsLangPrintf(stdout,
+                   _("Device: uri = %s\n"
+                     "        class = %s\n"
+                     "        info = %s\n"
+                     "        make-and-model = %s\n"
+                     "        device-id = %s\n"
+                     "        location = %s\n"),
+                   device_uri, device_class, device_info,
+                   device_make_and_model, device_id, device_location);
+  }
+  else
+    _cupsLangPrintf(stdout, "%s %s\n", device_class, device_uri);
+}
 
-     /*
-      * Display the device...
-      */
 
-      if (long_status)
-      {
-       _cupsLangPrintf(stdout, language,
-                       _("Device: uri = %s\n"
-                         "        class = %s\n"
-                         "        info = %s\n"
-                         "        make-and-model = %s\n"),
-                       device_uri, device_class, device_info, device_make);
-      }
-      else
-        _cupsLangPrintf(stdout, language, "%s %s\n", device_class, device_uri);
-
-      if (attr == NULL)
-        break;
-    }
+/*
+ * 'show_devices()' - Show available devices.
+ */
 
-    ippDelete(response);
-  }
-  else
+static int                             /* O - 0 on success, 1 on failure */
+show_devices(http_t *http,             /* I - HTTP connection to server */
+             int    long_status)       /* I - Long status report? */
+{
+  if (cupsGetDevices(http, CUPS_TIMEOUT_DEFAULT, CUPS_EXCLUDE_NONE, device_cb,
+                     &long_status) != IPP_OK)
   {
-    _cupsLangPrintf(stderr, NULL, _("lpinfo: cups-get-devices failed: %s\n"),
-                   ippErrorString(cupsLastError()));
+    _cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString());
     return (1);
   }
 
@@ -327,10 +233,10 @@ show_models(http_t *http,         /* I - HTTP connection to server */
   ipp_t                *request,               /* IPP Request */
                *response;              /* IPP Response */
   ipp_attribute_t *attr;               /* Current attribute */
-  cups_lang_t  *language;              /* Default language */
-  const char   *ppd_language,          /* Pointer into ppd-natural-language */
-               *ppd_make,              /* Pointer into ppd-make-and-model */
-               *ppd_name;              /* Pointer into ppd-name */
+  const char   *ppd_device_id,         /* Pointer to ppd-device-id */
+               *ppd_language,          /* Pointer to ppd-natural-language */
+               *ppd_make,              /* Pointer to ppd-make-and-model */
+               *ppd_name;              /* Pointer to ppd-name */
 
 
   if (http == NULL)
@@ -342,24 +248,9 @@ show_models(http_t *http,          /* I - HTTP connection to server */
   *
   *    attributes-charset
   *    attributes-natural-language
-  *    printer-uri
   */
 
-  request = ippNew();
-
-  request->request.op.operation_id = CUPS_GET_PPDS;
-  request->request.op.request_id   = 1;
-
-  language = cupsLangDefault();
-
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
-               "attributes-charset", NULL, cupsLangEncoding(language));
-
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
-               "attributes-natural-language", NULL, language->language);
-
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
-               NULL, "ipp://localhost/printers/");
+  request = ippNewRequest(CUPS_GET_PPDS);
 
  /*
   * Do the request and get back a response...
@@ -373,8 +264,7 @@ show_models(http_t *http,           /* I - HTTP connection to server */
 
     if (response->request.status.status_code > IPP_OK_CONFLICT)
     {
-      _cupsLangPrintf(stderr, NULL, _("lpinfo: cups-get-ppds failed: %s\n"),
-                     ippErrorString(response->request.status.status_code));
+      _cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString());
       ippDelete(response);
       return (1);
     }
@@ -395,22 +285,24 @@ show_models(http_t *http,         /* I - HTTP connection to server */
       * Pull the needed attributes from this PPD...
       */
 
-      ppd_language = NULL;
-      ppd_make     = NULL;
-      ppd_name     = NULL;
+      ppd_device_id = "NONE";
+      ppd_language  = NULL;
+      ppd_make      = NULL;
+      ppd_name      = NULL;
 
       while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
       {
-        if (!strcmp(attr->name, "ppd-natural-language") &&
-           attr->value_tag == IPP_TAG_LANGUAGE)
-         ppd_language = attr->values[0].string.text;
-
-        if (!strcmp(attr->name, "ppd-make-and-model") &&
+        if (!strcmp(attr->name, "ppd-device-id") &&
            attr->value_tag == IPP_TAG_TEXT)
+         ppd_device_id = attr->values[0].string.text;
+        else if (!strcmp(attr->name, "ppd-natural-language") &&
+                attr->value_tag == IPP_TAG_LANGUAGE)
+         ppd_language = attr->values[0].string.text;
+        else if (!strcmp(attr->name, "ppd-make-and-model") &&
+                attr->value_tag == IPP_TAG_TEXT)
          ppd_make = attr->values[0].string.text;
-
-        if (!strcmp(attr->name, "ppd-name") &&
-           attr->value_tag == IPP_TAG_NAME)
+        else if (!strcmp(attr->name, "ppd-name") &&
+                attr->value_tag == IPP_TAG_NAME)
          ppd_name = attr->values[0].string.text;
 
         attr = attr->next;
@@ -434,14 +326,15 @@ show_models(http_t *http,         /* I - HTTP connection to server */
 
       if (long_status)
       {
-       _cupsLangPrintf(stdout, language,
+       _cupsLangPrintf(stdout,
                        _("Model:  name = %s\n"
                          "        natural_language = %s\n"
-                         "        make-and-model = %s\n"),
-                       ppd_name, ppd_language, ppd_make);
+                         "        make-and-model = %s\n"
+                         "        device-id = %s\n"),
+                       ppd_name, ppd_language, ppd_make, ppd_device_id);
       }
       else
-        _cupsLangPrintf(stdout, language, "%s %s\n", ppd_name, ppd_make);
+        _cupsLangPrintf(stdout, "%s %s\n", ppd_name, ppd_make);
 
       if (attr == NULL)
         break;
@@ -451,8 +344,7 @@ show_models(http_t *http,           /* I - HTTP connection to server */
   }
   else
   {
-    _cupsLangPrintf(stderr, NULL, _("lpinfo: cups-get-ppds failed: %s\n"),
-                   ippErrorString(cupsLastError()));
+    _cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString());
 
     return (1);
   }
@@ -462,5 +354,5 @@ show_models(http_t *http,           /* I - HTTP connection to server */
 
 
 /*
- * End of "$Id: lpinfo.c 4906 2006-01-10 20:53:28Z mike $".
+ * End of "$Id: lpinfo.c 7810 2008-07-29 01:11:15Z mike $".
  */