xref: /OK3568_Linux_fs/u-boot/common/zreadline.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 extern int read_data(int tout_in_100ms, char *buf, int size);
46 
47 RETSIGTYPE
zreadline_alarm_handler(int dummy LRZSZ_ATTRIB_UNUSED)48 zreadline_alarm_handler(int dummy LRZSZ_ATTRIB_UNUSED)
49 {
50 	/* doesn't need to do anything */
51 }
52 
53 /*
54  * This version of readline is reasonably well suited for
55  * reading many characters.
56  *
57  * timeout is in tenths of seconds
58  */
59 int
readline_internal(unsigned int timeout)60 readline_internal(unsigned int timeout)
61 {
62 	unsigned int n=0;
63 
64 	if (!no_timeout)
65 	{
66 		n = timeout/10;
67 		if (n < 2 && timeout!=1)
68 			n = 3;
69 		else if (n==0)
70 			n=1;
71 		if (Verbose > 5)
72 			vstringf("Calling read: alarm=%d  Readnum=%d ",
73 			  n, (u32)readline_readnum);
74 		//signal(SIGALRM, zreadline_alarm_handler);
75 		//alarm(n);
76 	}
77 	else if (Verbose > 5)
78 		vstringf("Calling read: Readnum=%d ",
79 		  (u32)readline_readnum);
80 
81 	readline_ptr=readline_buffer;
82 	readline_left=read_data(n*10, readline_ptr, readline_readnum);
83 	if (!no_timeout)
84 		;//alarm(0);
85 	if (readline_left>0 && bytes_per_error) {
86 		static long ct=0;
87 		static int mod=1;
88 		ct+=readline_left;
89 		while (ct>bytes_per_error) {
90 			readline_ptr[ct % bytes_per_error]^=mod;
91 			ct-=bytes_per_error;
92 			mod++;
93 			if (mod==256)
94 				mod=1;
95 		}
96 	}
97 	if (Verbose > 5) {
98 		vstringf("Read returned %d bytes\n", readline_left);
99 		//if (readline_left==-1)
100 		//	vstringf("errno=%d:%s\n", errno,strerror(errno));
101 		if (Verbose > 9 && readline_left>0) {
102 			int i,j;
103 			j=readline_left > 48 ? 48 : readline_left;
104 			vstring("    ");
105 			for (i=0;i<j;i++) {
106 				if (i%24==0 && i)
107 					vstring("\n    ");
108 				vstringf("%02x ", readline_ptr[i] & 0377);
109 			}
110 			vstringf("\n");
111 		}
112 	}
113 	if (readline_left < 1)
114 		return TIMEOUT;
115 	--readline_left;
116 	return (*readline_ptr++ & 0377);
117 }
118 
119 
120 
121 void
readline_setup(int fd,size_t readnum,size_t bufsize)122 readline_setup(int fd, size_t readnum, size_t bufsize)
123 {
124 	readline_fd=fd;
125 	readline_readnum=readnum;
126 	readline_buffer=malloc(bufsize > readnum ? bufsize : readnum);
127 	//if (!readline_buffer)
128 	//	error(1,0,_("out of memory"));
129 }
130 
131 void
readline_purge(void)132 readline_purge(void)
133 {
134 	readline_left=0;
135 	return;
136 }
137 
138