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
) {
47 if (!command
|| !*command
) {
51 syscmd
= talloc_strdup(ctx
, command
);
56 while ((arg
= va_arg(ap
, char *))) {
57 char *value
= va_arg(ap
,char *);
58 syscmd
= talloc_string_sub(ctx
, syscmd
, arg
, value
);
65 syscmd
= talloc_string_sub(ctx
, syscmd
, "%p", printername
);
70 if (do_sub
&& snum
!= -1) {
71 syscmd
= talloc_sub_advanced(ctx
,
73 current_user_info
.unix_name
,
76 get_current_username(),
77 current_user_info
.domain
,
84 ret
= smbrun_no_sanitize(syscmd
,outfd
);
86 DEBUG(3,("Running the command `%s' gave %d\n",syscmd
,ret
));
92 /****************************************************************************
94 ****************************************************************************/
95 static int generic_job_delete( const char *sharename
, const char *lprm_command
, struct printjob
*pjob
)
99 /* need to delete the spooled entry */
100 slprintf(jobstr
, sizeof(jobstr
)-1, "%d", pjob
->sysjob
);
101 return print_run_command( -1, sharename
, False
, lprm_command
, NULL
,
103 "%T", http_timestring(pjob
->starttime
),
107 /****************************************************************************
109 ****************************************************************************/
110 static int generic_job_pause(int snum
, struct printjob
*pjob
)
114 /* need to pause the spooled entry */
115 slprintf(jobstr
, sizeof(jobstr
)-1, "%d", pjob
->sysjob
);
116 return print_run_command(snum
, PRINTERNAME(snum
), True
,
117 lp_lppausecommand(snum
), NULL
,
122 /****************************************************************************
124 ****************************************************************************/
125 static int generic_job_resume(int snum
, struct printjob
*pjob
)
129 /* need to pause the spooled entry */
130 slprintf(jobstr
, sizeof(jobstr
)-1, "%d", pjob
->sysjob
);
131 return print_run_command(snum
, PRINTERNAME(snum
), True
,
132 lp_lpresumecommand(snum
), NULL
,
137 /****************************************************************************
138 Submit a file for printing - called from print_job_end()
139 ****************************************************************************/
141 static int generic_job_submit(int snum
, struct printjob
*pjob
)
144 char *current_directory
= NULL
;
145 char *print_directory
= NULL
;
148 char *jobname
= NULL
;
149 TALLOC_CTX
*ctx
= talloc_tos();
150 fstring job_page_count
, job_size
;
152 /* we print from the directory path to give the best chance of
153 parsing the lpq output */
154 current_directory
= TALLOC_ARRAY(ctx
,
157 if (!current_directory
) {
160 wd
= sys_getwd(current_directory
);
165 print_directory
= talloc_strdup(ctx
, pjob
->filename
);
166 if (!print_directory
) {
169 p
= strrchr_m(print_directory
,'/');
175 if (chdir(print_directory
) != 0) {
179 jobname
= talloc_strdup(ctx
, pjob
->jobname
);
184 jobname
= talloc_string_sub(ctx
, jobname
, "'", "_");
189 slprintf(job_page_count
, sizeof(job_page_count
)-1, "%d", pjob
->page_count
);
190 slprintf(job_size
, sizeof(job_size
)-1, "%lu", (unsigned long)pjob
->size
);
192 /* send it to the system spooler */
193 ret
= print_run_command(snum
, PRINTERNAME(snum
), True
,
194 lp_printcommand(snum
), NULL
,
199 "%c", job_page_count
,
205 TALLOC_FREE(current_directory
);
210 /****************************************************************************
211 get the current list of queued jobs
212 ****************************************************************************/
213 static int generic_queue_get(const char *printer_name
,
214 enum printing_types printing_type
,
216 print_queue_struct
**q
,
217 print_status_struct
*status
)
221 int numlines
, i
, qcount
;
222 print_queue_struct
*queue
= NULL
;
224 /* never do substitution when running the 'lpq command' since we can't
225 get it rigt when using the background update daemon. Make the caller
226 do it before passing off the command string to us here. */
228 print_run_command(-1, printer_name
, False
, lpq_command
, &fd
, NULL
);
231 DEBUG(5,("generic_queue_get: Can't read print queue status for printer %s\n",
237 qlines
= fd_lines_load(fd
, &numlines
,0);
240 /* turn the lpq output into a series of job structures */
242 ZERO_STRUCTP(status
);
243 if (numlines
&& qlines
) {
244 queue
= SMB_MALLOC_ARRAY(print_queue_struct
, numlines
+1);
246 file_lines_free(qlines
);
250 memset(queue
, '\0', sizeof(print_queue_struct
)*(numlines
+1));
252 for (i
=0; i
<numlines
; i
++) {
254 if (parse_lpq_entry(printing_type
,qlines
[i
],
255 &queue
[qcount
],status
,qcount
==0)) {
261 file_lines_free(qlines
);
266 /****************************************************************************
268 ****************************************************************************/
269 static int generic_queue_pause(int snum
)
271 return print_run_command(snum
, PRINTERNAME(snum
), True
, lp_queuepausecommand(snum
), NULL
, NULL
);
274 /****************************************************************************
276 ****************************************************************************/
277 static int generic_queue_resume(int snum
)
279 return print_run_command(snum
, PRINTERNAME(snum
), True
, lp_queueresumecommand(snum
), NULL
, NULL
);
282 /****************************************************************************
283 * Generic printing interface definitions...
284 ***************************************************************************/
286 struct printif generic_printif
=
291 generic_queue_resume
,