Nife version Beta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

326 lines
7.0 KiB

  1. /* Copyright (C) 2011-2014 Patrick H. E. Foubet - S.E.R.I.A.N.E.
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or any
  5. later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>
  12. *******************************************************************/
  13. /* nife.c */
  14. #include "conf.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <signal.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include "nife.h"
  25. #include "mth.h"
  26. #include "err.h"
  27. #include "lib.h"
  28. #include "stackC.h"
  29. #include "stackF.h"
  30. #include "histo.h"
  31. #include "tasks.h"
  32. #include "debug.h"
  33. #include "net.h"
  34. #include "gplot.h"
  35. static char sepa[] = " \t\n";
  36. void putTrSuite(void (*f)(char*))
  37. {
  38. int i;
  39. i=iTS;
  40. traiteSuite[i++]=f;
  41. _MODIF_iTS_(i);
  42. if (iTS==NBTRSUITE) fprintf(stderr,"traiteSuite limit raise !\n");
  43. }
  44. void dropTrSuite(void)
  45. {
  46. int i;
  47. i=iTS-1;
  48. _MODIF_iTS_(i);
  49. if (iTS < 0) fprintf(stderr,"traiteSuite index negative !\n");
  50. }
  51. PFC getTrSuite(void)
  52. {
  53. if (iTS<1) return (PFC)NULL;
  54. else return(traiteSuite[iTS-1]);
  55. }
  56. void interInfos(char *F, char*P)
  57. {
  58. fprintf(stderr, " Error in %s ( %s ) !!\n",F,P);
  59. if (errno) perror(F);
  60. if (inSonProc) {
  61. sleep(2); exit(1);
  62. }
  63. }
  64. void Interrupt(int S)
  65. {
  66. int status;
  67. switch(S) {
  68. case SIGCHLD :
  69. if(WAITPID) return;
  70. while (waitpid(-1, &status, WNOHANG) > 0);
  71. return;
  72. break;
  73. case SIGSEGV :
  74. printf("Segmentation Error !!\n");
  75. exit(1);
  76. break;
  77. case SIGPIPE :
  78. printf("Pipe is broken");
  79. break;
  80. case SIGFPE :
  81. printf("Floating Point");
  82. break;
  83. case SIGALRM :
  84. printf("Compilation");
  85. break;
  86. default :
  87. printf("Signal %d",S);
  88. break;
  89. }
  90. siglongjmp(ENV_INT,1);
  91. }
  92. void IF_about(void)
  93. {
  94. char Lib[8];
  95. *Lib='\0';
  96. #ifdef _MULTI_THREADING_
  97. strcpy(Lib,"mt-");
  98. #endif
  99. printf("nife (Networking Industrial Forth-like Environment) - version %s%s-%ld/%ld\n\t (c) S.E.R.I.A.N.E. 2009-2014\n",Lib,VERSION,sizeof(long)*8,sizeof(double)*8);
  100. }
  101. int isSepa(char c, int m)
  102. {
  103. unsigned int i;
  104. if (m == 1) /* '\0 fait partie du lot */
  105. if (c == (char)'\0') return 1;
  106. for (i=0; i<strlen(sepa);i++)
  107. if (c == sepa[i]) return 1;
  108. return 0;
  109. }
  110. int traiteMot(char *M)
  111. {
  112. int Err=0;
  113. PFC tS;
  114. if (sigsetjmp(ENV_INT,1)) {
  115. interInfos("traiteMot",M);
  116. return 1;
  117. }
  118. /* printf("traiteMot <%s> iTS=%d\n",M,iTS); */
  119. tS = getTrSuite();
  120. if (tS != (PFC)NULL) tS(M);
  121. else
  122. if (! execLib(M)) { Err=1; messErr2(10,M); }
  123. if (ITASK) exit(0); /* non interpretation in task ! */
  124. return Err;
  125. }
  126. static void traiteLigne(char *b)
  127. {
  128. char *mot, *d, *f, *w;
  129. d=b; f=b+strlen(d);
  130. #ifdef DEBUG
  131. printf("traiteLigne : <%s>\n",d);
  132. #endif
  133. while (d<f) {
  134. if (noErr()) break;
  135. /* recherche du 1er mot */
  136. if (stringEnCours) {
  137. mot = d;
  138. while (1) {
  139. if((d = strchr(d,'"')) == NULL) {
  140. d=mot+strlen(mot);
  141. break;
  142. }
  143. if (*(d-1) == '\\') {
  144. w = d-1;
  145. while (*w != '\0') {
  146. *w = *(w+1);
  147. w++;
  148. }
  149. continue;
  150. }
  151. d++;
  152. if (!isSepa(*d,1)) continue;
  153. break;
  154. }
  155. } else {
  156. /* on ignore les commentaires */
  157. if ((mot = strchr(d, (int)'#')) != NULL) {
  158. *mot = '\0';
  159. f = mot;
  160. }
  161. while (isSepa(*d,0)) d++; /* on avance tant que separateurs */
  162. mot = d;
  163. while (!isSepa(*d,1)) d++; /* on avance si nonSepa ET non \0 */
  164. }
  165. *d++ = '\0'; /* fin de la commande */
  166. if (strlen(mot)>0)
  167. if (traiteMot(mot)) break; /* abort if error */
  168. }
  169. }
  170. void compileFile(char * f)
  171. {
  172. FILE *F;
  173. int i=0;
  174. if ((F = fopen(f,"r")) != NULL) {
  175. while (fgets(bufP, LBUF,F)) {
  176. if (noErr()) {
  177. printf("In file %s line %d !\n",f,i);
  178. break;
  179. }
  180. traiteLigne(bufP);
  181. i++;
  182. }
  183. fclose(F);
  184. }
  185. }
  186. static void lectFic(char *L)
  187. {
  188. int fd;
  189. dropTrSuite();
  190. if ((fd = open(L,O_RDONLY)) == -1) {
  191. perror(L);
  192. messErr(16);
  193. } else addFD(fd,L);
  194. }
  195. void IF_LoadCS(void)
  196. {
  197. char * f;
  198. f = getString();
  199. if (f != NULL) {
  200. compileFile(f);
  201. free((void*)f);
  202. }
  203. }
  204. void IF_ExecCS(void)
  205. {
  206. char * f;
  207. f = getString();
  208. if (f != NULL) {
  209. if (strlen(f)>0) traiteLigne(f);
  210. free((void*)f);
  211. }
  212. }
  213. void * makeFunction(char * f)
  214. {
  215. void *M;
  216. if ((M = malloc(strlen(f)+8)) == NULL) stopErr("makeFunction","malloc");
  217. sprintf((char*)M,": _f %s ;",f);
  218. traiteLigne((char*)M);
  219. free(M);
  220. if (noErr() == 0) {
  221. M = fctByName("_f");
  222. return M;
  223. }
  224. messErr(48);
  225. return VIDE;
  226. }
  227. void IF_ExecCSf(void)
  228. {
  229. char * f;
  230. void *C;
  231. f = getString();
  232. if (f != NULL) {
  233. C = VIDE;
  234. if (strlen(f)>0) C = makeFunction(f);
  235. free((void*)f);
  236. if (C != VIDE) {
  237. IF_execFct("_f");
  238. rmLastFct();
  239. }
  240. }
  241. }
  242. void IF_Load(void)
  243. {
  244. putTrSuite(lectFic);
  245. }
  246. int main(int N, char *P[])
  247. {
  248. int n;
  249. char *dirW = ".nife";
  250. if (N > 2) {
  251. fprintf(stderr,"nife [nif-file]\n");
  252. return(1);
  253. }
  254. if ((sizeof(void*) != sizeof(long)) ||
  255. (sizeof(double) != sizeof(long long))) {
  256. fprintf(stderr,"Nife open-source don't runs on these machine !\n");
  257. return(2);
  258. }
  259. signal(SIGUSR1,SIG_IGN);
  260. signal(SIGINT,Interrupt);
  261. signal(SIGTERM,Interrupt);
  262. signal(SIGPIPE,Interrupt);
  263. signal(SIGCHLD,Interrupt);
  264. signal(SIGQUIT,Interrupt);
  265. signal(SIGSEGV,Interrupt);
  266. signal(SIGFPE,Interrupt);
  267. signal(SIGALRM,Interrupt);
  268. /* work in ./.nife for facilities of debugging !! */
  269. if (chdir(dirW) != 0) {
  270. if (mkdir(dirW, 0755) == -1) {
  271. perror("mkdir"); return 1;
  272. }
  273. if (chdir(dirW) != 0) {
  274. perror("chdir"); return 1;
  275. }
  276. }
  277. termInit(); /* may stop if no term found */
  278. TH_init();
  279. initLib();
  280. D_Reset();
  281. if (N==2) {
  282. IF_Load();
  283. lectFic(P[1]);
  284. } else
  285. printf("Welcome to Nife : Just stack it !\n");
  286. while (RUN) {
  287. if ((FD_IN+iTERM) == 0) {
  288. printf("> ");
  289. fflush(stdout);
  290. }
  291. razErr();
  292. if ((n=lireLigne(FD_IN,bufP,bufP2,LBUF)) == -1)
  293. printf("Line too long!\n");
  294. else
  295. if (n>0) traiteLigne(bufP);
  296. }
  297. IF_delAllGP();
  298. IF_netStopS();
  299. IF_netOff();
  300. termReset();
  301. printf("Bye !\n");
  302. return 0;
  303. }