As Andrew suggested, make smbrun return a fd for a deleted file which can then
[jra/samba/.git] / source3 / printing / print_generic.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    printing command routines
5    Copyright (C) Andrew Tridgell 1992-2000
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "printing.h"
23
24
25 /*
26  * Generic printing interface definitions...
27  */
28
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);
37
38
39 struct printif  generic_printif =
40                 {
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,
48                 };
49
50 extern int DEBUGLEVEL;
51
52 /****************************************************************************
53 run a given print command 
54 a null terminated list of value/substitute pairs is provided
55 for local substitution strings
56 ****************************************************************************/
57
58 #ifdef HAVE_STDARG_H
59 static int print_run_command(int snum,char *command, int *outfd, char *outfile, ...)
60 {
61 #else /* HAVE_STDARG_H */
62 static int print_run_command(va_alist)
63 va_dcl
64 {
65         int snum;
66         int *outfd;
67         char *command, *outfile;
68 #endif /* HAVE_STDARG_H */
69
70         pstring syscmd;
71         char *p, *arg;
72         int ret;
73         va_list ap;
74
75 #ifdef HAVE_STDARG_H
76         va_start(ap, outfile);
77 #else /* HAVE_STDARG_H */
78         va_start(ap);
79         snum = va_arg(ap,int);
80         fd = va_arg(ap, int *);
81         command = va_arg(ap,char *);
82         outfile = va_arg(ap,char *);
83 #endif /* HAVE_STDARG_H */
84
85         if (!command || !*command) return -1;
86
87         if (!VALID_SNUM(snum)) {
88                 DEBUG(0,("Invalid snum %d for command %s\n", snum, command));
89                 return -1;
90         }
91
92         pstrcpy(syscmd, command);
93
94         while ((arg = va_arg(ap, char *))) {
95                 char *value = va_arg(ap,char *);
96                 pstring_sub(syscmd, arg, value);
97         }
98         va_end(ap);
99   
100         p = PRINTERNAME(snum);
101   
102         pstring_sub(syscmd, "%p", p);
103         standard_sub_snum(snum,syscmd);
104
105         /* Convert script args to unix-codepage */
106         dos_to_unix(syscmd, True);
107         ret = smbrun(syscmd,outfd,outfile);
108
109         DEBUG(3,("Running the command `%s' gave %d\n",syscmd,ret));
110
111         return ret;
112 }
113
114
115 /****************************************************************************
116 delete a print job
117 ****************************************************************************/
118 static int generic_job_delete(int snum, struct printjob *pjob)
119 {
120         fstring jobstr;
121
122         /* need to delete the spooled entry */
123         slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
124         return print_run_command(
125                    snum, 
126                    lp_lprmcommand(snum), NULL, NULL,
127                    "%j", jobstr,
128                    "%T", http_timestring(pjob->starttime),
129                    NULL);
130 }
131
132 /****************************************************************************
133 pause a job
134 ****************************************************************************/
135 static int generic_job_pause(int snum, struct printjob *pjob)
136 {
137         fstring jobstr;
138         
139         /* need to pause the spooled entry */
140         slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
141         return print_run_command(snum, 
142                                  lp_lppausecommand(snum), NULL, NULL,
143                                  "%j", jobstr,
144                                  NULL);
145 }
146
147 /****************************************************************************
148 resume a job
149 ****************************************************************************/
150 static int generic_job_resume(int snum, struct printjob *pjob)
151 {
152         fstring jobstr;
153         
154         /* need to pause the spooled entry */
155         slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
156         return print_run_command(snum, 
157                                  lp_lpresumecommand(snum), NULL, NULL,
158                                  "%j", jobstr,
159                                  NULL);
160 }
161
162 /****************************************************************************
163  Submit a file for printing - called from print_job_end()
164 ****************************************************************************/
165
166 static int generic_job_submit(int snum, struct printjob *pjob)
167 {
168         int ret;
169         pstring current_directory;
170         pstring print_directory;
171         char *wd, *p;
172         pstring jobname;
173
174         /* we print from the directory path to give the best chance of
175            parsing the lpq output */
176         wd = sys_getwd(current_directory);
177         if (!wd)
178                 return 0;
179
180         pstrcpy(print_directory, pjob->filename);
181         p = strrchr(print_directory,'/');
182         if (!p)
183                 return 0;
184         *p++ = 0;
185
186         if (chdir(print_directory) != 0)
187                 return 0;
188
189         pstrcpy(jobname, pjob->jobname);
190         pstring_sub(jobname, "'", "_");
191
192         /* send it to the system spooler */
193         ret = print_run_command(snum, 
194                           lp_printcommand(snum), NULL, NULL,
195                           "%s", p,
196                           "%J", jobname,
197                           "%f", p,
198                           NULL);
199
200         chdir(wd);
201
202         return ret;
203 }
204
205
206 /****************************************************************************
207 get the current list of queued jobs
208 ****************************************************************************/
209 static int generic_queue_get(int snum, print_queue_struct **q, print_status_struct *status)
210 {
211         char *path = lp_pathname(snum);
212         char *cmd = lp_lpqcommand(snum);
213         char **qlines;
214         int fd;
215         pstring tmp_file;
216         int numlines, i, qcount;
217         print_queue_struct *queue;
218         fstring printer_name;
219               
220         /* Convert printer name (i.e. share name) to unix-codepage */
221         fstrcpy(printer_name, lp_servicename(snum));
222         dos_to_unix(printer_name, True);
223         
224         slprintf(tmp_file, sizeof(tmp_file)-1, "%s/smblpq.%d", path, sys_getpid());
225
226         unlink(tmp_file);
227         print_run_command(snum, cmd, &fd, tmp_file, NULL);
228
229         if (fd == -1) {
230                 DEBUG(5,("generic_queue_get: Can't read print queue status for printer %s\n",
231                         printer_name ));
232                 return 0;
233         }
234         
235         numlines = 0;
236         qlines = fd_lines_load(fd, &numlines, True);
237         close(fd);
238
239         /* turn the lpq output into a series of job structures */
240         qcount = 0;
241         ZERO_STRUCTP(status);
242         if (numlines)
243                 queue = (print_queue_struct *)malloc(sizeof(print_queue_struct)*(numlines+1));
244
245         if (queue) {
246                 for (i=0; i<numlines; i++) {
247                         /* parse the line */
248                         if (parse_lpq_entry(snum,qlines[i],
249                                             &queue[qcount],status,qcount==0)) {
250                                 qcount++;
251                         }
252                 }               
253         }
254         file_lines_free(qlines);
255
256         *q = queue;
257         return qcount;
258 }
259
260 /****************************************************************************
261  pause a queue
262 ****************************************************************************/
263 static int generic_queue_pause(int snum)
264 {
265         return print_run_command(snum, lp_queuepausecommand(snum), NULL, NULL,
266                                  NULL);
267 }
268
269 /****************************************************************************
270  resume a queue
271 ****************************************************************************/
272 static int generic_queue_resume(int snum)
273 {
274         return print_run_command(snum, lp_queueresumecommand(snum), NULL, NULL,
275                                  NULL);
276 }