File: io.c

package info (click to toggle)
hztty 2.0-5.2woody2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,028 kB
  • ctags: 295
  • sloc: ansic: 26,960; makefile: 162; sh: 109
file content (238 lines) | stat: -rw-r--r-- 6,241 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* $Id: io.c,v 2.0 1995/01/29 08:10:04 ygz Exp $ */

/* Copyright 1992	Yongguang Zhang	*/

#ifndef lint
static char *rcs_id="$Id: io.c,v 2.0 1995/01/29 08:10:04 ygz Exp $";
#endif /* lint */

#include "config.h"
#include "io.h"

static char *dummy(s) char *s; { return (s); }
static int dummy_init() { return (0); }

/* all implemented modules for io streams are listed here */
struct mod_def moduleTable[] = {

  {	"gb2hz",	gb2hz,		gb2hz_init,	0,
	ENC_GB,		ENC_HZ,		"GB => HZ" },
  {	"hz2gb",	hz2gb,		hz2gb_init,	0,
	ENC_HZ,		ENC_GB,		"HZ => GB" },

#ifdef HZTTY_GB_BIG5
  {	"gb2big",	gb2big,		gb2big_init,	0,
	ENC_GB,		ENC_BIG5,	"GB => Big5" },
  {	"big2gb",	big2gb,		big2gb_init,	0,
	ENC_BIG5,	ENC_GB,		"Big5 => GB" },
#endif

  {	"uni-utf",	uni_utf8,	uni_utf8_init,	0,
	ENC_UNICODE,	ENC_UTF8,	"Unicode (16bit) => UTF-8" },
  {	"utf-uni",	utf8_uni,	utf8_uni_init,	0,
	ENC_UTF8,	ENC_UNICODE,	"UTF-8 => Unicode (16bit)" },

  {	"uni-utf7",	uni_utf7,	uni_utf7_init,	0,
	ENC_UNICODE,	ENC_UTF7,	"Unicode (16bit) => UTF7 (mail-safe)" },
  {	"utf7-uni",	utf7_uni,	utf7_uni_init,	0,
	ENC_UTF7,	ENC_UNICODE,	"UTF7 (mail-safe) => Unicode (16bit)" },

# ifdef HZTTY_UNICODE_GB
  {	"gb2uni",	gb2uni,		gb2uni_init,	0,
	ENC_GB,		ENC_UNICODE,	"GB => Unicode" },
  {	"uni2gb",	uni2gb,		uni2gb_init,	0,
	ENC_UNICODE,	ENC_GB,		"Unicode => GB" },
# endif

# ifdef HZTTY_UNICODE_BIG5
  {	"big2uni",	big2uni,	big2uni_init,	0,
	ENC_BIG5,	ENC_UNICODE,	"Big5 => Unicode" },
  {	"uni2big",	uni2big,	uni2big_init,	0,
	ENC_UNICODE,	ENC_BIG5,	"Unicode => Big5" },
# endif

  {	"toascii",	to_ascii,	toascii_init,	0,
	ENC_ANY,	ENC_ANY,	"Strip the 8th bit" },
  {	"toupper",	to_upper,	toupper_init,	0,
	ENC_ANY,	ENC_ANY,	"make everything upper-case" },
  {	"tolower",	to_lower,	tolower_init,	0,
	ENC_ANY,	ENC_ANY,	"make everything lower-case" },

  {	"rot13",	rot13,		rot13_init,	0,
	ENC_ANY,	ENC_ANY,	"ROT-13 (don't try this at home :-)" },

  {	"log",		io_log,		log_init,	1,
	ENC_ANY,	ENC_ANY,	"keep a log in file (arg: filename)" },

  {	"dummy",	dummy,		dummy_init,	0,
	ENC_ANY,	ENC_ANY,	"does nothing" },
  {	"",		dummy,		dummy_init,	0,
	ENC_ANY,	ENC_ANY,	"" },
  {	NULL,		dummy,		dummy_init,	0,
	ENC_ANY,	ENC_ANY,	NULL }
};

/* input/output stream */
struct io_stream {
	struct io_module {
		char *(*func)();	/* the conversion function */
		int inst;		/* instant number for the function */
	} st[MAX_MODULE];
	int len;			/* number of modules in the stream */
} in_stream, out_stream;

static int stream_init (str, pstream)
     char *str;
     struct io_stream *pstream;
{
  char *pstr = str;
  char *name, *arg;
  int more_module, has_arg;
  struct mod_def *pmod;
  int inst;

	pstream->len = 0;
	if ( (!pstr) || (! *pstr) || ((*pstr == '-') && (*(pstr+1) == '\0')) )
		return (0);

	do {
		/*
		 * A stream definition string looks like:
		 *    <stream>  ::=  <module> { ':' <module> }
		 *    <module>  ::=  <module_name> [ '(' <arguments> ')' ]
		 */

		name = pstr;
		for (; (*pstr && (*pstr != ':')); pstr++)
			/* empty */ ;
		more_module = (*pstr == ':') ? 1 : 0;	/* more modules? */
		*pstr++ = '\0';		/* null-terminate the module */

		for (arg = name; (*arg) && (*arg != '('); arg++)
			/* empty */ ;
		has_arg = ((*arg == '(') ? 1 : 0);	/* has arguments? */
		*arg++ = '\0';		/* null-terminate the module_name */

		/* search for the name in the module table */
		for (pmod = moduleTable; pmod->name; pmod++)
			if (strcmp (name, pmod->name) == 0)
				break;
		if (! pmod->name) {	/* not found */
			fprintf (stderr, "no such module \"%s\"\n", name);
			return (-1);
		}
		if (pmod->needarg != has_arg) {
			fprintf (stderr, "\"%s:%s\" argument mismatch\n",
				 name, arg);
			return (-1);
		}

		pstream->st[pstream->len].func = pmod->func;
		if (pmod->needarg) {
			/* remember that arg end with a ')' */
			arg[strlen(arg) - 1] = '\0';
			inst = (*pmod->f_init)(arg);
		} else
			inst = (*pmod->f_init)(NULL);
		if (inst < 0) {
			fprintf (stderr, "fail to initialize \"%s\"\n", name);
			return (-1);
		}
		pstream->st[pstream->len].inst = inst;
		pstream->len++;

	} while (more_module);
	return (0);
}


int in_stream_setup (idef)
     char *idef;
{
  char defstr[256];

       strncpy (defstr, (idef ? idef : ""), sizeof(defstr));
	return (stream_init (defstr, &in_stream));
}

int out_stream_setup (odef)
     char *odef;
{
  char defstr[256];

       strncpy (defstr, (odef ? odef : ""), sizeof(defstr));
	return (stream_init (defstr, &out_stream));
} 

/*
 * Conversion buffer.  One for both directions.  They are in different
 * address space anyway.  Set the area to be at the middle of the buffer.
 * Just play safe
 */
static char conv_buf[BUFSIZ*3];
static char *buf = conv_buf + BUFSIZ;

/* pty --> stdout, do out_stream */
int stream_write (fd,s,len)
     int fd;
     char *s;
     int len;
{
  int i;
  char *p = buf;		/* point to the buffer for conversion */

	bcopy (s, p, len);	/* copy in */
	for (i = 0; i < out_stream.len; i++)
		p = (* out_stream.st[i].func)(p, &len, out_stream.st[i].inst);
	return ((len != 0) ? write (fd, p, len) : 0);
}

/* stdin --> pty, do in_stream */
int stream_read (fd,s,len)
     int fd;
     char *s;
     int len;
{
  int i, cc;
  char *p = buf;		/* point to the buffer for conversion */

	cc = read (fd, p, len);
	if (cc <= 0)
		return (-1);
	for (i = 0; i < in_stream.len; i++)
		p = (* in_stream.st[i].func)(p, &cc, in_stream.st[i].inst);
	if (cc != 0)
		bcopy (p, s, cc);	/* copy out */
	return (cc);
}

char *hzconvert (s, plen, psaved, dbcvrt)
     char *s;
     int *plen;
     char *psaved;		/* unprocessed char buffer pointer */
     void (*dbcvrt)();		/* 2-byte conversion func for a hanzi */
{
  char *p, *pend;

	if (*plen == 0)
		return (s);
        if (*psaved) {			/* previous buffered char */
                *(--s) = *psaved;	/* put the unprocessed char down */
		(*plen) ++;
		*psaved = 0;		/* clean this char buffer */
        }
	p = s;  pend = s + (*plen);	/* begin/end of the buffer string */
	while (p < pend) {
		if ((*p) & 0x80)	/* hi-bit on: hanzi */
			if (p < pend-1) 	/* not the last one */
				dbcvrt (p++);
			else {			/* the end of string */
				*psaved = *p;	/* save the unprocessed char */
				(*plen) --;
				break;
			}
		p++;
        }
        return (s);
}