- inserted possiblity to send spy commands when busy

- general enhancements of six
This commit is contained in:
zolliker
2005-03-17 16:08:08 +00:00
parent 7bf31b710b
commit c33fb15498
3 changed files with 289 additions and 176 deletions

View File

@ -11,50 +11,67 @@
#include "myc_err.h" #include "myc_err.h"
#include "myc_str.h" #include "myc_str.h"
typedef enum { NORMAL, SPY, CLIENT } Mode;
#define MAXMSG 256
static char *host; static char *host;
static char instr[32]; static char instr[32];
static int fd; static int sock[2]; /* main socket and spy socket */
static int level=3; static int level=3;
static int deflevel=0; static int deflevel=0;
static int remember=1; static int remember=1;
static int hidecom=1;
static char user1[32]=""; static char user1[32]="";
static char pswd1[32]=""; static char pswd1[32]="";
static char user2[32]=""; static char user2[32]="";
static char pswd2[32]=""; static char pswd2[32]="";
static char status[MAXMSG]="Busy";
static int busy[2];
static int port;
static char msg0[MAXMSG];
static char msg1[MAXMSG]; /* contains messages for main and spy */
static int pos0 = 0;
static int pos1 = 0;
static char *prefix[3]={"| ", "* ", ""};
void Usage(int cmds_only) { void Usage(int cmds_only) {
if (!cmds_only) { if (!cmds_only) {
printf("\n"); printf(" \n");
printf(" six commandline options:\n"); printf(" six commandline options:\n");
printf(" - login as spy\n"); printf(" - login as spy\n");
printf(" + login as manager\n"); printf(" + login as manager\n");
printf(" 0 login as user\n"); printf(" 0 login as user\n");
printf(" -- set spy as default\n"); printf(" -- set spy as default\n");
printf(" 00 set user as default\n"); printf(" 00 set user as default\n");
printf(" ++ set manager as default\n"); printf(" ++ set manager as default\n");
printf(" help show this help text\n"); printf(" help show this help text\n");
printf(" -a or a ask always for username/password, forget passwords\n"); printf(" -a or a ask always for username/password, forget passwords\n");
printf(" -s or s simulation mode (on some instruments)\n"); printf(" -s or s simulation mode (on some instruments)\n");
printf(" -w or w do not skip welcome message\n"); printf(" -h \"host\" connect to a SICServer on a different host\n");
printf(" -h \"host\" connect to a SICServer on a different host\n"); printf(" -p \"port\" connect to a SICServer on a different port\n");
printf(" -p \"port\" connect to a SICServer on a different port\n"); printf(" -n do only a miminal login (no check of instrument)\n");
printf(" -n do not login\n"); printf(" no option login with default privilege\n");
printf(" no option login with default privilege\n");
} }
printf("\n"); printf("\n");
printf(" Special commands treated by six (these are no SICS commands!)\n"); printf(" Special commands treated by six (these are no SICS commands!)\n");
printf("\n"); printf("\n");
printf(" quit exit six\n"); printf(" quit exit six\n");
printf(" exit exit six\n"); printf(" exit exit six\n");
printf(" stop interrupt SICS\n"); printf(" stop interrupt SICS\n");
printf(" help show this help text\n"); printf(" six help show this help text\n");
printf(" + increase privilege\n"); printf(" + increase privilege\n");
printf(" - decrease privilege\n"); printf(" - decrease privilege\n");
printf("\n"); printf("\n");
printf(" The SICS status is shown, if it is not 'Eager to execute commands'.\n"); printf(" The SICS status is shown, if it is not 'Eager to execute commands'.\n");
printf(" A shown status does not prohibit to enter commands.\n"); printf(" A shown status does not prohibit to enter commands.\n");
printf("\n"); printf("\n");
printf(" Markus Zolliker, Oct. 2003\n"); printf(" When SICS is busy, an asterisk '*' is shown at the left of the line.\n");
printf(" You may then enter more commands, but only under Spy privilege.\n");
printf(" All communication to and from SICS is prefixed with '|' or '*',\n");
printf(" messages from the six client have no prefix.\n");
printf("\n");
printf(" Markus Zolliker, Mar. 2005\n");
printf("\n"); printf("\n");
} }
@ -92,19 +109,23 @@ int CocCreateSockAdr(
return 0; return 0;
} }
char* readWrite(int fd, int tmo, int skip, char *find) { char* readWrite(int tmo, int skip, char *find) {
char msg[256]; int lbuf, go, iret, n, shortTmo, l, socket;
int lbuf, pos, go, iret, n, ga; Mode idx;
char *p, chr; char *msg, *p, *q;
static char result[256]; char chr;
static char result[MAXMSG];
int match; int match;
static int laststat=0; static int laststat=0, laststatlen=0;
static int newline[2]={1,1};
/* /*
* read from fd and write to stdout until a key pressed, * read from registered sockets and write to stdout until a key pressed,
* a timeout occurred (tmo [msec]), or a <go ahead> message * a timeout occurred (tmo [msec]), or a TRANSACTIONFINISHED message
* received. * received. Handle also status and TRANSACTIONSTART messages.
*
* skip=0: skip response that does not match * skip=0: skip response that does not match
* skip=1: skip all responses * skip=1: skip all responses
* skip=2: skip all responses, quit on match
* *
* if the response starts with the contents of find * if the response starts with the contents of find
* the second part of the line is returned * the second part of the line is returned
@ -114,84 +135,123 @@ char* readWrite(int fd, int tmo, int skip, char *find) {
*/ */
n=0; n=0;
go=0; go=0;
ga=0; shortTmo = 0;
str_copy(result, ""); str_copy(result, "");
iret=term_wait_fd(fd, tmo); socket=term_wait_socket(0, tmo);
while (iret>0) { while (socket > 0) {
pos=0; idx = (socket == sock[1]);
ERR_SI(lbuf=recv(fd, &chr, 1, 0)); ERR_SI(lbuf=recv(socket, &chr, 1, 0));
if (lbuf==0) return "0"; if (lbuf==0) return "0";
while (lbuf>0) { while (lbuf>0) {
if (chr<' ') { if (chr == '\n') break;
if (chr=='\n') break; if (idx) {
if (chr==-1) { if (chr != '\r' && pos1 < MAXMSG-1) { /* cut lines longer than MAXMSG */
ga=1; msg1[pos1]=chr;
} else if (chr==-7 && ga) { pos1++;
go=1; break;
} else {
ga=0;
if (chr!='\r') {
msg[pos++]=chr;
}
} }
} else { } else {
msg[pos++]=chr; if (chr != '\r' && pos0 < MAXMSG-1) { /* cut lines longer than MAXMSG */
msg0[pos0]=chr;
pos0++;
}
} }
iret=term_wait_fd(fd, tmo); socket=term_wait_socket(0, tmo);
if (iret!=1) break; if (socket <= 0) break;
ERR_SI(lbuf=recv(fd, &chr, 1, 0)); idx = (socket == sock[1]);
ERR_SI(lbuf=recv(socket, &chr, 1, 0));
}
if (idx) {
msg1[pos1] = '\0';
pos1 = 0;
msg = msg1;
} else {
msg0[pos0] = '\0';
pos0 = 0;
msg = msg0;
} }
msg[pos]='\0';
/* printf("[%s]\n", msg); */ /* printf("[%s]\n", msg); */
match=0; match=0;
if (find != NULL) { if (strncmp(msg, "TRANSACTIONSTART", 16) == 0) {
busy[idx] = 1;
match = 1;
} else if (strncmp(msg, "TRANSACTIONFINISHED", 19) == 0) {
busy[idx] = 0;
if (skip != 2) go = 1;
shortTmo = 1;
match = 1;
} else if (idx == 0 && strncmp(msg, "status = ", 9) == 0) {
strcpy(status, msg+9);
l = strlen(msg);
if (status[0] == 'E') {
} else if (laststat!=status[0] || laststatlen != l) {
laststat = status[0];
laststatlen = l;
}
match = 1;
}
if (idx == 0 && find != NULL) {
p=strstr(msg, find); p=strstr(msg, find);
if (p!=NULL) { /* copy message to result */ if (p!=NULL) { /* copy message to result */
if (p==msg) { if (p==msg) {
str_copy(result, msg+strlen(find)); str_copy(result, msg+strlen(find));
p=strchr(result,'\r'); q=strchr(result,'\r');
if (p != NULL) *p='\0'; if (q != NULL) *q='\0';
} else { } else {
str_copy(result, "1"); str_copy(result, "1");
} }
match=1; match=1;
if (find==strstr(find,"status")) { if (skip == 2) go = 1;
if (result[0]=='E') {
tmo=100;
} else if (laststat!=result[0]) {
laststat=result[0];
go=1;
}
}
} }
} }
if (skip || match) { /* skip text */ if (hidecom && (skip || match)) { /* skip text */
n+=strlen(msg); n+=strlen(msg);
} else { } else {
term_clear(); term_clear();
if (newline[idx]) {
fputs(prefix[idx], stdout);
newline[idx]=0;
}
if (strncmp(msg, "ERROR:", 6)==0) { if (strncmp(msg, "ERROR:", 6)==0) {
fputs("\033[1;31m", stdout); fputs("\033[1;31m", stdout); /* red bold */
fputs(msg, stdout); fputs(msg, stdout);
fputs("\033[0m", stdout); fputs("\033[0m", stdout); /* clear colors */
} else { } else {
fputs(msg, stdout); fputs(msg, stdout);
} }
if (chr=='\n') fputs("\n", stdout); if (chr=='\n') {
newline[idx]=1;
fputs("\n", stdout);
}
} }
if (go) break; if (go) break;
iret=term_wait_fd(fd, tmo); socket = term_wait_socket(0, shortTmo?100:tmo);
shortTmo = 0;
} }
fflush(stdout);
if (iret==0) laststat=0; if (iret==0) laststat=0;
return result; return result;
OnError: return NULL; OnError: return NULL;
} }
int sendCmd(int fd, char *cmd) { void PrintCmd(char *buf, int mode) {
fputs("\r", stdout); /* clear command line */
fputs(prefix[mode], stdout);
fputs("\033[K\n", stdout); /* empty line */
fputs(prefix[mode], stdout);
fputs("\033[1;34m", stdout); /* bold blue */
fputs(">> ", stdout);
fputs(buf, stdout);
fputs("\033[0m\n", stdout); /* clear colors */
}
int sendCmd(int socket, char *cmd) {
int l; int l;
char buf[256]; char buf[256];
ERR_I(str_copy(buf, cmd));
ERR_I(str_append(buf, "\r\n")); snprintf(buf, sizeof buf, "fulltransact %s\n", cmd);
ERR_SI(l=send(fd, buf, strlen(buf), 0)); ERR_SI(l=send(socket, buf, strlen(buf), 0));
return l; return l;
OnError: return -1; OnError: return -1;
} }
@ -287,8 +347,8 @@ int setrights(int gotolevel) {
ask=1; ask=1;
if (us[0]!='\0' && pw[0]!='\0' && remember) { if (us[0]!='\0' && pw[0]!='\0' && remember) {
sprintf(buf, "config rights %s %s", us, pw); sprintf(buf, "config rights %s %s", us, pw);
ERR_I(sendCmd(fd, buf)); ERR_I(sendCmd(sock[0], buf));
ERR_P(p=readWrite(fd,10000,0,"Acknowledged")); ERR_P(p=readWrite(12000,0,"Acknowledged"));
if (*p=='\0') { if (*p=='\0') {
if (0==strcmp(us, user1)) { if (0==strcmp(us, user1)) {
user1[0]='\0'; user1[0]='\0';
@ -321,9 +381,9 @@ int setrights(int gotolevel) {
if (0==strcmp(pswd, "exit")) return 1; if (0==strcmp(pswd, "exit")) return 1;
if (pswd[0]!='\0') { if (pswd[0]!='\0') {
sprintf(buf, "config rights %s %s", user, pswd); sprintf(buf, "config rights %s %s", user, pswd);
ERR_I(sendCmd(fd, buf)); ERR_I(sendCmd(sock[0], buf));
ERR_P(p=readWrite(fd,10000,0,"Acknowledged")); ERR_P(p=readWrite(12000,0,"Acknowledged"));
if (*p=='\0') { if (*p=='\0') { /* no success */
gotolevel=3; gotolevel=3;
if (0==strcmp(user, user1)) { if (0==strcmp(user, user1)) {
user1[0]='\0'; user1[0]='\0';
@ -334,7 +394,7 @@ int setrights(int gotolevel) {
pswd2[0]='\0'; pswd2[0]='\0';
} }
pw=NULL; pw=NULL;
} else { } else { /* success */
if (0==strcmp(user, user1)) { if (0==strcmp(user, user1)) {
str_copy(pswd1, pswd); str_copy(pswd1, pswd);
} }
@ -349,8 +409,8 @@ int setrights(int gotolevel) {
} }
} }
level=gotolevel; level=gotolevel;
ERR_I(sendCmd(fd, "config list")); ERR_I(sendCmd(sock[0], "config list"));
ERR_P(p=readWrite(fd,10000,1,"UserRights = ")); ERR_P(p=readWrite(12000,1,"UserRights = "));
if (*p!='\0') { if (*p!='\0') {
level=*p-'0'; level=*p-'0';
} }
@ -391,36 +451,54 @@ int setrights(int gotolevel) {
OnError: return -1; OnError: return -1;
} }
int Connect(void) {
int sock;
struct sockaddr_in sadr;
char *p;
ERR_I(CocCreateSockAdr(&sadr, host, port));
ERR_SI(sock=socket(AF_INET, SOCK_STREAM, 0));
term_reg_socket(sock);
ERR_SI(connect(sock, (struct sockaddr *)&sadr, sizeof(sadr)));
ERR_SI(send(sock, "Spy 007\n", 8, 0));
ERR_P(p=readWrite(12000,2,"Login O"));
if (*p!='K') { /* looking for the 'K' of 'Login OK' */
printf("rejected\n");
return 0;
}
return sock;
OnError: return -1;
}
int main (int argc, char *argv[]) { int main (int argc, char *argv[]) {
int iret, pos; int iret, pos;
fd_set mask; fd_set mask;
int i, j, port, skip, gotolevel, sicslogin; int i, j, gotolevel, sicslogin;
int savehist = 0; int savehist = 0;
char buf[128], lbuf[16], ilow[64]; char buf[128], lbuf[16], ilow[64];
char stdPrompt[128], prompt[256]; char stdPrompt[128], prompt[256];
char *sim=""; char *sim="";
char *p, *statusMatch=NULL; char *p;
char *bar;
char *pnam[4]={"0", "MANAGER", "user", "spy"}; char *pnam[4]={"0", "MANAGER", "user", "spy"};
struct sockaddr_in sadr;
printf("---------------------------------------------------\n"); printf("---------------------------------------------------\n");
printf("six, a fast SICS commandline client (doc: six help)\n"); printf("six, a fast SICS commandline client (doc: six help)\n");
printf("---------------------------------------------------\n"); printf("---------------------------------------------------\n");
port=1301; port=2911;
skip=1;
sicslogin=1; sicslogin=1;
j=0; j=0;
deflevel=0; deflevel=0;
gotolevel=0; gotolevel=0;
host="0"; /* localhost by default */ host="0"; /* localhost by default */
sock[1]=0; /* do not yet connect 2nd connection */
for (i=1; i<argc; i++) { for (i=1; i<argc; i++) {
if (0==strcmp(argv[i], "-s") || 0==strcmp(argv[i], "s")) { if (0==strcmp(argv[i], "-s") || 0==strcmp(argv[i], "s")) {
port=1303; sim="-sim"; port=2927; sim="-sim";
} else if (0==strcmp(argv[i], "-w") || 0==strcmp(argv[i], "w")) {
skip=0;
} else if (0==strcmp(argv[i], "-a") || 0==strcmp(argv[i], "a")) { } else if (0==strcmp(argv[i], "-a") || 0==strcmp(argv[i], "a")) {
remember=0; remember=0;
} else if (0==strcmp(argv[i], "-d") || 0==strcmp(argv[i], "d")) {
hidecom=0;
} else if (0==strcmp(argv[i], "+")) { } else if (0==strcmp(argv[i], "+")) {
gotolevel=1; gotolevel=1;
} else if (0==strcmp(argv[i], "-")) { } else if (0==strcmp(argv[i], "-")) {
@ -474,24 +552,12 @@ int main (int argc, char *argv[]) {
} }
} }
} }
ERR_I(CocCreateSockAdr(&sadr, host, port)); ERR_I(sock[0]=Connect());
ERR_SI(fd=socket(AF_INET, SOCK_STREAM, 0)); if (sock[0] == 0) return 0;
term_reg_socket(fd);
ERR_SI(connect(fd, (struct sockaddr *)&sadr, sizeof(sadr)));
if (sicslogin) { if (sicslogin) {
ERR_I(sendCmd(fd, "sicslogin Spy 007"));
ERR_P(p=readWrite(fd,10000,skip,"SICS"));
if (*p=='\0') {
printf("rejected\n");
return 0;
}
if (skip) printf("reading welcome message ...\n");
ERR_I(sendCmd(fd, "status interest")); ERR_I(sendCmd(sock[0], "Instrument"));
ERR_P(readWrite(fd,10000,0,"OK")); ERR_P(p=readWrite(12000,0,"Instrument = "));
ERR_I(sendCmd(fd, "Instrument"));
ERR_P(p=readWrite(fd,10000,0,"Instrument = "));
str_copy(instr, p); str_copy(instr, p);
if (*instr=='\0') { if (*instr=='\0') {
printf("can not detect instrument\n"); printf("can not detect instrument\n");
@ -503,18 +569,20 @@ int main (int argc, char *argv[]) {
str_copy(instr, "SANS2"); str_copy(instr, "SANS2");
} }
ERR_I(i=setrights(gotolevel)); ERR_I(i=setrights(gotolevel));
if (i==1) return 0;
printf("\rlogged in to SICS as %s on %s\n", pnam[level], instr); printf("\rlogged in to SICS as %s on %s\n", pnam[level], instr);
str_lowcase(ilow,instr); str_lowcase(ilow,instr);
sprintf(stdPrompt, "six[%s] ", ilow); sprintf(stdPrompt, "six[%s] ", ilow);
ERR_I(sendCmd(fd, "status")); ERR_I(sendCmd(sock[0], "status interest"));
ERR_P(p=readWrite(fd,10000,0,"status = ")); ERR_P(readWrite(12000,0,"OK"));
ERR_I(sendCmd(sock[0], "status"));
ERR_P(readWrite(12000,0,NULL));
} else { } else {
sprintf(stdPrompt, "six[%s] ", host); sprintf(stdPrompt, "six[%s] ", host);
p="E"; status[0]='E'; status[1]='\0';
} }
iret=1; iret=1;
@ -525,27 +593,34 @@ int main (int argc, char *argv[]) {
savehist = 1; savehist = 1;
while (1) { while (1) {
if (*p=='E') { /* Eager to ... */ if (busy[0]) {
strcpy(prompt, stdPrompt); bar = prefix[SPY];
} else if (*p>'1') { } else {
sprintf(prompt, "%s(%s) ", stdPrompt, p); bar = "";
}
if (status[0] == 'E') { /* Eager to ... */
sprintf(prompt, "%s%s", bar, stdPrompt);
} else {
sprintf(prompt, "%s%s(%s) ", bar, stdPrompt, status);
} }
FD_ZERO(&mask); FD_ZERO(&mask);
FD_SET(fd, &mask); FD_SET(sock[0], &mask);
iret=term_get_line(buf, sizeof(buf)-2, &pos, prompt, &mask); iret=term_get_line(buf, sizeof(buf)-2, &pos, prompt, &mask);
if (iret==STDIN_FILENO) { /* input line terminated */ if (iret==STDIN_FILENO) { /* input line terminated */
str_lowcase(lbuf, buf); str_lowcase(lbuf, buf);
if (0==strcmp(lbuf,"quit")) break; if (0==strcmp(lbuf,"quit") || 0==strcmp(lbuf,"exit")) {
if (0==strcmp(lbuf,"exit")) break; PrintCmd(buf, CLIENT);
if (sicslogin) statusMatch="status = "; break;
skip=0; }
printf("\n");
if (0==strcmp(lbuf,"stop")) { if (0==strcmp(lbuf,"stop")) {
strcpy(buf, "INT1712 3"); PrintCmd(buf, CLIENT);
ERR_SI(send(sock[1], "INT1712 3\n", 10, 0));
buf[0]='\0';
} else if (0==strcmp(buf, "-") || } else if (0==strcmp(buf, "-") ||
0==strcmp(buf, "--") || 0==strcmp(buf, "--") ||
0==strcmp(buf, "+") || 0==strcmp(buf, "+") ||
0==strcmp(buf, "++")) { 0==strcmp(buf, "++")) {
PrintCmd(buf, CLIENT);
j=level; j=level;
if (buf[0]=='-') { if (buf[0]=='-') {
if (level<3) level++; if (level<3) level++;
@ -557,38 +632,61 @@ int main (int argc, char *argv[]) {
} }
term_off(); term_off();
ERR_I(i=setrights(level)); /* level might be changed */ ERR_I(i=setrights(level)); /* level might be changed */
if (i==1) return 0;
if (j!=level) { if (j!=level) {
printf("\rswitched to %s privilege\n", pnam[level]); printf("\rswitched to %s privilege\n", pnam[level]);
} else { } else {
printf("\rremain at %s privilege\n", pnam[level]); printf("\rremain at %s privilege\n", pnam[level]);
} }
skip=1; buf[0]='\0';
strcpy(buf, "status"); } else if (0==strcmp(buf, "help six")) {
} else if (0==strcmp(buf, "help")) { PrintCmd(buf, CLIENT);
Usage(1); Usage(1);
strcpy(buf, "status"); buf[0]='\0';
} else if (0==strcmp(buf, "six help")) { } else if (0==strcmp(buf, "six help")) {
PrintCmd(buf, CLIENT);
Usage(0); Usage(0);
strcpy(buf, "status"); buf[0]='\0';
} else if (0==strcmp(buf, "")) { }
strcpy(buf, "status"); if (buf[0] != 0) {
statusMatch=NULL; if (busy[0]) {
if (!sock[1]) {
ERR_I(sock[1] = Connect());
if (!sock[1]) {
printf("spy connection rejected\n");
}
}
if (busy[1]) {
/* print in red bold */
PrintCmd(buf, CLIENT);
fputs("\033[1;31mBUSY (already 2 commands pending)\033[0m\n", stdout);
buf[0]='\0';
} else if (sock[1]) {
PrintCmd(buf, SPY);
ERR_I(sendCmd(sock[1], buf));
} else {
PrintCmd(buf, CLIENT);
buf[0]='\0';
}
} else {
PrintCmd(buf, NORMAL);
ERR_I(sendCmd(sock[0], buf));
}
}
if (buf[0] != '\0') {
ERR_P(p=readWrite(500,0,NULL));
} else {
p="";
} }
ERR_I(sendCmd(fd, buf));
ERR_P(p=readWrite(fd,1000,skip,statusMatch));
buf[0]='\0'; buf[0]='\0';
pos=0; pos=0;
} else if (iret<0) { /* ?? */ } else { /* something arrived from sockets */
printf("\nwhat?\n"); exit(1); assert(iret == sock[0] || iret == sock[1]);
} else { /* socket iret ready to read */ ERR_P(p=readWrite(500,0,NULL));
assert(fd==iret); }
ERR_P(p=readWrite(fd,1000,0,"status = ")); if (strcmp(p, "0") == 0) {
if (strcmp(p, "0") == 0) { term_clear();
term_clear(); printf("\nconnection lost");
printf("\nconnection lost"); break;
break;
}
} }
} }
printf("\nexit %s\n", prompt); printf("\nexit %s\n", prompt);

View File

@ -25,13 +25,20 @@
static char esc_key='\0'; static char esc_key='\0';
static fd_set regMask; static fd_set regMask;
static int minReg=0, maxReg=0;
void term_reg_socket(int fd) { void term_reg_socket(int socket) {
FD_SET(fd, &regMask); FD_SET(socket, &regMask);
if (socket > maxReg) {
if (maxReg == 0) minReg = socket;
maxReg = socket;
} else if (socket < minReg) {
minReg = socket;
}
} }
void term_unr_socket(int fd) { void term_unr_socket(int socket) {
FD_CLR(fd, &regMask); FD_CLR(socket, &regMask);
} }
int term_raw_key(char *key, int msecTmo) { int term_raw_key(char *key, int msecTmo) {
@ -41,21 +48,37 @@ int term_raw_key(char *key, int msecTmo) {
return(sys_select_or_key(&mask, msecTmo, key)); return(sys_select_or_key(&mask, msecTmo, key));
} }
int term_wait_fd(int fd, int msecTmo) { int term_wait_socket(int socket, int msecTmo) {
fd_set mask; fd_set mask;
struct timeval tmo; struct timeval tmo;
int i; int i;
FD_ZERO(&mask); if (socket == 0) {
FD_SET(fd, &mask); mask = regMask;
} else {
FD_ZERO(&mask);
FD_SET(socket, &mask);
}
FD_SET(STDIN_FILENO, &mask); FD_SET(STDIN_FILENO, &mask);
tmo.tv_sec=msecTmo / 1000; tmo.tv_sec=msecTmo / 1000;
tmo.tv_usec=(msecTmo % 1000) * 1000+1; tmo.tv_usec=(msecTmo % 1000) * 1000;
i=select(FD_SETSIZE, &mask, NULL, NULL, &tmo); i=select(FD_SETSIZE, &mask, NULL, NULL, &tmo);
if (FD_ISSET(STDIN_FILENO, &mask)) { if (FD_ISSET(STDIN_FILENO, &mask)) {
return 0; return 0;
} }
return i; if (i<0) return i;
if (i) {
if (socket && FD_ISSET(socket, &mask)) {
return socket;
}
for (i=minReg; i<=maxReg; i++) {
if (FD_ISSET(i, &mask)) {
return i;
};
}
return -1;
}
return 0;
} }
int term_get_key(char *key, int msecTmo) { int term_get_key(char *key, int msecTmo) {
@ -250,7 +273,7 @@ static int dirty=0; /* line is to be cleared through a call of term_clear */
void term_clear(void) { void term_clear(void) {
if (dirty) { if (dirty) {
fputs("\r\033[K\033[0m", stdout); fputs("\r\033[K\033[0m", stdout); /* clear to end of line, clear colors */
dirty=0; dirty=0;
} }
} }
@ -281,19 +304,10 @@ int term_get_line(char *buf, int size, int *pos, char *prompt, fd_set *mask) {
while (1) { while (1) {
if (iret==-1 || key == RET_CHAR || key==EVT_CHAR) { /* refresh after a short timeout */ if (iret==-1 || key == RET_CHAR || key==EVT_CHAR) { /* refresh after a short timeout */
snprintf(tmp, sizeof tmp, "%s%s%s%s%s%.*s", snprintf(tmp, sizeof tmp, "%s%s%s%s%s%.*s",
"\r\033[1m", prompt, "\033[34m", buf, "\033[K\033[0m", l - *pos, back); "\r\033[1m", /* bold */
/* prompt, "\033[34m", /* blue */
assert(l*2+20+strlen(prompt)<sizeof(tmp)); buf, "\033[K\033[0m", /* clear to end of line, clear colors */
strcpy(tmp,"\r\033[1;34m"); l - *pos, back);
strcat(tmp, prompt);
strcat(tmp, buf);
strcat(tmp,"\033[K\033[0m");
j=strlen(tmp);
for (i=l; i>*pos; i--) {
tmp[j]='\b'; j++;
}
tmp[j]='\0';
*/
fputs(tmp, stdout); fputs(tmp, stdout);
if (iret==-1) { if (iret==-1) {
iret=term_get_key(&key, -1); /* no timeout */ iret=term_get_key(&key, -1); /* no timeout */
@ -322,9 +336,6 @@ int term_get_line(char *buf, int size, int *pos, char *prompt, fd_set *mask) {
} }
hist_pos=hist_end; hist_pos=hist_end;
term_save_hist(0); term_save_hist(0);
printf("\r\033[K\n\033[1;34m");
printf("%s", buf);
printf("\033[0m");
return(STDIN_FILENO); return(STDIN_FILENO);
/* normal EXIT */ /* normal EXIT */

View File

@ -3,16 +3,20 @@
#include <stdio.h> #include <stdio.h>
void term_reg_socket(int fd); void term_reg_socket(int socket);
/* register socket */ /* register socket */
void term_unr_socket(int fd); void term_unr_socket(int socket);
/* unregister socket */ /* unregister socket */
int term_raw_key(char *key, int msecTmo); int term_raw_key(char *key, int msecTmo);
int term_wait_fd(int fd, int msecTmo); int term_wait_socket(int socket, int msecTmo);
/* wait for a read event on fd or timeout */ /* wait for a read event on socket or timeout
special case socket=0: wait for a read event on any socket
return socket number
or 0 for timeout
*/
int term_get_key(char *key, int msecTmo); int term_get_key(char *key, int msecTmo);