summaryrefslogtreecommitdiff
path: root/src/cmd/gc/mksys.c
blob: cf0537c31890ab9d8aa149f43b568cb1c92adfe5 (plain)
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
// Copyright 2009 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Extract import data from sys.6 and generate C string version.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

int
main(int argc, char **argv)
{
	FILE *fin;
	char buf[1024], *p, *q;

	if(argc != 2) {
		fprintf(stderr, "usage: mksys sys.6\n");
		exit(1);
	}
	if((fin = fopen(argv[1], "r")) == NULL) {
		fprintf(stderr, "open %s: %s\n", argv[1], strerror(errno));
		exit(1);
	}

	// look for $$ that introduces imports
	while(fgets(buf, sizeof buf, fin) != NULL)
		if(strstr(buf, "$$"))
			goto begin;
	fprintf(stderr, "did not find beginning of imports\n");
	exit(1);

begin:
	printf("char *sysimport = \n");

	// process imports, stopping at $$ that closes them
	while(fgets(buf, sizeof buf, fin) != NULL) {
		buf[strlen(buf)-1] = 0;	// chop \n
		if(strstr(buf, "$$"))
			goto end;

		// chop leading white space
		for(p=buf; *p==' ' || *p == '\t'; p++)
			;

		// cut out decl of init_sys_function - it doesn't exist
		if(strstr(buf, "init_sys_function"))
			continue;

		// sys.go claims to be in package SYS to avoid
		// conflicts during "6g sys.go".  rename SYS to sys.
		for(q=p; *q; q++)
			if(memcmp(q, "SYS", 3) == 0)
				memmove(q, "sys", 3);

		printf("\t\"%s\\n\"\n", p);
	}
	fprintf(stderr, "did not find end of imports\n");
	exit(1);

end:
	printf("\t\"$$\\n\";\n");
	return 0;
}