runtime: make goc2c build on Plan 9

pkg/runtime/Makefile:
. Adjusted so "goc2c.c" is built using the Plan 9 libraries.

pkg/runtime/goc2c.c:
. Added/subtracted #include headers to correspond to Plan 9
  toolkit.
. Changed fprintf(stderr,...)/exit() combinations to
  sysfatal() calls, adjusted the "%u" format to "%ud".
. Added exits(0) at the end of main().
. Made main() a void-returning function and removed the
  "return 0" at the end of it.

Tested on UBUNTU and Plan 9 only.

R=r, rsc
CC=golang-dev
https://golang.org/cl/4626093
diff --git a/src/pkg/runtime/Makefile b/src/pkg/runtime/Makefile
index 03f960c..64bd2b7 100644
--- a/src/pkg/runtime/Makefile
+++ b/src/pkg/runtime/Makefile
@@ -120,7 +120,7 @@
 	mv -f $@.x $@
 
 goc2c: goc2c.c
-	quietgcc -o $@ $<
+	quietgcc -o $@ -I "$(GOROOT)/include" $< "$(GOROOT)/lib/lib9.a"
 
 mkversion: mkversion.c
 	quietgcc -o $@ -I "$(GOROOT)/include" $< "$(GOROOT)/lib/lib9.a"
diff --git a/src/pkg/runtime/goc2c.c b/src/pkg/runtime/goc2c.c
index 826ceff..61236e2 100644
--- a/src/pkg/runtime/goc2c.c
+++ b/src/pkg/runtime/goc2c.c
@@ -2,26 +2,27 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-/* Translate a .goc file into a .c file.  A .goc file is a combination
-   of a limited form of Go with C.  */
+/*
+ * Translate a .goc file into a .c file.  A .goc file is a combination
+ * of a limited form of Go with C.
+ */
 
 /*
-   package PACKAGENAME
-   {# line}
-   func NAME([NAME TYPE { , NAME TYPE }]) [(NAME TYPE { , NAME TYPE })] \{
-     C code with proper brace nesting
-   \}
+	package PACKAGENAME
+	{# line}
+	func NAME([NAME TYPE { , NAME TYPE }]) [(NAME TYPE { , NAME TYPE })] \{
+	  C code with proper brace nesting
+	\}
 */
 
-/* We generate C code which implements the function such that it can
-   be called from Go and executes the C code.  */
+/*
+ * We generate C code which implements the function such that it can
+ * be called from Go and executes the C code.
+ */
 
-#include <assert.h>
-#include <ctype.h>
+#include <u.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
+#include <libc.h>
 
 /* Whether we're emitting for gcc */
 static int gcc;
@@ -88,16 +89,14 @@
 static void
 bad_eof(void)
 {
-	fprintf(stderr, "%s:%u: unexpected EOF\n", file, lineno);
-	exit(1);
+	sysfatal("%s:%ud: unexpected EOF\n", file, lineno);
 }
 
 /* Out of memory.  */
 static void
 bad_mem(void)
 {
-	fprintf(stderr, "%s:%u: out of memory\n", file, lineno);
-	exit(1);
+	sysfatal("%s:%ud: out of memory\n", file, lineno);
 }
 
 /* Allocate memory without fail.  */
@@ -196,8 +195,10 @@
 	}
 }
 
-/* Read and return a token.  Tokens are delimited by whitespace or by
-   [(),{}].  The latter are all returned as single characters.  */
+/*
+ * Read and return a token.  Tokens are delimited by whitespace or by
+ * [(),{}].  The latter are all returned as single characters.
+ */
 static char *
 read_token(void)
 {
@@ -259,11 +260,11 @@
 	char *token;
 
 	token = read_token_no_eof();
+	if (token == nil)
+		sysfatal("%s:%ud: no token\n", file, lineno);
 	if (strcmp(token, "package") != 0) {
-		fprintf(stderr,
-			"%s:%u: expected \"package\", got \"%s\"\n",
+		sysfatal("%s:%ud: expected \"package\", got \"%s\"\n",
 			file, lineno, token);
-		exit(1);
 	}
 	return read_token_no_eof();
 }
@@ -290,8 +291,10 @@
 	}
 }
 
-/* Read a type in Go syntax and return a type in C syntax.  We only
-   permit basic types and pointers.  */
+/*
+ * Read a type in Go syntax and return a type in C syntax.  We only
+ * permit basic types and pointers.
+ */
 static char *
 read_type(void)
 {
@@ -333,13 +336,14 @@
 	for(i=0; type_table[i].name; i++)
 		if(strcmp(type_table[i].name, p) == 0)
 			return type_table[i].size;
-	fprintf(stderr, "%s:%u: unknown type %s\n", file, lineno, p);
-	exit(1);
+	sysfatal("%s:%ud: unknown type %s\n", file, lineno, p);
 	return 0;
 }
 
-/* Read a list of parameters.  Each parameter is a name and a type.
-   The list ends with a ')'.  We have already read the '('.  */
+/*
+ * Read a list of parameters.  Each parameter is a name and a type.
+ * The list ends with a ')'.  We have already read the '('.
+ */
 static struct params *
 read_params(int *poffset)
 {
@@ -375,17 +379,18 @@
 		}
 	}
 	if (strcmp(token, ")") != 0) {
-		fprintf(stderr, "%s:%u: expected '('\n",
+		sysfatal("%s:%ud: expected '('\n",
 			file, lineno);
-		exit(1);
 	}
 	if (poffset != NULL)
 		*poffset = offset;
 	return ret;
 }
 
-/* Read a function header.  This reads up to and including the initial
-   '{' character.  Returns 1 if it read a header, 0 at EOF.  */
+/*
+ * Read a function header.  This reads up to and including the initial
+ * '{' character.  Returns 1 if it read a header, 0 at EOF.
+ */
 static int
 read_func_header(char **name, struct params **params, int *paramwid, struct params **rets)
 {
@@ -416,9 +421,8 @@
 
 	token = read_token();
 	if (token == NULL || strcmp(token, "(") != 0) {
-		fprintf(stderr, "%s:%u: expected \"(\"\n",
+		sysfatal("%s:%ud: expected \"(\"\n",
 			file, lineno);
-		exit(1);
 	}
 	*params = read_params(paramwid);
 
@@ -430,9 +434,8 @@
 		token = read_token();
 	}
 	if (token == NULL || strcmp(token, "{") != 0) {
-		fprintf(stderr, "%s:%u: expected \"{\"\n",
+		sysfatal("%s:%ud: expected \"{\"\n",
 			file, lineno);
-		exit(1);
 	}
 	return 1;
 }
@@ -581,8 +584,10 @@
 		write_6g_func_trailer(rets);
 }
 
-/* Read and write the body of the function, ending in an unnested }
-   (which is read but not written).  */
+/*
+ * Read and write the body of the function, ending in an unnested }
+ * (which is read but not written).
+ */
 static void
 copy_body(void)
 {
@@ -669,15 +674,15 @@
 static void
 usage(void)
 {
-	fprintf(stderr, "Usage: goc2c [--6g | --gc] [file]\n");
-	exit(1);
+	sysfatal("Usage: goc2c [--6g | --gc] [file]\n");
 }
 
-int
+void
 main(int argc, char **argv)
 {
 	char *goarch;
 
+	argv0 = argv[0];
 	while(argc > 1 && argv[1][0] == '-') {
 		if(strcmp(argv[1], "-") == 0)
 			break;
@@ -694,7 +699,7 @@
 	if(argc <= 1 || strcmp(argv[1], "-") == 0) {
 		file = "<stdin>";
 		process_file();
-		return 0;
+		exits(0);
 	}
 
 	if(argc > 2)
@@ -702,8 +707,7 @@
 
 	file = argv[1];
 	if(freopen(file, "r", stdin) == 0) {
-		fprintf(stderr, "open %s: %s\n", file, strerror(errno));
-		exit(1);
+		sysfatal("open %s: %r\n", file);
 	}
 
 	if(!gcc) {
@@ -719,5 +723,5 @@
 	}
 
 	process_file();
-	return 0;
+	exits(0);
 }