2000-05-06 21:34:20 +02:00
|
|
|
/****************************************************************/
|
|
|
|
/* */
|
|
|
|
/* chario.c */
|
|
|
|
/* DOS-C */
|
|
|
|
/* */
|
|
|
|
/* Character device functions and device driver interface */
|
|
|
|
/* */
|
|
|
|
/* Copyright (c) 1994 */
|
|
|
|
/* Pasquale J. Villani */
|
|
|
|
/* All Rights Reserved */
|
|
|
|
/* */
|
|
|
|
/* This file is part of DOS-C. */
|
|
|
|
/* */
|
|
|
|
/* DOS-C is free software; you can redistribute it and/or */
|
|
|
|
/* modify it under the terms of the GNU General Public License */
|
|
|
|
/* as published by the Free Software Foundation; either version */
|
|
|
|
/* 2, or (at your option) any later version. */
|
|
|
|
/* */
|
|
|
|
/* DOS-C 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 General Public License for more details. */
|
|
|
|
/* */
|
|
|
|
/* You should have received a copy of the GNU General Public */
|
|
|
|
/* License along with DOS-C; see the file COPYING. If not, */
|
|
|
|
/* write to the Free Software Foundation, 675 Mass Ave, */
|
|
|
|
/* Cambridge, MA 02139, USA. */
|
|
|
|
/* */
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
#include "portab.h"
|
|
|
|
|
|
|
|
#ifdef VERSION_STRINGS
|
2001-11-18 15:01:12 +01:00
|
|
|
static BYTE *charioRcsId =
|
|
|
|
"$Id$";
|
2000-05-06 21:34:20 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "globals.h"
|
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
STATIC int CharRequest(struct dhdr FAR **pdev, unsigned command)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
struct dhdr FAR *dev = *pdev;
|
|
|
|
CharReqHdr.r_command = command;
|
2002-10-22 04:40:19 +02:00
|
|
|
CharReqHdr.r_unit = 0;
|
|
|
|
CharReqHdr.r_status = 0;
|
|
|
|
CharReqHdr.r_length = sizeof(request);
|
|
|
|
execrh(&CharReqHdr, dev);
|
|
|
|
if (CharReqHdr.r_status & S_ERROR)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
for (;;) {
|
|
|
|
switch (char_error(&CharReqHdr, dev))
|
|
|
|
{
|
2002-10-22 04:40:19 +02:00
|
|
|
case ABORT:
|
|
|
|
case FAIL:
|
|
|
|
return DE_INVLDACC;
|
|
|
|
case CONTINUE:
|
|
|
|
CharReqHdr.r_count = 0;
|
|
|
|
return 0;
|
|
|
|
case RETRY:
|
|
|
|
return 1;
|
2003-09-09 19:43:43 +02:00
|
|
|
}
|
2002-10-22 04:40:19 +02:00
|
|
|
}
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
2003-09-09 19:43:43 +02:00
|
|
|
return SUCCESS;
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
2000-05-26 21:25:19 +02:00
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
long BinaryCharIO(struct dhdr FAR **pdev, size_t n, void FAR * bp,
|
|
|
|
unsigned command)
|
2002-08-04 03:14:18 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
int err;
|
2002-10-22 04:40:19 +02:00
|
|
|
do
|
2002-08-04 03:14:18 +02:00
|
|
|
{
|
|
|
|
CharReqHdr.r_count = n;
|
|
|
|
CharReqHdr.r_trans = bp;
|
2003-09-09 19:43:43 +02:00
|
|
|
err = CharRequest(pdev, command);
|
|
|
|
} while (err == 1);
|
2003-09-04 21:05:27 +02:00
|
|
|
return err == SUCCESS ? (long)CharReqHdr.r_count : err;
|
2002-10-22 04:40:19 +02:00
|
|
|
}
|
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
STATIC int CharIO(struct dhdr FAR **pdev, unsigned char ch, unsigned command)
|
|
|
|
{
|
|
|
|
int err = (int)BinaryCharIO(pdev, 1, &ch, command);
|
|
|
|
if (err == 0)
|
|
|
|
return 256;
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
/* STATE FUNCTIONS */
|
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
void CharCmd(struct dhdr FAR **pdev, unsigned command)
|
2002-10-22 04:40:19 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
while (CharRequest(pdev, command) == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int Busy(struct dhdr FAR **pdev)
|
|
|
|
{
|
|
|
|
CharCmd(pdev, C_ISTAT);
|
|
|
|
return CharReqHdr.r_status & S_BUSY;
|
2002-10-22 04:40:19 +02:00
|
|
|
}
|
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
void con_flush(struct dhdr FAR **pdev)
|
2002-10-22 04:40:19 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
CharCmd(pdev, C_IFLUSH);
|
|
|
|
}
|
2002-10-22 04:40:19 +02:00
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
/* if the sft is invalid, then we just monitor syscon */
|
|
|
|
struct dhdr FAR *sft_to_dev(sft FAR *s)
|
|
|
|
{
|
|
|
|
if (FP_OFF(s) == (size_t) -1)
|
|
|
|
return syscon;
|
2002-10-22 04:40:19 +02:00
|
|
|
if (s->sft_flags & SFT_FDEVICE)
|
2003-09-09 19:43:43 +02:00
|
|
|
return s->sft_dev;
|
|
|
|
return NULL;
|
2002-10-22 04:40:19 +02:00
|
|
|
}
|
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
int StdinBusy(void)
|
2002-10-22 04:40:19 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
sft FAR *s = get_sft(STDIN);
|
|
|
|
struct dhdr FAR *dev = sft_to_dev(s);
|
|
|
|
|
|
|
|
if (dev)
|
|
|
|
return Busy(&dev);
|
|
|
|
|
|
|
|
return s->sft_posit >= s->sft_size;
|
2002-10-22 04:40:19 +02:00
|
|
|
}
|
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
STATIC void Do_DosIdle_loop(struct dhdr FAR **pdev)
|
2002-10-22 04:40:19 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
/* the idle loop is only safe if we're using the character stack */
|
2002-10-22 04:40:19 +02:00
|
|
|
if (user_r->AH < 0xd)
|
2003-09-09 19:43:43 +02:00
|
|
|
while (Busy(pdev) && Busy(&syscon))
|
2002-10-22 04:40:19 +02:00
|
|
|
DosIdle_int();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get character from the console - this is how DOS gets
|
|
|
|
CTL_C/CTL_S/CTL_P when outputting */
|
2003-09-09 19:43:43 +02:00
|
|
|
STATIC int ndread(struct dhdr FAR **pdev)
|
2002-10-22 04:40:19 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
CharCmd(pdev, C_NDREAD);
|
2002-10-22 04:40:19 +02:00
|
|
|
if (CharReqHdr.r_status & S_BUSY)
|
|
|
|
return -1;
|
|
|
|
return CharReqHdr.r_ndbyte;
|
|
|
|
}
|
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
STATIC void con_skip_char(struct dhdr FAR **pdev)
|
2002-10-22 04:40:19 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
if (CharIO(pdev, 0, C_INPUT) == CTL_C)
|
|
|
|
handle_break(pdev);
|
2002-10-22 04:40:19 +02:00
|
|
|
}
|
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
STATIC void con_hold(struct dhdr FAR **pdev)
|
2002-10-22 04:40:19 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
int c = check_handle_break();
|
|
|
|
if (*pdev != syscon)
|
|
|
|
c = ndread(pdev);
|
|
|
|
if (c == CTL_S || c == CTL_C)
|
2002-10-22 04:40:19 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
con_skip_char(pdev);
|
|
|
|
Do_DosIdle_loop(pdev);
|
2002-10-22 04:40:19 +02:00
|
|
|
/* just wait */
|
2003-09-09 19:43:43 +02:00
|
|
|
check_handle_break();
|
|
|
|
con_skip_char(pdev);
|
2002-10-22 04:40:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
int con_break(void)
|
2002-10-22 04:40:19 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
int c = ndread(&syscon);
|
|
|
|
if (c == CTL_C)
|
|
|
|
con_skip_char(&syscon);
|
|
|
|
return c;
|
2002-08-04 03:14:18 +02:00
|
|
|
}
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
/* OUTPUT FUNCTIONS */
|
|
|
|
|
|
|
|
#ifdef __WATCOMC__
|
2003-09-09 19:43:43 +02:00
|
|
|
void fast_put_char(char c);
|
|
|
|
#pragma aux fast_put_char = "int 29h" parm[al] modify exact [bx]
|
|
|
|
#else
|
2002-10-22 04:40:19 +02:00
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
/* writes a character in raw mode using int29 for speed */
|
|
|
|
STATIC void fast_put_char(unsigned char chr)
|
2000-05-26 21:25:19 +02:00
|
|
|
{
|
2001-11-18 15:01:12 +01:00
|
|
|
#if defined(__TURBOC__)
|
2002-11-11 18:42:17 +01:00
|
|
|
_AL = chr;
|
2001-11-18 15:01:12 +01:00
|
|
|
__int__(0x29);
|
2002-08-04 03:14:18 +02:00
|
|
|
#elif defined(I86)
|
2001-11-18 15:01:12 +01:00
|
|
|
asm
|
|
|
|
{
|
2002-11-11 18:42:17 +01:00
|
|
|
mov al, byte ptr chr;
|
2001-11-18 15:01:12 +01:00
|
|
|
int 0x29;
|
|
|
|
}
|
|
|
|
#endif
|
2000-05-26 21:25:19 +02:00
|
|
|
}
|
2003-09-09 19:43:43 +02:00
|
|
|
#endif
|
2000-05-26 21:25:19 +02:00
|
|
|
|
2004-01-27 21:07:28 +01:00
|
|
|
void update_scr_pos(unsigned char c, unsigned char count)
|
2001-08-19 14:58:36 +02:00
|
|
|
{
|
2003-06-15 16:21:49 +02:00
|
|
|
unsigned char scrpos = scr_pos;
|
2003-09-09 19:43:43 +02:00
|
|
|
|
|
|
|
if (c == CR)
|
|
|
|
scrpos = 0;
|
|
|
|
else if (c == BS) {
|
|
|
|
if (scrpos > 0)
|
|
|
|
scrpos--;
|
|
|
|
} else if (c != LF && c != BELL) {
|
|
|
|
scrpos += count;
|
2001-08-19 14:58:36 +02:00
|
|
|
}
|
2003-06-15 16:21:49 +02:00
|
|
|
scr_pos = scrpos;
|
2004-01-27 21:07:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* writes a character in cooked mode; maybe with printer echo;
|
|
|
|
handles TAB expansion */
|
|
|
|
STATIC int cooked_write_char(struct dhdr FAR **pdev,
|
|
|
|
unsigned char c,
|
|
|
|
unsigned char *fast_counter)
|
|
|
|
{
|
|
|
|
unsigned char count = 1;
|
|
|
|
|
|
|
|
if (c == HT) {
|
|
|
|
count = 8 - (scr_pos & 7);
|
|
|
|
c = ' ';
|
|
|
|
}
|
|
|
|
update_scr_pos(c, count);
|
2003-09-09 19:43:43 +02:00
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
|
|
/* if not fast then < 0x80; always check
|
|
|
|
otherwise check every 32 characters */
|
|
|
|
if (*fast_counter <= 0x80)
|
|
|
|
/* Test for hold char and ctl_c */
|
|
|
|
con_hold(pdev);
|
|
|
|
*fast_counter += 1;
|
|
|
|
*fast_counter &= 0x9f;
|
|
|
|
|
|
|
|
if (PrinterEcho)
|
|
|
|
DosWrite(STDPRN, 1, &c);
|
|
|
|
if (*fast_counter & 0x80)
|
|
|
|
{
|
|
|
|
fast_put_char(c);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int err = CharIO(pdev, c, C_OUTPUT);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
}
|
2003-09-15 12:55:58 +02:00
|
|
|
} while (--count != 0);
|
2003-09-09 19:43:43 +02:00
|
|
|
return SUCCESS;
|
2001-08-19 14:58:36 +02:00
|
|
|
}
|
2000-05-06 21:34:20 +02:00
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
long cooked_write(struct dhdr FAR **pdev, size_t n, char FAR *bp)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
size_t xfer = 0;
|
|
|
|
unsigned char fast_counter;
|
|
|
|
|
|
|
|
/* bit 7 means fastcon; low 5 bits count number of characters */
|
|
|
|
fast_counter = ((*pdev)->dh_attr & ATTR_FASTCON) << 3;
|
|
|
|
|
|
|
|
for (xfer = 0; xfer < n; xfer++)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
unsigned char c = *bp++;
|
|
|
|
|
|
|
|
if (c == CTL_Z)
|
|
|
|
break;
|
|
|
|
|
|
|
|
err = cooked_write_char(pdev, c, &fast_counter);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
return xfer;
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
/* writes character for disk file or device */
|
|
|
|
void write_char(int c, int sft_idx)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-10-22 04:40:19 +02:00
|
|
|
unsigned char ch = (unsigned char)c;
|
|
|
|
DosRWSft(sft_idx, 1, &ch, XFR_FORCE_WRITE);
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
|
|
|
|
2002-11-11 09:41:28 +01:00
|
|
|
void write_char_stdout(int c)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2004-01-27 20:26:58 +01:00
|
|
|
unsigned char count = 1;
|
|
|
|
unsigned flags = get_sft(STDOUT)->sft_flags & (SFT_FDEVICE | SFT_FBINARY);
|
|
|
|
|
|
|
|
/* ah=2, ah=9 should expand tabs even for raw devices and disk files */
|
|
|
|
if (flags != SFT_FDEVICE)
|
|
|
|
{
|
2004-01-27 21:07:28 +01:00
|
|
|
if (c == HT) {
|
|
|
|
count = 8 - (scr_pos & 7);
|
|
|
|
c = ' ';
|
2004-01-27 20:26:58 +01:00
|
|
|
}
|
|
|
|
/* for raw devices already updated in dosfns.c */
|
|
|
|
if (!(flags & SFT_FDEVICE))
|
2004-01-27 21:07:28 +01:00
|
|
|
update_scr_pos(c, count);
|
2004-01-27 20:26:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
write_char(c, get_sft_idx(STDOUT));
|
|
|
|
} while (--count != 0);
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
#define iscntrl(c) ((unsigned char)(c) < ' ')
|
|
|
|
|
|
|
|
/* this is for handling things like ^C, mostly used in echoed input */
|
|
|
|
STATIC int echo_char(int c, int sft_idx)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
int out = c;
|
2002-10-22 04:40:19 +02:00
|
|
|
if (iscntrl(c) && c != HT && c != LF && c != CR)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-10-22 04:40:19 +02:00
|
|
|
write_char('^', sft_idx);
|
2003-09-09 19:43:43 +02:00
|
|
|
out += '@';
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
2003-09-09 19:43:43 +02:00
|
|
|
write_char(out, sft_idx);
|
2002-10-22 04:40:19 +02:00
|
|
|
return c;
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
int echo_char_stdin(int c)
|
2001-08-19 14:58:36 +02:00
|
|
|
{
|
2003-07-10 12:46:31 +02:00
|
|
|
return echo_char(c, get_sft_idx(STDIN));
|
2001-08-19 14:58:36 +02:00
|
|
|
}
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
STATIC void destr_bs(int sft_idx)
|
2001-08-19 14:58:36 +02:00
|
|
|
{
|
2002-10-22 04:40:19 +02:00
|
|
|
write_char(BS, sft_idx);
|
|
|
|
write_char(' ', sft_idx);
|
|
|
|
write_char(BS, sft_idx);
|
|
|
|
}
|
2001-11-18 15:01:12 +01:00
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
/* READ FUNCTIONS */
|
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
STATIC int raw_get_char(struct dhdr FAR **pdev, BOOL check_break)
|
2002-10-22 04:40:19 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
Do_DosIdle_loop(pdev);
|
2002-10-22 04:40:19 +02:00
|
|
|
if (check_break)
|
2003-09-09 19:43:43 +02:00
|
|
|
{
|
|
|
|
con_hold(pdev);
|
|
|
|
Do_DosIdle_loop(pdev);
|
|
|
|
}
|
|
|
|
return CharIO(pdev, 0, C_INPUT);
|
2001-08-19 14:58:36 +02:00
|
|
|
}
|
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
long cooked_read(struct dhdr FAR **pdev, size_t n, char FAR *bp)
|
2001-08-19 14:58:36 +02:00
|
|
|
{
|
2002-10-22 04:40:19 +02:00
|
|
|
unsigned xfer = 0;
|
|
|
|
int c;
|
|
|
|
while(n--)
|
2001-08-20 22:32:15 +02:00
|
|
|
{
|
2003-09-09 19:43:43 +02:00
|
|
|
c = raw_get_char(pdev, TRUE);
|
2002-10-22 04:40:19 +02:00
|
|
|
if (c < 0)
|
|
|
|
return c;
|
2003-09-09 19:43:43 +02:00
|
|
|
if (c == 256)
|
|
|
|
break;
|
2002-10-22 04:40:19 +02:00
|
|
|
*bp++ = c;
|
|
|
|
xfer++;
|
2003-09-15 12:55:58 +02:00
|
|
|
if ((unsigned char)c == CTL_Z)
|
2002-10-22 04:40:19 +02:00
|
|
|
break;
|
2001-08-20 22:32:15 +02:00
|
|
|
}
|
2002-10-22 04:40:19 +02:00
|
|
|
return xfer;
|
2001-08-19 14:58:36 +02:00
|
|
|
}
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
unsigned char read_char(int sft_idx, BOOL check_break)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-10-22 04:40:19 +02:00
|
|
|
unsigned char c;
|
2003-09-09 19:43:43 +02:00
|
|
|
struct dhdr FAR *dev = sft_to_dev(idx_to_sft(sft_idx));
|
2002-10-22 04:40:19 +02:00
|
|
|
|
2003-09-09 19:43:43 +02:00
|
|
|
if (dev)
|
|
|
|
return (unsigned char)raw_get_char(&dev, check_break);
|
2002-10-22 04:40:19 +02:00
|
|
|
|
|
|
|
DosRWSft(sft_idx, 1, &c, XFR_READ);
|
2000-05-06 21:34:20 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
STATIC unsigned char read_char_check_break(int sft_idx)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-10-22 04:40:19 +02:00
|
|
|
return read_char(sft_idx, TRUE);
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
unsigned char read_char_stdin(BOOL check_break)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-10-22 04:40:19 +02:00
|
|
|
return read_char(get_sft_idx(STDIN), check_break);
|
|
|
|
}
|
2000-05-06 21:34:20 +02:00
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
/* reads a line (buffered, called by int21/ah=0ah, 3fh) */
|
|
|
|
void read_line(int sft_in, int sft_out, keyboard FAR * kp)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-10-22 04:40:19 +02:00
|
|
|
unsigned c;
|
|
|
|
unsigned cu_pos = scr_pos;
|
|
|
|
unsigned count = 0, stored_pos = 0;
|
|
|
|
unsigned size = kp->kb_size, stored_size = kp->kb_count;
|
2002-08-05 21:56:38 +02:00
|
|
|
BOOL insert = FALSE;
|
|
|
|
|
|
|
|
if (size == 0)
|
2000-05-06 21:34:20 +02:00
|
|
|
return;
|
2002-08-05 21:56:38 +02:00
|
|
|
|
|
|
|
/* the stored line is invalid unless it ends with a CR */
|
|
|
|
if (kp->kb_buf[stored_size] != CR)
|
|
|
|
stored_size = 0;
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
do
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-10-22 04:40:19 +02:00
|
|
|
unsigned new_pos = stored_size;
|
|
|
|
|
|
|
|
c = read_char_check_break(sft_in);
|
|
|
|
if (c == 0)
|
|
|
|
c = (unsigned)read_char_check_break(sft_in) << 8;
|
2002-08-05 21:56:38 +02:00
|
|
|
switch (c)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
|
|
|
case CTL_F:
|
|
|
|
continue;
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
case RIGHT:
|
|
|
|
case F1:
|
|
|
|
if (stored_pos < stored_size && count < size - 1)
|
|
|
|
local_buffer[count++] = echo_char(kp->kb_buf[stored_pos++], sft_out);
|
|
|
|
break;
|
2002-08-05 21:56:38 +02:00
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
case F2:
|
|
|
|
case F4:
|
|
|
|
/* insert/delete up to character c */
|
|
|
|
{
|
|
|
|
unsigned char c2 = read_char_check_break(sft_in);
|
|
|
|
new_pos = stored_pos;
|
|
|
|
if (c2 == 0)
|
|
|
|
{
|
|
|
|
read_char_check_break(sft_in);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char FAR *sp = fmemchr(&kp->kb_buf[stored_pos],
|
|
|
|
c2, stored_size - stored_pos);
|
|
|
|
if (sp != NULL)
|
|
|
|
new_pos = (FP_OFF(sp) - FP_OFF(&kp->kb_buf[stored_pos])) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case F3:
|
|
|
|
if (c != F4) /* not delete */
|
|
|
|
{
|
|
|
|
while (stored_pos < new_pos && count < size - 1)
|
|
|
|
local_buffer[count++] = echo_char(kp->kb_buf[stored_pos++], sft_out);
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
2002-10-22 04:40:19 +02:00
|
|
|
stored_pos = new_pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case F5:
|
|
|
|
fmemcpy(kp->kb_buf, local_buffer, count);
|
|
|
|
stored_size = count;
|
|
|
|
write_char('@', sft_out);
|
|
|
|
goto start_new_line;
|
|
|
|
|
|
|
|
case INS:
|
|
|
|
insert = !insert;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEL:
|
|
|
|
stored_pos++;
|
2000-05-06 21:34:20 +02:00
|
|
|
break;
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
case LEFT:
|
2000-05-06 21:34:20 +02:00
|
|
|
case CTL_BS:
|
|
|
|
case BS:
|
2002-08-05 21:56:38 +02:00
|
|
|
if (count > 0)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-08-05 21:56:38 +02:00
|
|
|
unsigned new_pos;
|
2002-10-22 04:40:19 +02:00
|
|
|
char c2 = local_buffer[--count];
|
|
|
|
if (c2 == HT)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-08-05 21:56:38 +02:00
|
|
|
unsigned i;
|
|
|
|
new_pos = cu_pos;
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
if (local_buffer[i] == HT)
|
|
|
|
new_pos = (new_pos + 8) & ~7;
|
2002-10-22 04:40:19 +02:00
|
|
|
else if (iscntrl(local_buffer[i]))
|
2002-08-05 21:56:38 +02:00
|
|
|
new_pos += 2;
|
|
|
|
else
|
|
|
|
new_pos++;
|
|
|
|
}
|
|
|
|
do
|
2002-10-22 04:40:19 +02:00
|
|
|
destr_bs(sft_out);
|
2002-08-05 21:56:38 +02:00
|
|
|
while (scr_pos > new_pos);
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
2002-08-05 21:56:38 +02:00
|
|
|
else
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-10-22 04:40:19 +02:00
|
|
|
if (iscntrl(c2))
|
|
|
|
destr_bs(sft_out);
|
|
|
|
destr_bs(sft_out);
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
|
|
|
}
|
2002-10-22 04:40:19 +02:00
|
|
|
if (stored_pos > 0)
|
2002-08-05 21:56:38 +02:00
|
|
|
stored_pos--;
|
2000-05-06 21:34:20 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ESC:
|
2002-10-22 04:40:19 +02:00
|
|
|
write_char('\\', sft_out);
|
2002-08-05 21:56:38 +02:00
|
|
|
start_new_line:
|
2002-10-22 04:40:19 +02:00
|
|
|
write_char(CR, sft_out);
|
|
|
|
write_char(LF, sft_out);
|
|
|
|
for (count = 0; count < cu_pos; count++)
|
|
|
|
write_char(' ', sft_out);
|
2002-08-05 21:56:38 +02:00
|
|
|
count = 0;
|
|
|
|
stored_pos = 0;
|
|
|
|
insert = FALSE;
|
2000-05-06 21:34:20 +02:00
|
|
|
break;
|
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
case F6:
|
|
|
|
c = CTL_Z;
|
2001-04-22 00:32:53 +02:00
|
|
|
/* fall through */
|
2002-10-22 04:40:19 +02:00
|
|
|
|
2000-05-06 21:34:20 +02:00
|
|
|
default:
|
2002-10-22 04:40:19 +02:00
|
|
|
if (count < size - 1 || c == CR)
|
|
|
|
local_buffer[count++] = echo_char(c, sft_out);
|
2002-08-05 21:56:38 +02:00
|
|
|
else
|
2002-10-22 04:40:19 +02:00
|
|
|
write_char(BELL, sft_out);
|
2002-08-05 21:56:38 +02:00
|
|
|
if (stored_pos < stored_size && !insert)
|
|
|
|
stored_pos++;
|
2000-05-06 21:34:20 +02:00
|
|
|
break;
|
|
|
|
}
|
2002-10-22 04:40:19 +02:00
|
|
|
} while (c != CR);
|
|
|
|
fmemcpy(kp->kb_buf, local_buffer, count);
|
2002-08-05 21:56:38 +02:00
|
|
|
/* if local_buffer overflows into the CON default buffer we
|
|
|
|
must invalidate it */
|
|
|
|
if (count > LINEBUFSIZECON)
|
|
|
|
kb_buf.kb_size = 0;
|
2002-10-22 04:40:19 +02:00
|
|
|
/* kb_count does not include the final CR */
|
|
|
|
kp->kb_count = count - 1;
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
2001-08-19 14:58:36 +02:00
|
|
|
|
2002-10-22 04:40:19 +02:00
|
|
|
/* called by handle func READ (int21/ah=3f) */
|
|
|
|
size_t read_line_handle(int sft_idx, size_t n, char FAR * bp)
|
2001-08-19 14:58:36 +02:00
|
|
|
{
|
2003-09-04 21:05:27 +02:00
|
|
|
size_t chars_left;
|
2002-08-05 21:56:38 +02:00
|
|
|
|
|
|
|
if (inputptr == NULL)
|
2001-08-19 14:58:36 +02:00
|
|
|
{
|
2002-08-05 21:56:38 +02:00
|
|
|
/* can we reuse kb_buf or was it overwritten? */
|
|
|
|
if (kb_buf.kb_size != LINEBUFSIZECON)
|
|
|
|
{
|
|
|
|
kb_buf.kb_count = 0;
|
|
|
|
kb_buf.kb_size = LINEBUFSIZECON;
|
|
|
|
}
|
2002-10-22 04:40:19 +02:00
|
|
|
read_line(sft_idx, sft_idx, &kb_buf);
|
2003-09-04 21:05:27 +02:00
|
|
|
kb_buf.kb_buf[kb_buf.kb_count + 1] = echo_char(LF, sft_idx);
|
2002-08-05 21:56:38 +02:00
|
|
|
inputptr = kb_buf.kb_buf;
|
|
|
|
if (*inputptr == CTL_Z)
|
|
|
|
{
|
|
|
|
inputptr = NULL;
|
|
|
|
return 0;
|
2003-09-04 21:05:27 +02:00
|
|
|
}
|
2001-08-19 14:58:36 +02:00
|
|
|
}
|
2002-08-05 21:56:38 +02:00
|
|
|
|
2003-09-04 21:05:27 +02:00
|
|
|
chars_left = &kb_buf.kb_buf[kb_buf.kb_count + 2] - inputptr;
|
|
|
|
if (n > chars_left)
|
|
|
|
n = chars_left;
|
2002-08-05 21:56:38 +02:00
|
|
|
|
|
|
|
fmemcpy(bp, inputptr, n);
|
|
|
|
inputptr += n;
|
2003-09-04 21:05:27 +02:00
|
|
|
if (n == chars_left)
|
2002-08-05 21:56:38 +02:00
|
|
|
inputptr = NULL;
|
|
|
|
return n;
|
2001-08-19 14:58:36 +02:00
|
|
|
}
|
2001-11-18 00:26:45 +01:00
|
|
|
|