fixing domain join and domain login problems
[samba.git] / source3 / utils / make_printerdef.c
1  /*
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Create printer definition files.
5
6    Copyright (C) Jean-Francois.Micouleau@utc.fr, 10/26/97 - 1998
7
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.
12
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.
17
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.
21 */
22
23 #include "includes.h"
24
25 /*
26 #define DEBUGIT
27 */
28
29 char *files_to_copy;
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)];
34
35 static void usage(char *name)
36 {
37  fprintf(stderr,"%s: printer.def \"Printer Name\"\n", name);
38 }
39
40 static char *myfgets(char *s, int n, FILE *stream)
41 {
42   char *LString1;
43   char *LString2;
44   char *temp;
45   pstring String;
46   pstring NewString;
47   int i;
48
49   fgets(s,n,stream);
50   while ((LString1 = strchr(s,'%')) != NULL) {
51     if (!(LString2 = strchr(LString1+1,'%'))) break;
52     *LString2 = '\0';
53     pstrcpy(String,LString1+1);
54     i = 0;
55     while(*sbuffer[i]!='\0') {
56       if (strncmp(sbuffer[i],String,strlen(String))==0)
57       {
58         pstrcpy(String,sbuffer[i]);
59         if ((temp = strchr(String,'=')) != NULL) ++temp;
60         pstrcpy(String,temp);
61         break;
62       }
63       i++;      
64     }
65     *LString1 = '\0';
66     pstrcpy(NewString,s);
67     pstrcat(NewString,String);
68     pstrcat(NewString,LString2+1);
69     pstrcpy(s, NewString);
70   }
71   return(s);
72 }
73
74 /*
75    This function split a line in two parts
76    on both side of the equal sign
77    "entry=value"
78 */
79 static char *scan(char *chaine,char **entry)
80 {
81   char *value;
82   char *temp;
83   int i=0;
84  
85   *entry=(char *)malloc(sizeof(pstring));
86   value=(char *)malloc(sizeof(pstring));
87
88   if(*entry == NULL || value == NULL) {
89     fprintf(stderr,"scan: malloc fail !\n");
90     exit(1);
91   }
92
93   pstrcpy(*entry,chaine);
94   temp=chaine;
95   while( temp[i]!='=' && temp[i]!='\0') {
96         i++;
97   }
98   (*entry)[i]='\0'; 
99   pstrcpy(value,temp+i+1);      
100   return (value);
101 }
102
103 static void build_subdir(void)
104 {
105   int i=0;
106   char *entry;
107   char *data;
108  
109   while (*buffer[i]!='\0') { 
110     data=scan(buffer[i],&entry);
111 #ifdef DEBUGIT
112     fprintf(stderr,"\tentry=data %s:%s\n",entry,data);
113 #endif      
114
115     if (strcmp(data,"11")==0) {
116       pstrcpy(sub_dir[i][0],entry);
117       pstrcpy(sub_dir[i][1],"");
118     }
119     if (strcmp(data,"23")==0) {
120       pstrcpy(sub_dir[i][0],entry);
121       pstrcpy(sub_dir[i][1],"color\\");
122     }
123 #ifdef DEBUGIT
124     fprintf(stderr,"\tsubdir %s:%s\n",sub_dir[i][0],sub_dir[i][1]);
125 #endif      
126     i++;
127   }
128 }
129
130 /*
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.
134 */
135 static void lookup_strings(FILE *fichier)
136 {
137   int found=0,pointeur=0,i=0;
138   char *temp,*temp2;
139   
140   temp=(char *)malloc(sizeof(pstring));
141   temp2=(char *)malloc(sizeof(pstring));
142   
143   if(temp == NULL || temp2 == NULL) {
144     fprintf(stderr,"lookup_strings: malloc fail !\n");
145     exit(1);
146   }
147
148   *sbuffer[0]='\0';
149   
150   pstrcpy(temp2,"[Strings]");
151   
152   rewind(fichier);
153 #ifdef DEBUGIT
154   fprintf(stderr,"\tLooking for Strings\n");
155 #endif
156   
157   while (!feof(fichier) && found==0) {
158         *temp='\0';
159         fgets(temp,255,fichier);
160         if (strncmp(temp,temp2,strlen(temp2))==0) found=1;
161   }
162
163
164   while (!feof(fichier) && found==1) {
165         *temp='\0';
166         fgets(temp,255,fichier);
167         if (*temp=='[') {
168                 found=2;
169                 *sbuffer[pointeur]='\0';
170         }
171         else {
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';
176                 pointeur++;
177         }  
178   }
179 #ifdef DEBUGIT
180   fprintf(stderr,"\t\tFound %d entries\n",pointeur-1);
181 #endif
182 }
183
184
185 /*
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.
189 */
190 static void lookup_entry(FILE *fichier,char *chaine)
191 {
192   int found=0,pointeur=0,i=0;
193   char *temp,*temp2;
194   
195   temp=(char *)malloc(sizeof(pstring));
196   temp2=(char *)malloc(sizeof(pstring));
197   
198   if(temp == NULL || temp2 == NULL) {
199     fprintf(stderr,"lookup_entry: malloc fail !\n");
200     exit(1);
201   }
202
203   *buffer[0]='\0';
204   
205   pstrcpy(temp2,"[");
206   pstrcat(temp2,chaine);
207   pstrcat(temp2,"]");
208   
209   rewind(fichier);
210 #ifdef DEBUGIT
211   fprintf(stderr,"\tLooking for %s\n",chaine);
212 #endif
213   
214   while (!feof(fichier) && found==0) {
215         *temp='\0';
216         myfgets(temp,255,fichier);
217         if (strncmp(temp,temp2,strlen(temp2))==0) found=1;
218   }
219
220
221   while (!feof(fichier) && found==1) {
222         *temp='\0';
223         myfgets(temp,255,fichier);
224         if (*temp=='[') {
225                 found=2;
226                 *buffer[pointeur]='\0';
227         }
228         else {
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';
233                 pointeur++;
234         }  
235   }
236 #ifdef DEBUGIT
237   fprintf(stderr,"\t\tFound %d entries\n",pointeur-1);
238 #endif
239 }
240
241 static char *find_desc(FILE *fichier,char *text)
242 {
243   char *chaine;
244   char *long_desc;
245   char *short_desc;
246   char *crap = NULL;
247   char *p;
248
249   int found=0;
250
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");
256     exit(1);
257   }
258
259   rewind(fichier);
260   while (!feof(fichier) && found==0)
261   {
262     myfgets(chaine,255,fichier);
263
264     long_desc=strtok(chaine,"=");
265     crap=strtok(NULL,",\r");
266
267     p=long_desc;
268     while(*p!='"' && *p!='\0')
269      p++;
270     if (*p=='"' && *(p+1)!='\0') p++;       
271     long_desc=p;
272
273     if (*p!='\0')
274     {
275       p++;
276       while(*p!='\"')
277        p++;
278       *p='\0';
279     }
280     if (!strcmp(text,long_desc)) 
281         found=1;
282   }
283   free(chaine);
284   if (!found || !crap) return(NULL);
285   while(*crap==' ') crap++;
286   pstrcpy(short_desc,crap);
287   return(short_desc);
288 }
289
290 static void scan_copyfiles(FILE *fichier, char *chaine)
291 {
292   char *part;
293   char *mpart;
294   int i;
295   pstring direc;
296 #ifdef DEBUGIT
297   fprintf(stderr,"In scan_copyfiles Lookup up of %s\n",chaine);
298 #endif 
299   fprintf(stderr,"\nCopy the following files to your printer$ share location:\n");
300   part=strtok(chaine,",");
301   do {
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
306     */
307     if (*part=='@') {
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]);
312     } else {
313       lookup_entry(fichier,part);
314       i=0;
315       pstrcpy(direc,"");
316       while (*sub_dir[i][0]!='\0') {
317 #ifdef DEBUGIT
318         fprintf(stderr,"\tsubdir %s:%s\n",sub_dir[i][0],sub_dir[i][1]);
319 #endif      
320         if (strcmp(sub_dir[i][0],part)==0)
321                 pstrcpy(direc,sub_dir[i][1]);
322         i++;
323       } 
324       i=0;
325       while (*buffer[i]!='\0') {
326 /*
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:
330  * 
331  * pscript.hlp =  pscript.hl_
332  * hpdcmon.dll,hpdcmon.dl_
333  * ctl3dv2.dll,ctl3dv2.dl_,ctl3dv2.tmp
334  *
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.
339  */
340         part = strchr(buffer[i],'=');
341         if (part) {
342           *part = '\0';
343           while (--part > buffer[i])
344             if ((*part == ' ') || (*part =='\t')) *part = '\0';
345             else break;
346         } else {
347           part = strchr(buffer[i],',');
348           if (part) {
349             if ((mpart = strrchr(part+1,','))!=NULL) {
350                 pstrcpy(buffer[i],mpart+1);
351             } else
352                 *part = '\0';
353             while (--part > buffer[i])
354               if ((*part == ' ') || (*part =='\t')) *part = '\0';
355               else break;
356           }
357         }
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]);
363         i++;
364       } 
365     }
366     part=strtok(NULL,",");
367   }
368   while (part!=NULL);
369   fprintf(stderr,"\n");
370 }
371
372
373 static void scan_short_desc(FILE *fichier, char *short_desc)
374 {
375   int i=0;
376   char *temp;
377   char *copyfiles=0,*datasection=0;
378  
379   helpfile=0;
380   languagemonitor=0;
381   datatype="RAW";
382   if((temp=(char *)malloc(sizeof(pstring))) == NULL) {
383     fprintf(stderr, "scan_short_desc: malloc fail !\n");
384     exit(1);
385   }
386   
387   driverfile=short_desc;
388   datafile=short_desc;
389
390   lookup_entry(fichier,short_desc);
391
392   while(*buffer[i]!='\0') {
393 #ifdef DEBUGIT
394     fprintf(stderr,"\tLookup up of %s\n",buffer[i]);
395 #endif
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);
410     i++;        
411   }
412
413   if (datasection) {
414     lookup_entry(fichier,datasection);
415
416     i = 0;
417     while(*buffer[i]!='\0') {
418 #ifdef DEBUGIT
419       fprintf(stderr,"\tLookup up of %s\n",buffer[i]);
420 #endif
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);
435       i++;      
436     }
437   }
438
439   if (languagemonitor) {
440         temp = strtok(languagemonitor,",");
441         if (*temp == '"') ++temp;
442         pstrcpy(languagemonitor,temp);
443         if ((temp = strchr(languagemonitor,'"'))!=NULL) *temp = '\0';
444   }
445
446   if (i) fprintf(stderr,"End of section found\n");
447  
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);
461 }
462
463 int main(int argc, char *argv[])
464 {
465   char *short_desc;
466   FILE *inf_file;
467
468   if (argc!=3)
469   {
470     usage(argv[0]);
471     return(-1);
472   }
473
474   inf_file=sys_fopen(argv[1],"r");  
475   if (!inf_file)
476   {
477     fprintf(stderr,"Description file not found, bye\n");
478     return(-1);
479   }
480
481   lookup_strings(inf_file);
482
483   short_desc=find_desc(inf_file,argv[2]);
484   if (short_desc==NULL)
485   {
486     fprintf(stderr,"Printer not found\n");
487     return(-1);
488   }
489   else fprintf(stderr,"Found:%s\n",short_desc);
490
491   lookup_entry(inf_file,"DestinationDirs");
492   build_subdir();
493
494   if((files_to_copy=(char *)malloc(2048*sizeof(char))) == NULL) {
495     fprintf(stderr, "%s: malloc fail.\n", argv[0] );
496     exit(1);
497   }
498   *files_to_copy='\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);
508   return 0;
509 }
510