NEURON
mswinprt.cpp
Go to the documentation of this file.
1 #ifndef CYGWIN
2 #include <../../nrnconf.h>
3 #endif
4 
5 #if defined(MINGW)
6 #define CYGWIN
7 #endif
8 
9 #include <windows.h>
10 #include <stdio.h>
11 #include <stdarg.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <errno.h>
15 #include "hoc.h"
16 #include "../mswin/extra/d2upath.cpp"
17 
18 #include "gui-redirect.h"
19 
20 
21 extern Object** (*nrnpy_gui_helper_)(const char* name, Object* obj);
22 extern double (*nrnpy_object_to_double_)(Object*);
23 
24 extern char* neuron_home;
25 extern char* neuron_home_dos;
26 extern void hoc_quit();
27 
28 #if !defined(CYGWIN)
29 extern HWND hCurrWnd;
30 #endif
31 static HCURSOR wait_cursor;
32 static HCURSOR old_cursor;
33 #if HAVE_IV
34 extern int bad_install_ok;
35 #else
37 #endif // HAVE_IV
38 extern FILE* hoc_redir_stdout;
39 void setneuronhome(const char* p) {
40  // if the program lives in .../bin/neuron.exe
41  // and .../lib exists then use ... as the
42  // NEURONHOME
43  char buf[256]; char *s;
44  int i, j;
45 // printf("p=|%s|\n", p);
46  bad_install_ok = 1;
47 #if 0
48  if (p[0] == '"') {
49  strcpy(buf, p+1);
50  }else{
51  strcpy(buf, p);
52  }
53 #endif
54  GetModuleFileName(NULL, buf, 256);
55  for (i=strlen(buf); i >= 0 && buf[i] != '\\'; --i) {;}
56  buf[i] = '\0'; // /neuron.exe gone
57 // printf("setneuronhome |%s|\n", buf);
58  for (j=strlen(buf); j >= 0 && buf[j] != '\\'; --j) {;}
59  buf[j] = '\0'; // /bin gone
60  neuron_home_dos = static_cast<char *>(emalloc(strlen(buf) + 1));
61  strcpy(neuron_home_dos, buf);
63  return;
64 #if 0
65  // but make sure it was bin Bin or BIN -- damn you bill gates
66 // printf("i=%d j=%d buf=|%s|\n",i, j, buf);
67  if (i == j+4
68  &&(buf[--i] == 'n' || buf[i] == 'N')
69  &&(buf[--i] == 'i' || buf[i] == 'I')
70  &&(buf[--i] == 'b' || buf[i] == 'B')
71  ) {
72  char buf1[256], *nh_old;
73  // check for nrn.def or nrn.defaults
74  // if it exists assume valid installation
75  FILE* f;
76  sprintf(buf1, "%s/lib/nrn.def", buf);
77  if ((f = fopen(buf1, "r")) == (FILE*)0) {
78  sprintf(buf1, "%s/lib/nrn.defaults", buf);
79  if ((f = fopen(buf1, "r")) == (FILE*)0) {
80 // printf("couldn't open %s\n", buf1);
81 // printf("%s not valid neuronhome\n", buf);
82  return;
83  }
84  }
85  fclose(f);
86  sprintf(buf1, "NEURONHOME=%s", buf);
87  nh_old = getenv("NEURONHOME");
88 // if (nh_old){ printf("nh_old from first getenv is %s\n", nh_old);
89 // }else{ printf("first getenv of NEURONHOME returns nil\n");}
90  if (!nh_old || stricmp(buf, nh_old) != 0) {
91  printf("Setting %s", buf1);
92  if (nh_old) {
93  printf(" from old value of %s\n", nh_old);
94  }else{
95  printf("\n");
96  }
97  s = static_cast<char *>(emalloc(strlen(buf1)+1));
98  strcpy(s, buf1);
99  i = putenv(s); // arg must be global
100 // printf("putenv of %s returned %d\n", s, i);
101  }
102  }
103  neuron_home = getenv("NEURONHOME");
104 // if (neuron_home){ printf("neuron_home from second getenv is %s\n", neuron_home);
105 // }else{ printf("second getenv of NEURONHOME returns nil\n");}
106  if (!neuron_home) MessageBox(NULL, p, "Can't compute NEURONHOME from", MB_OK);
107 #endif
108 }
109 void HandleOutput(char* s) {
110  printf("%s", s);
111 }
112 static long exception_filter(LPEXCEPTION_POINTERS p) {
113 // hoc_execerror("unhandled exception", "");
114 // return EXCEPTION_CONTINUE_EXECUTION;
115  static int n = 0;
116  ++n;
117  if (n == 1) {
118  hoc_execerror("\nUnhandled Exception. This usually means a bad memory \n\
119 address.", "It is not possible to make a judgment as to whether it is safe\n\
120 to continue. If this happened while compiling a template, you will have to\n\
121 quit.");
122  }
123  if (n == 2) {
124 MessageBox(NULL, "Second Unhandled Exception: Quitting NEURON. You will be asked to save \
125 any unsaved em buffers before exiting.", "NEURON Internal ERROR", MB_OK);
126  hoc_quit();
127  }
128  return EXCEPTION_EXECUTE_HANDLER;
129 }
130 
132  SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)exception_filter);
133 }
134 BOOL hoc_copyfile(const char* src, const char* dest) {
135  return CopyFile(src, dest, FALSE);
136 }
137 
138 static FILE* dll_stdio_[] = {(FILE*)0x0, (FILE*)0x20, (FILE*)0x40};
139 
140 void nrn_mswindll_stdio(FILE* i, FILE* o, FILE* e) {
141  if (o != dll_stdio_[1]) {
142  printf("nrn_mswindll_stdio stdio in dll = %p but expected %p\n", o, dll_stdio_[1]);
143  }
144  dll_stdio_[0] = i;
145  dll_stdio_[1] = o;
146  dll_stdio_[2] = e;
147 }
148 
149 #if defined(CYGWIN)
150 /* the cygwin nrnmech.dll will use these pointers for stdin, stdout, stderr */
151 /* and when we come back to ncyg_fprintf we compare the stream to them */
152 FILE __files[4];
153 #else
154 #define ncyg_fprintf fprintf
155 #endif
156 
157 int ncyg_fprintf(FILE /*_FAR*/ *stream, const char * strFmt, ...)
158 {
159  int len;
160  static char s[4096] = {0};
161  va_list marker;
162  va_start(marker, strFmt);
163 #if defined(CYGWIN)
164 #if 0
165  printf("ncyg stdin=%lx\n", (long)stdin);
166  printf("ncyg stdout=%lx\n", (long)stdout);
167  printf("ncyg stderr=%lx\n", (long)stderr);
168  printf("ncyg dll_stdio[0]=%lx\n", (long)dll_stdio_[0]);
169  printf("ncyg dll_stdio[1]=%lx\n", (long)dll_stdio_[1]);
170  printf("ncyg dll_sdtio[2]=%lx\n", (long)dll_stdio_[2]);
171  printf("ncyg stream=%lx\n", (long)stream);
172 #endif
173  if (stream == dll_stdio_[1]) {
174  stream = stdout;
175  }else if (stream == dll_stdio_[2]) {
176  stream = stderr;
177  }
178  len = vsprintf(s, strFmt, marker);
179  fputs(s, stream);
180  return len;
181 #endif
182 }
183 
184 void hoc_forward2back(char* s) {
185  char* cp;
186  for (cp = s; *cp; ++cp) {
187  if (*cp == '/') {
188  *cp = '\\';
189  }
190  }
191 }
192 
193 extern "C" char* hoc_back2forward(char* s) {
194  char* cp = s;
195  while(*cp) {
196  if (*cp == '\\') {
197  *cp = '/';
198  }
199  ++cp;
200  }
201  return s;
202 }
203 
204 #if HAVE_IV
205 void ivoc_win32_cleanup();
206 #endif
207 
209  char buf[256];
210  char* path;
211 #if HAVE_IV
212  ivoc_win32_cleanup();
213 #endif
214  path = getenv("TEMP");
215  if (path) {
216  sprintf(buf, "%s/oc%d.hl", path, getpid());
217  unlink(buf);
218 // DebugMessage("unlinked %s\n", buf);
219  }
220 }
221 
222 void hoc_win_exec(void) {
223  int i;
224  i = SW_SHOW;
225  if (ifarg(2)) {
226  i = (int)chkarg(2, -1000, 1000);
227  }
228  i = WinExec(gargstr(1), i);
229  ret();
230  pushx((double)i);
231 }
232 
233 #if !defined(CYGWIN)
234 
235 FILE* popen(char* s1, char* s2) {
236  printf("no popen\n");
237  return 0;
238 }
239 
240 pclose(FILE* p) {
241  printf("no pclose\n");
242 }
243 
244 void hoc_check_intupt(int intupt) {
245 #if !OCSMALL
246  MSG msg;
247  while (PeekMessage(&msg, hCurrWnd, 0, 0, PM_REMOVE))
248  {
249  TranslateMessage(&msg);
250  DispatchMessage(&msg);
251  }
252 #endif
253 }
254 
255 #if defined(__MWERKS__)
256 void __assertfail() { printf("assertfail\n");}
257 #endif
258 
259 #if 0
260 char* dos_neuronhome() {
261  static char* dnrnhome;
262  char* nrnhome, *cp;
263  if (!dnrnhome) {
264  nrnhome = getenv("NEURONHOME");
265  dnrnhome = static_cast<char *>(emalloc(strlen(nrnhome) + 1));
266  strcpy(dnrnhome, nrnhome);
267  for (cp = dnrnhome; *cp; ++cp) {
268  if (*cp == '/') {
269  *cp = '\\';
270  }
271  }
272  }
273  return dnrnhome;
274 }
275 #endif
276 
277 #define HOCXDOS "lib/nrnsys.sh"
278 #define SEMA1 "tmpdos1.tmp"
279 #define SEMA2 "tmpdos2.tmp"
280 
281 int system(const char* s) {
282  char buf[256], stin[128];
283  char* redirect;
284  FILE* fin;
285 
286  unlink(SEMA1);
287  unlink(SEMA2);
288  errno = 0;
289  redirect = strchr(s, '>');
290  if (redirect) {/* redirection filename is first arg */
291  strcpy(stin, redirect+1);
292  sprintf(buf, "%s\\bin\\sh %s/%s %s %s %s",
294  redirect = strchr(buf, '>');
295  *redirect = '\0';
296  }else{
297  sprintf(buf, "%s\\bin\\sh %s/%s %s %s %s",
299  }
300 //printf("%s\n", buf);
301  if (WinExec(buf, 0) < 32) {
302  hoc_execerror("WinExec failed:", buf);
303  }
304  while((fin = fopen(SEMA1, "r")) == (FILE*)0) {
305  Sleep(1);
306  wmhandler_yield();
307  }
308  fclose(fin);
309  unlink(SEMA1);
310  if (!redirect && (fin = fopen(SEMA2, "r")) != (FILE*)0) {
311  while(fgets(buf, 256, fin)) {
312  printf("%s", buf);
313  }
314  fclose(fin);
315  unlink(SEMA2);
316  }
317  return 0;
318 }
319 
321  if (old_cursor) {
322  (HCURSOR)SetClassLong(hCurrWnd, GCL_HCURSOR, (long)old_cursor);
323  SetCursor(old_cursor);
324  old_cursor = 0;
325  }
326 }
327 
329  static int ready = 0;
330  if (!ready) {
331  wait_cursor = LoadCursor(NULL, IDC_WAIT);
332  }
333  if (!old_cursor) {
334 //DebugMessage("set the wait cursor\n");
335  old_cursor = (HCURSOR)SetClassLong(hCurrWnd, GCL_HCURSOR, (long)wait_cursor);
336  SetCursor(wait_cursor);
337  }
338 }
339 
340 #endif /* not CYGWIN */
341 
342 void hoc_winio_show(int b) {
343 #ifndef CYGWIN
344  ShowWindow(hCurrWnd, b ? SW_SHOW : SW_HIDE);
345 #endif
346 }
347 
348 #if 0
349 void plprint(const char* s) {
350  printf("%s", s);
351 }
352 #endif
353 
354 #if !defined(__MWERKS__)
355 int getpid() {
356 #if 0
357  extern int __hInst;
358  return __hInst;
359 #else
360  return 1;
361 #endif
362 }
363 #endif
364 //hoc_close_plot(){}
365 //hoc_Graphmode(){ret();pushx(0.);}
366 //hoc_Graph(){ret();pushx(0.);}
367 //hoc_regraph(){ret();pushx(0.);}
368 //hoc_plotx(){ret();pushx(0.);}
369 //hoc_ploty(){ret();pushx(0.);}
370 void hoc_Plt() {TRY_GUI_REDIRECT_DOUBLE("plt", NULL); ret(); pushx(0.);}
371 void hoc_Setcolor(){TRY_GUI_REDIRECT_DOUBLE("setcolor", NULL); ret(); pushx(0.);}
372 void hoc_Lw(){ret(); pushx(0.);}
373 // TODO : just temporary fix to avoid duplicate symbol error
374 //void hoc_settext(){ret(); pushx(0.);}
375 //Plot(){ret();pushx(0.);}
376 //axis(){ret();pushx(0.);}
377 //hoc_fmenu() {ret();pushx(0.);}
378 
379 //int gethostname() {printf("no gethostname\n");}
380 
381 
382 //plt(int mode, double x, double y) {}
383 
384 //initplot() {}
385 #if 0
386 Fig_file(){}
387 #endif
388 
o
Definition: seclist.cpp:180
void hoc_Lw()
Definition: mswinprt.cpp:372
void setneuronhome(const char *p)
Definition: mswinprt.cpp:39
double(* nrnpy_object_to_double_)(Object *)
Definition: xmenu.cpp:14
char * nrnhome
Definition: mos2nrn.cpp:10
void hoc_winio_show(int b)
Definition: mswinprt.cpp:342
void pushx(double d)
Definition: code.cpp:641
char * neuron_home_dos
Definition: code2.cpp:204
void hoc_win32_cleanup()
Definition: mswinprt.cpp:208
hoc_win_normal_cursor()
Definition: mswinprt.cpp:320
#define ncyg_fprintf
Definition: mswinprt.cpp:154
#define stricmp
Definition: mwprefix.h:36
static HCURSOR wait_cursor
Definition: mswinprt.cpp:31
int bad_install_ok
Definition: mswinprt.cpp:36
HWND hCurrWnd
#define SEMA2
Definition: mswinprt.cpp:279
char * hoc_dos2unixpath(const char *d)
Definition: d2upath.cpp:46
#define SEMA1
Definition: mswinprt.cpp:278
size_t p
void HandleOutput(char *s)
Definition: mswinprt.cpp:109
const char * expand_env_var(const char *s)
Definition: fileio.cpp:146
pclose(FILE *p)
Definition: mswinprt.cpp:240
#define HOCXDOS
Definition: mswinprt.cpp:277
sprintf(buf," if (secondorder) {\ " int _i;\" " for(_i=0;_i< %d;++_i) {\" " _p[_slist%d[_i]]+=dt *_p[_dlist%d[_i]];\" " }}\", numeqn, listnum, listnum)
#define plprint
Definition: redef.h:110
int system(const char *s)
Definition: mswinprt.cpp:281
#define e
Definition: passive0.cpp:24
#define gargstr
Definition: hocdec.h:14
void hoc_Plt()
Definition: mswinprt.cpp:370
#define putenv
Definition: mwprefix.h:37
static long exception_filter(LPEXCEPTION_POINTERS p)
Definition: mswinprt.cpp:112
int const size_t const size_t n
Definition: nrngsl.h:12
#define TRY_GUI_REDIRECT_DOUBLE(name, obj)
Definition: gui-redirect.h:54
_CONST char * s
Definition: system.cpp:74
#define fin
Definition: redef.h:60
FILE * hoc_redir_stdout
#define printf
Definition: mwprefix.h:26
void hoc_check_intupt(int intupt)
Definition: mswinprt.cpp:244
FILE * popen(char *s1, char *s2)
Definition: mswinprt.cpp:235
#define ret
Definition: redef.h:123
int
Definition: nrnmusic.cpp:71
void hoc_win_exec(void)
Definition: mswinprt.cpp:222
hoc_win_wait_cursor()
Definition: mswinprt.cpp:328
void hoc_execerror(const char *, const char *)
Definition: hoc.cpp:741
void Fig_file(const char *, int)
Definition: plot.cpp:618
BOOL hoc_copyfile(const char *src, const char *dest)
Definition: mswinprt.cpp:134
errno
Definition: system.cpp:98
char * getenv(const char *s)
Definition: macprt.cpp:67
char * hoc_back2forward(char *s)
Definition: mswinprt.cpp:193
size_t j
static FILE * dll_stdio_[]
Definition: mswinprt.cpp:138
char * name
Definition: init.cpp:16
char * emalloc(unsigned n)
Definition: list.cpp:189
int ifarg(int)
Definition: code.cpp:1562
Definition: hocdec.h:226
#define FALSE
Definition: err.c:56
static HCURSOR old_cursor
Definition: mswinprt.cpp:32
#define i
Definition: md1redef.h:12
void hoc_forward2back(char *s)
Definition: mswinprt.cpp:184
void hoc_set_unhandled_exception_filter()
Definition: mswinprt.cpp:131
char buf[512]
Definition: init.cpp:13
char * neuron_home
Definition: hoc_init.cpp:268
int getpid()
Definition: mswinprt.cpp:355
FILE * fopen()
void nrn_mswindll_stdio(FILE *i, FILE *o, FILE *e)
Definition: mswinprt.cpp:140
return NULL
Definition: cabcode.cpp:461
double chkarg(int, double low, double high)
Definition: code2.cpp:608
void hoc_Setcolor()
Definition: mswinprt.cpp:371
void hoc_quit()
Definition: hoc.cpp:1164