home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / s / seyon197.tz / seyon197 / seyon / SeScript.c < prev    next >
C/C++ Source or Header  |  1993-02-20  |  9KB  |  604 lines

  1.  
  2. /*
  3.  * This file is part of the Seyon, Copyright (c) 1992-1993 by Muhammad M.
  4.  * Saggaf. All rights reserved.
  5.  *
  6.  * See the file COPYING (1-COPYING) or the manual page seyon(1) for a full
  7.  * statement of rights and permissions for this program.
  8.  */
  9.  
  10. /*
  11. Historical note:
  12.     Program    xcscrpt.c    Script handler for xcomm
  13.     Author    larry gensch, ESQ  December 4, 1987
  14.  
  15.     This code is released to the public domain
  16. */
  17.  
  18. #include <time.h>
  19.  
  20. #include <X11/Intrinsic.h>
  21. #include <X11/StringDefs.h>
  22.  
  23. #include "seyon.h"
  24. #include "SeDecl.h"
  25.  
  26. #define    MAX_PATH    256
  27. #define    MAX_LINE    128
  28.  
  29. #define GET_ARG() \
  30. { \
  31.   getword(); \
  32.   if (eof_flag) \
  33.     return; \
  34. }
  35.  
  36. #define GETTEST_ARG(cmd) \
  37. { \
  38.   GET_ARG(); \
  39.   if (word[0] == '\0') { \
  40.     se_errorf("missing argument to the command '%s'", cmd, "", ""); \
  41.     eof_flag = 1; \
  42.     return; \
  43.   } \
  44. }
  45.  
  46. extern char     scriptDirectory[REG_BUF];
  47. extern FILE    *tfp;
  48.  
  49. static FILE    *cf;
  50. int             waitfor_time = 0,
  51.                 if_flag = 0,
  52.                 waitflag = 0;
  53. Boolean         tty_flag = True,
  54.                 echo_flag = False,
  55.                 captflag = True;
  56. jmp_buf         here;
  57.  
  58. void            k_debug(),
  59.                 k_echo(),
  60.                 k_flush(),
  61.                 k_hangup(),
  62.                 k_purge(),
  63.                 k_send_break(),
  64.                 k_waitfor(),
  65.                 k_when(),
  66.                 k_transmit(),
  67.                 k_pause(),
  68.                 k_exit(),
  69.                 k_quit(),
  70.                 k_if(),
  71.                 k_goto(),
  72.                 k_else(),
  73.                 k_endif(),
  74.                 k_redial(),
  75.                 k_dial(),
  76.                 s_set(),
  77.                 hangup(),
  78.                 k_tty(),
  79.                 k_capture();
  80.  
  81. /* globals */
  82.  
  83. int             linkflag = 0;
  84. int             scriptflag = 0;
  85.  
  86. static struct kw kw[] =
  87. {
  88.   {"debug", k_debug},
  89.   {"echo", k_echo},
  90.   {"flush", k_flush},
  91.   {"hangup", k_hangup},
  92.   {"purge", k_purge},
  93.   {"send_break", k_send_break},
  94.   {"waitfor", k_waitfor},
  95.   {"when", k_when},
  96.   {"transmit", k_transmit},
  97.   {"pause", k_pause},
  98.   {"exit", k_exit},
  99.   {"if", k_if},
  100.   {"else", k_else},
  101.   {"endif", k_endif},
  102.   {"hangup", hangup},
  103.   {"goto", k_goto},
  104.   {"dial", k_dial},
  105.   {"redial", k_redial},
  106.   {"quit", k_quit},
  107.   {"set", s_set},
  108.   {"capture", k_capture},
  109.   {"tty", k_tty},
  110.   {NULL, NULL}};
  111.  
  112. Boolean
  113. do_script(file)
  114.      char           *file;
  115. {
  116.   char           *script_dir,
  117.                   buf[REG_BUF];
  118.  
  119.   FILE           *script_fp;
  120.  
  121.   if (qres.scriptDirectory == NULL)
  122.     script_dir = qres.defaultDirectory;
  123.   else
  124.     script_dir = qres.scriptDirectory;
  125.  
  126.   strcpy(buf, file);
  127.   if ((script_fp = open_file(buf, script_dir)) == NULL)
  128.     return False;
  129.  
  130.   exec_close_script(script_fp);
  131.   return True;
  132. }
  133.  
  134. void
  135. exec_close_script(script_fp)
  136.      FILE           *script_fp;
  137. {
  138.   if_flag = 0;
  139.   echo_flag = False;
  140.   captflag = False;
  141.   tty_flag = True;
  142.   scriptflag = 1;
  143.   eof_flag = 0;
  144.  
  145.   if (linkflag == 2)
  146.     linkflag = 0;
  147.  
  148.   while (!eof_flag)
  149.     get_line(script_fp);
  150.  
  151.   fclose(script_fp);
  152.   if (captflag)
  153.     fclose(cf);
  154.  
  155.   eof_flag = 0;
  156.   lptr = strcpy(line, "");
  157.   k_when();
  158.  
  159.   linkflag = 0;
  160.   scriptflag = 0;
  161.  
  162.   return;
  163. }
  164.  
  165. static char     wf[MAX_LINE];
  166.  
  167. void
  168. get_line(script_fp)
  169.      FILE           *script_fp;
  170. {
  171.   int             i;
  172.  
  173.   getline(script_fp);
  174.  
  175.   if (eof_flag)
  176.     return;
  177.  
  178.   getword();
  179.  
  180.   if (echo_flag) {
  181.     if (strcmp(word, "transmit"))
  182.       show(line);
  183.     else
  184.       show("TRANSMIT...");
  185.   }
  186.  
  187.   if (word[0] == '\0')           /* Ignore blank lines */
  188.     return;
  189.   if (word[0] == '#')           /* Ignore comments */
  190.     return;
  191.  
  192.   if (word[strlen(word) - 1] == ':')    /* Ignore labels */
  193.     return;
  194.  
  195.   if (if_flag == -1) {
  196.     if (strcmp(word, "else") && strcmp(word, "endif"))
  197.       return;
  198.   }
  199.  
  200.   for (i = 0; kw[i].keyword != NULL; i++)
  201.     if (strcmp(kw[i].keyword, word) == 0) {
  202.       (*kw[i].rtn) (script_fp);
  203.       return;
  204.     }
  205.  
  206.   fprintf(tfp, "UNDEFINED COMMAND: \"%s\"\r\n", word);
  207.   eof_flag = 1;
  208.   return;
  209. }
  210.  
  211. struct _when {
  212.   String          expect;
  213.   String          send;
  214.   String          ptr;
  215. };
  216.  
  217. struct _when    when[MAX_ENT] =
  218. {
  219.   {NULL, NULL, NULL}};
  220.  
  221. void
  222. k_waitfor()
  223. {
  224.   long            t;
  225.   int             c;
  226.   char           *ptr = wf;
  227.   struct _when   *whenPtr;
  228.  
  229.  
  230.   GETTEST_ARG("waitfor");
  231.   strcpy(wf, word);
  232.  
  233.   GET_ARG();
  234.  
  235.   for (whenPtr = when; whenPtr->expect; whenPtr++)
  236.     whenPtr->ptr = whenPtr->expect;
  237.  
  238.   if (word[0])
  239.     waitfor_time = atoi(word);
  240.   else
  241.     waitfor_time = 30;
  242.  
  243.   t = time(NULL) + waitfor_time;
  244.  
  245.   while (t != time(NULL) && !eof_flag) {
  246.     if ((c = readbyte(1)) < 0)
  247.       continue;
  248.  
  249.     if (tty_flag)
  250.       fputc(c, tfp);
  251.  
  252.     if (captflag)
  253.       fputc(c, cf);
  254.  
  255.     if ((char)c != *ptr)
  256.       ptr = wf;
  257.     else if (*++ptr == '\0') {
  258.       waitflag = 1;
  259.       return;
  260.     }
  261.  
  262.     for (whenPtr = when; whenPtr->expect; whenPtr++) {
  263.       if ((char)c != *(whenPtr->ptr))
  264.     whenPtr->ptr = whenPtr->expect;
  265.       else if (*++(whenPtr->ptr) == '\0')
  266.     mputs(whenPtr->send);
  267.     }
  268.  
  269.   }
  270.  
  271.   waitflag = 0;
  272. }
  273.  
  274. void
  275. k_when()
  276. {
  277.   struct _when   *whenPtr;
  278.  
  279.   GET_ARG();
  280.   if (word[0] == '\0') {
  281.     for (whenPtr = when; whenPtr->expect; whenPtr++) {
  282.       XtFree(whenPtr->expect);
  283.       XtFree(whenPtr->send);
  284.     }
  285.     when[0].expect = NULL;
  286.     return;
  287.   }
  288.  
  289.   for (whenPtr = when; whenPtr->expect; whenPtr++);
  290.   whenPtr->expect = XtNewString(word);
  291.   (whenPtr + 1)->expect = NULL;
  292.  
  293.   GETTEST_ARG("when");
  294.   whenPtr->send = XtNewString(word);
  295. }
  296.  
  297. void
  298. k_transmit()
  299. {
  300.  
  301.   getword();
  302.   if (eof_flag)
  303.     return;
  304.  
  305.   if (word[0] == '\0') {
  306.     fprintf(tfp, "No argument to TRANSMIT command\r\n");
  307.     eof_flag = 1;
  308.     return;
  309.   }
  310.  
  311.   mputs(word);
  312. }
  313.  
  314. void
  315. k_pause()
  316. {
  317.   int             pause_time;
  318.  
  319.   getword();
  320.   if (eof_flag)
  321.     return;
  322.  
  323.   if (word[0] == '\0')
  324.     pause_time = 5;
  325.   else
  326.     pause_time = atoi(word);
  327.  
  328.   sleep(pause_time);
  329. }
  330.  
  331. void
  332. k_quit()
  333. {
  334.   write_child_info(child_pipe, EXIT_PROGRAM, "");
  335.   k_exit();
  336. }
  337.  
  338. void
  339. k_exit()
  340. {
  341.   eof_flag = 1;
  342. }
  343.  
  344. static char     label[WBSIZE];
  345.  
  346. void
  347. k_goto(script_fp)
  348.      FILE           *script_fp;
  349. {
  350.   int             found = 0,
  351.                   i;
  352.  
  353.   getword();
  354.   if (word[0] == '\0') {
  355.     fprintf(tfp, "No argument for GOTO: %s\r\n", line);
  356.     eof_flag++;
  357.     return;
  358.   }
  359.  
  360.   strcpy(label, word);
  361.  
  362.   rewind(script_fp);
  363.   while (!found) {
  364.     getline(script_fp);
  365.     if (eof_flag)
  366.       break;
  367.  
  368.     getword();
  369.     if (word[0] == '\0' || word[0] == '#')
  370.       continue;
  371.  
  372.     if (word[i = (strlen(word) - 1)] != ':')
  373.       continue;
  374.  
  375.     word[i] = '\0';
  376.  
  377.     found = (strcmp(word, label) == 0);
  378.   }
  379.  
  380.   if (eof_flag) {
  381.     fprintf(tfp, "Label %s not found\r\n", label);
  382.     eof_flag++;
  383.     return;
  384.   }
  385.  
  386.   if_flag = 0;               /* reset IF flag */
  387. }
  388.  
  389. static          if_negate = 0;
  390.  
  391. static int
  392. if_test(cond)
  393.      int             cond;
  394. {
  395.   if (if_negate)
  396.     cond = !cond;
  397.  
  398.   if (cond)
  399.     return 1;
  400.   else
  401.     return -1;
  402. }
  403.  
  404. void
  405. k_if()
  406. {
  407.   char           *ptr;
  408.  
  409.   if (if_flag) {
  410.     fprintf(tfp, "Nested IF statements not allowed\r\n");
  411.     eof_flag++;
  412.     return;
  413.   }
  414.  
  415.   if_negate = 0;
  416.   getword();
  417.   if (word[0] == '\0') {
  418.     fprintf(tfp, "No condition on IF statement\r\n");
  419.     eof_flag++;
  420.     return;
  421.   }
  422.  
  423.   if (strcmp(word, "not") == 0) {
  424.     if_negate = 1;
  425.     getword();
  426.     if (word[0] == '\0') {
  427.       fprintf(tfp, "No condition on IF statement\r\n");
  428.       eof_flag++;
  429.       return;
  430.     }
  431.   }
  432.  
  433.   if (word[0] == '!') {
  434.     if_negate = 1;
  435.     ptr = word + 1;
  436.   }
  437.   else
  438.     ptr = word;
  439.  
  440.   if (strcmp(ptr, "waitfor") == 0) {
  441.     if_flag = if_test(waitflag);
  442.     return;
  443.   }
  444.  
  445.   if (strcmp(ptr, "linked") == 0) {
  446.     if_flag = if_test(linkflag);
  447.     return;
  448.   }
  449.  
  450.   fprintf(tfp, "Undefined IF condition %s\r\n", ptr);
  451.   eof_flag++;
  452.   return;
  453. }
  454.  
  455. void
  456. k_else()
  457. {
  458.   if (!if_flag) {
  459.     fprintf(tfp, "ELSE not within IF\r\n");
  460.     eof_flag++;
  461.     return;
  462.   }
  463.  
  464.   if_flag = -if_flag;
  465. }
  466.  
  467. void
  468. k_endif()
  469. {
  470.   if (!if_flag) {
  471.     fprintf(tfp, "ENDIF not wihtin IF\r\n");
  472.     eof_flag++;
  473.     return;
  474.   }
  475.  
  476.   if_flag = 0;
  477. }
  478.  
  479. void
  480. k_dial()
  481. {
  482.   getword();
  483.  
  484.   if (word[0] == '\0') {
  485.     fprintf(tfp, "DIAL command must have an argument\r\n");
  486.     eof_flag++;
  487.     return;
  488.   }
  489.  
  490.   dial(word);
  491. }
  492.  
  493. void
  494. k_redial()
  495. {
  496.   if (redial(NULL)) {
  497.     eof_flag++;
  498.     return;
  499.   }
  500. }
  501.  
  502. void
  503. k_debug()
  504. {
  505.   set_onoff(&echo_flag);
  506.   return;
  507. }
  508.  
  509. void
  510. k_echo()
  511. {
  512.   GET_ARG();
  513.   show(word);
  514. }
  515.  
  516. void
  517. k_flush()
  518. {
  519.   flush_modem();
  520. }
  521.  
  522. void
  523. k_hangup()
  524. {
  525.   hangup();
  526. }
  527.  
  528. void
  529. k_purge()
  530. {
  531.   mpurge();
  532. }
  533.  
  534. void
  535. k_send_break()
  536. {
  537.   send_break();
  538. }
  539.  
  540. void
  541. k_capture()
  542. {
  543.   Boolean         val = captflag;
  544.  
  545.   set_onoff(&captflag);
  546.   if (eof_flag)
  547.     return;
  548.  
  549.   if (val == captflag)
  550.     return;
  551.  
  552.   if (captflag == False)
  553.     fclose(cf);
  554.   else {
  555.     if ((cf = fopen(captureFile, "a")) == NULL) {
  556.       fprintf(tfp, "Cannot open capture file %s\r\n", captureFile);
  557.       eof_flag++;
  558.       return;
  559.     }
  560.   }
  561. }
  562.  
  563. void
  564. k_tty()
  565. {
  566.   set_onoff(&tty_flag);
  567.   return;
  568. }
  569.  
  570. /*
  571.  * Dial a phone number, using proper format and delay.
  572.  */
  573.  
  574. static char    *last_nbr = NULL;
  575.  
  576. void
  577. dial(s)
  578.      char           *s;
  579. {
  580.   if (last_nbr)
  581.     XtFree(last_nbr);
  582.  
  583.   last_nbr = XtNewString(s);
  584.  
  585.   mprintf("\r%s %s%s", qres.dialPrefix, s, qres.dialSuffix);
  586. }
  587.  
  588. int
  589. redial(last_nbr)
  590.      char           *last_nbr;
  591. {
  592.   char           *s;
  593.  
  594.   if (last_nbr == NULL) {
  595.     show("REDIAL FAILURE");
  596.     return 1;
  597.   }
  598.  
  599.   s = XtNewString(last_nbr);
  600.   dial(s);
  601.   XtFree(s);
  602.   return 0;
  603. }
  604.