diff main.c @ 7:b8167949474a

make places work better
author David A. Holland
date Sun, 19 Dec 2010 19:08:24 -0500
parents 0601b6e8e53d
children 97243badae69
line wrap: on
line diff
--- a/main.c	Sun Dec 19 18:55:51 2010 -0500
+++ b/main.c	Sun Dec 19 19:08:24 2010 -0500
@@ -55,7 +55,7 @@
 // commandline macros
 
 struct commandline_macro {
-	unsigned column;
+	struct place *place;
 	const char *macro;
 	const char *expansion;
 };
@@ -78,20 +78,20 @@
 
 static
 void
-commandline_macro_add(unsigned column,
+commandline_macro_add(const struct place *p,
 		      const char *macro, const char *expansion)
 {
 	struct commandline_macro *cm;
 
 	cm = domalloc(sizeof(*cm));
-	cm->column = column;
+	cm->place = place_clone(p);
 	cm->macro = macro;
 	cm->expansion = expansion;
 }
 
 static
 void
-commandline_def(unsigned column, char *str)
+commandline_def(const struct place *p, char *str)
 {
 	char *val;
 
@@ -100,14 +100,14 @@
 		*val = '\0';
 		val++;
 	}
-	commandline_macro_add(column, str, val ? val : "1");
+	commandline_macro_add(p, str, val ? val : "1");
 }
 
 static
 void
-commandline_undef(unsigned column, char *str)
+commandline_undef(const struct place *p, char *str)
 {
-	commandline_macro_add(column, str, NULL);
+	commandline_macro_add(p, str, NULL);
 }
 
 static
@@ -115,62 +115,65 @@
 apply_commandline_macros(void)
 {
 	struct commandline_macro *cm;
-	struct place *p;
 	unsigned i, num;
 
-	p = place_gettemporary();
 	num = array_num(&commandline_macros);
 	for (i=0; i<num; i++) {
 		cm = array_get(&commandline_macros, i);
 		if (cm->expansion != NULL) {
-			place_setcommandline(p, cm->column);
-			macro_define(p, cm->macro, cm->expansion);
+			macro_define(cm->place, cm->macro, cm->expansion);
 		} else {
 			macro_undef(cm->macro);
 		}
+		place_destroy(cm->place);
 		free(cm);
 	}
 	array_setsize(&commandline_macros, 0);
-	place_puttemporary(p);
 }
 
 static
 void
-apply_builtin_macro(const char *name, const char *val)
+apply_builtin_macro(unsigned num, const char *name, const char *val)
 {
-	/* XXX distinguish builtin-place and commandline-place and nowhere */
-	macro_define(NULL, name, val);
+	struct place *p;
+
+	p = place_gettemporary();
+	place_setbuiltin(p, num);
+	macro_define(p, name, val);
+	place_puttemporary(p);
 }
 
 static
 void
 apply_builtin_macros(void)
 {
+	unsigned n = 1;
+
 #ifdef CONFIG_OS
-	apply_builtin_macro(CONFIG_OS, "1");
+	apply_builtin_macro(n++, CONFIG_OS, "1");
 #endif
 #ifdef CONFIG_OS_2
-	apply_builtin_macro(CONFIG_OS_2, "1");
+	apply_builtin_macro(n++, CONFIG_OS_2, "1");
 #endif
 
 #ifdef CONFIG_CPU
-	apply_builtin_macro(CONFIG_CPU, "1");
+	apply_builtin_macro(n++, CONFIG_CPU, "1");
 #endif
 #ifdef CONFIG_CPU_2
-	apply_builtin_macro(CONFIG_CPU_2, "1");
+	apply_builtin_macro(n++, CONFIG_CPU_2, "1");
 #endif
 
 #ifdef CONFIG_SIZE
-	apply_builtin_macro(CONFIG_SIZE, "1");
+	apply_builtin_macro(n++, CONFIG_SIZE, "1");
 #endif
 #ifdef CONFIG_BINFMT
-	apply_builtin_macro(CONFIG_BINFMT, "1");
+	apply_builtin_macro(n++, CONFIG_BINFMT, "1");
 #endif
 
 #ifdef CONFIG_COMPILER
-	apply_builtin_macro(CONFIG_COMPILER, VERSION_MAJOR);
-	apply_builtin_macro(CONFIG_COMPILER_MINOR, VERSION_MINOR);
-	apply_builtin_macro("__VERSION__", VERSION_LONG);
+	apply_builtin_macro(n++, CONFIG_COMPILER, VERSION_MAJOR);
+	apply_builtin_macro(n++, CONFIG_COMPILER_MINOR, VERSION_MINOR);
+	apply_builtin_macro(n++, "__VERSION__", VERSION_LONG);
 #endif
 }
 
@@ -178,9 +181,9 @@
 // extra included files
 
 struct commandline_file {
+	struct place *place;
 	char *name;
 	bool suppress_output;
-	unsigned column;
 };
 
 static struct array commandline_files;
@@ -201,29 +204,29 @@
 
 static
 void
-commandline_addfile(char *name, bool suppress_output, unsigned column)
+commandline_addfile(const struct place *p, char *name, bool suppress_output)
 {
 	struct commandline_file *cf;
 
 	cf = domalloc(sizeof(*cf));
+	cf->place = place_clone(p);
 	cf->name = name;
 	cf->suppress_output = suppress_output;
-	cf->column = column;
 	array_add(&commandline_files, cf, NULL);
 }
 
 static
 void
-commandline_addfile_output(unsigned column, char *name)
+commandline_addfile_output(const struct place *p, char *name)
 {
-	commandline_addfile(name, false, column);
+	commandline_addfile(p, name, false);
 }
 
 static
 void
-commandline_addfile_nooutput(unsigned column, char *name)
+commandline_addfile_nooutput(const struct place *p, char *name)
 {
-	commandline_addfile(name, true, column);
+	commandline_addfile(p, name, true);
 }
 
 static
@@ -233,24 +236,21 @@
 	struct commandline_file *cf;
 	unsigned i, num;
 	bool save = false;
-	struct place *p;
 
-	p = place_gettemporary();
 	num = array_num(&commandline_files);
 	for (i=0; i<num; i++) {
 		cf = array_get(&commandline_files, i);
-		place_setcommandline(p, cf->column);
 		if (cf->suppress_output) {
 			save = mode.do_output;
 			mode.do_output = false;
-			file_readquote(p, cf->name);
+			file_readquote(cf->place, cf->name);
 			mode.do_output = save;
 		} else {
-			file_readquote(p, cf->name);
+			file_readquote(cf->place, cf->name);
 		}
+		place_destroy(cf->place);
 		free(cf);
 	}
-	place_puttemporary(p);
 	array_setsize(&commandline_files, 0);
 }
 
@@ -285,9 +285,9 @@
 
 static
 void
-commandline_isysroot(unsigned column, char *dir)
+commandline_isysroot(const struct place *p, char *dir)
 {
-	(void)column;
+	(void)p;
 	sysroot = dir;
 }
 
@@ -300,33 +300,33 @@
 
 static
 void
-commandline_addincpath_quote(unsigned column, char *dir)
+commandline_addincpath_quote(const struct place *p, char *dir)
 {
-	(void)column;
+	(void)p;
 	commandline_addincpath(&incpath_quote, dir);
 }
 
 static
 void
-commandline_addincpath_user(unsigned column, char *dir)
+commandline_addincpath_user(const struct place *p, char *dir)
 {
-	(void)column;
+	(void)p;
 	commandline_addincpath(&incpath_user, dir);
 }
 
 static
 void
-commandline_addincpath_system(unsigned column, char *dir)
+commandline_addincpath_system(const struct place *p, char *dir)
 {
-	(void)column;
+	(void)p;
 	commandline_addincpath(&incpath_system, dir);
 }
 
 static
 void
-commandline_addincpath_late(unsigned column, char *dir)
+commandline_addincpath_late(const struct place *p, char *dir)
 {
-	(void)column;
+	(void)p;
 	commandline_addincpath(&incpath_late, dir);
 }
 
@@ -396,15 +396,15 @@
 
 static
 void
-commandline_setprefix(unsigned column, char *prefix)
+commandline_setprefix(const struct place *p, char *prefix)
 {
-	(void)column;
+	(void)p;
 	commandline_prefix = prefix;
 }
 
 static
 void
-commandline_addincpath_user_withprefix(unsigned column, char *dir)
+commandline_addincpath_user_withprefix(const struct place *p, char *dir)
 {
 	char *s;
 
@@ -414,12 +414,12 @@
 	}
 	s = dostrdup3(commandline_prefix, "/", dir);
 	freestringlater(s);
-	commandline_addincpath_user(column, s);
+	commandline_addincpath_user(p, s);
 }
 
 static
 void
-commandline_addincpath_late_withprefix(unsigned column, char *dir)
+commandline_addincpath_late_withprefix(const struct place *p, char *dir)
 {
 	char *s;
 
@@ -429,14 +429,14 @@
 	}
 	s = dostrdup3(commandline_prefix, "/", dir);
 	freestringlater(s);
-	commandline_addincpath_late(column, s);
+	commandline_addincpath_late(p, s);
 }
 
 static
 void
-commandline_setstd(unsigned column, char *std)
+commandline_setstd(const struct place *p, char *std)
 {
-	(void)column;
+	(void)p;
 
 	if (!strcmp(std, "krc")) {
 		return;
@@ -447,9 +447,9 @@
 
 static
 void
-commandline_setlang(unsigned column, char *lang)
+commandline_setlang(const struct place *p, char *lang)
 {
-	(void)column;
+	(void)p;
 
 	if (!strcmp(lang, "c") || !strcmp(lang, "assembler-with-cpp")) {
 		return;
@@ -463,9 +463,9 @@
 
 static
 void
-commandline_iremap(unsigned column, char *str)
+commandline_iremap(const struct place *p, char *str)
 {
-	(void)column;
+	(void)p;
 	/* XXX */
 	(void)str;
 	warnx("-iremap not supported");
@@ -474,12 +474,12 @@
 
 static
 void
-commandline_tabstop(unsigned column, char *s)
+commandline_tabstop(const struct place *p, char *s)
 {
 	char *t;
 	unsigned long val;
 
-	(void)column;
+	(void)p;
 
 	t = strchr(s, '=');
 	if (t == NULL) {
@@ -561,27 +561,27 @@
 
 static
 void
-commandline_setdependtarget(unsigned column, char *str)
+commandline_setdependtarget(const struct place *p, char *str)
 {
-	(void)column;
+	(void)p;
 	mode.depend_target = str;
 	mode.depend_quote_target = false;
 }
 
 static
 void
-commandline_setdependtarget_quoted(unsigned column, char *str)
+commandline_setdependtarget_quoted(const struct place *p, char *str)
 {
-	(void)column;
+	(void)p;
 	mode.depend_target = str;
 	mode.depend_quote_target = true;
 }
 
 static
 void
-commandline_setdependoutput(unsigned column, char *str)
+commandline_setdependoutput(const struct place *p, char *str)
 {
-	(void)column;
+	(void)p;
 	mode.depend_file = str;
 }
 
@@ -663,12 +663,12 @@
 
 struct prefix_option {
 	const char *string;
-	void (*func)(unsigned column, char *);
+	void (*func)(const struct place *, char *);
 };
 
 struct arg_option {
 	const char *string;
-	void (*func)(unsigned column, char *);
+	void (*func)(const struct place *, char *);
 };
 
 static const struct flag_option flag_options[] = {
@@ -779,7 +779,7 @@
 
 static
 bool
-check_prefix_option(unsigned column, char *opt)
+check_prefix_option(const struct place *p, char *opt)
 {
 	unsigned i;
 	int r;
@@ -788,7 +788,7 @@
 		r = strncmp(opt, prefix_options[i].string,
 			    strlen(prefix_options[i].string));
 		if (r == 0) {
-			prefix_options[i].func(column, opt);
+			prefix_options[i].func(p, opt);
 			return true;
 		}
 		if (r < 0) {
@@ -800,7 +800,7 @@
 
 static
 bool
-check_arg_option(const char *opt, unsigned argcolumn, char *arg)
+check_arg_option(const char *opt, const struct place *argplace, char *arg)
 {
 	unsigned i;
 	int r;
@@ -812,7 +812,7 @@
 				warnx("Option -%s requires an argument", opt);
 				die();
 			}
-			arg_options[i].func(argcolumn, arg);
+			arg_options[i].func(argplace, arg);
 			return true;
 		}
 		if (r < 0) {
@@ -904,31 +904,29 @@
 {
 	const char *inputfile = NULL;
 	const char *outputfile = NULL;
-	unsigned column, nextcolumn;
 	struct place *p;
 	int i;
 
 	init();
+	p = place_gettemporary();
 
-	column = 1;
-	for (i=1; i<argc; i++, column = nextcolumn) {
+	for (i=1; i<argc; i++) {
 		if (argv[i][0] != '-') {
 			break;
 		}
-		nextcolumn = column + strlen(argv[i]) + 1;
+		place_setcommandline(p, i);
 		if (check_flag_option(argv[i]+1)) {
 			continue;
 		}
 		if (check_act_option(argv[i]+1)) {
 			continue;
 		}
-		if (check_prefix_option(column, argv[i]+1)) {
+		if (check_prefix_option(p, argv[i]+1)) {
 			continue;
 		}
-		if (check_arg_option(argv[i]+1, nextcolumn, argv[i+1])) {
-			column = nextcolumn;
+		place_setcommandline(p, i+1);
+		if (check_arg_option(argv[i]+1, p, argv[i+1])) {
 			i++;
-			nextcolumn = strlen(argv[i]) + 1;
 			continue;
 		}
 		usage();
@@ -949,11 +947,10 @@
 	apply_builtin_macros();
 	apply_commandline_macros();
 	read_commandline_files();
-	p = place_gettemporary();
 	place_setnowhere(p);
 	file_readabsolute(p, inputfile);
+
 	place_puttemporary(p);
-
 	cleanup();
 	if (complain_failed()) {
 		return EXIT_FAILURE;