]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - berkeley/lprm.c
Update libtool support to include --mode=foo stuff - surprise, libtool changed in
[thirdparty/cups.git] / berkeley / lprm.c
index 0551dbbd45a27c9acf05438214235eac2321140b..bd190bb3e57b5230743221559ab545d663fd7e8e 100644 (file)
@@ -1,41 +1,21 @@
 /*
- * "$Id: lprm.c 5926 2006-09-05 20:45:47Z mike $"
+ * "lprm" command for CUPS.
  *
- *   "lprm" command for the Common UNIX Printing System (CUPS).
+ * Copyright 2007-2016 by Apple Inc.
+ * Copyright 1997-2006 by Easy Software Products.
  *
- *   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
- *
- * Contents:
- *
- *   main() - Parse options and cancel jobs.
+ * 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 <cups/cups.h>
-#include <cups/i18n.h>
-#include <cups/string.h>
+#include <cups/cups-private.h>
 
 
 /*
@@ -46,19 +26,14 @@ int                         /* O - Exit status */
 main(int  argc,                        /* I - Number of command-line arguments */
      char *argv[])             /* I - Command-line arguments */
 {
-  http_t       *http;          /* HTTP connection to server */
   int          i;              /* Looping var */
   int          job_id;         /* Job ID */
-  const char   *dest;          /* Destination printer */
-  char         *instance;      /* Pointer to instance name */
-  char         uri[1024];      /* Printer or job URI */
-  ipp_t                *request;       /* IPP request */
-  ipp_t                *response;      /* IPP response */
-  ipp_op_t     op;             /* Operation */
-  int          num_dests;      /* Number of destinations */
-  cups_dest_t  *dests,         /* Destinations */
+  const char   *name;          /* Destination printer */
+  char         *instance,      /* Pointer to instance name */
+               *opt;           /* Option pointer */
+  cups_dest_t  *dest,          /* Destination */
                *defdest;       /* Default destination */
-  http_encryption_t encryption;        /* Encryption? */
+  int          did_cancel;     /* Did we cancel something? */
 
 
   _cupsSetLocale(argv);
@@ -67,143 +42,122 @@ main(int  argc,                   /* I - Number of command-line arguments */
   * Setup to cancel individual print jobs...
   */
 
-  op         = IPP_CANCEL_JOB;
-  job_id     = 0;
-  dest       = NULL;
-  response   = NULL;
-  http       = NULL;
-  encryption = cupsEncryption();
-
- /*
-  * Open a connection to the server...
-  */
-
-  if ((http = httpConnectEncrypt(cupsServer(), ippPort(), encryption)) == NULL)
-  {
-    _cupsLangPuts(stderr, _("lprm: Unable to contact server!\n"));
-    return (1);
-  }
-
-  num_dests  = cupsGetDests2(http, &dests);
-  defdest    = cupsGetDest(NULL, NULL, num_dests, dests);
-  dest       = defdest ? defdest->name : NULL;
+  did_cancel = 0;
+  defdest    = cupsGetNamedDest(CUPS_HTTP_DEFAULT, NULL, NULL);
+  name       = defdest ? defdest->name : NULL;
 
  /*
   * Process command-line arguments...
   */
 
   for (i = 1; i < argc; i ++)
+  {
     if (argv[i][0] == '-' && argv[i][1] != '\0')
-      switch (argv[i][1])
+    {
+      for (opt = argv[i] + 1; *opt; opt ++)
       {
-        case 'E' : /* Encrypt */
+       switch (*opt)
+       {
+         case 'E' : /* Encrypt */
 #ifdef HAVE_SSL
-           encryption = HTTP_ENCRYPT_REQUIRED;
-
-           httpEncryption(http, encryption);
-           cupsSetEncryption(encryption);
+             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 'P' : /* Cancel jobs on a printer */
-           if (argv[i][2])
-             dest = argv[i] + 2;
-           else
-           {
-             i ++;
-             dest = argv[i];
-           }
-
-           if ((instance = strchr(dest, '/')) != NULL)
-             *instance = '\0';
-
-           if (cupsGetDest(dest, NULL, num_dests, dests) == NULL)
-           {
-             _cupsLangPrintf(stderr,
-                             _("%s: Error - unknown destination \"%s\"!\n"),
-                             argv[0], dest);
-              cupsFreeDests(num_dests, dests);
-             httpClose(http);
-             return(1);
-           }
-           break;
-
-        case 'U' : /* Username */
-           if (argv[i][2] != '\0')
-             cupsSetUser(argv[i] + 2);
-           else
-           {
-             i ++;
-             if (i >= argc)
+             break;
+
+         case 'P' : /* Cancel jobs on a printer */
+             if (opt[1] != '\0')
+             {
+               name = opt + 1;
+               opt += strlen(opt) - 1;
+             }
+             else
+             {
+               i ++;
+               name = argv[i];
+             }
+
+             if ((instance = strchr(name, '/')) != NULL)
+               *instance = '\0';
+
+             if ((dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, name, NULL)) == NULL)
+             {
+               _cupsLangPrintf(stderr, _("%s: Error - unknown destination \"%s\"."), argv[0], name);
+               goto error;
+             }
+
+             cupsFreeDests(1, dest);
+             break;
+
+         case 'U' : /* Username */
+             if (opt[1] != '\0')
              {
-               _cupsLangPrintf(stderr,
-                               _("%s: Error - expected username after "
-                                 "\'-U\' option!\n"),
-                               argv[0]);
-               return (1);
+               cupsSetUser(opt + 1);
+               opt += strlen(opt) - 1;
              }
+             else
+             {
+               i ++;
+               if (i >= argc)
+               {
+                 _cupsLangPrintf(stderr, _("%s: Error - expected username after \"-U\" option."), argv[0]);
+                 goto error;
+               }
+
+               cupsSetUser(argv[i]);
+             }
+             break;
 
-              cupsSetUser(argv[i]);
-           }
-           break;
-           
-        case 'h' : /* Connect to host */
-           if (argv[i][2] != '\0')
-              cupsSetServer(argv[i] + 2);
-           else
-           {
-             i ++;
-
-             if (i >= argc)
+         case 'h' : /* Connect to host */
+             if (opt[1] != '\0')
              {
-               _cupsLangPrintf(stderr,
-                               _("%s: Error - expected hostname after "
-                                 "\'-h\' option!\n"),
-                               argv[0]);
-               return (1);
-              }
+               cupsSetServer(opt + 1);
+               opt += strlen(opt) - 1;
+             }
              else
-                cupsSetServer(argv[i]);
-           }
-
-            httpClose(http);
-            cupsFreeDests(num_dests, dests);
-
-           if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
-                                          encryption)) == NULL)
-           {
-             _cupsLangPuts(stderr, _("lprm: Unable to contact server!\n"));
-             return (1);
-           }
-
-           num_dests  = cupsGetDests2(http, &dests);
-           defdest    = cupsGetDest(NULL, NULL, num_dests, dests);
-           dest       = defdest ? defdest->name : NULL;
-           break;
-
-       default :
-           _cupsLangPrintf(stderr,
-                           _("%s: Error - unknown option \'%c\'!\n"),
-                           argv[0], argv[i][1]);
-            cupsFreeDests(num_dests, dests);
-           httpClose(http);
-           return (1);
+             {
+               i ++;
+
+               if (i >= argc)
+               {
+                 _cupsLangPrintf(stderr, _("%s: Error - expected hostname after \"-h\" option."), argv[0]);
+                 goto error;
+               }
+               else
+                 cupsSetServer(argv[i]);
+             }
+
+             if (defdest)
+               cupsFreeDests(1, defdest);
+
+             defdest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, NULL, NULL);
+             name    = defdest ? defdest->name : NULL;
+             break;
+
+         default :
+             _cupsLangPrintf(stderr, _("%s: Error - unknown option \"%c\"."), argv[0], *opt);
+             goto error;
+       }
       }
+    }
     else
     {
      /*
       * Cancel a job or printer...
       */
 
-      if (isdigit(argv[i][0] & 255) &&
-          cupsGetDest(argv[i], NULL, num_dests, dests) == NULL)
+      if ((dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, argv[i], NULL)) != NULL)
+        cupsFreeDests(1, dest);
+
+      if (dest)
       {
-        dest   = NULL;
-       op     = IPP_CANCEL_JOB;
+        name   = argv[i];
+        job_id = 0;
+      }
+      else if (isdigit(argv[i][0] & 255))
+      {
+        name   = NULL;
         job_id = atoi(argv[i]);
       }
       else if (!strcmp(argv[i], "-"))
@@ -212,87 +166,49 @@ main(int  argc,                   /* I - Number of command-line arguments */
         * Cancel all jobs
         */
 
-        op = IPP_PURGE_JOBS;
+        job_id = -1;
       }
       else
       {
-        dest   = argv[i];
-        job_id = 0;
+       _cupsLangPrintf(stderr, _("%s: Error - unknown destination \"%s\"."),
+                       argv[0], argv[i]);
+       goto error;
       }
 
-     /*
-      * Build an IPP request, which requires the following
-      * attributes:
-      *
-      *    attributes-charset
-      *    attributes-natural-language
-      *    printer-uri + job-id *or* job-uri
-      *    [requesting-user-name]
-      */
-
-      request = ippNewRequest(op);
-
-      if (dest)
+      if (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0) != IPP_OK)
       {
-        httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                        "localhost", 0, "/printers/%s", dest);
-       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
-                    "printer-uri", NULL, uri);
-       ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
-                     job_id);
-      }
-      else
-      {
-        sprintf(uri, "ipp://localhost/jobs/%d", job_id);
-       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
-                    uri);
+        _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString());
+       goto error;
       }
 
-      ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
-                   "requesting-user-name", NULL, cupsUser());
-
-     /*
-      * Do the request and get back a response...
-      */
-
-      if (op == IPP_PURGE_JOBS)
-        response = cupsDoRequest(http, request, "/admin/");
-      else
-        response = cupsDoRequest(http, request, "/jobs/");
-
-      ippDelete(response);
-
-      if (cupsLastError() > IPP_OK_CONFLICT)
-      {
-        _cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString());
-
-        cupsFreeDests(num_dests, dests);
-        httpClose(http);
-       return (1);
-      }
+      did_cancel = 1;
     }
+  }
 
  /*
   * If nothing has been canceled yet, cancel the current job on the specified
   * (or default) printer...
   */
 
-  if (response == NULL)
-    if (!cupsCancelJob(dest, 0))
+  if (!did_cancel && cupsCancelJob2(CUPS_HTTP_DEFAULT, name, 0, 0) != IPP_OK)
     {
-      _cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString());
-      cupsFreeDests(num_dests, dests);
-      httpClose(http);
-      return (1);
+      _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString());
+      goto error;
     }
 
-  cupsFreeDests(num_dests, dests);
-  httpClose(http);
+  if (defdest)
+    cupsFreeDests(1, defdest);
 
   return (0);
-}
 
+ /*
+  * If we get here there was an error, so clean up...
+  */
 
-/*
- * End of "$Id: lprm.c 5926 2006-09-05 20:45:47Z mike $".
- */
+  error:
+
+  if (defdest)
+    cupsFreeDests(1, defdest);
+
+  return (1);
+}