File: gentbl.c

package info (click to toggle)
fdclone 3.01b-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 4,772 kB
  • ctags: 8,674
  • sloc: ansic: 100,552; makefile: 4,497; sh: 1,480; sed: 224
file content (165 lines) | stat: -rw-r--r-- 2,513 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
/*
 *	gentbl.c
 *
 *	table generator module
 */

#include "headers.h"
#include "gentbl.h"

#define	MAXBYTES		8

int textmode = 0;

static int textlen = 0;
static int textnext = 0;


FILE *opentbl(path)
CONST char *path;
{
	FILE *fp;

	if (!(fp = fopen(path, (textmode) ? "w" : "wb"))) {
		fprintf(stderr, "%s: Cannot open.\n", path);
		return(NULL);
	}

	return(fp);
}

int fputheader(name, fp)
CONST char *name;
FILE *fp;
{
	if (!textmode) return(0);
	if (fprintf(fp, "#include \"%s.h\"\n", name) < 0 && ferror(fp))
		return(-1);

	return(0);
}

int fputbegin(name, fp)
CONST char *name;
FILE *fp;
{
	if (!textmode) return(0);
	textnext = textlen = 0;
	if (fprintf(fp, "CONST u_char %s[] = {", name) < 0 && ferror(fp))
		return(-1);

	return(0);
}

int fputend(fp)
FILE *fp;
{
	if (!textmode) return(0);

	if (!textnext) {
		fputbyte('\0', fp);
		fputs("\t/* dummy entry */", fp);
	}

	return((fputs("\n};\n", fp) == EOF && ferror(fp)) ? -1 : 0);
}

int fputbyte(c, fp)
int c;
FILE *fp;
{
	CONST char *sep1, *sep2;

	if (!textmode) return((fputc(c, fp) == EOF && ferror(fp)) ? -1 : 0);

	sep1 = (textnext) ? "," : "";
	sep2 = (textlen) ? " " : "\n\t";

	textnext = 1;
	if (++textlen >= MAXBYTES) textlen = 0;

	if (fprintf(fp, "%s%s0x%02x", sep1, sep2, c & 0xff) < 0 && ferror(fp))
		return(-1);

	return(0);
}

int fputword(w, fp)
u_int w;
FILE *fp;
{
	if (fputbyte((int)(w & 0xff), fp) < 0
	|| fputbyte((int)((w >> 8) & 0xff), fp) < 0)
		return(-1);

	return(0);
}

int fputdword(dw, fp)
long dw;
FILE *fp;
{
	if (fputword((u_int)(dw & 0xffff), fp) < 0
	|| fputword((u_int)((dw >> 16) & 0xffff), fp) < 0)
		return(-1);

	return(0);
}

int fputbuf(buf, size, fp)
CONST u_char *buf;
ALLOC_T size;
FILE *fp;
{
	ALLOC_T n;

	if (!textmode) {
		if (fwrite(buf, size, (ALLOC_T)1, fp) != (ALLOC_T)1)
			return(-1);
	}
	else for (n = (ALLOC_T)0; n < size; n++)
		if (fputbyte((int)(buf[n]), fp) < 0) return(-1);

	return(0);
}

int fputlength(name, len, fp, width)
CONST char *name;
long len;
FILE *fp;
int width;
{
	CONST char *cp;

	if (!textmode) {
		switch (width) {
			case 2:
				if (fputword(len, fp) < 0) return(-1);
				break;
			case 4:
				if (fputdword(len, fp) < 0) return(-1);
				break;
			default:
				if (fputbyte(len, fp) < 0) return(-1);
				break;
		}
		return(0);
	}

	switch (width) {
		case 2:
			cp = "u_int";
			break;
		case 4:
			cp = "long";
			break;
		default:
			cp = "int";
			break;
	}

	if (fprintf(fp, "%s %s = %ld;\n", cp, name, len) < 0 && ferror(fp))
		return(-1);

	return(0);
}