visible difference for sics and sea

This commit is contained in:
zolliker
2005-08-31 05:58:57 +00:00
parent d8c7a1a948
commit b6bcc4993b

View File

@ -1,3 +1,10 @@
/* a simple commandline client for SICS and sea
Markus Zolliker
Mar. 2003 first version
Aug. 2005 visible difference between client for SICS and sea
*/
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -15,6 +22,8 @@ typedef enum { NORMAL, SPY, CLIENT, NMODE } Mode;
#define MAXMSG 256 #define MAXMSG 256
static char *clcname="six", *servername="SICS";
static int clclen=3;
static char *host; static char *host;
static char instr[32]; static char instr[32];
static char savedHost[128]; static char savedHost[128];
@ -71,14 +80,17 @@ void PutClear(void) {
} }
void UsageNote(void) { void UsageNote(void) {
PutC(" (for usage type: six help)\n"); PutC(" (for usage type: ");
PutC(clcname);
PutC(" help)\n");
} }
void Usage(int cmds_only) { void Usage(int cmds_only) {
if (!cmds_only) { if (!cmds_only) {
PutC(" \n ");
PutC(clcname);
PutC( PutC(
" \n" " commandline options:\n"
" six commandline options:\n"
" -x login as spy\n" " -x login as spy\n"
" -u login as user\n" " -u login as user\n"
" -m login as manager\n" " -m login as manager\n"
@ -86,33 +98,39 @@ void Usage(int cmds_only) {
" -a or a ask always for username/password, forget passwords\n" " -a or a ask always for username/password, forget passwords\n"
" -c or c use background color instead of # and |\n" " -c or c use background color instead of # and |\n"
" -s or s simulation mode (on some instruments)\n" " -s or s simulation mode (on some instruments)\n"
" \"host\" connect to a SICServer on a different host\n" " \"host\" connect to a server on a different host\n"
" -p \"port\" connect to a SICServer on a different port\n" " -p \"port\" connect to a server on a different port\n"
" -n do only a minimal login (no check of instrument)\n" " -n do only a minimal login (no check of instrument)\n"
" no option login with default privilege\n" " no option login with default privilege\n"
); );
} }
PutC( PutC(
"\n" "\n"
" Special commands treated by six (these are no SICS commands!)\n" " Special commands treated by ");
PutC(clcname); PutC(" (these are no "); PutC(servername); PutC(" commands!)\n"
"\n" "\n"
" quit exit six\n" " quit return to unix prompt\n"
" exit exit six\n" " exit return to unix prompt\n"
" stop interrupt SICS\n" " stop interrupt driving\n"
" six help show this help text\n" " "); PutC(clcname);
" six def stop define a key for stop command\n" PutC(" help show this help text\n"
" six save save stop key and connection (host/port/access)\n" " "); PutC(clcname);
PutC(" def stop define a key for stop command\n"
" "); PutC(clcname);
PutC(" save save stop key and connection (host/port/access)\n"
" + increase privilege\n" " + increase privilege\n"
" - decrease privilege\n" " - decrease privilege\n"
"\n" "\n"
" The SICS status is shown, if it is not 'Eager to execute commands'.\n" " The "); PutC(servername);
PutC(" status is shown, if it is not 'Eager to execute commands'.\n"
" A shown status does not prohibit to enter commands.\n" " A shown status does not prohibit to enter commands.\n"
"\n" "\n"
" When SICS is busy, an vertical bar '|' is shown at the left of the line.\n" " When "); PutC(servername);
PutC(" is busy, a vertical bar '|' is shown at the left of the line.\n"
" You may then enter more commands, but only under Spy privilege.\n" " You may then enter more commands, but only under Spy privilege.\n"
" All messages from the six client are prefixed with a hash symbol '#'.\n" " All messages from the client are prefixed with a hash symbol '#'.\n"
"\n" "\n"
" Markus Zolliker, Mar. 2005\n" " Markus Zolliker, Aug. 2005\n"
"\n" "\n"
); );
Put(-1, ""); /* clear colors */ Put(-1, ""); /* clear colors */
@ -328,7 +346,9 @@ int setrights(int gotolevel) {
int savepw; int savepw;
if (pw == NULL) pw=pswd; if (pw == NULL) pw=pswd;
str_copy(prefhead, ".six."); str_copy(prefhead, ".");
str_append(prefhead, clcname);
str_append(prefhead, ".");
str_append(prefhead, instr); str_append(prefhead, instr);
if (remember) { if (remember) {
@ -353,15 +373,25 @@ int setrights(int gotolevel) {
if (level != gotolevel) { if (level != gotolevel) {
if (gotolevel==1) { if (gotolevel==1) {
if (user1[0]=='\0') { if (user1[0]=='\0') {
str_copy(user1, "lnsmanager"); if (strcmp(clcname, "six") == 0) {
str_copy(user1, "lnsmanager");
} else {
str_copy(user2, servername);
str_append(user2, "manager");
}
} }
us=user1; us=user1;
pw=pswd1; pw=pswd1;
} else if (gotolevel==2) { } else if (gotolevel==2) {
if (user2[0]=='\0') { if (user2[0]=='\0') {
str_copy(user2, instr); if (strcmp(clcname, "six") == 0) {
str_lowcase(user2, user2); str_copy(user2, instr);
str_append(user2, "user"); str_lowcase(user2, user2);
str_append(user2, "user");
} else {
str_copy(user2, servername);
str_append(user2, "user");
}
} }
us=user2; us=user2;
pw=pswd2; pw=pswd2;
@ -390,7 +420,8 @@ int setrights(int gotolevel) {
} }
} }
if (ask) { if (ask) {
PutC("SICS username"); PutC(servername);
PutC(" username");
if (us[0]!='\0') { if (us[0]!='\0') {
PutC(" ["); PutC(" [");
PutC(us); PutC(us);
@ -488,7 +519,9 @@ int SavePrefs(void) {
char *p; char *p;
ERR_SI(gethostname(name, sizeof name)); /* get ip name of this host */ ERR_SI(gethostname(name, sizeof name)); /* get ip name of this host */
str_copy(prefhead, ".six_"); str_copy(prefhead, ".");
str_append(prefhead, clcname);
str_append(prefhead, "_");
str_append(prefhead, sim); str_append(prefhead, sim);
str_append(prefhead, name); str_append(prefhead, name);
@ -500,7 +533,7 @@ int SavePrefs(void) {
if (p != NULL) { if (p != NULL) {
ent = gethostbyname(p); /* get ip name of "Instrument" translation */ ent = gethostbyname(p); /* get ip name of "Instrument" translation */
if (ent !=NULL && strcmp(ent->h_name, name) == 0) { /* we are on an instrument computer */ if (ent !=NULL && strcmp(ent->h_name, name) == 0) { /* we are on an instrument computer */
ent = gethostbyname(instr); /* get host name of SICServer */ ent = gethostbyname(instr); /* get host name of server */
iret = (ent != NULL && strcmp(ent->h_name, name) != 0); iret = (ent != NULL && strcmp(ent->h_name, name) != 0);
strcpy(savedHost, "localhost"); strcpy(savedHost, "localhost");
} }
@ -520,7 +553,10 @@ int LoadPrefs(void) {
FILE *fil; FILE *fil;
ERR_SI(gethostname(name, sizeof name)); /* get ip name of this host */ ERR_SI(gethostname(name, sizeof name)); /* get ip name of this host */
str_copy(prefhead, ".six_"); str_copy(prefhead, ".");
str_append(prefhead, clcname);
str_append(prefhead, "_");
str_append(prefhead, sim);
str_append(prefhead, name); str_append(prefhead, name);
fil=term_open_pref(0, prefhead, "r"); fil=term_open_pref(0, prefhead, "r");
if (fil) { if (fil) {
@ -588,6 +624,7 @@ int main (int argc, char *argv[]) {
char *bar; char *bar;
char *pnam[4]={"0", "MANAGER", "user", "spy"}; char *pnam[4]={"0", "MANAGER", "user", "spy"};
char hostArg[128]; char hostArg[128];
char *subcmd;
atexit(term_off); atexit(term_off);
port=-1; port=-1;
@ -599,6 +636,10 @@ int main (int argc, char *argv[]) {
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")) {
sim="sim-"; sim="sim-";
} else if (0==strcmp(argv[i], "-sea") || 0==strcmp(argv[i], "sea")) {
clcname="seacmd";
servername="sea";
clclen=strlen(clcname);
} 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")) { } else if (0==strcmp(argv[i], "-c") || 0==strcmp(argv[i], "c")) {
@ -669,17 +710,25 @@ int main (int argc, char *argv[]) {
snprintf(hostArg, sizeof hostArg, "%s", ent->h_name); snprintf(hostArg, sizeof hostArg, "%s", ent->h_name);
host = hostArg; host = hostArg;
} }
PutC("---------------------------------------------------\n");
PutC("six, a fast SICS commandline client (doc: six help)\n");
PutC("---------------------------------------------------\n");
LoadPrefs(); LoadPrefs();
if (port == -1) {
if (*sim == '\0') { PutC( "---------------------------------------------------\n");
port = 2911; if (strcmp(clcname, "six") == 0) {
} else { PutC("six, a fast SICS commandline client (doc: six help)\n");
port = 2927; if (port == -1) {
if (*sim == '\0') {
port = 2911;
} else {
port = 2927;
}
}
} else {
PutC("seacmd, a sea commandline client (doc: seacmd help)\n");
if (port == -1) {
port = 22911;
} }
} }
PutC( "---------------------------------------------------\n");
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) {
@ -698,13 +747,13 @@ int main (int argc, char *argv[]) {
str_copy(instr, "SANS2"); str_copy(instr, "SANS2");
} }
ERR_I(i=setrights(gotolevel)); ERR_I(i=setrights(gotolevel));
PutC("\rlogged in to SICS as "); PutC("\rlogged in to "); PutC(servername); PutC(" as ");
PutC(pnam[level]); PutC(pnam[level]);
PutC(" on "); PutC(" on ");
PutC(instr); PutC(instr);
PutC("\n"); PutC("\n");
sprintf(stdPrompt, "six[%s] ", instr); sprintf(stdPrompt, "%s[%s] ", clcname, instr);
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"));
@ -713,7 +762,7 @@ int main (int argc, char *argv[]) {
ERR_P(readWrite(12000,0,NULL)); ERR_P(readWrite(12000,0,NULL));
} else { } else {
sprintf(stdPrompt, "six[%s] ", host); sprintf(stdPrompt, "%s[%s] ", clcname, host);
status[0]='E'; status[1]='\0'; status[0]='E'; status[1]='\0';
} }
@ -721,7 +770,7 @@ int main (int argc, char *argv[]) {
buf[0]='\0'; buf[0]='\0';
pos=0; pos=0;
term_read_hist("six"); term_read_hist(clcname);
savehist = 1; savehist = 1;
while (1) { while (1) {
@ -778,58 +827,65 @@ int main (int argc, char *argv[]) {
PutC(pnam[level]); PutC(pnam[level]);
PutC(" privilege\n"); PutC(" privilege\n");
buf[0]='\0'; buf[0]='\0';
} else if (0==strcmp(buf, "help six")) { } else if (0==strncmp(buf, "help ", 5) && 0==strcmp(buf+5, clcname)) {
PrintCmd(buf, CLIENT); PrintCmd(buf, CLIENT);
Usage(1); Usage(1);
buf[0]='\0'; buf[0]='\0';
} else if (0==strcmp(buf, "six help")) { } else if (0==strncmp(buf, clcname, clclen) && buf[clclen] == ' ') {
PrintCmd(buf, CLIENT); subcmd = buf + clclen;
Usage(0); while (*subcmd <= ' ' && *subcmd != 0) {
buf[0]='\0'; subcmd++;
} 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 (0==strcmp(subcmd, "help")) {
if (i == 1) { PrintCmd(buf, CLIENT);
PutC(getenv("Instrument")); Usage(0);
PutC(" (on "); buf[0]='\0';
PutC(savedHost); } else if (0==strncmp(subcmd, "def ", 4)) {
PutC(", this default can not be changed)\n"); PrintCmd(buf, CLIENT);
} else { term_define_key(subcmd+4, 0);
deflevel = gotolevel; buf[0]='\0';
ERR_I(setrights(gotolevel)); } else if (0==strcmp(subcmd, "def")) {
PutC(instr); PrintCmd(buf, CLIENT);
PutC(" ("); term_define_key("", 0);
PutC(savedHost); buf[0]='\0';
if (port != 2911 && port !=2927) { } else if (0==strcmp(subcmd, "save")) {
snprintf(buf, sizeof buf, ":%d", port); PrintCmd(buf, CLIENT);
PutC(buf); PutC("defaults for next calls to ");
PutC(clcname); PutC(":\n");
ERR_I(i=SavePrefs());
if (level == 0) {
gotolevel = 3;
} else {
gotolevel = level;
} }
PutC(")\n login as "); PutC(" connect to ");
PutC(us); if (i == 1) {
PutC(" with "); PutC(getenv("Instrument"));
PutC(pnam[level]); PutC(" (on ");
PutC(" privilege\n"); PutC(savedHost);
PutC(", this default can not be changed)\n");
} else {
deflevel = gotolevel;
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 {
PrintCmd(buf, CLIENT);
PutC("ERROR: unknown command\n");
buf[0]='\0';
} }
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 */ } else if (buf[0] == '\0') { /* empty command */
PrintCmd(buf, CLIENT); PrintCmd(buf, CLIENT);
ERR_P(p=readWrite(200,0,NULL)); /* just wait a little */ ERR_P(p=readWrite(200,0,NULL)); /* just wait a little */
@ -865,8 +921,9 @@ int main (int argc, char *argv[]) {
buf[0]='\0'; buf[0]='\0';
pos=0; pos=0;
} else if (iret>0) { /* something arrived from sockets */ } else if (iret>0) { /* something arrived from sockets */
assert(iret == sock[0] || iret == sock[1]); if (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();