#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/termio.h>
#include "xc-conf.h"
#if HAVE_USLEEP || HAVE_NAP || HAVE_NAPMS || HAVE_POLL
#define USE_SELECT_TIMER 0
#else
#define USE_SELECT_TIMER HAVE_SELECT
#if USE_SELECT_TIMER
#include <sys/time.h>
#endif
#endif
#if HAVE_USLEEP || HAVE_NAP || HAVE_NAPMS
#undef HAVE_POLL
#define HAVE_POLL 0
#endif
#if HAVE_USLEEP || HAVE_NAP
#undef HAVE_NAPMS
#define HAVE_NAPMS 0
#endif
#if HAVE_USLEEP
#undef HAVE_NAP
#define HAVE_NAP 0
#endif
static int ttblind();
static int ttcd();
struct script {
struct script *next;
int opcode;
char *strprm;
long intprm;
char *newstate;
};
struct script_opdef {
char *opname;
int opcode;
int prmtype;
int newstate;
};
#define SC_LABEL 0
#define SC_CDLY 1
#define SC_PCHR 2
#define SC_PTIM 3
#define SC_WCHR 4
#define SC_WTIM 5
#define SC_ZERO 6
#define SC_INCR 7
#define SC_IFGT 8
#define SC_WAIT 9
#define SC_GOTO 10
#define SC_SEND 11
#define SC_BRK 12
#define SC_HANG 13
#define SC_DIAL 14
#define SC_DTIM 15
#define SC_CTIM 16
#define SC_EXIT 17
#define SC_FAIL 18
#define SC_LOG 19
#define SC_LOGE 20
#define SC_DBG 21
#define SC_DBGE 22
#define SC_DBST 23
#define SC_DBCL 24
#define SC_TIMO 25
#define SC_XPCT 26
#define SC_CARR 27
#define SC_FLSH 28
#define SC_IFBL 29
#define SC_IFBG 30
#define SC_SNDP 31
#define SC_IF1P 32
#define SC_IF0P 33
#define SC_DBOF 34
#define SC_TELN 35
#define SC_7BIT 36
#define SC_8BIT 37
#define SC_PNON 38
#define SC_PEVN 39
#define SC_PODD 40
#define SC_HUPS 41
#define SC_XPST 42
#define SC_END 43
#define SC_NONE 0
#define SC_STR 1
#define SC_INT 2
#define SC_NWST 3
#define SC_XSTR 4
static struct script_opdef sc_opdef[] =
{
{"label", SC_LABEL, SC_NONE, SC_NONE},
{"chrdly", SC_CDLY, SC_INT, SC_NONE},
{"pchar", SC_PCHR, SC_STR, SC_NONE},
{"ptime", SC_PTIM, SC_INT, SC_NONE},
{"wchar", SC_WCHR, SC_STR, SC_NONE},
{"wtime", SC_WTIM, SC_INT, SC_NONE},
{"zero", SC_ZERO, SC_NONE, SC_NONE},
{"count", SC_INCR, SC_NONE, SC_NONE},
{"ifgtr", SC_IFGT, SC_INT, SC_NWST},
{"sleep", SC_WAIT, SC_INT, SC_NONE},
{"goto", SC_GOTO, SC_NONE, SC_NWST},
{"send", SC_SEND, SC_XSTR, SC_NONE},
{"break", SC_BRK, SC_NONE, SC_NONE},
{"hangup", SC_HANG, SC_NONE, SC_NONE},
{"7bit", SC_7BIT, SC_NONE, SC_NONE},
{"8bit", SC_8BIT, SC_NONE, SC_NONE},
{"nopar", SC_PNON, SC_NONE, SC_NONE},
{"evenpar", SC_PEVN, SC_NONE, SC_NONE},
{"oddpar", SC_PODD, SC_NONE, SC_NONE},
{"telno", SC_TELN, SC_INT, SC_NONE},
{"dial", SC_DIAL, SC_NONE, SC_NONE},
{"dgttime", SC_DTIM, SC_INT, SC_NONE},
{"ctime", SC_CTIM, SC_INT, SC_NONE},
{"success", SC_EXIT, SC_NONE, SC_NONE},
{"failed", SC_FAIL, SC_NONE, SC_NONE},
{"log", SC_LOG, SC_XSTR, SC_NONE},
{"logerr", SC_LOGE, SC_XSTR, SC_NONE},
{"debug", SC_DBG, SC_XSTR, SC_NONE},
{"debuge", SC_DBGE, SC_XSTR, SC_NONE},
{"dbgset", SC_DBST, SC_INT, SC_NONE},
{"dbgclr", SC_DBCL, SC_INT, SC_NONE},
{"dbgfile", SC_DBOF, SC_XSTR, SC_NONE},
{"timeout", SC_TIMO, SC_INT, SC_NWST},
{"expect", SC_XPCT, SC_XSTR, SC_NWST},
{"ifcarr", SC_CARR, SC_NONE, SC_NWST},
{"ifhang", SC_HUPS, SC_NONE, SC_NWST},
{"flush", SC_FLSH, SC_NONE, SC_NONE},
{"ifblind", SC_IFBL, SC_NONE, SC_NWST},
{"ifblgtr", SC_IFBG, SC_INT, SC_NWST},
{"sendstr", SC_SNDP, SC_INT, SC_NONE},
{"ifstr", SC_IF1P, SC_INT, SC_NWST},
{"ifnstr", SC_IF0P, SC_INT, SC_NWST},
{"expectstr", SC_XPST, SC_INT, SC_NWST},
{"table end", SC_END, SC_NONE, SC_NONE}
};
#define SUCCESS 0
#define FAIL 1
#define ERROR -1
#define MAX_SCLINE 255
#define MAX_EXPCT 127
#define CTL_DELIM " \t\n\r"
#define SAME 0
#define SLOP 10
#define MAX_STRING 200
#define DEBUG_LEVEL(level) \
(Debug & (1 << level))
#define DB_LOG 0
#define DB_LGIE 1
#define DB_LGI 2
#define DB_LGII 3
#define TRUE 1
#define FALSE 0
#define NONE 0
#define EVEN 1
#define ODD 2
#define logit(m, p1) fprintf(stderr, "%s %s\n", m, p1)
static char **paramv;
static int paramc;
static char telno[64];
static int Debug;
static int fShangup = FALSE;
static FILE *dbf = NULL;
static struct termio old, new;
extern int usignal();
extern int uhup();
static struct siglist
{
int signal;
int (*o_catcher) ();
int (*n_catcher) ();
} sigtbl[] = {
{ SIGHUP, NULL, uhup },
{ SIGINT, NULL, usignal },
{ SIGIOT, NULL, usignal },
{ SIGQUIT, NULL, usignal },
{ SIGTERM, NULL, usignal },
{ SIGALRM, NULL, usignal },
{ 0, NULL, NULL }
};
extern struct script *read_script();
extern void msleep();
extern char xgetc();
extern void charlog();
extern void setup_tty();
extern void restore_tty();
extern void ttoslow();
extern void ttflui();
extern void tthang();
extern void ttbreak();
extern void tt7bit();
extern void ttpar();
extern void DEBUG();
extern void *malloc();
int main(argc, argv)
int argc;
char *argv[];
{
int i, stat;
FILE *sf;
char sfname[256];
struct script *script;
struct siglist *sigs;
#if ! HAVE_SELECT
no_select_sorry();
#endif
paramv = &argv[2];
paramc = argc - 2;
telno[0] = '\0';
if (argc < 2)
{
fprintf(stderr, "%s: no script file supplied\n", argv[0]);
exit(FAIL);
}
*sfname = '\0';
if(argv[1][0] != '/')
strcat(sfname, SCRIPT_DIR);
strcat(sfname, argv[1]);
if ((sf = fopen(sfname, "r")) == NULL)
{
fprintf(stderr, "%s: Failed to open script %s\n", argv[0], sfname);
perror(" ");
exit(FAIL);
}
if ((script = read_script(sf)) == NULL)
{
fprintf(stderr, "%s: script error in \"%s\"\n", argv[0], argv[1]);
exit(FAIL);
}
sigs = &sigtbl[0];
while(sigs->signal)
{
sigs->o_catcher = (int (*) ())signal(sigs->signal, sigs->n_catcher);
sigs += 1;
}
setup_tty();
if ((stat = do_script(script)) != SUCCESS)
fprintf(stderr, "%s: script %s failed.\n", argv[0], argv[1]);
restore_tty();
#ifdef FIXSIGS
sigs = &sigtbl[0];
while(sigs->signal)
if(sigs->o_catcher != -1)
signal(sigs->signal, sigs->o_catcher);
#endif
exit(stat);
}
int deal_script(loc)
struct script *loc;
{
if (loc == (struct script *)NULL)
return SUCCESS;
deal_script(loc->next);
if (loc->strprm != (char *)NULL)
free(loc->strprm);
if (loc->newstate != (char *)NULL)
free(loc->newstate);
free(loc);
return SUCCESS;
}
struct script *read_script(fd)
FILE *fd;
{
struct script *this = NULL;
struct script *prev = NULL;
struct script *first = NULL;
long len, i;
char inpline[MAX_SCLINE];
char inpcopy[MAX_SCLINE];
char *c, *cln, *opc, *cp;
while ((c = fgets(inpline, (sizeof inpline - 1), fd)) != (char *)NULL)
{
if (*c == '#' || *c == '\n')
continue;
inpline[strlen(inpline)-1] = '\0';
strcpy(inpcopy, inpline);
if ((!isspace(inpline[0])) &&
(cln = strchr (inpline, ':')) != (char *)NULL) {
this = (struct script *)malloc (sizeof (struct script));
if (prev != (struct script *)NULL)
prev->next = this;
prev = this;
if (first == (struct script *)NULL)
first = this;
this->next = (struct script *)NULL;
this->opcode = SC_LABEL;
len = cln - c;
this->strprm = (char *)malloc(len+1);
strncpy(this->strprm, c, len);
(this->strprm)[len] = '\0';
this->intprm = 0;
this->newstate = (char *)NULL;
c = cln + 1;
}
opc = strtok(c, CTL_DELIM);
if (opc == (char *)NULL)
continue;
cp = opc;
while(*cp)
tolower(*cp++);
if (first == (struct script *)NULL)
{
this = (struct script *)malloc (sizeof (struct script));
prev = this;
first = this;
this->next = (struct script *)NULL;
this->opcode = SC_LABEL;
this->strprm = (char *)malloc(2);
strcpy(this->strprm, ":");
this->intprm = 0;
this->newstate = (char *)NULL;
}
for (i=1; sc_opdef[i].opcode != SC_END; i++)
if (strcmp(opc, sc_opdef[i].opname) == SAME)
break;
if ((sc_opdef[i].opcode) == SC_END)
{
logit ("Bad opcode in script", opc);
deal_script(first);
return (struct script *)NULL;
}
this = (struct script *)malloc(sizeof (struct script));
prev->next = this;
prev = this;
this->next = (struct script *)NULL;
this->opcode = sc_opdef[i].opcode;
this->strprm = (char *)NULL;
this->intprm = 0;
this->newstate = (char *)NULL;
if (sc_opdef[i].newstate == SC_NWST)
{
c = strtok((char *)NULL, CTL_DELIM);
if (c == (char *)NULL)
{
logit("Missing new state", opc);
deal_script(first);
return (struct script *)NULL;
}
else
{
this->newstate = (char *)malloc(strlen(c)+1);
strcpy(this->newstate, c);
}
}
switch (sc_opdef[i].prmtype)
{
case SC_INT:
c = strtok((char *)NULL, CTL_DELIM);
if (c == (char *)NULL)
{
logit("Missing script param", opc);
deal_script(first);
return (struct script *)NULL;
}
if (sc_opdef[i].opcode == SC_DBST ||
sc_opdef[i].opcode == SC_DBCL)
this->intprm = strtol(c, (char **)NULL, 0);
else
this->intprm = strtol(c, (char **)NULL, 10);
break;
case SC_STR:
case SC_XSTR:
c = strtok((char *)NULL, CTL_DELIM);
if (c == (char *)NULL)
{
logit("Missing script param", opc);
deal_script(first);
return (struct script *)NULL;
}
if (sc_opdef[i].prmtype == SC_XSTR)
c = &inpcopy[0] + (c - &inpline[0]);
this->strprm = (char *)malloc(strlen(c)+1);
if (sc_opdef[i].prmtype == SC_XSTR)
{
this->intprm = xlat_str(this->strprm, c);
this->strprm[this->intprm] = '\0';
}
else
strcpy(this->strprm, c);
break;
}
}
return first;
}
xlat_str(out, in)
char *out, *in;
{
register int i = 0, j = 0;
int byte, k;
while (in[i])
{
if (in[i] != '\\')
{
out[j++] = in[i++];
}
else
{
switch (in[++i])
{
case 'd':
out[j++] = 0x04;
break;
case 'N':
out[j++] = 0x00;
break;
case 'n':
out[j++] = 0x0a;
break;
case 'r':
out[j++] = 0x0d;
break;
case 's':
out[j++] = ' ';
break;
case 't':
out[j++] = '\t';
break;
case '-':
out[j++] = '-';
break;
case '\\':
out[j++] = '\\';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
byte = in[i] - '0';
k = 0;
while (3 > ++k)
if ((in[i+1] < '0') || (in[i+1] > '7'))
break;
else
{
byte = (byte<<3) + in[i+1] - '0';
++i;
}
out[j++] = byte;
break;
default:
break;
}
++i;
}
}
return j;
}
struct script *
find_state(begin, newstate)
struct script *begin;
char *newstate;
{
struct script *here;
for (here=begin; here != (struct script *)NULL; here=here->next) {
if (here->opcode == SC_LABEL &&
strcmp(here->strprm, newstate) == SAME)
return here;
}
return (struct script *)NULL;
}
int do_script(begin)
struct script *begin;
{
struct script *curstate, *newstate, *curscr;
int dbgsave;
char tempstr[MAX_SCLINE];
char dfname[256];
char *c, chr;
int prmlen;
int dbfd;
time_t sc_carrtime = 45000;
time_t sc_chrdly = 100;
time_t sc_ptime = 2000;
time_t sc_wtime = 10000;
time_t sc_dtime = 100;
time_t sc_dtmo;
int sc_counter;
char sc_pchar = ',';
char sc_wchar = 'W';
time_t sc_begwait;
time_t sc_secs;
int expcnt;
int expin;
static char expbuf[MAX_EXPCT];
dbgsave = Debug;
curstate = begin;
if (curstate == (struct script *)NULL)
return SUCCESS;
_newstate:
expin = 0;
for (curscr = curstate->next;
(curscr != (struct script *)NULL) &&
(curscr->opcode != SC_LABEL);
curscr = curscr->next)
{
expcnt = 0;
switch (curscr->opcode)
{
case SC_LABEL:
logit("Script proc err", curstate->strprm);
return FAIL;
case SC_FLSH:
DEBUG(DB_LGII, "Flushing typeahead buffer\n", 0);
ttflui();
break;
case SC_CDLY:
sc_chrdly = curscr->intprm;
DEBUG(DB_LGII, "Set chrdly to %d\n", sc_chrdly);
break;
case SC_PCHR:
sc_pchar = *(curscr->strprm);
DEBUG(DB_LGII, "Set pause char to %c\n", sc_pchar);
break;
case SC_PTIM:
sc_ptime = curscr->intprm;
DEBUG(DB_LGII, "Set pause time to %d\n", sc_ptime);
break;
case SC_WCHR:
sc_wchar = *(curscr->strprm);
DEBUG(DB_LGII, "Set wait char to %c\n", sc_wchar);
break;
case SC_WTIM:
sc_wtime = curscr->intprm;
DEBUG(DB_LGII, "Set wait time to %d\n", sc_wtime);
break;
case SC_ZERO:
sc_counter = 0;
DEBUG(DB_LGII, "Set counter to %d\n", sc_counter);
break;
case SC_INCR:
sc_counter++;
DEBUG(DB_LGII, "Incr counter to %d\n", sc_counter);
break;
case SC_WAIT:
DEBUG(DB_LGII, "Sleeping %d tenth-secs\n", curscr->intprm);
msleep(curscr->intprm);
break;
case SC_DTIM:
sc_dtime = curscr->intprm;
DEBUG(DB_LGII, "Digit time is %d\n", sc_dtime);
break;
case SC_CTIM:
sc_carrtime = curscr->intprm;
DEBUG(DB_LGII, "Carrier time is %d\n", sc_carrtime);
break;
case SC_EXIT:
Debug = dbgsave;
DEBUG(DB_LGI, "Script ended successfully\n", 0);
return SUCCESS;
case SC_FAIL:
Debug = dbgsave;
if (DEBUG_LEVEL(DB_LGI) && dbf != NULL)
fprintf(dbf, "Script failed\n");
else if (expin)
charlog(expbuf, expin, DB_LOG,
"Script failed. Last received data");
return FAIL;
case SC_LOG:
logit(curscr->strprm, "");
break;
case SC_LOGE:
logit("ERROR: ", curscr->strprm);
break;
case SC_DBOF:
*dfname = '\0';
if(curscr->strprm[0] != '/')
strcat(dfname, LOG_DIR);
strcat(dfname, curscr->strprm);
DEBUG(DB_LGII, "Open debug file %s\n", dfname);
if ((dbfd = creat (dfname, 0600)) <= 0)
{
logit("Failed to create debug log %s", dfname);
perror("");
return FAIL;
}
if ((dbf = fdopen(dbfd, "w")) == NULL)
{
logit("Failed to open debug log fildes.", "");
perror("");
return FAIL;
}
break;
case SC_DBG:
DEBUG(DB_LGI, "<%s>\n", curscr->strprm);
break;
case SC_DBGE:
DEBUG(DB_LGIE, "ERROR: <%s>\n", curscr->strprm);
break;
case SC_DBST:
Debug |= curscr->intprm;
DEBUG(DB_LGII, "Debug mask set to %04o (octal)\n", Debug);
break;
case SC_DBCL:
Debug &= ~(curscr->intprm);
DEBUG(DB_LGII, "Debug mask set to %04o (octal)\n", Debug);
break;
case SC_BRK:
DEBUG(DB_LGI, "Sending break\n", 0);
ttbreak();
break;
case SC_HANG:
DEBUG(DB_LGI, "Dropping DTR\n", 0);
tthang();
break;
case SC_7BIT:
DEBUG(DB_LGI, "Enabling 7-bit stripping\n", 0);
tt7bit(TRUE);
break;
case SC_8BIT:
DEBUG(DB_LGI, "Disabling 7-bit stripping\n", 0);
tt7bit(FALSE);
break;
case SC_PNON:
DEBUG(DB_LGI, "Setting 8-bit, no parity\n", 0);
ttpar(NONE);
break;
case SC_PEVN:
DEBUG(DB_LGI, "Setting 7-bit, even parity\n", 0);
ttpar(EVEN);
break;
case SC_PODD:
DEBUG(DB_LGI, "Setting 7-bit, odd parity\n", 0);
ttpar(ODD);
break;
case SC_IFBL:
if (ttblind())
{
DEBUG(DB_LGI, "Blind mux,\n", 0);
goto _chgstate;
}
break;
case SC_IFBG:
if (ttblind() && sc_counter > curscr->intprm)
{
DEBUG(DB_LGI, "Blind mux & ctr > %d\n",
curscr->intprm);
goto _chgstate;
}
break;
case SC_IFGT:
if (sc_counter > curscr->intprm)
{
DEBUG(DB_LGI, "Counter > %d\n", curscr->intprm);
goto _chgstate;
}
break;
case SC_GOTO:
_chgstate:
DEBUG(DB_LGI, "Changing to state %s\n",
curscr->newstate);
curstate = find_state(begin, curscr->newstate);
if (curstate == NULL)
{
logit("New state not found",
curscr->newstate);
return FAIL;
}
goto _newstate;
case SC_SEND:
ttoslow(curscr->strprm, curscr->intprm, sc_chrdly);
break;
case SC_TELN:
if (curscr->intprm > paramc - 1)
{
sprintf(tempstr, "telno - param #%d", curscr->intprm);
logit(tempstr, " not present");
return FAIL;
}
strcpy(telno, paramv[curscr->intprm]);
DEBUG(DB_LGII, "telno set to %s\n", telno);
break;
case SC_SNDP:
if (curscr->intprm > paramc - 1)
{
sprintf(tempstr, "sendstr - param #%d", curscr->intprm);
logit(tempstr, " not present");
return FAIL;
}
prmlen = xlat_str(tempstr, paramv[curscr->intprm]);
ttoslow(tempstr, prmlen, sc_chrdly);
break;
case SC_IF1P:
if (curscr->intprm < paramc)
goto _chgstate;
break;
case SC_IF0P:
if (curscr->intprm >= paramc)
goto _chgstate;
break;
case SC_DIAL:
if(telno[0] == '\0')
{
logit("telno not set", "");
return(FAIL);
}
sc_dtmo = (sc_dtime+sc_chrdly)*strlen(telno)
+ sc_carrtime;
c=strcpy(tempstr, telno);
for (; *c!='\0'; c++)
{
if (*c == 'W')
{
*c = sc_wchar;
sc_dtmo += sc_wtime;
}
else if (*c == 'P')
{
*c = sc_pchar;
sc_dtmo += sc_ptime;
}
}
DEBUG(DB_LGI, "Dialing, default timeout is %d millisecs\n", sc_dtmo);
ttoslow(tempstr, 0, sc_chrdly);
break;
case SC_TIMO:
case SC_XPCT:
case SC_CARR:
case SC_XPST:
expcnt++;
break;
}
}
if (expcnt == 0)
{
if (curscr != (struct script *)NULL &&
(curscr->opcode == SC_LABEL))
{
curstate = curscr;
DEBUG(DB_LGI, "Fell through to state %s\n",
curstate->strprm);
goto _newstate;
}
else
{
logit("No way out of state", curstate->strprm);
return FAIL;
}
}
time(&sc_begwait);
DEBUG(DB_LGI, "Doing expects for state %s\n", curstate->strprm);
charlog((char *)NULL, 0, DB_LGI, "Received");
while (1)
{
chr = xgetc(1);
charlog(&chr, 1, DB_LGI, (char *)NULL);
if (chr != EOF)
{
if (expin < MAX_EXPCT)
{
expbuf[expin++] = chr & 0x7f;
}
else
{
strncpy(expbuf, &expbuf[1], MAX_EXPCT-1);
expbuf[MAX_EXPCT-1] = chr & 0x7f;
}
}
for (curscr = curstate->next;
(curscr != (struct script *)NULL) &&
(curscr->opcode != SC_LABEL);
curscr = curscr->next)
{
switch (curscr->opcode)
{
case SC_TIMO:
sc_secs = curscr->intprm;
if (sc_secs == 0)
sc_secs = sc_dtmo;
sc_secs /= 1000;
if (time(NULL)-sc_begwait > sc_secs)
{
DEBUG(DB_LGI,
"\nTimed out (%d secs)\n", sc_secs);
goto _chgstate;
}
break;
case SC_CARR:
if (ttcd())
{
DEBUG(DB_LGI, "\nGot carrier\n", 0);
goto _chgstate;
}
break;
case SC_HUPS:
if (fShangup)
{
DEBUG(DB_LGI, "\nGot data set hangup\n", 0);
goto _chgstate;
}
break;
case SC_XPCT:
if ((expin >= curscr->intprm) &&
(strncmp(curscr->strprm,
&expbuf[expin - curscr->intprm],
curscr->intprm) == SAME))
{
charlog(curscr->strprm, curscr->intprm,
DB_LGI, "Matched");
goto _chgstate;
}
break;
case SC_XPST:
if(curscr->intprm >paramc-1)
{
sprintf(tempstr,"expectstr - param#%d",curscr->intprm);
logit(tempstr, " not present");
return(FAIL);
}
prmlen=xlat_str(tempstr,paramv[curscr->intprm]);
if((expin >= prmlen) &&
(strncmp(tempstr,&expbuf[expin-prmlen],
prmlen) == SAME))
{
charlog(tempstr,prmlen,DB_LGI, "Matched");
goto _chgstate;
}
break;
}
}
}
}
static int usignal(isig)
int isig;
{
DEBUG(DB_LOG, "Caught signal %d. Exiting...\n", isig);
restore_tty();
exit(FAIL);
}
static int uhup(isig)
int isig;
{
DEBUG(DB_LOG, "Data set hangup.\n");
fShangup = TRUE;
}
static char xgetc(tmo)
int tmo;
{
char c;
struct timeval s;
int f = 1;
int result;
if(read(0, &c, 1) <= 0)
{
s.tv_sec = (long)tmo;
s.tv_usec = 0L;
if(select (1, &f, (int *) NULL, &f, &s) == 1)
read(0, &c, 1);
else
c = '\377';
}
return(c);
}
void msleep(msec)
long msec;
{
#if HAVE_USLEEP
if(msec == 0)
return;
usleep (msec * (long)1000);
#endif
#if HAVE_NAPMS
if(msec == 0)
return;
napms (msec);
#endif
#if HAVE_NAP
if(msec == 0)
return;
nap (msec);
#endif
#if HAVE_POLL
struct pollfd sdummy;
if(msec == 0)
return;
poll (&sdummy, 0, msec);
#endif
#if USE_SELECT_TIMER
struct timeval s;
if(msec == 0)
return;
s.tv_sec = msec / 1000L;
s.tv_usec = (msec % 1000L) * 1000L;
select (0, (int *) NULL, (int *) NULL, (int *) NULL, &s);
#endif
#if ! HAVE_NAPMS && ! HAVE_NAP && ! HAVE_USLEEP && \
! HAVE_POLL && ! USE_SELECT_TIMER
if(msec == 0)
return;
sleep (1);
#endif
}
static void DEBUG(level, msg1, msg2)
int level;
char *msg1, *msg2;
{
if ((dbf != NULL) && DEBUG_LEVEL(level))
fprintf(dbf, msg1, msg2);
}
static void charlog(buf, len, mask, msg)
char *buf;
int len, mask;
char *msg;
{
char tbuf[256];
if (DEBUG_LEVEL(mask) && dbf != NULL)
{
if(msg == (char *)NULL)
msg = "";
strncpy(tbuf, buf, len);
tbuf[len] = '\0';
if(len == 1 && tbuf[0] == '\377')
strcpy(tbuf, "<1 sec.>");
fprintf(dbf, "%s %s\n", msg, tbuf);
}
}
static void setup_tty()
{
register int i;
ioctl(0, TCGETA, &old);
new = old;
for(i = 0; i < 7; i++)
new.c_cc[i] = '\0';
new.c_cc[VMIN] = 0;
new.c_cc[VTIME] = 10;
new.c_iflag = ISTRIP;
new.c_lflag = 0;
ioctl(0, TCSETA, &new);
}
static void restore_tty(sig)
int sig;
{
ioctl(0, TCSETA, &old);
return;
}
static void ttoslow(s, len, delay)
char *s;
int len;
time_t delay;
{
int i;
if (len == 0)
len = strlen(s);
charlog (s, len, DB_LGI, "Sending slowly");
for (i = 0; i < len; i++, s++)
{
write(1, s, 1);
msleep(delay);
}
}
static void ttflui()
{
if(isatty(0))
(void) ioctl ( 0, TCFLSH, 0);
}
static int ttcd()
{
return TRUE;
}
static void tthang()
{
if(!isatty())
return;
#ifdef TCCLRDTR
(void) ioctl (1, TCCLRDTR, 0);
sleep (2);
(void) ioctl (1, TCSETDTR, 0);
#endif
return;
}
static void ttbreak()
{
(void) ioctl (1, TCSBRK, 0);
}
static int ttblind()
{
return FALSE;
}
static void tt7bit(enable)
int enable;
{
if(enable)
new.c_iflag |= ISTRIP;
else
new.c_iflag &= ~ISTRIP;
ioctl(0, TCSETA, &new);
}
static void ttpar(mode)
int mode;
{
switch(mode)
{
case NONE:
new.c_iflag &= ~(INPCK | IGNPAR);
new.c_cflag &= ~(CSIZE | PARENB | PARODD);
new.c_cflag |= CS8;
break;
case EVEN:
new.c_iflag |= (INPCK | IGNPAR);
new.c_cflag &= ~(CSIZE | PARODD);
new.c_cflag |= (CS7 | PARENB);
break;
case ODD:
new.c_iflag |= (INPCK | IGNPAR);
new.c_cflag &= ~(CSIZE);
new.c_cflag |= (CS7 | PARENB | PARODD);
break;
}
ioctl(0, TCSETA, &new);
}