28 #include <sys/types.h>
35 #include <tqstringlist.h>
39 #include "driverparse.h"
42 #define PROCESS_PPD_FILE_CONTENTS \
43 memset(value,0,256); \
44 c1 = strchr(line,':'); \
47 c2 = strchr(c1,'"'); \
51 c1 = strchr(c2,'"'); \
52 if (c1) strlcpy(value,c2,c1-c2+1); \
57 while (*c1 && isspace(*c1)) \
61 c2 = line+strlen(line)-1;
\
62 while (*c2 && isspace(*c2)) \
64 strlcpy(value,c1,c2-c1+2); \
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)); \
79 if (count >= 7 || strncmp(line, "*OpenUI", 7) == 0) \
81 if (strlen(langver) > 0) \
83 strncat(desc, " [", 255-strlen(desc)); \
84 strncat(desc, langver, 255-strlen(desc)); \
85 strncat(desc, "]", 255-strlen(desc)); \
87 if (strlen(desc) > 0) \
88 fprintf(output_file, "DESCRIPTION=%s\n", desc); \
92 void initPpd(
const char *dirname)
95 if (stat(dirname, &stat_res) == -1) {
96 fprintf(stderr,
"Can't open drivers directory : %s\n", dirname);
100 if (S_ISDIR(stat_res.st_mode)) {
101 DIR *dir = opendir(dirname);
102 struct dirent *entry;
103 char buffer[4096] = {0};
105 int len = strlen(dirname);
109 fprintf(stderr,
"Can't open drivers directory : %s\n", dirname);
112 while ((entry=readdir(dir)) != NULL)
114 if (strcmp(entry->d_name,
".") == 0 || strcmp(entry->d_name,
"..") == 0)
118 if (len+strlen(entry->d_name)+1 < 4096)
122 strcpy(buffer,dirname);
124 strcat(buffer,entry->d_name);
125 if (stat(buffer,&st) == 0)
127 if (S_ISDIR(st.st_mode))
131 else if (S_ISREG(st.st_mode))
133 char *c = strrchr(buffer,
'.');
134 snprintf(drFile, 255,
"ppd:%s", buffer);
135 if (c && strncmp(c,
".ppd",4) == 0)
137 addFile(drFile,
"",
"");
139 else if (c && strncmp(c,
".gz", 3) == 0)
143 if (*(--c) ==
'.')
break;
145 if (*c ==
'.' && strncmp(c,
".ppd",4) == 0)
147 addFile(drFile,
"",
"");
156 else if (access(dirname, X_OK) != -1) {
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");
164 FILE* file = popen(filename,
"r");
169 while ((read = getline(&line, &len, file)) != -1) {
170 char * pos1 = strstr(line,
"\"");
172 char * pos2 = strstr(pos1 + 1,
"\"");
175 char * pos3 = strstr(pos1 + 1,
":");
178 int n2 = strlen(
"compressed-ppd:")+strlen(pos3+1);
179 ppduri = (
char*)malloc(n2*
sizeof(
char)+1);
181 strcat(ppduri,
"compressed-ppd:");
182 strcat(ppduri, pos3+1);
183 addFile(ppduri, dirname, pos2+1);
197 fprintf(stderr,
"Can't execute compressed driver handler : %s\n", dirname);
204 fprintf(stderr,
"Can't open drivers directory : %s\n", dirname);
209 void initCompressedPpd(
const char *dirname)
216 int parsePpdFile(
const char *filename,
const char * ,
const char * , FILE *output_file)
219 char line[4096], value[256], langver[64] = {0}, desc[256] = {0};
223 ppd_file = gzopen(filename,
"r");
224 if (ppd_file == NULL)
226 fprintf(stderr,
"Can't open driver file : %s\n", filename);
229 fprintf(output_file,
"FILE=ppd:%s\n",filename);
231 while (gzgets(ppd_file,line,4095) != Z_NULL)
233 PROCESS_PPD_FILE_CONTENTS
235 fprintf(output_file,
"\n");
241 int parseCompressedPpdFile(
const char *ppdfilename,
const char *origin,
const char *metadata, FILE *output_file)
243 char value[256], langver[64] = {0}, desc[256] = {0};
247 bool useFallbackExtractionMethod =
false;
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];
258 int pos = metadataProcessed.find(
"MFG:");
260 pos = metadataProcessed.find(
"MANUFACTURER:");
263 TQString manufacturer;
266 TQString pnpManufacturer;
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];
275 else if ((kvPair[0].upper() ==
"MDL") ||(kvPair[0].upper() ==
"MODEL")) {
276 modelName = kvPair[1];
284 else if ((kvPair[0].upper() ==
"DRV") || (kvPair[0].upper() ==
"DRIVER")) {
289 manufacturer = manufacturer.stripWhiteSpace();
290 modelName = modelName.stripWhiteSpace();
291 driver = driver.stripWhiteSpace();
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();
299 model = manufacturer +
" " + modelName +
" " + driver;
300 description = description +
" [" + languageVersion +
"]";
302 fprintf(output_file,
"FILE=compressed-ppd:%s:%s\n", origin, ppdfilename);
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());
310 if (pnpModel.length() > 0) {
311 fprintf(output_file,
"PNPMODEL=%s\n",pnpModel.ascii());
313 if (description.length() > 0) {
314 fprintf(output_file,
"DESCRIPTION=%s\n",description.ascii());
318 useFallbackExtractionMethod =
true;
322 if (useFallbackExtractionMethod) {
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);
331 FILE* file = popen(filename,
"r");
337 fprintf(output_file,
"FILE=compressed-ppd:%s:%s\n", origin, ppdfilename);
339 while ((read = getline(&line, &len, file)) != -1) {
340 PROCESS_PPD_FILE_CONTENTS
349 fprintf(stderr,
"Can't open driver file : %s\n", ppdfilename);
360 int main(
int argc,
char *argv[])
362 registerHandler(
"ppd:", initPpd, parsePpdFile);
363 registerHandler(
"compressed-ppd:", initCompressedPpd, parseCompressedPpdFile);
365 return execute(argc, argv);