[MKHIVE][CMAKE] Make mkhive a bit more flexible, so that it can generate only specifi...
[reactos.git] / sdk / tools / mkhive / mkhive.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2003, 2006 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS hive maker
22 * FILE: tools/mkhive/mkhive.c
23 * PURPOSE: Hive maker
24 * PROGRAMMERS: Eric Kohl
25 * Hervé Poussineau
26 * Hermès Bélusca-Maïto
27 */
28
29 #include <limits.h>
30 #include <string.h>
31 #include <stdio.h>
32
33 #include "mkhive.h"
34
35 #ifdef _MSC_VER
36 #include <stdlib.h>
37 #define PATH_MAX _MAX_PATH
38 #endif // _MSC_VER
39
40 #ifndef _WIN32
41 #ifndef PATH_MAX
42 #define PATH_MAX 260
43 #endif
44 #define DIR_SEPARATOR_CHAR '/'
45 #define DIR_SEPARATOR_STRING "/"
46 #else
47 #define DIR_SEPARATOR_CHAR '\\'
48 #define DIR_SEPARATOR_STRING "\\"
49 #endif
50
51
52 void usage(void)
53 {
54 printf("Usage: mkhive -h:hive1[,hiveN...] -d:<dstdir> <inffiles>\n\n"
55 " -h:hiveN - Comma-separated list of hives to create. Possible values are:\n"
56 " SETUPREG, SYSTEM, SOFTWARE, DEFAULT, SAM, SECURITY, BCD.\n"
57 " -d:dstdir - The binary hive files are created in this directory.\n"
58 " inffiles - List of INF files with full path.\n");
59 }
60
61 void convert_path(char *dst, char *src)
62 {
63 int i;
64
65 i = 0;
66 while (src[i] != 0)
67 {
68 #ifdef _WIN32
69 if (src[i] == '/')
70 {
71 dst[i] = '\\';
72 }
73 #else
74 if (src[i] == '\\')
75 {
76 dst[i] = '/';
77 }
78 #endif
79 else
80 {
81 dst[i] = src[i];
82 }
83
84 i++;
85 }
86 dst[i] = 0;
87 }
88
89 int main(int argc, char *argv[])
90 {
91 INT ret;
92 UINT i;
93 PCSTR HiveList = NULL;
94 CHAR DestPath[PATH_MAX] = "";
95 CHAR FileName[PATH_MAX];
96
97 if (argc < 4)
98 {
99 usage();
100 return -1;
101 }
102
103 printf("Binary hive maker\n");
104
105 /* Read the options */
106 for (i = 1; i < argc && *argv[i] == '-'; i++)
107 {
108 if (argv[i][1] == 'h' && (argv[i][2] == ':' || argv[i][2] == '='))
109 {
110 HiveList = argv[i] + 3;
111 }
112 else if (argv[i][1] == 'd' && (argv[i][2] == ':' || argv[i][2] == '='))
113 {
114 convert_path(DestPath, argv[i] + 3);
115 }
116 else
117 {
118 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
119 return -1;
120 }
121 }
122
123 /* Check whether we have all the parameters needed */
124 if (!HiveList || !*HiveList)
125 {
126 fprintf(stderr, "The mandatory list of hives is missing.\n");
127 return -1;
128 }
129 if (!*DestPath)
130 {
131 fprintf(stderr, "The mandatory output directory is missing.\n");
132 return -1;
133 }
134 if (i >= argc)
135 {
136 fprintf(stderr, "Not enough parameters, or the list of INF files is missing.\n");
137 return -1;
138 }
139
140 /* Initialize the registry */
141 RegInitializeRegistry(HiveList);
142
143 /* Default to failure */
144 ret = -1;
145
146 /* Now we should have the list of INF files: parse it */
147 for (; i < argc; ++i)
148 {
149 convert_path(FileName, argv[i]);
150 if (!ImportRegistryFile(FileName))
151 goto Quit;
152 }
153
154 for (i = 0; i < MAX_NUMBER_OF_REGISTRY_HIVES; ++i)
155 {
156 /* Skip this registry hive if it's not in the list */
157 if (!strstr(HiveList, RegistryHives[i].HiveName))
158 continue;
159
160 strcpy(FileName, DestPath);
161 strcat(FileName, DIR_SEPARATOR_STRING);
162 strcat(FileName, RegistryHives[i].HiveName);
163
164 /* Exception for the special setup registry hive */
165 // if (strcmp(RegistryHives[i].HiveName, "SETUPREG") == 0)
166 if (i == 0)
167 strcat(FileName, ".HIV");
168
169 if (!ExportBinaryHive(FileName, RegistryHives[i].CmHive))
170 goto Quit;
171
172 /* If we happen to deal with the special setup registry hive, stop there */
173 // if (strcmp(RegistryHives[i].HiveName, "SETUPREG") == 0)
174 if (i == 0)
175 break;
176 }
177
178 /* Success */
179 ret = 0;
180
181 Quit:
182 /* Shut down the registry */
183 RegShutdownRegistry();
184
185 if (ret == 0)
186 printf(" Done.\n");
187
188 return ret;
189 }
190
191 /* EOF */