2 * Copyright (c) 2003 Matthijs Hollemans
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
32 #ifndef HAIKU_HOST_PLATFORM_SUNOS
33 extern const char *__progname
;
36 static const char *kTitle
= "Haiku Resource Compiler 1.1";
37 #ifdef HAIKU_HOST_PLATFORM_SUNOS
38 static const char *kProgramName
= "rc";
40 static const char *kProgramName
= __progname
;
44 static bool sQuiet
= false;
45 static bool sDecompile
= false;
46 static uint32 sFlags
= 0;
48 static char sOutputFile
[B_PATH_NAME_LENGTH
] = { 0 };
49 static char *sFirstInputFile
= NULL
;
53 warn(const char *format
, ...)
58 fprintf(stderr
, "%s: Warning! ", kProgramName
);
60 vfprintf(stderr
, format
, ap
);
62 fprintf(stderr
, "\n");
68 error(const char *format
, ...)
73 fprintf(stderr
, "%s: Error! ", kProgramName
);
75 vfprintf(stderr
, format
, ap
);
77 fprintf(stderr
, "\n");
88 "To compile an rdef script into a resource file:\n"
89 " %s [options] [-o <file>] <file>...\n\n"
90 "To convert a resource file back into an rdef script:\n"
91 " %s [options] [-o <file>] -d <file>...\n\n"
93 " -d --decompile create an rdef script from a resource file\n"
94 " --auto-names construct resource names from ID symbols\n"
95 " -h --help show this message\n"
96 " -I --include <dir> add <dir> to the list of include paths\n"
97 " -m --merge do not erase existing contents of output file\n"
98 " -o --output specify output file name, default is out.xxx\n"
99 " -q --quiet do not display any error messages\n"
100 " -V --version show software version and license\n",
101 kTitle
, kProgramName
, kProgramName
);
111 "Copyright (c) 2003 Matthijs Hollemans\n\n"
113 "Permission is hereby granted, free of charge, to any person obtaining a\n"
114 "copy of this software and associated documentation files (the \"Software\"),\n"
115 "to deal in the Software without restriction, including without limitation\n"
116 "the rights to use, copy, modify, merge, publish, distribute, sublicense,\n"
117 "and/or sell copies of the Software, and to permit persons to whom the\n"
118 "Software is furnished to do so, subject to the following conditions:\n\n"
120 "The above copyright notice and this permission notice shall be included in\n"
121 "all copies or substantial portions of the Software.\n\n"
123 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n"
124 "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n"
125 "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n"
126 "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n"
127 "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\n"
128 "FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\n"
129 "DEALINGS IN THE SOFTWARE.\n",
137 has_extension(char *name
, const char *ext
)
139 size_t nameLength
= strlen(name
);
140 size_t extLength
= strlen(ext
);
142 if (nameLength
> extLength
)
143 return strcmp(name
+ nameLength
- extLength
, ext
) == 0;
150 cut_extension(char *name
, const char *ext
)
152 if (!has_extension(name
, ext
))
155 name
[strlen(name
) - strlen(ext
)] = '\0';
160 add_extension(char *name
, const char *ext
)
162 strlcat(name
, ext
, B_PATH_NAME_LENGTH
);
167 parse_options(int argc
, char *argv
[])
169 int32 args_left
= argc
- 1;
171 // ToDo: use getopt_long()
173 for (int32 i
= 1; i
< argc
; ++i
) {
174 if (strcmp(argv
[i
], "-o") == 0
175 || strcmp(argv
[i
], "--output") == 0) {
177 error("%s should be followed by a file name", argv
[i
]);
179 strcpy(sOutputFile
, argv
[i
+ 1]);
184 } else if (strcmp(argv
[i
], "-I") == 0
185 || strcmp(argv
[i
], "--include") == 0) {
187 error("%s should be followed by a directory name", argv
[i
]);
189 rdef_add_include_dir(argv
[i
+ 1], true);
194 } else if (strcmp(argv
[i
], "-d") == 0
195 || strcmp(argv
[i
], "--decompile") == 0) {
199 } else if (strcmp(argv
[i
], "-m") == 0
200 || strcmp(argv
[i
], "--merge") == 0) {
201 sFlags
|= RDEF_MERGE_RESOURCES
;
204 } else if (strcmp(argv
[i
], "--auto-names") == 0) {
205 sFlags
|= RDEF_AUTO_NAMES
;
208 } else if (strcmp(argv
[i
], "-q") == 0
209 || strcmp(argv
[i
], "--quiet") == 0) {
213 } else if (strcmp(argv
[i
], "-h") == 0
214 || strcmp(argv
[i
], "--help") == 0) {
216 } else if (strcmp(argv
[i
], "-V") == 0
217 || strcmp(argv
[i
], "--version") == 0) {
219 } else if (!strcmp(argv
[i
], "-")) {
222 } else if (argv
[i
][0] == '-') {
223 error("unknown option %s", argv
[i
]);
230 error("no input files");
234 for (int i
= 1; i
< argc
; ++i
) {
238 if (sFirstInputFile
== NULL
)
239 sFirstInputFile
= argv
[i
];
241 rdef_add_input_file(argv
[i
]);
244 if (sOutputFile
[0] == '\0') {
245 // no output file name was given, use the name of the
246 // first source file as base
247 strlcpy(sOutputFile
, sFirstInputFile
, sizeof(sOutputFile
));
249 cut_extension(sOutputFile
, sDecompile
? ".rsrc" : ".rdef");
250 add_extension(sOutputFile
, sDecompile
? ".rdef" : ".rsrc");
258 if (!has_extension(sOutputFile
, ".rsrc"))
259 add_extension(sOutputFile
, ".rsrc");
261 rdef_compile(sOutputFile
);
268 if (!has_extension(sOutputFile
, ".rdef"))
269 add_extension(sOutputFile
, ".rdef");
271 rdef_decompile(sOutputFile
);
279 case RDEF_COMPILE_ERR
:
280 error("%s:%ld %s", rdef_err_file
, rdef_err_line
, rdef_err_msg
);
283 case RDEF_FILE_NOT_FOUND
:
284 error("%s not found", rdef_err_file
);
287 case RDEF_NO_RESOURCES
:
288 error("%s is not a resource file", rdef_err_file
);
292 error("error writing to %s (%s)", rdef_err_file
, rdef_err_msg
);
296 error("out of memory");
301 error("unknown error: %lx (%s)", rdef_err
, strerror(rdef_err
));
308 main(int argc
, char *argv
[])
310 parse_options(argc
, argv
);
312 rdef_set_flags(sFlags
);
319 rdef_free_input_files();
320 rdef_free_include_dirs();
322 if (rdef_err
!= B_OK
)