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 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
42 * back to WriteFile(), assuming the console encoding is still the right
45 len
= WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
,
47 msgA
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(char));
51 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
, msgA
, len
,
53 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), msgA
, len
, &count
, FALSE
);
54 HeapFree(GetProcessHeap(), 0, msgA
);
60 static int reg_message(int msg
)
62 static const WCHAR formatW
[] = {'%','s',0};
63 WCHAR msg_buffer
[8192];
65 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
,
66 sizeof(msg_buffer
)/sizeof(WCHAR
));
67 return reg_printfW(formatW
, msg_buffer
);
70 static HKEY
get_rootkey(LPWSTR key
)
72 static const WCHAR szHKLM
[] = {'H','K','L','M',0};
73 static const WCHAR szHKEY_LOCAL_MACHINE
[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
74 static const WCHAR szHKCU
[] = {'H','K','C','U',0};
75 static const WCHAR szHKEY_CURRENT_USER
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
76 static const WCHAR szHKCR
[] = {'H','K','C','R',0};
77 static const WCHAR szHKEY_CLASSES_ROOT
[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
78 static const WCHAR szHKU
[] = {'H','K','U',0};
79 static const WCHAR szHKEY_USERS
[] = {'H','K','E','Y','_','U','S','E','R','S',0};
80 static const WCHAR szHKCC
[] = {'H','K','C','C',0};
81 static const WCHAR szHKEY_CURRENT_CONFIG
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
83 if (CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,4,szHKLM
,4)==2 ||
84 CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,18,szHKEY_LOCAL_MACHINE
,18)==2)
85 return HKEY_LOCAL_MACHINE
;
86 else if (CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,4,szHKCU
,4)==2 ||
87 CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,17,szHKEY_CURRENT_USER
,17)==2)
88 return HKEY_CURRENT_USER
;
89 else if (CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,4,szHKCR
,4)==2 ||
90 CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,17,szHKEY_CLASSES_ROOT
,17)==2)
91 return HKEY_CLASSES_ROOT
;
92 else if (CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,3,szHKU
,3)==2 ||
93 CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,10,szHKEY_USERS
,10)==2)
95 else if (CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,4,szHKCC
,4)==2 ||
96 CompareStringW(CP_ACP
,NORM_IGNORECASE
,key
,19,szHKEY_CURRENT_CONFIG
,19)==2)
97 return HKEY_CURRENT_CONFIG
;
101 static DWORD
get_regtype(LPWSTR type
)
103 static const WCHAR szREG_SZ
[] = {'R','E','G','_','S','Z',0};
104 static const WCHAR szREG_MULTI_SZ
[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
105 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};
106 static const WCHAR szREG_DWORD
[] = {'R','E','G','_','D','W','O','R','D',0};
107 static const WCHAR szREG_BINARY
[] = {'R','E','G','_','B','I','N','A','R','Y',0};
108 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};
109 static const WCHAR szREG_NONE
[] = {'R','E','G','_','N','O','N','E',0};
110 static const WCHAR szREG_EXPAND_SZ
[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
115 if (lstrcmpiW(type
,szREG_SZ
)==0) return REG_SZ
;
116 if (lstrcmpiW(type
,szREG_DWORD
)==0) return REG_DWORD
;
117 if (lstrcmpiW(type
,szREG_MULTI_SZ
)==0) return REG_MULTI_SZ
;
118 if (lstrcmpiW(type
,szREG_EXPAND_SZ
)==0) return REG_EXPAND_SZ
;
119 if (lstrcmpiW(type
,szREG_DWORD_BIG_ENDIAN
)==0) return REG_DWORD_BIG_ENDIAN
;
120 if (lstrcmpiW(type
,szREG_DWORD_LITTLE_ENDIAN
)==0) return REG_DWORD_LITTLE_ENDIAN
;
121 if (lstrcmpiW(type
,szREG_BINARY
)==0) return REG_BINARY
;
122 if (lstrcmpiW(type
,szREG_NONE
)==0) return REG_NONE
;
127 static LPBYTE
get_regdata(LPWSTR data
, DWORD reg_type
, WCHAR separator
, DWORD
*reg_count
)
129 LPBYTE out_data
= NULL
;
136 *reg_count
= (lstrlenW(data
) + 1) * sizeof(WCHAR
);
137 out_data
= HeapAlloc(GetProcessHeap(),0,*reg_count
);
138 lstrcpyW((LPWSTR
)out_data
,data
);
145 val
= strtolW(data
, &rest
, 0);
147 static const WCHAR nonnumber
[] = {'E','r','r','o','r',':',' ','/','d',' ','r','e','q','u','i','r','e','s',' ','n','u','m','b','e','r','.','\n',0};
148 reg_printfW(nonnumber
);
151 *reg_count
= sizeof(DWORD
);
152 out_data
= HeapAlloc(GetProcessHeap(),0,*reg_count
);
153 ((LPDWORD
)out_data
)[0] = val
;
158 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};
159 reg_printfW(unhandled
, reg_type
,data
);
166 static int reg_add(WCHAR
*key_name
, WCHAR
*value_name
, BOOL value_empty
,
167 WCHAR
*type
, WCHAR separator
, WCHAR
*data
, BOOL force
)
169 static const WCHAR stubW
[] = {'A','D','D',' ','-',' ','%','s',
170 ' ','%','s',' ','%','d',' ','%','s',' ','%','s',' ','%','d','\n',0};
174 reg_printfW(stubW
, key_name
, value_name
, value_empty
, type
, data
, force
);
176 if (key_name
[0]=='\\' && key_name
[1]=='\\')
178 reg_message(STRING_NO_REMOTE
);
182 p
= strchrW(key_name
,'\\');
185 reg_message(STRING_INVALID_KEY
);
190 root
= get_rootkey(key_name
);
193 reg_message(STRING_INVALID_KEY
);
197 if(RegCreateKeyW(root
,p
,&subkey
)!=ERROR_SUCCESS
)
199 reg_message(STRING_INVALID_KEY
);
203 if (value_name
|| data
)
207 BYTE
* reg_data
= NULL
;
211 if (RegQueryValueW(subkey
,value_name
,NULL
,NULL
)==ERROR_SUCCESS
)
213 /* FIXME: Prompt for overwrite */
217 reg_type
= get_regtype(type
);
221 reg_message(STRING_INVALID_CMDLINE
);
226 reg_data
= get_regdata(data
,reg_type
,separator
,®_count
);
228 RegSetValueExW(subkey
,value_name
,0,reg_type
,reg_data
,reg_count
);
229 HeapFree(GetProcessHeap(),0,reg_data
);
233 reg_message(STRING_SUCCESS
);
238 static int reg_delete(WCHAR
*key_name
, WCHAR
*value_name
, BOOL value_empty
,
239 BOOL value_all
, BOOL force
)
244 static const WCHAR stubW
[] = {'D','E','L','E','T','E',
245 ' ','-',' ','%','s',' ','%','s',' ','%','d',' ','%','d',' ','%','d','\n'
247 reg_printfW(stubW
, key_name
, value_name
, value_empty
, value_all
, force
);
249 if (key_name
[0]=='\\' && key_name
[1]=='\\')
251 reg_message(STRING_NO_REMOTE
);
255 p
= strchrW(key_name
,'\\');
258 reg_message(STRING_INVALID_KEY
);
263 root
= get_rootkey(key_name
);
266 reg_message(STRING_INVALID_KEY
);
270 if (value_name
&& value_empty
)
272 reg_message(STRING_INVALID_CMDLINE
);
276 if (value_empty
&& value_all
)
278 reg_message(STRING_INVALID_CMDLINE
);
284 /* FIXME: Prompt for delete */
287 /* Delete subtree only if no /v* option is given */
288 if (!value_name
&& !value_empty
&& !value_all
)
290 if (RegDeleteTreeW(root
,p
)!=ERROR_SUCCESS
)
292 reg_message(STRING_CANNOT_FIND
);
295 reg_message(STRING_SUCCESS
);
299 if(RegOpenKeyW(root
,p
,&subkey
)!=ERROR_SUCCESS
)
301 reg_message(STRING_CANNOT_FIND
);
312 rc
= RegQueryInfoKeyW(subkey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
313 &maxValue
, NULL
, NULL
, NULL
);
314 if (rc
!= ERROR_SUCCESS
)
321 szValue
= HeapAlloc(GetProcessHeap(),0,maxValue
*sizeof(WCHAR
));
326 rc
= RegEnumValueW(subkey
, 0, szValue
, &count
, NULL
, NULL
, NULL
, NULL
);
327 if (rc
== ERROR_SUCCESS
)
329 rc
= RegDeleteValueW(subkey
, szValue
);
330 if (rc
!= ERROR_SUCCESS
)
335 if (rc
!= ERROR_SUCCESS
)
337 /* FIXME delete failed */
342 if (RegDeleteValueW(subkey
,value_name
) != ERROR_SUCCESS
)
345 reg_message(STRING_CANNOT_FIND
);
349 else if (value_empty
)
351 RegSetValueExW(subkey
,NULL
,0,REG_SZ
,NULL
,0);
355 reg_message(STRING_SUCCESS
);
359 static int reg_query(WCHAR
*key_name
, WCHAR
*value_name
, BOOL value_empty
,
362 static const WCHAR stubW
[] = {'S','T','U','B',' ','Q','U','E','R','Y',' ',
363 '-',' ','%','s',' ','%','s',' ','%','d',' ','%','d','\n',0};
364 reg_printfW(stubW
, key_name
, value_name
, value_empty
, subkey
);
369 int wmain(int argc
, WCHAR
*argvW
[])
373 static const WCHAR addW
[] = {'a','d','d',0};
374 static const WCHAR deleteW
[] = {'d','e','l','e','t','e',0};
375 static const WCHAR queryW
[] = {'q','u','e','r','y',0};
376 static const WCHAR slashDW
[] = {'/','d',0};
377 static const WCHAR slashFW
[] = {'/','f',0};
378 static const WCHAR slashHW
[] = {'/','h',0};
379 static const WCHAR slashSW
[] = {'/','s',0};
380 static const WCHAR slashTW
[] = {'/','t',0};
381 static const WCHAR slashVW
[] = {'/','v',0};
382 static const WCHAR slashVAW
[] = {'/','v','a',0};
383 static const WCHAR slashVEW
[] = {'/','v','e',0};
384 static const WCHAR slashHelpW
[] = {'/','?',0};
386 if (argc
< 2 || !lstrcmpW(argvW
[1], slashHelpW
)
387 || !lstrcmpiW(argvW
[1], slashHW
))
389 reg_message(STRING_USAGE
);
393 if (!lstrcmpiW(argvW
[1], addW
))
395 WCHAR
*key_name
, *value_name
= NULL
, *type
= NULL
, separator
= '\0', *data
= NULL
;
396 BOOL value_empty
= FALSE
, force
= FALSE
;
400 reg_message(STRING_INVALID_CMDLINE
);
403 else if (argc
== 3 && (!lstrcmpW(argvW
[2], slashHelpW
) ||
404 !lstrcmpiW(argvW
[2], slashHW
)))
406 reg_message(STRING_ADD_USAGE
);
411 for (i
= 1; i
< argc
; i
++)
413 if (!lstrcmpiW(argvW
[i
], slashVW
))
414 value_name
= argvW
[++i
];
415 else if (!lstrcmpiW(argvW
[i
], slashVEW
))
417 else if (!lstrcmpiW(argvW
[i
], slashTW
))
419 else if (!lstrcmpiW(argvW
[i
], slashSW
))
420 separator
= argvW
[++i
][0];
421 else if (!lstrcmpiW(argvW
[i
], slashDW
))
423 else if (!lstrcmpiW(argvW
[i
], slashFW
))
426 return reg_add(key_name
, value_name
, value_empty
, type
, separator
,
429 else if (!lstrcmpiW(argvW
[1], deleteW
))
431 WCHAR
*key_name
, *value_name
= NULL
;
432 BOOL value_empty
= FALSE
, value_all
= FALSE
, force
= FALSE
;
436 reg_message(STRING_INVALID_CMDLINE
);
439 else if (argc
== 3 && (!lstrcmpW(argvW
[2], slashHelpW
) ||
440 !lstrcmpiW(argvW
[2], slashHW
)))
442 reg_message(STRING_DELETE_USAGE
);
447 for (i
= 1; i
< argc
; i
++)
449 if (!lstrcmpiW(argvW
[i
], slashVW
))
450 value_name
= argvW
[++i
];
451 else if (!lstrcmpiW(argvW
[i
], slashVEW
))
453 else if (!lstrcmpiW(argvW
[i
], slashVAW
))
455 else if (!lstrcmpiW(argvW
[i
], slashFW
))
458 return reg_delete(key_name
, value_name
, value_empty
, value_all
, force
);
460 else if (!lstrcmpiW(argvW
[1], queryW
))
462 WCHAR
*key_name
, *value_name
= NULL
;
463 BOOL value_empty
= FALSE
, subkey
= FALSE
;
467 reg_message(STRING_INVALID_CMDLINE
);
470 else if (argc
== 3 && (!lstrcmpW(argvW
[2], slashHelpW
) ||
471 !lstrcmpiW(argvW
[2], slashHW
)))
473 reg_message(STRING_QUERY_USAGE
);
478 for (i
= 1; i
< argc
; i
++)
480 if (!lstrcmpiW(argvW
[i
], slashVW
))
481 value_name
= argvW
[++i
];
482 else if (!lstrcmpiW(argvW
[i
], slashVEW
))
484 else if (!lstrcmpiW(argvW
[i
], slashSW
))
487 return reg_query(key_name
, value_name
, value_empty
, subkey
);
491 reg_message(STRING_INVALID_CMDLINE
);