Support -l/-L, -M*, and -print-klibc-* options in klcc

diff --git a/Makefile b/Makefile
index a512aa3..7a9b92e 100644
--- a/Makefile
+++ b/Makefile
@@ -27,7 +27,10 @@
 	echo 'STRIPFLAGS=$(STRIPFLAGS)' >> $@
 	echo 'EMAIN=$(EMAIN)' >> $@
 	echo 'BITSIZE=$(BITSIZE)' >> $@
-	echo 'INSTALLDIR=$(INSTALLDIR)' >> $@
+	echo 'prefix=$(INSTALLDIR)' >> $@
+	echo 'bindir=$(INSTALLDIR)/$(KCROSS)bin' >> $@
+	echo 'libdir=$(INSTALLDIR)/$(KCROSS)lib' >> $@
+	echo 'includedir=$(INSTALLDIR)/$(KCROSS)include' >> $@
 
 $(CROSS)klcc: klcc.in $(CROSS)klibc.config makeklcc.pl
 	$(PERL) makeklcc.pl klcc.in $(CROSS)klibc.config \
diff --git a/klcc.in b/klcc.in
index d8721c9..c873217 100644
--- a/klcc.in
+++ b/klcc.in
@@ -1,19 +1,22 @@
 # -*- perl -*-
 
 # Standard includes
-@includes = ("-I${INSTALLDIR}/${KCROSS}include/arch/${ARCH}",
-	     "-I${INSTALLDIR}/${KCROSS}include/bits${BITSIZE}",
-	     "-I${INSTALLDIR}/${KCROSS}include");
+@includes = ("-I${prefix}/${KCROSS}include/arch/${ARCH}",
+	     "-I${prefix}/${KCROSS}include/bits${BITSIZE}",
+	     "-I${prefix}/${KCROSS}include");
 
 # Default optimization options (for compiles without -g)
 @optopt =  @OPTFLAGS;
 @goptopt = ('-O');
 
+# Standard library directories
+@stdlibpath = ("-L${prefix}/${KCROSS}lib");
+
 # Options and libraries to pass to ld; shared versus static
-@staticopt = ("$INSTALLDIR/${KCROSS}lib/crt0.o");
-@staticlib = ("$INSTALLDIR/${KCROSS}lib/libc.a");
-@sharedopt = (@EMAIN, "$INSTALLDIR/${KCROSS}lib/interp.o");
-@sharedlib = ('-R', "$INSTALLDIR/${KCROSS}lib/libc.so");
+@staticopt = ("${prefix}/${KCROSS}lib/crt0.o");
+@staticlib = ("${prefix}/${KCROSS}lib/libc.a");
+@sharedopt = (@EMAIN, "${prefix}/${KCROSS}lib/interp.o");
+@sharedlib = ('-R', "${prefix}/${KCROSS}lib/libc.so");
 
 # Returns the language (-x option string) for a specific extension.
 sub filename2lang($) {
@@ -88,6 +91,7 @@
 
 @ccopt = ();
 @ldopt = ();
+@libs  = ();
 
 @files = ();			# List of files
 %flang = ();			# Languages for files
@@ -117,7 +121,7 @@
     } elsif ( $a =~ /^-([fmwWQdO]|std=|ansi|pedantic)/ ) {
 	# Options to gcc
 	push(@ccopt, $a);
-    } elsif ( $a =~ /^-([DUI])(.*)$/ ) {
+    } elsif ( $a =~ /^-([DUI]|M[FQT])(.*)$/ ) {
 	# Options to gcc, which can take either a conjoined argument
 	# (-DFOO) or a disjoint argument (-D FOO)
 	push(@ccopt, $a);
@@ -125,10 +129,9 @@
     } elsif ( $a eq '-include' ) {
 	# Options to gcc which always take a disjoint argument
 	push(@ccopt, $a, shift(@ARGV));
-    } elsif ( $a =~ /^-[gp]/ ) {
-	# Debugging options to gcc *and* ld
+    } elsif ( $a =~ /^-(g|pg)/ || $a eq '-p' ) {
+	# Debugging options to gcc
 	push(@ccopt, $a);
-	push(@ldopt, $a);
 	$debugging = 1;
     } elsif ( $a eq '-v' ) {
 	push(@ccopt, $a);
@@ -150,7 +153,18 @@
     } elsif ( $a eq '-nostdinc' ) {
 	push(@ccopt, $a);
 	@includes = ();
-    } elsif ( $a =~ /^(-print|--help)/ ) {
+    } elsif ( $a =~ /^-([lL])(.*)$/ ) {
+	# Libraries
+	push(@libs, $a);
+	push(@libs, shift(@ARGV)) if ( $2 eq '' );
+    } elsif ( $a =~ /^-print-klibc-(.*)$/ ) {
+	if ( defined($conf{$1}) ) {
+	    print ${$conf{$1}}, "\n";
+	    exit 0;
+	} else {
+	    die "$0: unknown option: $a\n";
+	}
+    } elsif ( $a =~ /^(-print|-dump|--help)/ ) {
 	# Pseudo-operations; just pass to gcc and don't do anything else
 	push(@ccopt, $a);
 	$operation = 'c' if ( $operation eq '' );
@@ -204,9 +218,9 @@
     close(LIBGCC);
 
     if ( $shared ) {
-	$rv = mysystem($LD, @LDFLAGS, @sharedopt, @ldopt, @outopt, @objs, @sharedlib, $libgcc);
+	$rv = mysystem($LD, @LDFLAGS, @sharedopt, @ldopt, @outopt, @objs, @libs, @stdlibpath, @sharedlib, $libgcc);
     } else {
-	$rv = mysystem($LD, @LDFLAGS, @staticopt, @ldopt, @outopt, @objs, @staticlib, $libgcc);
+	$rv = mysystem($LD, @LDFLAGS, @staticopt, @ldopt, @outopt, @objs, @libs, @stdlibpath, @staticlib, $libgcc);
     }
 
     unlink(@rmobjs);
diff --git a/makeklcc.pl b/makeklcc.pl
index ea3f8a8..ba851ae 100755
--- a/makeklcc.pl
+++ b/makeklcc.pl
@@ -21,9 +21,11 @@
 open(KLIBCCONF, '<', $klibcconf) or die "$0: cannot open $klibcconf: $!\n";
 while ( defined($l = <KLIBCCONF>) ) {
     chomp $l;
-    if ( $l =~ /=/ ) {
-	print "\$$` = \"\Q$'\E\";\n";
-	print "\@$` = ", string2list("$'"), ";\n";
+    if ( $l =~ /^([^=]+)\=(.*)$/ ) {
+	$n = $1;  $s = $2;
+	print "\$$n = \"\Q$s\E\";\n";
+	print "\@$n = ", string2list($s), ";\n";
+	print "\$conf{\'\L$n\E\'} = \\\$$n;\n";
     }
 }
 close(KLIBCCONF);