home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1989 / 06 / script.asc < prev    next >
Text File  |  1989-05-27  |  9KB  |  361 lines

  1. _C PROGRAMMING COLUMN_
  2. by Al Stevens
  3.  
  4. [LISTING ONE]
  5.  
  6. /* -------- junehook.c ------------ */
  7.  
  8. /*
  9.  * make these changes to smallcom.c to install the script
  10.  * processor program
  11.  */
  12.  
  13. /* ------- making logfp external ---------- */
  14. FILE *logfp;
  15. static FILE *uploadfp, *downloadfp, *cfg;
  16.  
  17.  
  18. /* ------- the hook to script processors ---------- */
  19. extern void script(void);
  20. void (*script_processor)(void) = script; 
  21.  
  22.  
  23. [LISTING TWO]
  24.  
  25. /* ---------- script.c -------------- */
  26.  
  27. /*
  28.  * The SI shell to implement interpreted scripts in SMALLCOM
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <conio.h>
  33. #include <stdlib.h>
  34. #include <ctype.h>
  35. #include <dos.h>
  36. #include <setjmp.h>
  37. #include "window.h"
  38. #include "serial.h"
  39. #include "interp.h"
  40. #include "modem.h"
  41.  
  42. #if COMPILER == MSOFT
  43. #define MK_FP(s,o) ((void far *) \
  44.     (((unsigned long)(s) << 16) | (unsigned)(o)))
  45. #endif
  46.  
  47. void upload_ASCII(FILE *);
  48. void download_ASCII(FILE *);
  49. void upload_xmodem(FILE *);
  50. void download_xmodem(FILE *);
  51. int waitforstring(char **, int, int);
  52. char *prompt_line(char *, int, char *);
  53. void reset_prompt(char *, int);
  54.  
  55. /* ----------- intrinsic interpreter functions ---------- */
  56. static int si_logon(int *);
  57. static int si_logoff(int *);
  58. static int si_upload(int *);
  59. static int si_download(int *);
  60. static int si_hangup(int *);
  61. static int si_quit(int *);
  62. static int si_sendstring(int *);
  63. static int si_sendchar(int *);
  64. static int si_waitforstrings(int *);
  65. static int si_waitfor(int *);
  66. static int si_system(int *);
  67. static int si_message(int *);
  68.  
  69. INTRINSIC ffs[] = {
  70.     "logon",            si_logon,
  71.     "logoff",           si_logoff,
  72.     "upload",           si_upload,
  73.     "download",         si_download,
  74.     "hangup",           si_hangup,
  75.     "quit",             si_quit,
  76.     "sendstring",       si_sendstring,
  77.     "sendchar",         si_sendchar,
  78.     "waitforstrings",   si_waitforstrings,
  79.     "waitfor",          si_waitfor,
  80.     "system",           si_system,
  81.     "message",          si_message,
  82.     NULL,               NULL
  83. };
  84.  
  85. extern INTRINSIC *infs = ffs;
  86. extern FILE *logfp;
  87.  
  88. /* ------------------ error messages ----------------------- */
  89. char *erm[]={  "Unexpected end of file", "Unrecognized",
  90.                "Duplicate ident",        "Symbol table full",
  91.                "Out of heap memory",     "Undeclared ident",
  92.                "Syntax Error",           "Unmatched {}",
  93.                "Unmatched ()",           "Missing",
  94.                "Not a function",         "Misplaced break",
  95.                "Out of place",           "Too many strings",
  96.                "Token buffer overflow",  "Divide by zero"    };
  97.  
  98. static FILE *fp;
  99. static char *prompt = NULL;
  100. extern char scriptfile[];
  101. extern char *tokenbf;
  102.  
  103. jmp_buf errorjmp;
  104.  
  105. /* ---------- process the named script file --------- */
  106. void script()
  107. {
  108.     if ((fp = fopen(scriptfile, "r")) != NULL) {
  109.         if (setjmp(errorjmp) == 0)  {
  110.             loadsource();
  111.             interpret();
  112.         }
  113.         fclose(fp);
  114.         if (prompt != NULL)
  115.             reset_prompt(prompt, 25);
  116.         if (tokenbf != NULL)
  117.             free(tokenbf);
  118.     }
  119. }
  120.  
  121. /* ----------- syntax error in script language --------- */
  122. void sierror(enum errs erno, char *s, int line)
  123. {
  124.     char msg[80];
  125.     sprintf(msg, "SI Error: %s %s line %d\n",s,erm[erno],line);
  126.     error_message(msg);
  127.     longjmp(errorjmp, 1);
  128. }
  129.  
  130. /* ---------- get a character of script source code -------- */
  131. int getsource(void)
  132. {
  133.     return getc(fp);
  134. }
  135.  
  136. /* -------- unget a character of script source code ------- */
  137. void ungetsource(int c)
  138. {
  139.     ungetc(c, fp);
  140. }
  141.  
  142. /* ------------ intrinsic functions -------------- */
  143.  
  144. /* ---------- turn logging on ----------------- */
  145. static int si_logon(int *ptr)
  146. {
  147.     si_logoff(ptr);
  148.     logfp = fopen((char *) *ptr, "ab");
  149.     return 0;
  150. }
  151.  
  152. /* ---------- turn logging off ----------------- */
  153. static int si_logoff(int *ptr)
  154. {
  155.     if (logfp)
  156.         fclose(logfp);
  157.     logfp = NULL;
  158.     return 0;
  159. }
  160.  
  161. /* ----------- upload a file ----------------- */
  162. static int si_upload(int *ptr)
  163. {
  164.     FILE *up;
  165.     int x = wherex();
  166.     int y = wherey();
  167.  
  168.     if ((up = fopen((char *) ptr[0], "rb")) != NULL)    {
  169.         if (toupper(ptr[1]) == 'A')
  170.             upload_ASCII(up);
  171.         else if (toupper(ptr[1]) == 'X')
  172.             upload_xmodem(up);
  173.         fclose(up);
  174.     }
  175.     gotoxy(x,y);
  176.     return 0;
  177. }
  178.  
  179. /* --------- download a file ------------- */
  180. static int si_download(int *ptr)
  181. {
  182.     FILE *dn;
  183.     int x = wherex();
  184.     int y = wherey();
  185.  
  186.     if ((dn = fopen((char *) ptr[0], "wb")) != NULL)    {
  187.         if (toupper(ptr[1]) == 'A')
  188.             download_ASCII(dn);
  189.         else if (toupper(ptr[1]) == 'X')
  190.             download_xmodem(dn);
  191.         fclose(dn);
  192.     }
  193.     gotoxy(x,y);
  194.     return 0;
  195. }
  196.  
  197. /* ---------- hangup the call ------------ */
  198. static int si_hangup(int *ptr)
  199. {
  200.     disconnect();
  201.     return 0;
  202. }
  203.  
  204. /* ----------- terminate the program ---------- */
  205. static int si_quit(int *ptr)
  206. {
  207.     int far *bp = MK_FP(0x40, 0x1a); /* BIOS read-ahead buff */
  208.  
  209.     if (*ptr)   {
  210.         *bp++ = 0x1e;   /* next off pointer */
  211.         *bp++ = 0x22;   /* next on pointer  */
  212.         *bp++ = 27;     /* Esc key          */
  213.         *bp   = 'y';    /* 'y' for Yes      */
  214.     }
  215.     longjmp(errorjmp, 1);
  216. }
  217.  
  218. /* ---------- send a string to the callee --------- */
  219. static int si_sendstring(int *ptr)
  220. {
  221.     char *cp = (char *) *ptr;
  222.  
  223.     while (*cp)
  224.         writecomm(*cp++);
  225.     return 0;
  226. }
  227.  
  228. /* ---------- send a character to the callee --------- */
  229. static int si_sendchar(int *ptr)
  230. {
  231.     writecomm(*ptr);
  232.     return 0;
  233. }
  234.  
  235. /* --- wait for one of a set of strings from the callee --- */
  236. static int si_waitforstrings(int *ptr)
  237. {
  238.     return waitforstring((char **) ptr[0], 60, 0);
  239. }
  240.  
  241. /* ------- wait for a string from the callee ------- */
  242. static int si_waitfor(int *ptr)
  243. {
  244.     static char *ws[] = {NULL, NULL};
  245.  
  246.     ws[0] = (char *) *ptr;
  247.     return waitforstring(ws, 60, 0);
  248. }
  249.  
  250. /* ---------- execute a system (DOS) command ----------- */
  251. static int si_system(int *ptr)
  252. {
  253.     char cmd[80];
  254.     sprintf(cmd, "%s >nul", (char *) *ptr);
  255.     system(cmd);
  256.     return 0;
  257. }
  258.  
  259. /* --------- display a message to the user ------------ */
  260. static int si_message(int *ptr)
  261. {
  262.     int x = wherex();
  263.     int y = wherey();
  264.     prompt = prompt_line((char *) *ptr, 25, prompt);
  265.     gotoxy(x,y);
  266.     return 0;
  267. }
  268.  
  269.  
  270. [LISTING THREE]
  271.  
  272. /*
  273.  * PROCOMM.SCR : A SMALLCOM script that calls a ProComm system,
  274.  *               downloads a file, and uploads another file
  275.  */
  276.  
  277. /* ----- key strings that ProComm sends ----- */
  278. char *strs[] = {
  279.     "choice?",
  280.     "Denied"
  281. };
  282. char *Name      = "Name:";
  283. char *Password  = "Password:";
  284. char *choice    = "choice?";
  285. char *procedure = "procedure.";
  286. char *spec      = "spec?";
  287.  
  288. /* -------- strings that SMALLCOM sends -------------- */
  289. char *name      = "Al Stevens\r";
  290. char *password  = "PASSWORD\r";
  291. char *filename1 = "test1.fil";
  292. char *filename2 = "test2.fil";
  293.  
  294. /* -------- poor man's #define or enum ---------- */
  295. int CHOICE    = 0;
  296. int DENIED    = 1;
  297. int XMODEM    = 'X';
  298. int UPLOAD    = 'u';
  299. int DOWNLOAD  = 'd';
  300. int GOODBYE   = 'g';
  301.  
  302. /* -------- main entrance to the script --------- */
  303. main()
  304. {
  305.     message("  --> Signing on to ProComm <--");
  306.     if (signon())   {
  307.         downloadops();
  308.         uploadops();
  309.         waitfor(choice);
  310.         message(" Signing Off");
  311.         sendchar(GOODBYE);
  312.     }
  313.     else
  314.         message(" Not allowed access! ");
  315.     hangup();
  316. }
  317.  
  318. /* ---------- sign on and send the password ----------- */
  319. signon()
  320. {
  321.     waitfor(Name);
  322.     sendstring(name);
  323.     waitfor(Password);
  324.     sendstring(password);
  325.     if (waitforstrings(strs) == CHOICE)
  326.         return 1;
  327.     return 0;
  328. }
  329.  
  330. /* --------- download a file ---------- */
  331. downloadops()
  332. {
  333.     message("  Downloading");
  334.     sendchar(DOWNLOAD);
  335.     waitfor(choice);
  336.     sendchar(XMODEM);
  337.     waitfor(spec);
  338.     sendstring(filename1);
  339.     sendchar('\r');
  340.     waitfor(procedure);
  341.     download(filename1, XMODEM);
  342.     system("del download.fil");
  343.     system("ren test1.fil download.fil");
  344. }
  345.  
  346. /* --------- upload a file ---------- */
  347. uploadops()
  348. {
  349.     waitfor(choice);
  350.     message("  Uploading");
  351.     sendchar(UPLOAD);
  352.     waitfor(choice);
  353.     sendchar(XMODEM);
  354.     waitfor(spec);
  355.     sendstring(filename2);
  356.     sendchar('\r');
  357.     waitfor(procedure);
  358.     upload(filename2, XMODEM);
  359. }
  360.  
  361.