-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
192 lines (173 loc) · 3.12 KB
/
util.c
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
/*
* j-random utility functions
* these are mostly prime candidates for assembly code
*/
#include "ccc.h"
#define PSIZE 80 // max string containing bitdefs
#define NPATS 2 // and 2 per printf
char patspace[PSIZE * NPATS];
char patoff;
/*
* byte bitoff formatter.
* usage: char *foo = { "0", "1", "2", "3", 0, "5", "6", "7" };
* printf("%s %s\n", bitdef(0xa, foo), bitdef(0x83, foo));
* null pointer for defs is fine.
*/
char *
bitdef(unsigned char v, char **defs)
{
int i;
char *patptr;
int sep = 0;
patptr = &patspace[PSIZE * patoff];
patoff = (patoff + 1) % NPATS;
*patptr = 0;
if (defs) {
for (i = 0; i < 8; i++) {
if ((v & (1 << i)) && defs[i]) {
if (sep++) {
strcat(patptr, ",");
}
strcat(patptr, defs[i]);
}
}
}
return patptr;
}
/*
* append string s at d
*/
void
append(char *d, char *s)
{
while (*s) {
*d++ = *s++;
}
*d = 0;
}
/*
* change a binary character into a form we can print
*/
static int
controlify(char *d, char c)
{
int ret = 0;
char digit;
if (c == '\n') {
append(d, "\\n");
ret = 3;
} else if (c == '\\') {
append(d, "\\");
ret = 2;
} else if ((c < ' ') || (c >= 0x7f)) {
int shift;
*d++ = '\\';
*d++ = '0';
ret = 2;
for (shift = 6; shift >= 0; shift -= 3) {
digit = (c >> shift) & 0x7;
if ((ret == 2) && (digit == 0)) {
continue;
}
*d++ = '0' + digit;
ret++;
}
*d = 0;
} else {
*d++ = c;
ret = 1;
}
return ret;
}
/*
* format an integer for output
*/
int
longout(char *d, long v)
{
int shift = 28;
int ret = 2;
int nibble;
*d++ = '0';
*d++ = 'x';
for (shift = 28; shift >= 0; shift -= 4) {
nibble = (v >> shift) & 0xf;
if (shift && (ret == 2) && (nibble == 0)) { // skip leading zeros
continue;
}
ret++;
*d++ = (nibble > 9) ? (nibble - 0xa) + 'a' : nibble + '0';
}
return ret;
}
/*
* output a string in a form that we can emit as source code
* this means escaping control characters
*/
int
quoted_string(char *d, char *s)
{
int len = *s++;
int ret = 1;
d[0] = '\"';
while (len--) {
ret += controlify(&d[ret], *s++);
}
d[ret] = '\"';
return ++ret;
}
int
iswhite(char c)
{
switch (c) {
case ' ': case '\t': case '\n':
return 1;
default:
return 0;
}
}
char xxbuf[200];
void
hexdump(char *tag, char *h, int l)
{
int i;
char *z = xxbuf;
char c;
strcpy(xxbuf, tag);
for (i = 0; i < l; i++) {
c = h[i];
if ((i % 16) == 0) {
printf(" %s\n%04x ", xxbuf, i);
z = xxbuf;
*z = 0;
}
printf("%02x ", c);
if ((i % 4) == 3) printf(" ");
if ((c < ' ') || (c > 0x7e)) c = '.';
*z++ = c;
*z = 0;
}
while ((i++ % 16) != 0) {
if ((i % 4) == 3) printf(" ");
printf(" ");
}
printf(" %s\n", xxbuf);
}
/*
* return the index in an array of the first occurrance of a char
* return 0xff for miss
*/
unsigned char
lookupc(char *s, char c)
{
int i;
for (i = 0; s[i]; i++) {
if (c == s[i]) {
return i;
}
}
return 0xff;
}
/*
* vim: tabstop=4 shiftwidth=4 expandtab:
*/