2009-03-05 19:26:12 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2008-10-03 17:15:55 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-03-05 19:26:12 -07:00
|
|
|
// Compile .go file, import data from .6 file, and generate C string version.
|
2008-10-03 17:15:55 -06:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2008-12-08 20:46:39 -07:00
|
|
|
char *name;
|
2008-10-03 17:15:55 -06:00
|
|
|
FILE *fin;
|
2008-12-08 20:46:39 -07:00
|
|
|
char buf[1024], initfunc[1024], *p, *q;
|
2008-10-03 17:15:55 -06:00
|
|
|
|
|
|
|
if(argc != 2) {
|
2009-03-05 19:26:12 -07:00
|
|
|
fprintf(stderr, "usage: mkbuiltin1 sys\n");
|
2008-12-08 20:46:39 -07:00
|
|
|
fprintf(stderr, "in file $1.6 s/PACKAGE/$1/\n");
|
2008-10-03 17:15:55 -06:00
|
|
|
exit(1);
|
|
|
|
}
|
2008-12-08 20:46:39 -07:00
|
|
|
|
|
|
|
name = argv[1];
|
|
|
|
snprintf(initfunc, sizeof(initfunc), "init_%s_function", name);
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "%s.6", name);
|
|
|
|
if((fin = fopen(buf, "r")) == NULL) {
|
|
|
|
fprintf(stderr, "open %s: %s\n", buf, strerror(errno));
|
2008-10-03 17:15:55 -06:00
|
|
|
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:
|
2008-12-17 11:14:29 -07:00
|
|
|
printf("char *%simport =\n", name);
|
2008-10-03 17:15:55 -06:00
|
|
|
|
|
|
|
// 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++)
|
|
|
|
;
|
|
|
|
|
2008-12-08 20:46:39 -07:00
|
|
|
// cut out decl of init_$1_function - it doesn't exist
|
|
|
|
if(strstr(buf, initfunc))
|
2008-10-03 17:15:55 -06:00
|
|
|
continue;
|
|
|
|
|
2008-12-08 20:46:39 -07:00
|
|
|
// 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;
|
|
|
|
printf("%s", p); // up to the substitution
|
|
|
|
printf("%s", name); // the sub name
|
|
|
|
p = q+7; // continue with rest
|
|
|
|
}
|
2008-10-03 17:15:55 -06:00
|
|
|
|
2008-12-08 20:46:39 -07:00
|
|
|
printf("%s\\n\"\n", p);
|
2008-10-03 17:15:55 -06:00
|
|
|
}
|
|
|
|
fprintf(stderr, "did not find end of imports\n");
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
end:
|
|
|
|
printf("\t\"$$\\n\";\n");
|
|
|
|
return 0;
|
|
|
|
}
|