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