Commit 6431b1c9 authored by Jon Ringle's avatar Jon Ringle Committed by Lev Walkin

Add '-D destdir' option for generated files

parent 7871abf7
......@@ -63,16 +63,19 @@ main(int ac, char **av) {
int print_arg__fix_n_print = 0; /* Fix and print */
int warnings_as_errors = 0; /* Treat warnings as errors */
char *skeletons_dir = NULL; /* Directory with supplementary stuff */
char destdir[PATH_MAX]; /* Destination directory for generated files */
asn1p_t *asn = 0; /* An ASN.1 parsed tree */
int ret; /* Return value from misc functions */
int ch; /* Command line character */
int i; /* Index in some loops */
int exit_code = 0; /* Exit code */
destdir[0] = '\0';
/*
* Process command-line options.
*/
while((ch = getopt(ac, av, "EFf:g:hn:LPp:RS:vW:X")) != -1) switch(ch) {
while((ch = getopt(ac, av, "EFf:g:hn:LPp:RS:D:vW:X")) != -1) switch(ch) {
case 'E':
print_arg__print_out = 1;
break;
......@@ -179,6 +182,11 @@ main(int ac, char **av) {
case 'S':
skeletons_dir = optarg;
break;
case 'D':
strncat(destdir, optarg, PATH_MAX - 2); /* leave room for possible trailing '/' */
if(destdir[strlen(destdir)-1] != '/')
strcat(destdir, "/");
break;
case 'v':
fprintf(stderr, "ASN.1 Compiler, v" VERSION "\n" COPYRIGHT);
exit(0);
......@@ -241,7 +249,7 @@ main(int ac, char **av) {
ac -= optind;
av += optind;
} else {
const char *bin_name = a1c_basename(av[0]);
const char *bin_name = a1c_basename(av[0], NULL);
fprintf(stderr,
"%s: No input files specified. "
"Try '%s -h' for more information\n",
......@@ -370,7 +378,7 @@ main(int ac, char **av) {
* Compile the ASN.1 tree into a set of source files
* of another language.
*/
if(asn1_compile(asn, skeletons_dir, asn1_compiler_flags, ac + optind,
if(asn1_compile(asn, skeletons_dir, destdir, asn1_compiler_flags, ac + optind,
optind, av - optind)) {
exit_code = EX_SOFTWARE;
}
......@@ -499,6 +507,7 @@ usage(const char *av0) {
" -R Restrict output (tables only, no support code)\n"
" -S <dir> Directory with support (skeleton?) files\n"
" (Default is \"%s\")\n"
" -D <dir> Destination directory for generated files (default current dir)\n"
" -X Generate and print the XML DTD\n"
"\n"
......@@ -532,7 +541,7 @@ usage(const char *av0) {
" -print-lines Generate \"-- #line\" comments in -E output\n"
,
a1c_basename(av0), DATADIR);
a1c_basename(av0, NULL), DATADIR);
/* clang-format on */
exit(EX_USAGE);
}
......@@ -36,7 +36,7 @@ int mkstemp(char *template) {
#endif
FILE *
asn1c_open_file(const char *name, const char *ext, char **opt_tmpname) {
asn1c_open_file(const char* destdir, const char *name, const char *ext, char **opt_tmpname) {
char fname[PATH_MAX];
int created = 1;
#ifndef _WIN32
......@@ -49,7 +49,9 @@ asn1c_open_file(const char *name, const char *ext, char **opt_tmpname) {
/*
* Compute filenames.
*/
ret = snprintf(fname, sizeof(fname), "%s%s%s", name, ext,
ret = snprintf(fname, sizeof(fname), "%s%s%s%s",
opt_tmpname ? "" : destdir,
name, ext,
opt_tmpname ? ".XXXXXX" : "");
assert(ret > 0 && ret < (ssize_t)sizeof(fname));
......@@ -124,14 +126,20 @@ asn1c_open_file(const char *name, const char *ext, char **opt_tmpname) {
}
const char *
a1c_basename(const char *path) {
a1c_basename(const char *path, const char *destdir) {
static char strbuf[PATH_MAX];
const char *pend;
const char *name;
char *sbuf = strbuf;
if(destdir) {
strncpy(strbuf, destdir, PATH_MAX - 1);
strbuf[PATH_MAX - 1] = '\0';
sbuf = strbuf + strlen(strbuf);
}
pend = path + strlen(path);
if(pend == path) {
strcpy(strbuf, ".");
strcpy(sbuf, ".");
return strbuf;
}
......@@ -139,7 +147,7 @@ a1c_basename(const char *path) {
for(pend--; pend > path && *pend == '/'; pend--);
if(pend == path && *path == '/') {
strcpy(strbuf, "/");
strcpy(sbuf, "/");
return strbuf;
}
......@@ -150,8 +158,8 @@ a1c_basename(const char *path) {
return 0;
}
memcpy(strbuf, name, pend - name + 1);
strbuf[pend - name + 1] = '\0';
memcpy(sbuf, name, pend - name + 1);
sbuf[pend - name + 1] = '\0';
return strbuf;
}
......
......@@ -7,14 +7,14 @@
* its name returned in (*opt_tmpname).
* The (*opt_tmpname) should then be subsequently freed by free(3).
*/
FILE *asn1c_open_file(const char *base_part, const char *extension,
FILE *asn1c_open_file(const char *destdir, const char *base_part, const char *extension,
char **opt_tmpname);
/*
* Obtain base name and directory name of a path.
* Some systems have them in <libgen.h> as dirname(3) and basename(3).
*/
const char *a1c_basename(const char *path);
const char *a1c_basename(const char *path, const char* destdir);
const char *a1c_dirname(const char *path);
#endif /* ASN1C_COMPAT_H */
This diff is collapsed.
#ifndef ASN1C_SAVE_H
#define ASN1C_SAVE_H
int asn1c_save_compiled_output(arg_t *arg, const char *datadir,
int asn1c_save_compiled_output(arg_t *arg, const char *datadir, const char* destdir,
int argc, int optc, char **argv);
#endif /* ASN1C_SAVE_H */
......@@ -11,7 +11,7 @@ static int asn1c_attach_streams(asn1p_expr_t *expr);
static int asn1c_detach_streams(asn1p_expr_t *expr);
int
asn1_compile(asn1p_t *asn, const char *datadir, enum asn1c_flags flags,
asn1_compile(asn1p_t *asn, const char *datadir, const char *destdir, enum asn1c_flags flags,
int argc, int optc, char **argv) {
arg_t arg_s;
arg_t *arg = &arg_s;
......@@ -84,7 +84,7 @@ asn1_compile(asn1p_t *asn, const char *datadir, enum asn1c_flags flags,
/*
* Save or print out the compiled result.
*/
if(asn1c_save_compiled_output(arg, datadir, argc, optc, argv))
if(asn1c_save_compiled_output(arg, datadir, destdir, argc, optc, argv))
return -1;
TQ_FOR(mod, &(asn->modules), mod_next) {
......
......@@ -93,7 +93,7 @@ enum asn1c_flags {
/*
* Compile the ASN.1 specification.
*/
int asn1_compile(asn1p_t *asn, const char *datadir, enum asn1c_flags,
int asn1_compile(asn1p_t *asn, const char *datadir, const char *destdir, enum asn1c_flags,
int argc, int optc, char **argv);
void asn1c__add_pdu_type(const char *typename);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment