vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1c_internal.h" |
| 2 | |
| 3 | static int asn1c_dump_streams(arg_t *arg); |
| 4 | static int asn1c_print_streams(arg_t *arg); |
| 5 | static int asn1c_save_streams(arg_t *arg); |
| 6 | static int asn1c_copy_over(arg_t *arg, char *path); |
| 7 | |
| 8 | int |
| 9 | asn1c_save_compiled_output(arg_t *arg, const char *datadir) { |
| 10 | |
| 11 | (void)datadir; |
| 12 | |
| 13 | TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) { |
| 14 | TQ_FOR(arg->expr, &(arg->mod->members), next) { |
| 15 | if(asn1_lang_map[arg->expr->meta_type] |
| 16 | [arg->expr->expr_type].type_cb) { |
| 17 | if(asn1c_dump_streams(arg)) |
| 18 | return -1; |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | /* |
| 24 | * Dump out the Makefile template and the rest of the support code. |
| 25 | */ |
| 26 | if((arg->flags & A1C_PRINT_COMPILED) == 0 |
| 27 | && (arg->flags & A1C_OMIT_SUPPORT_CODE) == 0) { |
| 28 | glob_t pg; |
| 29 | FILE *mkf; |
| 30 | char *p; |
| 31 | int i; |
| 32 | |
| 33 | i = strlen(datadir) + sizeof("/*.[ch]"); |
| 34 | p = alloca(i); |
| 35 | snprintf(p, i, "%s/*.[ch]", datadir); |
| 36 | |
| 37 | memset(&pg, 0, sizeof(pg)); |
| 38 | if(glob(p, GLOB_ERR |
| 39 | #ifdef GLOB_TILDE |
| 40 | | GLOB_TILDE |
| 41 | #endif /* GLOB_TILDE */ |
| 42 | , NULL, &pg)) { |
| 43 | fprintf(stderr, |
| 44 | "Bad skeletons directory (-S) %s: %s\n", |
| 45 | datadir, strerror(errno)); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | mkf = asn1c_open_file(arg, "Makefile.am", ".sample"); |
| 50 | if(mkf == NULL) { |
| 51 | globfree(&pg); |
| 52 | perror("Makefile.am.sample"); |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | fprintf(mkf, "ASN_SRCS="); |
| 57 | TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) { |
| 58 | TQ_FOR(arg->expr, &(arg->mod->members), next) { |
| 59 | if(asn1_lang_map[arg->expr->meta_type] |
| 60 | [arg->expr->expr_type].type_cb) { |
| 61 | fprintf(mkf, "\t\\\n\t%s.c %s.h", |
| 62 | arg->expr->Identifier, |
| 63 | arg->expr->Identifier); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | for(i = 0; i < pg.gl_pathc; i++) { |
| 69 | if(asn1c_copy_over(arg, pg.gl_pathv[i])) { |
| 70 | fprintf(mkf, ">>>ABORTED<<<"); |
| 71 | fclose(mkf); |
| 72 | globfree(&pg); |
| 73 | return -1; |
| 74 | } else { |
| 75 | fprintf(mkf, "\t\\\n\t%s", |
| 76 | basename(pg.gl_pathv[i])); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | fprintf(mkf, "\n\n"); |
| 81 | fprintf(mkf, "lib_LTLIBRARIES=libsomething.la\n"); |
| 82 | fprintf(mkf, "libsomething_la_SOURCES=${ASN_SRCS}\n"); |
| 83 | fclose(mkf); |
| 84 | fprintf(stderr, "Generated Makefile.am.sample\n"); |
| 85 | globfree(&pg); |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Dump the streams. |
| 93 | */ |
| 94 | static int |
| 95 | asn1c_dump_streams(arg_t *arg) { |
| 96 | if(arg->flags & A1C_PRINT_COMPILED) { |
| 97 | return asn1c_print_streams(arg); |
| 98 | } else { |
| 99 | return asn1c_save_streams(arg); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static int |
| 104 | asn1c_print_streams(arg_t *arg) { |
| 105 | compiler_streams_t *cs = arg->expr->data; |
| 106 | asn1p_expr_t *expr = arg->expr; |
| 107 | int i; |
| 108 | |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 109 | for(i = 1; i < OT_MAX; i++) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | out_chunk_t *ot; |
| 111 | if(TQ_FIRST(&cs->targets[i]) == NULL) |
| 112 | continue; |
| 113 | |
| 114 | printf("\n/*** <<< %s [%s] >>> ***/\n\n", |
| 115 | _compiler_stream2str[i], |
| 116 | expr->Identifier); |
| 117 | |
| 118 | TQ_FOR(ot, &(cs->targets[i]), next) { |
| 119 | fwrite(ot->buf, ot->len, 1, stdout); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int |
| 127 | asn1c_save_streams(arg_t *arg) { |
| 128 | asn1p_expr_t *expr = arg->expr; |
| 129 | compiler_streams_t *cs = expr->data; |
| 130 | out_chunk_t *ot; |
| 131 | FILE *fp_c, *fp_h; |
| 132 | char *header_id; |
| 133 | |
| 134 | if(cs == NULL) { |
| 135 | fprintf(stderr, "Cannot compile %s at line %d\n", |
| 136 | expr->Identifier, expr->_lineno); |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | fp_c = asn1c_open_file(arg, expr->Identifier, ".c"); |
| 141 | fp_h = asn1c_open_file(arg, expr->Identifier, ".h"); |
| 142 | if(fp_c == NULL || fp_h == NULL) { |
| 143 | if(fp_c) fclose(fp_c); /* lacks unlink() */ |
| 144 | if(fp_h) fclose(fp_h); /* lacks unlink() */ |
| 145 | return -1; |
| 146 | } |
| 147 | |
vlm | f18d856 | 2004-06-03 05:06:25 +0000 | [diff] [blame] | 148 | fprintf(fp_c, |
| 149 | "/*\n" |
| 150 | " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n" |
| 151 | " * From ASN.1 module \"%s\" found in \"%s\"\n" |
| 152 | " */\n\n", |
| 153 | arg->mod->Identifier, |
| 154 | arg->mod->source_file_name |
| 155 | ); |
| 156 | fprintf(fp_h, |
| 157 | "/*\n" |
| 158 | " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n" |
| 159 | " * From ASN.1 module \"%s\" found in \"%s\"\n" |
| 160 | " */\n\n", |
| 161 | arg->mod->Identifier, |
| 162 | arg->mod->source_file_name |
| 163 | ); |
| 164 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 165 | header_id = alloca(strlen(expr->Identifier) + 1); |
| 166 | if(1) { |
| 167 | char *src, *dst; |
| 168 | for(src = expr->Identifier, dst = header_id; |
| 169 | (*dst=*src); src++, dst++) |
| 170 | if(!isalnum(*src)) *dst = '_'; |
| 171 | *dst = '\0'; |
| 172 | } |
| 173 | |
| 174 | fprintf(fp_h, |
| 175 | "#ifndef\t_%s_H_\n" |
| 176 | "#define\t_%s_H_\n" |
| 177 | "\n", header_id, header_id); |
| 178 | |
vlm | 8098252 | 2004-08-10 01:14:29 +0000 | [diff] [blame] | 179 | fprintf(fp_h, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n"); |
| 180 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 181 | fprintf(fp_h, "#include <constr_TYPE.h>\n\n"); |
| 182 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 183 | TQ_FOR(ot, &(cs->targets[OT_INCLUDES]), next) |
| 184 | fwrite(ot->buf, ot->len, 1, fp_h); |
| 185 | fprintf(fp_h, "\n"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 186 | TQ_FOR(ot, &(cs->targets[OT_DEPS]), next) |
| 187 | fwrite(ot->buf, ot->len, 1, fp_h); |
| 188 | fprintf(fp_h, "\n"); |
| 189 | TQ_FOR(ot, &(cs->targets[OT_TYPE_DECLS]), next) |
| 190 | fwrite(ot->buf, ot->len, 1, fp_h); |
| 191 | fprintf(fp_h, "\n"); |
| 192 | TQ_FOR(ot, &(cs->targets[OT_FUNC_DECLS]), next) |
| 193 | fwrite(ot->buf, ot->len, 1, fp_h); |
| 194 | |
vlm | 33a4ff1 | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 195 | fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n" |
| 196 | "#endif\t/* _%s_H_ */\n", |
| 197 | header_id); |
| 198 | |
| 199 | fprintf(fp_c, "#include <%s.h>\n\n", expr->Identifier); /* Myself */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 200 | TQ_FOR(ot, &(cs->targets[OT_STAT_DEFS]), next) |
| 201 | fwrite(ot->buf, ot->len, 1, fp_c); |
| 202 | TQ_FOR(ot, &(cs->targets[OT_CODE]), next) |
| 203 | fwrite(ot->buf, ot->len, 1, fp_c); |
| 204 | |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 205 | assert(OT_MAX == 7); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 206 | |
| 207 | fclose(fp_c); |
| 208 | fclose(fp_h); |
| 209 | fprintf(stderr, "Compiled %s.c\n", expr->Identifier); |
| 210 | fprintf(stderr, "Compiled %s.h\n", expr->Identifier); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static int |
| 215 | asn1c_copy_over(arg_t *arg, char *path) { |
| 216 | char *fname = basename(path); |
| 217 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 218 | (void)arg; /* Unused argument */ |
| 219 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 220 | if(symlink(path, fname)) { |
| 221 | if(errno == EEXIST) { |
| 222 | struct stat sb1, sb2; |
| 223 | if(stat(path, &sb1) == 0 |
| 224 | && stat(fname, &sb2) == 0 |
| 225 | && sb1.st_dev == sb2.st_dev |
| 226 | && sb1.st_ino == sb2.st_ino) { |
| 227 | /* |
| 228 | * Nothing to do. |
| 229 | */ |
| 230 | fprintf(stderr, |
| 231 | "File %s is already here as %s\n", |
| 232 | path, fname); |
| 233 | return 0; |
| 234 | } else { |
| 235 | fprintf(stderr, |
| 236 | "Retaining local %s (%s suggested)\n", |
| 237 | fname, path); |
| 238 | return 0; |
| 239 | } |
| 240 | } else { |
| 241 | fprintf(stderr, "Symlink %s -> %s failed: %s\n", |
| 242 | path, fname, strerror(errno)); |
| 243 | return -1; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | fprintf(stderr, "Symlinked %s\t-> %s\n", path, fname); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |