summaryrefslogtreecommitdiff
path: root/src/cmd/gc/mkbuiltin1.c
blob: f8f61c278f695d7d6335f0083b387fc21d023fe9 (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
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
// 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.

// +build ignore

// Compile .go file, import data from .6 file, and generate C string version.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>

void esc(char*);
void fatal(char*, ...);

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

	if(argc != 2) {
		fprintf(stderr, "usage: mkbuiltin1 sys\n");
		fatal("in file $1.6 s/PACKAGE/$1/");
	}

	name = argv[1];
	snprintf(initfunc, sizeof(initfunc), "init_%s_function", name);

	snprintf(buf, sizeof(buf), "%s.%s", name, getenv("O"));
	if((fin = fopen(buf, "r")) == NULL) {
		fatal("open %s: %s", buf, strerror(errno));
	}

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

begin:
	printf("char *%simport =\n", name);

	// 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_$1_function - it doesn't exist
		if(strstr(buf, initfunc))
			continue;

		// sys.go claims to be in package PACKAGE to avoid
		// conflicts during "6g sys.go".  rename PACKAGE to $2.
		printf("\t\"");
		while(q = strstr(p, "PACKAGE")) {
			*q = 0;
			esc(p);	// up to the substitution
			printf("%s", name);	// the sub name
			p = q+7;		// continue with rest
		}

		esc(p);
		printf("\\n\"\n");
	}
	fatal("did not find end of imports");

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

void
esc(char *p)
{
	for(; *p; p++) {
		if(*p == '\\' || *p == '\"')
			printf("\\");
		putchar(*p);
	}
}

void
fatal(char *msg, ...)
{
	va_list arg;
	
	va_start(arg, msg);
	fprintf(stderr, "fatal: ");
	vfprintf(stderr, msg, arg);
	fprintf(stderr, "\n");
	exit(2);
}