mirror of
https://github.com/lcn2/calc.git
synced 2025-08-16 01:03:29 +03:00
Release calc version 2.11.1
This commit is contained in:
177
hist.c
177
hist.c
@@ -1,8 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 1997 David I. Bell
|
||||
* Permission is granted to use, distribute, or modify this source,
|
||||
* provided that this copyright notice remains intact.
|
||||
* hist - interactive readline module
|
||||
*
|
||||
* Copyright (C) 1999 David I. Bell
|
||||
*
|
||||
* Calc is open software; you can redistribute it and/or modify it under
|
||||
* the terms of the version 2.1 of the GNU Lesser General Public License
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* Calc is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
|
||||
* Public License for more details.
|
||||
*
|
||||
* A copy of version 2.1 of the GNU Lesser General Public License is
|
||||
* distributed with calc under the filename COPYING-LGPL. You should have
|
||||
* received a copy with calc; if not, write to Free Software Foundation, Inc.
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* @(#) $Revision: 29.1 $
|
||||
* @(#) $Id: hist.c,v 29.1 1999/12/14 09:16:10 chongo Exp $
|
||||
* @(#) $Source: /usr/local/src/cmd/calc/RCS/hist.c,v $
|
||||
*
|
||||
* Under source code control: 1993/05/02 20:09:19
|
||||
* File existed as early as: 1993
|
||||
*
|
||||
* Share and enjoy! :-) http://reality.sgi.com/chongo/tech/comp/calc/
|
||||
*/
|
||||
|
||||
/*
|
||||
* Adapted from code written by Stephen Rothwell.
|
||||
*
|
||||
* GNU readline support added by Martin Buck <mbuck@debian.org>
|
||||
@@ -12,6 +37,7 @@
|
||||
* The key bindings for the editing commands are (slightly) configurable.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <pwd.h>
|
||||
@@ -28,7 +54,6 @@
|
||||
|
||||
#include "calc.h"
|
||||
#include "hist.h"
|
||||
#include "terminal.h"
|
||||
#include "have_string.h"
|
||||
|
||||
#include "have_strdup.h"
|
||||
@@ -36,21 +61,6 @@
|
||||
# define strdup(x) calc_strdup((CONST char *)(x))
|
||||
#endif /* HAVE_STRDUP */
|
||||
|
||||
|
||||
#if defined(USE_TERMIOS)
|
||||
# include <termios.h>
|
||||
# define TTYSTRUCT struct termios
|
||||
#else /* USE_SGTTY */
|
||||
# if defined(USE_TERMIO)
|
||||
# include <termio.h>
|
||||
# define TTYSTRUCT struct termio
|
||||
# else /* USE_TERMIO */
|
||||
/* assume USE_SGTTY */
|
||||
# include <sys/ioctl.h>
|
||||
# define TTYSTRUCT struct sgttyb
|
||||
# endif /* USE_TERMIO */
|
||||
#endif /* USE_SGTTY */
|
||||
|
||||
#ifdef HAVE_STRING_H
|
||||
# include <string.h>
|
||||
#endif
|
||||
@@ -204,7 +214,6 @@ static int canedit;
|
||||
static int histused;
|
||||
static int key_count;
|
||||
static int save_len;
|
||||
static TTYSTRUCT oldtty;
|
||||
static KEY_MAP *cur_map;
|
||||
static KEY_MAP *base_map;
|
||||
static KEY_ENT key_table[MAX_KEYS];
|
||||
@@ -316,14 +325,12 @@ hist_getline(char *prompt, char *buf, int len)
|
||||
int
|
||||
hist_init(char *filename)
|
||||
{
|
||||
TTYSTRUCT newtty;
|
||||
|
||||
/*
|
||||
* prevent multiple initializations
|
||||
*/
|
||||
if (inited) {
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: inited already set in hist_init\n");
|
||||
printf("hist_init: inited already set\n");
|
||||
return HIST_INITED;
|
||||
}
|
||||
|
||||
@@ -333,7 +340,7 @@ hist_init(char *filename)
|
||||
inited = 1;
|
||||
canedit = 0;
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: Set inited, cleared canedit in hist_init\n");
|
||||
printf("hist_init: Set inited, cleared canedit\n");
|
||||
|
||||
/*
|
||||
* open the bindings file
|
||||
@@ -354,77 +361,16 @@ hist_init(char *filename)
|
||||
*/
|
||||
closeinput();
|
||||
|
||||
#ifdef USE_SGTTY
|
||||
if (ioctl(STDIN, TIOCGETP, &oldtty) < 0) {
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: Cannot TIOCGETP stdin in hist_init\n");
|
||||
/*
|
||||
* setup the calc TTY on STDIN
|
||||
*/
|
||||
if (!calc_tty(STDIN)) {
|
||||
return HIST_NOTTY;
|
||||
}
|
||||
|
||||
newtty = oldtty;
|
||||
newtty.sg_flags &= ~ECHO;
|
||||
newtty.sg_flags |= CBREAK;
|
||||
|
||||
if (ioctl(STDIN, TIOCSETP, &newtty) < 0) {
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: Cannot TIOCSETP stdin in hist_init\n");
|
||||
return HIST_NOTTY;
|
||||
}
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: stty -ECHO +CBREAK in hist_init\n");
|
||||
#endif
|
||||
|
||||
#ifdef USE_TERMIO
|
||||
if (ioctl(STDIN, TCGETA, &oldtty) < 0) {
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: Cannot TCGETA stdin in hist_init\n");
|
||||
return HIST_NOTTY;
|
||||
}
|
||||
|
||||
newtty = oldtty;
|
||||
newtty.c_lflag &= ~(ECHO | ECHOE | ECHOK);
|
||||
newtty.c_iflag |= ISTRIP;
|
||||
newtty.c_lflag &= ~ICANON;
|
||||
newtty.c_cc[VMIN] = 1;
|
||||
newtty.c_cc[VTIME] = 0;
|
||||
|
||||
if (ioctl(STDIN, TCSETAW, &newtty) < 0) {
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: Cannot TCSETAW stdin in hist_init\n");
|
||||
return HIST_NOTTY;
|
||||
}
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: stty -ECHO -ECHOE -ECHOK -ICANON +ISTRIP "
|
||||
"VMIN=1 VTIME=0 in hist_init\n");
|
||||
#endif
|
||||
|
||||
#ifdef USE_TERMIOS
|
||||
if (tcgetattr(STDIN, &oldtty) < 0) {
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: Cannot tcgetattr stdin in hist_init\n");
|
||||
return HIST_NOTTY;
|
||||
}
|
||||
|
||||
newtty = oldtty;
|
||||
newtty.c_lflag &= ~(ECHO | ECHOE | ECHOK);
|
||||
newtty.c_iflag |= ISTRIP;
|
||||
newtty.c_lflag &= ~ICANON;
|
||||
newtty.c_cc[VMIN] = 1;
|
||||
newtty.c_cc[VTIME] = 0;
|
||||
|
||||
if (tcsetattr(STDIN, TCSANOW, &newtty) < 0) {
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: Cannot tcsetattr stdin in hist_init\n");
|
||||
return HIST_NOTTY;
|
||||
}
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: stty -ECHO -ECHOE -ECHOK -ICANON +ISTRIP "
|
||||
"VMIN=1 VTIME=0 in hist_init\n");
|
||||
#endif
|
||||
|
||||
canedit = 1;
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: Set canedit in hist_init\n");
|
||||
printf("hist_init: Set canedit\n");
|
||||
|
||||
return HIST_SUCCESS;
|
||||
}
|
||||
@@ -439,35 +385,20 @@ hist_term(void)
|
||||
if (!inited || !canedit) {
|
||||
if (conf->calc_debug & CALCDBG_TTY) {
|
||||
if (!inited)
|
||||
printf("DEBUG: inited already cleared "
|
||||
"in hist_term\n");
|
||||
printf("hist_term: inited already cleared\n");
|
||||
if (!canedit)
|
||||
printf("DEBUG: canedit already cleared "
|
||||
"in hist_term\n");
|
||||
printf("hist_term: canedit already cleared\n");
|
||||
}
|
||||
inited = 0;
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: Cleared inited in hist_term\n");
|
||||
printf("hist_term: Cleared inited\n");
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef USE_SGTTY
|
||||
(void) ioctl(STDIN, TIOCSETP, &oldtty);
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: TIOCSETP restored stdin in hist_term\n");
|
||||
#endif
|
||||
|
||||
#ifdef USE_TERMIO
|
||||
(void) ioctl(STDIN, TCSETAW, &oldtty);
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: TCSETAW restored stdin in hist_term\n");
|
||||
#endif
|
||||
|
||||
#ifdef USE_TERMIOS
|
||||
(void) tcsetattr(STDIN, TCSANOW, &oldtty);
|
||||
if (conf->calc_debug & CALCDBG_TTY)
|
||||
printf("DEBUG: TCSANOW restored stdin in hist_term\n");
|
||||
#endif
|
||||
/*
|
||||
* restore orignal tty state
|
||||
*/
|
||||
(void) orig_tty(STDIN);
|
||||
}
|
||||
|
||||
|
||||
@@ -1023,8 +954,8 @@ delete_char(void)
|
||||
* if config("ctrl_d", "virgin") and we have never typed on the line.
|
||||
*/
|
||||
if ((HS.end == HS.buf) &&
|
||||
(conf->ctrl_d == CTRL_D_EOF ||
|
||||
(conf->ctrl_d == CTRL_D_VIRGIN && HS.virgin_line == TRUE))) {
|
||||
(conf->ctrl_d == CTRL_D_EMPTY_EOF ||
|
||||
(conf->ctrl_d == CTRL_D_VIRGIN_EOF && HS.virgin_line == TRUE))) {
|
||||
quit_calc();
|
||||
}
|
||||
|
||||
@@ -1476,6 +1407,7 @@ memrcpy(char *dest, char *src, int len)
|
||||
*dest-- = *src--;
|
||||
}
|
||||
|
||||
#endif /* !USE_READLINE */
|
||||
|
||||
static void
|
||||
quit_calc(void)
|
||||
@@ -1486,7 +1418,7 @@ quit_calc(void)
|
||||
exit(0);
|
||||
}
|
||||
|
||||
#else /* USE_READLINE */
|
||||
#if defined(USE_READLINE)
|
||||
|
||||
|
||||
#define HISTORY_LEN (1024) /* number of entries to save */
|
||||
@@ -1516,8 +1448,17 @@ hist_getline(char *prompt, char *buf, int len)
|
||||
|
||||
buf[0] = '\0';
|
||||
line = readline(prompt);
|
||||
if (!line)
|
||||
return 0;
|
||||
if (!line) {
|
||||
switch (conf->ctrl_d) {
|
||||
case CTRL_D_NEVER_EOF:
|
||||
return 0;
|
||||
case CTRL_D_VIRGIN_EOF:
|
||||
case CTRL_D_EMPTY_EOF:
|
||||
default:
|
||||
quit_calc();
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
}
|
||||
strncpy(buf, line, len - 1);
|
||||
buf[len - 2] = '\0';
|
||||
len = strlen(buf);
|
||||
@@ -1577,7 +1518,7 @@ hist_saveline(char *line, int len)
|
||||
if (prev != NULL && strcmp(prev, line) == 0)
|
||||
return;
|
||||
|
||||
free (prev);
|
||||
free(prev);
|
||||
|
||||
/* fail silently */
|
||||
prev = strdup(line);
|
||||
|
Reference in New Issue
Block a user