1998-02-10 03:23:50 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1998-03-20 19:48:28 +00:00
|
|
|
* $Id: prompt.c,v 1.1.2.15 1998/03/16 22:54:22 brian Exp $
|
1998-02-10 03:23:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <netinet/in.h>
|
1998-03-16 22:54:35 +00:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1998-02-10 03:23:50 +00:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "descriptor.h"
|
|
|
|
#include "prompt.h"
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "lcp.h"
|
|
|
|
#include "auth.h"
|
|
|
|
#include "loadalias.h"
|
|
|
|
#include "vars.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "iplist.h"
|
|
|
|
#include "throughput.h"
|
1998-03-16 22:54:35 +00:00
|
|
|
#include "slcompress.h"
|
1998-02-10 03:23:50 +00:00
|
|
|
#include "ipcp.h"
|
1998-03-16 22:52:54 +00:00
|
|
|
#include "filter.h"
|
1998-03-13 21:07:46 +00:00
|
|
|
#include "bundle.h"
|
1998-03-13 00:44:26 +00:00
|
|
|
#include "lqr.h"
|
1998-02-16 00:01:12 +00:00
|
|
|
#include "hdlc.h"
|
|
|
|
#include "async.h"
|
|
|
|
#include "mbuf.h"
|
|
|
|
#include "link.h"
|
|
|
|
#include "physical.h"
|
1998-02-17 01:05:47 +00:00
|
|
|
#include "chat.h"
|
1998-02-23 00:38:44 +00:00
|
|
|
#include "ccp.h"
|
1998-03-01 01:07:49 +00:00
|
|
|
#include "chap.h"
|
1998-02-17 01:05:47 +00:00
|
|
|
#include "datalink.h"
|
1998-02-10 03:23:50 +00:00
|
|
|
|
|
|
|
static int prompt_nonewline = 1;
|
|
|
|
|
|
|
|
static int
|
|
|
|
prompt_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
|
|
|
|
{
|
|
|
|
struct prompt *p = descriptor2prompt(d);
|
1998-02-13 05:10:26 +00:00
|
|
|
int sets;
|
1998-02-10 03:23:50 +00:00
|
|
|
|
|
|
|
LogPrintf(LogDEBUG, "descriptor2prompt; %p -> %p\n", d, p);
|
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
sets = 0;
|
1998-02-10 03:23:50 +00:00
|
|
|
if (p->fd_in >= 0) {
|
1998-02-13 05:10:26 +00:00
|
|
|
if (r) {
|
|
|
|
FD_SET(p->fd_in, r);
|
|
|
|
sets++;
|
|
|
|
}
|
|
|
|
if (e) {
|
|
|
|
FD_SET(p->fd_in, e);
|
|
|
|
sets++;
|
|
|
|
}
|
|
|
|
if (sets && *n < p->fd_in + 1)
|
1998-02-10 03:23:50 +00:00
|
|
|
*n = p->fd_in + 1;
|
|
|
|
}
|
|
|
|
|
1998-02-13 05:10:26 +00:00
|
|
|
return sets;
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1998-03-20 19:48:28 +00:00
|
|
|
prompt_IsSet(struct descriptor *d, const fd_set *fdset)
|
1998-02-10 03:23:50 +00:00
|
|
|
{
|
|
|
|
struct prompt *p = descriptor2prompt(d);
|
|
|
|
LogPrintf(LogDEBUG, "descriptor2prompt; %p -> %p\n", d, p);
|
|
|
|
return p->fd_in >= 0 && FD_ISSET(p->fd_in, fdset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
prompt_ShowHelp(struct prompt *p)
|
|
|
|
{
|
|
|
|
prompt_Printf(p, "The following commands are available:\r\n");
|
|
|
|
prompt_Printf(p, " ~p\tEnter Packet mode\r\n");
|
|
|
|
prompt_Printf(p, " ~-\tDecrease log level\r\n");
|
|
|
|
prompt_Printf(p, " ~+\tIncrease log level\r\n");
|
|
|
|
prompt_Printf(p, " ~t\tShow timers (only in \"log debug\" mode)\r\n");
|
|
|
|
prompt_Printf(p, " ~m\tShow memory map (only in \"log debug\" mode)\r\n");
|
|
|
|
prompt_Printf(p, " ~.\tTerminate program\r\n");
|
|
|
|
prompt_Printf(p, " ~?\tThis help\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
prompt_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
|
|
|
|
{
|
|
|
|
struct prompt *p = descriptor2prompt(d);
|
|
|
|
int n;
|
|
|
|
char ch;
|
|
|
|
static int ttystate;
|
|
|
|
char linebuff[LINE_LEN];
|
|
|
|
|
|
|
|
LogPrintf(LogDEBUG, "descriptor2prompt; %p -> %p\n", d, p);
|
1998-02-17 19:28:13 +00:00
|
|
|
LogPrintf(LogDEBUG, "termode = %p, p->fd_in = %d, mode = %d\n",
|
1998-02-10 03:23:50 +00:00
|
|
|
p->TermMode, p->fd_in, mode);
|
|
|
|
|
1998-02-17 19:28:13 +00:00
|
|
|
if (p->TermMode == NULL) {
|
1998-02-10 03:23:50 +00:00
|
|
|
n = read(p->fd_in, linebuff, sizeof linebuff - 1);
|
|
|
|
if (n > 0) {
|
|
|
|
if (linebuff[n-1] == '\n')
|
|
|
|
linebuff[--n] = '\0';
|
|
|
|
else
|
|
|
|
linebuff[n] = '\0';
|
|
|
|
if (n)
|
|
|
|
DecodeCommand(bundle, linebuff, n, IsInteractive(0) ? NULL : "Client");
|
|
|
|
prompt_nonewline = 1;
|
|
|
|
prompt_Display(&prompt, bundle);
|
|
|
|
} else if (n <= 0) {
|
|
|
|
LogPrintf(LogPHASE, "Client connection closed.\n");
|
|
|
|
prompt_Drop(&prompt, 0);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1998-02-17 19:28:35 +00:00
|
|
|
switch (p->TermMode->state) {
|
|
|
|
case DATALINK_CLOSED:
|
|
|
|
prompt_Printf(p, "Link lost, terminal mode.\n");
|
|
|
|
prompt_TtyCommandMode(&prompt);
|
|
|
|
prompt_nonewline = 0;
|
|
|
|
prompt_Display(&prompt, bundle);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case DATALINK_READY:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DATALINK_OPEN:
|
|
|
|
prompt_Printf(p, "\nPacket mode detected.\n");
|
|
|
|
prompt_TtyCommandMode(&prompt);
|
|
|
|
prompt_nonewline = 0;
|
|
|
|
/* We'll get a prompt because of our status change */
|
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Wait 'till we're in a state we care about */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1998-02-10 03:23:50 +00:00
|
|
|
/*
|
|
|
|
* We are in terminal mode, decode special sequences
|
|
|
|
*/
|
|
|
|
n = read(p->fd_in, &ch, 1);
|
|
|
|
LogPrintf(LogDEBUG, "Got %d bytes (reading from the terminal)\n", n);
|
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
switch (ttystate) {
|
|
|
|
case 0:
|
|
|
|
if (ch == '~')
|
|
|
|
ttystate++;
|
|
|
|
else
|
|
|
|
/* XXX missing return value check */
|
1998-02-16 00:01:12 +00:00
|
|
|
Physical_Write(bundle2physical(bundle, NULL), &ch, n);
|
1998-02-10 03:23:50 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
switch (ch) {
|
|
|
|
case '?':
|
|
|
|
prompt_ShowHelp(p);
|
|
|
|
break;
|
|
|
|
case 'p':
|
1998-02-17 19:28:35 +00:00
|
|
|
datalink_Up(p->TermMode, 0, 1);
|
|
|
|
prompt_Printf(p, "\nPacket mode.\n");
|
|
|
|
prompt_TtyCommandMode(&prompt);
|
|
|
|
break;
|
1998-02-10 03:23:50 +00:00
|
|
|
case '.':
|
|
|
|
prompt_TtyCommandMode(&prompt);
|
|
|
|
prompt_nonewline = 0;
|
|
|
|
prompt_Display(&prompt, bundle);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
if (LogIsKept(LogDEBUG)) {
|
|
|
|
ShowTimers();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'm':
|
|
|
|
if (LogIsKept(LogDEBUG)) {
|
|
|
|
ShowMemMap(NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
1998-02-16 00:01:12 +00:00
|
|
|
if (Physical_Write(bundle2physical(bundle, NULL), &ch, n) < 0)
|
1998-02-10 03:23:50 +00:00
|
|
|
LogPrintf(LogERROR, "error writing to modem.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ttystate = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-02-23 00:38:44 +00:00
|
|
|
prompt_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
|
1998-02-10 03:23:50 +00:00
|
|
|
{
|
1998-02-23 00:38:44 +00:00
|
|
|
/* We never want to write here ! */
|
|
|
|
LogPrintf(LogERROR, "prompt_Write: Internal error: Bad call !\n");
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct prompt prompt = {
|
|
|
|
{
|
|
|
|
PROMPT_DESCRIPTOR,
|
|
|
|
NULL,
|
|
|
|
prompt_UpdateSet,
|
|
|
|
prompt_IsSet,
|
|
|
|
prompt_Read,
|
|
|
|
prompt_Write
|
|
|
|
},
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
prompt_Init(struct prompt *p, int fd)
|
|
|
|
{
|
|
|
|
if (p->Term && p->Term != stdout)
|
|
|
|
return 0; /* must prompt_Drop() first */
|
|
|
|
|
|
|
|
if (fd == PROMPT_NONE) {
|
|
|
|
p->fd_in = p->fd_out = -1;
|
|
|
|
p->Term = NULL;
|
|
|
|
} else if (fd == PROMPT_STD) {
|
|
|
|
p->fd_in = STDIN_FILENO;
|
|
|
|
p->fd_out = STDOUT_FILENO;
|
|
|
|
p->Term = stdout;
|
|
|
|
} else {
|
|
|
|
p->fd_in = p->fd_out = fd;
|
|
|
|
p->Term = fdopen(fd, "a+");
|
|
|
|
}
|
1998-02-17 19:28:13 +00:00
|
|
|
p->TermMode = NULL;
|
1998-02-10 03:23:50 +00:00
|
|
|
tcgetattr(STDIN_FILENO, &p->oldtio); /* Save original tty mode */
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-02-16 00:01:12 +00:00
|
|
|
void
|
1998-02-10 03:23:50 +00:00
|
|
|
prompt_Display(struct prompt *p, struct bundle *bundle)
|
|
|
|
{
|
|
|
|
const char *pconnect, *pauth;
|
|
|
|
|
1998-03-16 22:53:15 +00:00
|
|
|
if (!p->Term || p->TermMode != NULL || bundle->CleaningUp)
|
1998-02-10 03:23:50 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (prompt_nonewline)
|
|
|
|
prompt_nonewline = 0;
|
|
|
|
else
|
|
|
|
fprintf(p->Term, "\n");
|
|
|
|
|
|
|
|
if (VarLocalAuth == LOCAL_AUTH)
|
|
|
|
pauth = " ON ";
|
|
|
|
else
|
|
|
|
pauth = " on ";
|
|
|
|
|
1998-03-13 21:07:46 +00:00
|
|
|
if (bundle->ncp.ipcp.fsm.state == ST_OPENED)
|
1998-02-10 03:23:50 +00:00
|
|
|
pconnect = "PPP";
|
|
|
|
else if (bundle_Phase(bundle) == PHASE_NETWORK)
|
|
|
|
pconnect = "PPp";
|
|
|
|
else if (bundle_Phase(bundle) == PHASE_AUTHENTICATE)
|
|
|
|
pconnect = "Ppp";
|
|
|
|
else
|
|
|
|
pconnect = "ppp";
|
|
|
|
|
|
|
|
fprintf(p->Term, "%s%s%s> ", pconnect, pauth, VarShortHost);
|
|
|
|
fflush(p->Term);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prompt_Drop(struct prompt *p, int verbose)
|
|
|
|
{
|
|
|
|
if (p->Term && p->Term != stdout) {
|
|
|
|
FILE *oVarTerm;
|
|
|
|
|
|
|
|
oVarTerm = p->Term;
|
|
|
|
p->Term = NULL;
|
|
|
|
if (oVarTerm)
|
|
|
|
fclose(oVarTerm);
|
|
|
|
close(p->fd_in);
|
|
|
|
if (p->fd_out != p->fd_in)
|
|
|
|
close(p->fd_out);
|
|
|
|
p->fd_in = p->fd_out = -1;
|
|
|
|
if (verbose)
|
|
|
|
LogPrintf(LogPHASE, "Client connection dropped.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prompt_Printf(struct prompt *p, const char *fmt,...)
|
|
|
|
{
|
|
|
|
if (p->Term) {
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(p->Term, fmt, ap);
|
|
|
|
fflush(p->Term);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prompt_vPrintf(struct prompt *p, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
if (p->Term) {
|
|
|
|
vfprintf(p->Term, fmt, ap);
|
|
|
|
fflush(p->Term);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prompt_TtyInit(struct prompt *p, int DontWantInt)
|
|
|
|
{
|
|
|
|
struct termios newtio;
|
|
|
|
int stat;
|
|
|
|
|
|
|
|
stat = fcntl(p->fd_in, F_GETFL, 0);
|
|
|
|
if (stat > 0) {
|
|
|
|
stat |= O_NONBLOCK;
|
|
|
|
(void) fcntl(p->fd_in, F_SETFL, stat);
|
|
|
|
}
|
|
|
|
newtio = p->oldtio;
|
|
|
|
newtio.c_lflag &= ~(ECHO | ISIG | ICANON);
|
|
|
|
newtio.c_iflag = 0;
|
|
|
|
newtio.c_oflag &= ~OPOST;
|
|
|
|
newtio.c_cc[VEOF] = _POSIX_VDISABLE;
|
|
|
|
if (DontWantInt)
|
|
|
|
newtio.c_cc[VINTR] = _POSIX_VDISABLE;
|
|
|
|
newtio.c_cc[VMIN] = 1;
|
|
|
|
newtio.c_cc[VTIME] = 0;
|
|
|
|
newtio.c_cflag |= CS8;
|
|
|
|
tcsetattr(p->fd_in, TCSANOW, &newtio);
|
|
|
|
p->comtio = newtio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set tty into command mode. We allow canonical input and echo processing.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
prompt_TtyCommandMode(struct prompt *p)
|
|
|
|
{
|
|
|
|
struct termios newtio;
|
|
|
|
int stat;
|
|
|
|
|
|
|
|
if (!(mode & MODE_INTER))
|
|
|
|
return;
|
|
|
|
|
|
|
|
tcgetattr(p->fd_in, &newtio);
|
|
|
|
newtio.c_lflag |= (ECHO | ISIG | ICANON);
|
|
|
|
newtio.c_iflag = p->oldtio.c_iflag;
|
|
|
|
newtio.c_oflag |= OPOST;
|
|
|
|
tcsetattr(p->fd_in, TCSADRAIN, &newtio);
|
|
|
|
stat = fcntl(p->fd_in, F_GETFL, 0);
|
|
|
|
if (stat > 0) {
|
|
|
|
stat |= O_NONBLOCK;
|
|
|
|
(void) fcntl(p->fd_in, F_SETFL, stat);
|
|
|
|
}
|
1998-02-17 19:28:13 +00:00
|
|
|
p->TermMode = NULL;
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set tty into terminal mode which is used while we invoke term command.
|
|
|
|
*/
|
|
|
|
void
|
1998-02-17 19:28:13 +00:00
|
|
|
prompt_TtyTermMode(struct prompt *p, struct datalink *dl)
|
1998-02-10 03:23:50 +00:00
|
|
|
{
|
|
|
|
int stat;
|
|
|
|
|
|
|
|
tcsetattr(p->fd_in, TCSADRAIN, &p->comtio);
|
|
|
|
stat = fcntl(p->fd_in, F_GETFL, 0);
|
|
|
|
if (stat > 0) {
|
|
|
|
stat &= ~O_NONBLOCK;
|
|
|
|
(void) fcntl(p->fd_in, F_SETFL, stat);
|
|
|
|
}
|
1998-02-17 19:28:13 +00:00
|
|
|
p->TermMode = dl;
|
1998-02-10 03:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prompt_TtyOldMode(struct prompt *p)
|
|
|
|
{
|
|
|
|
int stat;
|
|
|
|
|
|
|
|
stat = fcntl(p->fd_in, F_GETFL, 0);
|
|
|
|
if (stat > 0) {
|
|
|
|
stat &= ~O_NONBLOCK;
|
|
|
|
(void) fcntl(p->fd_in, F_SETFL, stat);
|
|
|
|
}
|
|
|
|
tcsetattr(p->fd_in, TCSADRAIN, &p->oldtio);
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t
|
|
|
|
prompt_pgrp(struct prompt *p)
|
|
|
|
{
|
|
|
|
return p->Term ? tcgetpgrp(p->fd_in) : -1;
|
|
|
|
}
|