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 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
25 static const WCHAR short_hklm
[] = {'H','K','L','M',0};
26 static const WCHAR short_hkcu
[] = {'H','K','C','U',0};
27 static const WCHAR short_hkcr
[] = {'H','K','C','R',0};
28 static const WCHAR short_hku
[] = {'H','K','U',0};
29 static const WCHAR short_hkcc
[] = {'H','K','C','C',0};
30 static const WCHAR long_hklm
[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
31 static const WCHAR long_hkcu
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
32 static const WCHAR long_hkcr
[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
33 static const WCHAR long_hku
[] = {'H','K','E','Y','_','U','S','E','R','S',0};
34 static const WCHAR long_hkcc
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
39 const WCHAR
*short_name
;
40 const WCHAR
*long_name
;
44 {HKEY_LOCAL_MACHINE
, short_hklm
, long_hklm
},
45 {HKEY_CURRENT_USER
, short_hkcu
, long_hkcu
},
46 {HKEY_CLASSES_ROOT
, short_hkcr
, long_hkcr
},
47 {HKEY_USERS
, short_hku
, long_hku
},
48 {HKEY_CURRENT_CONFIG
, short_hkcc
, long_hkcc
},
51 static const WCHAR type_none
[] = {'R','E','G','_','N','O','N','E',0};
52 static const WCHAR type_sz
[] = {'R','E','G','_','S','Z',0};
53 static const WCHAR type_expand_sz
[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0};
54 static const WCHAR type_binary
[] = {'R','E','G','_','B','I','N','A','R','Y',0};
55 static const WCHAR type_dword
[] = {'R','E','G','_','D','W','O','R','D',0};
56 static const WCHAR type_dword_le
[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0};
57 static const WCHAR type_dword_be
[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0};
58 static const WCHAR type_multi_sz
[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0};
67 {REG_NONE
, type_none
},
69 {REG_EXPAND_SZ
, type_expand_sz
},
70 {REG_BINARY
, type_binary
},
71 {REG_DWORD
, type_dword
},
72 {REG_DWORD_LITTLE_ENDIAN
, type_dword_le
},
73 {REG_DWORD_BIG_ENDIAN
, type_dword_be
},
74 {REG_MULTI_SZ
, type_multi_sz
},
77 static int reg_printfW(const WCHAR
*msg
, ...)
82 WCHAR msg_buffer
[8192];
84 va_start(va_args
, msg
);
85 vsnprintfW(msg_buffer
, 8192, msg
, va_args
);
88 wlen
= lstrlenW(msg_buffer
);
89 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), msg_buffer
, wlen
, &count
, NULL
);
95 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
96 * back to WriteFile(), assuming the console encoding is still the right
99 len
= WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
,
100 NULL
, 0, NULL
, NULL
);
101 msgA
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(char));
105 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
, msgA
, len
,
107 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), msgA
, len
, &count
, FALSE
);
108 HeapFree(GetProcessHeap(), 0, msgA
);
114 static int reg_message(int msg
)
116 static const WCHAR formatW
[] = {'%','s',0};
117 WCHAR msg_buffer
[8192];
119 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
,
120 sizeof(msg_buffer
)/sizeof(WCHAR
));
121 return reg_printfW(formatW
, msg_buffer
);
124 static inline BOOL
path_rootname_cmp(const WCHAR
*input_path
, const WCHAR
*rootkey_name
)
126 DWORD length
= strlenW(rootkey_name
);
128 return (!strncmpiW(input_path
, rootkey_name
, length
) &&
129 (input_path
[length
] == 0 || input_path
[length
] == '\\'));
132 static HKEY
path_get_rootkey(const WCHAR
*path
)
136 for (i
= 0; i
< ARRAY_SIZE(root_rels
); i
++)
138 if (path_rootname_cmp(path
, root_rels
[i
].short_name
) ||
139 path_rootname_cmp(path
, root_rels
[i
].long_name
))
140 return root_rels
[i
].key
;
146 static DWORD
wchar_get_type(const WCHAR
*type_name
)
153 for (i
= 0; i
< ARRAY_SIZE(type_rels
); i
++)
155 if (!strcmpiW(type_rels
[i
].name
, type_name
))
156 return type_rels
[i
].type
;
162 /* hexchar_to_byte from programs/regedit/hexedit.c */
163 static inline BYTE
hexchar_to_byte(WCHAR ch
)
165 if (ch
>= '0' && ch
<= '9')
167 else if (ch
>= 'a' && ch
<= 'f')
168 return ch
- 'a' + 10;
169 else if (ch
>= 'A' && ch
<= 'F')
170 return ch
- 'A' + 10;
175 static LPBYTE
get_regdata(LPWSTR data
, DWORD reg_type
, WCHAR separator
, DWORD
*reg_count
)
177 LPBYTE out_data
= NULL
;
184 *reg_count
= (lstrlenW(data
) + 1) * sizeof(WCHAR
);
185 out_data
= HeapAlloc(GetProcessHeap(),0,*reg_count
);
186 lstrcpyW((LPWSTR
)out_data
,data
);
193 val
= strtolW(data
, &rest
, 0);
195 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};
196 reg_printfW(nonnumber
);
199 *reg_count
= sizeof(DWORD
);
200 out_data
= HeapAlloc(GetProcessHeap(),0,*reg_count
);
201 ((LPDWORD
)out_data
)[0] = val
;
206 static const WCHAR nohex
[] = {'E','r','r','o','r',':',' ','/','d',' ','r','e','q','u','i','r','e','s',' ','h','e','x',' ','d','a','t','a','.','\n',0};
208 int i
= 0, destByteIndex
= 0, datalen
= lstrlenW(data
);
209 *reg_count
= ((datalen
+ datalen
% 2) / 2) * sizeof(BYTE
);
210 out_data
= HeapAlloc(GetProcessHeap(), 0, *reg_count
);
213 hex1
= hexchar_to_byte(data
[i
++]);
216 out_data
[destByteIndex
++] = hex1
;
218 for(;i
+ 1 < datalen
;i
+= 2)
220 hex0
= hexchar_to_byte(data
[i
]);
221 hex1
= hexchar_to_byte(data
[i
+ 1]);
222 if(hex0
== 0xFF || hex1
== 0xFF)
224 out_data
[destByteIndex
++] = (hex0
<< 4) | hex1
;
228 /* cleanup, print error */
229 HeapFree(GetProcessHeap(), 0, out_data
);
236 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};
237 reg_printfW(unhandled
, reg_type
,data
);
244 static BOOL
sane_path(const WCHAR
*key
)
246 unsigned int i
= strlenW(key
);
248 if (i
< 3 || (key
[i
- 1] == '\\' && key
[i
- 2] == '\\'))
250 reg_message(STRING_INVALID_KEY
);
254 if (key
[0] == '\\' && key
[1] == '\\' && key
[2] != '\\')
256 reg_message(STRING_NO_REMOTE
);
263 static int reg_add(WCHAR
*key_name
, WCHAR
*value_name
, BOOL value_empty
,
264 WCHAR
*type
, WCHAR separator
, WCHAR
*data
, BOOL force
)
266 static const WCHAR stubW
[] = {'A','D','D',' ','-',' ','%','s',
267 ' ','%','s',' ','%','d',' ','%','s',' ','%','s',' ','%','d','\n',0};
271 reg_printfW(stubW
, key_name
, value_name
, value_empty
, type
, data
, force
);
273 if (!sane_path(key_name
))
276 p
= strchrW(key_name
,'\\');
279 reg_message(STRING_INVALID_KEY
);
284 root
= path_get_rootkey(key_name
);
287 reg_message(STRING_INVALID_KEY
);
291 if(RegCreateKeyW(root
,p
,&subkey
)!=ERROR_SUCCESS
)
293 reg_message(STRING_INVALID_KEY
);
297 if (value_name
|| data
)
301 BYTE
* reg_data
= NULL
;
305 if (RegQueryValueW(subkey
,value_name
,NULL
,NULL
)==ERROR_SUCCESS
)
307 /* FIXME: Prompt for overwrite */
311 reg_type
= wchar_get_type(type
);
315 reg_message(STRING_UNSUPPORTED_TYPE
);
320 reg_data
= get_regdata(data
,reg_type
,separator
,®_count
);
322 RegSetValueExW(subkey
,value_name
,0,reg_type
,reg_data
,reg_count
);
323 HeapFree(GetProcessHeap(),0,reg_data
);
327 reg_message(STRING_SUCCESS
);
332 static int reg_delete(WCHAR
*key_name
, WCHAR
*value_name
, BOOL value_empty
,
333 BOOL value_all
, BOOL force
)
338 static const WCHAR stubW
[] = {'D','E','L','E','T','E',
339 ' ','-',' ','%','s',' ','%','s',' ','%','d',' ','%','d',' ','%','d','\n'
341 reg_printfW(stubW
, key_name
, value_name
, value_empty
, value_all
, force
);
343 if (!sane_path(key_name
))
346 p
= strchrW(key_name
,'\\');
349 reg_message(STRING_INVALID_KEY
);
354 root
= path_get_rootkey(key_name
);
357 reg_message(STRING_INVALID_KEY
);
361 if (value_name
&& value_empty
)
363 reg_message(STRING_INVALID_CMDLINE
);
367 if (value_empty
&& value_all
)
369 reg_message(STRING_INVALID_CMDLINE
);
375 /* FIXME: Prompt for delete */
378 /* Delete subtree only if no /v* option is given */
379 if (!value_name
&& !value_empty
&& !value_all
)
381 if (RegDeleteTreeW(root
,p
)!=ERROR_SUCCESS
)
383 reg_message(STRING_CANNOT_FIND
);
386 reg_message(STRING_SUCCESS
);
390 if(RegOpenKeyW(root
,p
,&subkey
)!=ERROR_SUCCESS
)
392 reg_message(STRING_CANNOT_FIND
);
403 rc
= RegQueryInfoKeyW(subkey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
404 &maxValue
, NULL
, NULL
, NULL
);
405 if (rc
!= ERROR_SUCCESS
)
412 szValue
= HeapAlloc(GetProcessHeap(),0,maxValue
*sizeof(WCHAR
));
417 rc
= RegEnumValueW(subkey
, 0, szValue
, &count
, NULL
, NULL
, NULL
, NULL
);
418 if (rc
== ERROR_SUCCESS
)
420 rc
= RegDeleteValueW(subkey
, szValue
);
421 if (rc
!= ERROR_SUCCESS
)
426 if (rc
!= ERROR_SUCCESS
)
428 /* FIXME delete failed */
433 if (RegDeleteValueW(subkey
,value_name
) != ERROR_SUCCESS
)
436 reg_message(STRING_CANNOT_FIND
);
440 else if (value_empty
)
442 RegSetValueExW(subkey
,NULL
,0,REG_SZ
,NULL
,0);
446 reg_message(STRING_SUCCESS
);
450 static int reg_query(WCHAR
*key_name
, WCHAR
*value_name
, BOOL value_empty
,
453 static const WCHAR stubW
[] = {'S','T','U','B',' ','Q','U','E','R','Y',' ',
454 '-',' ','%','s',' ','%','s',' ','%','d',' ','%','d','\n',0};
455 reg_printfW(stubW
, key_name
, value_name
, value_empty
, subkey
);
460 int wmain(int argc
, WCHAR
*argvW
[])
464 static const WCHAR addW
[] = {'a','d','d',0};
465 static const WCHAR deleteW
[] = {'d','e','l','e','t','e',0};
466 static const WCHAR queryW
[] = {'q','u','e','r','y',0};
467 static const WCHAR slashDW
[] = {'/','d',0};
468 static const WCHAR slashFW
[] = {'/','f',0};
469 static const WCHAR slashHW
[] = {'/','h',0};
470 static const WCHAR slashSW
[] = {'/','s',0};
471 static const WCHAR slashTW
[] = {'/','t',0};
472 static const WCHAR slashVW
[] = {'/','v',0};
473 static const WCHAR slashVAW
[] = {'/','v','a',0};
474 static const WCHAR slashVEW
[] = {'/','v','e',0};
475 static const WCHAR slashHelpW
[] = {'/','?',0};
477 if (argc
< 2 || !lstrcmpW(argvW
[1], slashHelpW
)
478 || !lstrcmpiW(argvW
[1], slashHW
))
480 reg_message(STRING_USAGE
);
484 if (!lstrcmpiW(argvW
[1], addW
))
486 WCHAR
*key_name
, *value_name
= NULL
, *type
= NULL
, separator
= '\0', *data
= NULL
;
487 BOOL value_empty
= FALSE
, force
= FALSE
;
491 reg_message(STRING_INVALID_CMDLINE
);
494 else if (argc
== 3 && (!lstrcmpW(argvW
[2], slashHelpW
) ||
495 !lstrcmpiW(argvW
[2], slashHW
)))
497 reg_message(STRING_ADD_USAGE
);
502 for (i
= 1; i
< argc
; i
++)
504 if (!lstrcmpiW(argvW
[i
], slashVW
))
505 value_name
= argvW
[++i
];
506 else if (!lstrcmpiW(argvW
[i
], slashVEW
))
508 else if (!lstrcmpiW(argvW
[i
], slashTW
))
510 else if (!lstrcmpiW(argvW
[i
], slashSW
))
511 separator
= argvW
[++i
][0];
512 else if (!lstrcmpiW(argvW
[i
], slashDW
))
514 else if (!lstrcmpiW(argvW
[i
], slashFW
))
517 return reg_add(key_name
, value_name
, value_empty
, type
, separator
,
520 else if (!lstrcmpiW(argvW
[1], deleteW
))
522 WCHAR
*key_name
, *value_name
= NULL
;
523 BOOL value_empty
= FALSE
, value_all
= FALSE
, force
= FALSE
;
527 reg_message(STRING_INVALID_CMDLINE
);
530 else if (argc
== 3 && (!lstrcmpW(argvW
[2], slashHelpW
) ||
531 !lstrcmpiW(argvW
[2], slashHW
)))
533 reg_message(STRING_DELETE_USAGE
);
538 for (i
= 1; i
< argc
; i
++)
540 if (!lstrcmpiW(argvW
[i
], slashVW
))
541 value_name
= argvW
[++i
];
542 else if (!lstrcmpiW(argvW
[i
], slashVEW
))
544 else if (!lstrcmpiW(argvW
[i
], slashVAW
))
546 else if (!lstrcmpiW(argvW
[i
], slashFW
))
549 return reg_delete(key_name
, value_name
, value_empty
, value_all
, force
);
551 else if (!lstrcmpiW(argvW
[1], queryW
))
553 WCHAR
*key_name
, *value_name
= NULL
;
554 BOOL value_empty
= FALSE
, subkey
= FALSE
;
558 reg_message(STRING_INVALID_CMDLINE
);
561 else if (argc
== 3 && (!lstrcmpW(argvW
[2], slashHelpW
) ||
562 !lstrcmpiW(argvW
[2], slashHW
)))
564 reg_message(STRING_QUERY_USAGE
);
569 for (i
= 1; i
< argc
; i
++)
571 if (!lstrcmpiW(argvW
[i
], slashVW
))
572 value_name
= argvW
[++i
];
573 else if (!lstrcmpiW(argvW
[i
], slashVEW
))
575 else if (!lstrcmpiW(argvW
[i
], slashSW
))
578 return reg_query(key_name
, value_name
, value_empty
, subkey
);
582 reg_message(STRING_INVALID_CMDLINE
);