new version

This commit is contained in:
zolliker
2005-06-29 14:09:07 +00:00
parent b12e896286
commit 33d23d4171

View File

@ -11,14 +11,15 @@
#include "myc_err.h" #include "myc_err.h"
#include "myc_str.h" #include "myc_str.h"
typedef enum { NORMAL, SPY, CLIENT } Mode; typedef enum { NORMAL, SPY, CLIENT, NMODE } Mode;
#define MAXMSG 256 #define MAXMSG 256
static char *host; static char *host;
static char instr[32]; static char instr[32];
static char savedHost[128];
static int sock[2]; /* main socket and spy socket */ static int sock[2]; /* main socket and spy socket */
static int level=3; static int level=0; /* undefined level */
static int deflevel=0; static int deflevel=0;
static int remember=1; static int remember=1;
static int hidecom=1; static int hidecom=1;
@ -33,46 +34,88 @@ static char msg0[MAXMSG];
static char msg1[MAXMSG]; /* contains messages for main and spy */ static char msg1[MAXMSG]; /* contains messages for main and spy */
static int pos0 = 0; static int pos0 = 0;
static int pos1 = 0; static int pos1 = 0;
static char *prefix[3]={"| ", "* ", ""}; static char *prefix[NMODE+1]={"", "| ", "# ", ""};
static int newline[NMODE+1]={1,1,1,1};
static char *sim="";
static char *pw=NULL;
static char *us="";
void Put(Mode mode, char *text) {
char chr;
while (*text) {
if (newline[mode]) {
fputs("\033[0m", stdout);
fputs(prefix[mode], stdout);
newline[mode] = 0;
if (strncmp(text, "ERROR:", 6) == 0) {
fputs("\033[31;1m", stdout);
}
}
chr = *text; text++;
fputc(chr, stdout);
if (chr == '\r' || chr == '\n') {
newline[mode] = 1;
}
}
fflush(stdout);
}
void PutC(char *text) {
Put(CLIENT, text);
}
void PutClear(void) {
Put(NMODE, "");
}
void UsageNote(void) {
PutC(" (for usage type: six help)\n");
}
void Usage(int cmds_only) { void Usage(int cmds_only) {
if (!cmds_only) { if (!cmds_only) {
printf(" \n"); PutC(
printf(" six commandline options:\n"); " \n"
printf(" - login as spy\n"); " six commandline options:\n"
printf(" + login as manager\n"); " -x login as spy\n"
printf(" 0 login as user\n"); " -u login as user\n"
printf(" -- set spy as default\n"); " -m login as manager\n"
printf(" 00 set user as default\n"); " help show this help text\n"
printf(" ++ set manager as default\n"); " -a or a ask always for username/password, forget passwords\n"
printf(" help show this help text\n"); " -c or c use background color instead of # and |\n"
printf(" -a or a ask always for username/password, forget passwords\n"); " -s or s simulation mode (on some instruments)\n"
printf(" -s or s simulation mode (on some instruments)\n"); " \"host\" connect to a SICServer on a different host\n"
printf(" -h \"host\" connect to a SICServer on a different host\n"); " -p \"port\" connect to a SICServer on a different port\n"
printf(" -p \"port\" connect to a SICServer on a different port\n"); " -n do only a minimal login (no check of instrument)\n"
printf(" -n do only a miminal login (no check of instrument)\n"); " no option login with default privilege\n"
printf(" no option login with default privilege\n"); );
} }
printf("\n"); PutC(
printf(" Special commands treated by six (these are no SICS commands!)\n"); "\n"
printf("\n"); " Special commands treated by six (these are no SICS commands!)\n"
printf(" quit exit six\n"); "\n"
printf(" exit exit six\n"); " quit exit six\n"
printf(" stop interrupt SICS\n"); " exit exit six\n"
printf(" six help show this help text\n"); " stop interrupt SICS\n"
printf(" + increase privilege\n"); " six help show this help text\n"
printf(" - decrease privilege\n"); " six def stop define a key for stop command\n"
printf("\n"); " six save save stop key and connection (host/port/access)\n"
printf(" The SICS status is shown, if it is not 'Eager to execute commands'.\n"); " + increase privilege\n"
printf(" A shown status does not prohibit to enter commands.\n"); " - decrease privilege\n"
printf("\n"); "\n"
printf(" When SICS is busy, an asterisk '*' is shown at the left of the line.\n"); " The SICS status is shown, if it is not 'Eager to execute commands'.\n"
printf(" You may then enter more commands, but only under Spy privilege.\n"); " A shown status does not prohibit to enter commands.\n"
printf(" All communication to and from SICS is prefixed with '|' or '*',\n"); "\n"
printf(" messages from the six client have no prefix.\n"); " When SICS is busy, an vertical bar '|' is shown at the left of the line.\n"
printf("\n"); " You may then enter more commands, but only under Spy privilege.\n"
printf(" Markus Zolliker, Mar. 2005\n"); " All messages from the six client are prefixed with a hash symbol '#'.\n"
printf("\n"); "\n"
" Markus Zolliker, Mar. 2005\n"
"\n"
);
Put(-1, ""); /* clear colors */
} }
int CocCreateSockAdr( int CocCreateSockAdr(
@ -115,9 +158,8 @@ char* readWrite(int tmo, int skip, char *find) {
char *msg, *p, *q; char *msg, *p, *q;
char chr; char chr;
static char result[MAXMSG]; static char result[MAXMSG];
int match; int hide;
static int laststat=0, laststatlen=0; static int laststat=0, laststatlen=0;
static int newline[2]={1,1};
/* /*
* read from registered sockets 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 TRANSACTIONFINISHED message * a timeout occurred (tmo [msec]), or a TRANSACTIONFINISHED message
@ -160,25 +202,28 @@ char* readWrite(int tmo, int skip, char *find) {
idx = (socket == sock[1]); idx = (socket == sock[1]);
ERR_SI(lbuf=recv(socket, &chr, 1, 0)); ERR_SI(lbuf=recv(socket, &chr, 1, 0));
} }
if (idx) { hide=0;
if (idx) { /* spy mode */
msg1[pos1] = '\0'; msg1[pos1] = '\0';
pos1 = 0; pos1 = 0;
msg = msg1; msg = msg1;
if (!busy[1]) { /* skip spy messages if not busy (they will be doubled) */
hide = 1;
}
} else { } else {
msg0[pos0] = '\0'; msg0[pos0] = '\0';
pos0 = 0; pos0 = 0;
msg = msg0; msg = msg0;
} }
/* printf("[%s]\n", msg); */ /* printf("[%s]\n", msg); */
match=0;
if (strncmp(msg, "TRANSACTIONSTART", 16) == 0) { if (strncmp(msg, "TRANSACTIONSTART", 16) == 0) {
busy[idx] = 1; busy[idx] = 1;
match = 1; hide = 1;
} else if (strncmp(msg, "TRANSACTIONFINISHED", 19) == 0) { } else if (strncmp(msg, "TRANSACTIONFINISHED", 19) == 0) {
busy[idx] = 0; busy[idx] = 0;
if (skip != 2) go = 1; if (skip != 2) go = 1;
shortTmo = 1; shortTmo = 1;
match = 1; hide = 1;
} else if (idx == 0 && strncmp(msg, "status = ", 9) == 0) { } else if (idx == 0 && strncmp(msg, "status = ", 9) == 0) {
strcpy(status, msg+9); strcpy(status, msg+9);
l = strlen(msg); l = strlen(msg);
@ -187,7 +232,7 @@ char* readWrite(int tmo, int skip, char *find) {
laststat = status[0]; laststat = status[0];
laststatlen = l; laststatlen = l;
} }
match = 1; hide = 1;
} }
if (idx == 0 && find != NULL) { if (idx == 0 && find != NULL) {
p=strstr(msg, find); p=strstr(msg, find);
@ -200,28 +245,17 @@ char* readWrite(int tmo, int skip, char *find) {
} else { } else {
str_copy(result, "1"); str_copy(result, "1");
} }
match=1; hide=1;
if (skip == 2) go = 1; if (skip == 2) go = 1;
} }
} }
if (hidecom && (skip || match)) { /* skip text */ if (hidecom && (skip || hide)) { /* skip text */
n+=strlen(msg); n+=strlen(msg);
} else { } else {
term_clear(); term_clear();
if (newline[idx]) { Put(idx, msg);
fputs(prefix[idx], stdout); if (chr == '\n') {
newline[idx]=0; Put(idx, "\033[K\n");
}
if (strncmp(msg, "ERROR:", 6)==0) {
fputs("\033[1;31m", stdout); /* red bold */
fputs(msg, stdout);
fputs("\033[0m", stdout); /* clear colors */
} else {
fputs(msg, stdout);
}
if (chr=='\n') {
newline[idx]=1;
fputs("\n", stdout);
} }
} }
if (go) break; if (go) break;
@ -235,15 +269,10 @@ char* readWrite(int tmo, int skip, char *find) {
} }
void PrintCmd(char *buf, int mode) { void PrintCmd(char *buf, int mode) {
Put(mode, "\r\033[K\n");
fputs("\r", stdout); /* clear command line */ Put(mode, "\033[1;34m>> ");
fputs(prefix[mode], stdout); Put(mode, buf);
fputs("\033[K\n", stdout); /* empty line */ Put(mode, "\033[K\033[0m\n");
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 sendCmd(int socket, char *cmd) {
@ -290,26 +319,20 @@ void putscrambled(char *buf, FILE *fil) {
} }
int setrights(int gotolevel) { int setrights(int gotolevel) {
char user[32]; char *p;
char pswd[32];
char *pw, *us, *p;
char prefhead[128], buf[128]; char prefhead[128], buf[128];
FILE *fil; FILE *fil;
int ask; int ask;
static char user[32]="";
static char pswd[32]="";
int savepw;
us=""; if (pw == NULL) pw=pswd;
user[0]='\0'; str_copy(prefhead, ".six.");
pw=pswd;
pswd[0]='\0';
p=getenv("HOME");
if (p != NULL) str_copy(prefhead, p);
str_append(prefhead, "/.six.");
str_append(prefhead, instr); str_append(prefhead, instr);
str_append(prefhead, ".");
if (0==strcmp(host, "0") && remember) { if (remember) {
fil=term_open_pref(prefhead, "r"); fil=term_open_pref(0, prefhead, "r");
if (fil != NULL) { if (fil != NULL) {
term_fgets(buf, sizeof(buf), fil); term_fgets(buf, sizeof(buf), fil);
getscrambled(user1, sizeof(user1), fil); getscrambled(user1, sizeof(user1), fil);
@ -325,99 +348,107 @@ int setrights(int gotolevel) {
} else { } else {
deflevel=2; deflevel=2;
} }
if (gotolevel==0) gotolevel=deflevel; if (gotolevel == 0) gotolevel = deflevel;
if (gotolevel==1) { if (deflevel == 0) deflevel = 3;
if (user1[0]=='\0') { if (level != gotolevel) {
str_copy(user1, "lnsmanager"); if (gotolevel==1) {
} if (user1[0]=='\0') {
us=user1; str_copy(user1, "lnsmanager");
pw=pswd1;
} else if (gotolevel==2) {
if (user2[0]=='\0') {
str_copy(user2, instr);
str_lowcase(user2, user2);
str_append(user2, "user");
}
us=user2;
pw=pswd2;
} else if (gotolevel==3) {
us="Spy";
pw="007";
}
ask=1;
if (us[0]!='\0' && pw[0]!='\0' && remember) {
sprintf(buf, "config rights %s %s", us, pw);
ERR_I(sendCmd(sock[0], buf));
ERR_P(p=readWrite(12000,0,"Acknowledged"));
if (*p=='\0') {
if (0==strcmp(us, user1)) {
user1[0]='\0';
pswd1[0]='\0';
} }
if (0==strcmp(us, user2)) { us=user1;
user2[0]='\0'; pw=pswd1;
pswd2[0]='\0'; } else if (gotolevel==2) {
if (user2[0]=='\0') {
str_copy(user2, instr);
str_lowcase(user2, user2);
str_append(user2, "user");
} }
} else { us=user2;
ask=0; pw=pswd2;
pw=NULL; } else if (gotolevel==3) {
us="Spy";
pw="007";
} }
} ask=1;
if (ask) { savepw = 1;
printf("SICS username"); if (us[0]!='\0' && pw[0]!='\0' && remember) {
if (us[0]!='\0') { sprintf(buf, "config rights %s %s", us, pw);
printf(" [%s]", us);
}
printf(": ");
term_fgets(user, sizeof(user), stdin);
if (0==strcmp(user, "quit")) return 1;
if (0==strcmp(user, "exit")) return 1;
if (user[0]=='\0') {
str_copy(user, us);
}
printf("password: ");
term_fgets(pswd, sizeof(pswd), stdin);
if (0==strcmp(pswd, "quit")) return 1;
if (0==strcmp(pswd, "exit")) return 1;
if (pswd[0]!='\0') {
sprintf(buf, "config rights %s %s", user, pswd);
ERR_I(sendCmd(sock[0], buf)); ERR_I(sendCmd(sock[0], buf));
ERR_P(p=readWrite(12000,0,"Acknowledged")); ERR_P(p=readWrite(12000,0,"Acknowledged"));
if (*p=='\0') { /* no success */ if (*p=='\0') {
gotolevel=3; if (0==strcmp(us, user1)) {
if (0==strcmp(user, user1)) {
user1[0]='\0'; user1[0]='\0';
pswd1[0]='\0'; pswd1[0]='\0';
} }
if (0==strcmp(user, user2)) { if (0==strcmp(us, user2)) {
user2[0]='\0'; user2[0]='\0';
pswd2[0]='\0'; pswd2[0]='\0';
} }
pw=NULL; } else {
} else { /* success */ ask=0;
if (0==strcmp(user, user1)) { savepw = 0;
str_copy(pswd1, pswd);
}
if (0==strcmp(user, user2)) {
str_copy(pswd2, pswd);
}
us=user;
pw=pswd;
} }
} else { }
gotolevel=3; if (ask) {
PutC("SICS username");
if (us[0]!='\0') {
PutC(" [");
PutC(us);
PutC("]");
}
PutC(": ");
term_fgets(user, sizeof(user), stdin);
newline[CLIENT]=1;
if (0==strcmp(user, "quit")) return 1;
if (0==strcmp(user, "exit")) return 1;
if (user[0]=='\0') {
str_copy(user, us);
}
PutC("password: ");
term_fgets(pswd, sizeof(pswd), stdin);
newline[CLIENT]=1;
if (0==strcmp(pswd, "quit")) return 1;
if (0==strcmp(pswd, "exit")) return 1;
if (pswd[0]!='\0') {
sprintf(buf, "config rights %s %s", user, pswd);
ERR_I(sendCmd(sock[0], buf));
ERR_P(p=readWrite(12000,0,"Acknowledged"));
if (*p=='\0') { /* no success */
gotolevel=3;
if (0==strcmp(user, user1)) {
user1[0]='\0';
pswd1[0]='\0';
}
if (0==strcmp(user, user2)) {
user2[0]='\0';
pswd2[0]='\0';
}
pw=NULL;
} else { /* success */
if (0==strcmp(user, user1)) {
str_copy(pswd1, pswd);
}
if (0==strcmp(user, user2)) {
str_copy(pswd2, pswd);
}
us=user;
pw=pswd;
}
} else {
gotolevel=3;
}
}
level=3;
ERR_I(sendCmd(sock[0], "config list"));
ERR_P(p=readWrite(12000,1,"UserRights = "));
if (*p!='\0') {
level=*p-'0';
}
if (level==3) {
us="Spy";
} }
} }
level=gotolevel; if (pw!=NULL && savepw) {
ERR_I(sendCmd(sock[0], "config list"));
ERR_P(p=readWrite(12000,1,"UserRights = "));
if (*p!='\0') {
level=*p-'0';
}
if (level==3) {
us="Spy";
}
if (pw!=NULL) {
if (level==1) { if (level==1) {
str_copy(user1, us); str_copy(user1, us);
str_copy(pswd1, pw); str_copy(pswd1, pw);
@ -434,17 +465,92 @@ int setrights(int gotolevel) {
} }
} }
} }
if (0==strcmp(host, "0")) { fil=term_open_pref(0, prefhead, "w");
fil=term_open_pref(prefhead, "w"); if (fil!=NULL) {
if (fil!=NULL) { fprintf(fil, "%d\n", deflevel);
fprintf(fil, "%d\n", deflevel); if (remember) {
if (remember) { putscrambled(user1, fil);
putscrambled(user1, fil); putscrambled(pswd1, fil);
putscrambled(pswd1, fil); putscrambled(user2, fil);
putscrambled(user2, fil); putscrambled(pswd2, fil);
putscrambled(pswd2, fil); }
fclose(fil);
}
return 0;
OnError: return -1;
}
int SavePrefs(void) {
struct hostent *ent;
char prefhead[128], name[128];
FILE *fil;
int iret;
char *p;
ERR_SI(gethostname(name, sizeof name)); /* get ip name of this host */
str_copy(prefhead, ".six_");
str_append(prefhead, sim);
str_append(prefhead, name);
ERR_SP(ent = gethostbyname(name)); /* get my ip name */
snprintf(name, sizeof name, "%s", ent->h_name);
ERR_SP(fil=term_open_pref(0, prefhead, "w"));
p = getenv("Instrument");
if (p != NULL) {
ent = gethostbyname(p); /* get ip name of "Instrument" translation */
if (ent !=NULL && strcmp(ent->h_name, name) == 0) { /* we are on an instrument computer */
ent = gethostbyname(instr); /* get host name of SICServer */
iret = (ent != NULL && strcmp(ent->h_name, name) != 0);
strcpy(savedHost, "localhost");
}
}
fprintf(fil, "host %s\n", savedHost);
fprintf(fil, "port %d\n", port);
fprintf(fil, "keys\n");
term_save_keys(fil, PutC);
fclose(fil);
return 0;
OnError: return -1;
}
int LoadPrefs(void) {
struct hostent *ent;
char prefhead[128], name[128], line[128];
FILE *fil;
ERR_SI(gethostname(name, sizeof name)); /* get ip name of this host */
str_copy(prefhead, ".six_");
str_append(prefhead, name);
fil=term_open_pref(0, prefhead, "r");
if (fil) {
savedHost[0]='\0';
while (term_fgets(line, sizeof(line), fil)) {
if (0 == strncmp(line, "host ", 5)) {
snprintf(savedHost, sizeof savedHost, "%s", line+5);
} else if (0 == strncmp(line, "port ", 5)) {
if (port < 0) {
port = atoi(line+5);
}
} else if (0 == strcmp(line, "keys")) {
term_load_keys(fil, PutC);
} }
fclose(fil); }
fclose(fil);
if (host == NULL && savedHost[0] != '\0') {
host = savedHost;
}
}
if (host == NULL) {
strcpy(savedHost, "localhost");
host = savedHost;
} else { /* standardize host name */
ent = gethostbyname(host);
if (ent) {
snprintf(savedHost, sizeof savedHost, "%s", ent->h_name);
host = savedHost;
} else {
savedHost[0]='\0'; /* do not save an unconfirmed host */
} }
} }
return 0; return 0;
@ -463,7 +569,7 @@ int Connect(void) {
ERR_SI(send(sock, "Spy 007\n", 8, 0)); ERR_SI(send(sock, "Spy 007\n", 8, 0));
ERR_P(p=readWrite(12000,2,"Login O")); ERR_P(p=readWrite(12000,2,"Login O"));
if (*p!='K') { /* looking for the 'K' of 'Login OK' */ if (*p!='K') { /* looking for the 'K' of 'Login OK' */
printf("rejected\n"); PutC("rejected\n");
return 0; return 0;
} }
return sock; return sock;
@ -471,39 +577,41 @@ int Connect(void) {
} }
int main (int argc, char *argv[]) { int main (int argc, char *argv[]) {
struct hostent *ent;
int iret, pos; int iret, pos;
fd_set mask; fd_set mask;
int i, j, 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];
char stdPrompt[128], prompt[256]; char stdPrompt[128]="", prompt[256]="";
char *sim="";
char *p; char *p;
char *bar; char *bar;
char *pnam[4]={"0", "MANAGER", "user", "spy"}; char *pnam[4]={"0", "MANAGER", "user", "spy"};
char hostArg[128];
printf("---------------------------------------------------\n"); atexit(term_off);
printf("six, a fast SICS commandline client (doc: six help)\n"); port=-1;
printf("---------------------------------------------------\n");
port=2911;
sicslogin=1; sicslogin=1;
j=0;
deflevel=0; deflevel=0;
gotolevel=0; gotolevel=0;
host="0"; /* localhost by default */ host=NULL;
sock[1]=0; /* do not yet connect 2nd connection */ 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=2927; sim="-sim"; sim="sim-";
} 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], "-c") || 0==strcmp(argv[i], "c")) {
prefix[NORMAL] = "\033[47m";
prefix[SPY] = "\033[43m";
prefix[CLIENT] = "\033[0m";
} else if (0==strcmp(argv[i], "-d") || 0==strcmp(argv[i], "d")) { } else if (0==strcmp(argv[i], "-d") || 0==strcmp(argv[i], "d")) {
hidecom=0; hidecom=0;
} else if (0==strcmp(argv[i], "+")) { } else if (0==strcmp(argv[i], "+") || 0==strcmp(argv[i], "-m")) {
gotolevel=1; gotolevel=1;
} else if (0==strcmp(argv[i], "-")) { } else if (0==strcmp(argv[i], "-") || 0==strcmp(argv[i], "-x")) {
gotolevel=3; gotolevel=3;
} else if (0==strcmp(argv[i], "0")) { } else if (0==strcmp(argv[i], "0") || 0==strcmp(argv[i], "-u")) {
gotolevel=2; gotolevel=2;
} else if (0==strcmp(argv[i], "++")) { } else if (0==strcmp(argv[i], "++")) {
gotolevel=1; gotolevel=1;
@ -520,38 +628,58 @@ int main (int argc, char *argv[]) {
} else if (0==strcmp(argv[i], "-h")) { } else if (0==strcmp(argv[i], "-h")) {
i++; i++;
if (i>=argc) { if (i>=argc) {
printf("missing host\n"); PutC("missing host");
Usage(0); return 0; UsageNote(); return 0;
} }
host=argv[i]; host = argv[i];
} else if (0==strcmp(argv[i], "-p")) { } else if (0==strcmp(argv[i], "-p")) {
i++; i++;
if (i>=argc) { if (i>=argc) {
printf("missing port\n"); PutC("missing port");
Usage(0); return 0; UsageNote(); return 0;
} }
port=atoi(argv[i]); port=atoi(argv[i]);
if (port == 0) { if (port == 0) {
printf("illegal port\n"); PutC("illegal port");
Usage(0); return 0; UsageNote(); return 0;
} }
} else if (0==strcmp(argv[i], "-n")) { } else if (0==strcmp(argv[i], "-n")) {
sicslogin=0; sicslogin=0;
} else { } else {
if (strlen(argv[i])>=32) { if (strlen(argv[i])>=32) {
printf("argument too long\n"); PutC("argument too long");
Usage(0); return 0; UsageNote(); return 0;
} else if (argv[i][0]!='-') { } else if (argv[i][0] == '-') {
if (j==0) { PutC("unknown option: ");
printf("syntax has changed, username can not be given as argument\n"); PutC(argv[i]);
Usage(0); j=1; UsageNote(); return 0;
}
} else { } else {
printf("unknown option: %s\n", argv[i]); host = argv[i];
Usage(0); return 0;
} }
} }
} }
if (host) {
ent = gethostbyname(host);
if (!ent) {
PutC("unknown host: ");
PutC(host);
PutC("\n");
return 0;
}
snprintf(hostArg, sizeof hostArg, "%s", ent->h_name);
host = hostArg;
}
PutC("---------------------------------------------------\n");
PutC("six, a fast SICS commandline client (doc: six help)\n");
PutC("---------------------------------------------------\n");
LoadPrefs();
if (port == -1) {
if (*sim == '\0') {
port = 2911;
} else {
port = 2927;
}
}
ERR_I(sock[0]=Connect()); ERR_I(sock[0]=Connect());
if (sock[0] == 0) return 0; if (sock[0] == 0) return 0;
if (sicslogin) { if (sicslogin) {
@ -560,19 +688,23 @@ int main (int argc, char *argv[]) {
ERR_P(p=readWrite(12000,0,"Instrument = ")); ERR_P(p=readWrite(12000,0,"Instrument = "));
str_copy(instr, p); str_copy(instr, p);
if (*instr=='\0') { if (*instr=='\0') {
printf("can not detect instrument\n"); PutC("can not detect instrument\n");
return 0; return 0;
} }
str_lowcase(instr,instr);
p=strchr(instr,' '); p=strchr(instr,' ');
if (p!=NULL) *p='\0'; if (p!=NULL) *p='\0';
if (0==strcmp(instr,"SANS-II")) { if (0==strcmp(instr,"SANS-II")) {
str_copy(instr, "SANS2"); str_copy(instr, "SANS2");
} }
ERR_I(i=setrights(gotolevel)); ERR_I(i=setrights(gotolevel));
printf("\rlogged in to SICS as %s on %s\n", pnam[level], instr); PutC("\rlogged in to SICS as ");
PutC(pnam[level]);
PutC(" on ");
PutC(instr);
PutC("\n");
str_lowcase(ilow,instr); sprintf(stdPrompt, "six[%s] ", instr);
sprintf(stdPrompt, "six[%s] ", ilow);
ERR_I(sendCmd(sock[0], "status interest")); ERR_I(sendCmd(sock[0], "status interest"));
ERR_P(readWrite(12000,0,"OK")); ERR_P(readWrite(12000,0,"OK"));
@ -596,7 +728,7 @@ int main (int argc, char *argv[]) {
if (busy[0]) { if (busy[0]) {
bar = prefix[SPY]; bar = prefix[SPY];
} else { } else {
bar = ""; bar = prefix[NORMAL];
} }
if (status[0] == 'E') { /* Eager to ... */ if (status[0] == 'E') { /* Eager to ... */
sprintf(prompt, "%s%s", bar, stdPrompt); sprintf(prompt, "%s%s", bar, stdPrompt);
@ -612,31 +744,42 @@ int main (int argc, char *argv[]) {
PrintCmd(buf, CLIENT); PrintCmd(buf, CLIENT);
break; break;
} }
p="";
if (0==strcmp(lbuf,"stop")) { if (0==strcmp(lbuf,"stop")) {
PrintCmd(buf, CLIENT); PrintCmd(buf, CLIENT);
ERR_SI(send(sock[1], "INT1712 3\n", 10, 0)); ERR_SI(send(sock[0], "INT1712 3\n", 10, 0));
buf[0]='\0'; buf[0]='\0';
if (level != 1 && !busy[0]) {
PutC("nothing to stop\n");
}
} 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); PrintCmd(buf, CLIENT);
j=level; if (level == 0) {
if (buf[0]=='-') { gotolevel = 3;
if (level<3) level++;
} else { } else {
if (level>1) level--; gotolevel = level;
}
if (buf[0]=='-') {
if (gotolevel<3) gotolevel++;
} else {
if (gotolevel>1) gotolevel--;
} }
if (strlen(buf)==2) { if (strlen(buf)==2) {
deflevel=level; deflevel=gotolevel;
} }
term_off(); term_off();
ERR_I(i=setrights(level)); /* level might be changed */ j = level;
ERR_I(i=setrights(gotolevel)); /* level might be changed */
if (j!=level) { if (j!=level) {
printf("\rswitched to %s privilege\n", pnam[level]); PutC("\rswitched to ");
} else { } else {
printf("\rremain at %s privilege\n", pnam[level]); PutC("\rremain at ");
} }
PutC(pnam[level]);
PutC(" privilege\n");
buf[0]='\0'; buf[0]='\0';
} else if (0==strcmp(buf, "help six")) { } else if (0==strcmp(buf, "help six")) {
PrintCmd(buf, CLIENT); PrintCmd(buf, CLIENT);
@ -646,19 +789,65 @@ int main (int argc, char *argv[]) {
PrintCmd(buf, CLIENT); PrintCmd(buf, CLIENT);
Usage(0); Usage(0);
buf[0]='\0'; buf[0]='\0';
} else if (0==strncmp(buf, "six def ", 8)) {
PrintCmd(buf, CLIENT);
term_define_key(buf+8, 0);
buf[0]='\0';
} else if (0==strcmp(buf, "six def")) {
PrintCmd(buf, CLIENT);
term_define_key("", 0);
buf[0]='\0';
} else if (0==strcmp(buf, "six save")) {
PrintCmd(buf, CLIENT);
PutC("defaults for next calls to six:\n");
ERR_I(i=SavePrefs());
if (level == 0) {
gotolevel = 3;
} else {
gotolevel = level;
}
PutC(" connect to ");
if (i == 1) {
PutC(getenv("Instrument"));
PutC(" (on ");
PutC(savedHost);
PutC(", this default can not be changed)\n");
} else {
ERR_I(setrights(gotolevel));
PutC(instr);
PutC(" (");
PutC(savedHost);
if (port != 2911 && port !=2927) {
snprintf(buf, sizeof buf, ":%d", port);
PutC(buf);
}
PutC(")\n login as ");
PutC(us);
PutC(" with ");
PutC(pnam[level]);
PutC(" privilege\n");
}
buf[0]='\0';
} else if (0==strncmp(buf, "six ", 4)) {
PrintCmd(buf, CLIENT);
PutC("ERROR: unknown command\n");
buf[0]='\0';
} else if (buf[0] == '\0') { /* empty command */
PrintCmd(buf, CLIENT);
ERR_P(p=readWrite(200,0,NULL)); /* just wait a little */
} }
if (buf[0] != 0) { if (buf[0] != '\0') {
if (busy[0]) { if (busy[0]) {
if (!sock[1]) { if (!sock[1]) {
ERR_I(sock[1] = Connect()); ERR_I(sock[1] = Connect());
if (!sock[1]) { if (!sock[1]) {
printf("spy connection rejected\n"); PutC("spy connection rejected\n");
} }
} }
if (busy[1]) { if (busy[1]) {
/* print in red bold */ /* print in red bold */
PrintCmd(buf, CLIENT); PrintCmd(buf, CLIENT);
fputs("\033[1;31mBUSY (already 2 commands pending)\033[0m\n", stdout); PutC("\033[1;31mBUSY (already 2 commands pending)\033[0m\n");
buf[0]='\0'; buf[0]='\0';
} else if (sock[1]) { } else if (sock[1]) {
PrintCmd(buf, SPY); PrintCmd(buf, SPY);
@ -674,26 +863,33 @@ int main (int argc, char *argv[]) {
} }
if (buf[0] != '\0') { if (buf[0] != '\0') {
ERR_P(p=readWrite(500,0,NULL)); ERR_P(p=readWrite(500,0,NULL));
} else {
p="";
} }
buf[0]='\0'; buf[0]='\0';
pos=0; pos=0;
} else { /* something arrived from sockets */ } else if (iret>0) { /* something arrived from sockets */
assert(iret == sock[0] || iret == sock[1]); assert(iret == sock[0] || iret == sock[1]);
ERR_P(p=readWrite(500,0,NULL)); ERR_P(p=readWrite(500,0,NULL));
} }
if (strcmp(p, "0") == 0) { if (strcmp(p, "0") == 0) {
term_clear(); term_clear();
printf("\nconnection lost"); PutC("\nconnection lost\n");
break; goto SaveHist;
} }
} }
printf("\nexit %s\n", prompt); if (savehist) term_save_hist(1); /* save history without last line */
term_save_hist(1); /* save history without last line */ goto Bye;
return 0;
OnError: OnError:
if (savehist) term_save_hist(0); /* save history with last line */
ErrShow("end"); ErrShow("end");
SaveHist:
if (savehist) term_save_hist(0); /* save history with last line */
Bye:
term_off();
if (stdPrompt[0]) {
PutC("\nexit ");
PutC(stdPrompt);
PutC("\n");
PutClear(); /* clear colors */
}
return 0; return 0;
} }