util.c
3.19 KB
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
#include <stdio.h>
getline(s,lim,fp)
char s[];
FILE *fp;
int lim;
{
int c,i;
for (i=0; i<lim-1 && (c=getc(fp)) != EOF && c != '\n'; ++i)
{
s[i] = c;
}
if (c == '\n')
{
s[i] = '\0';
++i;
}
return(i);
}
dastodi(buf)
char buf[];
{
int i;
long n;
n = 0;
for (i=0; buf[i] != '\0'; ++i)
n = n * 10 + buf[i] - '0';
return(n);
}
bastodi(buf)
char buf[];
{
int i;
long n;
n = 0;
for (i=0; buf[i] != '\0'; ++i)
n = n * 2 + buf[i] - '0';
return(n);
}
hastodi(buf) /* hex char to decimal integer */
char buf[];
{
int i;
long n;
n = 0;
for (i=0; buf[i] != '\0'; ++i)
{
if (buf[i] >='0' && buf[i] <='9')
n = n * 16 + buf[i] - '0';
else
if (buf[i] >='A' && buf[i] <='F')
n = n * 16 + buf[i] - 55;
else
n = n * 16 + buf[i] - 87;
}
return(n);
}
itochst(value,buf)
unsigned long value;
char buf[];
{
int i,n;
for (i = 0;i<=7;++i)
buf[i] = '0';
buf[8] = '\0';
i = 7;
do
{
n = (value % 16)&0xF;
if (n<=9)
buf[i] = n + '0';
else
buf[i] = n - 10 + 'A';
i-=1;
} while ((value /= 16) >0);
}
itobichst(value,buf,len)
unsigned long value;
char buf[];
{
int i,n;
for (i = 0;i<len;++i)
buf[i] = '0';
buf[len] = '\0';
i = len-1;
do
{
n = (value % 2)&0x1;
buf[i] = n + '0';
i-=1;
} while ((value /= 2) >0);
}
getnum(numbuf_in,err)
short *err;
char numbuf_in[];
{
char buf[50];
char numbuf[50];
int i,k;
long n;
*err = 0;
if (numbuf_in[0] == '\0')
{
*err = 2;
return(0);
}
else
{
for (i=0,k=0; numbuf_in[i] != '\0'; ++i)
if (numbuf_in[i]!='_') numbuf[k++] = numbuf_in[i];
numbuf[k] = '\0';
/* hex number */
if (numbuf[0] == '0' && (numbuf[1] == 'x' || numbuf[1] == 'X'))
{
for (i=2,k=0;(numbuf[i] >= '0' && numbuf[i] <= '9') || (numbuf[i] >= 'a' && numbuf[i] <= 'f') || (numbuf[i] >= 'A' && numbuf[i] <= 'F');++i,++k)
buf[k]=numbuf[i];
buf[k] = '\0';
if (numbuf[i] != '\0' || k>8)
*err = 1;
else
return(hastodi(&buf[0]));
}
else
/* bin number */
if (numbuf[0] == '0' && (numbuf[1] == 'b' || numbuf[1] == 'B'))
{
for (i=2,k=0;(numbuf[i] == '0' || numbuf[i] == '1');++i,++k)
buf[k]=numbuf[i];
buf[k] = '\0';
if (numbuf[i] != '\0' || k>32)
*err = 1;
else
return(bastodi(&buf[0]));
}
else
/* dec number */
{
for (i=0,k=0;numbuf[i] >= '0' && numbuf[i] <= '9';++i,++k)
buf[k]=numbuf[i];
buf[k] = '\0';
if (numbuf[i] != '\0' || k>10)
*err = 1;
else
return(dastodi(&buf[0]));
}
}
}
err_print(lnum,ertype)
int lnum;
short ertype;
{
static char *errtab[] = {
"", /*0*/
"invalid operation", /*1*/
"count value incorrect", /*2*/
"direction field is missing", /*3*/
"direction value is incorrect", /*4*/
"size value is missing ", /*5*/
"size value is incorrect", /*6*/
"address is missing", /*7*/
"address value is incorrect", /*8*/
"data0,1 are missing", /*9*/
"data0,1 are incorrect", /*10*/
"Wcount/command value incorrect", /*11*/
"Interrupt level is missing/wrong", /*12*/
"RMI_Mem/Boot_mode Directive missing/wrong", /*13*/
};
printf("error line: %4d ----- %s error no: %2d\n",
lnum, errtab[ertype], ertype);
}