winnt-win2000-win98-afs-client-updates-20010623
[openafs.git] / src / config / util_cr.c
1 /* crlf.c : Defines the entry point for the console application.*/
2
3 /* Copyright 2000, International Business Machines Corporation and others.
4         All Rights Reserved.
5  
6         This software has been released under the terms of the IBM Public
7         License.  For details, see the LICENSE file in the top-level source
8         directory or online at http://www.openafs.org/dl/license10.html
9 */
10
11 #include "stdio.h"
12 #include "io.h"\r
13 #include <assert.h>
14 #include "string.h"
15 #include "process.h"
16 #include "windows.h"
17 #include "malloc.h"
18
19 void usuage()
20 {
21         printf("util_cr file ;remove cr (from crlf)\n\
22         OR util_cr + file ;add cr\n \
23         OR util_cr * \"+[register key value] x=y\" ; add register key value\n\
24         OR util_cr * \"-[register key value]\" ; aremove register key value\n\
25         OR util_cr & file.ini \"SectionKey=value\" ; update ini-ipr-pwf file\n\
26         OR util_cr ~  ;force error\n");
27         exit(0xc000);
28 }
29
30
31 void Addkey (const char *hkey,const char *subkey,const char *stag,const char *sval)
32 {
33         DWORD disposition,result;
34         HKEY kPkey,kHkey=0;
35         if (strcmp(hkey,"HKEY_CLASSES_ROOT")==0) kHkey=HKEY_CLASSES_ROOT;
36         if (strcmp(hkey,"HKEY_CURRENT_USER")==0) kHkey=HKEY_CURRENT_USER;
37         if (strcmp(hkey,"HKEY_LOCAL_MACHINE")==0) kHkey=HKEY_LOCAL_MACHINE;
38         if (kHkey==0)
39                 usuage();
40         result=(RegCreateKeyEx(kHkey    /*HKEY_LOCAL_MACHINE*/
41                 ,subkey
42                 ,0,NULL
43                 ,REG_OPTION_NON_VOLATILE
44                 ,KEY_ALL_ACCESS,NULL
45                 ,&kPkey
46                 ,&disposition)==ERROR_SUCCESS);
47         if(!result)
48         {
49                 printf("AFS Error - Could Not create a registration key\n");
50                 exit(0xc000);
51         }
52         if (stag==NULL) return;
53         if ((sval)&&(strlen(sval)))
54         {
55                 if (*stag=='@')
56                         result=RegSetValueEx(kPkey,"",0,REG_SZ,(CONST BYTE *)sval,strlen(sval));
57                 else
58                         result=RegSetValueEx(kPkey,stag,0,REG_SZ,(CONST BYTE *)sval,strlen(sval));
59         } else {
60
61                 if (*stag=='@')
62                         result=(RegSetValueEx(kPkey,"",0,REG_SZ,(CONST BYTE *)"",0));
63                 else
64                         result=(RegSetValueEx(kPkey,stag,0,REG_SZ,(CONST BYTE *)"",0));
65         }
66         if(result!=ERROR_SUCCESS)
67         {
68                 printf("AFS Error - Could Not create a registration key\n");
69                 exit(0xc000);
70         }
71 }
72
73 void Subkey(const char *hkey,const char *subkey)
74 {
75         DWORD result;
76         HKEY kHkey=0;
77         if (strcmp(hkey,"HKEY_CLASSES_ROOT")==0) kHkey=HKEY_CLASSES_ROOT;
78         if (strcmp(hkey,"HKEY_CURRENT_USER")==0) kHkey=HKEY_CURRENT_USER;
79         if (strcmp(hkey,"HKEY_LOCAL_MACHINE")==0) kHkey=HKEY_LOCAL_MACHINE;
80         if (kHkey==0)
81                 usuage();
82         result=RegDeleteKey(
83                 kHkey,
84                 subkey
85         );
86         if(result!=ERROR_SUCCESS)
87         {
88                 printf("AFS Error - Could Not create a registration key\n");
89                 exit(0xc000);
90         }
91 }
92
93 int main(int argc, char* argv[])
94 {
95         char fname[128];
96         FILE *file;
97         int l,i;
98         char **pvar,*ch;
99         long len;
100         typedef char * CHARP;\r
101
102         if (argc<3)
103                 usuage();
104         if (strcmp(argv[1],"~")==0)
105         {       //check for file presence\r
106                 if (fopen(argv[2],"r"))\r
107                         return(0);
108                 printf("Error---%s\n",argv[3]);\r
109                 exit(0xc000);
110         }
111         if (strcmp(argv[1],"*")==0)
112         {               /* "[HKEY_CLASSES_ROOT\CLSID\{DC515C27-6CAC-11D1-BAE7-00C04FD140D2}]  @=AFS Client Shell Extension" */
113                 if (argc<3)
114                         usuage();
115                 for (i=2;argc>=3;i++)
116                 {
117                         char *ssub=strtok(argv[i],"[");
118                         BOOL option;
119                         char *skey=strtok(NULL,"]");
120                         char *sval,*stag;
121                         if ((ssub==NULL) || (skey==NULL))
122                         {
123                                 printf("format error parameter %s\n",argv[i]);
124                                 exit(0xc000);
125                         }
126                         option=(*ssub=='-');
127                         stag=strtok(NULL,"\0");
128                         if (stag)
129                         while(*stag==' ') 
130                                 stag++;
131                         ssub=strtok(skey,"\\");
132                         ssub=strtok(NULL,"\0");
133                         sval=strtok(stag,"=");
134                         sval=strtok(NULL,"\0");
135                         switch (option)
136                         {
137                         case 0:
138                                 Addkey (skey,ssub,stag,sval);
139                                 break;
140                         default :
141                                 if (stag)
142                                         Addkey (skey,ssub,stag,"");
143                                 else
144                                         Subkey(skey,ssub);
145                                 break;
146                         }
147
148                         argc-=1;
149                 }
150                 return 0;
151         }
152         if (strcmp(argv[1],"&")==0)
153         {
154                 if (argc<4)
155                         usuage();
156                 for (i=3;argc>=4;i++)
157                 {
158                         char *ssect=strtok(argv[i],"[");
159                         char *skey=strtok(argv[i],"]");
160                         char *sval;
161                         skey=strtok(NULL,"=");
162                         if ((ssect==NULL) || (skey==NULL))
163                         {
164                                 printf("format error parameter %s\n",argv[i]);
165                                 exit(0xc000);
166                         }
167                         while(*skey==' ') 
168                                 skey++;
169
170                         sval=strtok(NULL,"=");
171                         if (sval==NULL)
172                         {
173                                 printf("format error parameter %s\n",argv[i]);
174                                 exit(0xc000);
175                         }
176 //                      printf("parameters %s %s %s %s\n",ssect,skey,sval,argv[2]);
177                         if (WritePrivateProfileString(ssect,skey,sval,argv[2])==0)
178                         {
179                                 LPVOID lpMsgBuf;
180                                 FormatMessage( 
181                                         FORMAT_MESSAGE_ALLOCATE_BUFFER | 
182                                         FORMAT_MESSAGE_FROM_SYSTEM | 
183                                         FORMAT_MESSAGE_IGNORE_INSERTS,
184                                         NULL,
185                                         GetLastError(),
186                                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
187                                         (LPTSTR) &lpMsgBuf,
188                                         0,
189                                         NULL 
190                                 );
191                                 printf("Error writing profile string - %s",lpMsgBuf);
192                                 LocalFree( lpMsgBuf );
193                                 exit(0xc000);
194                         }
195                         argc-=1;
196                 }
197                 return 0;
198         }
199         strcpy(fname,argv[2]);
200         if (strcmp(argv[1],"+")==0)
201         {
202                 file=fopen(fname,"rb");
203                 if (file==NULL)
204                         exit(0xc000);
205                 len=filelength(_fileno(file));
206                 ch=(char *)malloc(len+2);
207                 *ch++=0;        /* a small hack to allow matching /r/n if /n is first character*/
208                 len=fread(ch,sizeof(char),len,file);
209                 file=freopen(fname,"wb",file);
210                 while (len-->0)
211                 {
212                         if ((*ch=='\n') && (*(ch-1)!='\r')) /*line feed alone*/
213                         {
214                                 fputc('\r',file);
215                         }
216                         fputc(*ch,file);
217                         ch++;
218                 }
219                 fclose(file);
220                 return 0;
221         }
222         if (strcmp(argv[1],"-")==0)
223         {
224                 strcpy(fname,argv[2]);
225                 file=fopen(fname,"rb");
226                 if (file==NULL)
227                         exit(0xc000);
228                 len=filelength(_fileno(file));
229                 ch=(char *)malloc(len+1);
230                 len=fread(ch,sizeof(char),len,file);
231                 file=freopen(fname,"wb",file);
232                 while (len-->0)
233                 {
234                         if (*ch!='\r')
235                                 fputc(*ch,file);
236                         ch++;
237                 }
238                 fclose(file);
239                 return 0;
240         }
241         if (strstr(fname,".et")==NULL)
242                 strcat(fname,".et");
243         file=fopen(fname,"rb");
244         if (file==NULL)
245                 exit(0xc000);
246         len=filelength(_fileno(file));
247         ch=(char *)malloc(len+1);
248         len=fread(ch,sizeof(char),len,file);
249         file=freopen(fname,"wb",file);
250         while (len-->0)
251         {
252                 if (*ch!='\r')
253                         fputc(*ch,file);
254                 ch++;
255         }
256         fclose(file);
257         pvar=(CHARP *)malloc(argc*sizeof(CHARP));
258         for (i=1;i<argc-1;i++)
259                 pvar[i]=argv[i+1];
260         pvar[argc-1]=NULL;
261         pvar[0]=argv[1];
262         l=_spawnvp(_P_WAIT,argv[1],pvar);
263         if (ch)
264                 free(ch);
265         if (pvar)
266                 free(pvar);
267         return 0;
268 }