| 1 |
/* |
| 2 |
zreadline.c - line reading stuff for lrzsz |
| 3 |
Copyright (C) until 1998 Chuck Forsberg (OMEN Technology Inc) |
| 4 |
Copyright (C) 1994 Matt Porter |
| 5 |
Copyright (C) 1996, 1997 Uwe Ohse |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 2, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program; if not, write to the Free Software |
| 19 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 20 |
02111-1307, USA. |
| 21 |
|
| 22 |
originally written by Chuck Forsberg |
| 23 |
*/ |
| 24 |
/* once part of lrz.c, taken out to be useful to lsz.c too */ |
| 25 |
|
| 26 |
#include "zglobal.h" |
| 27 |
|
| 28 |
#include <stdlib.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <signal.h> |
| 31 |
#include <ctype.h> |
| 32 |
#include <errno.h> |
| 33 |
|
| 34 |
#include "error.h" |
| 35 |
|
| 36 |
|
| 37 |
/* Ward Christensen / CP/M parameters - Don't change these! */ |
| 38 |
#define TIMEOUT (-2) |
| 39 |
|
| 40 |
static size_t readline_readnum; |
| 41 |
static int readline_fd; |
| 42 |
static char *readline_buffer; |
| 43 |
int readline_left=0; |
| 44 |
char *readline_ptr; |
| 45 |
|
| 46 |
static RETSIGTYPE |
| 47 |
zreadline_alarm_handler(int dummy) |
| 48 |
{ |
| 49 |
dummy++; /* doesn't need to do anything */ |
| 50 |
} |
| 51 |
|
| 52 |
/* |
| 53 |
* This version of readline is reasonably well suited for |
| 54 |
* reading many characters. |
| 55 |
* |
| 56 |
* timeout is in tenths of seconds |
| 57 |
*/ |
| 58 |
int |
| 59 |
readline_internal(unsigned int timeout) |
| 60 |
{ |
| 61 |
|
| 62 |
if (!no_timeout) |
| 63 |
{ |
| 64 |
unsigned int n; |
| 65 |
n = timeout/10; |
| 66 |
if (n < 2 && timeout!=1) |
| 67 |
n = 3; |
| 68 |
else if (n==0) |
| 69 |
n=1; |
| 70 |
if (Verbose > 5) |
| 71 |
vstringf("Calling read: alarm=%d Readnum=%d ", |
| 72 |
n, readline_readnum); |
| 73 |
signal(SIGALRM, zreadline_alarm_handler); |
| 74 |
alarm(n); |
| 75 |
} |
| 76 |
else if (Verbose > 5) |
| 77 |
vstringf("Calling read: Readnum=%d ", |
| 78 |
readline_readnum); |
| 79 |
|
| 80 |
readline_ptr=readline_buffer; |
| 81 |
readline_left=read(readline_fd, readline_ptr, readline_readnum); |
| 82 |
if (!no_timeout) |
| 83 |
alarm(0); |
| 84 |
if (readline_left>0 && bytes_per_error) { |
| 85 |
static long ct=0; |
| 86 |
static int mod=1; |
| 87 |
ct+=readline_left; |
| 88 |
while (ct>bytes_per_error) { |
| 89 |
readline_ptr[ct % bytes_per_error]^=mod; |
| 90 |
ct-=bytes_per_error; |
| 91 |
mod++; |
| 92 |
if (mod==256) |
| 93 |
mod=1; |
| 94 |
} |
| 95 |
} |
| 96 |
if (Verbose > 5) { |
| 97 |
vstringf("Read returned %d bytes\n", readline_left); |
| 98 |
if (readline_left==-1) |
| 99 |
vstringf("errno=%d:%s\n", errno,strerror(errno)); |
| 100 |
if (Verbose > 9 && readline_left>0) { |
| 101 |
int i,j; |
| 102 |
j=readline_left > 48 ? 48 : readline_left; |
| 103 |
vstring(" "); |
| 104 |
for (i=0;i<j;i++) { |
| 105 |
if (i%24==0 && i) |
| 106 |
vstring("\n "); |
| 107 |
vstringf("%02x ", readline_ptr[i] & 0377); |
| 108 |
} |
| 109 |
vstringf("\n"); |
| 110 |
} |
| 111 |
} |
| 112 |
if (readline_left < 1) |
| 113 |
return TIMEOUT; |
| 114 |
--readline_left; |
| 115 |
return (*readline_ptr++ & 0377); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
void |
| 121 |
readline_setup(int fd, size_t readnum, size_t bufsize) |
| 122 |
{ |
| 123 |
readline_fd=fd; |
| 124 |
readline_readnum=readnum; |
| 125 |
readline_buffer=malloc(bufsize > readnum ? bufsize : readnum); |
| 126 |
if (!readline_buffer) |
| 127 |
error(1,0,_("out of memory")); |
| 128 |
} |
| 129 |
|
| 130 |
void |
| 131 |
readline_purge(void) |
| 132 |
{ |
| 133 |
readline_left=0; |
| 134 |
return; |
| 135 |
} |
| 136 |
|