android: Update app-specific/MIME type icons
[LibreOffice.git] / solenv / wsl / wsl-lo-helper.cpp
blob87285dcb1d3c0d95543dc45ec6c96618a58b4d78
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <cstdio>
11 #include <cstring>
13 #include <Windows.h>
15 static void print_result(const wchar_t* argv0, const wchar_t* result)
17 char output[1000];
18 if (WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, result, -1, output, sizeof(output), NULL,
19 NULL)
20 == 0)
22 fprintf(stderr, "%S --8.3: Could not convert result to UTF-8.\n", argv0);
23 exit(1);
26 // Intentionally output no newline at end
27 printf("%s", output);
28 fflush(stdout);
31 static REGSAM parse_bitness(const wchar_t* argv0, const wchar_t* command, const wchar_t* bitness)
33 if (wcscmp(bitness, L"32") == 0)
34 return KEY_WOW64_32KEY;
35 else if (wcscmp(bitness, L"64") == 0)
36 return KEY_WOW64_64KEY;
37 else
39 fprintf(stderr, "%S %S: Use 32 or 64.\n", argv0, command);
40 exit(1);
44 static void parse_hive(const wchar_t* argv0, const wchar_t* path, HKEY* hivep,
45 const wchar_t** hive_namep)
47 if (wcscmp(path, L"HKEY_CLASSES_ROOT") == 0)
49 *hivep = HKEY_CLASSES_ROOT;
50 *hive_namep = L"HKEY_CLASSES_ROOT";
52 else if (wcscmp(path, L"HKEY_CURRENT_CONFIG") == 0)
54 *hivep = HKEY_CURRENT_CONFIG;
55 *hive_namep = L"HKEY_CURRENT_CONFIG";
57 else if (wcscmp(path, L"HKEY_CURRENT_USER") == 0)
59 *hivep = HKEY_CURRENT_USER;
60 *hive_namep = L"HKEY_CURRENT_USER";
62 else if (wcscmp(path, L"HKEY_LOCAL_MACHINE") == 0)
64 *hivep = HKEY_LOCAL_MACHINE;
65 *hive_namep = L"HKEY_LOCAL_MACHINE";
67 else if (wcscmp(path, L"HKEY_USERS") == 0)
69 *hivep = HKEY_USERS;
70 *hive_namep = L"HKEY_USERS";
72 else
74 fprintf(stderr, "%S: Invalid Registry hive %S.\n", argv0, path);
75 exit(1);
79 int wmain(int argc, wchar_t** argv)
81 if (argc <= 1)
83 fprintf(stderr, "%S: Usage:\n", argv[0]);
84 fprintf(stderr, "%S --8.3 <windows-pathname>\n", argv[0]);
85 fprintf(stderr, " Print the 8.3 form of a Windows pathnam. Fail it not present.\n");
86 fprintf(stderr, "%S --read-registry [32|64] <path>\n", argv[0]);
87 fprintf(stderr, " Read a string value from the Registry and print it.\n");
88 fprintf(stderr, "%S --list-registry [32|64] <path>\n", argv[0]);
89 fprintf(stderr, " List subkeys of a key in the Registry.\n");
90 exit(1);
93 if (wcscmp(argv[1], L"--8.3") == 0)
95 if (argc != 3)
97 fprintf(stderr, "%S --8.3: One pathname argument expected\n", argv[0]);
98 exit(1);
101 // The argument should be a pathname in Windows format. The
102 // output will be the 8.3 pathname if present. If not present,
103 // return failure.
105 wchar_t woutput[1000];
106 if (GetShortPathNameW(argv[2], woutput, sizeof(woutput) / sizeof(woutput[0])) == 0)
108 fprintf(stderr, "%S --8.3: Could not get short pathname of %S.\n", argv[0], argv[2]);
109 exit(1);
112 print_result(argv[0], woutput);
114 else if (wcscmp(argv[1], L"--read-registry") == 0)
116 if (argc != 4)
118 fprintf(stderr, "%S --read-registry: Bitness and path arguments expected.\n", argv[0]);
119 exit(1);
122 REGSAM sam = parse_bitness(argv[0], L"--read-registry", argv[2]);
124 wchar_t* path = wcsdup(argv[3]);
126 for (wchar_t* p = path; *p != L'\0'; p++)
127 if (*p == '/')
128 *p = '\\';
130 wchar_t* const first_backslash = wcschr(path, L'\\');
131 if (first_backslash == NULL)
133 fprintf(stderr, "%S: Invalid path %S to value in the Registry.\n", argv[0], path);
134 exit(1);
137 *first_backslash = L'\0';
138 wchar_t* const key_path = first_backslash + 1;
140 HKEY hive;
141 const wchar_t* hive_name;
142 parse_hive(argv[0], path, &hive, &hive_name);
144 DWORD type;
145 wchar_t result[1000];
146 DWORD count = sizeof(result);
148 wchar_t* last_backslash = wcsrchr(key_path, L'\\');
149 if (last_backslash == NULL)
151 fprintf(stderr, "%S: Invalid path %S\\%S to value in the Registry.\n", argv[0],
152 hive_name, key_path);
153 exit(1);
156 *last_backslash = L'\0';
157 wchar_t* value = last_backslash + 1;
159 HKEY key;
160 if (RegOpenKeyExW(hive, key_path, 0, KEY_QUERY_VALUE | sam, &key) != ERROR_SUCCESS)
162 fprintf(stderr, "%S: Opening key %S\\%S in %S-bit Registry failed.\n", argv[0],
163 hive_name, key_path, argv[2]);
164 exit(1);
167 if (RegQueryValueExW(key, value, NULL, &type, (LPBYTE)result, &count) != ERROR_SUCCESS)
169 fprintf(stderr, "%S: Reading value %S\\%S\\%S from %S-bit Registry failed.\n", argv[0],
170 hive_name, key_path, value, argv[2]);
171 exit(1);
173 if (type != REG_SZ)
175 fprintf(stderr, "%S: Value %S\\%S\\%S is not a string.\n", argv[0], hive_name, key_path,
176 value);
177 exit(1);
179 print_result(argv[0], result);
181 else if (wcscmp(argv[1], L"--list-registry") == 0)
183 if (argc != 4)
185 fprintf(stderr, "%S --list-registry: Bitness and path arguments expected.\n", argv[0]);
186 exit(1);
189 REGSAM sam = parse_bitness(argv[0], L"--list-registry", argv[2]);
191 wchar_t* path = wcsdup(argv[3]);
193 for (wchar_t* p = path; *p != L'\0'; p++)
194 if (*p == '/')
195 *p = '\\';
197 wchar_t* const first_backslash = wcschr(path, L'\\');
198 if (first_backslash == NULL)
200 fprintf(stderr, "%S: Invalid path %S to key in the Registry.\n", argv[0], path);
201 exit(1);
204 *first_backslash = L'\0';
205 wchar_t* const key_path = first_backslash + 1;
207 HKEY hive;
208 const wchar_t* hive_name;
209 parse_hive(argv[0], path, &hive, &hive_name);
211 HKEY key;
212 if (RegOpenKeyExW(hive, key_path, 0, KEY_ENUMERATE_SUB_KEYS | sam, &key) != ERROR_SUCCESS)
214 fprintf(stderr, "%S: Opening key %S\\%S in %S-bit Registry failed.\n", argv[0],
215 hive_name, key_path, argv[2]);
216 exit(1);
219 DWORD index = 0;
220 wchar_t name[256];
221 DWORD namelen = sizeof(name) / sizeof(name[0]);
222 while (RegEnumKeyExW(key, index, name, &namelen, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
224 printf("%S\n", name);
225 index++;
226 namelen = sizeof(name) / sizeof(name[0]);
229 else
231 fprintf(stderr, "%S: Unrecognized sub-command %S.\n", argv[0], argv[1]);
232 exit(1);
235 return 0;
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */