• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeprint
 

tdeprint

  • tdeprint
  • cups
make_driver_db_cups.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
4  * Copyright (c) 2014 Timothy Pearson <kb9vqf@pearsoncomputing.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License version 2 as published by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  **/
20 
21 #include <config.h>
22 #ifndef _GNU_SOURCE
23 #define _GNU_SOURCE /* Needed for getline */
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <dirent.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <zlib.h>
34 
35 #include <tqstringlist.h>
36 #include <tqlocale.h>
37 
38 extern "C" {
39  #include "driverparse.h"
40 }
41 
42 #define PROCESS_PPD_FILE_CONTENTS \
43  memset(value,0,256); \
44  c1 = strchr(line,':'); \
45  if (c1) \
46  { \
47  c2 = strchr(c1,'"'); \
48  if (c2) \
49  { \
50  c2++; \
51  c1 = strchr(c2,'"'); \
52  if (c1) strlcpy(value,c2,c1-c2+1); \
53  } \
54  else \
55  { \
56  c1++; \
57  while (*c1 && isspace(*c1)) \
58  c1++; \
59  if (!*c1) \
60  continue; \
61  c2 = line+strlen(line)-1; /* point to \n */ \
62  while (*c2 && isspace(*c2)) \
63  c2--; \
64  strlcpy(value,c1,c2-c1+2); \
65  } \
66  } \
67  count++; \
68  if (strncmp(line,"*Manufacturer:",14) == 0) fprintf(output_file,"MANUFACTURER=%s\n",value); \
69  else if (strncmp(line,"*ShortNickName:",15) == 0) fprintf(output_file,"MODEL=%s\n",value); \
70  else if (strncmp(line,"*ModelName:",11) == 0) fprintf(output_file,"MODELNAME=%s\n",value); \
71  else if (strncmp(line,"*NickName:",10) == 0) strncat(desc,value,255-strlen(desc)); \
72  else if (strncmp(line,"*pnpManufacturer:",17) == 0) fprintf(output_file,"PNPMANUFACTURER=%s\n",value); \
73  else if (strncmp(line,"*pnpModel:",10) == 0) fprintf(output_file,"PNPMODEL=%s\n",value); \
74  else if (strncmp(line,"*LanguageVersion:",17) == 0) strncat(langver,value,63-strlen(langver)); \
75  else count--; \
76  /* Either we got everything we needed, or we encountered an "OpenUI" directive \
77  * and it's reasonable to assume that there's no needed info further in the file, \
78  * just stop here */ \
79  if (count >= 7 || strncmp(line, "*OpenUI", 7) == 0) \
80  { \
81  if (strlen(langver) > 0) \
82  { \
83  strncat(desc, " [", 255-strlen(desc)); \
84  strncat(desc, langver, 255-strlen(desc)); \
85  strncat(desc, "]", 255-strlen(desc)); \
86  } \
87  if (strlen(desc) > 0) \
88  fprintf(output_file, "DESCRIPTION=%s\n", desc); \
89  break; \
90  }
91 
92 void initPpd(const char *dirname)
93 {
94  struct stat stat_res;
95  if (stat(dirname, &stat_res) == -1) {
96  fprintf(stderr, "Can't open drivers directory : %s\n", dirname);
97  return;
98  }
99 
100  if (S_ISDIR(stat_res.st_mode)) {
101  DIR *dir = opendir(dirname);
102  struct dirent *entry;
103  char buffer[4096] = {0};
104  char drFile[256];
105  int len = strlen(dirname);
106 
107  if (dir == NULL)
108  {
109  fprintf(stderr, "Can't open drivers directory : %s\n", dirname);
110  return;
111  }
112  while ((entry=readdir(dir)) != NULL)
113  {
114  if (strcmp(entry->d_name,".") == 0 || strcmp(entry->d_name,"..") == 0)
115  {
116  continue;
117  }
118  if (len+strlen(entry->d_name)+1 < 4096)
119  {
120  struct stat st;
121 
122  strcpy(buffer,dirname);
123  strcat(buffer,"/");
124  strcat(buffer,entry->d_name);
125  if (stat(buffer,&st) == 0)
126  {
127  if (S_ISDIR(st.st_mode))
128  {
129  initPpd(buffer);
130  }
131  else if (S_ISREG(st.st_mode))
132  {
133  char *c = strrchr(buffer,'.');
134  snprintf(drFile, 255, "ppd:%s", buffer);
135  if (c && strncmp(c,".ppd",4) == 0)
136  {
137  addFile(drFile, "", "");
138  }
139  else if (c && strncmp(c, ".gz", 3) == 0)
140  { /* keep also compressed driver files */
141  while (c != buffer)
142  {
143  if (*(--c) == '.') break;
144  }
145  if (*c == '.' && strncmp(c, ".ppd",4) == 0)
146  {
147  addFile(drFile, "", "");
148  }
149  }
150  }
151  }
152  }
153  }
154  closedir(dir);
155  }
156  else if (access(dirname, X_OK) != -1) {
157  char *filename;
158  int n = strlen(dirname)+strlen(" list");
159  filename = (char*)malloc(n*sizeof(char)+1);
160  memset(filename,0,n);
161  strcat(filename, dirname);
162  strcat(filename, " list");
163 
164  FILE* file = popen(filename, "r");
165  if (file) {
166  char * line = NULL;
167  size_t len = 0;
168  ssize_t read;
169  while ((read = getline(&line, &len, file)) != -1) {
170  char * pos1 = strstr(line, "\"");
171  if (pos1 != NULL) {
172  char * pos2 = strstr(pos1 + 1, "\"");
173  if (pos2 != NULL) {
174  *pos2 = 0;
175  char * pos3 = strstr(pos1 + 1, ":");
176  if (pos3 != NULL) {
177  char *ppduri;
178  int n2 = strlen("compressed-ppd:")+strlen(pos3+1);
179  ppduri = (char*)malloc(n2*sizeof(char)+1);
180  memset(ppduri,0,n2);
181  strcat(ppduri, "compressed-ppd:");
182  strcat(ppduri, pos3+1);
183  addFile(ppduri, dirname, pos2+1);
184  free(ppduri);
185  ppduri = NULL;
186  }
187  }
188  }
189  }
190  if (line) {
191  free(line);
192  }
193 
194  pclose(file);
195  }
196  else {
197  fprintf(stderr, "Can't execute compressed driver handler : %s\n", dirname);
198  }
199 
200  free(filename);
201  filename = NULL;
202  }
203  else {
204  fprintf(stderr, "Can't open drivers directory : %s\n", dirname);
205  return;
206  }
207 }
208 
209 void initCompressedPpd(const char *dirname)
210 {
211  // HACK
212  // The initPpd function actually handles the compressed PPDs as well, so do nothing here
213  // If we were to rerun initPpd here then all drivers would be duplicated!
214 }
215 
216 int parsePpdFile(const char *filename, const char * /*origin*/, const char * /*metadata*/, FILE *output_file)
217 {
218  gzFile ppd_file;
219  char line[4096], value[256], langver[64] = {0}, desc[256] = {0};
220  char *c1, *c2;
221  int count = 0;
222 
223  ppd_file = gzopen(filename,"r");
224  if (ppd_file == NULL)
225  {
226  fprintf(stderr, "Can't open driver file : %s\n", filename);
227  return 0;
228  }
229  fprintf(output_file,"FILE=ppd:%s\n",filename);
230 
231  while (gzgets(ppd_file,line,4095) != Z_NULL)
232  {
233  PROCESS_PPD_FILE_CONTENTS
234  }
235  fprintf(output_file,"\n");
236 
237  gzclose(ppd_file);
238  return 1;
239 }
240 
241 int parseCompressedPpdFile(const char *ppdfilename, const char *origin, const char *metadata, FILE *output_file)
242 {
243  char value[256], langver[64] = {0}, desc[256] = {0};
244  char *c1, *c2;
245  int count = 0;
246 
247  bool useFallbackExtractionMethod = false;
248 
249  if (strlen(metadata) > 0) {
250  TQString metadataProcessed(metadata);
251  metadataProcessed = metadataProcessed.stripWhiteSpace();
252  TQStringList metadataList = TQStringList::split(" ", metadataProcessed, TRUE);
253  TQLocale ppdLanguage(metadataList[0]);
254  TQString languageVersion = TQLocale::languageToString(ppdLanguage.language());
255  metadataList = TQStringList::split("\" \"", metadataProcessed, TRUE);
256  TQString description = metadataList[1];
257 
258  int pos = metadataProcessed.find("MFG:");
259  if (pos < 0) {
260  pos = metadataProcessed.find("MANUFACTURER:");
261  }
262  if (pos >= 0) {
263  TQString manufacturer;
264  TQString model;
265  TQString modelName;
266  TQString pnpManufacturer;
267  TQString pnpModel;
268  TQString driver;
269  TQStringList metadataList = TQStringList::split(";", metadataProcessed.mid(pos), TRUE);
270  for (TQStringList::Iterator it = metadataList.begin(); it != metadataList.end(); ++it) {
271  TQStringList kvPair = TQStringList::split(":", *it, TRUE);
272  if ((kvPair[0].upper() == "MFG") || (kvPair[0].upper() == "MANUFACTURER")) {
273  manufacturer = kvPair[1];
274  }
275  else if ((kvPair[0].upper() == "MDL") ||(kvPair[0].upper() == "MODEL")) {
276  modelName = kvPair[1];
277  }
278 // else if (kvPair[0].upper() == "PNPMANUFACTURER") {
279 // pnpManufacturer = kvPair[1];
280 // }
281 // else if (kvPair[0].upper() == "PNPMODEL") {
282 // pnpModel = kvPair[1];
283 // }
284  else if ((kvPair[0].upper() == "DRV") || (kvPair[0].upper() == "DRIVER")) {
285  driver = kvPair[1];
286  }
287  }
288 
289  manufacturer = manufacturer.stripWhiteSpace();
290  modelName = modelName.stripWhiteSpace();
291  driver = driver.stripWhiteSpace();
292 
293  TQStringList driverList = TQStringList::split(",", driver, TRUE);
294  driver = driverList[0];
295  if (driver.startsWith("D")) {
296  driver = driver.mid(1);
297  driver = driver.stripWhiteSpace();
298  }
299  model = manufacturer + " " + modelName + " " + driver;
300  description = description + " [" + languageVersion + "]";
301 
302  fprintf(output_file,"FILE=compressed-ppd:%s:%s\n", origin, ppdfilename);
303 
304  fprintf(output_file,"MANUFACTURER=%s\n",manufacturer.ascii());
305  fprintf(output_file,"MODELNAME=%s\n",modelName.ascii());
306  fprintf(output_file,"MODEL=%s\n",model.ascii());
307  if (pnpManufacturer.length() > 0) {
308  fprintf(output_file,"PNPMANUFACTURER=%s\n",pnpManufacturer.ascii());
309  }
310  if (pnpModel.length() > 0) {
311  fprintf(output_file,"PNPMODEL=%s\n",pnpModel.ascii());
312  }
313  if (description.length() > 0) {
314  fprintf(output_file,"DESCRIPTION=%s\n",description.ascii());
315  }
316  }
317  else {
318  useFallbackExtractionMethod = true;
319  }
320  }
321 
322  if (useFallbackExtractionMethod) {
323  char *filename;
324  int n = strlen(origin)+strlen(" cat ")+strlen(ppdfilename);
325  filename = (char*)malloc(n*sizeof(char)+1);
326  memset(filename,0,n);
327  strcat(filename, origin);
328  strcat(filename, " cat ");
329  strcat(filename, ppdfilename);
330 
331  FILE* file = popen(filename, "r");
332  if (file) {
333  char * line = NULL;
334  size_t len = 0;
335  ssize_t read;
336 
337  fprintf(output_file,"FILE=compressed-ppd:%s:%s\n", origin, ppdfilename);
338 
339  while ((read = getline(&line, &len, file)) != -1) {
340  PROCESS_PPD_FILE_CONTENTS
341  }
342  if (line) {
343  free(line);
344  }
345 
346  pclose(file);
347  }
348  else {
349  fprintf(stderr, "Can't open driver file : %s\n", ppdfilename);
350  return 0;
351  }
352 
353  free(filename);
354  filename = NULL;
355  }
356 
357  return 1;
358 }
359 
360 int main(int argc, char *argv[])
361 {
362  registerHandler("ppd:", initPpd, parsePpdFile);
363  registerHandler("compressed-ppd:", initCompressedPpd, parseCompressedPpdFile);
364  initFoomatic();
365  return execute(argc, argv);
366 }

tdeprint

Skip menu "tdeprint"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeprint

Skip menu "tdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeprint by doxygen 1.9.1
This website is maintained by Timothy Pearson.