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;
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') {
99 pstrcpy(value,temp+i+1);
103 static void build_subdir(void)
109 while (*buffer[i]!='\0') {
110 data=scan(buffer[i],&entry);
112 fprintf(stderr,"\tentry=data %s:%s\n",entry,data);
115 if (strcmp(data,"11")==0) {
116 pstrcpy(sub_dir[i][0],entry);
117 pstrcpy(sub_dir[i][1],"");
119 if (strcmp(data,"23")==0) {
120 pstrcpy(sub_dir[i][0],entry);
121 pstrcpy(sub_dir[i][1],"color\\");
124 fprintf(stderr,"\tsubdir %s:%s\n",sub_dir[i][0],sub_dir[i][1]);
131 Lockup Strings entry in a file
132 Return all the lines between the entry and the next one or the end of file
133 An entry is something between braces.
135 static void lookup_strings(FILE *fichier)
137 int found=0,pointeur=0,i=0;
140 temp=(char *)malloc(sizeof(pstring));
141 temp2=(char *)malloc(sizeof(pstring));
143 if(temp == NULL || temp2 == NULL) {
144 fprintf(stderr,"lookup_strings: malloc fail !\n");
150 pstrcpy(temp2,"[Strings]");
154 fprintf(stderr,"\tLooking for Strings\n");
157 while (!feof(fichier) && found==0) {
159 fgets(temp,255,fichier);
160 if (strncmp(temp,temp2,strlen(temp2))==0) found=1;
164 while (!feof(fichier) && found==1) {
166 fgets(temp,255,fichier);
169 *sbuffer[pointeur]='\0';
172 pstrcpy(sbuffer[pointeur],temp);
173 i=strlen(sbuffer[pointeur])-1;
174 while (sbuffer[pointeur][i]=='\r' || sbuffer[pointeur][i]=='\n')
175 sbuffer[pointeur][i--]='\0';
180 fprintf(stderr,"\t\tFound %d entries\n",pointeur-1);
186 Lockup an entry in a file
187 Return all the lines between the entry and the next one or the end of file
188 An entry is something between braces.
190 static void lookup_entry(FILE *fichier,char *chaine)
192 int found=0,pointeur=0,i=0;
195 temp=(char *)malloc(sizeof(pstring));
196 temp2=(char *)malloc(sizeof(pstring));
198 if(temp == NULL || temp2 == NULL) {
199 fprintf(stderr,"lookup_entry: malloc fail !\n");
206 pstrcat(temp2,chaine);
211 fprintf(stderr,"\tLooking for %s\n",chaine);
214 while (!feof(fichier) && found==0) {
216 myfgets(temp,255,fichier);
217 if (strncmp(temp,temp2,strlen(temp2))==0) found=1;
221 while (!feof(fichier) && found==1) {
223 myfgets(temp,255,fichier);
226 *buffer[pointeur]='\0';
229 pstrcpy(buffer[pointeur],temp);
230 i=strlen(buffer[pointeur])-1;
231 while (buffer[pointeur][i]=='\r' || buffer[pointeur][i]=='\n')
232 buffer[pointeur][i--]='\0';
237 fprintf(stderr,"\t\tFound %d entries\n",pointeur-1);
241 static char *find_desc(FILE *fichier,char *text)
251 chaine=(char *)malloc(sizeof(pstring));
252 long_desc=(char *)malloc(sizeof(pstring));
253 short_desc=(char *)malloc(sizeof(pstring));
254 if (!chaine || !long_desc || !short_desc) {
255 fprintf(stderr,"find_desc: Unable to malloc memory\n");
260 while (!feof(fichier) && found==0)
262 myfgets(chaine,255,fichier);
264 long_desc=strtok(chaine,"=");
265 crap=strtok(NULL,",\r");
268 while(*p!='"' && *p!='\0')
270 if (*p=='"' && *(p+1)!='\0') p++;
280 if (!strcmp(text,long_desc))
284 if (!found || !crap) return(NULL);
285 while(*crap==' ') crap++;
286 pstrcpy(short_desc,crap);
290 static void scan_copyfiles(FILE *fichier, char *chaine)
297 fprintf(stderr,"In scan_copyfiles Lookup up of %s\n",chaine);
299 fprintf(stderr,"\nCopy the following files to your printer$ share location:\n");
300 part=strtok(chaine,",");
302 /* If the entry start with a @ then it's a file to copy
303 else it's an entry refering to files to copy
304 the main difference is when it's an entry
305 you can have a directory to append before the file name
308 if (strlen(files_to_copy) != 0)
309 pstrcat(files_to_copy,",");
310 pstrcat(files_to_copy,&part[1]);
311 fprintf(stderr,"%s\n",&part[1]);
313 lookup_entry(fichier,part);
316 while (*sub_dir[i][0]!='\0') {
318 fprintf(stderr,"\tsubdir %s:%s\n",sub_dir[i][0],sub_dir[i][1]);
320 if (strcmp(sub_dir[i][0],part)==0)
321 pstrcpy(direc,sub_dir[i][1]);
325 while (*buffer[i]!='\0') {
327 * HP inf files have strange entries that this attempts to address
328 * Entries in the Copy sections normally have only a single file name
329 * on each line. I have seen the following format in various HP inf files:
331 * pscript.hlp = pscript.hl_
332 * hpdcmon.dll,hpdcmon.dl_
333 * ctl3dv2.dll,ctl3dv2.dl_,ctl3dv2.tmp
335 * In the first 2 cases you want the first file name - in the last case
336 * you only want the last file name (at least that is what a Win95
337 * machine sent). This may still be wrong but at least I get the same list
338 * of files as seen on a printer test page.
340 part = strchr(buffer[i],'=');
343 while (--part > buffer[i])
344 if ((*part == ' ') || (*part =='\t')) *part = '\0';
347 part = strchr(buffer[i],',');
349 if ((mpart = strrchr(part+1,','))!=NULL) {
350 pstrcpy(buffer[i],mpart+1);
353 while (--part > buffer[i])
354 if ((*part == ' ') || (*part =='\t')) *part = '\0';
358 if (strlen(files_to_copy) != 0)
359 pstrcat(files_to_copy,",");
360 pstrcat(files_to_copy,direc);
361 pstrcat(files_to_copy,buffer[i]);
362 fprintf(stderr,"%s%s\n",direc,buffer[i]);
366 part=strtok(NULL,",");
369 fprintf(stderr,"\n");
373 static void scan_short_desc(FILE *fichier, char *short_desc)
377 char *copyfiles=0,*datasection=0;
382 if((temp=(char *)malloc(sizeof(pstring))) == NULL) {
383 fprintf(stderr, "scan_short_desc: malloc fail !\n");
387 driverfile=short_desc;
390 lookup_entry(fichier,short_desc);
392 while(*buffer[i]!='\0') {
394 fprintf(stderr,"\tLookup up of %s\n",buffer[i]);
396 if (strncasecmp(buffer[i],"CopyFiles",9)==0)
397 copyfiles=scan(buffer[i],&temp);
398 else if (strncasecmp(buffer[i],"DataSection",11)==0)
399 datasection=scan(buffer[i],&temp);
400 else if (strncasecmp(buffer[i],"DataFile",8)==0)
401 datafile=scan(buffer[i],&temp);
402 else if (strncasecmp(buffer[i],"DriverFile",10)==0)
403 driverfile=scan(buffer[i],&temp);
404 else if (strncasecmp(buffer[i],"HelpFile",8)==0)
405 helpfile=scan(buffer[i],&temp);
406 else if (strncasecmp(buffer[i],"LanguageMonitor",15)==0)
407 languagemonitor=scan(buffer[i],&temp);
408 else if (strncasecmp(buffer[i],"DefaultDataType",15)==0)
409 datatype=scan(buffer[i],&temp);
414 lookup_entry(fichier,datasection);
417 while(*buffer[i]!='\0') {
419 fprintf(stderr,"\tLookup up of %s\n",buffer[i]);
421 if (strncasecmp(buffer[i],"CopyFiles",9)==0)
422 copyfiles=scan(buffer[i],&temp);
423 else if (strncasecmp(buffer[i],"DataSection",11)==0)
424 datasection=scan(buffer[i],&temp);
425 else if (strncasecmp(buffer[i],"DataFile",8)==0)
426 datafile=scan(buffer[i],&temp);
427 else if (strncasecmp(buffer[i],"DriverFile",10)==0)
428 driverfile=scan(buffer[i],&temp);
429 else if (strncasecmp(buffer[i],"HelpFile",8)==0)
430 helpfile=scan(buffer[i],&temp);
431 else if (strncasecmp(buffer[i],"LanguageMonitor",15)==0)
432 languagemonitor=scan(buffer[i],&temp);
433 else if (strncasecmp(buffer[i],"DefaultDataType",15)==0)
434 datatype=scan(buffer[i],&temp);
439 if (languagemonitor) {
440 temp = strtok(languagemonitor,",");
441 if (*temp == '"') ++temp;
442 pstrcpy(languagemonitor,temp);
443 if ((temp = strchr(languagemonitor,'"'))!=NULL) *temp = '\0';
446 if (i) fprintf(stderr,"End of section found\n");
448 fprintf(stderr,"CopyFiles: %s\n",
449 copyfiles?copyfiles:"(null)");
450 fprintf(stderr,"Datasection: %s\n",
451 datasection?datasection:"(null)");
452 fprintf(stderr,"Datafile: %s\n",
453 datafile?datafile:"(null)");
454 fprintf(stderr,"Driverfile: %s\n",
455 driverfile?driverfile:"(null)");
456 fprintf(stderr,"Helpfile: %s\n",
457 helpfile?helpfile:"(null)");
458 fprintf(stderr,"LanguageMonitor: %s\n",
459 languagemonitor?languagemonitor:"(null)");
460 if (copyfiles) scan_copyfiles(fichier,copyfiles);
463 int main(int argc, char *argv[])
474 inf_file=sys_fopen(argv[1],"r");
477 fprintf(stderr,"Description file not found, bye\n");
481 lookup_strings(inf_file);
483 short_desc=find_desc(inf_file,argv[2]);
484 if (short_desc==NULL)
486 fprintf(stderr,"Printer not found\n");
489 else fprintf(stderr,"Found:%s\n",short_desc);
491 lookup_entry(inf_file,"DestinationDirs");
494 if((files_to_copy=(char *)malloc(2048*sizeof(char))) == NULL) {
495 fprintf(stderr, "%s: malloc fail.\n", argv[0] );
499 scan_short_desc(inf_file,short_desc);
500 fprintf(stdout,"%s:%s:%s:",
501 argv[2],driverfile,datafile);
502 fprintf(stdout,"%s:",
503 helpfile?helpfile:"");
504 fprintf(stdout,"%s:",
505 languagemonitor?languagemonitor:"");
506 fprintf(stdout,"%s:",datatype);
507 fprintf(stdout,"%s\n",files_to_copy);