[MKHIVE] Minor additional functionality.
[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...] [-u] -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 " -u - Generate file names in uppercase (default: lowercase) (TEMPORARY FLAG!).\n"
58 " -d:dstdir - The binary hive files are created in this directory.\n"
59 " inffiles - List of INF files with full path.\n"
60 " -? - Displays this help screen.\n");
61 }
62
63 void convert_path(char *dst, char *src)
64 {
65 int i;
66
67 i = 0;
68 while (src[i] != 0)
69 {
70 #ifdef _WIN32
71 if (src[i] == '/')
72 {
73 dst[i] = '\\';
74 }
75 #else
76 if (src[i] == '\\')
77 {
78 dst[i] = '/';
79 }
80 #endif
81 else
82 {
83 dst[i] = src[i];
84 }
85
86 i++;
87 }
88 dst[i] = 0;
89 }
90
91 int main(int argc, char *argv[])
92 {
93 INT ret;
94 UINT i;
95 BOOL UpperCaseFileName = FALSE;
96 PCSTR HiveList = NULL;
97 CHAR DestPath[PATH_MAX] = "";
98 CHAR FileName[PATH_MAX];
99
100 if (argc < 4)
101 {
102 usage();
103 return -1;
104 }
105
106 printf("Binary hive maker\n");
107
108 /* Read the options */
109 for (i = 1; i < argc && (*argv[i] == '-' || *argv[i] == '/'); i++)
110 {
111 if (argv[i][1] == '?' && argv[i][1] == 0)
112 {
113 usage();
114 return 0;
115 }
116
117 if (argv[i][1] == 'u' && argv[i][1] == 0)
118 {
119 UpperCaseFileName = TRUE;
120 }
121 else
122 if (argv[i][1] == 'h' && (argv[i][2] == ':' || argv[i][2] == '='))
123 {
124 HiveList = argv[i] + 3;
125 }
126 else if (argv[i][1] == 'd' && (argv[i][2] == ':' || argv[i][2] == '='))
127 {
128 convert_path(DestPath, argv[i] + 3);
129 }
130 else
131 {
132 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
133 return -1;
134 }
135 }
136
137 /* Check whether we have all the parameters needed */
138 if (!HiveList || !*HiveList)
139 {
140 fprintf(stderr, "The mandatory list of hives is missing.\n");
141 return -1;
142 }
143 if (!*DestPath)
144 {
145 fprintf(stderr, "The mandatory output directory is missing.\n");
146 return -1;
147 }
148 if (i >= argc)
149 {
150 fprintf(stderr, "Not enough parameters, or the list of INF files is missing.\n");
151 return -1;
152 }
153
154 /* Initialize the registry */
155 RegInitializeRegistry(HiveList);
156
157 /* Default to failure */
158 ret = -1;
159
160 /* Now we should have the list of INF files: parse it */
161 for (; i < argc; ++i)
162 {
163 convert_path(FileName, argv[i]);
164 if (!ImportRegistryFile(FileName))
165 goto Quit;
166 }
167
168 for (i = 0; i < MAX_NUMBER_OF_REGISTRY_HIVES; ++i)
169 {
170 /* Skip this registry hive if it's not in the list */
171 if (!strstr(HiveList, RegistryHives[i].HiveName))
172 continue;
173
174 strcpy(FileName, DestPath);
175 strcat(FileName, DIR_SEPARATOR_STRING);
176 strcat(FileName, RegistryHives[i].HiveName);
177
178 /* Exception for the special setup registry hive */
179 // if (strcmp(RegistryHives[i].HiveName, "SETUPREG") == 0)
180 if (i == 0)
181 strcat(FileName, ".HIV");
182
183 /* Adjust file name case if needed */
184 if (UpperCaseFileName)
185 {
186 PSTR ptr = FileName;
187 while (*ptr)
188 *ptr++ = toupper(*ptr);
189 }
190 else
191 {
192 PSTR ptr = FileName;
193 while (*ptr)
194 *ptr++ = tolower(*ptr);
195 }
196
197 if (!ExportBinaryHive(FileName, RegistryHives[i].CmHive))
198 goto Quit;
199
200 /* If we happen to deal with the special setup registry hive, stop there */
201 // if (strcmp(RegistryHives[i].HiveName, "SETUPREG") == 0)
202 if (i == 0)
203 break;
204 }
205
206 /* Success */
207 ret = 0;
208
209 Quit:
210 /* Shut down the registry */
211 RegShutdownRegistry();
212
213 if (ret == 0)
214 printf(" Done.\n");
215
216 return ret;
217 }
218
219 /* EOF */