aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/postscript/tr2post/Bgetfield.c
blob: 974fce62b800a983b078575ffe532a7c91d50fac (plain)
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
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "../common/common.h"
#include "tr2post.h"

int
isspace(Rune r)
{
	return(r==' ' || r=='\t' || r=='\n' || r == '\r' || r=='\f');
}

int
Bskipws(Biobufhdr *bp) {
	int r;
	char c[UTFmax];
	int sindex = 0;

	/* skip over initial white space */
	do {
		r = Bgetrune(bp);
		if (r == '\n') inputlineno++;
		sindex++;	
	} while (r>=0 && isspace(r));
	if (r<0) {
		return(-1);
	} else if (!isspace(r)) {
		Bungetrune(bp);
		--sindex;
	}
	return(sindex);
}

int
asc2dig(char c, int base) {
	if (c >= '0' && c <= '9'){
		if (base == 8 && c > '7') return(-1);
		else return(c - '0');
	}

	if (base == 16){
		if (c >= 'a' && c <= 'f') return(10 + c - 'a');
		else if (c >= 'A' && c <= 'F') return(10 + c - 'A');
	}

	return(-1);
}

/* get a string of type: "d" for decimal integer, "u" for unsigned,
 * "s" for string", "c" for char, 
 * return the number of characters gotten for the field.  If nothing
 * was gotten and the end of file was reached, a negative value
 * from the Bgetrune is returned.
 */

int
Bgetfield(Biobufhdr *bp, int type, void *thing, int size) {
	int r;
	Rune R;
	char c[UTFmax];
	int sindex = 0, i, j, n = 0;
	int negate = 0;
	int base = 10;
	BOOLEAN bailout = FALSE;
	int dig;
	unsigned int u = 0;

	/* skip over initial white space */
	if (Bskipws(bp) < 0)
		return(-1);

	switch (type) {
	case 'd':
		while (!bailout && (r = Bgetrune(bp))>=0) {
			switch (sindex++) {
			case 0:
				switch (r) {
				case '-':
					negate = 1;
					continue;
				case '+':
					continue;
				case '0':
					base = 8;
					continue;
				default:	
					break;
				}
				break;
			case 1:
				if ((r == 'x' || r == 'X') && base == 8) {
					base = 16;
					continue;
				}
			}
			if ((dig = asc2dig(r, base)) == -1) bailout = TRUE;						
			else n = dig + (n * base);
		}
		if (r < 0) return(-1);
		*(int *)thing = (negate)?-n:n;
		Bungetrune(bp);
		break;
	case 'u':
		while (!bailout && (r = Bgetrune(bp))>=0) {
			switch (sindex++) {
			case 0:
				if (*c == '0') {
					base = 8;
					continue;
				}
				break;
			case 1:
				if ((r == 'x' || r == 'X') && base == 8) {
					base = 16;
					continue;
				}
			}
			if ((dig = asc2dig(r, base)) == -1) bailout = TRUE;						
			else u = dig + (n * base);
		}
		*(int *)thing = u;
		if (r < 0) return(-1);
		Bungetrune(bp);
		break;
	case 's':
		j = 0;
		while ((size>j+UTFmax) && (r = Bgetrune(bp))>=0 && !isspace(r)) {
			R = r;
			i = runetochar(&(((char *)thing)[j]), &R);
			j += i;
			sindex++;
		}
		((char *)thing)[j++] = '\0';
		if (r < 0) return(-1);
		Bungetrune(bp);
		break;
	case 'r':
		if ((r = Bgetrune(bp))>=0) {
			*(Rune *)thing = r;
			sindex++;
			return(sindex);
		}
		if (r <= 0) return(-1);
		Bungetrune(bp);
		break;
	default:
		return(-2);
	}
	if (r < 0 && sindex == 0)
		return(r);
	else if (bailout && sindex == 1) {
		return(0);
	} else
		return(sindex);
}