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