2 Unix SMB/Netbios implementation.
4 Create printer definition files.
6 Copyright (C) Jean-Francois.Micouleau@utc.fr, 10/26/97 - 1998
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 char *driverfile
, *datafile
, *helpfile
, *languagemonitor
, *datatype
, *vendorsetup
;
31 char buffer
[50][sizeof(pstring
)];
32 char sbuffer
[50][sizeof(pstring
)];
33 char sub_dir
[50][2][sizeof(pstring
)];
35 static void usage(char *name
)
37 fprintf(stderr
,"%s: printer.def \"Printer Name\"\n", name
);
40 static char *myfgets(char *s
, int n
, FILE *stream
)
50 while ((LString1
= strchr(s
,'%')) != NULL
) {
51 if (!(LString2
= strchr(LString1
+1,'%'))) break;
53 pstrcpy(String
,LString1
+1);
55 while(*sbuffer
[i
]!='\0') {
56 if (strncmp(sbuffer
[i
],String
,strlen(String
))==0)
58 pstrcpy(String
,sbuffer
[i
]);
59 if ((temp
= strchr(String
,'=')) != NULL
) ++temp
;
67 pstrcat(NewString
,String
);
68 pstrcat(NewString
,LString2
+1);
69 pstrcpy(s
, NewString
);
75 This function split a line in two parts
76 on both side of the equal sign
79 static char *scan(char *chaine
,char **entry
)
85 *entry
=(char *)malloc(sizeof(pstring
));
86 value
=(char *)malloc(sizeof(pstring
));
88 if(*entry
== NULL
|| value
== NULL
) {
89 fprintf(stderr
,"scan: malloc fail !\n");
93 pstrcpy(*entry
,chaine
);
95 while( temp
[i
]!='=' && temp
[i
]!='\0') {
102 while( temp
[i
]==' ' && temp
[i
]!='\0') {
105 pstrcpy(value
,temp
+i
);
109 static void build_subdir(void)
116 while (*buffer
[i
]!='\0') {
117 data
=scan(buffer
[i
],&entry
);
119 fprintf(stderr
,"\tentry=data %s:%s\n",entry
,data
);
123 if (entry
[j
-1] != ' ') break;
128 if (strncmp(data
,"11",2)==0) {
129 pstrcpy(sub_dir
[i
][0],entry
);
130 pstrcpy(sub_dir
[i
][1],"");
132 if (strncmp(data
,"23",2)==0) {
133 pstrcpy(sub_dir
[i
][0],entry
);
134 pstrcpy(sub_dir
[i
][1],"color\\");
137 fprintf(stderr
,"\tsubdir %s:%s\n",sub_dir
[i
][0],sub_dir
[i
][1]);
144 Lockup Strings entry in a file
145 Return all the lines between the entry and the next one or the end of file
146 An entry is something between braces.
148 static void lookup_strings(FILE *fichier
)
150 int found
=0,pointeur
=0,i
=0;
153 temp
=(char *)malloc(sizeof(pstring
));
154 temp2
=(char *)malloc(sizeof(pstring
));
156 if(temp
== NULL
|| temp2
== NULL
) {
157 fprintf(stderr
,"lookup_strings: malloc fail !\n");
163 pstrcpy(temp2
,"[Strings]");
167 fprintf(stderr
,"\tLooking for Strings\n");
170 while (!feof(fichier
) && found
==0) {
172 fgets(temp
,255,fichier
);
173 if (strncmp(temp
,temp2
,strlen(temp2
))==0) found
=1;
177 while (!feof(fichier
) && found
==1) {
179 fgets(temp
,255,fichier
);
182 *sbuffer
[pointeur
]='\0';
185 pstrcpy(sbuffer
[pointeur
],temp
);
186 i
=strlen(sbuffer
[pointeur
])-1;
187 while (sbuffer
[pointeur
][i
]=='\r' || sbuffer
[pointeur
][i
]=='\n')
188 sbuffer
[pointeur
][i
--]='\0';
193 /* CCMRCF Mod, seg fault or worse if not found */
195 fprintf(stderr
,"Printer not found\tNo [Strings] block in inf file\n");
200 fprintf(stderr
,"\t\tFound %d entries\n",pointeur
-1);
206 Lockup an entry in a file
207 Return all the lines between the entry and the next one or the end of file
208 An entry is something between braces.
210 static void lookup_entry(FILE *fichier
,char *chaine
)
212 int found
=0,pointeur
=0,i
=0;
215 temp
=(char *)malloc(sizeof(pstring
));
216 temp2
=(char *)malloc(sizeof(pstring
));
218 if(temp
== NULL
|| temp2
== NULL
) {
219 fprintf(stderr
,"lookup_entry: malloc fail !\n");
226 pstrcat(temp2
,chaine
);
231 fprintf(stderr
,"\tLooking for %s\n",chaine
);
234 while (!feof(fichier
) && found
==0) {
236 myfgets(temp
,255,fichier
);
237 if (strncmp(temp
,temp2
,strlen(temp2
))==0) found
=1;
241 while (!feof(fichier
) && found
==1) {
243 myfgets(temp
,255,fichier
);
246 *buffer
[pointeur
]='\0';
249 pstrcpy(buffer
[pointeur
],temp
);
250 i
=strlen(buffer
[pointeur
])-1;
251 while (buffer
[pointeur
][i
]=='\r' || buffer
[pointeur
][i
]=='\n')
252 buffer
[pointeur
][i
--]='\0';
257 fprintf(stderr
,"\t\tFound %d entries\n",pointeur
-1);
261 static char *find_desc(FILE *fichier
,char *text
)
271 chaine
=(char *)malloc(sizeof(pstring
));
272 long_desc
=(char *)malloc(sizeof(pstring
));
273 short_desc
=(char *)malloc(sizeof(pstring
));
274 if (!chaine
|| !long_desc
|| !short_desc
) {
275 fprintf(stderr
,"find_desc: Unable to malloc memory\n");
280 while (!feof(fichier
) && found
==0)
282 myfgets(chaine
,255,fichier
);
284 long_desc
=strtok(chaine
,"=");
285 crap
=strtok(NULL
,",\r");
288 while(*p
!='"' && *p
!='\0')
290 if (*p
=='"' && *(p
+1)!='\0') p
++;
300 if (!strcmp(text
,long_desc
))
304 if (!found
|| !crap
) return(NULL
);
305 while(*crap
==' ') crap
++;
306 pstrcpy(short_desc
,crap
);
310 static void scan_copyfiles(FILE *fichier
, char *chaine
)
317 fprintf(stderr
,"In scan_copyfiles Lookup up of %s\n",chaine
);
319 fprintf(stderr
,"\nCopy the following files to your printer$ share location:\n");
320 part
=strtok(chaine
,",");
322 /* If the entry start with a @ then it's a file to copy
323 else it's an entry refering to files to copy
324 the main difference is when it's an entry
325 you can have a directory to append before the file name
328 if (strlen(files_to_copy
) != 0)
329 pstrcat(files_to_copy
,",");
330 pstrcat(files_to_copy
,&part
[1]);
331 fprintf(stderr
,"%s\n",&part
[1]);
333 lookup_entry(fichier
,part
);
336 while (*sub_dir
[i
][0]!='\0') {
338 fprintf(stderr
,"\tsubdir %s:%s\n",sub_dir
[i
][0],sub_dir
[i
][1]);
340 if (strcmp(sub_dir
[i
][0],part
)==0)
341 pstrcpy(direc
,sub_dir
[i
][1]);
345 while (*buffer
[i
]!='\0') {
347 * HP inf files have strange entries that this attempts to address
348 * Entries in the Copy sections normally have only a single file name
349 * on each line. I have seen the following format in various HP inf files:
351 * pscript.hlp = pscript.hl_
352 * hpdcmon.dll,hpdcmon.dl_
353 * MSVCRT.DLL,MSVCRT.DL_,,32
354 * ctl3dv2.dll,ctl3dv2.dl_,ctl3dv2.tmp
356 * In the first 2 cases you want the first file name - in the last case
357 * you only want the last file name (at least that is what a Win95
358 * machine sent). In the third case you also want the first file name
359 * (detect by the last component being just a number ?).
360 * This may still be wrong but at least I get the same list
361 * of files as seen on a printer test page.
363 part
= strchr(buffer
[i
],'=');
366 * Case (1) eg. pscript.hlp = pscript.hl_ - chop after the first name.
372 * Now move back to the start and print that.
375 while (--part
> buffer
[i
]) {
376 if ((*part
== ' ') || (*part
=='\t'))
382 part
= strchr(buffer
[i
],',');
388 if ((mpart
= strrchr(part
+1,','))!=NULL
) {
390 * Second ',' - case 3 or 4.
391 * Check if the last part is just a number,
392 * if so we need the first part.
396 BOOL isnumber
= False
;
399 (void)strtol(mpart
, &endptr
, 10);
401 isnumber
= ((endptr
> mpart
) && isdigit(*mpart
));
403 pstrcpy(buffer
[i
],mpart
+1);
409 while (--part
> buffer
[i
])
410 if ((*part
== ' ') || (*part
=='\t')) *part
= '\0';
414 if (*buffer
[i
] != ';') {
415 if (strlen(files_to_copy
) != 0)
416 pstrcat(files_to_copy
,",");
417 pstrcat(files_to_copy
,direc
);
418 pstrcat(files_to_copy
,buffer
[i
]);
419 fprintf(stderr
,"%s%s\n",direc
,buffer
[i
]);
424 part
=strtok(NULL
,",");
426 while( *part
==' ' && *part
!= '\0') {
430 } while (part
!=NULL
);
431 fprintf(stderr
,"\n");
435 static void scan_short_desc(FILE *fichier
, char *short_desc
)
439 char *copyfiles
=0,*datasection
=0;
445 if((temp
=(char *)malloc(sizeof(pstring
))) == NULL
) {
446 fprintf(stderr
, "scan_short_desc: malloc fail !\n");
450 driverfile
=short_desc
;
453 lookup_entry(fichier
,short_desc
);
455 while(*buffer
[i
]!='\0') {
457 fprintf(stderr
,"\tLookup up of %s\n",buffer
[i
]);
459 if (strncasecmp(buffer
[i
],"CopyFiles",9)==0)
460 copyfiles
=scan(buffer
[i
],&temp
);
461 else if (strncasecmp(buffer
[i
],"DataSection",11)==0)
462 datasection
=scan(buffer
[i
],&temp
);
463 else if (strncasecmp(buffer
[i
],"DataFile",8)==0)
464 datafile
=scan(buffer
[i
],&temp
);
465 else if (strncasecmp(buffer
[i
],"DriverFile",10)==0)
466 driverfile
=scan(buffer
[i
],&temp
);
467 else if (strncasecmp(buffer
[i
],"HelpFile",8)==0)
468 helpfile
=scan(buffer
[i
],&temp
);
469 else if (strncasecmp(buffer
[i
],"LanguageMonitor",15)==0)
470 languagemonitor
=scan(buffer
[i
],&temp
);
471 else if (strncasecmp(buffer
[i
],"DefaultDataType",15)==0)
472 datatype
=scan(buffer
[i
],&temp
);
473 else if (strncasecmp(buffer
[i
],"VendorSetup",11)==0)
474 vendorsetup
=scan(buffer
[i
],&temp
);
479 lookup_entry(fichier
,datasection
);
482 while(*buffer
[i
]!='\0') {
484 fprintf(stderr
,"\tLookup up of %s\n",buffer
[i
]);
486 if (strncasecmp(buffer
[i
],"CopyFiles",9)==0)
487 copyfiles
=scan(buffer
[i
],&temp
);
488 else if (strncasecmp(buffer
[i
],"DataSection",11)==0)
489 datasection
=scan(buffer
[i
],&temp
);
490 else if (strncasecmp(buffer
[i
],"DataFile",8)==0)
491 datafile
=scan(buffer
[i
],&temp
);
492 else if (strncasecmp(buffer
[i
],"DriverFile",10)==0)
493 driverfile
=scan(buffer
[i
],&temp
);
494 else if (strncasecmp(buffer
[i
],"HelpFile",8)==0)
495 helpfile
=scan(buffer
[i
],&temp
);
496 else if (strncasecmp(buffer
[i
],"LanguageMonitor",15)==0)
497 languagemonitor
=scan(buffer
[i
],&temp
);
498 else if (strncasecmp(buffer
[i
],"DefaultDataType",15)==0)
499 datatype
=scan(buffer
[i
],&temp
);
500 else if (strncasecmp(buffer
[i
],"VendorSetup",11)==0)
501 vendorsetup
=scan(buffer
[i
],&temp
);
506 if (languagemonitor
) {
507 temp
= strtok(languagemonitor
,",");
508 if (*temp
== '"') ++temp
;
509 pstrcpy(languagemonitor
,temp
);
510 if ((temp
= strchr(languagemonitor
,'"'))!=NULL
) *temp
= '\0';
513 if (i
) fprintf(stderr
,"End of section found\n");
515 fprintf(stderr
,"CopyFiles: %s\n",
516 copyfiles
?copyfiles
:"(null)");
517 fprintf(stderr
,"Datasection: %s\n",
518 datasection
?datasection
:"(null)");
519 fprintf(stderr
,"Datafile: %s\n",
520 datafile
?datafile
:"(null)");
521 fprintf(stderr
,"Driverfile: %s\n",
522 driverfile
?driverfile
:"(null)");
523 fprintf(stderr
,"Helpfile: %s\n",
524 helpfile
?helpfile
:"(null)");
525 fprintf(stderr
,"LanguageMonitor: %s\n",
526 languagemonitor
?languagemonitor
:"(null)");
527 fprintf(stderr
,"VendorSetup: %s\n",
528 vendorsetup
?vendorsetup
:"(null)");
529 if (copyfiles
) scan_copyfiles(fichier
,copyfiles
);
532 int main(int argc
, char *argv
[])
543 inf_file
=sys_fopen(argv
[1],"r");
546 fprintf(stderr
,"Description file not found, bye\n");
550 lookup_strings(inf_file
);
552 short_desc
=find_desc(inf_file
,argv
[2]);
553 if (short_desc
==NULL
)
555 fprintf(stderr
,"Printer not found\n");
558 else fprintf(stderr
,"Found:%s\n",short_desc
);
560 lookup_entry(inf_file
,"DestinationDirs");
563 if((files_to_copy
=(char *)malloc(2048*sizeof(char))) == NULL
) {
564 fprintf(stderr
, "%s: malloc fail.\n", argv
[0] );
568 scan_short_desc(inf_file
,short_desc
);
569 fprintf(stdout
,"%s:%s:%s:",
570 argv
[2],driverfile
,datafile
);
571 fprintf(stdout
,"%s:",
572 helpfile
?helpfile
:"");
573 fprintf(stdout
,"%s:",
574 languagemonitor
?languagemonitor
:"");
575 fprintf(stdout
,"%s:",datatype
);
576 fprintf(stdout
,"%s\n",files_to_copy
);