printing: Consolidate add_to_jobs_list()
[samba.git] / source3 / printing / print_svid.c
1 /*
2  * Copyright (C) 1997-1998 by Norm Jacobs, Colorado Springs, Colorado, USA
3  * Copyright (C) 1997-1998 by Sun Microsystem, Inc.
4  * All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /*
21  * This module implements support for gathering and comparing available
22  * printer information on a SVID or XPG4 compliant system.  It does this
23  * through the use of the SVID/XPG4 command "lpstat(1)".
24  *
25  * The expectations is that execution of the command "lpstat -v" will
26  * generate responses in the form of:
27  *
28  *      device for serial: /dev/term/b
29  *      system for fax: server
30  *      system for color: server (as printer chroma)
31  */
32
33
34 #include "includes.h"
35 #include "printing/pcap.h"
36 #include "lib/util_file.h"
37
38 #if defined(SYSV) || defined(HPUX)
39 bool sysv_cache_reload(struct pcap_cache **_pcache)
40 {
41         char **lines;
42         int i;
43         struct pcap_cache *pcache = NULL;
44         char **argl = NULL;
45
46 #if defined(HPUX)
47         DEBUG(5, ("reloading hpux printcap cache\n"));
48 #else
49         DEBUG(5, ("reloading sysv printcap cache\n"));
50 #endif
51
52         argl = talloc_zero_array(talloc_tos(), char *, 3);
53         if (argl == NULL) {
54                 return false;
55         }
56         argl[0] = talloc_strdup(argl, "/usr/bin/lpstat");
57         if (argl[0] == NULL) {
58                 TALLOC_FREE(argl);
59                 return false;
60         }
61         argl[1] = talloc_strdup(argl, "-v");
62         if (argl[1] == NULL) {
63                 TALLOC_FREE(argl);
64                 return false;
65         }
66         argl[2] = NULL;
67
68         lines = file_lines_ploadv(talloc_tos(), argl, NULL);
69         if (lines == NULL) {
70 #if defined(HPUX)
71       
72                /*
73                 * if "lpstat -v" is NULL then we check if schedular is running if it is
74                 * that means no printers are added on the HP-UX system, if schedular is not
75                 * running we display reload error.
76                 */
77
78                 char **scheduler;
79
80                 argl[1] = talloc_strdup(argl, "-r");
81                 if (argl[1] == NULL) {
82                         TALLOC_FREE(argl);
83                         return false;
84                 }
85                 scheduler = file_lines_ploadv(talloc_tos(), argl, NULL);
86                 TALLOC_FREE(argl);
87                 if(!strcmp(*scheduler,"scheduler is running")){
88                         DEBUG(3,("No Printers found!!!\n"));
89                         TALLOC_FREE(scheduler);
90                         return True;
91                 }
92                 else{
93                         DEBUG(3,("Scheduler is not running!!!\n"));
94                         TALLOC_FREE(scheduler);
95                         return False;
96                 }
97 #else
98                 DEBUG(3,("No Printers found!!!\n"));
99                 return False;
100 #endif
101         }
102         TALLOC_FREE(argl);
103
104         for (i = 0; lines[i]; i++) {
105                 char *name, *tmp;
106                 char *buf = lines[i];
107
108                 /* eat "system/device for " */
109                 if (((tmp = strchr_m(buf, ' ')) == NULL) ||
110                     ((tmp = strchr_m(++tmp, ' ')) == NULL))
111                         continue;
112
113                 /*
114                  * In case we're only at the "for ".
115                  */
116
117                 if(!strncmp("for ", ++tmp, 4)) {
118                         tmp=strchr_m(tmp, ' ');
119                         tmp++;
120                 }
121
122                 /* Eat whitespace. */
123
124                 while(*tmp == ' ')
125                         ++tmp;
126
127                 /*
128                  * On HPUX there is an extra line that can be ignored.
129                  * d.thibadeau 2001/08/09
130                  */
131                 if(!strncmp("remote to", tmp, 9))
132                         continue;
133
134                 name = tmp;
135
136                 /* truncate the ": ..." */
137                 if ((tmp = strchr_m(name, ':')) != NULL)
138                         *tmp = '\0';
139                 
140                 /* add it to the cache */
141                 if (!pcap_cache_add_specific(&pcache, name, NULL, NULL)) {
142                         TALLOC_FREE(lines);
143                         pcap_cache_destroy_specific(&pcache);
144                         return false;
145                 }
146         }
147
148         TALLOC_FREE(lines);
149         *_pcache = pcache;
150         return true;
151 }
152
153 #else
154 /* this keeps fussy compilers happy */
155  void print_svid_dummy(void);
156  void print_svid_dummy(void) {}
157 #endif