summaryrefslogtreecommitdiff
path: root/jam-files/engine/w32_getreg.c
blob: 5a06f43e92465ed4a01bc32bffe2717a55bc6302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
Copyright Paul Lin 2003. Copyright 2006 Bojan Resnik.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/

# include "jam.h"

# if defined( OS_NT ) || defined( OS_CYGWIN )

# include "lists.h"
# include "newstr.h"
# include "parse.h"
# include "frames.h"
# include "strings.h"

# define WIN32_LEAN_AND_MEAN
# include <windows.h>

# define  MAX_REGISTRY_DATA_LENGTH 4096
# define  MAX_REGISTRY_KEYNAME_LENGTH 256
# define  MAX_REGISTRY_VALUENAME_LENGTH 16384

typedef struct
{
    LPCSTR  name;
    HKEY    value;
} KeyMap;

static const KeyMap dlRootKeys[] = {
    { "HKLM", HKEY_LOCAL_MACHINE },
    { "HKCU", HKEY_CURRENT_USER },
    { "HKCR", HKEY_CLASSES_ROOT },
    { "HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE },
    { "HKEY_CURRENT_USER", HKEY_CURRENT_USER },
    { "HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT },
    { 0, 0 }
};

static HKEY get_key(char const** path)
{
    const KeyMap *p;

    for (p = dlRootKeys; p->name; ++p)
    {
        int n = strlen(p->name);
        if (!strncmp(*path,p->name,n))
        {
            if ((*path)[n] == '\\' || (*path)[n] == 0)
            {
                *path += n + 1;
                break;
            }
        }
    }

    return p->value;
}

LIST*
builtin_system_registry(
    PARSE    *parse,
    FRAME    *frame )
{
    char const* path = lol_get(frame->args, 0)->string;
    LIST* result = L0;
    HKEY key = get_key(&path);

    if (
        key != 0
        && ERROR_SUCCESS == RegOpenKeyEx(key, path, 0, KEY_QUERY_VALUE, &key)
    )
    {
        DWORD  type;
        BYTE   data[MAX_REGISTRY_DATA_LENGTH];
        DWORD  len = sizeof(data);
        LIST const* const field = lol_get(frame->args, 1);

        if ( ERROR_SUCCESS ==
             RegQueryValueEx(key, field ? field->string : 0, 0, &type, data, &len) )
        {
            switch (type)
            {

             case REG_EXPAND_SZ:
                 {
                     long len;
                     string expanded[1];
                     string_new(expanded);

                     while (
                         (len = ExpandEnvironmentStrings(
                             (LPCSTR)data, expanded->value, expanded->capacity))
                         > expanded->capacity
                     )
                         string_reserve(expanded, len);

                     expanded->size = len - 1;

                     result = list_new( result, newstr(expanded->value) );
                     string_free( expanded );
                 }
                 break;

             case REG_MULTI_SZ:
                 {
                     char* s;

                     for (s = (char*)data; *s; s += strlen(s) + 1)
                         result = list_new( result, newstr(s) );

                 }
                 break;

             case REG_DWORD:
                 {
                     char buf[100];
                     sprintf( buf, "%u", *(PDWORD)data );
                     result = list_new( result, newstr(buf) );
                 }
                 break;

             case REG_SZ:
                 result = list_new( result, newstr((char*)data) );
                 break;
            }
        }
        RegCloseKey(key);
    }
    return  result;
}

static LIST* get_subkey_names(HKEY key, char const* path)
{
    LIST* result = 0;

    if ( ERROR_SUCCESS ==
         RegOpenKeyEx(key, path, 0, KEY_ENUMERATE_SUB_KEYS, &key)
    )
    {
        char name[MAX_REGISTRY_KEYNAME_LENGTH];
        DWORD name_size = sizeof(name);
        DWORD index;
        FILETIME last_write_time;

        for ( index = 0;
              ERROR_SUCCESS == RegEnumKeyEx(
                  key, index, name, &name_size, 0, 0, 0, &last_write_time);
              ++index,
              name_size = sizeof(name)
        )
        {
            name[name_size] = 0;
            result = list_append(result, list_new(0, newstr(name)));
        }

        RegCloseKey(key);
    }

    return result;
}

static LIST* get_value_names(HKEY key, char const* path)
{
    LIST* result = 0;

    if ( ERROR_SUCCESS == RegOpenKeyEx(key, path, 0, KEY_QUERY_VALUE, &key) )
    {
        char name[MAX_REGISTRY_VALUENAME_LENGTH];
        DWORD name_size = sizeof(name);
        DWORD index;

        for ( index = 0;
              ERROR_SUCCESS == RegEnumValue(
                  key, index, name, &name_size, 0, 0, 0, 0);
              ++index,
              name_size = sizeof(name)
        )
        {
            name[name_size] = 0;
            result = list_append(result, list_new(0, newstr(name)));
        }

        RegCloseKey(key);
    }

    return result;
}

LIST*
builtin_system_registry_names(
    PARSE    *parse,
    FRAME    *frame )
{
    char const* path        = lol_get(frame->args, 0)->string;
    char const* result_type = lol_get(frame->args, 1)->string;

    HKEY key = get_key(&path);

    if ( !strcmp(result_type, "subkeys") )
        return get_subkey_names(key, path);
    if ( !strcmp(result_type, "values") )
        return get_value_names(key, path);
    return 0;
}

# endif