-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmk-mk-ctxt
executable file
·92 lines (79 loc) · 1.56 KB
/
mk-mk-ctxt
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
#!/bin/sh
fatal()
{
echo "mk-mk-ctxt: fatal: $1" 1>&2
exit 1
}
cleanup()
{
rm -f mk-ctxt.c mk-ctxt.o
}
#
# check if base directory was specified
#
BASE_DIR="."
echo "$1" | grep '^@' 2>&1 >/dev/null
if [ $? -eq 0 ]
then
BASE_DIR=`echo $1 | sed 's/^@//g'`
shift
fi
CC=`head -n 1 ${BASE_DIR}/conf-cc`
if [ $? -ne 0 ]
then
fatal "could not read ${BASE_DIR}/conf-cc"
fi
LD=`head -n 1 ${BASE_DIR}/conf-ld`
if [ $? -ne 0 ]
then
fatal "could not read ${BASE_DIR}/conf-ld"
fi
(cat << EOF
#include <stdio.h>
#include <unistd.h>
char buf[1024];
int main(int argc, char **argv)
{
char *name;
unsigned long bytes = 0;
unsigned int line = 0;
long num;
long r;
long i;
int ch;
int done = 0;
if (argc != 2) return 100;
name = argv[1];
printf("char %s[] = {\n", name);
while (!done) {
r = read(0, buf, sizeof(buf));
if (r == 0) done = 1;
if (r == -1) return 112;
if (r != sizeof(buf)) {
for (i = r; i > 0; --i) {
if (buf[i] == '\n') {
buf[i] = 0;
break;
}
}
}
num = 0;
while (num != r) {
ch = buf[num];
++num;
++bytes;
printf("0x%.2x, ", ch);
line += 6;
if (line >= 72) { line = 0; printf("\n"); }
}
}
printf("0x00,\n};\n");
printf("const unsigned int %s_len = %u;\n", name, bytes);
if (fflush(stdout) == -1) return 111;
return 0;
}
EOF
) > mk-ctxt.c || fatal "could not create mk-ctxt.c"
${CC} -c -o mk-ctxt.o mk-ctxt.c || fatal "could not compile mk-ctxt.c"
${LD} -o mk-ctxt mk-ctxt.o || fatal "could not link mk-ctxt"
cleanup