This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatictab.c
137 lines (110 loc) · 2.68 KB
/
statictab.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
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include "init.h"
#include "init_conf.h"
/* to keep the linker happy */
int currlevel;
int state;
struct config* cfg;
extern struct memblock newblock;
extern char* inittab;
extern int configure(int strict);
void die(const char* msg, ...);
int warn(const char* msg, ...);
char* quote(const char* val);
void dump_config(struct config* cfg);
void dump_tab(const char* var, struct initrec** inittab);
void dump_env(const char* var, char** list);
void dump_rec(const char* var, int i, struct initrec* p);
int main(int argc, char** argv)
{
if(argc < 2)
die("Bad call\n");
inittab = argv[1];
if(configure(STRICT))
die("Failed to compile inittab\n");
dump_config((struct config*)newblock.addr);
return 0;
}
void dump_config(struct config* cfg)
{
printf("#include \"init.h\"\n\n");
printf("#define NULL ((void*)0)\n"); /* eh */
dump_tab("builtin_tab", cfg->inittab);
dump_env("builtin_env", cfg->env);
printf("static struct config builtin = {\n");
printf("\t.inittab = builtin_tab + 1,\n");
printf("\t.initnum = %i,\n", cfg->initnum);
printf("\t.env = builtin_env,\n");
printf("};\n\n");
printf("struct config* cfg = &builtin;\n");
}
void dump_env(const char* var, char** list)
{
char** p;
printf("static char* %s[] = {\n", var);
for(p = list; *p; p++)
printf("\t%s,\n", quote(*p));
printf("\tNULL\n");
printf("};\n\n");
}
void dump_tab(const char* var, struct initrec** inittab)
{
const char* rec = "irec";
struct initrec *p, **pp;
int i, n;
for(i = 0, pp = inittab; (p = *pp); pp++, i++)
dump_rec(rec, i, p);
n = i;
printf("static struct initrec* %s[] = {\n", var);
printf("\tNULL,\n");
for(i = 0; i < n; i++)
printf("\t&%s%i,\n", rec, i);
printf("\tNULL\n");
printf("};\n\n");
}
void dump_rec(const char* var, int i, struct initrec* p)
{
char** a;
printf("static struct initrec %s%i = {\n", var, i);
printf("\t.name = %s,\n", quote(p->name));
printf("\t.rlvl = %i,\n", p->rlvl);
printf("\t.flags = %i,\n", p->flags);
printf("\t.pid = %i,\n", p->pid);
printf("\t.lastrun = %li,\n", (long)p->lastrun);
printf("\t.lastsig = %li,\n", (long)p->lastsig);
printf("\t.argv = { ");
for(a = p->argv; *a; a++)
// no quoting for now
printf("\"%s\", ", *a);
printf(" NULL }\n");
printf("};\n");
}
char* quote(const char* val)
{
static char buf[1024];
int len = strlen(val);
buf[0] = '"';
strncpy(buf + 1, val, 1000);
buf[len+1] = '"';
buf[len+2] = '\0';
return buf;
}
void die(const char* msg, ...)
{
va_list ap;
va_start(ap, msg);
vprintf(msg, ap);
va_end(ap);
_exit(-1);
}
int warn(const char* msg, ...)
{
va_list ap;
va_start(ap, msg);
vprintf(msg, ap);
va_end(ap);
return 0;
}