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