commands.g 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /* $Id$ */
  2. /* Command grammar */
  3. {
  4. #include <stdio.h>
  5. #include <alloc.h>
  6. #include <signal.h>
  7. #include "ops.h"
  8. #include "class.h"
  9. #include "position.h"
  10. #include "file.h"
  11. #include "idf.h"
  12. #include "symbol.h"
  13. #include "tree.h"
  14. #include "langdep.h"
  15. #include "token.h"
  16. #include "expr.h"
  17. #include "misc.h"
  18. extern char *Salloc();
  19. extern char *strchr();
  20. extern char *strcpy();
  21. extern void signal_child();
  22. extern FILE *db_in;
  23. extern int disable_intr;
  24. extern p_tree run_command, print_command;
  25. struct token tok, aside;
  26. int errorgiven = 0;
  27. int child_interrupted = 0;
  28. int interrupted = 0;
  29. int eof_seen = 0;
  30. static int shellescape();
  31. static int extended_charset = 0;
  32. static int in_expression = 0;
  33. #define binprio(op) ((*(currlang->binop_prio))(op))
  34. #define unprio(op) ((*(currlang->unop_prio))(op))
  35. }
  36. %start Commands, commands;
  37. %lexical LLlex;
  38. commands
  39. { p_tree com, lastcom = 0;
  40. int give_prompt;
  41. }
  42. :
  43. { errorgiven = 0; }
  44. [ %persistent command_line(&com)
  45. [ '\n' { give_prompt = 1; }
  46. | %default ';' { give_prompt = 0; }
  47. ]
  48. { if (com) {
  49. if (lastcom) {
  50. freenode(lastcom);
  51. lastcom = 0;
  52. }
  53. if (errorgiven) {
  54. if (com != run_command) freenode(com);
  55. com = 0;
  56. }
  57. else {
  58. enterlog(com);
  59. eval(com);
  60. if (repeatable(com)) {
  61. lastcom = com;
  62. }
  63. else if (! in_status(com) &&
  64. com != run_command &&
  65. com != print_command) {
  66. freenode(com);
  67. com = 0;
  68. }
  69. }
  70. } else if (lastcom && ! errorgiven) {
  71. enterlog(lastcom);
  72. eval(lastcom);
  73. }
  74. if (give_prompt) {
  75. errorgiven = 0;
  76. interrupted = 0;
  77. prompt();
  78. }
  79. }
  80. ]*
  81. ;
  82. command_line(p_tree *p;)
  83. :
  84. { *p = 0; }
  85. [
  86. list_command(p)
  87. | file_command(p)
  88. | run_command(p)
  89. | stop_command(p)
  90. | when_command(p)
  91. | continue_command(p)
  92. | step_command(p)
  93. | next_command(p)
  94. | regs_command(p)
  95. | where_command(p)
  96. | STATUS { *p = mknode(OP_STATUS); }
  97. | DUMP { *p = mknode(OP_DUMP); }
  98. | RESTORE opt_num(p) { *p = mknode(OP_RESTORE, *p); }
  99. | delete_command(p)
  100. | print_command(p)
  101. | display_command(p)
  102. | trace_command(p)
  103. | set_command(p)
  104. | help_command(p)
  105. | FIND qualified_name(p){ *p = mknode(OP_FIND, *p); }
  106. | WHICH qualified_name(p){ *p = mknode(OP_WHICH, *p); }
  107. | able_command(p)
  108. | '!' { (void) shellescape();
  109. *p = mknode(OP_SHELL);
  110. }
  111. | source_command(p)
  112. | log_command(p)
  113. | frame_command(p)
  114. |
  115. ]
  116. ;
  117. frame_command(p_tree *p;)
  118. :
  119. FRAME
  120. [ { *p = mknode(OP_FRAME, (p_tree) 0); }
  121. | count(p) { *p = mknode(OP_FRAME, *p); }
  122. | '-' count(p) { *p = mknode(OP_DOWN, *p); }
  123. | '+' count(p) { *p = mknode(OP_UP, *p); }
  124. ]
  125. ;
  126. source_command(p_tree *p;)
  127. :
  128. SOURCE { extended_charset = 1; }
  129. name(p) { (*p)->t_idf = str2idf((*p)->t_str, 0); }
  130. { *p = mknode(OP_SOURCE, *p);
  131. extended_charset = 0;
  132. }
  133. ;
  134. log_command(p_tree *p;)
  135. :
  136. LOG { extended_charset = 1; }
  137. [ name(p) { (*p)->t_idf = str2idf((*p)->t_str, 0); }
  138. | { *p = 0; }
  139. ]
  140. { *p = mknode(OP_LOG, *p);
  141. extended_charset = 0;
  142. }
  143. ;
  144. where_command(p_tree *p;)
  145. :
  146. WHERE opt_num(p) { *p = mknode(OP_WHERE, *p); }
  147. ;
  148. list_command(p_tree *p;)
  149. { p_tree t1 = 0, t2 = 0; }
  150. :
  151. LIST
  152. [
  153. | position(&t1)
  154. | qualified_name(&t1)
  155. ]
  156. [ ',' count(&t2)
  157. | '-'
  158. [ count(&t2) { t2->t_ival = - t2->t_ival; }
  159. | { t2 = mknode(OP_INTEGER, -100000000L); }
  160. ]
  161. |
  162. ]
  163. { *p = mknode(OP_LIST, t1, t2); }
  164. ;
  165. file_command(p_tree *p;)
  166. :
  167. XFILE { extended_charset = 1; }
  168. [ { *p = 0; }
  169. | name(p) { (*p)->t_idf = str2idf((*p)->t_str, 0); }
  170. ] { *p = mknode(OP_FILE, *p);
  171. extended_charset = 0;
  172. }
  173. ;
  174. help_command(p_tree *p;)
  175. :
  176. [ HELP | '?' ]
  177. { *p = mknode(OP_HELP, (p_tree) 0); }
  178. [ name(&(*p)->t_args[0])?
  179. | '?' { (*p)->t_args[0] = mknode(OP_NAME, str2idf("help",0), (char *) 0); }
  180. | '!' { (*p)->t_args[0] = mknode(OP_NAME, (struct idf *) 0, "!"); }
  181. ]
  182. ;
  183. run_command(p_tree *p;)
  184. :
  185. RUN { extended_charset = 1; }
  186. args(p) { *p = mknode(OP_RUN, *p);
  187. extended_charset = 0;
  188. }
  189. | RERUN { if (! run_command) {
  190. error("no run command given yet");
  191. }
  192. else *p = run_command;
  193. }
  194. [ '?' { *p = mknode(OP_PRCOMM, *p); }
  195. |
  196. ]
  197. ;
  198. stop_command(p_tree *p;)
  199. { p_tree whr = 0, cond = 0; }
  200. :
  201. STOP
  202. where(&whr)?
  203. condition(&cond)? { if (! whr && ! cond) {
  204. error("no position or condition");
  205. *p = 0;
  206. }
  207. else *p = mknode(OP_STOP, whr, cond);
  208. }
  209. ;
  210. trace_command(p_tree *p;)
  211. { p_tree whr = 0, cond = 0, exp = 0; }
  212. :
  213. TRACE
  214. [ ON format_expression(&exp) ]?
  215. where(&whr)?
  216. condition(&cond)? { *p = mknode(OP_TRACE, whr, cond, exp); }
  217. ;
  218. continue_command(p_tree *p;)
  219. { long l; p_tree pos = 0; }
  220. :
  221. CONT
  222. [ INTEGER { l = tok.ival; }
  223. | { l = 1; }
  224. ]
  225. [ AT position(&pos) ]?
  226. { *p = mknode(OP_CONT, mknode(OP_INTEGER, l), pos); }
  227. ;
  228. when_command(p_tree *p;)
  229. { p_tree whr = 0, cond = 0; }
  230. :
  231. WHEN
  232. where(&whr)?
  233. condition(&cond)? { *p = mknode(OP_WHEN, whr, cond, (p_tree) 0);
  234. p = &(*p)->t_args[2];
  235. }
  236. '{'
  237. command_line(p)
  238. [ ';' { if (*p) {
  239. *p = mknode(OP_LINK, *p, (p_tree) 0);
  240. p = &((*p)->t_args[1]);
  241. }
  242. }
  243. command_line(p)
  244. ]*
  245. '}'
  246. { if (! whr && ! cond) {
  247. error("no position or condition");
  248. }
  249. else if (! *p) {
  250. error("no commands given");
  251. }
  252. }
  253. ;
  254. step_command(p_tree *p;)
  255. :
  256. STEP { *p = mknode(OP_STEP, (p_tree) 0); }
  257. count(&(*p)->t_args[0])?
  258. ;
  259. next_command(p_tree *p;)
  260. :
  261. NEXT { *p = mknode(OP_NEXT, (p_tree) 0); }
  262. count(&(*p)->t_args[0])?
  263. ;
  264. regs_command(p_tree *p;)
  265. :
  266. REGS { *p = mknode(OP_REGS, (p_tree) 0); }
  267. count(&(*p)->t_args[0])?
  268. ;
  269. delete_command(p_tree *p;)
  270. :
  271. DELETE num_list(p)? { *p = mknode(OP_DELETE, *p); }
  272. ;
  273. print_command(p_tree *p;)
  274. :
  275. PRINT
  276. [ format_expression_list(p)
  277. { *p = mknode(OP_PRINT, *p); }
  278. |
  279. { *p = mknode(OP_PRINT, (p_tree) 0); }
  280. ]
  281. ;
  282. display_command(p_tree *p;)
  283. :
  284. DISPLAY format_expression_list(p)
  285. { *p = mknode(OP_DISPLAY, *p); }
  286. ;
  287. format_expression_list(p_tree *p;)
  288. :
  289. format_expression(p)
  290. [ ',' { *p = mknode(OP_LINK, *p, (p_tree) 0);
  291. p = &((*p)->t_args[1]);
  292. }
  293. format_expression(p)
  294. ]*
  295. ;
  296. format_expression(p_tree *p;)
  297. { p_tree p1; }
  298. :
  299. expression(p, 0)
  300. [ '\\' name(&p1) { register char *c = p1->t_str;
  301. while (*c) {
  302. if (! strchr("doshcax", *c)) {
  303. error("illegal format: %c", *c);
  304. break;
  305. }
  306. c++;
  307. }
  308. *p = mknode(OP_FORMAT, *p, p1);
  309. }
  310. |
  311. ]
  312. ;
  313. set_command(p_tree *p;)
  314. :
  315. SET expression(p, 0) { *p = mknode(OP_SET, *p, (p_tree) 0); }
  316. TO expression(&((*p)->t_args[1]), 0)
  317. ;
  318. able_command(p_tree *p;)
  319. :
  320. [ ENABLE { *p = mknode(OP_ENABLE, (p_tree) 0); }
  321. | DISABLE { *p = mknode(OP_DISABLE, (p_tree) 0); }
  322. ]
  323. num_list(&(*p)->t_args[0])?
  324. ;
  325. num_list(p_tree *p;)
  326. :
  327. num(p)
  328. [ ',' { *p = mknode(OP_LINK, *p, (p_tree) 0); }
  329. num(&(*p)->t_args[1])
  330. ]*
  331. ;
  332. condition(p_tree *p;)
  333. :
  334. IF expression(p, 0)
  335. ;
  336. where(p_tree *p;)
  337. :
  338. IN qualified_name(p) { *p = mknode(OP_IN, *p, (p_tree) 0); }
  339. [ AT position(&((*p)->t_args[1])) ]?
  340. |
  341. AT position(p)
  342. ;
  343. expression(p_tree *p; int level;)
  344. { int currprio, currop; }
  345. : { in_expression++; }
  346. factor(p)
  347. [ %while ((currprio = binprio(currop = (int) tok.ival)) > level)
  348. [ BIN_OP | PREF_OR_BIN_OP ]
  349. { *p = mknode(OP_BINOP, *p, (p_tree) 0);
  350. (*p)->t_whichoper = currop;
  351. }
  352. expression(&((*p)->t_args[1]), currprio)
  353. |
  354. SEL_OP { *p = mknode(OP_BINOP, *p, (p_tree) 0);
  355. (*p)->t_whichoper = (int) tok.ival;
  356. }
  357. name(&(*p)->t_args[1])
  358. |
  359. '[' { *p = mknode(OP_BINOP, *p, (p_tree) 0);
  360. (*p)->t_whichoper = E_ARRAY;
  361. }
  362. expression(&(*p)->t_args[1], 0)
  363. [ ',' { *p = mknode(OP_BINOP, *p, (p_tree) 0);
  364. (*p)->t_whichoper = E_ARRAY;
  365. }
  366. expression(&(*p)->t_args[1], 0)
  367. ]*
  368. ']'
  369. ]*
  370. { in_expression--; }
  371. ;
  372. factor(p_tree *p;)
  373. :
  374. [
  375. %default EXPRESSION /* lexical analyzer will never return this token */
  376. { *p = mknode(OP_INTEGER, 0L); }
  377. |
  378. '(' expression(p, 0) ')'
  379. |
  380. INTEGER { *p = mknode(OP_INTEGER, tok.ival); }
  381. |
  382. REAL { *p = mknode(OP_REAL, tok.fval); }
  383. |
  384. STRING { *p = mknode(OP_STRING, tok.str); }
  385. |
  386. qualified_name(p)
  387. |
  388. { *p = mknode(OP_UNOP, (p_tree) 0);
  389. (*p)->t_whichoper = (int) tok.ival;
  390. }
  391. [ PREF_OP
  392. | PREF_OR_BIN_OP
  393. { (*currlang->fix_bin_to_pref)(*p); }
  394. ]
  395. expression(&(*p)->t_args[0], unprio((*p)->t_whichoper))
  396. ]
  397. [ %while(1)
  398. POST_OP { *p = mknode(OP_UNOP, *p);
  399. (*p)->t_whichoper = (int) tok.ival;
  400. }
  401. ]*
  402. ;
  403. position(p_tree *p;)
  404. { p_tree lin;
  405. char *str;
  406. }
  407. :
  408. [ STRING { str = tok.str; }
  409. ':'
  410. | { if (! listfile) str = 0;
  411. else str = listfile->sy_idf->id_text;
  412. }
  413. ]
  414. count(&lin) { *p = mknode(OP_AT, lin->t_ival, str);
  415. freenode(lin);
  416. }
  417. ;
  418. args(p_tree *p;)
  419. { int first_time = 1; }
  420. :
  421. [ { if (! first_time) {
  422. *p = mknode(OP_LINK, *p, (p_tree) 0);
  423. p = &((*p)->t_args[1]);
  424. }
  425. first_time = 0;
  426. }
  427. arg(p)
  428. ]*
  429. ;
  430. arg(p_tree *p;)
  431. :
  432. name(p)
  433. |
  434. '>' name(p) { (*p)->t_oper = OP_OUTPUT; }
  435. |
  436. '<' name(p) { (*p)->t_oper = OP_INPUT; }
  437. ;
  438. count(p_tree *p;)
  439. :
  440. INTEGER { *p = mknode(OP_INTEGER, tok.ival); }
  441. ;
  442. opt_num(p_tree *p;)
  443. :
  444. num(p)
  445. |
  446. { *p = 0; }
  447. ;
  448. num(p_tree *p;)
  449. :
  450. count(p)
  451. |
  452. '-' count(p) { (*p)->t_ival = - (*p)->t_ival; }
  453. ;
  454. qualified_name(p_tree *p;)
  455. :
  456. name(p)
  457. [ '`' { *p = mknode(OP_SELECT, *p, (p_tree) 0); }
  458. name(&((*p)->t_args[1]))
  459. ]*
  460. ;
  461. name(p_tree *p;)
  462. :
  463. [ XFILE
  464. | LIST
  465. | RUN
  466. | RERUN
  467. | STOP
  468. | WHEN
  469. | AT
  470. | IN
  471. | IF
  472. | %default NAME
  473. | CONT
  474. | STEP
  475. | NEXT
  476. | REGS
  477. | WHERE
  478. | STATUS
  479. | PRINT
  480. | DELETE
  481. | DUMP
  482. | RESTORE
  483. | TRACE
  484. | ON
  485. | SET
  486. | TO
  487. | FIND
  488. | DISPLAY
  489. | WHICH
  490. | HELP
  491. | DISABLE
  492. | ENABLE
  493. | SOURCE
  494. | FRAME
  495. | LOG
  496. ] { *p = mknode(OP_NAME, tok.idf, tok.str); }
  497. ;
  498. {
  499. int
  500. LLlex()
  501. {
  502. register int c;
  503. if (ASIDE) {
  504. tok = aside;
  505. ASIDE = 0;
  506. return TOK;
  507. }
  508. do {
  509. c = getc(db_in);
  510. if (interrupted && c == EOF) {
  511. c = ' ';
  512. interrupted = 0;
  513. continue;
  514. }
  515. } while (c != EOF && class(c) == STSKIP);
  516. if (c == EOF) {
  517. eof_seen = 1;
  518. return c;
  519. }
  520. if (extended_charset && in_ext(c)) {
  521. TOK = get_name(c);
  522. return TOK;
  523. }
  524. switch(class(c)) {
  525. case STSTR:
  526. TOK = (*currlang->get_string)(c);
  527. break;
  528. case STIDF:
  529. if (in_expression) TOK = (*currlang->get_name)(c);
  530. else TOK = get_name(c);
  531. break;
  532. case STNUM:
  533. TOK = (*currlang->get_number)(c);
  534. break;
  535. case STNL:
  536. TOK = c;
  537. break;
  538. case STSIMP:
  539. if (! in_expression) {
  540. TOK = c;
  541. break;
  542. }
  543. /* Fall through */
  544. default:
  545. TOK = (*currlang->get_token)(c);
  546. break;
  547. }
  548. return TOK;
  549. }
  550. int
  551. get_name(c)
  552. register int c;
  553. {
  554. char buf[512+1];
  555. register char *p = &buf[0];
  556. register struct idf *id;
  557. do {
  558. if (p - buf < 512) *p++ = c;
  559. c = getc(db_in);
  560. } while ((extended_charset && in_ext(c)) || in_idf(c));
  561. ungetc(c, db_in);
  562. *p++ = 0;
  563. if (extended_charset) {
  564. tok.idf = 0;
  565. tok.str = Salloc(buf, (unsigned) (p - buf));
  566. return NAME;
  567. }
  568. id = str2idf(buf, 1);
  569. tok.idf = id;
  570. tok.str = id->id_text;
  571. return id->id_reserved ? id->id_reserved : NAME;
  572. }
  573. extern char *symbol2str();
  574. LLmessage(t)
  575. {
  576. if (t > 0) {
  577. if (! errorgiven) {
  578. error("%s missing before %s", symbol2str(t), symbol2str(TOK));
  579. }
  580. aside = tok;
  581. }
  582. else if (t == 0) {
  583. if (! errorgiven) {
  584. error("%s unexpected", symbol2str(TOK));
  585. }
  586. }
  587. else if (! errorgiven) {
  588. error("EOF expected");
  589. }
  590. errorgiven = 1;
  591. }
  592. static void
  593. catch_del()
  594. {
  595. signal(SIGINT, catch_del);
  596. if (! disable_intr) {
  597. signal_child(7);
  598. child_interrupted = 1;
  599. }
  600. interrupted = 1;
  601. }
  602. void
  603. init_del()
  604. {
  605. signal(SIGINT, catch_del);
  606. }
  607. static void
  608. ctch()
  609. {
  610. /* Only for shell escapes ... */
  611. signal(SIGINT, ctch);
  612. }
  613. #define SHBUFSIZ 512
  614. static int
  615. shellescape()
  616. {
  617. register char *p; /* walks through command */
  618. static char previous[SHBUFSIZ]; /* previous command */
  619. char comm[SHBUFSIZ]; /* space for command */
  620. register int cnt; /* prevent array bound errors */
  621. register int c; /* current char */
  622. register int lastc = 0; /* will contain the previous char */
  623. p = comm;
  624. cnt = SHBUFSIZ-2;
  625. while (c = getc(db_in), c != '\n') {
  626. switch(c) {
  627. case '!':
  628. /*
  629. * An unescaped ! expands to the previous
  630. * command, but disappears if there is none
  631. */
  632. if (lastc != '\\') {
  633. if (*previous) {
  634. int len = strlen(previous);
  635. if ((cnt -= len) <= 0) break;
  636. strcpy(p,previous);
  637. p += len;
  638. }
  639. }
  640. else {
  641. *p++ = c;
  642. }
  643. continue;
  644. case '%':
  645. /*
  646. * An unescaped % will expand to the current
  647. * filename, but disappears is there is none
  648. */
  649. if (lastc != '\\') {
  650. if (listfile) {
  651. int len = strlen(listfile->sy_idf->id_text);
  652. if ((cnt -= len) <= 0) break;
  653. strcpy(p,listfile->sy_idf->id_text);
  654. p += len;
  655. }
  656. }
  657. else {
  658. *p++ = c;
  659. }
  660. continue;
  661. default:
  662. lastc = c;
  663. if (cnt-- <= 0) break;
  664. *p++ = c;
  665. continue;
  666. }
  667. break;
  668. }
  669. *p = '\0';
  670. if (c != '\n') {
  671. warning("shell command too long");
  672. while (c != '\n') c = getc(db_in);
  673. }
  674. ungetc(c, db_in);
  675. strcpy(previous, comm);
  676. signal(SIGINT, ctch);
  677. cnt = system(comm);
  678. signal(SIGINT, catch_del);
  679. return cnt;
  680. }
  681. }