2 * Copyright 2008 Andrew Riedi
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <wine/unicode.h>
23 static int reg_printfW(const WCHAR
*msg
, ...)
28 WCHAR msg_buffer
[8192];
30 va_start(va_args
, msg
);
31 vsprintfW(msg_buffer
, msg
, va_args
);
34 wlen
= lstrlenW(msg_buffer
);
35 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), msg_buffer
, wlen
, &count
, NULL
);
41 len
= WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
,
43 msgA
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(char));
47 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
, msgA
, len
,
49 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), msgA
, len
, &count
, FALSE
);
50 HeapFree(GetProcessHeap(), 0, msgA
);
56 static int reg_message(int msg
)
58 static const WCHAR formatW
[] = {'%','s',0};
59 WCHAR msg_buffer
[8192];
61 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
,
62 sizeof(msg_buffer
)/sizeof(WCHAR
));
63 return reg_printfW(formatW
, msg_buffer
);
66 static HKEY
get_rootkey(LPWSTR key
)
68 static const WCHAR szHKLM
[] = {'H','K','L','M',0};
69 static const WCHAR szHKCU
[] = {'H','K','C','U',0};
70 static const WCHAR szHKCR
[] = {'H','K','C','R',0};
71 static const WCHAR szHKU
[] = {'H','K','U',0};
72 static const WCHAR szHKCC
[] = {'H','K','C','C',0};
74 if (CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,4,szHKLM
,4)==2)
75 return HKEY_LOCAL_MACHINE
;
76 else if (CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,4,szHKCU
,4)==2)
77 return HKEY_CURRENT_USER
;
78 else if (CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,4,szHKCR
,4)==2)
79 return HKEY_CLASSES_ROOT
;
80 else if (CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,3,szHKU
,3)==2)
82 else if (CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,4,szHKCC
,4)==2)
83 return HKEY_CURRENT_CONFIG
;
87 static DWORD
get_regtype(LPWSTR type
)
89 static const WCHAR szREG_SZ
[] = {'R','E','G','_','S','Z',0};
90 static const WCHAR szREG_MULTI_SZ
[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
91 static const WCHAR szREG_DWORD_BIG_ENDIAN
[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
92 static const WCHAR szREG_DWORD
[] = {'R','E','G','_','D','W','O','R','D',0};
93 static const WCHAR szREG_BINARY
[] = {'R','E','G','_','B','I','N','A','R','Y',0};
94 static const WCHAR szREG_DWORD_LITTLE_ENDIAN
[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0};
95 static const WCHAR szREG_NONE
[] = {'R','E','G','_','N','O','N','E',0};
96 static const WCHAR szREG_EXPAND_SZ
[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
101 if (lstrcmpiW(type
,szREG_SZ
)==0) return REG_SZ
;
102 if (lstrcmpiW(type
,szREG_DWORD
)==0) return REG_DWORD
;
103 if (lstrcmpiW(type
,szREG_MULTI_SZ
)==0) return REG_MULTI_SZ
;
104 if (lstrcmpiW(type
,szREG_EXPAND_SZ
)==0) return REG_EXPAND_SZ
;
105 if (lstrcmpiW(type
,szREG_DWORD_BIG_ENDIAN
)==0) return REG_DWORD_BIG_ENDIAN
;
106 if (lstrcmpiW(type
,szREG_DWORD_LITTLE_ENDIAN
)==0) return REG_DWORD_LITTLE_ENDIAN
;
107 if (lstrcmpiW(type
,szREG_BINARY
)==0) return REG_BINARY
;
108 if (lstrcmpiW(type
,szREG_NONE
)==0) return REG_NONE
;
113 static LPBYTE
get_regdata(LPWSTR data
, DWORD reg_type
, WCHAR separator
, DWORD
*reg_count
)
115 LPBYTE out_data
= NULL
;
122 *reg_count
= (lstrlenW(data
) + 1) * sizeof(WCHAR
);
123 out_data
= HeapAlloc(GetProcessHeap(),0,*reg_count
);
124 lstrcpyW((LPWSTR
)out_data
,data
);
129 static const WCHAR unhandled
[] = {'U','n','h','a','n','d','l','e','d',' ','T','y','p','e',' ','0','x','%','x',' ',' ','d','a','t','a',' ','%','s','\n',0};
130 reg_printfW(unhandled
, reg_type
,data
);
137 static int reg_add(WCHAR
*key_name
, WCHAR
*value_name
, BOOL value_empty
,
138 WCHAR
*type
, WCHAR separator
, WCHAR
*data
, BOOL force
)
140 static const WCHAR stubW
[] = {'A','D','D',' ','-',' ','%','s',
141 ' ','%','s',' ','%','d',' ','%','s',' ','%','s',' ','%','d','\n',0};
145 reg_printfW(stubW
, key_name
, value_name
, value_empty
, type
, data
, force
);
147 if (key_name
[0]=='\\' && key_name
[1]=='\\')
149 reg_message(STRING_NO_REMOTE
);
153 p
= strchrW(key_name
,'\\');
156 reg_message(STRING_INVALID_KEY
);
161 root
= get_rootkey(key_name
);
164 reg_message(STRING_INVALID_KEY
);
168 if(RegCreateKeyW(root
,p
,&subkey
)!=ERROR_SUCCESS
)
170 reg_message(STRING_INVALID_KEY
);
174 if (value_name
|| data
)
178 BYTE
* reg_data
= NULL
;
182 if (RegQueryValueW(subkey
,value_name
,NULL
,NULL
)==ERROR_SUCCESS
)
184 /* FIXME: Prompt for overwrite */
188 reg_type
= get_regtype(type
);
192 reg_message(STRING_INVALID_CMDLINE
);
197 reg_data
= get_regdata(data
,reg_type
,separator
,®_count
);
199 RegSetValueExW(subkey
,value_name
,0,reg_type
,reg_data
,reg_count
);
200 HeapFree(GetProcessHeap(),0,reg_data
);
204 reg_message(STRING_SUCCESS
);
209 static int reg_delete(WCHAR
*key_name
, WCHAR
*value_name
, BOOL value_empty
,
210 BOOL value_all
, BOOL force
)
215 static const WCHAR stubW
[] = {'D','E','L','E','T','E',
216 ' ','-',' ','%','s',' ','%','s',' ','%','d',' ','%','d',' ','%','d','\n'
218 reg_printfW(stubW
, key_name
, value_name
, value_empty
, value_all
, force
);
220 if (key_name
[0]=='\\' && key_name
[1]=='\\')
222 reg_message(STRING_NO_REMOTE
);
226 p
= strchrW(key_name
,'\\');
229 reg_message(STRING_INVALID_KEY
);
234 root
= get_rootkey(key_name
);
237 reg_message(STRING_INVALID_KEY
);
241 if (value_name
&& value_empty
)
243 reg_message(STRING_INVALID_CMDLINE
);
247 if (value_empty
&& value_all
)
249 reg_message(STRING_INVALID_CMDLINE
);
255 /* FIXME: Prompt for delete */
258 /* Delete subtree only if no /v* option is given */
259 if (!value_name
&& !value_empty
&& !value_all
)
261 if (RegDeleteTreeW(root
,p
)!=ERROR_SUCCESS
)
263 reg_message(STRING_CANNOT_FIND
);
266 reg_message(STRING_SUCCESS
);
270 if(RegOpenKeyW(root
,p
,&subkey
)!=ERROR_SUCCESS
)
272 reg_message(STRING_CANNOT_FIND
);
283 rc
= RegQueryInfoKeyW(subkey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
284 &maxValue
, NULL
, NULL
, NULL
);
285 if (rc
!= ERROR_SUCCESS
)
292 szValue
= HeapAlloc(GetProcessHeap(),0,maxValue
*sizeof(WCHAR
));
297 rc
= RegEnumValueW(subkey
, 0, szValue
, &count
, NULL
, NULL
, NULL
, NULL
);
298 if (rc
== ERROR_SUCCESS
)
300 rc
= RegDeleteValueW(subkey
, szValue
);
301 if (rc
!= ERROR_SUCCESS
)
306 if (rc
!= ERROR_SUCCESS
)
308 /* FIXME delete failed */
313 if (RegDeleteValueW(subkey
,value_name
) != ERROR_SUCCESS
)
316 reg_message(STRING_CANNOT_FIND
);
320 else if (value_empty
)
322 RegSetValueExW(subkey
,NULL
,0,REG_SZ
,NULL
,0);
326 reg_message(STRING_SUCCESS
);
330 static int reg_query(WCHAR
*key_name
, WCHAR
*value_name
, BOOL value_empty
,
333 static const WCHAR stubW
[] = {'S','T','U','B',' ','Q','U','E','R','Y',' ',
334 '-',' ','%','s',' ','%','s',' ','%','d',' ','%','d','\n',0};
335 reg_printfW(stubW
, key_name
, value_name
, value_empty
, subkey
);
340 int wmain(int argc
, WCHAR
*argvW
[])
344 static const WCHAR addW
[] = {'a','d','d',0};
345 static const WCHAR deleteW
[] = {'d','e','l','e','t','e',0};
346 static const WCHAR queryW
[] = {'q','u','e','r','y',0};
347 static const WCHAR slashDW
[] = {'/','d',0};
348 static const WCHAR slashFW
[] = {'/','f',0};
349 static const WCHAR slashHW
[] = {'/','h',0};
350 static const WCHAR slashSW
[] = {'/','s',0};
351 static const WCHAR slashTW
[] = {'/','t',0};
352 static const WCHAR slashVW
[] = {'/','v',0};
353 static const WCHAR slashVAW
[] = {'/','v','a',0};
354 static const WCHAR slashVEW
[] = {'/','v','e',0};
355 static const WCHAR slashHelpW
[] = {'/','?',0};
357 if (argc
< 2 || !lstrcmpW(argvW
[1], slashHelpW
)
358 || !lstrcmpiW(argvW
[1], slashHW
))
360 reg_message(STRING_USAGE
);
364 if (!lstrcmpiW(argvW
[1], addW
))
366 WCHAR
*key_name
, *value_name
= NULL
, *type
= NULL
, separator
= '\0', *data
= NULL
;
367 BOOL value_empty
= FALSE
, force
= FALSE
;
371 reg_message(STRING_INVALID_CMDLINE
);
374 else if (argc
== 3 && (!lstrcmpW(argvW
[2], slashHelpW
) ||
375 !lstrcmpiW(argvW
[2], slashHW
)))
377 reg_message(STRING_ADD_USAGE
);
382 for (i
= 1; i
< argc
; i
++)
384 if (!lstrcmpiW(argvW
[i
], slashVW
))
385 value_name
= argvW
[++i
];
386 else if (!lstrcmpiW(argvW
[i
], slashVEW
))
388 else if (!lstrcmpiW(argvW
[i
], slashTW
))
390 else if (!lstrcmpiW(argvW
[i
], slashSW
))
391 separator
= argvW
[++i
][0];
392 else if (!lstrcmpiW(argvW
[i
], slashDW
))
394 else if (!lstrcmpiW(argvW
[i
], slashFW
))
397 return reg_add(key_name
, value_name
, value_empty
, type
, separator
,
400 else if (!lstrcmpiW(argvW
[1], deleteW
))
402 WCHAR
*key_name
, *value_name
= NULL
;
403 BOOL value_empty
= FALSE
, value_all
= FALSE
, force
= FALSE
;
407 reg_message(STRING_INVALID_CMDLINE
);
410 else if (argc
== 3 && (!lstrcmpW(argvW
[2], slashHelpW
) ||
411 !lstrcmpiW(argvW
[2], slashHW
)))
413 reg_message(STRING_DELETE_USAGE
);
418 for (i
= 1; i
< argc
; i
++)
420 if (!lstrcmpiW(argvW
[i
], slashVW
))
421 value_name
= argvW
[++i
];
422 else if (!lstrcmpiW(argvW
[i
], slashVEW
))
424 else if (!lstrcmpiW(argvW
[i
], slashVAW
))
426 else if (!lstrcmpiW(argvW
[i
], slashFW
))
429 return reg_delete(key_name
, value_name
, value_empty
, value_all
, force
);
431 else if (!lstrcmpiW(argvW
[1], queryW
))
433 WCHAR
*key_name
, *value_name
= NULL
;
434 BOOL value_empty
= FALSE
, subkey
= FALSE
;
438 reg_message(STRING_INVALID_CMDLINE
);
441 else if (argc
== 3 && (!lstrcmpW(argvW
[2], slashHelpW
) ||
442 !lstrcmpiW(argvW
[2], slashHW
)))
444 reg_message(STRING_QUERY_USAGE
);
449 for (i
= 1; i
< argc
; i
++)
451 if (!lstrcmpiW(argvW
[i
], slashVW
))
452 value_name
= argvW
[++i
];
453 else if (!lstrcmpiW(argvW
[i
], slashVEW
))
455 else if (!lstrcmpiW(argvW
[i
], slashSW
))
458 return reg_query(key_name
, value_name
, value_empty
, subkey
);
462 reg_message(STRING_INVALID_CMDLINE
);