#include <nidl.h>
#include <signal.h>
#include <setjmp.h>
#include <driver.h>
#include <ast.h>
#include <command.h>
#include <errors.h>
#include <files.h>
#include <backend.h>
#include <message.h>
#include <frontend.h>
#if defined(MIA) && (defined(VMS) || defined(__osf__) || defined(DUMPERS))
static char *saved_header_file;
#endif
#define CALL(rtn) \
{ \
status = rtn; \
if (!status) \
{nidl_terminate();} \
}
static jmp_buf nidl_termination_jmp_buf;
void nidl_terminate (
void
)
{
extern void *errors;
if (errors == NULL)
{
message_print(NIDL_COMPABORT);
}
longjmp(nidl_termination_jmp_buf,1);
}
static long attempt_to_print_errors()
{
#if !defined(vms)
#ifndef _MSDOS
signal(SIGBUS,SIG_DFL);
#endif
signal(SIGSEGV,SIG_DFL);
signal(SIGFPE,SIG_DFL);
signal(SIGILL,SIG_DFL);
#endif
print_errors();
return 0;
}
static boolean open_fe_files
(
boolean *cmd_opt,
void **cmd_val,
FILE **lis_fid
)
{
*lis_fid = NULL;
return TRUE;
}
static boolean open_be_files
(
boolean *cmd_opt,
void **cmd_val,
FILE **h_fid,
FILE **caux_fid,
FILE **saux_fid,
FILE **cstub_fid,
FILE **sstub_fid,
AST_interface_n_t *int_p
)
{
AST_export_n_t *export_p;
boolean stubs_required;
boolean status;
boolean all_encode_decode;
*caux_fid = NULL;
*saux_fid = NULL;
*h_fid = NULL;
*cstub_fid = NULL;
*sstub_fid = NULL;
if (cmd_opt[opt_header])
{
if (cmd_opt[opt_verbose])
message_print(NIDL_INCLCREATE, (char *)cmd_val[opt_header]);
if (!cmd_opt[opt_confirm])
{
CALL(FILE_create((char *)cmd_val[opt_header], h_fid));
fprintf(*h_fid, IDL_VERSION_TEMPLATE, IDL_VERSION_TEXT);
}
}
stubs_required = FALSE;
all_encode_decode = TRUE;
if (!AST_LOCAL_SET(int_p))
{
for (export_p = int_p->exports;
export_p != NULL;
export_p = export_p->next)
if (export_p->kind == AST_operation_k)
{
stubs_required = TRUE;
if (!AST_ENCODE_SET(export_p->thing_p.exported_operation)
&& !AST_DECODE_SET(export_p->thing_p.exported_operation))
{
all_encode_decode = FALSE;
break;
}
}
}
if (stubs_required
&& cmd_opt[opt_cstub]
&& cmd_opt[opt_emit_cstub])
{
if (cmd_opt[opt_verbose])
message_print(NIDL_STUBCREATE, (char *)cmd_val[opt_cstub]);
if (!cmd_opt[opt_confirm])
{
CALL(FILE_create((char *)cmd_val[opt_cstub], cstub_fid))
fprintf(*cstub_fid, IDL_VERSION_TEMPLATE, IDL_VERSION_TEXT);
}
}
else
cmd_opt[opt_cstub] = FALSE;
if (stubs_required
&& !all_encode_decode
&& cmd_opt[opt_sstub]
&& cmd_opt[opt_emit_sstub])
{
if (cmd_opt[opt_verbose])
message_print(NIDL_STUBCREATE, (char *)cmd_val[opt_sstub]);
if (!cmd_opt[opt_confirm])
{
CALL(FILE_create((char *)cmd_val[opt_sstub], sstub_fid))
fprintf(*sstub_fid, IDL_VERSION_TEMPLATE, IDL_VERSION_TEXT);
}
}
else
cmd_opt[opt_sstub] = FALSE;
#ifdef MIA
if (getenv("IDL_GEN_AUX_FILES") != NULL)
#endif
{
if (cmd_opt[opt_caux]
&& (int_p->sp_types != NULL
|| int_p->ool_types != NULL))
{
if (cmd_opt[opt_verbose])
message_print(NIDL_STUBCREATE, (char *)cmd_val[opt_caux]);
if (!cmd_opt[opt_confirm])
{
CALL(FILE_create((char *)cmd_val[opt_caux], caux_fid))
fprintf(*caux_fid, IDL_VERSION_TEMPLATE, IDL_VERSION_TEXT);
#ifdef MIA
fprintf(*caux_fid,"static int idl_aux_stub = 0;\n");
#endif
}
}
else
cmd_opt[opt_caux] = FALSE;
if (cmd_opt[opt_saux]
&& (int_p->sp_types != NULL
|| int_p->ool_types != NULL
|| int_p->pipe_types != NULL))
{
if (cmd_opt[opt_verbose])
message_print(NIDL_STUBCREATE, (char *)cmd_val[opt_saux]);
if (!cmd_opt[opt_confirm])
{
CALL(FILE_create((char *)cmd_val[opt_saux], saux_fid))
fprintf(*saux_fid, IDL_VERSION_TEMPLATE, IDL_VERSION_TEXT);
#ifdef MIA
fprintf(*saux_fid,"static int idl_aux_stub = 0;\n");
#endif
}
}
else
cmd_opt[opt_saux] = FALSE;
}
#ifdef MIA
else
{
cmd_opt[opt_caux] = FALSE;
cmd_opt[opt_saux] = FALSE;
}
#endif
return (*caux_fid != NULL
|| *saux_fid != NULL
|| *h_fid != NULL
|| *cstub_fid != NULL
|| *sstub_fid != NULL);
}
static int stub_compile
(
boolean *cmd_opt,
void **cmd_val,
int opt_file,
FILE *fid,
char *compile_cmd
)
{
char compile_opt[max_string_len];
char filespec[PATH_MAX];
if (!cmd_opt[opt_file])
return pgm_ok;
if (cmd_opt[opt_verbose] && error_count == 0 && cmd_opt[opt_confirm])
message_print(NIDL_STUBCOMPILE, (char *)cmd_val[opt_file]);
if (cmd_opt[opt_confirm] || fid == NULL)
return pgm_ok;
compile_opt[0] = '\0';
FILE_parse((char *)cmd_val[opt_file], filespec, (char *)NULL, (char *)NULL);
if (!FILE_is_cwd(filespec))
#ifdef CC_OPT_OBJECT
if (strcmp((char *)cmd_val[opt_cc_cmd], CC_DEF_CMD) == 0)
{
FILE_form_filespec((char *)NULL, (char *)NULL, OBJ_FILETYPE,
(char *)cmd_val[opt_file], filespec);
sprintf(compile_opt, "%s%s", CC_OPT_OBJECT, filespec);
}
else
#endif
message_print(NIDL_OUTDIRIGN, (char *)cmd_val[opt_file]);
return FILE_execute_cmd(compile_cmd, compile_opt, (char *)cmd_val[opt_file],
((cmd_opt[opt_verbose]) ? NIDL_STUBCOMPILE: 0)
);
}
static void close_files
(
FILE *lis_fid,
FILE *h_fid,
FILE *caux_fid,
FILE *saux_fid,
FILE *cstub_fid,
FILE *sstub_fid
)
{
if (lis_fid != NULL) fclose(lis_fid);
if (h_fid != NULL) fclose(h_fid);
if (caux_fid != NULL) fclose(caux_fid);
if (saux_fid != NULL) fclose(saux_fid);
if (cstub_fid != NULL) fclose(cstub_fid);
if (sstub_fid != NULL) fclose(sstub_fid);
}
static boolean init(char *image_name)
{
message_open(image_name);
return TRUE;
}
static boolean cleanup(void)
{
message_close();
return TRUE;
}
boolean DRIVER_main
(
int argc,
char **argv
)
{
boolean *cmd_opt;
void **cmd_val;
STRTAB_str_t idl_sid;
FILE *lis_fid;
FILE *h_fid;
FILE *caux_fid;
FILE *saux_fid;
FILE *cstub_fid;
FILE *sstub_fid;
AST_interface_n_t *int_p;
boolean status = TRUE;
int tmpsts;
lis_fid = NULL;
h_fid = NULL;
caux_fid = NULL;
saux_fid = NULL;
cstub_fid = NULL;
sstub_fid = NULL;
cmd_opt = NULL;
cmd_val = NULL;
#ifdef vms
VAXC$ESTABLISH(attempt_to_print_errors);
#else
#ifndef _MSDOS
signal(SIGBUS, (void (*)())attempt_to_print_errors);
#endif
signal(SIGSEGV, (void (*)())attempt_to_print_errors);
signal(SIGFPE, (void (*)())attempt_to_print_errors);
signal(SIGILL, (void (*)())attempt_to_print_errors);
#endif
if (setjmp(nidl_termination_jmp_buf) == 0)
{
CALL(init(argv[0]));
CALL(CMD_parse_args(argc-1, &argv[1], &cmd_opt, &cmd_val, &idl_sid));
if (!cmd_opt[opt_confirm])
CALL(open_fe_files(cmd_opt, cmd_val, &lis_fid));
CALL(FE_main(cmd_opt, cmd_val, idl_sid, &int_p));
if (cmd_opt == NULL || !cmd_opt[opt_confirm])
print_errors();
if (open_be_files(cmd_opt, cmd_val, &h_fid, &caux_fid,
&saux_fid, &cstub_fid, &sstub_fid, int_p))
{
char filename[PATH_MAX];
char filetype[PATH_MAX];
char *saved_header;
if (cmd_val[opt_header] != NULL)
{
FILE_parse((char *)cmd_val[opt_header], NULL, filename, filetype);
strcat(filename, filetype);
}
else
filename[0] = '\0';
saved_header = (char *)cmd_val[opt_header];
#if defined(MIA) && (defined(VMS) || defined(__osf__) || defined(DUMPERS))
saved_header_file = saved_header;
#endif
cmd_val[opt_header] = (void *)filename;
CALL(BE_main(cmd_opt, cmd_val, h_fid, caux_fid, saux_fid,
cstub_fid, sstub_fid, int_p));
cmd_val[opt_header] = (void *)saved_header;
}
}
else status = FALSE;
if (cmd_opt == NULL || !cmd_opt[opt_confirm])
print_errors();
close_files(lis_fid, h_fid, caux_fid, saux_fid,
cstub_fid, sstub_fid);
if (cmd_opt != NULL && cmd_opt[opt_keep_obj] && status)
{
char **idir_list;
char idir_opt[max_string_len];
char cc_cmd[max_string_len];
#ifdef VMS
boolean paren_flag;
#endif
#if defined(UNIX) || defined(VMS)
idir_list = (char **)cmd_val[opt_idir];
idir_opt[0] = '\0';
#ifdef VMS
if (*idir_list || cmd_opt[opt_out])
{
paren_flag = TRUE;
strcat(idir_opt, " /INCLUDE_DIRECTORY=(");
}
else
paren_flag = FALSE;
#endif
if (cmd_opt[opt_out])
{
#ifdef UNIX
strcat(idir_opt, " -I");
#endif
strcat(idir_opt, (char *)cmd_val[opt_out]);
#ifdef VMS
strcat(idir_opt, ",");
#endif
}
while (*idir_list)
{
#ifdef UNIX
strcat(idir_opt, " -I");
#endif
if (strcmp(*idir_list, DEFAULT_IDIR) == 0)
strcat(idir_opt, DEFAULT_H_IDIR);
else
strcat(idir_opt, *idir_list);
idir_list++;
#ifdef VMS
strcat(idir_opt, ",");
#endif
}
#ifdef VMS
if (paren_flag)
idir_opt[strlen(idir_opt)-1] = ')';
#endif
#endif
#ifdef PASS_I_DIRS_TO_CC
sprintf(cc_cmd, "%s %s %s", (char *)cmd_val[opt_cc_cmd],
(char *)cmd_val[opt_cc_opt], idir_opt);
#else
sprintf(cc_cmd, "%s %s", (char *)cmd_val[opt_cc_cmd],
(char *)cmd_val[opt_cc_opt]);
#endif
tmpsts = stub_compile(cmd_opt, cmd_val, opt_cstub, cstub_fid, cc_cmd);
if (ERROR_STATUS(tmpsts)) status = FALSE;
else
tmpsts = stub_compile(cmd_opt, cmd_val, opt_sstub, sstub_fid, cc_cmd);
if (ERROR_STATUS(tmpsts)) status = FALSE;
else
tmpsts = stub_compile(cmd_opt, cmd_val, opt_caux, caux_fid, cc_cmd);
if (ERROR_STATUS(tmpsts)) status = FALSE;
else
tmpsts = stub_compile(cmd_opt, cmd_val, opt_saux, saux_fid, cc_cmd);
if (ERROR_STATUS(tmpsts)) status = FALSE;
}
if (cmd_opt != NULL && !cmd_opt[opt_keep_c] && status)
{
if (cmd_opt[opt_cstub])
{
if (cmd_opt[opt_verbose] && error_count == 0)
message_print(NIDL_STUBDELETE, (char *)cmd_val[opt_cstub]);
if (!cmd_opt[opt_confirm] && cstub_fid)
FILE_delete((char *)cmd_val[opt_cstub]);
}
if (cmd_opt[opt_sstub])
{
if (cmd_opt[opt_verbose] && error_count == 0)
message_print(NIDL_STUBDELETE, (char *)cmd_val[opt_sstub]);
if (!cmd_opt[opt_confirm] && sstub_fid)
FILE_delete((char *)cmd_val[opt_sstub]);
}
if (cmd_opt[opt_caux])
{
if (cmd_opt[opt_verbose] && error_count == 0)
message_print(NIDL_STUBDELETE, (char *)cmd_val[opt_caux]);
if (!cmd_opt[opt_confirm] && caux_fid)
FILE_delete((char *)cmd_val[opt_caux]);
}
if (cmd_opt[opt_saux])
{
if (cmd_opt[opt_verbose] && error_count == 0)
message_print(NIDL_STUBDELETE, (char *)cmd_val[opt_saux]);
if (!cmd_opt[opt_confirm] && saux_fid)
FILE_delete((char *)cmd_val[opt_saux]);
}
}
if (!cleanup()) status = FALSE;
return status;
}