1 | /* Copyright Paul Lin 2003. Distributed under the Boost */ |
---|
2 | /* Software License, Version 1.0. (See accompanying */ |
---|
3 | /* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) */ |
---|
4 | |
---|
5 | # include "jam.h" |
---|
6 | |
---|
7 | # if defined( OS_NT ) || defined( OS_CYGWIN ) |
---|
8 | |
---|
9 | # include "lists.h" |
---|
10 | # include "newstr.h" |
---|
11 | # include "parse.h" |
---|
12 | # include "frames.h" |
---|
13 | # include "strings.h" |
---|
14 | |
---|
15 | # define WIN32_LEAN_AND_MEAN |
---|
16 | # include <windows.h> |
---|
17 | |
---|
18 | # define MAX_REGISTRY_DATA_LENGTH 4096 |
---|
19 | |
---|
20 | typedef struct |
---|
21 | { |
---|
22 | LPCSTR name; |
---|
23 | HKEY value; |
---|
24 | } KeyMap; |
---|
25 | |
---|
26 | static const KeyMap dlRootKeys[] = { |
---|
27 | { "HKLM", HKEY_LOCAL_MACHINE }, |
---|
28 | { "HKCU", HKEY_CURRENT_USER }, |
---|
29 | { "HKCR", HKEY_CLASSES_ROOT }, |
---|
30 | { "HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE }, |
---|
31 | { "HKEY_CURRENT_USER", HKEY_CURRENT_USER }, |
---|
32 | { "HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT }, |
---|
33 | { 0, 0 } |
---|
34 | }; |
---|
35 | |
---|
36 | LIST* |
---|
37 | builtin_system_registry( |
---|
38 | PARSE *parse, |
---|
39 | FRAME *frame ) |
---|
40 | { |
---|
41 | char const* path = lol_get(frame->args, 0)->string; |
---|
42 | LIST* result = L0; |
---|
43 | HKEY key; |
---|
44 | |
---|
45 | { |
---|
46 | const KeyMap *p; |
---|
47 | |
---|
48 | for (p = dlRootKeys; p->name; ++p) |
---|
49 | { |
---|
50 | int n = strlen(p->name); |
---|
51 | if (!strncmp(path,p->name,n)) |
---|
52 | { |
---|
53 | if (path[n] == '\\' || path[n] == 0) |
---|
54 | { |
---|
55 | path += n + 1; |
---|
56 | break; |
---|
57 | } |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | key = p->value; |
---|
62 | } |
---|
63 | |
---|
64 | if ( |
---|
65 | key != 0 |
---|
66 | && ERROR_SUCCESS == RegOpenKeyEx(key, path, 0, KEY_QUERY_VALUE, &key) |
---|
67 | ) |
---|
68 | { |
---|
69 | DWORD type; |
---|
70 | BYTE data[MAX_REGISTRY_DATA_LENGTH]; |
---|
71 | DWORD len = sizeof(data); |
---|
72 | LIST const* const field = lol_get(frame->args, 1); |
---|
73 | |
---|
74 | if ( ERROR_SUCCESS == |
---|
75 | RegQueryValueEx(key, field ? field->string : 0, 0, &type, data, &len) ) |
---|
76 | { |
---|
77 | switch (type) |
---|
78 | { |
---|
79 | |
---|
80 | case REG_EXPAND_SZ: |
---|
81 | { |
---|
82 | long len; |
---|
83 | string expanded[1]; |
---|
84 | string_new(expanded); |
---|
85 | |
---|
86 | while ( |
---|
87 | (len = ExpandEnvironmentStrings( |
---|
88 | (LPCSTR)data, expanded->value, expanded->capacity)) |
---|
89 | > expanded->capacity |
---|
90 | ) |
---|
91 | string_reserve(expanded, len); |
---|
92 | |
---|
93 | expanded->size = len - 1; |
---|
94 | |
---|
95 | result = list_new( result, newstr(expanded->value) ); |
---|
96 | string_free( expanded ); |
---|
97 | } |
---|
98 | break; |
---|
99 | |
---|
100 | case REG_MULTI_SZ: |
---|
101 | { |
---|
102 | char* s; |
---|
103 | |
---|
104 | for (s = (char*)data; *s; s += strlen(s) + 1) |
---|
105 | result = list_new( result, newstr(s) ); |
---|
106 | |
---|
107 | } |
---|
108 | break; |
---|
109 | |
---|
110 | case REG_DWORD: |
---|
111 | { |
---|
112 | char buf[100]; |
---|
113 | sprintf( buf, "%u", *(PDWORD)data ); |
---|
114 | result = list_new( result, newstr(buf) ); |
---|
115 | } |
---|
116 | break; |
---|
117 | |
---|
118 | case REG_SZ: |
---|
119 | result = list_new( result, newstr((char*)data) ); |
---|
120 | break; |
---|
121 | } |
---|
122 | } |
---|
123 | RegCloseKey(key); |
---|
124 | } |
---|
125 | return result; |
---|
126 | } |
---|
127 | |
---|
128 | # endif |
---|