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