]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - systemv/lpinfo.c
Fix source file header text duplication text duplication.
[thirdparty/cups.git] / systemv / lpinfo.c
index 72e8fbab8a99745af44dcb6d36e2d22111faaca5..b250df557450b9734df07b4eab487e7054c8f5bd 100644 (file)
@@ -1,36 +1,22 @@
 /*
- * "$Id: lpinfo.c 7810 2008-07-29 01:11:15Z mike $"
+ * "lpinfo" command for CUPS.
  *
- *   "lpinfo" command for the Common UNIX Printing System (CUPS).
+ * Copyright 2007-2016 by Apple Inc.
+ * Copyright 1997-2006 by Easy Software Products.
  *
- *   Copyright 2007 by Apple Inc.
- *   Copyright 1997-2006 by Easy Software Products.
- *
- *   These coded instructions, statements, and computer programs are the
- *   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.
+ * These coded instructions, statements, and computer programs are the
+ * 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
+ * missing or damaged, see the license at "http://www.cups.org/".
  */
 
 /*
  * Include necessary headers...
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <cups/string.h>
-#include <cups/cups.h>
-#include <cups/i18n.h>
-#include <cups/debug.h>
+#include <cups/cups-private.h>
+#include <cups/adminutil.h>
 
 
 /*
@@ -42,10 +28,14 @@ static void device_cb(const char *device_clas, const char *device_id,
                          const char *device_make_and_model,
                          const char *device_uri, const char *device_location,
                          void *user_data);
-static int     show_devices(http_t *http, int long_status, int timeout);
-static int     show_models(http_t *http, int long_status,
+static int     show_devices(int long_status, int timeout,
+                            const char *include_schemes,
+                            const char *exclude_schemes);
+static int     show_models(int long_status,
                            const char *device_id, const char *language,
-                           const char *make_model, const char *product);
+                           const char *make_model, const char *product,
+                           const char *include_schemes,
+                           const char *exclude_schemes);
 
 
 /*
@@ -57,214 +47,198 @@ main(int  argc,                           /* I - Number of command-line arguments */
      char *argv[])                     /* I - Command-line arguments */
 {
   int          i;                      /* Looping var */
-  http_t       *http;                  /* Connection to server */
   int          long_status;            /* Long listing? */
-  const char   *device_id,             /* 1284 device ID */
+  const char   *opt,                   /* Option pointer */
+               *device_id,             /* 1284 device ID */
                *language,              /* Language */
                *make_model,            /* Make and model */
-               *product;               /* Product */
+               *product,               /* Product */
+               *include_schemes,       /* Schemes to include */
+               *exclude_schemes;       /* Schemes to exclude */
   int          timeout;                /* Device timeout */
 
 
   _cupsSetLocale(argv);
 
-  http        = NULL;
-  long_status = 0;
-  device_id   = NULL;
-  language    = NULL;
-  make_model  = NULL;
-  product     = NULL;
-  timeout     = CUPS_TIMEOUT_DEFAULT;
+  long_status     = 0;
+  device_id       = NULL;
+  language        = NULL;
+  make_model      = NULL;
+  product         = NULL;
+  include_schemes = CUPS_INCLUDE_ALL;
+  exclude_schemes = CUPS_EXCLUDE_NONE;
+  timeout         = CUPS_TIMEOUT_DEFAULT;
 
   for (i = 1; i < argc; i ++)
-    if (argv[i][0] == '-')
-      switch (argv[i][1])
+  {
+    if (!strcmp(argv[i], "--device-id"))
+    {
+      i ++;
+
+      if (i < argc)
+       device_id = argv[i];
+      else
       {
-        case 'E' : /* Encrypt */
-#ifdef HAVE_SSL
-           cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
+       _cupsLangPuts(stderr, _("lpinfo: Expected 1284 device ID string after \"--device-id\"."));
+       return (1);
+      }
+    }
+    else if (!strncmp(argv[i], "--device-id=", 12) && argv[i][12])
+    {
+      device_id = argv[i] + 12;
+    }
+    else if (!strcmp(argv[i], "--exclude-schemes"))
+    {
+      i ++;
+
+      if (i < argc)
+       exclude_schemes = argv[i];
+      else
+      {
+       _cupsLangPuts(stderr, _("lpinfo: Expected scheme list after \"--exclude-schemes\"."));
+       return (1);
+      }
+    }
+    else if (!strncmp(argv[i], "--exclude-schemes=", 18) && argv[i][18])
+    {
+      exclude_schemes = argv[i] + 18;
+    }
+    else if (!strcmp(argv[i], "--include-schemes"))
+    {
+      i ++;
 
-           if (http)
-             httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
+      if (i < argc)
+       include_schemes = argv[i];
+      else
+      {
+       _cupsLangPuts(stderr, _("lpinfo: Expected scheme list after \"--include-schemes\"."));
+       return (1);
+      }
+    }
+    else if (!strncmp(argv[i], "--include-schemes=", 18) && argv[i][18])
+    {
+      include_schemes = argv[i] + 18;
+    }
+    else if (!strcmp(argv[i], "--language"))
+    {
+      i ++;
+      if (i < argc)
+       language = argv[i];
+      else
+      {
+       _cupsLangPuts(stderr, _("lpinfo: Expected language after \"--language\"."));
+       return (1);
+      }
+    }
+    else if (!strncmp(argv[i], "--language=", 11) && argv[i][11])
+    {
+      language = argv[i] + 11;
+    }
+    else if (!strcmp(argv[i], "--make-and-model"))
+    {
+      i ++;
+      if (i < argc)
+       make_model= argv[i];
+      else
+      {
+       _cupsLangPuts(stderr, _("lpinfo: Expected make and model after \"--make-and-model\"."));
+       return (1);
+      }
+    }
+    else if (!strncmp(argv[i], "--make-and-model=", 17) && argv[i][17])
+    {
+      make_model = argv[i] + 17;
+    }
+    else if (!strcmp(argv[i], "--product"))
+    {
+      i ++;
+      if (i < argc)
+       product = argv[i];
+      else
+      {
+       _cupsLangPuts(stderr, _("lpinfo: Expected product string after \"--product\"."));
+       return (1);
+      }
+    }
+    else if (!strncmp(argv[i], "--product=", 10) && argv[i][10])
+    {
+      product = argv[i] + 10;
+    }
+    else if (!strcmp(argv[i], "--timeout"))
+    {
+      i ++;
+      if (i < argc)
+       timeout = atoi(argv[i]);
+      else
+      {
+       _cupsLangPuts(stderr, _("lpinfo: Expected timeout after \"--timeout\"."));
+       return (1);
+      }
+    }
+    else if (!strncmp(argv[i], "--timeout=", 10) && argv[i][10])
+    {
+      timeout = atoi(argv[i] + 10);
+    }
+    else if (argv[i][0] == '-')
+    {
+      for (opt = argv[i] + 1; *opt; opt ++)
+      {
+       switch (*opt)
+       {
+         case 'E' : /* Encrypt */
+#ifdef HAVE_SSL
+             cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
 #else
-            _cupsLangPrintf(stderr,
-                           _("%s: Sorry, no encryption support compiled in!\n"),
-                           argv[0]);
+             _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."), argv[0]);
 #endif /* HAVE_SSL */
-           break;
-
-        case 'h' : /* Connect to host */
-           if (http)
-           {
-             httpClose(http);
-             http = NULL;
-           }
-
-           if (argv[i][2] != '\0')
-             cupsSetServer(argv[i] + 2);
-           else
-           {
-             i ++;
-
-             if (i >= argc)
-             {
-               _cupsLangPuts(stderr,
-                             _("Error: need hostname after \'-h\' option!\n"));
-               return (1);
-              }
-
-             cupsSetServer(argv[i]);
-           }
-           break;
-
-        case 'l' : /* Show long listing */
-           long_status = 1;
-           break;
+             break;
 
-        case 'm' : /* Show models */
-           if (!http)
-           {
-              http = httpConnectEncrypt(cupsServer(), ippPort(),
-                                       cupsEncryption());
-
-             if (http == NULL)
+         case 'h' : /* Connect to host */
+             if (opt[1] != '\0')
              {
-               _cupsLangPrintf(stderr,
-                               _("lpinfo: Unable to connect to server: %s\n"),
-                               strerror(errno));
-               return (1);
+               cupsSetServer(opt + 1);
+               opt += strlen(opt) - 1;
              }
-            }
-
-            if (show_models(http, long_status, device_id, language, make_model,
-                           product))
-             return (1);
-           break;
-           
-        case 'v' : /* Show available devices */
-           if (!http)
-           {
-              http = httpConnectEncrypt(cupsServer(), ippPort(),
-                                       cupsEncryption());
-
-             if (http == NULL)
+             else
              {
-               _cupsLangPrintf(stderr,
-                               _("lpinfo: Unable to connect to server: %s\n"),
-                               strerror(errno));
-               return (1);
-             }
-            }
+               i ++;
 
-            if (show_devices(http, long_status, timeout))
-             return (1);
-           break;
-
-        case '-' : /* --something */
-            if (!strcmp(argv[i], "--device-id"))
-           {
-             i ++;
+               if (i >= argc)
+               {
+                 _cupsLangPuts(stderr, _("Error: need hostname after \"-h\" option."));
+                 return (1);
+               }
 
-             if (i < argc)
-               device_id = argv[i];
-             else
-             {
-               _cupsLangPuts(stderr,
-                             _("lpinfo: Expected 1284 device ID string "
-                               "after --device-id!\n"));
-               return (1);
-             }
-           }
-           else if (!strncmp(argv[i], "--device-id=", 12) && argv[i][12])
-           {
-             device_id = argv[i] + 12;
-           }
-            else if (!strcmp(argv[i], "--language"))
-           {
-             i ++;
-             if (i < argc)
-               language = argv[i];
-             else
-             {
-               _cupsLangPuts(stderr,
-                             _("lpinfo: Expected language after "
-                               "--language!\n"));
-               return (1);
-             }
-           }
-           else if (!strncmp(argv[i], "--language=", 11) && argv[i][11])
-           {
-             language = argv[i] + 11;
-           }
-            else if (!strcmp(argv[i], "--make-and-model"))
-           {
-             i ++;
-             if (i < argc)
-               make_model= argv[i];
-             else
-             {
-               _cupsLangPuts(stderr,
-                             _("lpinfo: Expected make and model after "
-                               "--make-and-model!\n"));
-               return (1);
+               cupsSetServer(argv[i]);
              }
-           }
-           else if (!strncmp(argv[i], "--make-and-model=", 17) && argv[i][17])
-           {
-             make_model = argv[i] + 17;
-           }
-            else if (!strcmp(argv[i], "--product"))
-           {
-             i ++;
-             if (i < argc)
-               product = argv[i];
-             else
-             {
-               _cupsLangPuts(stderr,
-                             _("lpinfo: Expected product string after "
-                               "--product!\n"));
+             break;
+
+         case 'l' : /* Show long listing */
+             long_status = 1;
+             break;
+
+         case 'm' : /* Show models */
+             if (show_models(long_status, device_id, language, make_model, product, include_schemes, exclude_schemes))
                return (1);
-             }
-           }
-           else if (!strncmp(argv[i], "--product=", 10) && argv[i][10])
-           {
-             product = argv[i] + 10;
-           }
-            else if (!strcmp(argv[i], "--timeout"))
-           {
-             i ++;
-             if (i < argc)
-               timeout = atoi(argv[i]);
-             else
-             {
-               _cupsLangPuts(stderr,
-                             _("lpinfo: Expected timeout after --timeout!\n"));
+             break;
+
+         case 'v' : /* Show available devices */
+             if (show_devices(long_status, timeout, include_schemes, exclude_schemes))
                return (1);
-             }
-           }
-           else if (!strncmp(argv[i], "--timeout=", 10) && argv[i][10])
-           {
-             timeout = atoi(argv[i] + 10);
-           }
-           else
-           {
-             _cupsLangPrintf(stderr, _("lpinfo: Unknown option \'%s\'!\n"),
-                             argv[i]);
-             return (1);
-           }
-           break;
+             break;
 
-       default :
-           _cupsLangPrintf(stderr, _("lpinfo: Unknown option \'%c\'!\n"),
-                           argv[i][1]);
-           return (1);
+         default :
+             _cupsLangPrintf(stderr, _("%s: Unknown option \"%c\"."), argv[0], *opt);
+             return (1);
+       }
       }
+    }
     else
     {
-      _cupsLangPrintf(stderr, _("lpinfo: Unknown argument \'%s\'!\n"),
-                      argv[i]);
+      _cupsLangPrintf(stderr, _("%s: Unknown argument \"%s\"."), argv[0], argv[i]);
       return (1);
     }
+  }
 
   return (0);
 }
@@ -301,12 +275,12 @@ device_cb(
                      "        info = %s\n"
                      "        make-and-model = %s\n"
                      "        device-id = %s\n"
-                     "        location = %s\n"),
+                     "        location = %s"),
                    device_uri, device_class, device_info,
                    device_make_and_model, device_id, device_location);
   }
   else
-    _cupsLangPrintf(stdout, "%s %s\n", device_class, device_uri);
+    _cupsLangPrintf(stdout, "%s %s", device_class, device_uri);
 }
 
 
@@ -315,14 +289,16 @@ device_cb(
  */
 
 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? */
-            int    timeout)            /* I - Timeout */
+show_devices(
+    int        long_status,            /* I - Long status report? */
+    int        timeout,                        /* I - Timeout */
+    const char *include_schemes,       /* I - List of schemes to include */
+    const char *exclude_schemes)       /* I - List of schemes to exclude */
 {
-  if (cupsGetDevices(http, timeout, CUPS_EXCLUDE_NONE, device_cb,
-                     &long_status) != IPP_OK)
+  if (cupsGetDevices(CUPS_HTTP_DEFAULT, timeout, include_schemes,
+                     exclude_schemes, device_cb, &long_status) != IPP_OK)
   {
-    _cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString());
+    _cupsLangPrintf(stderr, "lpinfo: %s", cupsLastErrorString());
     return (1);
   }
 
@@ -335,12 +311,14 @@ show_devices(http_t *http,                /* I - HTTP connection to server */
  */
 
 static int                             /* O - 0 on success, 1 on failure */
-show_models(http_t     *http,          /* I - HTTP connection to server */
-            int        long_status,    /* I - Long status report? */
-           const char *device_id,      /* I - 1284 device ID */
-           const char *language,       /* I - Language */
-           const char *make_model,     /* I - Make and model */
-           const char *product)        /* I - Product */
+show_models(
+    int        long_status,            /* I - Long status report? */
+    const char *device_id,             /* I - 1284 device ID */
+    const char *language,              /* I - Language */
+    const char *make_model,            /* I - Make and model */
+    const char *product,               /* I - Product */
+    const char *include_schemes,       /* I - List of schemes to include */
+    const char *exclude_schemes)       /* I - List of schemes to exclude */
 {
   ipp_t                *request,               /* IPP Request */
                *response;              /* IPP Response */
@@ -349,11 +327,9 @@ show_models(http_t     *http,              /* I - HTTP connection to server */
                *ppd_language,          /* Pointer to ppd-natural-language */
                *ppd_make_model,        /* Pointer to ppd-make-and-model */
                *ppd_name;              /* Pointer to ppd-name */
+  cups_option_t        option;                 /* in/exclude-schemes option */
 
 
-  if (http == NULL)
-    return (1);
-
  /*
   * Build a CUPS_GET_PPDS request...
   */
@@ -373,11 +349,27 @@ show_models(http_t     *http,             /* I - HTTP connection to server */
     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, "ppd-product",
                  NULL, product);
 
+  if (include_schemes)
+  {
+    option.name  = "include-schemes";
+    option.value = (char *)include_schemes;
+
+    cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
+  }
+
+  if (exclude_schemes)
+  {
+    option.name  = "exclude-schemes";
+    option.value = (char *)exclude_schemes;
+
+    cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
+  }
+
  /*
   * Do the request and get back a response...
   */
 
-  if ((response = cupsDoRequest(http, request, "/")) != NULL)
+  if ((response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/")) != NULL)
   {
    /*
     * Loop through the device list and display them...
@@ -385,7 +377,7 @@ show_models(http_t     *http,               /* I - HTTP connection to server */
 
     if (response->request.status.status_code > IPP_OK_CONFLICT)
     {
-      _cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString());
+      _cupsLangPrintf(stderr, "lpinfo: %s", cupsLastErrorString());
       ippDelete(response);
       return (1);
     }
@@ -451,11 +443,11 @@ show_models(http_t     *http,             /* I - HTTP connection to server */
                        _("Model:  name = %s\n"
                          "        natural_language = %s\n"
                          "        make-and-model = %s\n"
-                         "        device-id = %s\n"),
+                         "        device-id = %s"),
                        ppd_name, ppd_language, ppd_make_model, ppd_device_id);
       }
       else
-        _cupsLangPrintf(stdout, "%s %s\n", ppd_name, ppd_make_model);
+        _cupsLangPrintf(stdout, "%s %s", ppd_name, ppd_make_model);
 
       if (attr == NULL)
         break;
@@ -465,15 +457,10 @@ show_models(http_t     *http,             /* I - HTTP connection to server */
   }
   else
   {
-    _cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString());
+    _cupsLangPrintf(stderr, "lpinfo: %s", cupsLastErrorString());
 
     return (1);
   }
 
   return (0);
 }
-
-
-/*
- * End of "$Id: lpinfo.c 7810 2008-07-29 01:11:15Z mike $".
- */