1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Compile .go file, import data from .6 file, and generate C string version.
6
7 #include <u.h>
8 #include <libc.h>
9 #include <stdio.h>
10
11 void esc(char*);
12
13 void
14 main(int argc, char **argv)
15 {
16 char *name;
17 FILE *fin;
18 char buf[1024], initfunc[1024], *p, *q;
19
20 if(argc != 2) {
21 fprintf(stderr, "usage: mkbuiltin1 sys\n");
22 sysfatal("in file $1.6 s/PACKAGE/$1/\n");
23 }
24
25 name = argv[1];
26 snprintf(initfunc, sizeof(initfunc), "init_%s_function", name);
27
28 snprintf(buf, sizeof(buf), "%s.%s", name, getenv("O"));
29 if((fin = fopen(buf, "r")) == NULL) {
30 sysfatal("open %s: %r\n", buf);
31 }
32
33 // look for $$ that introduces imports
34 while(fgets(buf, sizeof buf, fin) != NULL)
35 if(strstr(buf, "$$"))
36 goto begin;
37 sysfatal("did not find beginning of imports\n");
38
39 begin:
40 printf("char *%simport =\n", name);
41
42 // process imports, stopping at $$ that closes them
43 while(fgets(buf, sizeof buf, fin) != NULL) {
44 buf[strlen(buf)-1] = 0; // chop \n
45 if(strstr(buf, "$$"))
46 goto end;
47
48 // chop leading white space
49 for(p=buf; *p==' ' || *p == '\t'; p++)
50 ;
51
52 // cut out decl of init_$1_function - it doesn't exist
53 if(strstr(buf, initfunc))
54 continue;
55
56 // sys.go claims to be in package PACKAGE to avoid
57 // conflicts during "6g sys.go". rename PACKAGE to $2.
58 printf("\t\"");
59 while(q = strstr(p, "PACKAGE")) {
60 *q = 0;
61 esc(p); // up to the substitution
62 printf("%s", name); // the sub name
63 p = q+7; // continue with rest
64 }
65
66 esc(p);
67 printf("\\n\"\n");
68 }
69 sysfatal("did not find end of imports\n");
70
71 end:
72 printf("\t\"$$\\n\";\n");
73 exits(0);
74 }
75
76 void
77 esc(char *p)
78 {
79 for(; *p; p++) {
80 if(*p == '\\' || *p == '\"')
81 printf("\\");
82 putchar(*p);
83 }
84 }