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