gcg.c 3.55 KB
Newer Older
Lauri Alanko's avatar
Lauri Alanko committed
1
#include <stdio.h>
2 3 4 5
#include <string.h>
#include <errno.h>
#include "gcg.h"
#include "parse.h"
Lauri Alanko's avatar
Lauri Alanko committed
6
#include <unistd.h>
Lauri Alanko's avatar
Lauri Alanko committed
7
#include "output.h"
8
#include "marshall.h"
Lauri Alanko's avatar
Lauri Alanko committed
9

10 11 12 13
#ifndef CPP
#define CPP "cpp"
#endif

14
Id header_root = NULL;
15
Id source_name = NULL;
16

17 18
gboolean collect_marshall = FALSE;

19
GString* cpp_cmd;
Lauri Alanko's avatar
Lauri Alanko committed
20 21 22

void get_options(int argc, char* argv[]){
	gint x=0;
23
	yydebug = yy_flex_debug = FALSE;
Lauri Alanko's avatar
Lauri Alanko committed
24
	do{
25
		x=getopt(argc, argv, "D:i:dI:o:m");
Lauri Alanko's avatar
Lauri Alanko committed
26
		switch(x){
27 28
		case 'D':
			header_root=optarg;
Lauri Alanko's avatar
Lauri Alanko committed
29
			break;
30 31 32
		case 'I':
			g_string_append(cpp_cmd, " -I ");
			g_string_append(cpp_cmd, optarg);
Lauri Alanko's avatar
Lauri Alanko committed
33
			break;
34
		case 'd':
35 36 37 38 39 40 41
			if(!yydebug)
				yydebug = TRUE;
			else
				yy_flex_debug = TRUE;
			break;
		case 'o':
			source_name = optarg;
42
			break;
43 44
		case 'm':
			collect_marshall = TRUE;
Lauri Alanko's avatar
Lauri Alanko committed
45 46 47 48 49 50 51
		case '?':
		case ':':
			g_error("Bad option %c!\n", x);
		}
	}while(x!=EOF);
}

52 53
void output_cb(Def* def, gpointer out){
	output_def(out, def);
Lauri Alanko's avatar
Lauri Alanko committed
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


void open_out(PNode*(*func)(Module*),
	       Id suffix, PNode* n, PRoot* out){
	GString* s = g_string_new(header_root);
	gchar* str;
	PNode* guard;
	FILE* f;
	g_string_append(s, "/");
	str = p_to_str(func(current_module), NULL);
	g_string_append(s, str);
	g_free(str);
	f = fopen(s->str, "w+");
	if(!f)
		g_error("Unable to open file %s: %s",
			s->str,
			strerror(errno));
	g_string_free(s, TRUE);
	guard=p_fmt("_g_~_~_~",
		    p_c_ident(current_module->package->name),
		    p_c_ident(current_module->name),
		    p_str(suffix));
	p_write(p_fmt("#ifndef ~\n"
		      "#define ~\n"
		      "~"
		      "#endif /* ~ */\n",
		      guard,
		      guard,
		      n,
		      guard),
		f, out);
	fclose(f);
}	
	
Lauri Alanko's avatar
Lauri Alanko committed
90 91
int main(int argc, char* argv[]){
	/*	target=stdout;*/
92
	PRoot* out=pr_new();
93
	FILE* f;
94
	
95
	init_db();
96
	cpp_cmd = g_string_new(CPP);
Lauri Alanko's avatar
Lauri Alanko committed
97 98
	yydebug=0;
	get_options(argc, argv);
99 100 101 102 103 104
	g_string_append(cpp_cmd, " ");
	g_string_append(cpp_cmd, argv[optind]);

	
	yyin=popen(cpp_cmd->str, "r");
	/*yyin=fopen(argv[optind], "r");*/
Lauri Alanko's avatar
Lauri Alanko committed
105 106
	g_assert(yyin);
	yyparse();
107
	
108
	if(!source_name)
109
		source_name = p_to_str(p_fmt("~.c",
110 111
					     p_c_ident(current_module->name)),
				       NULL);
112 113 114 115 116 117 118
	if(!header_root)
		if(!current_module->package->headerbase
		   || current_module->package->headerbase[0])
			header_root = "..";
		else
			header_root = ".";
		   
119
	foreach_def(output_cb, out);
120

121
	f=fopen(source_name, "w+");
122 123 124
	if(!f)
		g_error("Unable to open file %s: %s",
			source_name, strerror(errno));
125 126 127 128 129 130 131
	p_write(p_fmt("~~~~"
		      "#ifdef GCG_IMPL\n"
		      "#\tinclude GCG_IMPL\n"
		      "#else\n"
		      "#\tinclude \"~\"\n"
		      "#endif\n"
		      "~",
132 133
		      p_col("source_prot_depends", p_prot_include),
		      p_col("source_head", NULL),
134 135
		      p_col("source_sigtypes", p_sigdemarsh_decl),
		      p_col("source_sigtypes", p_demarshaller),
136 137 138
		      p_fmt("~.i.c",
			     p_c_ident(current_module->name)),
		      p_col("source", NULL)),
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
		f, out);
	fclose(f);
	

	open_out(p_type_header, "type",
		 p_fmt("#include <gtk/gtktypeutils.h>\n"
		       "~",
		       p_col("type", NULL)),
		 out);
	
	open_out(p_func_header, "funcs",
		   p_fmt("~~~",
			 p_col("func_parent_depends", p_func_include),
			 p_col("func_depends", p_type_include),
			 p_col("functions", NULL)),
		   out);
	
	open_out(p_prot_header, "prot",
157 158
		 p_fmt("~~~~",
		       p_func_include(current_module),
159 160 161 162
		       p_col("prot_parent_depends", p_prot_include),
		       p_col("prot_depends", p_type_include),
		       p_col("protected", NULL)),
		 out);
163 164 165 166 167 168 169 170

	open_out(p_import_header, "import",
		 p_fmt("~~~",
		       p_func_include(current_module),
		       p_col("import_depends", p_import_include),
		       p_col("import_alias", NULL)),
		 out);
return 0;
Lauri Alanko's avatar
Lauri Alanko committed
171 172 173 174
}