2 Unix SMB/CIFS implementation.
3 Create printer definition files.
5 Copyright (C) Jean-Francois.Micouleau@utc.fr, 10/26/97 - 1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 char *driverfile, *datafile, *helpfile, *languagemonitor, *datatype, *vendorsetup;
30 char buffer[50][sizeof(pstring)];
31 char sbuffer[50][sizeof(pstring)];
32 char sub_dir[50][2][sizeof(pstring)];
34 static void usage(char *name)
36 fprintf(stderr,"%s: printer.def \"Printer Name\"\n", name);
39 static char *myfgets(char *s, int n, FILE *stream)
49 while ((LString1 = strchr_m(s,'%')) != NULL) {
50 if (!(LString2 = strchr_m(LString1+1,'%'))) break;
52 pstrcpy(String,LString1+1);
54 while(*sbuffer[i]!='\0') {
55 if (strncmp(sbuffer[i],String,strlen(String))==0)
57 pstrcpy(String,sbuffer[i]);
58 if ((temp = strchr_m(String,'=')) != NULL) ++temp;
66 pstrcat(NewString,String);
67 pstrcat(NewString,LString2+1);
68 pstrcpy(s, NewString);
74 This function split a line in two parts
75 on both side of the equal sign
78 static char *scan(char *chaine,char **entry)
84 *entry=(char *)malloc(sizeof(pstring));
85 value=(char *)malloc(sizeof(pstring));
87 if(*entry == NULL || value == NULL) {
88 fprintf(stderr,"scan: malloc fail !\n");
92 pstrcpy(*entry,chaine);
94 while( temp[i]!='=' && temp[i]!='\0') {
101 while( temp[i]==' ' && temp[i]!='\0') {
104 pstrcpy(value,temp+i);
108 static void build_subdir(void)
115 while (*buffer[i]!='\0') {
116 data=scan(buffer[i],&entry);
118 fprintf(stderr,"\tentry=data %s:%s\n",entry,data);
122 if (entry[j-1] != ' ') break;
127 if (strncmp(data,"11",2)==0) {
128 pstrcpy(sub_dir[i][0],entry);
129 pstrcpy(sub_dir[i][1],"");
131 if (strncmp(data,"23",2)==0) {
132 pstrcpy(sub_dir[i][0],entry);
133 pstrcpy(sub_dir[i][1],"color\\");
136 fprintf(stderr,"\tsubdir %s:%s\n",sub_dir[i][0],sub_dir[i][1]);
143 Lockup Strings entry in a file
144 Return all the lines between the entry and the next one or the end of file
145 An entry is something between braces.
147 static void lookup_strings(FILE *fichier)
149 int found=0,pointeur=0,i=0;
152 temp=(char *)malloc(sizeof(pstring));
153 temp2=(char *)malloc(sizeof(pstring));
155 if(temp == NULL || temp2 == NULL) {
158 fprintf(stderr,"lookup_strings: malloc fail !\n");
164 pstrcpy(temp2,"[Strings]");
168 fprintf(stderr,"\tLooking for Strings\n");
171 while (!feof(fichier) && found==0) {
173 fgets(temp,255,fichier);
174 if (strncmp(temp,temp2,strlen(temp2))==0) found=1;
178 while (!feof(fichier) && found==1) {
180 fgets(temp,255,fichier);
183 *sbuffer[pointeur]='\0';
186 pstrcpy(sbuffer[pointeur],temp);
187 i=strlen(sbuffer[pointeur])-1;
188 while (sbuffer[pointeur][i]=='\r' || sbuffer[pointeur][i]=='\n')
189 sbuffer[pointeur][i--]='\0';
194 /* CCMRCF Mod, seg fault or worse if not found */
196 fprintf(stderr,"Printer not found\tNo [Strings] block in inf file\n");
201 fprintf(stderr,"\t\tFound %d entries\n",pointeur-1);
207 Lockup an entry in a file
208 Return all the lines between the entry and the next one or the end of file
209 An entry is something between braces.
211 static void lookup_entry(FILE *fichier,char *chaine)
213 int found=0,pointeur=0,i=0;
216 temp=(char *)malloc(sizeof(pstring));
217 temp2=(char *)malloc(sizeof(pstring));
219 if(temp == NULL || temp2 == NULL) {
222 fprintf(stderr,"lookup_entry: malloc fail !\n");
229 pstrcat(temp2,chaine);
234 fprintf(stderr,"\tLooking for %s\n",chaine);
237 while (!feof(fichier) && found==0) {
239 myfgets(temp,255,fichier);
240 if (strncmp(temp,temp2,strlen(temp2))==0) found=1;
244 while (!feof(fichier) && found==1) {
246 myfgets(temp,255,fichier);
249 *buffer[pointeur]='\0';
252 pstrcpy(buffer[pointeur],temp);
253 i=strlen(buffer[pointeur])-1;
254 while (buffer[pointeur][i]=='\r' || buffer[pointeur][i]=='\n')
255 buffer[pointeur][i--]='\0';
260 fprintf(stderr,"\t\tFound %d entries\n",pointeur-1);
264 static char *find_desc(FILE *fichier,char *text)
274 chaine=(char *)malloc(sizeof(pstring));
275 long_desc=(char *)malloc(sizeof(pstring));
276 short_desc=(char *)malloc(sizeof(pstring));
277 if (!chaine || !long_desc || !short_desc) {
279 SAFE_FREE(long_desc);
280 SAFE_FREE(short_desc);
281 fprintf(stderr,"find_desc: Unable to malloc memory\n");
286 while (!feof(fichier) && found==0)
288 myfgets(chaine,255,fichier);
290 long_desc=strtok(chaine,"=");
291 crap=strtok(NULL,",\r");
294 while(*p!='"' && *p!='\0')
296 if (*p=='"' && *(p+1)!='\0') p++;
306 if (!strcmp(text,long_desc))
310 if (!found || !crap) return(NULL);
311 while(*crap==' ') crap++;
312 pstrcpy(short_desc,crap);
316 static void scan_copyfiles(FILE *fichier, char *chaine)
323 fprintf(stderr,"In scan_copyfiles Lookup up of %s\n",chaine);
325 fprintf(stderr,"\nCopy the following files to your printer$ share location:\n");
326 part=strtok(chaine,",");
328 /* If the entry start with a @ then it's a file to copy
329 else it's an entry refering to files to copy
330 the main difference is when it's an entry
331 you can have a directory to append before the file name
334 if (strlen(files_to_copy) != 0)
335 pstrcat(files_to_copy,",");
336 pstrcat(files_to_copy,&part[1]);
337 fprintf(stderr,"%s\n",&part[1]);
339 lookup_entry(fichier,part);
342 while (*sub_dir[i][0]!='\0') {
344 fprintf(stderr,"\tsubdir %s:%s\n",sub_dir[i][0],sub_dir[i][1]);
346 if (strcmp(sub_dir[i][0],part)==0)
347 pstrcpy(direc,sub_dir[i][1]);
351 while (*buffer[i]!='\0') {
353 * HP inf files have strange entries that this attempts to address
354 * Entries in the Copy sections normally have only a single file name
355 * on each line. I have seen the following format in various HP inf files:
357 * pscript.hlp = pscript.hl_
358 * hpdcmon.dll,hpdcmon.dl_
359 * MSVCRT.DLL,MSVCRT.DL_,,32
360 * ctl3dv2.dll,ctl3dv2.dl_,ctl3dv2.tmp
362 * In the first 2 cases you want the first file name - in the last case
363 * you only want the last file name (at least that is what a Win95
364 * machine sent). In the third case you also want the first file name
365 * (detect by the last component being just a number ?).
366 * This may still be wrong but at least I get the same list
367 * of files as seen on a printer test page.
369 part = strchr_m(buffer[i],'=');
372 * Case (1) eg. pscript.hlp = pscript.hl_ - chop after the first name.
378 * Now move back to the start and print that.
381 while (--part > buffer[i]) {
382 if ((*part == ' ') || (*part =='\t'))
388 part = strchr_m(buffer[i],',');
394 if ((mpart = strrchr_m(part+1,','))!=NULL) {
396 * Second ',' - case 3 or 4.
397 * Check if the last part is just a number,
398 * if so we need the first part.
402 BOOL isnumber = False;
405 (void)strtol(mpart, &endptr, 10);
407 isnumber = ((endptr > mpart) && isdigit(*mpart));
409 pstrcpy(buffer[i],mpart+1);
415 while (--part > buffer[i])
416 if ((*part == ' ') || (*part =='\t')) *part = '\0';
420 if (*buffer[i] != ';') {
421 if (strlen(files_to_copy) != 0)
422 pstrcat(files_to_copy,",");
423 pstrcat(files_to_copy,direc);
424 pstrcat(files_to_copy,buffer[i]);
425 fprintf(stderr,"%s%s\n",direc,buffer[i]);
430 part=strtok(NULL,",");
432 while( *part ==' ' && *part != '\0') {
436 } while (part!=NULL);
437 fprintf(stderr,"\n");
441 static void scan_short_desc(FILE *fichier, char *short_desc)
445 char *copyfiles=0,*datasection=0;
451 if((temp=(char *)malloc(sizeof(pstring))) == NULL) {
452 fprintf(stderr, "scan_short_desc: malloc fail !\n");
456 driverfile=short_desc;
459 lookup_entry(fichier,short_desc);
461 while(*buffer[i]!='\0') {
463 fprintf(stderr,"\tLookup up of %s\n",buffer[i]);
465 if (strncasecmp(buffer[i],"CopyFiles",9)==0)
466 copyfiles=scan(buffer[i],&temp);
467 else if (strncasecmp(buffer[i],"DataSection",11)==0)
468 datasection=scan(buffer[i],&temp);
469 else if (strncasecmp(buffer[i],"DataFile",8)==0)
470 datafile=scan(buffer[i],&temp);
471 else if (strncasecmp(buffer[i],"DriverFile",10)==0)
472 driverfile=scan(buffer[i],&temp);
473 else if (strncasecmp(buffer[i],"HelpFile",8)==0)
474 helpfile=scan(buffer[i],&temp);
475 else if (strncasecmp(buffer[i],"LanguageMonitor",15)==0)
476 languagemonitor=scan(buffer[i],&temp);
477 else if (strncasecmp(buffer[i],"DefaultDataType",15)==0)
478 datatype=scan(buffer[i],&temp);
479 else if (strncasecmp(buffer[i],"VendorSetup",11)==0)
480 vendorsetup=scan(buffer[i],&temp);
485 lookup_entry(fichier,datasection);
488 while(*buffer[i]!='\0') {
490 fprintf(stderr,"\tLookup up of %s\n",buffer[i]);
492 if (strncasecmp(buffer[i],"CopyFiles",9)==0)
493 copyfiles=scan(buffer[i],&temp);
494 else if (strncasecmp(buffer[i],"DataSection",11)==0)
495 datasection=scan(buffer[i],&temp);
496 else if (strncasecmp(buffer[i],"DataFile",8)==0)
497 datafile=scan(buffer[i],&temp);
498 else if (strncasecmp(buffer[i],"DriverFile",10)==0)
499 driverfile=scan(buffer[i],&temp);
500 else if (strncasecmp(buffer[i],"HelpFile",8)==0)
501 helpfile=scan(buffer[i],&temp);
502 else if (strncasecmp(buffer[i],"LanguageMonitor",15)==0)
503 languagemonitor=scan(buffer[i],&temp);
504 else if (strncasecmp(buffer[i],"DefaultDataType",15)==0)
505 datatype=scan(buffer[i],&temp);
506 else if (strncasecmp(buffer[i],"VendorSetup",11)==0)
507 vendorsetup=scan(buffer[i],&temp);
512 if (languagemonitor) {
513 temp = strtok(languagemonitor,",");
514 if (*temp == '"') ++temp;
515 pstrcpy(languagemonitor,temp);
516 if ((temp = strchr_m(languagemonitor,'"'))!=NULL) *temp = '\0';
519 if (i) fprintf(stderr,"End of section found\n");
521 fprintf(stderr,"CopyFiles: %s\n",
522 copyfiles?copyfiles:"(null)");
523 fprintf(stderr,"Datasection: %s\n",
524 datasection?datasection:"(null)");
525 fprintf(stderr,"Datafile: %s\n",
526 datafile?datafile:"(null)");
527 fprintf(stderr,"Driverfile: %s\n",
528 driverfile?driverfile:"(null)");
529 fprintf(stderr,"Helpfile: %s\n",
530 helpfile?helpfile:"(null)");
531 fprintf(stderr,"LanguageMonitor: %s\n",
532 languagemonitor?languagemonitor:"(null)");
533 fprintf(stderr,"VendorSetup: %s\n",
534 vendorsetup?vendorsetup:"(null)");
535 if (copyfiles) scan_copyfiles(fichier,copyfiles);
538 int main(int argc, char *argv[])
549 inf_file=sys_fopen(argv[1],"r");
552 fprintf(stderr,"Description file not found, bye\n");
556 lookup_strings(inf_file);
558 short_desc=find_desc(inf_file,argv[2]);
559 if (short_desc==NULL)
561 fprintf(stderr,"Printer not found\n");
564 else fprintf(stderr,"Found:%s\n",short_desc);
566 lookup_entry(inf_file,"DestinationDirs");
569 if((files_to_copy=(char *)malloc(2048*sizeof(char))) == NULL) {
570 fprintf(stderr, "%s: malloc fail.\n", argv[0] );
574 scan_short_desc(inf_file,short_desc);
575 fprintf(stdout,"%s:%s:%s:",
576 argv[2],driverfile,datafile);
577 fprintf(stdout,"%s:",
578 helpfile?helpfile:"");
579 fprintf(stdout,"%s:",
580 languagemonitor?languagemonitor:"");
581 fprintf(stdout,"%s:",datatype);
582 fprintf(stdout,"%s\n",files_to_copy);