📄 term.c
字号:
fd = fopen(nbuf, "r");
if( fd )
{
fscanf(fd, "%d", &i);
fclose(fd);
if( i == getpid() || kill(i, 0) != 0 )
unlink(nbuf);
}
}
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
#ifdef SCRIPT
#define MAXMATCH 20 /* Max number of match strings at a time */
#define MATCHLEN 20 /* Max size of a match string */
static int starcount = 50;
static FILE * script_file;
static char buffer[MATCHLEN*3];
static int offset = sizeof(buffer)/2;
static check_match();
static clear_matchtab();
static read_matchset();
static char * get_item();
static vdisp();
static script_com();
static int timeout = 0;
static long last_ok = 0;
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
out_schar(ch)
int ch;
{
long now;
int ok = 1;
if( ch != 0 ) /* Completely ignore nuls */
{
if( ch != '\377' ) buffer[offset++] = (ch&0x7F);
else buffer[offset++] = '\377';
if( offset == sizeof(buffer) )
{
offset = sizeof(buffer)/2;
memcpy(buffer, buffer+sizeof(buffer)/2, sizeof(buffer)/2);
}
buffer[offset] = '\0';
ok = check_match();
}
time(&now);
if( timeout && now > last_ok + timeout )
{
printf("\r\nTimeout ... Script aborted\r\n");
cleanup();
}
return ok;
}
open_script(fname)
char * fname;
{
char buf[200];
if(script_file) close_script();
strcpy(buf, fname);
strcat(buf, ".sct");
script_file = fopen(buf, "r");
if( script_file == 0 )
{
strcpy(buf, "sct/");
strcat(buf, fname);
strcat(buf, ".sct");
script_file = fopen(buf, "r");
}
if( script_file == 0 ) return 0;
time(&last_ok);
timeout = 10;
read_matchset();
check_match();
return 1;
}
close_script()
{
clear_matchtab();
if(script_file)
fclose(script_file);
script_file = 0;
timeout = 0;
}
#define VALIDREC 1 /* This record is available for matching */
#define ALTMATCH 2 /* Is an extra match record for prev line */
#define SCCOM 4 /* sendstr is a script command */
#define UXCOM 8 /* sendstr is a unix command */
static
struct matchrec
{
char match[MATCHLEN];
int matchlen;
int maxcount;
char * sendstr;
int flags;
}
matchtab[MAXMATCH];
static int matcount;
static
check_match()
{
struct matchrec * sptr;
struct matchrec * mptr;
extern int line_fd;
int i,j;
int checked_one;
Restart:
sptr = matchtab;
mptr = matchtab;
checked_one = 0;
for(i=0; i<matcount; i++, mptr++)
{
if( (mptr->flags & ALTMATCH) == 0 ) sptr = mptr;
if( (sptr->flags & VALIDREC) == 0 ) continue;
checked_one = 1;
if( (mptr->matchlen == 0
|| ( buffer[offset-mptr->matchlen] == mptr->match[0]
&& strcmp(buffer+offset-mptr->matchlen, mptr->match) == 0 )
)
)
{
time(&last_ok);
offset = sizeof(buffer)/2;
memset(buffer, 0, sizeof(buffer));
if( mptr->matchlen ) vdisp(0, "<Found>\r\n");
if( sptr->flags & SCCOM )
switch(script_com(sptr->sendstr))
{
case 0: break;
case 1: close_script(); return 0;
case 2: goto Restart;
}
#if 0
else if( sptr->flags & UXCOM )
#endif
else
{
vdisp(0, "<Sending> '");
if( line_fd )
{
/* Yes we want this ssslllooowww incase */
/* the other end is stupid */
/* Of course this isn't really that slow */
char ch[2];
ch[1] = '\0';
for(i=0; sptr->sendstr[i]; i++)
{
ch[0] = sptr->sendstr[i];
vdisp(1, ch);
if(*ch == '\377')
sleep(2);
else
{
write(line_fd, ch, 1);
#ifdef M_XENIX
nap(20);
#else
usleep(20000L);
#endif
}
}
}
vdisp(0, "'\r\n");
}
time(&last_ok);
if( sptr->maxcount )
{
if( --(sptr->maxcount) == 0 )
{
sptr->flags &= ~VALIDREC;
}
}
else
{
read_matchset();
goto Restart; /* Incase this is a '- "Send"' line */
}
break;
}
}
return checked_one;
}
static
clear_matchtab()
{
int i;
for(i=0; i<matcount; i++)
if( matchtab[i].sendstr ) free(matchtab[i].sendstr);
memset(matchtab, 0, sizeof(matchtab));
matcount = 0;
}
static
read_matchset()
{
char lbuf[128];
char sbuf[128];
char * p;
int i;
int saveptr;
int keepgoing = 1;
struct matchrec new;
int wflg = 0;
clear_matchtab();
memset(&new, 0, sizeof(new));
if( script_file == 0 ) return;
while( keepgoing )
{
if( fgets(lbuf, sizeof(lbuf), script_file) == 0 )
{
struct script_sv *p = saved_sc;
if( p == 0 ) break;
fclose(script_file);
script_file = p->scrfd;
strcpy(script_args, p->script_args);
saved_sc = p->next;
free(p);
continue;
}
p = lbuf;
while( *p == ' ' || *p == '\t' ) p++;
if( *p == '#' || *p == '\n' || *p == '\0' ) continue;
#ifdef VARS
if( *p == '%' )
if( *p == '$' )
#endif
memset(&new, 0, sizeof(new));
if( *p == '*' || *p == '?' )
{
if( *p == '?' ) new.maxcount = 1;
else new.maxcount = starcount;
p++; while( *p == ' ' || *p == '\t' ) p++;
}
else
keepgoing = 0;
saveptr = matcount;
do
{
if( *p == ',' ) p++;
p = get_item(p, new.match, sizeof(new.match), 0);
if( p == 0 )
{
fprintf(stderr, "Syntax error:%s", lbuf);
goto next_line;
}
new.matchlen = strlen(new.match);
if( new.matchlen )
{
if( wflg == 0 ) { vdisp(0, "<Waiting>"); wflg = 1; }
vdisp(0, " '");
vdisp(1, new.match);
vdisp(0, "'");
}
while( *p == ' ' || *p == '\t' ) p++;
if( matcount >= MAXMATCH )
fprintf(stderr, "Too many match strings\r\n");
else
{
matchtab[matcount] = new;
matcount++;
new.flags |= ALTMATCH;
}
}
while( *p == ',');
p = get_item(p, sbuf, sizeof(sbuf), 1);
if( p == 0 )
{
fprintf(stderr, "Syntax error:%s", lbuf);
goto next_line;
}
if( *sbuf != '"' )
{
matchtab[saveptr].sendstr = malloc(strlen(sbuf)+1);
strcpy(matchtab[saveptr].sendstr, sbuf);
matchtab[saveptr].flags |= SCCOM;
while( (p = get_item(p, sbuf, sizeof(sbuf), 1)) != 0 )
{
matchtab[saveptr].sendstr = realloc(matchtab[saveptr].sendstr,
strlen(matchtab[saveptr].sendstr) + strlen(sbuf)+2);
strcat(matchtab[saveptr].sendstr, " ");
strcat(matchtab[saveptr].sendstr, sbuf);
}
}
else
{
matchtab[saveptr].sendstr = malloc(strlen(sbuf));
strcpy(matchtab[saveptr].sendstr, sbuf+1);
}
matchtab[saveptr].flags |= VALIDREC;
next_line: ;
}
if( wflg ) vdisp(0, "\r\n");
}
static char *
get_item(p, buf, szbuf, itype)
char * p;
char * buf;
int szbuf;
int itype;
{
char stc;
while( *p == ' ' || *p == '\t' ) p++;
if( *p == '-' )
{
if( itype == 0 ) buf[0] = '\0'; else buf[0] = '"';
buf[1] = '\0';
p++; return p;
}
else if( ( itype == 0 && *p != '"' ) || *p == '\0' )
{
buf[0] = '\0'; return 0;
}
if( itype == 0 ) stc = *p++;
else { *buf++ = stc = *p++; szbuf--; }
while( *p )
{
if( stc == '"' && *p == '"' ) break;
if( stc != '"' && (*p == ' ' || *p == '\t' || *p == '\n') ) break;
if( szbuf < 2+(itype&1) ) { p++; continue; ; }
szbuf--;
if( *p == '\\' && p[1] )
{
switch(p[1])
{
case 'b': *buf++ = '\b'; break;
case 'c': itype &= ~1; break;
case 'd': *buf++ = '\377'; break;
case 'f': *buf++ = '\f'; break;
case 'n': *buf++ = '\n'; break;
case 'r': *buf++ = '\r'; break;
case 't': *buf++ = '\t'; break;
case 'a':
{
char * q = script_args;
while(*q) *buf++ = *q++;
}
break;
default: *buf++ = p[1]; break;
}
p+=2;
}
else
*buf++ = *p++;
}
if( itype&1 && stc == '"' ) *buf++ = '\r';
*buf = 0;
if( *p ) p++;
return p;
}
static
script_com(str)
char * str;
{
extern char logfname[];
vdisp(0, "<Command> '");
vdisp(1, str);
vdisp(0, "'\r\n");
if( strncmp("timeout=", str, 8) == 0 )
timeout = atoi(str+8);
else if( strncmp("starcount=", str, 10) == 0 )
starcount = atoi(str+10);
else if( strncmp("cd=", str, 3) == 0 )
chdir(str+3);
#ifdef LOGFILE
else if( strncmp("logfile=", str, 8) == 0 )
strcpy(logfname, str+8);
else if( strcmp("logstart", str) == 0 )
{
if( lfile == 0 ) { printf("\r\n"); open_logfile(); }
}
else if( strcmp("logbuf", str) == 0 )
ltptr = ltbuf;
#endif
else if( strncmp("con", str, 3) == 0 )
return 1;
else if( strcmp("abort", str) == 0 )
{
printf("\r\nAborted\r\n");
cleanup();
}
else if( strcmp("end", str) == 0 )
{
printf("\r\nScript completed\r\n");
cleanup();
}
else if( strncmp("call ", str, 5) == 0 )
{
struct script_sv * p;
char * s;
p = (struct script_sv * ) malloc(sizeof(struct script_sv));
if( p == 0 ) { fprintf(stderr, "Out of memory\r\n"); return 0; }
p->next = saved_sc;
p->scrfd = script_file;
strcpy(p->script_args, script_args);
s = strchr(str+5, ' ');
script_file = 0;
if( s ) { *s = '\0'; strcpy(script_args, s+1); }
else *script_args = '\0';
if( open_script(str+5) )
{
saved_sc = p;
return 2;
}
else
{
fprintf(stderr, "Cannot open '%s.sct'\r\n", str+5);
script_file = p->scrfd;
strcpy(script_args, p->script_args);
free(p);
}
}
else if( strcmp("nobreak", str) == 0 )
{
set_mode(2);
signal(SIGINT, SIG_IGN);
}
else if( strcmp("parity", str) == 0 )
{
Bit_mask = 0x7F; Parity=1;
}
else if( strcmp("sevenbit", str) == 0 )
{
Bit_mask = 0x7F;
}
else if( strcmp("eightbit", str) == 0 )
{
Bit_mask = 0xFF;
}
else if( strcmp("rawterm", str) == 0 )
{
Termtype = 0;
}
else if( strcmp("dumbterm", str) == 0 )
{
Termtype = 1;
}
else
fprintf(stderr, "< Error > Command '%s' not found!\r\n", str);
return 0;
}
static vdisp(flg, str)
int flg;
char * str;
{
if( verbose != 1 ) return;
fflush(stdout);
if( !flg ) fprintf(stderr, "%s", str);
else while( *str )
{
int ch = *str++;
ch &= 0xFF;
if( ch >= ' ' && ch != 0x9B && ch != '\\' && ch != 0xFF)
fputc(ch, stderr);
else if( ch < ' ' )
fprintf(stderr, "^%c", ch+'@');
else if( ch == 0xFF )
fprintf(stderr, "\\d");
else
fprintf(stderr, "\\0x%02x", ch);
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -