reg.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright 2008 Andrew Riedi
  3. * Copyright 2016-2017, 2021 Hugh McMaster
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  18. */
  19. #include "reg.h"
  20. #include <wine/debug.h>
  21. WINE_DEFAULT_DEBUG_CHANNEL(reg);
  22. static const struct
  23. {
  24. HKEY key;
  25. const WCHAR *short_name;
  26. const WCHAR *long_name;
  27. }
  28. root_rels[] =
  29. {
  30. {HKEY_LOCAL_MACHINE, L"HKLM", L"HKEY_LOCAL_MACHINE"},
  31. {HKEY_CURRENT_USER, L"HKCU", L"HKEY_CURRENT_USER"},
  32. {HKEY_CLASSES_ROOT, L"HKCR", L"HKEY_CLASSES_ROOT"},
  33. {HKEY_USERS, L"HKU", L"HKEY_USERS"},
  34. {HKEY_CURRENT_CONFIG, L"HKCC", L"HKEY_CURRENT_CONFIG"},
  35. };
  36. const struct reg_type_rels type_rels[] =
  37. {
  38. {REG_NONE, L"REG_NONE"},
  39. {REG_SZ, L"REG_SZ"},
  40. {REG_EXPAND_SZ, L"REG_EXPAND_SZ"},
  41. {REG_BINARY, L"REG_BINARY"},
  42. {REG_DWORD, L"REG_DWORD"},
  43. {REG_DWORD_LITTLE_ENDIAN, L"REG_DWORD_LITTLE_ENDIAN"},
  44. {REG_DWORD_BIG_ENDIAN, L"REG_DWORD_BIG_ENDIAN"},
  45. {REG_MULTI_SZ, L"REG_MULTI_SZ"},
  46. };
  47. void output_writeconsole(const WCHAR *str, DWORD wlen)
  48. {
  49. DWORD count, ret;
  50. ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL);
  51. if (!ret)
  52. {
  53. DWORD len;
  54. char *msgA;
  55. /* On Windows WriteConsoleW() fails if the output is redirected. So fall
  56. * back to WriteFile(), assuming the console encoding is still the right
  57. * one in that case.
  58. */
  59. len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL);
  60. msgA = malloc(len);
  61. WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL);
  62. WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
  63. free(msgA);
  64. }
  65. }
  66. static void output_formatstring(const WCHAR *fmt, va_list va_args)
  67. {
  68. WCHAR *str;
  69. DWORD len;
  70. len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
  71. fmt, 0, 0, (WCHAR *)&str, 0, &va_args);
  72. if (len == 0 && GetLastError() != ERROR_NO_WORK_DONE)
  73. {
  74. WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
  75. return;
  76. }
  77. output_writeconsole(str, len);
  78. LocalFree(str);
  79. }
  80. void WINAPIV output_message(unsigned int id, ...)
  81. {
  82. WCHAR *fmt = NULL;
  83. int len;
  84. va_list va_args;
  85. if (!(len = LoadStringW(GetModuleHandleW(NULL), id, (WCHAR *)&fmt, 0)))
  86. {
  87. WINE_FIXME("LoadString failed with %d\n", GetLastError());
  88. return;
  89. }
  90. len++;
  91. fmt = malloc(len * sizeof(WCHAR));
  92. if (!fmt) return;
  93. LoadStringW(GetModuleHandleW(NULL), id, fmt, len);
  94. va_start(va_args, id);
  95. output_formatstring(fmt, va_args);
  96. va_end(va_args);
  97. free(fmt);
  98. }
  99. void WINAPIV output_string(const WCHAR *fmt, ...)
  100. {
  101. va_list va_args;
  102. va_start(va_args, fmt);
  103. output_formatstring(fmt, va_args);
  104. va_end(va_args);
  105. }
  106. /* ask_confirm() adapted from programs/cmd/builtins.c */
  107. BOOL ask_confirm(unsigned int msgid, WCHAR *reg_info)
  108. {
  109. HMODULE hmod;
  110. WCHAR Ybuffer[4];
  111. WCHAR Nbuffer[4];
  112. WCHAR defval[32];
  113. WCHAR answer[MAX_PATH];
  114. WCHAR *str;
  115. DWORD count;
  116. hmod = GetModuleHandleW(NULL);
  117. LoadStringW(hmod, STRING_YES, Ybuffer, ARRAY_SIZE(Ybuffer));
  118. LoadStringW(hmod, STRING_NO, Nbuffer, ARRAY_SIZE(Nbuffer));
  119. LoadStringW(hmod, STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));
  120. str = (reg_info && *reg_info) ? reg_info : defval;
  121. while (1)
  122. {
  123. output_message(msgid, str);
  124. output_message(STRING_YESNO);
  125. ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), answer, ARRAY_SIZE(answer), &count, NULL);
  126. answer[0] = towupper(answer[0]);
  127. if (answer[0] == Ybuffer[0])
  128. return TRUE;
  129. if (answer[0] == Nbuffer[0])
  130. return FALSE;
  131. }
  132. }
  133. static inline BOOL path_rootname_cmp(const WCHAR *input_path, const WCHAR *rootkey_name)
  134. {
  135. DWORD length = lstrlenW(rootkey_name);
  136. return (!wcsnicmp(input_path, rootkey_name, length) &&
  137. (input_path[length] == 0 || input_path[length] == '\\'));
  138. }
  139. HKEY path_get_rootkey(const WCHAR *path)
  140. {
  141. DWORD i;
  142. for (i = 0; i < ARRAY_SIZE(root_rels); i++)
  143. {
  144. if (path_rootname_cmp(path, root_rels[i].short_name) ||
  145. path_rootname_cmp(path, root_rels[i].long_name))
  146. return root_rels[i].key;
  147. }
  148. return NULL;
  149. }
  150. static BOOL sane_path(const WCHAR *key)
  151. {
  152. unsigned int i = lstrlenW(key);
  153. if (i < 3 || (key[i - 1] == '\\' && key[i - 2] == '\\'))
  154. {
  155. output_message(STRING_INVALID_KEY);
  156. return FALSE;
  157. }
  158. if (key[0] == '\\' && key[1] == '\\' && key[2] != '\\')
  159. {
  160. output_message(STRING_NO_REMOTE);
  161. return FALSE;
  162. }
  163. return TRUE;
  164. }
  165. WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
  166. {
  167. WCHAR *subkey_path;
  168. subkey_path = malloc((path_len + subkey_len + 2) * sizeof(WCHAR));
  169. swprintf(subkey_path, path_len + subkey_len + 2, L"%s\\%s", path, subkey_name);
  170. return subkey_path;
  171. }
  172. WCHAR *get_long_key(HKEY root, WCHAR *path)
  173. {
  174. int i, len;
  175. WCHAR *long_key;
  176. for (i = 0; i < ARRAY_SIZE(root_rels); i++)
  177. {
  178. if (root == root_rels[i].key)
  179. break;
  180. }
  181. len = lstrlenW(root_rels[i].long_name);
  182. if (!path)
  183. {
  184. long_key = malloc((len + 1) * sizeof(WCHAR));
  185. lstrcpyW(long_key, root_rels[i].long_name);
  186. return long_key;
  187. }
  188. len += lstrlenW(path) + 1; /* add one for the concatenating backslash */
  189. long_key = malloc((len + 1) * sizeof(WCHAR));
  190. swprintf(long_key, len + 1, L"%s\\%s", root_rels[i].long_name, path);
  191. return long_key;
  192. }
  193. BOOL parse_registry_key(const WCHAR *key, HKEY *root, WCHAR **path)
  194. {
  195. WCHAR *p;
  196. if (!sane_path(key))
  197. return FALSE;
  198. *root = path_get_rootkey(key);
  199. if (!*root)
  200. {
  201. output_message(STRING_INVALID_SYSTEM_KEY);
  202. return FALSE;
  203. }
  204. *path = wcschr(key, '\\');
  205. if (!*path)
  206. return TRUE;
  207. (*path)++;
  208. if (!**path)
  209. {
  210. output_message(STRING_INVALID_SYSTEM_KEY);
  211. return FALSE;
  212. }
  213. p = *path + lstrlenW(*path) - 1;
  214. if (*p == '\\') *p = 0;
  215. return TRUE;
  216. }
  217. BOOL is_char(const WCHAR s, const WCHAR c)
  218. {
  219. return (s == c || s == towupper(c));
  220. }
  221. BOOL is_switch(const WCHAR *s, const WCHAR c)
  222. {
  223. if (lstrlenW(s) > 2)
  224. return FALSE;
  225. return ((s[0] == '/' || s[0] == '-') && is_char(s[1], c));
  226. }
  227. static BOOL is_help_switch(const WCHAR *s)
  228. {
  229. return (is_switch(s, '?') || is_switch(s, 'h'));
  230. }
  231. enum operations {
  232. REG_ADD,
  233. REG_COPY,
  234. REG_DELETE,
  235. REG_EXPORT,
  236. REG_IMPORT,
  237. REG_QUERY,
  238. REG_INVALID
  239. };
  240. static enum operations get_operation(const WCHAR *str, int *op_help)
  241. {
  242. struct op_info { const WCHAR *op; int id; int help_id; };
  243. static const struct op_info op_array[] =
  244. {
  245. { L"add", REG_ADD, STRING_ADD_USAGE },
  246. { L"copy", REG_COPY, STRING_COPY_USAGE },
  247. { L"delete", REG_DELETE, STRING_DELETE_USAGE },
  248. { L"export", REG_EXPORT, STRING_EXPORT_USAGE },
  249. { L"import", REG_IMPORT, STRING_IMPORT_USAGE },
  250. { L"query", REG_QUERY, STRING_QUERY_USAGE },
  251. { NULL, -1, 0 }
  252. };
  253. const struct op_info *ptr;
  254. for (ptr = op_array; ptr->op; ptr++)
  255. {
  256. if (!lstrcmpiW(str, ptr->op))
  257. {
  258. *op_help = ptr->help_id;
  259. return ptr->id;
  260. }
  261. }
  262. return REG_INVALID;
  263. }
  264. int __cdecl wmain(int argc, WCHAR *argvW[])
  265. {
  266. int op, op_help;
  267. if (argc == 1)
  268. {
  269. output_message(STRING_INVALID_SYNTAX);
  270. output_message(STRING_REG_HELP);
  271. return 1;
  272. }
  273. if (is_help_switch(argvW[1]))
  274. {
  275. output_message(STRING_USAGE);
  276. return 0;
  277. }
  278. op = get_operation(argvW[1], &op_help);
  279. if (op == REG_INVALID)
  280. {
  281. output_message(STRING_INVALID_OPTION, argvW[1]);
  282. output_message(STRING_REG_HELP);
  283. return 1;
  284. }
  285. else if (argc == 2) /* Valid operation, no arguments supplied */
  286. goto invalid;
  287. if (is_help_switch(argvW[2]))
  288. {
  289. if (argc > 3) goto invalid;
  290. output_message(op_help);
  291. output_message(STRING_REG_VIEW_USAGE);
  292. return 0;
  293. }
  294. if (op == REG_ADD)
  295. return reg_add(argc, argvW);
  296. if (op == REG_COPY)
  297. return reg_copy(argc, argvW);
  298. if (op == REG_DELETE)
  299. return reg_delete(argc, argvW);
  300. if (op == REG_EXPORT)
  301. return reg_export(argc, argvW);
  302. if (op == REG_IMPORT)
  303. return reg_import(argc, argvW);
  304. return reg_query(argc, argvW);
  305. invalid:
  306. output_message(STRING_INVALID_SYNTAX);
  307. output_message(STRING_FUNC_HELP, wcsupr(argvW[1]));
  308. return 1;
  309. }