2 Unix SMB/CIFS implementation.
3 printing command routines
4 Copyright (C) Andrew Tridgell 1992-2000
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 extern struct current_user current_user;
24 extern userdom_struct current_user_info;
26 /****************************************************************************
27 Run a given print command
28 a null terminated list of value/substitute pairs is provided
29 for local substitution strings
30 ****************************************************************************/
31 static int print_run_command(int snum, const char* printername, bool do_sub,
32 const char *command, int *outfd, ...)
37 TALLOC_CTX *ctx = talloc_tos();
41 /* check for a valid system printername and valid command to run */
43 if ( !printername || !*printername ) {
48 if (!command || !*command) {
53 syscmd = talloc_strdup(ctx, command);
59 while ((arg = va_arg(ap, char *))) {
60 char *value = va_arg(ap,char *);
61 syscmd = talloc_string_sub(ctx, syscmd, arg, value);
69 syscmd = talloc_string_sub(ctx, syscmd, "%p", printername);
74 if (do_sub && snum != -1) {
75 syscmd = talloc_sub_advanced(ctx,
77 current_user_info.unix_name,
80 get_current_username(),
81 current_user_info.domain,
88 ret = smbrun_no_sanitize(syscmd,outfd);
90 DEBUG(3,("Running the command `%s' gave %d\n",syscmd,ret));
96 /****************************************************************************
98 ****************************************************************************/
99 static int generic_job_delete( const char *sharename, const char *lprm_command, struct printjob *pjob)
103 /* need to delete the spooled entry */
104 slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
105 return print_run_command( -1, sharename, False, lprm_command, NULL,
107 "%T", http_timestring(talloc_tos(), pjob->starttime),
111 /****************************************************************************
113 ****************************************************************************/
114 static int generic_job_pause(int snum, struct printjob *pjob)
118 /* need to pause the spooled entry */
119 slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
120 return print_run_command(snum, lp_printername(snum), True,
121 lp_lppausecommand(snum), NULL,
126 /****************************************************************************
128 ****************************************************************************/
129 static int generic_job_resume(int snum, struct printjob *pjob)
133 /* need to pause the spooled entry */
134 slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
135 return print_run_command(snum, lp_printername(snum), True,
136 lp_lpresumecommand(snum), NULL,
141 /****************************************************************************
142 Submit a file for printing - called from print_job_end()
143 ****************************************************************************/
145 static int generic_job_submit(int snum, struct printjob *pjob,
146 enum printing_types printing_type,
150 char *current_directory = NULL;
151 char *print_directory = NULL;
154 char *jobname = NULL;
155 TALLOC_CTX *ctx = talloc_tos();
156 fstring job_page_count, job_size;
158 /* we print from the directory path to give the best chance of
159 parsing the lpq output */
165 current_directory = talloc_strdup(ctx, wd);
168 if (!current_directory) {
171 print_directory = talloc_strdup(ctx, pjob->filename);
172 if (!print_directory) {
175 p = strrchr_m(print_directory,'/');
181 if (chdir(print_directory) != 0) {
185 jobname = talloc_strdup(ctx, pjob->jobname);
190 jobname = talloc_string_sub(ctx, jobname, "'", "_");
195 slprintf(job_page_count, sizeof(job_page_count)-1, "%d", pjob->page_count);
196 slprintf(job_size, sizeof(job_size)-1, "%lu", (unsigned long)pjob->size);
198 /* send it to the system spooler */
199 ret = print_run_command(snum, lp_printername(snum), True,
200 lp_printcommand(snum), NULL,
205 "%c", job_page_count,
210 if (chdir(current_directory) == -1) {
211 smb_panic("chdir failed in generic_job_submit");
213 TALLOC_FREE(current_directory);
218 /****************************************************************************
219 get the current list of queued jobs
220 ****************************************************************************/
221 static int generic_queue_get(const char *printer_name,
222 enum printing_types printing_type,
224 print_queue_struct **q,
225 print_status_struct *status)
229 int numlines, i, qcount;
230 print_queue_struct *queue = NULL;
232 /* never do substitution when running the 'lpq command' since we can't
233 get it rigt when using the background update daemon. Make the caller
234 do it before passing off the command string to us here. */
236 print_run_command(-1, printer_name, False, lpq_command, &fd, NULL);
239 DEBUG(5,("generic_queue_get: Can't read print queue status for printer %s\n",
245 qlines = fd_lines_load(fd, &numlines,0,NULL);
248 /* turn the lpq output into a series of job structures */
250 ZERO_STRUCTP(status);
251 if (numlines && qlines) {
252 queue = SMB_MALLOC_ARRAY(print_queue_struct, numlines+1);
258 memset(queue, '\0', sizeof(print_queue_struct)*(numlines+1));
260 for (i=0; i<numlines; i++) {
262 if (parse_lpq_entry(printing_type,qlines[i],
263 &queue[qcount],status,qcount==0)) {
274 /****************************************************************************
276 ****************************************************************************/
277 static int generic_queue_pause(int snum)
279 return print_run_command(snum, lp_printername(snum), True,
280 lp_queuepausecommand(snum), NULL, NULL);
283 /****************************************************************************
285 ****************************************************************************/
286 static int generic_queue_resume(int snum)
288 return print_run_command(snum, lp_printername(snum), True,
289 lp_queueresumecommand(snum), NULL, NULL);
292 /****************************************************************************
293 * Generic printing interface definitions...
294 ***************************************************************************/
296 struct printif generic_printif =
301 generic_queue_resume,