1 #include <../../nrnconf.h>
3 #include "../nrnpython/nrnpython_config.h"
22 #include "../nrniv/backtrace_utils.h"
32 #if defined(USE_PYTHON)
33 int use_python_interpreter = 0;
41 #define use_rl_getc_function
44 #if carbon || defined(MINGW)
49 #if HAVE_FEENABLEEXCEPT
50 #define NRN_FLOAT_EXCEPTION 1
52 #define NRN_FLOAT_EXCEPTION 0
55 #if NRN_FLOAT_EXCEPTION
56 #if !defined(__USE_GNU)
60 #define FEEXCEPT (FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW)
61 static void matherr1(
void) {
63 switch (fegetexcept()) {
65 fprintf(stderr,
"Floating exception: Divide by zero\n");
68 fprintf(stderr,
"Floating exception: Invalid (no well defined result\n");
71 fprintf(stderr,
"Floating exception: Overflow\n");
84 #if NRN_FLOAT_EXCEPTION
86 result = fedisableexcept(FEEXCEPT);
88 result = feenableexcept(FEEXCEPT);
99 static unsigned long usec[30];
100 static unsigned long oldusec[30];
101 static struct timeval tp;
103 gettimeofday(&tp, 0);
104 oldusec[
i] = tp.tv_usec;
107 gettimeofday(&tp, 0);
108 if (tp.tv_usec > oldusec[
i]) {
109 usec[
i] += tp.tv_usec - oldusec[
i];
114 for (
i=0;
i < 30; ++
i) {
116 printf(
"sec[%d]=%g\n",
i, ((
double)usec[
i])/1000000.);
158 #define CHAR signed char
162 #define TMPBUFSIZE 512
176 #define EPS hoc_epsilon
188 #include <execinfo.h>
205 #if defined(WIN32) || MAC
211 extern void hoc_win32_cleanup();
214 static int follow(
int expect,
int ifyes,
int ifno);
221 printf(
"NEURON exiting abnormally, press return to quit\n");
229 int hoc_retreat_flag;
230 #define RETREAT_SIGNAL SIGHUP
232 static RETSIGTYPE retreat_handler(
int sig)
235 if (hoc_retreat_flag++) {
239 if (!
lookup(
"linda_retreat")) {
240 hoc_retreat_flag = 0;
245 IGNORE(signal(RETREAT_SIGNAL, retreat_handler));
248 void hoc_retreat(
void) {
255 #if defined(WIN32) || defined(MAC)
256 #define HAS_SIGPIPE 0
258 #define HAS_SIGPIPE 1
263 fprintf(stderr,
"writing to a broken pipe\n");
273 while ((
c =
Getc(
fin)) ==
' ' ||
c ==
'\t') {
283 #define YYNEEDMORE -3
288 static int lexstate = 0;
297 char* s =
buf + strlen(
buf);
299 while (isdigit(
c =
Getc(
fin)) && (s -
buf) < 200) {
329 if ((
c =
getnb()) == EOF) {
332 if (
c ==
'/' &&
follow(
'*', 1, 0))
339 acterror(
"comment not complete",
" in cbuf");
344 if (
c ==
'.' || isdigit(
c))
360 if (*npt ==
'.' && !isdigit(npt[1])) {
364 if (
c ==
'E' ||
c ==
'e') {
365 if (isdigit(
c =
Getc(
fin)) ||
c ==
'+' ||
c ==
'-') {
373 IGNORE(sscanf(npt,
"%lf", &d));
379 if (isalpha(
c) ||
c ==
'_') {
381 char sbuf[256], *
p = sbuf;
383 if (
p >= (sbuf + 255)) {
388 }
while ((
c =
Getc(
fin)) != EOF && (isalnum(
c) ||
c ==
'_'));
391 if (strncmp(sbuf,
"__nrnsec_0x", 11) == 0) {
393 return INTERNALSECTIONNAME;
403 if (strcmp(sbuf,
"_pysec") == 0) {
406 "Must be of form _pysec.secname where secname is the literal result of "
407 "nrn.Section.name() .",
422 "Must be of form _pysec.cellname.secname where cellname.secname is the "
423 "literal result of nrn.Section.name() .",
434 if ((s =
lookup(sbuf)) == 0)
437 return s->
type == UNDEF ? VAR : s->
type;
453 n = 10 *
n +
c -
'0';
456 acterror(
"strange $...", (
char*) 0);
472 if (
n >= sbuf->
size - 1) {
493 return follow(
'=', GE, GT);
495 return follow(
'=', LE, LT);
510 if (
follow(
'=', EQ,
'=') == EQ) {
532 return follow(
'\n',
'\n',
'\n');
541 }
else if (
follow(
'=', 1, 0)) {
553 static char transtab[] =
"b\bf\fn\nr\rt\t";
557 if (islower(
c) && strchr(transtab,
c))
558 return strchr(transtab,
c)[1];
562 static int follow(
int expect,
int ifyes,
int ifno)
576 #if defined(__TURBOC__)
581 #if defined(__TURBOC__)
590 if ((
OPVAL(sp) = (
double*)
hoc_Ecalloc((
unsigned)
i,
sizeof(
double))) == (
double*) 0) {
592 Fprintf(stderr,
"Not enough space for array %s\n", sp->
name);
596 #if defined(__TURBOC__)
602 double total, subscpt;
615 total = total * subscpt;
642 if (sp->
type == VAR) {
655 if (a->
a_varn != (
unsigned*) 0)
656 free((
char*) (a->
a_varn));
665 acterror(s,
"used outside definition");
714 #if defined(__GO32__)
718 hoc_outtext(
"Error:");
735 (*oc_jump_target_)();
785 const size_t nframes = 12;
786 void* frames[nframes];
788 char** bt_strings =
NULL;
790 size_t funcname_size = 256;
792 char* symbol =
static_cast<char*
>(malloc(
sizeof(
char) * funcname_size));
794 char* funcname =
static_cast<char*
>(malloc(
sizeof(
char) * funcname_size));
796 char* offset =
static_cast<char*
>(malloc(
sizeof(
char) * 10));
800 size = backtrace(frames, nframes);
803 Fprintf(stderr,
"Backtrace:\n");
805 bt_strings = backtrace_symbols(frames, size);
809 for (
i = 2;
i < size; ++
i) {
815 Fprintf(stderr,
"\t%s : %s+%s\n", bt_strings[
i], funcname, offset);
817 Fprintf(stderr,
"\t%s : %s()+%s\n", bt_strings[
i], symbol, offset);
820 Fprintf(stderr,
"\t%s\n", bt_strings[
i]);
829 Fprintf(stderr,
"No backtrace info available.\n");
840 #if NRN_FLOAT_EXCEPTION
843 Fprintf(stderr,
"Floating point exception\n");
849 execerror(
"Floating point exception.", (
char*) 0);
853 RETSIGTYPE sigsegvcatch(
int sig)
855 Fprintf(stderr,
"Segmentation violation\n");
866 RETSIGTYPE sigbuscatch(
int sig) {
867 Fprintf(stderr,
"Bus error\n");
873 execerror(
"Aborting. ",
"See $NEURONHOME/lib/help/oc.help");
878 return (
int) getpid();
927 "Copyright 1992 - Michael Hines, Neurobiology Dept., DUMC, Durham, NC. 27710\n");
939 #if defined(__GO32__)
949 hs->
buf =
static_cast<char*
>(
emalloc(size + 1));
985 int exit_status = EXIT_SUCCESS;
987 extern void hoc_set_unhandled_exception_filter();
988 hoc_set_unhandled_exception_filter();
1001 signal(RETREAT_SIGNAL, retreat_handler);
1010 if (setjmp(
begin)) {
1021 if (
argc > 2 && strcmp(
argv[1],
"-bbs_nhost") == 0) {
1026 if (
argc > 1 &&
argv[1][0] !=
'-') {
1044 static const char* stdinonly[] = {
"-"};
1061 #include <sys/select.h>
1062 static pthread_t* inputReady_;
1063 static pthread_mutex_t inputMutex_;
1064 static pthread_cond_t inputCond_;
1065 static int inputReadyFlag_;
1067 void* inputReadyThread(
void*
input) {
1074 FD_SET(fileno(stdin), &readfds);
1076 i = select(1, &readfds, 0, 0, 0);
1077 pthread_testcancel();
1078 if (FD_ISSET(fileno(stdin), &readfds)) {
1081 printf(
"Discarding input til Dialog is closed.\n");
1082 read(fileno(stdin), &
c, 1);
1086 pthread_mutex_lock(&inputMutex_);
1087 pthread_cond_wait(&inputCond_, &inputMutex_);
1088 pthread_mutex_unlock(&inputMutex_);
1090 printf(
"inputReadyThread done\n");
1095 static pthread_t* inputReady_;
1096 static pthread_mutex_t inputMutex_;
1097 static pthread_cond_t inputCond_;
1098 static int inputReadyFlag_;
1099 static int inputReadyVal_;
1100 extern "C" int getch();
1102 void* inputReadyThread(
void*
input) {
1107 pthread_testcancel();
1112 printf(
"Discarding input til Dialog is closed.\n");
1113 read(fileno(stdin), &
c, 1);
1120 pthread_mutex_lock(&inputMutex_);
1121 inputReadyFlag_ = 1;
1124 pthread_cond_wait(&inputCond_, &inputMutex_);
1125 pthread_mutex_unlock(&inputMutex_);
1127 printf(
"inputReadyThread done\n");
1133 #if defined(USE_PYTHON)
1135 (*p_nrnpython_start)(0);
1149 hoc_win32_cleanup();
1164 if (0 && inputReady_) {
1165 pthread_cancel(*inputReady_);
1166 pthread_kill(*inputReady_, SIGHUP);
1167 pthread_join(*inputReady_, 0);
1172 #if defined(USE_PYTHON)
1179 (*p_nrnpython_finalize)();
1187 static const char* double_at2space(
const char*
infile) {
1192 for (cp1 =
infile; *cp1; ++cp1) {
1193 if (*cp1 ==
'@' && cp1[1] ==
'@') {
1202 for (cp1 =
infile, cp2 =
buf; *cp1; ++cp1, ++cp2) {
1203 if (*cp1 ==
'@' && cp1[1] ==
'@') {
1222 if (
gargc == 0 && cygonce == 0) {
1227 #if defined(USE_PYTHON)
1228 return use_python_interpreter ? 0 : 1;
1239 #if defined(USE_PYTHON)
1240 return use_python_interpreter ? 0 : 1;
1258 char* cp = strdup(
infile + 1);
1259 for (++cp; *cp; ++cp) {
1276 #if defined(USE_PYTHON)
1277 if (use_python_interpreter) {
1287 if (strcmp(
infile,
"-") == 0) {
1291 }
else if (strcmp(
infile,
"-parallel") == 0) {
1296 }
else if (strcmp(
infile,
"-c") == 0) {
1321 (*p_nrnpy_pyrun)(
infile);
1363 signals[2] = signal(SIGSEGV, sigsegvcatch);
1366 signals[3] = signal(SIGBUS, sigbuscatch);
1388 if (setjmp(
begin)) {
1391 return EXIT_FAILURE;
1407 return EXIT_SUCCESS;
1445 if (cp[-1] ==
'\n') {
1450 if (cp[-1] !=
'\n') {
1543 for (cp =
cbuf; cp < (
cbuf +
n); ++cp) {
1544 if (!isprint((
int) (*cp)) && !isspace((
int) (*cp))) {
1546 "%scharacter \\%03o at position %ld is not printable\n",
1548 ((
int) (*cp) & 0xff),
1555 for (cp =
cbuf; cp !=
ctp; cp++) {
1635 }
while (
i ==
'\n');
1639 #if defined(__GO32__)
1640 #define INTERVIEWS 1
1643 #define INTERVIEWS 1
1646 #define INTERVIEWS 1
1660 extern int (*rl_event_hook)(
void);
1661 static int event_hook(
void) {
1663 inputReady_ = (pthread_t*)
emalloc(
sizeof(pthread_t));
1664 pthread_mutex_init(&inputMutex_, 0);
1665 pthread_cond_init(&inputCond_, 0);
1666 pthread_create(inputReady_, 0, inputReadyThread, 0);
1668 pthread_mutex_unlock(&inputMutex_);
1672 pthread_mutex_lock(&inputMutex_);
1673 pthread_cond_signal(&inputCond_);
1678 extern "C" int (*rl_getc_function)(
void);
1679 extern "C" int rl_getc(
void);
1680 static int getc_hook(
void) {
1684 inputReady_ = (pthread_t*)
emalloc(
sizeof(pthread_t));
1685 pthread_mutex_init(&inputMutex_, 0);
1686 pthread_cond_init(&inputCond_, 0);
1687 pthread_create(inputReady_, 0, inputReadyThread, 0);
1689 pthread_mutex_unlock(&inputMutex_);
1692 while (!inputReadyFlag_) {
1696 inputReadyFlag_ = 0;
1698 pthread_mutex_lock(&inputMutex_);
1699 pthread_cond_signal(&inputCond_);
1705 #if defined(use_rl_getc_function)
1708 extern int iv_dialog_is_running;
1709 extern "C" int (*rl_getc_function)(
void);
1710 static int getc_hook(
void) {
1718 if ((r = read(0, &
c,
sizeof(
c))) ==
sizeof(
c)) {
1731 if (errno != EINTR) {
1740 extern int (*rl_event_hook)(
void);
1741 static int event_hook(
void) {
1783 if (
c == EOF ||
c == nl1 ||
c == nl2) {
1799 if (bufstr->
size <=
i) {
1804 bufstr->
buf[
i + 1] =
'\0';
1815 hoc_execerror(
"Internal error:",
"Not finished with previous input line");
1835 extern CHAR* hoc_console_buffer;
1836 hoc_console_buffer =
cbuf;
1861 hoc_execerror(
"Internal error:",
"Not finished with previous input line");
1886 rl_getc_function = getc_hook;
1889 rl_getc_function = rl_getc;
1893 #if defined(use_rl_getc_function)
1895 rl_getc_function = getc_hook;
1898 rl_getc_function = getc_hook;
1902 rl_event_hook = event_hook;
1905 rl_event_hook =
NULL;
1911 extern int hoc_notify_stop;
1914 #if defined(__GO32__)
1918 for (
int i = 0;
i <
n; ++
i) {
1919 if (!isascii(
line[
i])) {
1920 hoc_execerr_ext(
"Non-ASCII character value 0x%hhx at input position %d\n",
1981 hoc_warning(
"Help only available from version with ivoc library", 0);
1987 #if defined(__GO32__)
1989 if (_go32_was_ctrl_break_hit()) {
int parse_bt_symbol(char *backtrace_line, void **addr, char *symbol, char *offset)
int cxx_demangle(char *symbol, char **funcname, size_t *funcname_sz)
static void prnt(const TQItem *b, int level)
char * hoc_strgets(char *cbuf, int nc)
int hoc_strgets_need(void)
sprintf(buf, " if (secondorder) {\n" " int _i;\n" " for (_i = 0; _i < %d; ++_i) {\n" " _p[_slist%d[_i]] += dt*_p[_dlist%d[_i]];\n" " }}\n", numeqn, listnum, listnum)
double chkarg(int, double low, double high)
void hoc_audit_command(const char *buf)
void hoc_execerr_ext(const char *fmt,...)
printf style specification of hoc_execerror message.
void hoc_execerror(const char *s, const char *t)
void hoc_free_val_array(double *, size_t)
int hoc_arayinfo_install(Symbol *sp, int nsub)
void hoc_final_exit(void)
void save_parallel_argv(int, const char **)
void * hoc_Ecalloc(size_t nmemb, size_t size)
void save_parallel_envp(void)
int hoc_ParseExec(int yystart)
void hoc_freearay(Symbol *sp)
void hoc_menu_cleanup(void)
void * hoc_sec_internal_name2ptr(const char *s, int eflag)
void * nrn_parsing_pysec_
int hoc_errno_check(void)
size_t hoc_total_array(Symbol *s)
int hoc_oc(const char *buf)
void hoc_warning(const char *, const char *)
void ivoc_help(const char *)
void * hoc_pysec_name2ptr(const char *s, int eflag)
int hoc_main1(int argc, const char **argv, const char **envp)
void hoc_audit_from_hoc_main1(int argc, const char **argv, const char **envp)
void hoc_oop_initaftererror(void)
void hoc_audit_from_final_exit(void)
void hoc_execerror_mes(const char *s, const char *t, int prnt)
int nrn_mpiabort_on_error_
static void unGetc(int c, NrnFILEWrap *fp)
static void set_signals(void)
char * fgets_unlimited(HocStr *s, NrnFILEWrap *f)
static int debug_message_
void hoc_main1_init(const char *pname, const char **envp)
void hoc_newobj1_err()
unref partially constructed objects controlled by current longjump handle
RETSIGTYPE fpecatch(int sig)
int hoc_execerror_messages
void add_history(const char *)
static CHAR * fgets_unlimited_nltrans(HocStr *s, NrnFILEWrap *f, int nltrans)
RETSIGTYPE(* SignalType)(int)
static int control_jmpbuf
HocStr * hocstr_create(size_t size)
static jmp_buf hoc_oc_begin
int hoc_print_first_instance
void(* oc_jump_target_)()
void nrn_feenableexcept()
RETSIGTYPE onintr(int sig)
static int Getc(NrnFILEWrap *fp)
void oc_restore_input_info(const char *i1, int i2, int i3, NrnFILEWrap *i4)
void * nrn_get_hoc_jmp()
If one of the two jmp_buf is controlling the longjmp hoc_newobj1_err needs handle to know how much to...
static RETSIGTYPE sigpipe_handler(int sig)
size_t hoc_xopen_file_size_
void hocstr_copy(HocStr *hs, const char *buf)
static void restore_signals(void)
CHAR * hoc_fgets_unlimited(HocStr *bufstr, NrnFILEWrap *f)
const char * hoc_promptstr
void oc_save_input_info(const char **i1, int *i2, int *i3, NrnFILEWrap **i4)
static int backslash(int c)
static SignalType signals[4]
static void nrn_inputbuf_getline(void)
void hocstr_resize(HocStr *hs, size_t n)
void hoc_coredump_on_error(void)
static char * optarray(char *buf)
char * readline(const char *prompt)
static const char * nrn_inputbufptr
void warning(const char *s, const char *t)
void hocstr_delete(HocStr *hs)
void defnonly(const char *s)
void hoc_show_errmess_always(void)
static int follow(int expect, int ifyes, int ifno)
void rl_deprep_terminal(void)
void free_arrayinfo(Arrayinfo *a)
int(* p_nrnpy_pyrun)(const char *fname)
static int hoc_run1(void)
void start_profile(int i)
void * erealloc(void *ptr, size_t n)
struct Arrayinfo Arrayinfo
#define NOT_PARALLEL_SUB(c1)
void hoc_acterror(const char *, const char *)
void(* p_nrnpython_finalize)()
static char line[MAXLINE]
void nrn_err_dialog(const char *mes)
int hoc_obj_run(const char *, Object *)
void hoc_check_intupt(int intupt)
#define input(prompt, fmt, var)
char * emalloc(unsigned n)
#define nrn_fw_eq(fw, ff)
#define nrn_fw_set_stdin()
#define nrn_fw_ungetc(c, fw)
int const size_t const size_t n
void nrnmpi_abort(int errcode)
int nrnmpi_numprocs_world
int(* p_nrnpython_start)(int)
int hoc_readcheckpoint(char *)
static const char ** pname(void *v)
static const char * fname(const char *name)