home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
ddjmag
/
ddj8906.arc
/
SCRIPT.ASC
< prev
next >
Wrap
Text File
|
1989-05-27
|
9KB
|
361 lines
_C PROGRAMMING COLUMN_
by Al Stevens
[LISTING ONE]
/* -------- junehook.c ------------ */
/*
* make these changes to smallcom.c to install the script
* processor program
*/
/* ------- making logfp external ---------- */
FILE *logfp;
static FILE *uploadfp, *downloadfp, *cfg;
/* ------- the hook to script processors ---------- */
extern void script(void);
void (*script_processor)(void) = script;
[LISTING TWO]
/* ---------- script.c -------------- */
/*
* The SI shell to implement interpreted scripts in SMALLCOM
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#include <dos.h>
#include <setjmp.h>
#include "window.h"
#include "serial.h"
#include "interp.h"
#include "modem.h"
#if COMPILER == MSOFT
#define MK_FP(s,o) ((void far *) \
(((unsigned long)(s) << 16) | (unsigned)(o)))
#endif
void upload_ASCII(FILE *);
void download_ASCII(FILE *);
void upload_xmodem(FILE *);
void download_xmodem(FILE *);
int waitforstring(char **, int, int);
char *prompt_line(char *, int, char *);
void reset_prompt(char *, int);
/* ----------- intrinsic interpreter functions ---------- */
static int si_logon(int *);
static int si_logoff(int *);
static int si_upload(int *);
static int si_download(int *);
static int si_hangup(int *);
static int si_quit(int *);
static int si_sendstring(int *);
static int si_sendchar(int *);
static int si_waitforstrings(int *);
static int si_waitfor(int *);
static int si_system(int *);
static int si_message(int *);
INTRINSIC ffs[] = {
"logon", si_logon,
"logoff", si_logoff,
"upload", si_upload,
"download", si_download,
"hangup", si_hangup,
"quit", si_quit,
"sendstring", si_sendstring,
"sendchar", si_sendchar,
"waitforstrings", si_waitforstrings,
"waitfor", si_waitfor,
"system", si_system,
"message", si_message,
NULL, NULL
};
extern INTRINSIC *infs = ffs;
extern FILE *logfp;
/* ------------------ error messages ----------------------- */
char *erm[]={ "Unexpected end of file", "Unrecognized",
"Duplicate ident", "Symbol table full",
"Out of heap memory", "Undeclared ident",
"Syntax Error", "Unmatched {}",
"Unmatched ()", "Missing",
"Not a function", "Misplaced break",
"Out of place", "Too many strings",
"Token buffer overflow", "Divide by zero" };
static FILE *fp;
static char *prompt = NULL;
extern char scriptfile[];
extern char *tokenbf;
jmp_buf errorjmp;
/* ---------- process the named script file --------- */
void script()
{
if ((fp = fopen(scriptfile, "r")) != NULL) {
if (setjmp(errorjmp) == 0) {
loadsource();
interpret();
}
fclose(fp);
if (prompt != NULL)
reset_prompt(prompt, 25);
if (tokenbf != NULL)
free(tokenbf);
}
}
/* ----------- syntax error in script language --------- */
void sierror(enum errs erno, char *s, int line)
{
char msg[80];
sprintf(msg, "SI Error: %s %s line %d\n",s,erm[erno],line);
error_message(msg);
longjmp(errorjmp, 1);
}
/* ---------- get a character of script source code -------- */
int getsource(void)
{
return getc(fp);
}
/* -------- unget a character of script source code ------- */
void ungetsource(int c)
{
ungetc(c, fp);
}
/* ------------ intrinsic functions -------------- */
/* ---------- turn logging on ----------------- */
static int si_logon(int *ptr)
{
si_logoff(ptr);
logfp = fopen((char *) *ptr, "ab");
return 0;
}
/* ---------- turn logging off ----------------- */
static int si_logoff(int *ptr)
{
if (logfp)
fclose(logfp);
logfp = NULL;
return 0;
}
/* ----------- upload a file ----------------- */
static int si_upload(int *ptr)
{
FILE *up;
int x = wherex();
int y = wherey();
if ((up = fopen((char *) ptr[0], "rb")) != NULL) {
if (toupper(ptr[1]) == 'A')
upload_ASCII(up);
else if (toupper(ptr[1]) == 'X')
upload_xmodem(up);
fclose(up);
}
gotoxy(x,y);
return 0;
}
/* --------- download a file ------------- */
static int si_download(int *ptr)
{
FILE *dn;
int x = wherex();
int y = wherey();
if ((dn = fopen((char *) ptr[0], "wb")) != NULL) {
if (toupper(ptr[1]) == 'A')
download_ASCII(dn);
else if (toupper(ptr[1]) == 'X')
download_xmodem(dn);
fclose(dn);
}
gotoxy(x,y);
return 0;
}
/* ---------- hangup the call ------------ */
static int si_hangup(int *ptr)
{
disconnect();
return 0;
}
/* ----------- terminate the program ---------- */
static int si_quit(int *ptr)
{
int far *bp = MK_FP(0x40, 0x1a); /* BIOS read-ahead buff */
if (*ptr) {
*bp++ = 0x1e; /* next off pointer */
*bp++ = 0x22; /* next on pointer */
*bp++ = 27; /* Esc key */
*bp = 'y'; /* 'y' for Yes */
}
longjmp(errorjmp, 1);
}
/* ---------- send a string to the callee --------- */
static int si_sendstring(int *ptr)
{
char *cp = (char *) *ptr;
while (*cp)
writecomm(*cp++);
return 0;
}
/* ---------- send a character to the callee --------- */
static int si_sendchar(int *ptr)
{
writecomm(*ptr);
return 0;
}
/* --- wait for one of a set of strings from the callee --- */
static int si_waitforstrings(int *ptr)
{
return waitforstring((char **) ptr[0], 60, 0);
}
/* ------- wait for a string from the callee ------- */
static int si_waitfor(int *ptr)
{
static char *ws[] = {NULL, NULL};
ws[0] = (char *) *ptr;
return waitforstring(ws, 60, 0);
}
/* ---------- execute a system (DOS) command ----------- */
static int si_system(int *ptr)
{
char cmd[80];
sprintf(cmd, "%s >nul", (char *) *ptr);
system(cmd);
return 0;
}
/* --------- display a message to the user ------------ */
static int si_message(int *ptr)
{
int x = wherex();
int y = wherey();
prompt = prompt_line((char *) *ptr, 25, prompt);
gotoxy(x,y);
return 0;
}
[LISTING THREE]
/*
* PROCOMM.SCR : A SMALLCOM script that calls a ProComm system,
* downloads a file, and uploads another file
*/
/* ----- key strings that ProComm sends ----- */
char *strs[] = {
"choice?",
"Denied"
};
char *Name = "Name:";
char *Password = "Password:";
char *choice = "choice?";
char *procedure = "procedure.";
char *spec = "spec?";
/* -------- strings that SMALLCOM sends -------------- */
char *name = "Al Stevens\r";
char *password = "PASSWORD\r";
char *filename1 = "test1.fil";
char *filename2 = "test2.fil";
/* -------- poor man's #define or enum ---------- */
int CHOICE = 0;
int DENIED = 1;
int XMODEM = 'X';
int UPLOAD = 'u';
int DOWNLOAD = 'd';
int GOODBYE = 'g';
/* -------- main entrance to the script --------- */
main()
{
message(" --> Signing on to ProComm <--");
if (signon()) {
downloadops();
uploadops();
waitfor(choice);
message(" Signing Off");
sendchar(GOODBYE);
}
else
message(" Not allowed access! ");
hangup();
}
/* ---------- sign on and send the password ----------- */
signon()
{
waitfor(Name);
sendstring(name);
waitfor(Password);
sendstring(password);
if (waitforstrings(strs) == CHOICE)
return 1;
return 0;
}
/* --------- download a file ---------- */
downloadops()
{
message(" Downloading");
sendchar(DOWNLOAD);
waitfor(choice);
sendchar(XMODEM);
waitfor(spec);
sendstring(filename1);
sendchar('\r');
waitfor(procedure);
download(filename1, XMODEM);
system("del download.fil");
system("ren test1.fil download.fil");
}
/* --------- upload a file ---------- */
uploadops()
{
waitfor(choice);
message(" Uploading");
sendchar(UPLOAD);
waitfor(choice);
sendchar(XMODEM);
waitfor(spec);
sendstring(filename2);
sendchar('\r');
waitfor(procedure);
upload(filename2, XMODEM);
}