4 * "lpadmin" command for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Parse options and configure the scheduler.
18 * add_printer_to_class() - Add a printer to a class.
19 * default_printer() - Set the default printing destination.
20 * delete_printer() - Delete a printer from the system.
21 * delete_printer_from_class() - Delete a printer from a class.
22 * enable_printer() - Enable a printer.
23 * set_printer_options() - Set the printer options and/or file.
24 * validate_name() - Make sure the printer name only contains
29 * Include necessary headers...
32 #include <cups/cups-private.h>
39 static int add_printer_to_class(http_t
*http
, char *printer
, char *pclass
);
40 static int default_printer(http_t
*http
, char *printer
);
41 static int delete_printer(http_t
*http
, char *printer
);
42 static int delete_printer_from_class(http_t
*http
, char *printer
,
44 static int enable_printer(http_t
*http
, char *printer
);
45 static int set_printer_options(http_t
*http
, char *printer
,
46 int num_options
, cups_option_t
*options
,
48 static int validate_name(const char *name
);
52 * 'main()' - Parse options and configure the scheduler.
56 main(int argc
, /* I - Number of command-line arguments */
57 char *argv
[]) /* I - Command-line arguments */
59 int i
; /* Looping var */
60 http_t
*http
; /* Connection to server */
61 char *printer
, /* Destination printer */
62 *pclass
, /* Printer class name */
63 *val
; /* Pointer to allow/deny value */
64 int num_options
; /* Number of options */
65 cups_option_t
*options
; /* Options */
66 char *file
; /* New PPD file/interface script */
77 for (i
= 1; i
< argc
; i
++)
78 if (argv
[i
][0] == '-')
81 case 'c' : /* Add printer to class */
84 http
= httpConnectEncrypt(cupsServer(), ippPort(),
89 _cupsLangPrintf(stderr
,
90 _("lpadmin: Unable to connect to server: %s\n"),
99 _("lpadmin: Unable to add a printer to the class:\n"
100 " You must specify a printer name "
106 pclass
= argv
[i
] + 2;
113 _cupsLangPuts(stderr
,
114 _("lpadmin: Expected class name after \'-c\' "
122 if (!validate_name(pclass
))
124 _cupsLangPuts(stderr
,
125 _("lpadmin: Class name can only contain printable "
130 if (add_printer_to_class(http
, printer
, pclass
))
134 case 'd' : /* Set as default destination */
137 http
= httpConnectEncrypt(cupsServer(), ippPort(),
142 _cupsLangPrintf(stderr
,
143 _("lpadmin: Unable to connect to server: %s\n"),
150 printer
= argv
[i
] + 2;
157 _cupsLangPuts(stderr
,
158 _("lpadmin: Expected printer name after \'-d\' "
166 if (!validate_name(printer
))
168 _cupsLangPuts(stderr
,
169 _("lpadmin: Printer name can only contain "
170 "printable characters\n"));
174 if (default_printer(http
, printer
))
180 case 'h' : /* Connect to host */
187 if (argv
[i
][2] != '\0')
188 cupsSetServer(argv
[i
] + 2);
195 _cupsLangPuts(stderr
,
196 _("lpadmin: Expected hostname after \'-h\' "
201 cupsSetServer(argv
[i
]);
205 case 'i' : /* Use the specified interface script */
214 _cupsLangPuts(stderr
,
215 _("lpadmin: Expected interface after \'-i\' "
224 case 'E' : /* Enable the printer */
228 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
231 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
233 _cupsLangPrintf(stderr
,
234 _("%s: Sorry, no encryption support compiled in\n"),
236 #endif /* HAVE_SSL */
242 http
= httpConnectEncrypt(cupsServer(), ippPort(),
247 _cupsLangPrintf(stderr
,
248 _("lpadmin: Unable to connect to server: %s\n"),
254 if (enable_printer(http
, printer
))
258 case 'm' : /* Use the specified standard script/PPD file */
260 num_options
= cupsAddOption("ppd-name", argv
[i
] + 2, num_options
,
268 _cupsLangPuts(stderr
,
269 _("lpadmin: Expected model after \'-m\' "
274 num_options
= cupsAddOption("ppd-name", argv
[i
], num_options
,
279 case 'o' : /* Set option */
281 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
288 _cupsLangPuts(stderr
,
289 _("lpadmin: Expected name=value after \'-o\' "
294 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
298 case 'p' : /* Add/modify a printer */
300 printer
= argv
[i
] + 2;
307 _cupsLangPuts(stderr
,
308 _("lpadmin: Expected printer after \'-p\' "
316 if (!validate_name(printer
))
318 _cupsLangPuts(stderr
,
319 _("lpadmin: Printer name can only contain "
320 "printable characters\n"));
325 case 'r' : /* Remove printer from class */
328 http
= httpConnectEncrypt(cupsServer(), ippPort(),
333 _cupsLangPrintf(stderr
,
334 _("lpadmin: Unable to connect to server: %s\n"),
342 _cupsLangPuts(stderr
,
343 _("lpadmin: Unable to remove a printer from the "
345 " You must specify a printer name "
351 pclass
= argv
[i
] + 2;
358 _cupsLangPuts(stderr
,
359 _("lpadmin: Expected class after \'-r\' "
367 if (!validate_name(pclass
))
369 _cupsLangPuts(stderr
,
370 _("lpadmin: Class name can only contain printable "
375 if (delete_printer_from_class(http
, printer
, pclass
))
379 case 'U' : /* Username */
380 if (argv
[i
][2] != '\0')
381 cupsSetUser(argv
[i
] + 2);
387 _cupsLangPrintf(stderr
,
388 _("%s: Error - expected username after "
394 cupsSetUser(argv
[i
]);
398 case 'u' : /* Allow/deny users */
407 _cupsLangPuts(stderr
,
408 _("lpadmin: Expected allow/deny:userlist after "
416 if (!strncasecmp(val
, "allow:", 6))
417 num_options
= cupsAddOption("requesting-user-name-allowed",
418 val
+ 6, num_options
, &options
);
419 else if (!strncasecmp(val
, "deny:", 5))
420 num_options
= cupsAddOption("requesting-user-name-denied",
421 val
+ 5, num_options
, &options
);
424 _cupsLangPrintf(stderr
,
425 _("lpadmin: Unknown allow/deny option \"%s\"\n"),
431 case 'v' : /* Set the device-uri attribute */
433 num_options
= cupsAddOption("device-uri", argv
[i
] + 2,
434 num_options
, &options
);
441 _cupsLangPuts(stderr
,
442 _("lpadmin: Expected device URI after \'-v\' "
447 num_options
= cupsAddOption("device-uri", argv
[i
],
448 num_options
, &options
);
452 case 'x' : /* Delete a printer */
455 http
= httpConnectEncrypt(cupsServer(), ippPort(),
460 _cupsLangPrintf(stderr
,
461 _("lpadmin: Unable to connect to server: %s\n"),
468 printer
= argv
[i
] + 2;
475 _cupsLangPuts(stderr
,
476 _("lpadmin: Expected printer or class after "
484 if (!validate_name(printer
))
486 _cupsLangPuts(stderr
,
487 _("lpadmin: Printer name can only contain "
488 "printable characters\n"));
492 if (delete_printer(http
, printer
))
498 case 'D' : /* Set the printer-info attribute */
500 num_options
= cupsAddOption("printer-info", argv
[i
] + 2,
501 num_options
, &options
);
508 _cupsLangPuts(stderr
,
509 _("lpadmin: Expected description after "
514 num_options
= cupsAddOption("printer-info", argv
[i
],
515 num_options
, &options
);
519 case 'I' : /* Set the supported file types (ignored) */
524 _cupsLangPuts(stderr
,
525 _("lpadmin: Expected file type(s) after \'-I\' "
530 _cupsLangPuts(stderr
,
531 _("lpadmin: Warning - content type list ignored\n"));
534 case 'L' : /* Set the printer-location attribute */
536 num_options
= cupsAddOption("printer-location", argv
[i
] + 2,
537 num_options
, &options
);
544 _cupsLangPuts(stderr
,
545 _("lpadmin: Expected location after \'-L\' "
550 num_options
= cupsAddOption("printer-location", argv
[i
],
551 num_options
, &options
);
555 case 'P' : /* Use the specified PPD file */
564 _cupsLangPuts(stderr
,
565 _("lpadmin: Expected PPD after \'-P\' option\n"));
574 _cupsLangPrintf(stderr
,
575 _("lpadmin: Unknown option \'%c\'\n"), argv
[i
][1]);
580 _cupsLangPrintf(stderr
, _("lpadmin: Unknown argument \'%s\'\n"),
586 * Set options as needed...
589 if (num_options
|| file
)
593 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
597 _cupsLangPrintf(stderr
,
598 _("lpadmin: Unable to connect to server: %s\n"),
606 _cupsLangPuts(stderr
,
607 _("lpadmin: Unable to set the printer options:\n"
608 " You must specify a printer name first\n"));
612 if (set_printer_options(http
, printer
, num_options
, options
, file
))
618 _cupsLangPuts(stdout
,
621 " lpadmin [-h server] -d destination\n"
622 " lpadmin [-h server] -x destination\n"
623 " lpadmin [-h server] -p printer [-c add-class] "
624 "[-i interface] [-m model]\n"
625 " [-r remove-class] [-v device] "
627 " [-P ppd-file] [-o name=value]\n"
628 " [-u allow:user,user] "
629 "[-u deny:user,user]\n"
641 * 'add_printer_to_class()' - Add a printer to a class.
644 static int /* O - 0 on success, 1 on fail */
645 add_printer_to_class(http_t
*http
, /* I - Server connection */
646 char *printer
, /* I - Printer to add */
647 char *pclass
) /* I - Class to add to */
649 int i
; /* Looping var */
650 ipp_t
*request
, /* IPP Request */
651 *response
; /* IPP Response */
652 ipp_attribute_t
*attr
, /* Current attribute */
653 *members
; /* Members in class */
654 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
657 DEBUG_printf(("add_printer_to_class(%p, \"%s\", \"%s\")\n", http
,
661 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
665 * attributes-natural-language
669 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
671 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
672 "localhost", 0, "/classes/%s", pclass
);
673 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
674 "printer-uri", NULL
, uri
);
677 * Do the request and get back a response...
680 response
= cupsDoRequest(http
, request
, "/");
683 * Build a CUPS_ADD_CLASS request, which requires the following
687 * attributes-natural-language
692 request
= ippNewRequest(CUPS_ADD_CLASS
);
694 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
695 "printer-uri", NULL
, uri
);
698 * See if the printer is already in the class...
701 if (response
!= NULL
&&
702 (members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) != NULL
)
703 for (i
= 0; i
< members
->num_values
; i
++)
704 if (strcasecmp(printer
, members
->values
[i
].string
.text
) == 0)
706 _cupsLangPrintf(stderr
,
707 _("lpadmin: Printer %s is already a member of class %s.\n"),
715 * OK, the printer isn't part of the class, so add it...
718 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
719 "localhost", 0, "/printers/%s", printer
);
721 if (response
!= NULL
&&
722 (members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
)) != NULL
)
725 * Add the printer to the existing list...
728 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
729 "member-uris", members
->num_values
+ 1, NULL
, NULL
);
730 for (i
= 0; i
< members
->num_values
; i
++)
731 attr
->values
[i
].string
.text
= _cupsStrAlloc(members
->values
[i
].string
.text
);
733 attr
->values
[i
].string
.text
= _cupsStrAlloc(uri
);
736 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "member-uris", NULL
,
740 * Then send the request...
745 if ((response
= cupsDoRequest(http
, request
, "/admin/")) == NULL
)
747 _cupsLangPrintf(stderr
, "lpadmin: %s\n", cupsLastErrorString());
751 else if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
753 _cupsLangPrintf(stderr
, "lpadmin: %s\n", cupsLastErrorString());
769 * 'default_printer()' - Set the default printing destination.
772 static int /* O - 0 on success, 1 on fail */
773 default_printer(http_t
*http
, /* I - Server connection */
774 char *printer
) /* I - Printer name */
776 ipp_t
*request
, /* IPP Request */
777 *response
; /* IPP Response */
778 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
781 DEBUG_printf(("default_printer(%p, \"%s\")\n", http
, printer
));
784 * Build a CUPS_SET_DEFAULT request, which requires the following
788 * attributes-natural-language
792 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
793 "localhost", 0, "/printers/%s", printer
);
795 request
= ippNewRequest(CUPS_SET_DEFAULT
);
797 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
798 "printer-uri", NULL
, uri
);
801 * Do the request and get back a response...
804 if ((response
= cupsDoRequest(http
, request
, "/admin/")) == NULL
)
806 _cupsLangPrintf(stderr
, "lpadmin: %s\n", cupsLastErrorString());
810 else if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
812 _cupsLangPrintf(stderr
, "lpadmin: %s\n", cupsLastErrorString());
828 * 'delete_printer()' - Delete a printer from the system...
831 static int /* O - 0 on success, 1 on fail */
832 delete_printer(http_t
*http
, /* I - Server connection */
833 char *printer
) /* I - Printer to delete */
835 ipp_t
*request
, /* IPP Request */
836 *response
; /* IPP Response */
837 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
840 DEBUG_printf(("delete_printer(%p, \"%s\")\n", http
, printer
));
843 * Build a CUPS_DELETE_PRINTER request, which requires the following
847 * attributes-natural-language
851 request
= ippNewRequest(CUPS_DELETE_PRINTER
);
853 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
854 "localhost", 0, "/printers/%s", printer
);
855 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
856 "printer-uri", NULL
, uri
);
859 * Do the request and get back a response...
862 if ((response
= cupsDoRequest(http
, request
, "/admin/")) == NULL
)
864 _cupsLangPrintf(stderr
, "lpadmin: %s\n", cupsLastErrorString());
868 else if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
870 _cupsLangPrintf(stderr
, "lpadmin: %s\n", cupsLastErrorString());
886 * 'delete_printer_from_class()' - Delete a printer from a class.
889 static int /* O - 0 on success, 1 on fail */
890 delete_printer_from_class(
891 http_t
*http
, /* I - Server connection */
892 char *printer
, /* I - Printer to remove */
893 char *pclass
) /* I - Class to remove from */
895 int i
, j
, k
; /* Looping vars */
896 ipp_t
*request
, /* IPP Request */
897 *response
; /* IPP Response */
898 ipp_attribute_t
*attr
, /* Current attribute */
899 *members
; /* Members in class */
900 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
903 DEBUG_printf(("delete_printer_from_class(%p, \"%s\", \"%s\")\n", http
,
907 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
911 * attributes-natural-language
915 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
917 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
918 "localhost", 0, "/classes/%s", pclass
);
919 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
920 "printer-uri", NULL
, uri
);
923 * Do the request and get back a response...
926 if ((response
= cupsDoRequest(http
, request
, "/classes/")) == NULL
||
927 response
->request
.status
.status_code
== IPP_NOT_FOUND
)
929 _cupsLangPrintf(stderr
, "lpadmin: %s\n", cupsLastErrorString());
937 * See if the printer is already in the class...
940 if ((members
= ippFindAttribute(response
, "member-names", IPP_TAG_NAME
)) == NULL
)
942 _cupsLangPuts(stderr
, _("lpadmin: No member names were seen\n"));
949 for (i
= 0; i
< members
->num_values
; i
++)
950 if (!strcasecmp(printer
, members
->values
[i
].string
.text
))
953 if (i
>= members
->num_values
)
955 _cupsLangPrintf(stderr
,
956 _("lpadmin: Printer %s is not a member of class %s.\n"),
964 if (members
->num_values
== 1)
967 * Build a CUPS_DELETE_CLASS request, which requires the following
971 * attributes-natural-language
975 request
= ippNewRequest(CUPS_DELETE_CLASS
);
977 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
978 "printer-uri", NULL
, uri
);
983 * Build a CUPS_ADD_CLASS request, which requires the following
987 * attributes-natural-language
992 request
= ippNewRequest(CUPS_ADD_CLASS
);
994 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
995 "printer-uri", NULL
, uri
);
998 * Delete the printer from the class...
1001 members
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
);
1002 attr
= ippAddStrings(request
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1003 "member-uris", members
->num_values
- 1, NULL
, NULL
);
1005 for (j
= 0, k
= 0; j
< members
->num_values
; j
++)
1007 attr
->values
[k
++].string
.text
=
1008 _cupsStrAlloc(members
->values
[j
].string
.text
);
1012 * Then send the request...
1015 ippDelete(response
);
1017 if ((response
= cupsDoRequest(http
, request
, "/admin/")) == NULL
)
1019 _cupsLangPrintf(stderr
, "lpadmin: %s\n", cupsLastErrorString());
1023 else if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
1025 _cupsLangPrintf(stderr
, "lpadmin: %s\n", cupsLastErrorString());
1027 ippDelete(response
);
1033 ippDelete(response
);
1041 * 'enable_printer()' - Enable a printer...
1044 static int /* O - 0 on success, 1 on fail */
1045 enable_printer(http_t
*http
, /* I - Server connection */
1046 char *printer
) /* I - Printer to enable */
1048 ipp_t
*request
, /* IPP Request */
1049 *response
; /* IPP Response */
1050 char uri
[HTTP_MAX_URI
]; /* URI for printer/class */
1053 DEBUG_printf(("enable_printer(%p, \"%s\")\n", http
, printer
));
1056 * Build a CUPS_ADD_PRINTER request, which requires the following
1059 * attributes-charset
1060 * attributes-natural-language
1063 * printer-is-accepting-jobs
1066 request
= ippNewRequest(CUPS_ADD_PRINTER
);
1068 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1069 "localhost", 0, "/printers/%s", printer
);
1070 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1071 "printer-uri", NULL
, uri
);
1073 ippAddInteger(request
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
1076 ippAddBoolean(request
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs", 1);
1079 * Do the request and get back a response...
1082 if ((response
= cupsDoRequest(http
, request
, "/admin/")) == NULL
)
1084 _cupsLangPrintf(stderr
, "lpadmin: %s\n", cupsLastErrorString());
1088 else if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
1090 _cupsLangPrintf(stderr
, "lpadmin: %s\n", cupsLastErrorString());
1092 ippDelete(response
);
1098 ippDelete(response
);
1106 * 'set_printer_options()' - Set the printer options.
1109 static int /* O - 0 on success, 1 on fail */
1110 set_printer_options(
1111 http_t
*http
, /* I - Server connection */
1112 char *printer
, /* I - Printer */
1113 int num_options
, /* I - Number of options */
1114 cups_option_t
*options
, /* I - Options */
1115 char *file
) /* I - PPD file/interface script */
1117 ipp_t
*request
, /* IPP Request */
1118 *response
; /* IPP Response */
1119 ipp_attribute_t
*attr
; /* IPP attribute */
1120 ipp_op_t op
; /* Operation to perform */
1121 const char *ppdfile
; /* PPD filename */
1122 int ppdchanged
; /* PPD changed? */
1123 ppd_file_t
*ppd
; /* PPD file */
1124 ppd_choice_t
*choice
; /* Marked choice */
1125 char uri
[HTTP_MAX_URI
], /* URI for printer/class */
1126 line
[1024], /* Line from PPD file */
1127 keyword
[1024], /* Keyword from Default line */
1128 *keyptr
, /* Pointer into keyword... */
1129 tempfile
[1024]; /* Temporary filename */
1130 cups_file_t
*in
, /* PPD file */
1131 *out
; /* Temporary file */
1132 const char *protocol
, /* Old protocol option */
1133 *customval
; /* Custom option value */
1136 DEBUG_printf(("set_printer_options(http=%p, printer=\"%s\", num_options=%d, "
1137 "options=%p, file=\"%s\")\n", http
, printer
, num_options
,
1140 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1141 "localhost", 0, "/printers/%s", printer
);
1144 * Build a GET_PRINTER_ATTRIBUTES request, which requires the following
1147 * attributes-charset
1148 * attributes-natural-language
1150 * requested-attributes
1153 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1155 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1156 "printer-uri", NULL
, uri
);
1157 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1158 "requested-attributes", NULL
, "printer-type");
1164 op
= CUPS_ADD_MODIFY_PRINTER
;
1166 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1169 * See what kind of printer or class it is...
1172 if ((attr
= ippFindAttribute(response
, "printer-type",
1173 IPP_TAG_ENUM
)) != NULL
)
1175 if (attr
->values
[0].integer
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
1177 op
= CUPS_ADD_MODIFY_CLASS
;
1178 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1179 "localhost", 0, "/classes/%s", printer
);
1183 ippDelete(response
);
1187 * Build a CUPS_ADD_MODIFY_PRINTER or CUPS_ADD_MODIFY_CLASS request, which
1188 * requires the following attributes:
1190 * attributes-charset
1191 * attributes-natural-language
1196 request
= ippNewRequest(op
);
1198 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1199 "printer-uri", NULL
, uri
);
1200 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1201 "requesting-user-name", NULL
, cupsUser());
1204 * Add the options...
1207 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_PRINTER
);
1209 if ((protocol
= cupsGetOption("protocol", num_options
, options
)) != NULL
)
1211 if (!strcasecmp(protocol
, "bcp"))
1212 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1214 else if (!strcasecmp(protocol
, "tbcp"))
1215 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
1221 else if (op
== CUPS_ADD_MODIFY_PRINTER
)
1222 ppdfile
= cupsGetPPD(printer
);
1226 if (ppdfile
!= NULL
)
1229 * Set default options in the PPD file...
1232 ppd
= ppdOpenFile(ppdfile
);
1233 ppdMarkDefaults(ppd
);
1234 cupsMarkOptions(ppd
, num_options
, options
);
1236 if ((out
= cupsTempFile2(tempfile
, sizeof(tempfile
))) < 0)
1238 _cupsLangPrintError(NULL
, _("lpadmin: Unable to create temporary file"));
1240 if (ppdfile
!= file
)
1245 if ((in
= cupsFileOpen(ppdfile
, "r")) == NULL
)
1247 _cupsLangPrintf(stderr
,
1248 _("lpadmin: Unable to open PPD file \"%s\" - %s\n"),
1249 ppdfile
, strerror(errno
));
1251 if (ppdfile
!= file
)
1260 while (cupsFileGets(in
, line
, sizeof(line
)))
1262 if (strncmp(line
, "*Default", 8))
1263 cupsFilePrintf(out
, "%s\n", line
);
1267 * Get default option name...
1270 strlcpy(keyword
, line
+ 8, sizeof(keyword
));
1272 for (keyptr
= keyword
; *keyptr
; keyptr
++)
1273 if (*keyptr
== ':' || isspace(*keyptr
& 255))
1277 while (isspace(*keyptr
& 255))
1280 if (!strcmp(keyword
, "PageRegion") ||
1281 !strcmp(keyword
, "PageSize") ||
1282 !strcmp(keyword
, "PaperDimension") ||
1283 !strcmp(keyword
, "ImageableArea"))
1285 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) == NULL
)
1286 choice
= ppdFindMarkedChoice(ppd
, "PageRegion");
1289 choice
= ppdFindMarkedChoice(ppd
, keyword
);
1291 if (choice
&& strcmp(choice
->choice
, keyptr
))
1293 if (strcmp(choice
->choice
, "Custom"))
1295 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, choice
->choice
);
1298 else if ((customval
= cupsGetOption(keyword
, num_options
,
1301 cupsFilePrintf(out
, "*Default%s: %s\n", keyword
, customval
);
1305 cupsFilePrintf(out
, "%s\n", line
);
1308 cupsFilePrintf(out
, "%s\n", line
);
1320 ippDelete(cupsDoFileRequest(http
, request
, "/admin/",
1321 ppdchanged
? tempfile
: file
));
1324 * Clean up temp files... (TODO: catch signals in case we CTRL-C during
1328 if (ppdfile
!= file
)
1335 * No PPD file - just set the options...
1338 ippDelete(cupsDoRequest(http
, request
, "/admin/"));
1342 * Check the response...
1345 if (cupsLastError() > IPP_OK_CONFLICT
)
1347 _cupsLangPrintf(stderr
, "lpadmin: %s\n", cupsLastErrorString());
1357 * 'validate_name()' - Make sure the printer name only contains valid chars.
1360 static int /* O - 0 if name is no good, 1 if name is good */
1361 validate_name(const char *name
) /* I - Name to check */
1363 const char *ptr
; /* Pointer into name */
1367 * Scan the whole name...
1370 for (ptr
= name
; *ptr
; ptr
++)
1373 else if ((*ptr
>= 0 && *ptr
<= ' ') || *ptr
== 127 || *ptr
== '/' ||
1378 * All the characters are good; validate the length, too...
1381 return ((ptr
- name
) < 128);