]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - systemv/lpadmin.c
Full sweep of all Clang warnings, plus some bug fixes for incorrect memcpy usage.
[thirdparty/cups.git] / systemv / lpadmin.c
index cc0e2d6203ba65c08c19ba59b6412d371ba5aeab..fe1e31e2ae3214d32b438b5399a86317970a2f0d 100644 (file)
@@ -1,33 +1,16 @@
 /*
- * "$Id: lpadmin.c 7720 2008-07-11 22:46:21Z mike $"
+ * "$Id$"
  *
- *   "lpadmin" command for CUPS.
+ * "lpadmin" command for CUPS.
  *
- *   Copyright 2007-2010 by Apple Inc.
- *   Copyright 1997-2006 by Easy Software Products.
+ * Copyright 2007-2014 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 configure the scheduler.
- *   add_printer_to_class()      - Add a printer to a class.
- *   default_printer()           - Set the default printing destination.
- *   delete_printer()            - Delete a printer from the system...
- *   delete_printer_from_class() - Delete a printer from a class.
- *   enable_printer()            - Enable a printer...
- *   set_printer_device()        - Set the device-uri attribute.
- *   set_printer_file()          - Set the interface script or PPD file.
- *   set_printer_info()          - Set the printer description string.
- *   set_printer_location()      - Set the printer location string.
- *   set_printer_model()         - Set the driver model file.
- *   set_printer_options()       - Set the printer options.
- *   validate_name()             - Make sure the printer name only contains
- *                                 valid chars...
+ * 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/".
  */
 
 /*
  */
 
 #include <cups/cups-private.h>
-#ifdef HAVE_LIBZ
-#  include <zlib.h>
-#endif /* HAVE_LIBZ */
 
 
 /*
  * Local functions...
  */
 
-static int     add_printer_to_class(http_t *, char *, char *);
-static int     default_printer(http_t *, char *);
-static int     delete_printer(http_t *, char *);
-static int     delete_printer_from_class(http_t *, char *, char *);
-static int     enable_printer(http_t *, char *);
-static char    *get_line(char *, int, FILE *fp);
-static int     set_printer_device(http_t *, char *, char *);
-static int     set_printer_file(http_t *, char *, char *);
-static int     set_printer_info(http_t *, char *, char *);
-static int     set_printer_location(http_t *, char *, char *);
-static int     set_printer_model(http_t *, char *, char *);
-static int     set_printer_options(http_t *, char *, int, cups_option_t *);
-static int     validate_name(const char *);
+static int             add_printer_to_class(http_t *http, char *printer, char *pclass);
+static int             default_printer(http_t *http, char *printer);
+static int             delete_printer(http_t *http, char *printer);
+static int             delete_printer_from_class(http_t *http, char *printer,
+                                                 char *pclass);
+static int             delete_printer_option(http_t *http, char *printer,
+                                             char *option);
+static int             enable_printer(http_t *http, char *printer);
+static cups_ptype_t    get_printer_type(http_t *http, char *printer, char *uri,
+                                        size_t urisize);
+static int             set_printer_options(http_t *http, char *printer,
+                                           int num_options, cups_option_t *options,
+                                           char *file);
+static int             validate_name(const char *name);
 
 
 /*
@@ -74,6 +55,7 @@ main(int  argc,                       /* I - Number of command-line arguments */
                *val;           /* Pointer to allow/deny value */
   int          num_options;    /* Number of options */
   cups_option_t        *options;       /* Options */
+  char         *file;          /* New PPD file/interface script */
 
 
   _cupsSetLocale(argv);
@@ -82,6 +64,7 @@ main(int  argc,                       /* I - Number of command-line arguments */
   printer     = NULL;
   num_options = 0;
   options     = NULL;
+  file        = NULL;
 
   for (i = 1; i < argc; i ++)
     if (argv[i][0] == '-')
@@ -96,7 +79,7 @@ main(int  argc,                       /* I - Number of command-line arguments */
              if (http == NULL)
              {
                _cupsLangPrintf(stderr,
-                               _("lpadmin: Unable to connect to server: %s\n"),
+                               _("lpadmin: Unable to connect to server: %s"),
                                strerror(errno));
                return (1);
              }
@@ -107,7 +90,7 @@ main(int  argc,                      /* I - Number of command-line arguments */
              _cupsLangPuts(stderr,
                            _("lpadmin: Unable to add a printer to the class:\n"
                              "         You must specify a printer name "
-                             "first\n"));
+                             "first."));
              return (1);
            }
 
@@ -120,8 +103,8 @@ main(int  argc,                     /* I - Number of command-line arguments */
              if (i >= argc)
              {
                _cupsLangPuts(stderr,
-                             _("lpadmin: Expected class name after \'-c\' "
-                               "option\n"));
+                             _("lpadmin: Expected class name after \"-c\" "
+                               "option."));
                return (1);
              }
 
@@ -132,7 +115,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
            {
              _cupsLangPuts(stderr,
                            _("lpadmin: Class name can only contain printable "
-                             "characters\n"));
+                             "characters."));
              return (1);
            }
 
@@ -149,7 +132,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
              if (http == NULL)
              {
                _cupsLangPrintf(stderr,
-                               _("lpadmin: Unable to connect to server: %s\n"),
+                               _("lpadmin: Unable to connect to server: %s"),
                                strerror(errno));
                return (1);
              }
@@ -164,8 +147,8 @@ main(int  argc,                     /* I - Number of command-line arguments */
              if (i >= argc)
              {
                _cupsLangPuts(stderr,
-                             _("lpadmin: Expected printer name after \'-d\' "
-                               "option\n"));
+                             _("lpadmin: Expected printer name after \"-d\" "
+                               "option."));
                return (1);
              }
 
@@ -176,7 +159,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
            {
              _cupsLangPuts(stderr,
                            _("lpadmin: Printer name can only contain "
-                             "printable characters\n"));
+                             "printable characters."));
              return (1);
            }
 
@@ -202,8 +185,8 @@ main(int  argc,                     /* I - Number of command-line arguments */
              if (i >= argc)
              {
                _cupsLangPuts(stderr,
-                             _("lpadmin: Expected hostname after \'-h\' "
-                               "option\n"));
+                             _("lpadmin: Expected hostname after \"-h\" "
+                               "option."));
                return (1);
               }
 
@@ -212,34 +195,8 @@ main(int  argc,                    /* I - Number of command-line arguments */
            break;
 
         case 'i' : /* Use the specified interface script */
-           if (!http)
-           {
-              http = httpConnectEncrypt(cupsServer(), ippPort(),
-                                       cupsEncryption());
-
-             if (http == NULL)
-             {
-               _cupsLangPrintf(stderr,
-                               _("lpadmin: Unable to connect to server: %s\n"),
-                               strerror(errno));
-               return (1);
-             }
-            }
-
-           if (printer == NULL)
-           {
-             _cupsLangPuts(stderr,
-                           _("lpadmin: Unable to set the interface script:\n"
-                             "         You must specify a printer name "
-                             "first\n"));
-             return (1);
-           }
-
            if (argv[i][2])
-           {
-             if (set_printer_file(http, printer, argv[i] + 2))
-               return (1);
-            }
+             file = argv[i] + 2;
            else
            {
              i ++;
@@ -247,13 +204,12 @@ main(int  argc,                   /* I - Number of command-line arguments */
              if (i >= argc)
              {
                _cupsLangPuts(stderr,
-                             _("lpadmin: Expected interface after \'-i\' "
-                               "option\n"));
+                             _("lpadmin: Expected interface after \"-i\" "
+                               "option."));
                return (1);
              }
 
-             if (set_printer_file(http, printer, argv[i]))
-               return (1);
+             file = argv[i];
            }
            break;
 
@@ -266,8 +222,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
              if (http)
                httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
 #else
-              _cupsLangPrintf(stderr,
-                             _("%s: Sorry, no encryption support compiled in\n"),
+              _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."),
                              argv[0]);
 #endif /* HAVE_SSL */
              break;
@@ -281,7 +236,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
              if (http == NULL)
              {
                _cupsLangPrintf(stderr,
-                               _("lpadmin: Unable to connect to server: %s\n"),
+                               _("lpadmin: Unable to connect to server: %s"),
                                strerror(errno));
                return (1);
              }
@@ -292,35 +247,9 @@ main(int  argc,                    /* I - Number of command-line arguments */
             break;
 
         case 'm' : /* Use the specified standard script/PPD file */
-           if (!http)
-           {
-              http = httpConnectEncrypt(cupsServer(), ippPort(),
-                                       cupsEncryption());
-
-             if (http == NULL)
-             {
-               _cupsLangPrintf(stderr,
-                               _("lpadmin: Unable to connect to server: %s\n"),
-                               strerror(errno));
-               return (1);
-             }
-            }
-
-           if (printer == NULL)
-           {
-             _cupsLangPuts(stderr,
-                           _("lpadmin: Unable to set the interface script or "
-                             "PPD file:\n"
-                             "         You must specify a printer name "
-                             "first\n"));
-             return (1);
-           }
-
            if (argv[i][2])
-           {
-             if (set_printer_model(http, printer, argv[i] + 2))
-               return (1);
-           }
+             num_options = cupsAddOption("ppd-name", argv[i] + 2, num_options,
+                                         &options);
            else
            {
              i ++;
@@ -328,13 +257,13 @@ main(int  argc,                   /* I - Number of command-line arguments */
              if (i >= argc)
              {
                _cupsLangPuts(stderr,
-                             _("lpadmin: Expected model after \'-m\' "
-                               "option\n"));
+                             _("lpadmin: Expected model after \"-m\" "
+                               "option."));
                return (1);
              }
 
-             if (set_printer_model(http, printer, argv[i]))
-               return (1);
+             num_options = cupsAddOption("ppd-name", argv[i], num_options,
+                                         &options);
            }
            break;
 
@@ -348,8 +277,8 @@ main(int  argc,                     /* I - Number of command-line arguments */
              if (i >= argc)
              {
                _cupsLangPuts(stderr,
-                             _("lpadmin: Expected name=value after \'-o\' "
-                               "option\n"));
+                             _("lpadmin: Expected name=value after \"-o\" "
+                               "option."));
                return (1);
              }
 
@@ -358,20 +287,6 @@ main(int  argc,                    /* I - Number of command-line arguments */
            break;
 
         case 'p' : /* Add/modify a printer */
-           if (!http)
-           {
-              http = httpConnectEncrypt(cupsServer(), ippPort(),
-                                       cupsEncryption());
-
-             if (http == NULL)
-             {
-               _cupsLangPrintf(stderr,
-                               _("lpadmin: Unable to connect to server: %s\n"),
-                               strerror(errno));
-               return (1);
-             }
-            }
-
            if (argv[i][2])
              printer = argv[i] + 2;
            else
@@ -381,8 +296,8 @@ main(int  argc,                     /* I - Number of command-line arguments */
              if (i >= argc)
              {
                _cupsLangPuts(stderr,
-                             _("lpadmin: Expected printer after \'-p\' "
-                               "option\n"));
+                             _("lpadmin: Expected printer after \"-p\" "
+                               "option."));
                return (1);
              }
 
@@ -393,7 +308,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
            {
              _cupsLangPuts(stderr,
                            _("lpadmin: Printer name can only contain "
-                             "printable characters\n"));
+                             "printable characters."));
              return (1);
            }
            break;
@@ -407,7 +322,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
              if (http == NULL)
              {
                _cupsLangPrintf(stderr,
-                               _("lpadmin: Unable to connect to server: %s\n"),
+                               _("lpadmin: Unable to connect to server: %s"),
                                strerror(errno));
                return (1);
              }
@@ -419,7 +334,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
                            _("lpadmin: Unable to remove a printer from the "
                              "class:\n"
                              "         You must specify a printer name "
-                             "first\n"));
+                             "first."));
              return (1);
            }
 
@@ -432,8 +347,8 @@ main(int  argc,                     /* I - Number of command-line arguments */
              if (i >= argc)
              {
                _cupsLangPuts(stderr,
-                             _("lpadmin: Expected class after \'-r\' "
-                               "option\n"));
+                             _("lpadmin: Expected class after \"-r\" "
+                               "option."));
                return (1);
              }
 
@@ -444,7 +359,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
            {
              _cupsLangPuts(stderr,
                            _("lpadmin: Class name can only contain printable "
-                             "characters\n"));
+                             "characters."));
              return (1);
            }
 
@@ -452,6 +367,51 @@ main(int  argc,                    /* I - Number of command-line arguments */
              return (1);
            break;
 
+        case 'R' : /* Remove option */
+           if (!http)
+           {
+              http = httpConnectEncrypt(cupsServer(), ippPort(),
+                                       cupsEncryption());
+
+             if (http == NULL)
+             {
+               _cupsLangPrintf(stderr,
+                               _("lpadmin: Unable to connect to server: %s"),
+                               strerror(errno));
+               return (1);
+             }
+            }
+
+           if (printer == NULL)
+           {
+             _cupsLangPuts(stderr,
+                           _("lpadmin: Unable to delete option:\n"
+                             "         You must specify a printer name "
+                             "first."));
+             return (1);
+           }
+
+           if (argv[i][2])
+             val = argv[i] + 2;
+           else
+           {
+             i ++;
+
+             if (i >= argc)
+             {
+               _cupsLangPuts(stderr,
+                             _("lpadmin: Expected name after \"-R\" "
+                               "option."));
+               return (1);
+             }
+
+             val = argv[i];
+           }
+
+            if (delete_printer_option(http, printer, val))
+             return (1);
+           break;
+
         case 'U' : /* Username */
            if (argv[i][2] != '\0')
              cupsSetUser(argv[i] + 2);
@@ -462,15 +422,14 @@ main(int  argc,                   /* I - Number of command-line arguments */
              {
                _cupsLangPrintf(stderr,
                                _("%s: Error - expected username after "
-                                 "\'-U\' option\n"),
-                               argv[0]);
+                                 "\"-U\" option."), argv[0]);
                return (1);
              }
 
               cupsSetUser(argv[i]);
            }
            break;
-           
+
         case 'u' : /* Allow/deny users */
            if (argv[i][2])
              val = argv[i] + 2;
@@ -482,57 +441,32 @@ main(int  argc,                   /* I - Number of command-line arguments */
              {
                _cupsLangPuts(stderr,
                              _("lpadmin: Expected allow/deny:userlist after "
-                               "\'-u\' option\n"));
+                               "\"-u\" option."));
                return (1);
              }
 
               val = argv[i];
            }
 
-            if (!strncasecmp(val, "allow:", 6))
+            if (!_cups_strncasecmp(val, "allow:", 6))
              num_options = cupsAddOption("requesting-user-name-allowed",
                                          val + 6, num_options, &options);
-            else if (!strncasecmp(val, "deny:", 5))
+            else if (!_cups_strncasecmp(val, "deny:", 5))
              num_options = cupsAddOption("requesting-user-name-denied",
                                          val + 5, num_options, &options);
             else
            {
              _cupsLangPrintf(stderr,
-                             _("lpadmin: Unknown allow/deny option \"%s\"\n"),
+                             _("lpadmin: Unknown allow/deny option \"%s\"."),
                              val);
              return (1);
            }
            break;
 
         case 'v' : /* Set the device-uri attribute */
-           if (!http)
-           {
-              http = httpConnectEncrypt(cupsServer(), ippPort(),
-                                       cupsEncryption());
-
-             if (http == NULL)
-             {
-               _cupsLangPrintf(stderr,
-                               _("lpadmin: Unable to connect to server: %s\n"),
-                               strerror(errno));
-               return (1);
-             }
-            }
-
-           if (printer == NULL)
-           {
-             _cupsLangPuts(stderr,
-                           _("lpadmin: Unable to set the device URI:\n"
-                             "         You must specify a printer name "
-                             "first\n"));
-             return (1);
-           }
-
            if (argv[i][2])
-           {
-             if (set_printer_device(http, printer, argv[i] + 2))
-               return (1);
-            }
+             num_options = cupsAddOption("device-uri", argv[i] + 2,
+                                         num_options, &options);
            else
            {
              i ++;
@@ -540,13 +474,13 @@ main(int  argc,                   /* I - Number of command-line arguments */
              if (i >= argc)
              {
                _cupsLangPuts(stderr,
-                             _("lpadmin: Expected device URI after \'-v\' "
-                               "option\n"));
+                             _("lpadmin: Expected device URI after \"-v\" "
+                               "option."));
                return (1);
              }
 
-             if (set_printer_device(http, printer, argv[i]))
-               return (1);
+             num_options = cupsAddOption("device-uri", argv[i],
+                                         num_options, &options);
            }
            break;
 
@@ -559,7 +493,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
              if (http == NULL)
              {
                _cupsLangPrintf(stderr,
-                               _("lpadmin: Unable to connect to server: %s\n"),
+                               _("lpadmin: Unable to connect to server: %s"),
                                strerror(errno));
                return (1);
              }
@@ -575,7 +509,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
              {
                _cupsLangPuts(stderr,
                              _("lpadmin: Expected printer or class after "
-                               "\'-x\' option\n"));
+                               "\"-x\" option."));
                return (1);
              }
 
@@ -586,7 +520,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
            {
              _cupsLangPuts(stderr,
                            _("lpadmin: Printer name can only contain "
-                             "printable characters\n"));
+                             "printable characters."));
              return (1);
            }
 
@@ -597,35 +531,9 @@ main(int  argc,                    /* I - Number of command-line arguments */
            break;
 
         case 'D' : /* Set the printer-info attribute */
-           if (!http)
-           {
-              http = httpConnectEncrypt(cupsServer(), ippPort(),
-                                       cupsEncryption());
-
-             if (http == NULL)
-             {
-               _cupsLangPrintf(stderr,
-                               _("lpadmin: Unable to connect to server: %s\n"),
-                               strerror(errno));
-               return (1);
-             }
-            }
-
-           if (printer == NULL)
-           {
-             _cupsLangPuts(stderr,
-                           _("lpadmin: Unable to set the printer "
-                             "description:\n"
-                             "         You must specify a printer name "
-                             "first\n"));
-             return (1);
-           }
-
            if (argv[i][2])
-           {
-             if (set_printer_info(http, printer, argv[i] + 2))
-               return (1);
-           }
+             num_options = cupsAddOption("printer-info", argv[i] + 2,
+                                         num_options, &options);
            else
            {
              i ++;
@@ -634,12 +542,12 @@ main(int  argc,                   /* I - Number of command-line arguments */
              {
                _cupsLangPuts(stderr,
                              _("lpadmin: Expected description after "
-                               "\'-D\' option\n"));
+                               "\"-D\" option."));
                return (1);
              }
 
-             if (set_printer_info(http, printer, argv[i]))
-               return (1);
+             num_options = cupsAddOption("printer-info", argv[i],
+                                         num_options, &options);
            }
            break;
 
@@ -649,44 +557,19 @@ main(int  argc,                   /* I - Number of command-line arguments */
            if (i >= argc)
            {
              _cupsLangPuts(stderr,
-                           _("lpadmin: Expected file type(s) after \'-I\' "
-                             "option\n"));
+                           _("lpadmin: Expected file type(s) after \"-I\" "
+                             "option."));
              return (1);
            }
 
            _cupsLangPuts(stderr,
-                         _("lpadmin: Warning - content type list ignored\n"));
+                         _("lpadmin: Warning - content type list ignored."));
            break;
-           
-        case 'L' : /* Set the printer-location attribute */
-           if (!http)
-           {
-              http = httpConnectEncrypt(cupsServer(), ippPort(),
-                                       cupsEncryption());
-
-             if (http == NULL)
-             {
-               _cupsLangPrintf(stderr,
-                               _("lpadmin: Unable to connect to server: %s\n"),
-                               strerror(errno));
-               return (1);
-             }
-            }
-
-           if (printer == NULL)
-           {
-             _cupsLangPuts(stderr,
-                           _("lpadmin: Unable to set the printer location:\n"
-                             "         You must specify a printer name "
-                             "first\n"));
-             return (1);
-           }
 
+        case 'L' : /* Set the printer-location attribute */
            if (argv[i][2])
-           {
-             if (set_printer_location(http, printer, argv[i] + 2))
-               return (1);
-            }
+             num_options = cupsAddOption("printer-location", argv[i] + 2,
+                                         num_options, &options);
            else
            {
              i ++;
@@ -694,45 +577,19 @@ main(int  argc,                   /* I - Number of command-line arguments */
              if (i >= argc)
              {
                _cupsLangPuts(stderr,
-                             _("lpadmin: Expected location after \'-L\' "
-                               "option\n"));
+                             _("lpadmin: Expected location after \"-L\" "
+                               "option."));
                return (1);
              }
 
-             if (set_printer_location(http, printer, argv[i]))
-               return (1);
+             num_options = cupsAddOption("printer-location", argv[i],
+                                         num_options, &options);
            }
            break;
 
         case 'P' : /* Use the specified PPD file */
-           if (!http)
-           {
-              http = httpConnectEncrypt(cupsServer(), ippPort(),
-                                       cupsEncryption());
-
-             if (http == NULL)
-             {
-               _cupsLangPrintf(stderr,
-                               _("lpadmin: Unable to connect to server: %s\n"),
-                               strerror(errno));
-               return (1);
-             }
-            }
-
-           if (printer == NULL)
-           {
-             _cupsLangPuts(stderr,
-                           _("lpadmin: Unable to set the PPD file:\n"
-                             "         You must specify a printer name "
-                             "first\n"));
-             return (1);
-           }
-
            if (argv[i][2])
-           {
-             if (set_printer_file(http, printer, argv[i] + 2))
-               return (1);
-           }
+             file = argv[i] + 2;
            else
            {
              i ++;
@@ -740,23 +597,22 @@ main(int  argc,                   /* I - Number of command-line arguments */
              if (i >= argc)
              {
                _cupsLangPuts(stderr,
-                             _("lpadmin: Expected PPD after \'-P\' option\n"));
+                             _("lpadmin: Expected PPD after \"-P\" option."));
                return (1);
              }
 
-             if (set_printer_file(http, printer, argv[i]))
-               return (1);
+             file = argv[i];
            }
            break;
 
        default :
            _cupsLangPrintf(stderr,
-                           _("lpadmin: Unknown option \'%c\'\n"), argv[i][1]);
+                           _("lpadmin: Unknown option \"%c\"."), argv[i][1]);
            return (1);
       }
     else
     {
-      _cupsLangPrintf(stderr, _("lpadmin: Unknown argument \'%s\'\n"),
+      _cupsLangPrintf(stderr, _("lpadmin: Unknown argument \"%s\"."),
                       argv[i]);
       return (1);
     }
@@ -765,7 +621,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
   * Set options as needed...
   */
 
-  if (num_options)
+  if (num_options || file)
   {
     if (!http)
     {
@@ -774,7 +630,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
       if (http == NULL)
       {
        _cupsLangPrintf(stderr,
-                       _("lpadmin: Unable to connect to server: %s\n"),
+                       _("lpadmin: Unable to connect to server: %s"),
                        strerror(errno));
        return (1);
       }
@@ -784,11 +640,11 @@ main(int  argc,                   /* I - Number of command-line arguments */
     {
       _cupsLangPuts(stderr,
                     _("lpadmin: Unable to set the printer options:\n"
-                     "         You must specify a printer name first\n"));
+                     "         You must specify a printer name first."));
       return (1);
     }
 
-    if (set_printer_options(http, printer, num_options, options))
+    if (set_printer_options(http, printer, num_options, options, file))
       return (1);
   }
 
@@ -805,8 +661,7 @@ main(int  argc,                     /* I - Number of command-line arguments */
                    "[-D description]\n"
                    "                       [-P ppd-file] [-o name=value]\n"
                    "                       [-u allow:user,user] "
-                   "[-u deny:user,user]\n"
-                   "\n"));
+                   "[-u deny:user,user]"));
   }
 
   if (http)
@@ -843,6 +698,7 @@ add_printer_to_class(http_t *http,  /* I - Server connection */
   *    attributes-charset
   *    attributes-natural-language
   *    printer-uri
+  *    requesting-user-name
   */
 
   request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
@@ -851,6 +707,8 @@ add_printer_to_class(http_t *http,  /* I - Server connection */
                    "localhost", 0, "/classes/%s", pclass);
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
                "printer-uri", NULL, uri);
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
+               NULL, cupsUser());
 
  /*
   * Do the request and get back a response...
@@ -859,32 +717,36 @@ add_printer_to_class(http_t *http,        /* I - Server connection */
   response = cupsDoRequest(http, request, "/");
 
  /*
-  * Build a CUPS_ADD_CLASS request, which requires the following
+  * Build a CUPS_ADD_MODIFY_CLASS request, which requires the following
   * attributes:
   *
   *    attributes-charset
   *    attributes-natural-language
   *    printer-uri
+  *    requesting-user-name
   *    member-uris
   */
 
-  request = ippNewRequest(CUPS_ADD_CLASS);
+  request = ippNewRequest(CUPS_ADD_MODIFY_CLASS);
 
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
                "printer-uri", NULL, uri);
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
+               NULL, cupsUser());
 
  /*
   * See if the printer is already in the class...
   */
 
   if (response != NULL &&
-      (members = ippFindAttribute(response, "member-names", IPP_TAG_NAME)) != NULL)
+      (members = ippFindAttribute(response, "member-names",
+                                  IPP_TAG_NAME)) != NULL)
     for (i = 0; i < members->num_values; i ++)
-      if (strcasecmp(printer, members->values[i].string.text) == 0)
+      if (_cups_strcasecmp(printer, members->values[i].string.text) == 0)
       {
         _cupsLangPrintf(stderr,
-                       _("lpadmin: Printer %s is already a member of class %s.\n"),
-                       printer, pclass);
+                       _("lpadmin: Printer %s is already a member of class "
+                         "%s."), printer, pclass);
         ippDelete(request);
        ippDelete(response);
        return (0);
@@ -898,7 +760,8 @@ add_printer_to_class(http_t *http,  /* I - Server connection */
                    "localhost", 0, "/printers/%s", printer);
 
   if (response != NULL &&
-      (members = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL)
+      (members = ippFindAttribute(response, "member-uris",
+                                  IPP_TAG_URI)) != NULL)
   {
    /*
     * Add the printer to the existing list...
@@ -907,7 +770,8 @@ add_printer_to_class(http_t *http,  /* I - Server connection */
     attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_URI,
                          "member-uris", members->num_values + 1, NULL, NULL);
     for (i = 0; i < members->num_values; i ++)
-      attr->values[i].string.text = _cupsStrAlloc(members->values[i].string.text);
+      attr->values[i].string.text =
+          _cupsStrAlloc(members->values[i].string.text);
 
     attr->values[i].string.text = _cupsStrAlloc(uri);
   }
@@ -921,26 +785,15 @@ add_printer_to_class(http_t *http,        /* I - Server connection */
 
   ippDelete(response);
 
-  if ((response = cupsDoRequest(http, request, "/admin/")) == NULL)
-  {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-
-    return (1);
-  }
-  else if (response->request.status.status_code > IPP_OK_CONFLICT)
+  ippDelete(cupsDoRequest(http, request, "/admin/"));
+  if (cupsLastError() > IPP_OK_CONFLICT)
   {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-
-    ippDelete(response);
+    _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
 
     return (1);
   }
   else
-  {
-    ippDelete(response);
-
     return (0);
-  }
 }
 
 
@@ -952,8 +805,7 @@ static int                          /* O - 0 on success, 1 on fail */
 default_printer(http_t *http,          /* I - Server connection */
                 char   *printer)       /* I - Printer name */
 {
-  ipp_t                *request,               /* IPP Request */
-               *response;              /* IPP Response */
+  ipp_t                *request;               /* IPP Request */
   char         uri[HTTP_MAX_URI];      /* URI for printer/class */
 
 
@@ -966,6 +818,7 @@ default_printer(http_t *http,               /* I - Server connection */
   *    attributes-charset
   *    attributes-natural-language
   *    printer-uri
+  *    requesting-user-name
   */
 
   httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
@@ -975,31 +828,23 @@ default_printer(http_t *http,             /* I - Server connection */
 
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
                "printer-uri", NULL, uri);
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
+               NULL, cupsUser());
 
  /*
   * Do the request and get back a response...
   */
 
-  if ((response = cupsDoRequest(http, request, "/admin/")) == NULL)
-  {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
+  ippDelete(cupsDoRequest(http, request, "/admin/"));
 
-    return (1);
-  }
-  else if (response->request.status.status_code > IPP_OK_CONFLICT)
+  if (cupsLastError() > IPP_OK_CONFLICT)
   {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-
-    ippDelete(response);
+    _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
 
     return (1);
   }
   else
-  {
-    ippDelete(response);
-
     return (0);
-  }
 }
 
 
@@ -1011,8 +856,7 @@ static int                         /* O - 0 on success, 1 on fail */
 delete_printer(http_t *http,           /* I - Server connection */
                char   *printer)                /* I - Printer to delete */
 {
-  ipp_t                *request,               /* IPP Request */
-               *response;              /* IPP Response */
+  ipp_t                *request;               /* IPP Request */
   char         uri[HTTP_MAX_URI];      /* URI for printer/class */
 
 
@@ -1025,6 +869,7 @@ delete_printer(http_t *http,               /* I - Server connection */
   *    attributes-charset
   *    attributes-natural-language
   *    printer-uri
+  *    requesting-user-name
   */
 
   request = ippNewRequest(CUPS_DELETE_PRINTER);
@@ -1033,31 +878,23 @@ delete_printer(http_t *http,             /* I - Server connection */
                    "localhost", 0, "/printers/%s", printer);
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
                "printer-uri", NULL, uri);
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
+               NULL, cupsUser());
 
  /*
   * Do the request and get back a response...
   */
 
-  if ((response = cupsDoRequest(http, request, "/admin/")) == NULL)
-  {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
+  ippDelete(cupsDoRequest(http, request, "/admin/"));
 
-    return (1);
-  }
-  else if (response->request.status.status_code > IPP_OK_CONFLICT)
+  if (cupsLastError() > IPP_OK_CONFLICT)
   {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-
-    ippDelete(response);
+    _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
 
     return (1);
   }
   else
-  {
-    ippDelete(response);
-
     return (0);
-  }
 }
 
 
@@ -1089,6 +926,7 @@ delete_printer_from_class(
   *    attributes-charset
   *    attributes-natural-language
   *    printer-uri
+  *    requesting-user-name
   */
 
   request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
@@ -1097,6 +935,8 @@ delete_printer_from_class(
                    "localhost", 0, "/classes/%s", pclass);
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
                "printer-uri", NULL, uri);
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
+               NULL, cupsUser());
 
  /*
   * Do the request and get back a response...
@@ -1105,7 +945,7 @@ delete_printer_from_class(
   if ((response = cupsDoRequest(http, request, "/classes/")) == NULL ||
       response->request.status.status_code == IPP_NOT_FOUND)
   {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
+    _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
 
     ippDelete(response);
 
@@ -1118,7 +958,7 @@ delete_printer_from_class(
 
   if ((members = ippFindAttribute(response, "member-names", IPP_TAG_NAME)) == NULL)
   {
-    _cupsLangPuts(stderr, _("lpadmin: No member names were seen\n"));
+    _cupsLangPuts(stderr, _("lpadmin: No member names were seen."));
 
     ippDelete(response);
 
@@ -1126,13 +966,13 @@ delete_printer_from_class(
   }
 
   for (i = 0; i < members->num_values; i ++)
-    if (!strcasecmp(printer, members->values[i].string.text))
+    if (!_cups_strcasecmp(printer, members->values[i].string.text))
       break;
 
   if (i >= members->num_values)
   {
     _cupsLangPrintf(stderr,
-                    _("lpadmin: Printer %s is not a member of class %s.\n"),
+                    _("lpadmin: Printer %s is not a member of class %s."),
                    printer, pclass);
 
     ippDelete(response);
@@ -1149,29 +989,35 @@ delete_printer_from_class(
     *    attributes-charset
     *    attributes-natural-language
     *    printer-uri
+    *    requesting-user-name
     */
 
     request = ippNewRequest(CUPS_DELETE_CLASS);
 
     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
                 "printer-uri", NULL, uri);
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+                 "requesting-user-name", NULL, cupsUser());
   }
   else
   {
    /*
-    * Build a CUPS_ADD_CLASS request, which requires the following
+    * Build a CUPS_ADD_MODIFY_CLASS request, which requires the following
     * attributes:
     *
     *    attributes-charset
     *    attributes-natural-language
     *    printer-uri
+    *    requesting-user-name
     *    member-uris
     */
 
-    request = ippNewRequest(CUPS_ADD_CLASS);
+    request = ippNewRequest(CUPS_ADD_MODIFY_CLASS);
 
     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
                 "printer-uri", NULL, uri);
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+                 "requesting-user-name", NULL, cupsUser());
 
    /*
     * Delete the printer from the class...
@@ -1193,315 +1039,63 @@ delete_printer_from_class(
 
   ippDelete(response);
 
-  if ((response = cupsDoRequest(http, request, "/admin/")) == NULL)
-  {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
+  ippDelete(cupsDoRequest(http, request, "/admin/"));
 
-    return (1);
-  }
-  else if (response->request.status.status_code > IPP_OK_CONFLICT)
+  if (cupsLastError() > IPP_OK_CONFLICT)
   {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-
-    ippDelete(response);
+    _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
 
     return (1);
   }
   else
-  {
-    ippDelete(response);
-
     return (0);
-  }
 }
 
 
 /*
- * 'enable_printer()' - Enable a printer...
+ * 'delete_printer_option()' - Delete a printer option.
  */
 
 static int                             /* O - 0 on success, 1 on fail */
-enable_printer(http_t *http,           /* I - Server connection */
-               char   *printer)                /* I - Printer to enable */
+delete_printer_option(http_t *http,    /* I - Server connection */
+                      char   *printer, /* I - Printer */
+                     char   *option)   /* I - Option to delete */
 {
-  ipp_t                *request,               /* IPP Request */
-               *response;              /* IPP Response */
+  ipp_t                *request;               /* IPP request */
   char         uri[HTTP_MAX_URI];      /* URI for printer/class */
 
 
-  DEBUG_printf(("enable_printer(%p, \"%s\")\n", http, printer));
-
  /*
-  * Build a CUPS_ADD_PRINTER request, which requires the following
-  * attributes:
+  * Build a CUPS_ADD_MODIFY_PRINTER or CUPS_ADD_MODIFY_CLASS request, which
+  * requires the following attributes:
   *
   *    attributes-charset
   *    attributes-natural-language
   *    printer-uri
-  *    printer-state
-  *    printer-is-accepting-jobs
+  *    requesting-user-name
+  *    option with deleteAttr tag
   */
 
-  request = ippNewRequest(CUPS_ADD_PRINTER);
+  if (get_printer_type(http, printer, uri, sizeof(uri)) & CUPS_PRINTER_CLASS)
+    request = ippNewRequest(CUPS_ADD_MODIFY_CLASS);
+  else
+    request = ippNewRequest(CUPS_ADD_MODIFY_PRINTER);
 
-  httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                   "localhost", 0, "/printers/%s", printer);
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
-               "printer-uri", NULL, uri);
-
-  ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
-                IPP_PRINTER_IDLE);
-
-  ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-accepting-jobs", 1);
-
- /*
-  * Do the request and get back a response...
-  */
-
-  if ((response = cupsDoRequest(http, request, "/admin/")) == NULL)
-  {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-
-    return (1);
-  }
-  else if (response->request.status.status_code > IPP_OK_CONFLICT)
-  {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-
-    ippDelete(response);
-
-    return (1);
-  }
-  else
-  {
-    ippDelete(response);
-
-    return (0);
-  }
-}
-
-
-/*
- * 'get_line()' - Get a line that is terminated by a LF, CR, or CR LF.
- */
-
-static char *                          /* O - Pointer to buf or NULL on EOF */
-get_line(char *buf,                    /* I - Line buffer */
-         int  length,                  /* I - Length of buffer */
-        FILE *fp)                      /* I - File to read from */
-{
-  char *bufptr;                        /* Pointer into buffer */
-  int  ch;                             /* Character from file */
-
-
-  length --;
-  bufptr = buf;
-
-  while ((ch = getc(fp)) != EOF)
-  {
-    if (ch == '\n')
-      break;
-    else if (ch == '\r')
-    {
-     /*
-      * Look for LF...
-      */
-
-      ch = getc(fp);
-      if (ch != '\n' && ch != EOF)
-        ungetc(ch, fp);
-
-      break;
-    }
-
-    *bufptr++ = ch;
-    length --;
-    if (length == 0)
-      break;
-  }
-
-  *bufptr = '\0';
-
-  if (ch == EOF)
-    return (NULL);
-  else
-    return (buf);
-}
-
-
-/*
- * 'set_printer_device()' - Set the device-uri attribute.
- */
-
-static int                             /* O - 0 on success, 1 on fail */
-set_printer_device(http_t *http,       /* I - Server connection */
-                   char   *printer,    /* I - Printer */
-                  char   *device)      /* I - New device URI */
-{
-  ipp_t                *request,               /* IPP Request */
-               *response;              /* IPP Response */
-  char         uri[HTTP_MAX_URI];      /* URI for printer/class */
-
-
-  DEBUG_printf(("set_printer_device(%p, \"%s\", \"%s\")\n", http, printer,
-                device));
-
- /*
-  * Build a CUPS_ADD_PRINTER request, which requires the following
-  * attributes:
-  *
-  *    attributes-charset
-  *    attributes-natural-language
-  *    printer-uri
-  */
-
-  request = ippNewRequest(CUPS_ADD_PRINTER);
-
-  httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                   "localhost", 0, "/printers/%s", printer);
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
-               "printer-uri", NULL, uri);
-
- /*
-  * Add the device URI...
-  */
-
-  if (device[0] == '/')
-  {
-   /*
-    * Convert filename to URI...
-    */
-
-    snprintf(uri, sizeof(uri), "file://%s", device);
-    ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
-                 uri);
-  }
-  else
-    ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
-                 device);
-
- /*
-  * Do the request and get back a response...
-  */
-
-  if ((response = cupsDoRequest(http, request, "/admin/")) == NULL)
-  {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-
-    return (1);
-  }
-  else if (response->request.status.status_code > IPP_OK_CONFLICT)
-  {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-
-    ippDelete(response);
-
-    return (1);
-  }
-  else
-  {
-    ippDelete(response);
-
-    return (0);
-  }
-}
-
-
-/*
- * 'set_printer_file()' - Set the interface script or PPD file.
- */
-
-static int                             /* O - 0 on success, 1 on fail */
-set_printer_file(http_t *http,         /* I - Server connection */
-                 char   *printer,      /* I - Printer */
-                char   *file)          /* I - PPD file or interface script */
-{
-  ipp_t                *request,               /* IPP Request */
-               *response;              /* IPP Response */
-  char         uri[HTTP_MAX_URI];      /* URI for printer/class */
-#ifdef HAVE_LIBZ
-  char         tempfile[1024];         /* Temporary filename */
-  int          fd;                     /* Temporary file */
-  gzFile       *gz;                    /* GZIP'd file */
-  char         buffer[8192];           /* Copy buffer */
-  int          bytes;                  /* Bytes in buffer */
-
-
-  DEBUG_printf(("set_printer_file(%p, \"%s\", \"%s\")\n", http, printer,
-                file));
-
- /*
-  * See if the file is gzip'd; if so, unzip it to a temporary file and
-  * send the uncompressed file.
-  */
-
-  if (!strcmp(file + strlen(file) - 3, ".gz"))
-  {
-   /*
-    * Yes, the file is compressed; uncompress to a temp file...
-    */
-
-    if ((fd = cupsTempFd(tempfile, sizeof(tempfile))) < 0)
-    {
-      _cupsLangPrintError(_("ERROR: Unable to create temporary file"));
-      return (1);
-    }
-
-    if ((gz = gzopen(file, "rb")) == NULL)
-    {
-      _cupsLangPrintf(stderr,
-                      _("lpadmin: Unable to open file \"%s\": %s\n"),
-                     file, strerror(errno));
-      close(fd);
-      unlink(tempfile);
-      return (1);
-    }
-
-    while ((bytes = gzread(gz, buffer, sizeof(buffer))) > 0)
-      write(fd, buffer, bytes);
-
-    close(fd);
-    gzclose(gz);
-
-    file = tempfile;
-  }
-#endif /* HAVE_LIBZ */
-
- /*
-  * Build a CUPS_ADD_PRINTER request, which requires the following
-  * attributes:
-  *
-  *    attributes-charset
-  *    attributes-natural-language
-  *    printer-uri
-  */
-
-  request = ippNewRequest(CUPS_ADD_PRINTER);
-
-  httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                   "localhost", 0, "/printers/%s", printer);
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
                "printer-uri", NULL, uri);
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+               "requesting-user-name", NULL, cupsUser());
+  ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_DELETEATTR, option, 0);
 
  /*
   * Do the request and get back a response...
   */
 
-  response = cupsDoFileRequest(http, request, "/admin/", file);
-  ippDelete(response);
-
-#ifdef HAVE_LIBZ
- /*
-  * Remove the temporary file as needed...
-  */
-
-  if (file == tempfile)
-    unlink(tempfile);
-#endif /* HAVE_LIBZ */
+  ippDelete(cupsDoRequest(http, request, "/admin/"));
 
   if (cupsLastError() > IPP_OK_CONFLICT)
   {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
+    _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
 
     return (1);
   }
@@ -1511,196 +1105,117 @@ set_printer_file(http_t *http,                /* I - Server connection */
 
 
 /*
- * 'set_printer_info()' - Set the printer description string.
+ * 'enable_printer()' - Enable a printer...
  */
 
 static int                             /* O - 0 on success, 1 on fail */
-set_printer_info(http_t *http,         /* I - Server connection */
-                 char   *printer,      /* I - Printer */
-                char   *info)          /* I - New description string */
+enable_printer(http_t *http,           /* I - Server connection */
+               char   *printer)                /* I - Printer to enable */
 {
-  ipp_t                *request,               /* IPP Request */
-               *response;              /* IPP Response */
+  ipp_t                *request;               /* IPP Request */
   char         uri[HTTP_MAX_URI];      /* URI for printer/class */
 
 
-  DEBUG_printf(("set_printer_info(%p, \"%s\", \"%s\")\n", http, printer,
-                info));
+  DEBUG_printf(("enable_printer(%p, \"%s\")\n", http, printer));
 
  /*
-  * Build a CUPS_ADD_PRINTER request, which requires the following
-  * attributes:
+  * Build a CUPS_ADD_MODIFY_PRINTER or CUPS_ADD_MODIFY_CLASS request, which
+  * require the following attributes:
   *
   *    attributes-charset
   *    attributes-natural-language
   *    printer-uri
+  *    requesting-user-name
+  *    printer-state
+  *    printer-is-accepting-jobs
   */
 
-  request = ippNewRequest(CUPS_ADD_PRINTER);
-
-  httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                   "localhost", 0, "/printers/%s", printer);
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
-               "printer-uri", NULL, uri);
-
- /*
-  * Add the info string...
-  */
-
-  ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info", NULL,
-               info);
-
- /*
-  * Do the request and get back a response...
-  */
-
-  if ((response = cupsDoRequest(http, request, "/admin/")) == NULL)
-  {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-    return (1);
-  }
-  else if (response->request.status.status_code > IPP_OK_CONFLICT)
-  {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-
-    ippDelete(response);
-
-    return (1);
-  }
+  if (get_printer_type(http, printer, uri, sizeof(uri)) & CUPS_PRINTER_CLASS)
+    request = ippNewRequest(CUPS_ADD_MODIFY_CLASS);
   else
-  {
-    ippDelete(response);
-
-    return (0);
-  }
-}
-
+    request = ippNewRequest(CUPS_ADD_MODIFY_PRINTER);
 
-/*
- * 'set_printer_location()' - Set the printer location string.
- */
-
-static int                             /* O - 0 on success, 1 on fail */
-set_printer_location(http_t *http,     /* I - Server connection */
-                     char   *printer,  /* I - Printer */
-                    char   *location)  /* I - New location string */
-{
-  ipp_t                *request,               /* IPP Request */
-               *response;              /* IPP Response */
-  char         uri[HTTP_MAX_URI];      /* URI for printer/class */
-
-
-  DEBUG_printf(("set_printer_location(%p, \"%s\", \"%s\")\n", http, printer,
-                location));
-
- /*
-  * Build a CUPS_ADD_PRINTER request, which requires the following
-  * attributes:
-  *
-  *    attributes-charset
-  *    attributes-natural-language
-  *    printer-uri
-  */
-
-  request = ippNewRequest(CUPS_ADD_PRINTER);
-
-  httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                   "localhost", 0, "/printers/%s", printer);
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
                "printer-uri", NULL, uri);
-
- /*
-  * Add the location string...
-  */
-
-  ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location", NULL,
-               location);
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+               "requesting-user-name", NULL, cupsUser());
+  ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
+                IPP_PRINTER_IDLE);
+  ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-accepting-jobs", 1);
 
  /*
   * Do the request and get back a response...
   */
 
-  if ((response = cupsDoRequest(http, request, "/admin/")) == NULL)
-  {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
+  ippDelete(cupsDoRequest(http, request, "/admin/"));
 
-    return (1);
-  }
-  else if (response->request.status.status_code > IPP_OK_CONFLICT)
+  if (cupsLastError() > IPP_OK_CONFLICT)
   {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-
-    ippDelete(response);
+    _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
 
     return (1);
   }
   else
-  {
-    ippDelete(response);
-
     return (0);
-  }
 }
 
 
 /*
- * 'set_printer_model()' - Set the driver model file.
+ * 'get_printer_type()' - Determine the printer type and URI.
  */
 
-static int                             /* O - 0 on success, 1 on fail */
-set_printer_model(http_t *http,                /* I - Server connection */
-                  char   *printer,     /* I - Printer */
-                 char   *model)        /* I - Driver model file */
+static cups_ptype_t                    /* O - printer-type value */
+get_printer_type(http_t *http,         /* I - Server connection */
+                 char   *printer,      /* I - Printer name */
+                char   *uri,           /* I - URI buffer */
+                 size_t urisize)       /* I - Size of URI buffer */
 {
-  ipp_t                *request,               /* IPP Request */
-               *response;              /* IPP Response */
-  char         uri[HTTP_MAX_URI];      /* URI for printer/class */
+  ipp_t                        *request,       /* IPP request */
+                       *response;      /* IPP response */
+  ipp_attribute_t      *attr;          /* printer-type attribute */
+  cups_ptype_t         type;           /* printer-type value */
 
 
  /*
-  * Build a CUPS_ADD_PRINTER request, which requires the following
+  * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
   * attributes:
   *
   *    attributes-charset
   *    attributes-natural-language
   *    printer-uri
-  *    ppd-name
+  *    requested-attributes
+  *    requesting-user-name
   */
 
-  request = ippNewRequest(CUPS_ADD_PRINTER);
+  httpAssembleURIf(HTTP_URI_CODING_ALL, uri, (int)urisize, "ipp", NULL, "localhost", ippPort(), "/printers/%s", printer);
 
-  httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                   "localhost", 0, "/printers/%s", printer);
+  request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
                "printer-uri", NULL, uri);
-
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
+               "requested-attributes", NULL, "printer-type");
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
-               "ppd-name", NULL, model);
+               "requesting-user-name", NULL, cupsUser());
 
  /*
-  * Do the request and get back a response...
+  * Do the request...
   */
 
-  if ((response = cupsDoRequest(http, request, "/admin/")) == NULL)
-  {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-
-    return (1);
-  }
-  else if (response->request.status.status_code > IPP_OK_CONFLICT)
+  response = cupsDoRequest(http, request, "/");
+  if ((attr = ippFindAttribute(response, "printer-type",
+                               IPP_TAG_ENUM)) != NULL)
   {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
-
-    ippDelete(response);
+    type = (cups_ptype_t)attr->values[0].integer;
 
-    return (1);
+    if (type & CUPS_PRINTER_CLASS)
+      httpAssembleURIf(HTTP_URI_CODING_ALL, uri, (int)urisize, "ipp", NULL, "localhost", ippPort(), "/classes/%s", printer);
   }
   else
-  {
-    ippDelete(response);
+    type = CUPS_PRINTER_LOCAL;
 
-    return (0);
-  }
+  ippDelete(response);
+
+  return (type);
 }
 
 
@@ -1713,12 +1228,10 @@ set_printer_options(
     http_t        *http,               /* I - Server connection */
     char          *printer,            /* I - Printer */
     int           num_options,         /* I - Number of options */
-    cups_option_t *options)            /* I - Options */
+    cups_option_t *options,            /* I - Options */
+    char          *file)               /* I - PPD file/interface script */
 {
-  ipp_t                *request,               /* IPP Request */
-               *response;              /* IPP Response */
-  ipp_attribute_t *attr;               /* IPP attribute */
-  ipp_op_t     op;                     /* Operation to perform */
+  ipp_t                *request;               /* IPP Request */
   const char   *ppdfile;               /* PPD filename */
   int          ppdchanged;             /* PPD changed? */
   ppd_file_t   *ppd;                   /* PPD file */
@@ -1728,94 +1241,60 @@ set_printer_options(
                keyword[1024],          /* Keyword from Default line */
                *keyptr,                /* Pointer into keyword... */
                tempfile[1024];         /* Temporary filename */
-  FILE         *in,                    /* PPD file */
+  cups_file_t  *in,                    /* PPD file */
                *out;                   /* Temporary file */
-  int          outfd;                  /* Temporary file descriptor */
-  const char   *protocol;              /* Old protocol option */
-
+  const char   *protocol,              /* Old protocol option */
+               *customval,             /* Custom option value */
+               *boolval;               /* Boolean value */
+  int          wrote_ipp_supplies = 0, /* Wrote cupsIPPSupplies keyword? */
+               wrote_snmp_supplies = 0;/* Wrote cupsSNMPSupplies keyword? */
 
-  DEBUG_printf(("set_printer_options(%p, \"%s\", %d, %p)\n", http, printer,
-                num_options, options));
 
-  httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                   "localhost", 0, "/printers/%s", printer);
+  DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
+                "options=%p, file=\"%s\")\n", http, printer, num_options,
+               options, file));
 
  /*
-  * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
-  * attributes:
-  *
-  *    attributes-charset
-  *    attributes-natural-language
-  *    printer-uri
-  *    requested-attributes
-  */
-
-  request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
-
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
-               "printer-uri", NULL, uri);
-
-  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
-               "requested-attributes", NULL, "printer-type");
-
- /*
-  * Do the request...
-  */
-
-  op = CUPS_ADD_PRINTER;
-
-  if ((response = cupsDoRequest(http, request, "/")) != NULL)
-  {
-   /*
-    * See what kind of printer or class it is...
-    */
-
-    if ((attr = ippFindAttribute(response, "printer-type",
-                                 IPP_TAG_ENUM)) != NULL)
-    {
-      if (attr->values[0].integer & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
-      {
-        op = CUPS_ADD_CLASS;
-       httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
-                        "localhost", 0, "/classes/%s", printer);
-      }
-    }
-
-    ippDelete(response);
-  }
-
- /*
-  * Build a CUPS_ADD_PRINTER or CUPS_ADD_CLASS request, which requires
-  * the following attributes:
+  * Build a CUPS_ADD_MODIFY_PRINTER or CUPS_ADD_MODIFY_CLASS request, which
+  * requires the following attributes:
   *
   *    attributes-charset
   *    attributes-natural-language
   *    printer-uri
+  *    requesting-user-name
   *    other options
   */
 
-  request = ippNewRequest(op);
+  if (get_printer_type(http, printer, uri, sizeof(uri)) & CUPS_PRINTER_CLASS)
+    request = ippNewRequest(CUPS_ADD_MODIFY_CLASS);
+  else
+    request = ippNewRequest(CUPS_ADD_MODIFY_PRINTER);
 
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
                "printer-uri", NULL, uri);
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+               "requesting-user-name", NULL, cupsUser());
 
  /*
   * Add the options...
   */
 
+  cupsEncodeOptions2(request, num_options, options, IPP_TAG_OPERATION);
   cupsEncodeOptions2(request, num_options, options, IPP_TAG_PRINTER);
 
   if ((protocol = cupsGetOption("protocol", num_options, options)) != NULL)
   {
-    if (!strcasecmp(protocol, "bcp"))
+    if (!_cups_strcasecmp(protocol, "bcp"))
       ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
                    NULL, "bcp");
-    else if (!strcasecmp(protocol, "tbcp"))
+    else if (!_cups_strcasecmp(protocol, "tbcp"))
       ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
                    NULL, "tbcp");
   }
 
-  if (op == CUPS_ADD_PRINTER)
+  if (file)
+    ppdfile = file;
+  else if (request->request.op.operation_id == CUPS_ADD_MODIFY_PRINTER)
     ppdfile = cupsGetPPD(printer);
   else
     ppdfile = NULL;
@@ -1830,33 +1309,54 @@ set_printer_options(
     ppdMarkDefaults(ppd);
     cupsMarkOptions(ppd, num_options, options);
 
-    if ((outfd = cupsTempFd(tempfile, sizeof(tempfile))) < 0)
+    if ((out = cupsTempFile2(tempfile, sizeof(tempfile))) == NULL)
     {
-      _cupsLangPrintError(_("ERROR: Unable to create temporary file"));
+      _cupsLangPrintError(NULL, _("lpadmin: Unable to create temporary file"));
       ippDelete(request);
-      unlink(ppdfile);
+      if (ppdfile != file)
+        unlink(ppdfile);
       return (1);
     }
 
-    if ((in = fopen(ppdfile, "rb")) == NULL)
+    if ((in = cupsFileOpen(ppdfile, "r")) == NULL)
     {
       _cupsLangPrintf(stderr,
-                      _("lpadmin: Unable to open PPD file \"%s\" - %s\n"),
+                      _("lpadmin: Unable to open PPD file \"%s\" - %s"),
                      ppdfile, strerror(errno));
       ippDelete(request);
-      unlink(ppdfile);
-      close(outfd);
+      if (ppdfile != file)
+       unlink(ppdfile);
+      cupsFileClose(out);
       unlink(tempfile);
       return (1);
     }
 
-    out        = fdopen(outfd, "wb");
     ppdchanged = 0;
 
-    while (get_line(line, sizeof(line), in) != NULL)
+    while (cupsFileGets(in, line, sizeof(line)))
     {
-      if (strncmp(line, "*Default", 8))
-        fprintf(out, "%s\n", line);
+      if (!strncmp(line, "*cupsIPPSupplies:", 17) &&
+         (boolval = cupsGetOption("cupsIPPSupplies", num_options,
+                                  options)) != NULL)
+      {
+        wrote_ipp_supplies = 1;
+        cupsFilePrintf(out, "*cupsIPPSupplies: %s\n",
+                      (!_cups_strcasecmp(boolval, "true") ||
+                       !_cups_strcasecmp(boolval, "yes") ||
+                       !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
+      }
+      else if (!strncmp(line, "*cupsSNMPSupplies:", 18) &&
+              (boolval = cupsGetOption("cupsSNMPSupplies", num_options,
+                                       options)) != NULL)
+      {
+        wrote_snmp_supplies = 1;
+        cupsFilePrintf(out, "*cupsSNMPSupplies: %s\n",
+                      (!_cups_strcasecmp(boolval, "true") ||
+                       !_cups_strcasecmp(boolval, "yes") ||
+                       !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
+      }
+      else if (strncmp(line, "*Default", 8))
+        cupsFilePrintf(out, "%s\n", line);
       else
       {
        /*
@@ -1886,17 +1386,47 @@ set_printer_options(
 
         if (choice && strcmp(choice->choice, keyptr))
        {
-         fprintf(out, "*Default%s: %s\n", keyword, choice->choice);
-         ppdchanged = 1;
+         if (strcmp(choice->choice, "Custom"))
+         {
+           cupsFilePrintf(out, "*Default%s: %s\n", keyword, choice->choice);
+           ppdchanged = 1;
+         }
+         else if ((customval = cupsGetOption(keyword, num_options,
+                                             options)) != NULL)
+         {
+           cupsFilePrintf(out, "*Default%s: %s\n", keyword, customval);
+           ppdchanged = 1;
+         }
+         else
+           cupsFilePrintf(out, "%s\n", line);
        }
        else
-         fprintf(out, "%s\n", line);
+         cupsFilePrintf(out, "%s\n", line);
       }
     }
 
-    fclose(in);
-    fclose(out);
-    close(outfd);
+    if (!wrote_ipp_supplies &&
+       (boolval = cupsGetOption("cupsIPPSupplies", num_options,
+                                options)) != NULL)
+    {
+      cupsFilePrintf(out, "*cupsIPPSupplies: %s\n",
+                    (!_cups_strcasecmp(boolval, "true") ||
+                     !_cups_strcasecmp(boolval, "yes") ||
+                     !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
+    }
+
+    if (!wrote_snmp_supplies &&
+        (boolval = cupsGetOption("cupsSNMPSupplies", num_options,
+                                options)) != NULL)
+    {
+      cupsFilePrintf(out, "*cupsSNMPSupplies: %s\n",
+                    (!_cups_strcasecmp(boolval, "true") ||
+                     !_cups_strcasecmp(boolval, "yes") ||
+                     !_cups_strcasecmp(boolval, "on")) ? "True" : "False");
+    }
+
+    cupsFileClose(in);
+    cupsFileClose(out);
     ppdClose(ppd);
 
    /*
@@ -1904,14 +1434,15 @@ set_printer_options(
     */
 
     ippDelete(cupsDoFileRequest(http, request, "/admin/",
-                                ppdchanged ? tempfile : NULL));
+                                ppdchanged ? tempfile : file));
 
    /*
     * Clean up temp files... (TODO: catch signals in case we CTRL-C during
     * lpadmin)
     */
 
-    unlink(ppdfile);
+    if (ppdfile != file)
+      unlink(ppdfile);
     unlink(tempfile);
   }
   else
@@ -1929,7 +1460,7 @@ set_printer_options(
 
   if (cupsLastError() > IPP_OK_CONFLICT)
   {
-    _cupsLangPrintf(stderr, "lpadmin: %s\n", cupsLastErrorString());
+    _cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
 
     return (1);
   }
@@ -1968,5 +1499,5 @@ validate_name(const char *name)           /* I - Name to check */
 
 
 /*
- * End of "$Id: lpadmin.c 7720 2008-07-11 22:46:21Z mike $".
+ * End of "$Id$".
  */