File: gconf-schemas-to-win32-reg.c

package info (click to toggle)
gnumeric 1.6.3-5.1%2Betch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 70,644 kB
  • ctags: 18,973
  • sloc: ansic: 204,271; xml: 47,128; sh: 8,917; makefile: 2,540; yacc: 1,137; perl: 179; python: 86
file content (401 lines) | stat: -rwxr-xr-x 9,310 bytes parent folder | download | duplicates (2)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * schemas-to-reg.c : Convert gconf's .schemas file into Win32 Registry's .reg file
 *
 * Copyright (C) 2005 Ivan, Wong Yat Cheung  email@ivanwong.info
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <glib-2.0/glib.h>

#define GCONF_APP_ROOT "/apps"
#define WIN32_USERS_APP_ROOT "[HKEY_USERS\\.DEFAULT\\Software"
#define WIN32_CURRENT_USER_APP_ROOT "[HKEY_CURRENT_USER\\Software"

static gboolean current_user = FALSE;

static GOptionEntry entries[] = 
{
	{ "current-user-only", 'c', 0, G_OPTION_ARG_NONE, &current_user,
	  "Output HKEY_CURRENT_USER instead of HKEY_USERS.", NULL },
	{ NULL }
};

enum {
	KEY_TYPE_NONE = 0,
	KEY_TYPE_INT,
	KEY_TYPE_BOOL,
	KEY_TYPE_STRING,
	KEY_TYPE_FLOAT,
	KEY_TYPE_LIST
};

typedef struct _KeyInfo KeyInfo;
struct _KeyInfo {
	gchar *name;
	gint type;
	gint list_type;
	gchar *def;
};

typedef struct _NodeInfo NodeInfo;
struct _NodeInfo {
	gchar *name;
	GSList *values;
};

static gint
get_type_from_str (const gchar *str)
{
	gint type;

	if (!xmlStrcmp (str, "int"))
		type = KEY_TYPE_INT;
	else if (!xmlStrcmp (str, "bool"))
		type = KEY_TYPE_BOOL;
	else if (!xmlStrcmp (str, "string"))
		type = KEY_TYPE_STRING;
	else if (!xmlStrcmp (str, "float"))
		type = KEY_TYPE_FLOAT;
	else if (!xmlStrcmp (str, "list"))
		type = KEY_TYPE_LIST;
	else
		type = KEY_TYPE_NONE;

	return type;
}

static void
print_string (gchar *str)
{
	for (; *str; ++str) {
		switch (*str) {
		case '\\':
		case '"':
			putchar ('\\');
			break;
		}
		putchar (*str);
	}
}

static void
convert_key (KeyInfo *info)
{
	gchar *ptr;

	g_print ("\"%s\"=", info->name);

	switch (info->type) {
	case KEY_TYPE_INT:
		g_print ("dword:%08x", (gint) atol (info->def));
		break;
	case KEY_TYPE_BOOL:
		g_print ("hex:0%d", g_ascii_strncasecmp (info->def, "TRUE", 4) ? 0 : 1);
		break;
	case KEY_TYPE_STRING:
		putchar ('"');
		if (info->def)
			print_string (info->def);
		putchar ('"');
		break;
	case KEY_TYPE_FLOAT:
		g_print ("\"%s\"", info->def);
		break;
	case KEY_TYPE_LIST:
		putchar ('"');
		if ((ptr = info->def) != NULL && *(ptr++) == '[') {
			GString *temp_str;
			gchar c, *str;
			gboolean end = FALSE;

			temp_str = g_string_new ("");
			for (; (c = *ptr) != '\0'; ++ptr) {
				if (c == '\\') {
					if (!(c = *(++ptr)))
						break;
				}
				else if (c == ',' || (end = c == ']')) {
					str = g_strescape (temp_str->str, NULL);
					print_string (temp_str->str);
					putchar ('\n');
					g_free (str);
					temp_str->len = 0;
					temp_str->str[0] = '\0';
					if (end)
						break;
					else
						continue;
				}
				g_string_append_c (temp_str, c);
			}
			if (!end)
				g_warning ("String List %s is not properly ended", info->name);
			g_string_free (temp_str, TRUE);
		}
		putchar ('"');
		break;
	default:
		g_printerr ("General type not implemented: %d\n", info->type);
	}

	putchar ('\n');
}

static void
convert_schema (xmlDocPtr doc, xmlNodePtr schema, GHashTable *table)
{
	xmlNodePtr n, node = schema->xmlChildrenNode;
	xmlChar *str;
	KeyInfo info = {0};
	gboolean is_locale;

	while (node) {
		if (node->type != XML_ELEMENT_NODE) {
			node = node->next;
			continue;
		}

		if (!xmlStrcmp (node->name, "applyto")) {
			str = xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
			if (info.name)
				xmlFree (info.name);
			info.name = str;
		}
		else if (!xmlStrcmp (node->name, "type")) {
			str = xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
			info.type = get_type_from_str (str);
			xmlFree (str);
		}
		else if (!xmlStrcmp (node->name, "list_type")) {
			str = xmlNodeListGetString (doc, node->xmlChildrenNode, 1);
			info.list_type = get_type_from_str (str);
			xmlFree (str);
		}
		else if (!(is_locale = xmlStrcmp (node->name, "default")) ||
			 !xmlStrcmp (node->name, "locale")) {
			if (is_locale) {
				n = node->xmlChildrenNode;
				while (n) {
					if (n->type == XML_ELEMENT_NODE &&
					    !xmlStrcmp (n->name, "default"))
						break;
					n = n->next;
				}
			}
			else
				n = node;

			if (n) {
				str = xmlNodeListGetString (doc, n->xmlChildrenNode, 1);
				if (info.def)
					g_free (info.def);
				info.def = g_strdup (str ? (gchar *) str : "");
				if (str)
					xmlFree (str);
			}
		}

		node = node->next;
	}

	if (info.name && info.type &&
	    (info.type != KEY_TYPE_LIST || info.list_type) &&
	    (info.type == KEY_TYPE_LIST || info.def)) {
		GNode *node = NULL;
		gchar *end;
		KeyInfo *new_info;
		gint i, cnt = 0;
		GSList **values;

		if (xmlStrncmp (info.name, GCONF_APP_ROOT "/", 6)) {
			g_printerr ("Don't know how to handle this key: %s\n", info.name);
			return;
		}

		do {
			if (!(end = strrchr (info.name, '/')))
				break;
			++cnt;
			*end = '\0';
		} while (!(node = g_hash_table_lookup (table, info.name)));

		if (!node) {
			g_printerr ("Can find the root node?\n");
			return;
		}

		for (i = 1; i < cnt; ++i) {
			NodeInfo *node_info = g_new (NodeInfo, 1);
			node_info->name = g_strdup (end + 1);
			node_info->values = NULL;
			*end = '/';
			end += strlen (node_info->name) + 1;
			node = g_node_insert_data (node, -1, node_info);
			g_hash_table_insert (table, g_strdup (info.name), node);
		}
	
		new_info = g_new (KeyInfo, 1);
		*new_info = info;
		new_info->name = g_strdup (end + 1);
		values = &((NodeInfo *) node->data)->values;
		*values = g_slist_prepend (*values, new_info);
			
		xmlFree (info.name);
	}
	else
		g_warning ("Invalid key %s", info.name ? info.name : "(unknown name)");
}

static gboolean
free_node (GNode *node, gpointer data)
{
	GSList *values;
	KeyInfo *key_info;
	NodeInfo *info = (NodeInfo *) node->data;

	g_free (info->name);
	for (values = info->values; values; values = values->next) {
		key_info = (KeyInfo *) values->data;
		g_free (key_info->name);
		if (key_info->def)
			g_free (key_info->def);
		g_free (key_info);
	}
	if (info->values)
		g_slist_free (info->values);
	
	return FALSE;
}

static gboolean
print_node (GNode *node, gpointer data)
{
	GNode *n, *root = (GNode *) data;
	GSList *values;
	NodeInfo *info = (NodeInfo *) node->data;
	GString *path = g_string_new ("");

	if (node == root)
		return FALSE;

	g_print (current_user ? WIN32_CURRENT_USER_APP_ROOT : WIN32_USERS_APP_ROOT);

	for (n = node; n != root; n = n->parent) {
		g_string_prepend (path, ((NodeInfo *) n->data)->name);
		g_string_prepend_c (path, '\\');
	}
	g_print ("%s]\n", path->str);
	g_string_free (path, TRUE);
	
	for (values = info->values; values; values = values->next)
		convert_key ((KeyInfo *) values->data);
	
	putchar ('\n');
	
	return FALSE;
}

static void
convert_schemalist (xmlDocPtr doc, xmlNodePtr schemalist)
{
	xmlNodePtr node = schemalist->xmlChildrenNode;
	GHashTable *table;
	GNode *root;
	NodeInfo *info;

	table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
	info = g_new (NodeInfo, 1);
	info->name = g_strdup (GCONF_APP_ROOT);
	info->values = NULL;
	root = g_node_new (info);
	g_hash_table_insert (table, g_strdup (GCONF_APP_ROOT), root);

	g_print ("REGEDIT4\n\n");

	while (node) {
		if (node->type == XML_ELEMENT_NODE &&
		    !xmlStrcmp (node->name, "schema"))
			convert_schema (doc, node, table);
		node = node->next;
	}

	g_hash_table_destroy (table);
	g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, print_node, root);
	g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, free_node, NULL);
}

static void
convert (xmlDocPtr doc, xmlNodePtr root)
{
	xmlNodePtr node = NULL;

	if (!root) {
		g_printerr ("Empty document\n");
		return;
	}

	if (xmlStrcmp (root->name, "gconfschemafile")) {
		g_printerr ("Document of the wrong type, root node != gconfschemafile\n");
		return;
	}

	node = root->xmlChildrenNode;

	while (node) {
		if (node->type == XML_ELEMENT_NODE &&
		    !xmlStrcmp (node->name, "schemalist"))
			convert_schemalist (doc, node);
		node = node->next;
	}
}

int main (int argc, char **argv)
{
	xmlDocPtr doc;
	xmlNodePtr root;
	GOptionContext *context;

	context = g_option_context_new ("inputfile\nConvert gconf .schemas file into win32 registry .reg file");
	g_option_context_add_main_entries (context, entries, NULL);
	g_option_context_parse (context, &argc, &argv, NULL);
	g_option_context_free (context);

	if (argc != 2) {
		g_printerr ("Please provide exactly one input filename\n");
		return 1;
	}

	LIBXML_TEST_VERSION

	doc = xmlReadFile (argv[1], NULL, 0);
	if (!doc) {
		g_printerr ("Failed to parse %s\n", argv[1]);
		return 2;
	}

	root = xmlDocGetRootElement (doc);
	convert (doc, root);

	xmlFreeDoc (doc);
	xmlCleanupParser ();

	return 0;
}