File: hz2gb.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 (150 lines) | stat: -rw-r--r-- 2,818 bytes parent folder | download | duplicates (2)
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
/* $Id: hz2gb.c,v 2.0 1995/01/29 08:10:04 ygz Exp $ */

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

#include "io.h"

#define	hzb(c)	((((c) & 0x7f) >= 0x21) && (((c) & 0x7f) <= 0x7e))

#define	S_ASC	0
#define	S_PHZ	1
#define	S_HZ	2
#define	S_HZ2	3
#define	S_HZA	4
#define	S_HZT	5
#define	S_PZW	6
#define	S_ZW	7
#define	S_ZW2	8
#define	S_ZWA	9

static int state[MAX_MODULE];
static int eoln[MAX_MODULE];

static char hzdecode(c, inst)
     char c;
     int inst;	/* instant number */
{
	switch (state[inst]) {
	  case S_ASC:
		switch (c) {
		  case '~':	state[inst] = S_PHZ;  break;
		  case 'z':	state[inst] = (eoln[inst] ? S_PZW : S_ASC);
				break;
		  default:	state[inst] = S_ASC;  break;
		}
		break;

	  case S_PHZ:
		switch (c) {
		  case '{':	state[inst] = S_HZ;   break;
		  default:	state[inst] = S_ASC;  break;
		}
		break;
	  case S_HZ:
		switch (c) {
		  case '~':	state[inst] = S_HZT;   break;
		  case ' ':	state[inst] = S_HZA;   break;
		  case '\b':	state[inst] = S_HZ2;   break;
		  default:	if (hzb(c)) {
					c |= 0x80;  state[inst] = S_HZ2;
				} else  state[inst] = S_ASC;
				break;
		}
		break;
	  case S_HZA:
		state[inst] = S_HZ;
		break;
	  case S_HZ2:
		switch (c) {
		  case ' ':
		  case '\b':	state[inst] = S_HZ;   break;
		  default:	if (hzb(c)) {
					c |= 0x80;  state[inst] = S_HZ;
				} else  state[inst] = S_ASC;
				break;
		}
		break;
	  case S_HZT:
		switch (c) {
		  case '}':	state[inst] = S_ASC;  break;
		  default:	state[inst] = S_HZ;   break;
		}
		break;

	  case S_PZW:
		switch (c) {
		  case 'W':	state[inst] = S_ZW;   break;
		  default:	state[inst] = S_ASC;  break;
		}
		break;
	  case S_ZW:
		switch (c) {
		  case ' ':	state[inst] = S_ZWA;   break;
		  case '\n':	state[inst] = S_ASC;   break;
		  case '\r':	state[inst] = S_ASC;   break;
		  default:	if (hzb(c)) {
					c |= 0x80;  state[inst] = S_ZW2;
				} else  state[inst] = S_ASC;
				break;
		}
		break;
	  case S_ZWA:	state[inst] = S_ZW;  break;
	  case S_ZW2:
		switch (c) {
		  case ' ':
		  case '\b':	state[inst] = S_ZW;   break;
		  default:	if (hzb(c)) {
					c |= 0x80;  state[inst] = S_ZW;
				} else  state[inst] = S_ASC;
				break;
		}
		break;
	}
	return (c);
}



char *hz2gb (s,plen,inst)
     char *s;
     int *plen;
     int inst;
{
  int i;

	for (i = 0; i < *plen; i++) {
		s[i] = hzdecode (s[i], inst);
		eoln[inst] = (s[i] == '\n' || s[i] == '\r') ? 1 : 0;
	}
	return (s);
}

char *gb2hz (s,plen,inst)
     char *s;
     int *plen;
     int inst;
{
  register int i;

	for (i = 0; i < *plen; i++)
		s[i] &= 0x7f;
	return (s);
}

int hz2gb_init (arg)
     char *arg;
{
  static hz2gb_inst = 0;

	eoln[hz2gb_inst] = 0;
	state[hz2gb_inst] = S_ASC;
	return (hz2gb_inst++);
}

int gb2hz_init (arg)
     char *arg;
{
	return (0);
}