📄 commands.c
字号:
env_varval, 1 },
#endif
{ "help", 0, env_help, 0 },
{ "?", "Print help information", env_help, 0 },
{ 0 },
};
static void
env_help()
{
struct envlist *c;
for (c = EnvList; c->name; c++) {
if (c->help) {
if (*c->help)
printf("%-15s %s\n", c->name, c->help);
else
printf("\n");
}
}
}
static struct envlist *
getenvcmd(name)
char *name;
{
return (struct envlist *)
genget(name, (char **) EnvList, sizeof(struct envlist));
}
env_cmd(argc, argv)
int argc;
char *argv[];
{
struct envlist *c;
if (argc < 2) {
fprintf(stderr,
"Need an argument to 'environ' command. 'environ ?' for help.\n");
return 0;
}
c = getenvcmd(argv[1]);
if (c == 0) {
fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
argv[1]);
return 0;
}
if (Ambiguous(c)) {
fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
argv[1]);
return 0;
}
if (c->narg + 2 != argc) {
fprintf(stderr,
"Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
c->narg < argc + 2 ? "only " : "",
c->narg, c->narg == 1 ? "" : "s", c->name);
return 0;
}
(*c->handler)(argv[2], argv[3]);
return 1;
}
struct env_lst {
struct env_lst *next; /* pointer to next structure */
struct env_lst *prev; /* pointer to previous structure */
unsigned char *var; /* pointer to variable name */
unsigned char *value; /* pointer to variable value */
int export; /* 1 -> export with default list of variables */
int welldefined; /* A well defined variable */
};
struct env_lst envlisthead;
struct env_lst *
env_find(var)
unsigned char *var;
{
register struct env_lst *ep;
for (ep = envlisthead.next; ep; ep = ep->next) {
if (strcmp((char *)ep->var, (char *)var) == 0)
return(ep);
}
return(NULL);
}
void
env_init()
{
extern char **environ;
register char **epp, *cp;
register struct env_lst *ep;
extern char *strchr();
for (epp = environ; *epp; epp++) {
if (cp = strchr(*epp, '=')) {
*cp = '\0';
ep = env_define((unsigned char *)*epp,
(unsigned char *)cp+1);
ep->export = 0;
*cp = '=';
}
}
/*
* Special case for DISPLAY variable. If it is ":0.0" or
* "unix:0.0", we have to get rid of "unix" and insert our
* hostname.
*/
if ((ep = env_find("DISPLAY"))
&& ((*ep->value == ':')
|| (strncmp((char *)ep->value, "unix:", 5) == 0))) {
char hbuf[256+1];
char *cp2 = strchr((char *)ep->value, ':');
gethostname(hbuf, 256);
hbuf[256] = '\0';
cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
sprintf((char *)cp, "%s%s", hbuf, cp2);
free(ep->value);
ep->value = (unsigned char *)cp;
}
/*
* If USER is not defined, but LOGNAME is, then add
* USER with the value from LOGNAME. By default, we
* don't export the USER variable.
*/
if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
env_define((unsigned char *)"USER", ep->value);
env_unexport((unsigned char *)"USER");
}
env_export((unsigned char *)"DISPLAY");
env_export((unsigned char *)"PRINTER");
}
struct env_lst *
env_define(var, value)
unsigned char *var, *value;
{
register struct env_lst *ep;
if (ep = env_find(var)) {
if (ep->var)
free(ep->var);
if (ep->value)
free(ep->value);
} else {
ep = (struct env_lst *)malloc(sizeof(struct env_lst));
ep->next = envlisthead.next;
envlisthead.next = ep;
ep->prev = &envlisthead;
if (ep->next)
ep->next->prev = ep;
}
ep->welldefined = opt_welldefined(var);
ep->export = 1;
ep->var = (unsigned char *)strdup((char *)var);
ep->value = (unsigned char *)strdup((char *)value);
return(ep);
}
void
env_undefine(var)
unsigned char *var;
{
register struct env_lst *ep;
if (ep = env_find(var)) {
ep->prev->next = ep->next;
if (ep->next)
ep->next->prev = ep->prev;
if (ep->var)
free(ep->var);
if (ep->value)
free(ep->value);
free(ep);
}
}
void
env_export(var)
unsigned char *var;
{
register struct env_lst *ep;
if (ep = env_find(var))
ep->export = 1;
}
void
env_unexport(var)
unsigned char *var;
{
register struct env_lst *ep;
if (ep = env_find(var))
ep->export = 0;
}
void
env_send(var)
unsigned char *var;
{
register struct env_lst *ep;
if (my_state_is_wont(TELOPT_NEW_ENVIRON)
#ifdef OLD_ENVIRON
&& my_state_is_wont(TELOPT_OLD_ENVIRON)
#endif
) {
fprintf(stderr,
"Cannot send '%s': Telnet ENVIRON option not enabled\n",
var);
return;
}
ep = env_find(var);
if (ep == 0) {
fprintf(stderr, "Cannot send '%s': variable not defined\n",
var);
return;
}
env_opt_start_info();
env_opt_add(ep->var);
env_opt_end(0);
}
void
env_list()
{
register struct env_lst *ep;
for (ep = envlisthead.next; ep; ep = ep->next) {
printf("%c %-20s %s\n", ep->export ? '*' : ' ',
ep->var, ep->value);
}
}
unsigned char *
env_default(init, welldefined)
int init;
{
static struct env_lst *nep = NULL;
if (init) {
nep = &envlisthead;
return;
}
if (nep) {
while (nep = nep->next) {
if (nep->export && (nep->welldefined == welldefined))
return(nep->var);
}
}
return(NULL);
}
unsigned char *
env_getvalue(var)
unsigned char *var;
{
register struct env_lst *ep;
if (ep = env_find(var))
return(ep->value);
return(NULL);
}
#if defined(OLD_ENVIRON) && defined(ENV_HACK)
void
env_varval(what)
unsigned char *what;
{
extern int old_env_var, old_env_value, env_auto;
int len = strlen((char *)what);
if (len == 0)
goto unknown;
if (strncasecmp((char *)what, "status", len) == 0) {
if (env_auto)
printf("%s%s", "VAR and VALUE are/will be ",
"determined automatically\n");
if (old_env_var == OLD_ENV_VAR)
printf("VAR and VALUE set to correct definitions\n");
else
printf("VAR and VALUE definitions are reversed\n");
} else if (strncasecmp((char *)what, "auto", len) == 0) {
env_auto = 1;
old_env_var = OLD_ENV_VALUE;
old_env_value = OLD_ENV_VAR;
} else if (strncasecmp((char *)what, "right", len) == 0) {
env_auto = 0;
old_env_var = OLD_ENV_VAR;
old_env_value = OLD_ENV_VALUE;
} else if (strncasecmp((char *)what, "wrong", len) == 0) {
env_auto = 0;
old_env_var = OLD_ENV_VALUE;
old_env_value = OLD_ENV_VAR;
} else {
unknown:
printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
}
}
#endif
#if defined(AUTHENTICATION)
/*
* The AUTHENTICATE command.
*/
struct authlist {
char *name;
char *help;
int (*handler)();
int narg;
};
extern int
auth_enable P((char *)),
auth_disable P((char *)),
auth_status P((void));
static int
auth_help P((void));
struct authlist AuthList[] = {
{ "status", "Display current status of authentication information",
auth_status, 0 },
{ "disable", "Disable an authentication type ('auth disable ?' for more)",
auth_disable, 1 },
{ "enable", "Enable an authentication type ('auth enable ?' for more)",
auth_enable, 1 },
{ "help", 0, auth_help, 0 },
{ "?", "Print help information", auth_help, 0 },
{ 0 },
};
static int
auth_help()
{
struct authlist *c;
for (c = AuthList; c->name; c++) {
if (c->help) {
if (*c->help)
printf("%-15s %s\n", c->name, c->help);
else
printf("\n");
}
}
return 0;
}
auth_cmd(argc, argv)
int argc;
char *argv[];
{
struct authlist *c;
if (argc < 2) {
fprintf(stderr,
"Need an argument to 'auth' command. 'auth ?' for help.\n");
return 0;
}
c = (struct authlist *)
genget(argv[1], (char **) AuthList, sizeof(struct authlist));
if (c == 0) {
fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
argv[1]);
return 0;
}
if (Ambiguous(c)) {
fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
argv[1]);
return 0;
}
if (c->narg + 2 != argc) {
fprintf(stderr,
"Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
c->narg < argc + 2 ? "only " : "",
c->narg, c->narg == 1 ? "" : "s", c->name);
return 0;
}
return((*c->handler)(argv[2], argv[3]));
}
#endif
#ifdef ENCRYPTION
/*
* The ENCRYPT command.
*/
struct encryptlist {
char *name;
char *help;
int (*handler)();
int needconnect;
int minarg;
int maxarg;
};
extern int
EncryptEnable P((char *, char *)),
EncryptDisable P((char *, char *)),
EncryptType P((char *, char *)),
EncryptStart P((char *)),
EncryptStartInput P((void)),
EncryptStartOutput P((void)),
EncryptStop P((char *)),
EncryptStopInput P((void)),
EncryptStopOutput P((void)),
EncryptStatus P((void));
static int
EncryptHelp P((void));
struct encryptlist EncryptList[] = {
{ "enable", "Enable encryption. ('encrypt enable ?' for more)",
EncryptEnable, 1, 1, 2 },
{ "disable", "Disable encryption. ('encrypt enable ?' for more)",
EncryptDisable, 0, 1, 2 },
{ "type", "Set encryption type. ('encrypt type ?' for more)",
EncryptType, 0, 1, 1 },
{ "start", "Start encryption. ('encrypt start ?' for more)",
EncryptStart, 1, 0, 1 },
{ "stop", "Stop encryption. ('encrypt stop ?' for more)",
EncryptStop, 1, 0, 1 },
{ "input", "Start encrypting the input stream",
EncryptStartInput, 1, 0, 0 },
{ "-input", "Stop encrypting the input stream",
EncryptStopInput, 1, 0, 0 },
{ "output", "Start encrypting the output stream",
EncryptStartOutput, 1, 0, 0 },
{ "-output", "Stop encrypting the output stream",
EncryptStopOutput, 1, 0, 0 },
{ "status", "Display current status of authentication information",
EncryptStatus, 0, 0, 0 },
{ "help", 0, EncryptHelp, 0, 0, 0 },
{ "?", "Print help information", EncryptHelp, 0, 0, 0 },
{ 0 },
};
static int
EncryptHelp()
{
struct encryptlist *c;
for (c = EncryptList; c->name; c++) {
if (c->help) {
if (*c->help)
printf("%-15s %s\n", c->name, c->help);
else
printf("\n");
}
}
return 0;
}
encrypt_cmd(argc, argv)
int argc;
char *argv[];
{
struct encryptlist *c;
if (argc < 2) {
fprintf(stderr,
"Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
return 0;
}
c = (struct encryptlist *)
genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
if (c == 0) {
fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\n",
argv[1]);
return 0;
}
if (Ambiguous(c)) {
fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\n",
argv[1]);
return 0;
}
argc -= 2;
if (argc < c->minarg || argc > c->maxarg) {
if (c->minarg == c->maxarg) {
fprintf(stderr, "Need %s%d argument%s ",
c->minarg < argc ? "only " : "", c->minarg,
c->minarg == 1 ? "" : "s");
} else {
fprintf(stderr, "Need %s%d-%d arguments ",
c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
}
fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
c->name);
return 0;
}
if (c->needconnect && !connected) {
if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
printf("?Need to be connected first.\n");
return 0;
}
}
return ((*c->handler)(argc > 0 ? argv[2] : 0,
argc > 1 ? argv[3] : 0,
argc > 2 ? argv[4] : 0));
}
#endif /* ENCRYPTION */
#if defined(unix) && defined(TN3270)
static void
filestuff(fd)
int fd;
{
int res;
#ifdef F_GETOWN
setconnmode(0);
res = fcntl(fd, F_GETOWN, 0);
setcommandmode();
if (res == -1) {
perror("fcntl");
return;
}
printf("\tOwner is %d.\n", res);
#endif
setconnmode(0);
res = fcntl(fd, F_GETFL, 0);
setcommandmode();
if (res == -1) {
perror("fcntl");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -