00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #include "includes.h"
00065
00066
00067 typedef struct pcap_cache {
00068 char *name;
00069 char *comment;
00070 struct pcap_cache *next;
00071 } pcap_cache_t;
00072
00073 static pcap_cache_t *pcap_cache = NULL;
00074
00075 BOOL pcap_cache_add(const char *name, const char *comment)
00076 {
00077 pcap_cache_t *p;
00078
00079 if (name == NULL || ((p = SMB_MALLOC_P(pcap_cache_t)) == NULL))
00080 return False;
00081
00082 p->name = SMB_STRDUP(name);
00083 p->comment = (comment && *comment) ? SMB_STRDUP(comment) : NULL;
00084
00085 p->next = pcap_cache;
00086 pcap_cache = p;
00087
00088 return True;
00089 }
00090
00091 static void pcap_cache_destroy(pcap_cache_t *cache)
00092 {
00093 pcap_cache_t *p, *next;
00094
00095 for (p = cache; p != NULL; p = next) {
00096 next = p->next;
00097
00098 SAFE_FREE(p->name);
00099 SAFE_FREE(p->comment);
00100 SAFE_FREE(p);
00101 }
00102 }
00103
00104 BOOL pcap_cache_loaded(void)
00105 {
00106 return (pcap_cache != NULL);
00107 }
00108
00109 void pcap_cache_reload(void)
00110 {
00111 const char *pcap_name = lp_printcapname();
00112 BOOL pcap_reloaded = False;
00113 pcap_cache_t *tmp_cache = NULL;
00114 XFILE *pcap_file;
00115 char *pcap_line;
00116
00117 DEBUG(3, ("reloading printcap cache\n"));
00118
00119
00120 if (pcap_name == NULL || *pcap_name == 0) {
00121 DEBUG(0, ("No printcap file name configured!\n"));
00122 return;
00123 }
00124
00125 tmp_cache = pcap_cache;
00126 pcap_cache = NULL;
00127
00128 #ifdef HAVE_CUPS
00129 if (strequal(pcap_name, "cups")) {
00130 pcap_reloaded = cups_cache_reload();
00131 goto done;
00132 }
00133 #endif
00134
00135 #ifdef HAVE_IPRINT
00136 if (strequal(pcap_name, "iprint")) {
00137 pcap_reloaded = iprint_cache_reload();
00138 goto done;
00139 }
00140 #endif
00141
00142 #if defined(SYSV) || defined(HPUX)
00143 if (strequal(pcap_name, "lpstat")) {
00144 pcap_reloaded = sysv_cache_reload();
00145 goto done;
00146 }
00147 #endif
00148
00149 #ifdef AIX
00150 if (strstr_m(pcap_name, "/qconfig") != NULL) {
00151 pcap_reloaded = aix_cache_reload();
00152 goto done;
00153 }
00154 #endif
00155
00156
00157
00158 if ((pcap_file = x_fopen(pcap_name, O_RDONLY, 0)) == NULL) {
00159 DEBUG(0, ("Unable to open printcap file %s for read!\n", pcap_name));
00160 goto done;
00161 }
00162
00163 for (; (pcap_line = fgets_slash(NULL, sizeof(pstring), pcap_file)) != NULL; safe_free(pcap_line)) {
00164 pstring name, comment;
00165 char *p, *q;
00166
00167 if (*pcap_line == '#' || *pcap_line == 0)
00168 continue;
00169
00170
00171 if ((p = strchr_m(pcap_line, ':')) != NULL)
00172 *p = 0;
00173
00174
00175
00176
00177
00178 for (*name = *comment = 0, p = pcap_line; p != NULL; p = q) {
00179 BOOL has_punctuation;
00180
00181 if ((q = strchr_m(p, '|')) != NULL)
00182 *q++ = 0;
00183
00184 has_punctuation = (strchr_m(p, ' ') ||
00185 strchr_m(p, '\t') ||
00186 strchr_m(p, '(') ||
00187 strchr_m(p, ')'));
00188
00189 if (strlen(p) > strlen(comment) && has_punctuation) {
00190 pstrcpy(comment, p);
00191 continue;
00192 }
00193
00194 if (strlen(p) <= MAXPRINTERLEN &&
00195 strlen(p) > strlen(name) && !has_punctuation) {
00196 if (!*comment)
00197 pstrcpy(comment, name);
00198
00199 pstrcpy(name, p);
00200 continue;
00201 }
00202
00203 if (!strchr_m(comment, ' ') &&
00204 strlen(p) > strlen(comment)) {
00205 pstrcpy(comment, p);
00206 continue;
00207 }
00208 }
00209
00210 comment[60] = 0;
00211 name[MAXPRINTERLEN] = 0;
00212
00213 if (*name && !pcap_cache_add(name, comment)) {
00214 x_fclose(pcap_file);
00215 goto done;
00216 }
00217 }
00218
00219 x_fclose(pcap_file);
00220 pcap_reloaded = True;
00221
00222 done:
00223 DEBUG(3, ("reload status: %s\n", (pcap_reloaded) ? "ok" : "error"));
00224
00225 if (pcap_reloaded)
00226 pcap_cache_destroy(tmp_cache);
00227 else {
00228 pcap_cache_destroy(pcap_cache);
00229 pcap_cache = tmp_cache;
00230 }
00231
00232 return;
00233 }
00234
00235
00236 BOOL pcap_printername_ok(const char *printername)
00237 {
00238 pcap_cache_t *p;
00239
00240 for (p = pcap_cache; p != NULL; p = p->next)
00241 if (strequal(p->name, printername))
00242 return True;
00243
00244 return False;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 void pcap_printer_fn(void (*fn)(char *, char *))
00256 {
00257 pcap_cache_t *p;
00258
00259 for (p = pcap_cache; p != NULL; p = p->next)
00260 fn(p->name, p->comment);
00261
00262 return;
00263 }