00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 static jmp_buf MXT_exception_env;
00025 static int MXT_exception_valid = 0;
00026 static char MXT_errstr[256];
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 void
00038 mxt_ErrHandler(const char *msg)
00039 {
00040 #ifdef DEBUG
00041 printf(" in mxt_ErrHandler, returning to %s.\n",
00042 MXT_exception_valid ? "caller" : "system");
00043 #endif
00044
00045 if (MXT_exception_valid) {
00046
00047 if (msg) {
00048 strncpy(MXT_errstr, msg, sizeof(MXT_errstr));
00049 MXT_errstr[sizeof(MXT_errstr)-1] = '\0';
00050 }
00051 longjmp(MXT_exception_env, 1);
00052 } else {
00053
00054 fprintf(stderr, "mxt_ErrHandler: no matching setjmp: should not happen.\n");
00055 if (msg)
00056 fprintf(stderr, "\tError message is: %s.\n", msg);
00057 fprintf(stderr, "\tReturning to system.\n");
00058 exit(1);
00059 }
00060 }
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 char *
00072 mxt_mexDispatcher(mexfn_t *mexfn,
00073 const char *progname,
00074 int verbosity,
00075 int nlhs, mxArray **plhs, int nrhs, const mxArray **prhs)
00076 {
00077 char *retval;
00078 jmp_buf save_MXT_exception_env;
00079 int save_MXT_exception_valid;
00080 int i;
00081
00082 strcpy(mex2c_phase, "mexFunction dispatcher");
00083 mex2c_progname = strdup(progname);
00084
00085
00086 save_MXT_exception_valid = MXT_exception_valid;
00087 memcpy(save_MXT_exception_env, MXT_exception_env, sizeof(MXT_exception_env));
00088
00089
00090 strcpy(mex2c_phase, "mexFunction internal");
00091 MXT_exception_valid = 1;
00092 if (setjmp(MXT_exception_env) == 0) {
00093 #ifdef DEBUG
00094 printf(" calling mexfunction %s...\n", progname);
00095 #endif
00096 (*mexfn)(nlhs, plhs, nrhs, prhs);
00097 #ifdef DEBUG
00098 printf(" returned normally from mexfunction %s.\n", progname);
00099 #endif
00100
00101 retval = NULL;
00102 } else {
00103
00104 if (verbosity) {
00105 fprintf(stderr, "dispatcher: encountered error within mexfunction `%s'.\n",
00106 progname);
00107 fprintf(stderr, "\tError message returned by `%s' follows:\n\t%s\n",
00108 progname, MXT_errstr);
00109 }
00110
00111 for (i = 0; i < nlhs; i++)
00112 plhs[i] = NULL;
00113
00114 retval = MXT_errstr;
00115 }
00116
00117 MXT_exception_valid = save_MXT_exception_valid;
00118 memcpy(MXT_exception_env, save_MXT_exception_env, sizeof(MXT_exception_env));
00119 return retval;
00120 }
00121
00122