NEURON
hel2mos.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 #ifdef WIN32
3 #include <windows.h>
4 #endif
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <OS/string.h>
9 #include <OS/file.h>
10 #include <InterViews/regexp.h>
11 #include "nrnbbs.h"
12 
13 #ifdef WIN32
14 #include "../winio/debug.h"
15 static void setneuronhome(const char*);
16 #endif
17 
18 extern const char* neuronhome();
19 
20 void start();
21 void stop();
22 void send(const char* url);
23 
24 static bool quit_;
25 
26 static void quit(const char* msg) {
27 //printf("hel2mos quit |%s|\n", msg);
28 //DebugMessage("hel2mos quit |%s|\n", msg);
29  if (strcmp(msg, "neuron closed") == 0) {
30  quit_ = true;
31  }
32 }
33 
34 static RETSIGTYPE quit1(int) {
35  quit_ = true;
36 }
37 
38 static void help(const char*);
39 static CopyString* shelp;
40 
41 
42 int main(int argc, const char** argv){
43 #ifdef WIN32
44  setneuronhome(argv[0]);
45 #endif
46  if (!neuronhome()) {
47  printf("no NEURONHOME\n");
48  return 1;
49  }
50 // printf("started hel2mos\n");
51 
52  char buf[256];
53  sprintf(buf, "%s/lib/helpdict", neuronhome());
54  String sf(buf);
55  InputFile* f = InputFile::open(sf);
56  if (f == nil) {
57  printf("Can't open %s\n", sf.string());
58  return 1;
59  }
60  const char* st;
61  int flen = f->read(st);
62  shelp = new CopyString(st, flen);
63  f->close();
64 
66  nrnbbs_post("ochelp running");
67  start();
68  nrnbbs_notify("ochelp", help);
69 #ifdef WIN32
70  nrnbbs_notify("neuron closed", help);
71 #else
72  nrnbbs_notify("neuron closed", quit);
73 #endif
74  help("");
76  stop();
77  nrnbbs_take("ochelp running");
79  return 0;
80 }
81 
82 static bool find(const char* key, char* val) {
83  static char buf[256];
84  sprintf(buf, "^%s ", key);
85 //printf("|%s|\n", buf);
86  Regexp r1(buf);
87  int i = r1.Search(shelp->string(), shelp->length(), 0, shelp->length());
88 //printf("i=%d\n", i);
89  if (i < 0) {
90  return false;
91  }
92  Regexp r2("neuron/");
93  i = r2.Search(shelp->string(), shelp->length(), i, shelp->length());
94 //printf("i=%d\n", i);
95  int j = shelp->search(i, '\n');
96 //printf("j=%d\n", j);
97  strncpy(val, shelp->string() + i, j-i);
98  val[j-i] = '\0';
99  return true;
100 }
101 
102 static void help(const char* msg) {
103  if (strcmp(msg, "neuron closed") == 0) {
104  quit_ = true;
105  }
106  char buf[256];
107 //printf("hel2mos help |%s|\n", msg);
108  while(nrnbbs_take_string("ochelp", buf)) {
109  if (buf[0] == '?') {
110 // printf("took ochelp: %s\n", buf);
111  }else{
112  if (find(buf, buf)) {
113 // printf("%s\n", buf);
114  }else{
115  sprintf(buf, "contents.html");
116  }
117 #ifdef WIN32
118 // DebugMessage("buf=|%s|\n", buf);
119  char buf1[256],buf2[256];
120  strcpy(buf2,buf);
121  strcpy(buf1, neuronhome());
122  for (char* cp = buf1; *cp; ++cp) {
123  if (*cp == ':') {
124  *cp = '|';
125  }
126  if (*cp == '\\') {
127  *cp = '/';
128  }
129  }
130  sprintf(buf, "file:///%s/html/help/%s", buf1, buf2);
131 #endif
132  send(buf);
133  }
134  }
135 }
136 
137 const char* neuronhome() {
138  const char* n = getenv("NEURONHOME");
139  if (n) {
140  return n;
141  }
142  return nil;
143 }
144 
145 #if defined(WIN32)
146 static void setneuronhome(const char* p) {
147  // if the program lives in .../bin/mos2nrn.exe
148  // and .../lib exists then use ... as the
149  // NEURONHOME
150 // printf("p=|%s|\n", p);
151  char buf[256];
152  if (p[0] == '"') {
153  strcpy(buf, p+1);
154  }else{
155  strcpy(buf, p);
156  }
157  int i, j;
158  for (i=strlen(buf); i >= 0 && buf[i] != '\\'; --i) {;}
159  buf[i] = '\0'; // /neuron.exe gone
160  //printf("setneuronhome |%s|\n", buf);
161  for (j=strlen(buf); j >= 0 && buf[j] != '\\'; --j) {;}
162  buf[j] = '\0'; // /bin gone
163  // but make sure it was bin Bin or BIN -- damn you bill gates
164  //printf("i=%d j=%d buf=|%s|\n",i, j, buf);
165  if (i == j+4
166  &&(buf[--i] == 'n' || buf[i] == 'N')
167  &&(buf[--i] == 'i' || buf[i] == 'I')
168  &&(buf[--i] == 'b' || buf[i] == 'B')
169  ) {
170  static char buf1[256];
171  // check for nrn.def or nrn.defaults
172  // if it exists assume valid installation
173  FILE* f;
174  sprintf(buf1, "%s/lib/nrn.def", buf);
175  if ((f = fopen(buf1, "r")) == (FILE*)0) {
176  sprintf(buf1, "%s/lib/nrn.defaults", buf);
177  if ((f = fopen(buf1, "r")) == (FILE*)0) {
178  sprintf(buf1, "%s not valid neuronhome\n", buf);
179  MessageBox(NULL, buf1, "mos2nrn", MB_OK);
180  return;
181  }
182  }
183  fclose(f);
184  sprintf(buf1, "NEURONHOME=%s", buf);
185  putenv(buf1); // arg must be global
186  }
187 }
188 
189 char* nrnhome;
190 
191 #else
192 
193 #include <unistd.h>
194 #include <signal.h>
195 
196 static int mosaic_pid_; /* no longer used. 0 is fine */
197 
198 void start() {
199 #if defined(SIGNAL_CAST)
200  signal(SIGHUP, (SIGNAL_CAST)quit1);
201 #else
202  signal(SIGHUP, quit1);
203 #endif
204 }
205 
206 void stop() {
207 }
208 
209 void send(const char* url) {
210  char buf1[512];
211  int start = 0;
212  while(url[start] == ' ') {
213  ++start;
214  }
215  sprintf(buf1, "%s/bin/hel2mos1.sh \"%s\"", neuronhome(), url+start);
216 //printf("sending |%s|\n", buf1);
217  signal(SIGCHLD, SIG_IGN);
218  system(buf1);
219 #if defined(SIGNAL_CAST)
220  signal(SIGCHLD, (SIGNAL_CAST)quit1);
221 #else
222  signal(SIGCHLD, quit1);
223 #endif
224 }
225 
226 #endif
void nrnbbs_post(const char *)
Definition: ivocmac.cpp:36
char * nrnhome
Definition: mos2nrn.cpp:10
virtual int search(int start, u_char) const
Definition: string.cpp:227
void nrnbbs_notify(const char *, NrnBBSCallback)
Definition: ivocmac.cpp:48
static CopyString * shelp
Definition: hel2mos.cpp:39
size_t p
#define RETSIGTYPE
Definition: nrnconf.h:5
int length() const
Definition: string.h:140
Definition: file.h:56
sprintf(buf," if (secondorder) {\ " int _i;\" " for(_i=0;_i< %d;++_i) {\" " _p[_slist%d[_i]]+=dt *_p[_dlist%d[_i]];\" " }}\", numeqn, listnum, listnum)
bool nrnbbs_take_string(const char *, char *)
Definition: ivocmac.cpp:42
int system(const char *s)
Definition: mswinprt.cpp:281
void start()
Definition: hel2mos.cpp:205
bool nrnbbs_connect()
Definition: ivocmac.cpp:32
static InputFile * open(const String &name)
Definition: file.cpp:219
const char * string() const
Definition: string.h:139
#define putenv
Definition: mwprefix.h:37
int const size_t const size_t n
Definition: nrngsl.h:12
static void quit(const char *msg)
Definition: hel2mos.cpp:26
int val
Definition: dll.cpp:167
void stop()
Definition: hel2mos.cpp:209
#define printf
Definition: mwprefix.h:26
static RETSIGTYPE quit1(int)
Definition: hel2mos.cpp:34
#define CopyString
Definition: _defines.h:2
#define key
Definition: spt2queue.cpp:20
char * getenv(const char *s)
Definition: macprt.cpp:67
size_t j
void nrnbbs_wait(bool *pflag=(bool *) 0)
Definition: ivocmac.cpp:50
#define nil
Definition: enter-scope.h:36
static bool quit_
Definition: hel2mos.cpp:24
bool nrnbbs_take(const char *)
Definition: ivocmac.cpp:40
void send(const char *url)
Definition: hel2mos.cpp:212
Definition: regexp.h:61
static int mosaic_pid_
Definition: hel2mos.cpp:196
void nrnbbs_disconnect()
Definition: ivocmac.cpp:33
#define i
Definition: md1redef.h:12
virtual int read(const char *&start)
Definition: file.cpp:245
Definition: string.h:34
char buf[512]
Definition: init.cpp:13
int main(int argc, const char **argv)
Definition: hel2mos.cpp:42
void setneuronhome(const char *)
Definition: ivocmain.cpp:196
static int argc
Definition: inithoc.cpp:53
int Search(const char *text, int length, int index, int range)
Definition: regexp.cpp:111
static bool find(const char *key, char *val)
Definition: hel2mos.cpp:82
FILE * fopen()
virtual void close()
Definition: file.cpp:178
const char * neuronhome()
Definition: hel2mos.cpp:139
return NULL
Definition: cabcode.cpp:461
static char ** argv
Definition: inithoc.cpp:54
static void help(const char *)
Definition: hel2mos.cpp:102