proc.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /*+-------------------------------------------------------------------------
  2. proc.c - procedure command and control
  3. wht@wht.net
  4. Defined functions:
  5. _cmd_gosub_common(param, type)
  6. _get_goto_label(param)
  7. do_proc(argc, argv)
  8. dump_proc(pcb)
  9. execute_esd(tesd)
  10. execute_goto(pcb, goto_type)
  11. execute_labelled_lcb(tesd)
  12. execute_proc(pcb, use_goto_label)
  13. find_cproc_labelled_lcb(label)
  14. find_labelled_lcb(label, first, last)
  15. find_proc_cmd(cmd_list, cmd)
  16. find_procedure(name)
  17. free_lcb_chain(lcb)
  18. pcmd_do(param)
  19. pcmd_gosub(param)
  20. pcmd_gosubb(param)
  21. pcmd_goto(param)
  22. pcmd_gotob(param)
  23. pcmd_return(param)
  24. pcmd_upon(param)
  25. proc_dcdloss_handler(pcb)
  26. show_error_position(pcb)
  27. trace_proc_cmd(pcb)
  28. --------------------------------------------------------------------------*/
  29. /*+:EDITS:*/
  30. /*:04-26-2000-11:16-wht@bob-RELEASE 4.42 */
  31. /*:01-08-2000-14:04-wht@menlo-add find_cproc_labelled_lcb */
  32. /*:10-26-1998-14:46-wht@menlo-obscure bug unreported: goto_label overflow */
  33. /*:11-11-1997-12:39-wht@gyro-more microfidgeting */
  34. /*:11-10-1997-17:52-wht@kepler-obscure unlikely memory leak */
  35. /*:11-03-1997-02:23-wht@kepler-4.08a-option command+doc primping */
  36. /*:02-09-1997-19:43-wht@yuriatin-static procpath from 256 to ECU_MAXPN in len */
  37. /*:01-24-1997-02:38-wht@yuriatin-SOURCE RELEASE 4.00 */
  38. /*:12-10-1996-19:29-wht@yuriatin-allow # comment after semicolon */
  39. /*:10-16-1996-02:07-wht@yuriatin-retain variables created during gosub */
  40. /*:09-11-1996-20:01-wht@yuriatin-3.48-major telnet,curses,structural overhaul */
  41. /*:08-20-1996-12:39-wht@kepler-locale/ctype fixes from ache@nagual.ru */
  42. /*:11-23-1995-11:20-wht@kepler-source control 3.37 for tsx-11 */
  43. /*:11-14-1995-10:23-wht@kepler-3.37.80-source control point: SOCKETS */
  44. /*:09-17-1995-16:28-wht@kepler-remove obsolete #if 0 code */
  45. /*:05-11-1995-15:55-wht@n4hgf-ck_sigint fools optimizing compilers */
  46. /*:03-11-1995-17:55-wht@kepler-no more use of 'index' as name of auto */
  47. /*:05-04-1994-04:39-wht@n4hgf-ECU release 3.30 */
  48. /*:03-10-1994-17:46-wht@n4hgf-was not closing files on proc exit */
  49. /*:09-10-1992-14:00-wht@n4hgf-ECU release 3.20 */
  50. /*:08-22-1992-15:39-wht@n4hgf-ECU release 3.20 BETA */
  51. /*:11-16-1991-15:39-wht@n4hgf2-add pcmd_upon stub */
  52. /*:11-16-1991-14:53-wht@n4hgf2-add proc_dcdloss_handler */
  53. /*:11-16-1991-14:01-wht@n4hgf-calloc pcb instead of malloc */
  54. /*:07-25-1991-12:59-wht@n4hgf-ECU release 3.10 */
  55. /*:07-01-1991-01:53-wht@n4hgf-fix return with value */
  56. /*:05-01-1991-04:18-wht@n4hgf-new find_procedure failed on home subdir match */
  57. /*:04-30-1991-03:19-root@n4hgf-add search for .ep in ecu lib ep subdir */
  58. /*:08-14-1990-20:40-wht@n4hgf-ecu3.00-flush old edit history */
  59. #include "ecu.h"
  60. #include "ecuerror.h"
  61. #include "ecukey.h"
  62. #include "esd.h"
  63. #include "var.h"
  64. #include "procedure.h"
  65. #define NEED_P_CMD
  66. #include "ecucmd.h"
  67. PCB *pcb_stack[PROC_STACK_MAX];
  68. int proc_level = 0;
  69. int proc_trace = 0;
  70. int proc_option_localvars = 0;
  71. char goto_label[64];
  72. /*+-------------------------------------------------------------------------
  73. _get_goto_label(param)
  74. --------------------------------------------------------------------------*/
  75. int
  76. _get_goto_label(param)
  77. ESD *param;
  78. {
  79. int erc;
  80. ESD *label_esd;
  81. goto_label[0] = 0;
  82. if (erc = get_alphanum_zstr(param, goto_label, sizeof(goto_label)))
  83. {
  84. if (!(label_esd = esdalloc(ESD_NOMSZ)))
  85. return (eNoMemory);
  86. if (!(erc = gstr(param, label_esd, 0)))
  87. {
  88. strncpy(goto_label, label_esd->pb,sizeof(goto_label));
  89. goto_label[sizeof(goto_label) - 1] = 0;
  90. }
  91. esdfree(label_esd);
  92. }
  93. return (erc);
  94. } /* end of _get_goto_label */
  95. /*+-------------------------------------------------------------------------
  96. pcmd_goto(param)
  97. --------------------------------------------------------------------------*/
  98. int
  99. pcmd_goto(param)
  100. ESD *param;
  101. {
  102. if (!proc_level)
  103. return (eNotExecutingProc);
  104. if (_get_goto_label(param))
  105. return (eInvalidLabel);
  106. return (eProcAttn_GOTO);
  107. } /* end of pcmd_goto */
  108. /*+-------------------------------------------------------------------------
  109. pcmd_gotob(param)
  110. --------------------------------------------------------------------------*/
  111. int
  112. pcmd_gotob(param)
  113. ESD *param;
  114. {
  115. if (!proc_level)
  116. return (eNotExecutingProc);
  117. if (_get_goto_label(param))
  118. return (eInvalidLabel);
  119. return (eProcAttn_GOTOB);
  120. } /* end of pcmd_gotob */
  121. /*+-------------------------------------------------------------------------
  122. _cmd_gosub_common(param,type)
  123. --------------------------------------------------------------------------*/
  124. int
  125. _cmd_gosub_common(param, type)
  126. ESD *param;
  127. int type;
  128. {
  129. int erc;
  130. LCB *current_save;
  131. int index_save;
  132. if (_get_goto_label(param))
  133. return (eInvalidLabel);
  134. current_save = pcb_stack[proc_level - 1]->current;
  135. index_save = current_save->text->index;
  136. if (!(erc = execute_proc(pcb_stack[proc_level - 1], type)))
  137. {
  138. pcb_stack[proc_level - 1]->current = current_save;
  139. current_save->text->index = index_save;
  140. }
  141. return (erc);
  142. } /* end of _cmd_gosub_common */
  143. /*+-------------------------------------------------------------------------
  144. pcmd_gosub(param)
  145. --------------------------------------------------------------------------*/
  146. int
  147. pcmd_gosub(param)
  148. ESD *param;
  149. {
  150. if (!proc_level)
  151. return (eNotExecutingProc);
  152. return (_cmd_gosub_common(param, eProcAttn_GOTO));
  153. } /* end of pcmd_gosub */
  154. /*+-------------------------------------------------------------------------
  155. pcmd_return(param)
  156. --------------------------------------------------------------------------*/
  157. int
  158. pcmd_upon(param)
  159. ESD *param;
  160. {
  161. pprintf("'upon' not implimented\n");
  162. param->index = param->cb;
  163. return (0);
  164. } /* end of pcmd_return */
  165. /*+-------------------------------------------------------------------------
  166. pcmd_gosubb(param)
  167. --------------------------------------------------------------------------*/
  168. int
  169. pcmd_gosubb(param)
  170. ESD *param;
  171. {
  172. if (!proc_level)
  173. return (eNotExecutingProc);
  174. return (_cmd_gosub_common(param, eProcAttn_GOTO));
  175. } /* end of pcmd_gosubb */
  176. /*+-------------------------------------------------------------------------
  177. pcmd_return(param)
  178. --------------------------------------------------------------------------*/
  179. int
  180. pcmd_return(param)
  181. ESD *param;
  182. {
  183. long value = 0;
  184. if (!gint(param, &value))
  185. {
  186. if ((value < 0) || (value > 255))
  187. value = 255;
  188. if (proc_trace)
  189. pprintf("return value %ld\n", value);
  190. if (value)
  191. value += e_USER;
  192. return ((int)value);
  193. }
  194. return (eProcAttn_RETURN);
  195. } /* end of pcmd_return */
  196. /*+-------------------------------------------------------------------------
  197. find_labelled_lcb(label,first,last)
  198. search for match between label
  199. --------------------------------------------------------------------------*/
  200. LCB *
  201. find_labelled_lcb(label, first, last)
  202. char *label;
  203. LCB *first;
  204. LCB *last;
  205. {
  206. int llen = strlen(label);
  207. ESD *text;
  208. while (first)
  209. {
  210. text = first->text;
  211. if ((text->cb >= llen) && (!strncmp(text->pb, label, llen))
  212. && (!text->pb[llen] || isspace((uchar) text->pb[llen])))
  213. return (first);
  214. if (first == last)
  215. return ((LCB *) 0);
  216. first = first->next;
  217. }
  218. pputs("find_labelled_lab logic error\n");
  219. return ((LCB *) 0);
  220. } /* end of find_labelled_lcb */
  221. /*+-------------------------------------------------------------------------
  222. find_cproc_labelled_lcb(label)
  223. --------------------------------------------------------------------------*/
  224. LCB *
  225. find_cproc_labelled_lcb(label)
  226. char *label;
  227. {
  228. PCB *pcb;
  229. if(!proc_level)
  230. return(0);
  231. pcb = pcb_stack[proc_level - 1];
  232. return(find_labelled_lcb(label, pcb->first, pcb->last));
  233. } /* end of find_cproc_labelled_lcb */
  234. /*+-------------------------------------------------------------------------
  235. execute_goto(pcb,goto_type)
  236. --------------------------------------------------------------------------*/
  237. execute_goto(pcb, goto_type)
  238. PCB *pcb;
  239. int goto_type;
  240. {
  241. LCB *next = (LCB *) 0; /* next lcb to execute */
  242. switch (goto_type)
  243. {
  244. case eProcAttn_GOTO:
  245. if (!(next = find_labelled_lcb(goto_label, pcb->current, pcb->last)))
  246. next = find_labelled_lcb(goto_label, pcb->first, pcb->current);
  247. break;
  248. case eProcAttn_GOTOB:
  249. if (!(next = find_labelled_lcb(goto_label, pcb->first, pcb->current)))
  250. next = find_labelled_lcb(goto_label, pcb->current, pcb->last);
  251. break;
  252. }
  253. if (next)
  254. {
  255. pcb->current = next;
  256. return (0);
  257. }
  258. pprintf("goto/gosub label not found: %s\n", goto_label);
  259. return (eFATAL_ALREADY);
  260. } /* end of execute_goto */
  261. /*+-------------------------------------------------------------------------
  262. show_error_position(pcb)
  263. cursor MUST be at left margin when this is called
  264. --------------------------------------------------------------------------*/
  265. void
  266. show_error_position(pcb)
  267. PCB *pcb;
  268. {
  269. ESD *tesd = pcb->current->text;
  270. int itmp = tesd->old_index;
  271. char tag[64];
  272. sprintf(tag, "%s %u> ", pcb->argv[0], pcb->current->lineno);
  273. pputs(tag);
  274. pputs(tesd->pb);
  275. pputs("\n");
  276. itmp = strlen(tag) + tesd->old_index;
  277. while (itmp--)
  278. pputc(' ');
  279. pputs("^\n");
  280. } /* end of show_error_position */
  281. /*+-------------------------------------------------------------------------
  282. find_proc_cmd(cmd_list,cmd)
  283. --------------------------------------------------------------------------*/
  284. P_CMD *
  285. find_proc_cmd(cmd_list, cmd)
  286. P_CMD *cmd_list;
  287. char *cmd;
  288. {
  289. while (cmd_list->token != -1)
  290. {
  291. if (!strcmp(cmd_list->cmd, cmd))
  292. break;
  293. cmd_list++;
  294. }
  295. return ((cmd_list->token == -1) ? (P_CMD *) 0 : cmd_list);
  296. } /* end of find_proc_cmd */
  297. /*+-------------------------------------------------------------------------
  298. execute_esd(tesd)
  299. --------------------------------------------------------------------------*/
  300. int
  301. execute_esd(tesd)
  302. ESD *tesd;
  303. {
  304. int erc;
  305. P_CMD *pcmd;
  306. static P_CMD *set_pcmd = (P_CMD *) 0; /* quick access to 'set' */
  307. char cmd[32];
  308. extern int proc_interrupt;
  309. /* if interrupt, exit */
  310. if (ck_sigint() | proc_interrupt)
  311. return (eCONINT);
  312. /* if blank, skip it */
  313. if (skip_cmd_break(tesd))
  314. return (0);
  315. /* if comment, skip it */
  316. if (!skip_cmd_char(tesd, '#'))
  317. return (0);
  318. if (*(tesd->pb + tesd->index) == '{')
  319. {
  320. pputs("invalid '{'\n");
  321. return (eFATAL_ALREADY);
  322. }
  323. while (1)
  324. {
  325. /* get command -- allow leading '$' to assume 'set' command */
  326. if (*(tesd->pb + tesd->index) == '#')
  327. break;
  328. if (*(tesd->pb + tesd->index) == '$')
  329. {
  330. /* find 'set' in the list -- save for rapid access later */
  331. if (set_pcmd)
  332. pcmd = set_pcmd;
  333. else if (!(pcmd = find_proc_cmd(icmd_cmds, "set")))
  334. return (eInternalLogicError);
  335. else
  336. set_pcmd = pcmd;
  337. }
  338. else
  339. {
  340. if (get_alphanum_zstr(tesd, cmd, sizeof(cmd)))
  341. return (eIllegalCommand);
  342. /* find it in the list */
  343. if ((pcmd = find_proc_cmd(icmd_cmds, cmd)) == (P_CMD *) 0)
  344. return (eIllegalCommand);
  345. }
  346. /* check to see if this command available for procedure */
  347. if (!pcmd->proc)
  348. return (eInteractiveCmd);
  349. /* execute the command */
  350. if (erc = (*pcmd->proc) (tesd))
  351. return (erc);
  352. /* look for comment */
  353. if (!skip_cmd_char(tesd, '#'))
  354. break;
  355. /* look for multiple commands on line */
  356. if (skip_cmd_char(tesd, ';'))
  357. break;
  358. /* if blank after ';', skip it */
  359. if (skip_cmd_break(tesd))
  360. break;
  361. }
  362. return (0);
  363. } /* end of execute_esd */
  364. /*+-------------------------------------------------------------------------
  365. execute_labelled_lcb(tesd)
  366. --------------------------------------------------------------------------*/
  367. execute_labelled_lcb(tesd)
  368. ESD *tesd;
  369. {
  370. int itmp = 0;
  371. int cb = tesd->cb;
  372. char *pb = tesd->pb;
  373. /* reset indices */
  374. tesd->index = itmp;
  375. tesd->old_index = itmp;
  376. /* if comment, skip it */
  377. if (!skip_cmd_char(tesd, '#'))
  378. return (0);
  379. /* skip over any label */
  380. while (!isspace((uchar) * (pb + itmp)) && (itmp < cb))
  381. itmp++;
  382. tesd->index = itmp;
  383. tesd->old_index = itmp;
  384. return (execute_esd(tesd));
  385. } /* end of execute_labelled_lcb */
  386. /*+-------------------------------------------------------------------------
  387. dump_proc(pcb)
  388. --------------------------------------------------------------------------*/
  389. #if 0
  390. void
  391. dump_proc(pcb)
  392. PCB *pcb;
  393. {
  394. int itmp;
  395. LCB *lcb;
  396. pprintf("------ pcb @ 0x%08lx -----------------\n", pcb);
  397. pprintf("argc=%d first=0x%08lx last=0x%08lx\n", pcb->argc,
  398. pcb->first, pcb->last);
  399. for (itmp = 0; itmp < pcb->argc; itmp++)
  400. {
  401. pprintf("argv(%d) @ 0x%lx: '%s'\n", itmp, pcb->argv[itmp],
  402. pcb->argv[itmp]);
  403. }
  404. pputs("\n");
  405. lcb = pcb->first;
  406. while (lcb)
  407. {
  408. pprintf("lcb @ 0x%08lx lineno=%u\n", lcb, lcb->lineno);
  409. pputs("\n");
  410. lcb = lcb->next;
  411. }
  412. pflush();
  413. } /* end of dump_proc */
  414. #endif
  415. /*+-------------------------------------------------------------------------
  416. trace_proc_cmd(pcb) - if asked, show command
  417. --------------------------------------------------------------------------*/
  418. void
  419. trace_proc_cmd(pcb)
  420. PCB *pcb;
  421. {
  422. if (proc_trace)
  423. {
  424. pprintf("%s %u> ", pcb->argv[0], pcb->current->lineno);
  425. pputs(pcb->current->text->pb);
  426. pputc('\n');
  427. }
  428. } /* end of trace_proc_cmd */
  429. /*+-------------------------------------------------------------------------
  430. proc_dcdloss_handler(pcb) - a statement execution found DCD loss
  431. --------------------------------------------------------------------------*/
  432. int
  433. proc_dcdloss_handler(pcb)
  434. PCB *pcb;
  435. {
  436. int erc = 0;
  437. int itmp;
  438. ESD esdcopy;
  439. ESD *tesd;
  440. if (pcb->upon_dcdloss.pb)
  441. {
  442. esdcopy = pcb->upon_dcdloss; /* a copy to preserve pcb->index */
  443. tesd = &esdcopy;
  444. if (proc_trace)
  445. {
  446. pprintf("%s DCDLOSS> ", pcb->argv[0]);
  447. pputs(tesd->pb + tesd->index);
  448. pputc('\n');
  449. }
  450. if (erc = execute_esd(tesd))
  451. {
  452. if (erc != eFATAL_ALREADY)
  453. proc_error(erc);
  454. pprintf("error in 'upon dcdloss' statement\n");
  455. pputs(tesd->pb + pcb->upon_dcdloss.index);
  456. pputs("\n");
  457. itmp = tesd->old_index - pcb->upon_dcdloss.index;;
  458. while (itmp--)
  459. pputc(' ');
  460. pputs("^\ninvoked while executing:\n");
  461. erc = eFATAL_ALREADY;
  462. }
  463. }
  464. else
  465. /* DCD watch enabled but no 'upon dcdloss' in effect */
  466. {
  467. pprintf("Connection terminated during procedure execution\n");
  468. pputs("while executing:\n");
  469. erc = eFATAL_ALREADY;
  470. }
  471. return (erc);
  472. } /* end of proc_dcdloss_handler */
  473. /*+-------------------------------------------------------------------------
  474. execute_proc(pcb,use_goto_label) - execute a memory-resident procedure
  475. --------------------------------------------------------------------------*/
  476. int
  477. execute_proc(pcb, use_goto_label)
  478. PCB *pcb;
  479. int use_goto_label;
  480. {
  481. int erc = 0;
  482. extern int proc_interrupt;
  483. /*
  484. * if first level, revert to keeping "local" variables created in
  485. * nested procedures
  486. */
  487. if (!proc_level)
  488. proc_option_localvars = 0;
  489. /*
  490. * nested too deeply?
  491. */
  492. if (proc_level == PROC_STACK_MAX)
  493. return (eProcStackTooDeep);
  494. /*
  495. * nest procedure level
  496. */
  497. pcb_stack[proc_level++] = pcb;
  498. if (use_goto_label)
  499. {
  500. if (erc = execute_goto(pcb, use_goto_label))
  501. return (erc);
  502. }
  503. else
  504. pcb->current = pcb->first;
  505. /*
  506. * mark variable "stack frame"
  507. */
  508. mkv_proc_starting(pcb);
  509. /*
  510. * walk the procedure step by step while Turing watches
  511. */
  512. while (pcb->current)
  513. {
  514. /* execute the command */
  515. trace_proc_cmd(pcb);
  516. if (erc = execute_labelled_lcb(pcb->current->text))
  517. {
  518. /* handle other classes of errors */
  519. switch (erc & 0xF000)
  520. {
  521. case e_WARNING: /* warning */
  522. erc = 0;
  523. break;
  524. case e_FATAL: /* fatal */
  525. goto FUNC_RETURN;
  526. case e_ProcAttn: /* proc attention */
  527. switch (erc)
  528. {
  529. case eProcAttn_GOTO:
  530. case eProcAttn_GOTOB:
  531. if (erc = execute_goto(pcb, erc))
  532. break; /* didn't find it */
  533. continue; /* pcb->current is now goto target */
  534. case eProcAttn_RETURN:
  535. erc = 0;
  536. break;
  537. case eProcAttn_Interrupt:
  538. case eProcAttn_ESCAPE:
  539. pprintf(
  540. "procedure %s interrupted.\n", pcb->argv[0]);
  541. erc = eFATAL_ALREADY;
  542. break;
  543. case eProcAttn_DCDloss:
  544. erc = proc_dcdloss_handler(pcb);
  545. break;
  546. default:
  547. pprintf("procedure error 0x%x\n", erc);
  548. erc = eFATAL_ALREADY;
  549. break;
  550. }
  551. goto FUNC_RETURN;
  552. default: /* must be proc return error code */
  553. goto FUNC_RETURN;
  554. }
  555. }
  556. if (ck_sigint() || proc_interrupt)
  557. {
  558. proc_interrupt = 0;
  559. sigint = 0;
  560. pprintf("procedure %s interrupted\n", pcb->argv[0]);
  561. erc = eFATAL_ALREADY;
  562. }
  563. if (erc)
  564. break;
  565. pcb->current = pcb->current->next;
  566. }
  567. /*
  568. * all exits through here
  569. */
  570. FUNC_RETURN:
  571. /*
  572. * we want to clean up the variable stack here (delete variables
  573. * created by the exiting procedure level) IF AND ONLY IF:
  574. *
  575. * 1. this is a real procedure exiting (and not just a gosub) 2. the user
  576. * has requested the feature with 'option localvars'
  577. */
  578. if (!use_goto_label || proc_option_localvars)
  579. mkv_proc_terminating(pcb);
  580. /*
  581. * handle errors: any non-zero value will terminate procedure
  582. * execution precipitously by unraveling the stack at every level
  583. * right here, spilling diagnostics as we recede to level 0
  584. */
  585. if (erc)
  586. {
  587. if ((erc > 0) && (erc < e_USER))
  588. {
  589. pprintf(">>procedure %s returned %d\n", pcb->argv[0], erc);
  590. erc |= e_USER;
  591. }
  592. else if ((erc > e_USER) && (erc <= 0x1FFF))
  593. {
  594. ; /* already said it */
  595. }
  596. else
  597. {
  598. if (erc != eFATAL_ALREADY)
  599. {
  600. proc_error(erc);
  601. erc = eFATAL_ALREADY;
  602. }
  603. show_error_position(pcb);
  604. }
  605. }
  606. /*
  607. * un-nest a level (returning from a gosub or exiting a procedure
  608. */
  609. pcb_stack[--proc_level] = (PCB *) 0;
  610. /*
  611. * if last procedure is exiting, make sure any opened files are closed
  612. */
  613. if (!proc_level)
  614. proc_file_reset();
  615. return (erc);
  616. } /* end of execute_proc */
  617. /*+-------------------------------------------------------------------------
  618. free_lcb_chain(lcb)
  619. --------------------------------------------------------------------------*/
  620. void
  621. free_lcb_chain(lcb)
  622. LCB *lcb;
  623. {
  624. LCB *plcb;
  625. while (lcb)
  626. {
  627. if (lcb->text)
  628. esdfree(lcb->text);
  629. plcb = lcb;
  630. lcb = lcb->next;
  631. free((char *)plcb);
  632. }
  633. } /* end of free_lcb_chain */
  634. /*+-------------------------------------------------------------------------
  635. find_procedure(name) - find procedure if it exists
  636. --------------------------------------------------------------------------*/
  637. char *
  638. find_procedure(name)
  639. char *name;
  640. {
  641. static char procpath[ECU_MAXPN];
  642. /*
  643. * try to find proc file in current directory
  644. */
  645. strcpy(procpath, name);
  646. strcat(procpath, ".ep");
  647. if (!access(procpath, 4))
  648. goto RETURN_PATH;
  649. if (proc_trace && (errno != ENOENT))
  650. pperror(procpath);
  651. /*
  652. * try to find proc file in home .ecu subdirectory
  653. */
  654. get_home_dir(procpath);
  655. strcat(procpath, "/.ecu/");
  656. strcat(procpath, name);
  657. strcat(procpath, ".ep");
  658. if (!access(procpath, 4))
  659. goto RETURN_PATH;
  660. if (proc_trace && (errno != ENOENT))
  661. pperror(procpath);
  662. /*
  663. * try to find proc file in library ep subdirectory
  664. */
  665. strcpy(procpath, CFG_EcuLibDir);
  666. strcat(procpath, "/ep/");
  667. strcat(procpath, name);
  668. strcat(procpath, ".ep");
  669. if (!access(procpath, 4))
  670. goto RETURN_PATH;
  671. if (proc_trace && (errno != ENOENT) && (errno != ENOTDIR))
  672. pperror(procpath);
  673. /*
  674. * no luck
  675. */
  676. return (0);
  677. /*
  678. * luck -- hack away //
  679. */
  680. RETURN_PATH:
  681. if ((procpath[0] == '/') && (procpath[1] == '/'))
  682. return (procpath + 1);
  683. return (procpath);
  684. } /* end of find_procedure */
  685. /*+-------------------------------------------------------------------------
  686. do_proc(argc,argv) - read in a disk-based procedure and execute it
  687. Acute and particular attention has been paid to the order of
  688. memory allocation and object linking in this function; if any
  689. error occurs, branching to FUNC_EXIT with erc non-zero
  690. guarrantees proper unravelling/freeing of buffers allocated by
  691. this pass through.
  692. --------------------------------------------------------------------------*/
  693. do_proc(argc, argv)
  694. int argc;
  695. char **argv;
  696. {
  697. int itmp;
  698. int itmp2;
  699. int erc;
  700. int iargv;
  701. char *pargv[MAX_PARGV];
  702. int ipargv = 0;
  703. char cmdbuf[ESD_NOMSZ];
  704. char *procpath;
  705. FILE *fp = 0;
  706. PCB *pcb = (PCB *) 0;
  707. LCB *lcb = (LCB *) 0;
  708. LCB *plcb;
  709. UINT16 line_count = 0;
  710. extern UINT32 colors_current;
  711. UINT32 colors_at_entry = colors_current;
  712. extern int proc_interrupt;
  713. proc_interrupt = 0; /* ok to reset here because no one ... */
  714. sigint = 0; /* ... would call here if interrupted */
  715. for (iargv = 0; iargv < argc; iargv++)
  716. {
  717. if (ipargv == MAX_PARGV)
  718. {
  719. pputs("\nMax arguments to procedure invocation exceeded\n");
  720. erc = eFATAL_ALREADY;
  721. goto FUNC_RETURN;
  722. }
  723. pargv[ipargv++] = argv[iargv];
  724. }
  725. if (!ipargv)
  726. {
  727. pputs("\nno procedure name given\n");
  728. erc = eFATAL_ALREADY;
  729. goto FUNC_RETURN;
  730. }
  731. if (!(procpath = find_procedure(pargv[0])))
  732. {
  733. pprintf("\nprocedure %s not found\n", pargv[0]);
  734. erc = eFATAL_ALREADY;
  735. goto FUNC_RETURN;
  736. }
  737. fp = fopen(procpath, "r");
  738. if (!fp)
  739. {
  740. pperror(procpath);
  741. erc = eFATAL_ALREADY;
  742. goto FUNC_RETURN;
  743. }
  744. if (proc_trace)
  745. pprintf("DO: %s\n", procpath);
  746. if (!(pcb = (PCB *) calloc(1, sizeof(PCB))))
  747. {
  748. erc = eNoMemory;
  749. goto FUNC_RETURN;
  750. }
  751. pcb->argv = pargv;
  752. pcb->argc = ipargv;
  753. plcb = 0;
  754. line_count = 0;
  755. while (1)
  756. {
  757. /*
  758. * read procedure file
  759. */
  760. if (!(fgets(cmdbuf, sizeof(cmdbuf), fp)))
  761. break;
  762. line_count++;
  763. /*
  764. * housekeeping
  765. */
  766. itmp = strlen(cmdbuf) - 1; /* skip blank lines */
  767. if (!itmp)
  768. continue;
  769. cmdbuf[itmp] = 0; /* kill trailing NL */
  770. if (cmdbuf[0] == '#')/* skip comments */
  771. continue;
  772. /*
  773. * convert tabs to spaces so we don't have to scan for each
  774. */
  775. for (itmp2 = 0; itmp2 < itmp; itmp2++)
  776. {
  777. if (cmdbuf[itmp2] == TAB)
  778. cmdbuf[itmp2] = SPACE;
  779. }
  780. /*
  781. * get a line control block
  782. */
  783. if (!(lcb = (LCB *) malloc(sizeof(LCB))))
  784. {
  785. erc = eNoMemory;
  786. goto FUNC_RETURN;
  787. }
  788. /*
  789. * link it into the pcb chain
  790. */
  791. lcb->prev = plcb;
  792. lcb->next = 0;
  793. lcb->lineno = line_count;
  794. if (plcb)
  795. plcb->next = lcb;
  796. else
  797. pcb->first = lcb;
  798. /*
  799. * now copy in the text
  800. */
  801. if (!(lcb->text = esdalloc(itmp)))
  802. {
  803. erc = eNoMemory;
  804. goto FUNC_RETURN;
  805. }
  806. strcpy(lcb->text->pb, cmdbuf);
  807. lcb->text->cb = itmp;
  808. esd_null_terminate(lcb->text);
  809. plcb = lcb;
  810. pcb->last = lcb;
  811. lcb = 0;
  812. }
  813. /*
  814. * error or not, all done reading the proc; if no error, execute the
  815. * procedure
  816. */
  817. if (line_count)
  818. {
  819. fclose(fp);
  820. fp = 0;
  821. erc = execute_proc(pcb, 0);
  822. }
  823. else
  824. erc = eProcEmpty;
  825. FUNC_RETURN:
  826. if (fp)
  827. fclose(fp);
  828. if (lcb)
  829. {
  830. if (lcb->text)
  831. esdfree(lcb->text);
  832. free((char *)lcb);
  833. }
  834. if (pcb)
  835. {
  836. if (pcb->first)
  837. free_lcb_chain(pcb->first);
  838. free((char *)pcb);
  839. }
  840. if ((erc > e_USER) && (erc <= 0x1FFF))
  841. erc -= e_USER;
  842. if (erc > e_USER)
  843. setcolor(colors_at_entry);
  844. return (erc);
  845. } /* end of do_proc */
  846. /*+-------------------------------------------------------------------------
  847. pcmd_do(param)
  848. --------------------------------------------------------------------------*/
  849. pcmd_do(param)
  850. ESD *param;
  851. {
  852. int erc;
  853. int ipargv;
  854. char *cmd_copy;
  855. char *pargv[MAX_PARGV];
  856. ESD *pargv_esd[MAX_PARGV];
  857. int pargc = 0;
  858. if (!(cmd_copy = (char *)malloc(param->cb)))
  859. return (eNoMemory);
  860. strcpy(cmd_copy, param->pb + param->old_index);
  861. while (pargc != MAX_PARGV)
  862. {
  863. if (end_of_cmd(param))
  864. break;
  865. if (!(pargv_esd[pargc] = esdalloc(ESD_NOMSZ)))
  866. {
  867. erc = eNoMemory;
  868. goto FUNC_RETURN;
  869. }
  870. if (erc = gstr(param, pargv_esd[pargc], 1))
  871. goto FUNC_RETURN;
  872. pargv[pargc] = pargv_esd[pargc]->pb;
  873. pargc++;
  874. }
  875. if (pargc < MAX_PARGV)
  876. erc = do_proc(pargc, pargv);
  877. else
  878. {
  879. pprintf("too many arguments to procedure\n");
  880. erc = eFATAL_ALREADY;
  881. }
  882. FUNC_RETURN:
  883. free(cmd_copy);
  884. for (ipargv = 0; ipargv < pargc; ipargv++)
  885. esdfree(pargv_esd[ipargv]);
  886. return (erc);
  887. } /* end of pcmd_do */
  888. /* vi: set tabstop=4 shiftwidth=4: */
  889. /* end of proc.c */