]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cupsctl.c
Save/restore line number to fix error line reporting.
[thirdparty/cups.git] / systemv / cupsctl.c
1 /*
2 * Scheduler control program for CUPS.
3 *
4 * Copyright 2007-2012 by Apple Inc.
5 * Copyright 2006-2007 by Easy Software Products.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
8 */
9
10 /*
11 * Include necessary headers...
12 */
13
14 #include <cups/cups-private.h>
15 #include <cups/adminutil.h>
16
17
18 /*
19 * Local functions...
20 */
21
22 static void usage(const char *opt) _CUPS_NORETURN;
23
24
25 /*
26 * 'main()' - Get/set server settings.
27 */
28
29 int /* O - Exit status */
30 main(int argc, /* I - Number of command-line args */
31 char *argv[]) /* I - Command-line arguments */
32 {
33 int i, /* Looping var */
34 num_settings; /* Number of settings */
35 cups_option_t *settings; /* Settings */
36 const char *opt; /* Current option character */
37 http_t *http; /* Connection to server */
38
39
40 /*
41 * Process the command-line...
42 */
43
44 _cupsSetLocale(argv);
45
46 num_settings = 0;
47 settings = NULL;
48
49 for (i = 1; i < argc; i ++)
50 {
51 if (argv[i][0] == '-')
52 {
53 if (argv[i][1] == '-')
54 {
55 if (!strcmp(argv[i], "--debug-logging"))
56 num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING, "1",
57 num_settings, &settings);
58 else if (!strcmp(argv[i], "--no-debug-logging"))
59 num_settings = cupsAddOption(CUPS_SERVER_DEBUG_LOGGING, "0",
60 num_settings, &settings);
61 else if (!strcmp(argv[i], "--remote-admin"))
62 num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN, "1",
63 num_settings, &settings);
64 else if (!strcmp(argv[i], "--no-remote-admin"))
65 num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ADMIN, "0",
66 num_settings, &settings);
67 else if (!strcmp(argv[i], "--remote-any"))
68 num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY, "1",
69 num_settings, &settings);
70 else if (!strcmp(argv[i], "--no-remote-any"))
71 num_settings = cupsAddOption(CUPS_SERVER_REMOTE_ANY, "0",
72 num_settings, &settings);
73 else if (!strcmp(argv[i], "--share-printers"))
74 num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS, "1",
75 num_settings, &settings);
76 else if (!strcmp(argv[i], "--no-share-printers"))
77 num_settings = cupsAddOption(CUPS_SERVER_SHARE_PRINTERS, "0",
78 num_settings, &settings);
79 else if (!strcmp(argv[i], "--user-cancel-any"))
80 num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY, "1",
81 num_settings, &settings);
82 else if (!strcmp(argv[i], "--no-user-cancel-any"))
83 num_settings = cupsAddOption(CUPS_SERVER_USER_CANCEL_ANY, "0",
84 num_settings, &settings);
85 else
86 usage(argv[i]);
87 }
88 else
89 {
90 for (opt = argv[i] + 1; *opt; opt ++)
91 switch (*opt)
92 {
93 case 'E' :
94 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
95 break;
96
97 case 'U' :
98 i ++;
99 if (i >= argc)
100 usage(NULL);
101
102 cupsSetUser(argv[i]);
103 break;
104
105 case 'h' :
106 i ++;
107 if (i >= argc)
108 usage(NULL);
109
110 cupsSetServer(argv[i]);
111 break;
112
113 default :
114 usage(opt);
115 break;
116 }
117 }
118 }
119 else if (strchr(argv[i], '='))
120 num_settings = cupsParseOptions(argv[i], num_settings, &settings);
121 else
122 usage(argv[i]);
123 }
124
125 if (cupsGetOption("Listen", num_settings, settings) ||
126 cupsGetOption("Port", num_settings, settings))
127 {
128 _cupsLangPuts(stderr, _("cupsctl: Cannot set Listen or Port directly."));
129 return (1);
130 }
131
132 /*
133 * Connect to the server using the defaults...
134 */
135
136 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
137 cupsEncryption())) == NULL)
138 {
139 _cupsLangPrintf(stderr, _("cupsctl: Unable to connect to server: %s"),
140 strerror(errno));
141 return (1);
142 }
143
144 /*
145 * Set the current configuration if we have anything on the command-line...
146 */
147
148 if (num_settings > 0)
149 {
150 if (!cupsAdminSetServerSettings(http, num_settings, settings))
151 {
152 _cupsLangPrintf(stderr, "cupsctl: %s", cupsLastErrorString());
153 return (1);
154 }
155 }
156 else if (!cupsAdminGetServerSettings(http, &num_settings, &settings))
157 {
158 _cupsLangPrintf(stderr, "cupsctl: %s", cupsLastErrorString());
159 return (1);
160 }
161 else
162 {
163 for (i = 0; i < num_settings; i ++)
164 _cupsLangPrintf(stdout, "%s=%s", settings[i].name, settings[i].value);
165 }
166
167 cupsFreeOptions(num_settings, settings);
168 return (0);
169 }
170
171
172 /*
173 * 'usage()' - Show program usage.
174 */
175
176 static void
177 usage(const char *opt) /* I - Option character/string */
178 {
179 if (opt)
180 {
181 if (*opt == '-')
182 _cupsLangPrintf(stderr, _("cupsctl: Unknown option \"%s\""), opt);
183 else
184 _cupsLangPrintf(stderr, _("cupsctl: Unknown option \"-%c\""), *opt);
185 }
186
187 _cupsLangPuts(stdout, _("Usage: cupsctl [options] [param=value ... "
188 "paramN=valueN]"));
189 _cupsLangPuts(stdout, "");
190 _cupsLangPuts(stdout, _("Options:"));
191 _cupsLangPuts(stdout, "");
192 _cupsLangPuts(stdout, _(" -E Encrypt the connection."));
193 _cupsLangPuts(stdout, _(" -U username Specify username."));
194 _cupsLangPuts(stdout, _(" -h server[:port] Specify server "
195 "address."));
196 _cupsLangPuts(stdout, "");
197 _cupsLangPuts(stdout, _(" --[no-]debug-logging Turn debug logging "
198 "on/off."));
199 _cupsLangPuts(stdout, _(" --[no-]remote-admin Turn remote "
200 "administration on/off."));
201 _cupsLangPuts(stdout, _(" --[no-]remote-any Allow/prevent access "
202 "from the Internet."));
203 _cupsLangPuts(stdout, _(" --[no-]share-printers Turn printer sharing "
204 "on/off."));
205 _cupsLangPuts(stdout, _(" --[no-]user-cancel-any Allow/prevent users to "
206 "cancel any job."));
207
208 exit(1);
209 }