if we are adding a new sambaAccount, make sure that we add a
[Samba.git] / source / printing / print_generic.c
blob2a6b11ebc4e2a59707c1acefc36cf649d5f69c4e
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 printing command routines
5 Copyright (C) Andrew Tridgell 1992-2000
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "printing.h"
26 * Generic printing interface definitions...
29 static int generic_job_delete(int snum, struct printjob *pjob);
30 static int generic_job_pause(int snum, struct printjob *pjob);
31 static int generic_job_resume(int snum, struct printjob *pjob);
32 static int generic_job_submit(int snum, struct printjob *pjob);
33 static int generic_queue_get(int snum, print_queue_struct **q,
34 print_status_struct *status);
35 static int generic_queue_pause(int snum);
36 static int generic_queue_resume(int snum);
39 struct printif generic_printif =
41 generic_queue_get,
42 generic_queue_pause,
43 generic_queue_resume,
44 generic_job_delete,
45 generic_job_pause,
46 generic_job_resume,
47 generic_job_submit,
50 /****************************************************************************
51 run a given print command
52 a null terminated list of value/substitute pairs is provided
53 for local substitution strings
54 ****************************************************************************/
55 static int print_run_command(int snum,char *command, int *outfd, ...)
58 pstring syscmd;
59 char *p, *arg;
60 int ret;
61 va_list ap;
62 va_start(ap, outfd);
64 if (!command || !*command) return -1;
66 if (!VALID_SNUM(snum)) {
67 DEBUG(0,("Invalid snum %d for command %s\n", snum, command));
68 return -1;
71 pstrcpy(syscmd, command);
73 while ((arg = va_arg(ap, char *))) {
74 char *value = va_arg(ap,char *);
75 pstring_sub(syscmd, arg, value);
77 va_end(ap);
79 p = PRINTERNAME(snum);
81 pstring_sub(syscmd, "%p", p);
82 standard_sub_snum(snum,syscmd,sizeof(syscmd));
84 /* Convert script args to unix-codepage */
85 dos_to_unix(syscmd);
86 ret = smbrun(syscmd,outfd);
88 DEBUG(3,("Running the command `%s' gave %d\n",syscmd,ret));
90 return ret;
94 /****************************************************************************
95 delete a print job
96 ****************************************************************************/
97 static int generic_job_delete(int snum, struct printjob *pjob)
99 fstring jobstr;
101 /* need to delete the spooled entry */
102 slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
103 return print_run_command(
104 snum,
105 lp_lprmcommand(snum), NULL,
106 "%j", jobstr,
107 "%T", http_timestring(pjob->starttime),
108 NULL);
111 /****************************************************************************
112 pause a job
113 ****************************************************************************/
114 static int generic_job_pause(int snum, struct printjob *pjob)
116 fstring jobstr;
118 /* need to pause the spooled entry */
119 slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
120 return print_run_command(snum,
121 lp_lppausecommand(snum), NULL,
122 "%j", jobstr,
123 NULL);
126 /****************************************************************************
127 resume a job
128 ****************************************************************************/
129 static int generic_job_resume(int snum, struct printjob *pjob)
131 fstring jobstr;
133 /* need to pause the spooled entry */
134 slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
135 return print_run_command(snum,
136 lp_lpresumecommand(snum), NULL,
137 "%j", jobstr,
138 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)
147 int ret;
148 pstring current_directory;
149 pstring print_directory;
150 char *wd, *p;
151 pstring jobname;
152 fstring job_page_count, job_size;
154 /* we print from the directory path to give the best chance of
155 parsing the lpq output */
156 wd = sys_getwd(current_directory);
157 if (!wd)
158 return 0;
160 pstrcpy(print_directory, pjob->filename);
161 p = strrchr(print_directory,'/');
162 if (!p)
163 return 0;
164 *p++ = 0;
166 if (chdir(print_directory) != 0)
167 return 0;
169 pstrcpy(jobname, pjob->jobname);
170 pstring_sub(jobname, "'", "_");
171 slprintf(job_page_count, sizeof(job_page_count)-1, "%d", pjob->page_count);
172 slprintf(job_size, sizeof(job_size)-1, "%d", pjob->size);
174 /* send it to the system spooler */
175 ret = print_run_command(snum,
176 lp_printcommand(snum), NULL,
177 "%s", p,
178 "%J", jobname,
179 "%f", p,
180 "%z", job_size,
181 "%c", job_page_count,
182 NULL);
184 chdir(wd);
186 return ret;
190 /****************************************************************************
191 get the current list of queued jobs
192 ****************************************************************************/
193 static int generic_queue_get(int snum, print_queue_struct **q, print_status_struct *status)
195 char **qlines;
196 int fd;
197 int numlines, i, qcount;
198 print_queue_struct *queue = NULL;
199 fstring printer_name;
201 /* Convert printer name (i.e. share name) to unix-codepage */
202 fstrcpy(printer_name, lp_servicename(snum));
203 dos_to_unix(printer_name);
205 print_run_command(snum, lp_lpqcommand(snum), &fd, NULL);
207 if (fd == -1) {
208 DEBUG(5,("generic_queue_get: Can't read print queue status for printer %s\n",
209 printer_name ));
210 return 0;
213 numlines = 0;
214 qlines = fd_lines_load(fd, &numlines, True);
215 close(fd);
217 /* turn the lpq output into a series of job structures */
218 qcount = 0;
219 ZERO_STRUCTP(status);
220 if (numlines)
221 queue = (print_queue_struct *)malloc(sizeof(print_queue_struct)*(numlines+1));
223 if (queue) {
224 for (i=0; i<numlines; i++) {
225 /* parse the line */
226 if (parse_lpq_entry(snum,qlines[i],
227 &queue[qcount],status,qcount==0)) {
228 qcount++;
232 file_lines_free(qlines);
234 *q = queue;
235 return qcount;
238 /****************************************************************************
239 pause a queue
240 ****************************************************************************/
241 static int generic_queue_pause(int snum)
243 return print_run_command(snum, lp_queuepausecommand(snum), NULL, NULL);
246 /****************************************************************************
247 resume a queue
248 ****************************************************************************/
249 static int generic_queue_resume(int snum)
251 return print_run_command(snum, lp_queueresumecommand(snum), NULL, NULL);