--- lemon.c	Tue Dec  5 20:52:24 2000
+++ lemon.c	Thu Jun  7 21:13:39 2001
@@ -30,10 +30,11 @@
 #include <string.h>
 #include <ctype.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/param.h>
 
 extern void qsort();
 extern double strtod();
 extern long strtol();
-extern void free();
-extern int access();
 extern int atoi();
 
@@ -54,5 +55,4 @@
 
 char *msort();
-extern void *malloc();
 
 /******** From the file "action.h" *************************************/
@@ -1168,4 +1168,26 @@
 }
 
+static char *lempar_locations[] = {
+	NULL, "lempar.c", LEMPAR
+};
+
+void setlempar(lempar)
+char *lempar;
+{
+	if (access(lempar, R_OK)) {
+		perror(lempar);
+		exit(1);
+	}
+	lempar_locations[0] = lempar;
+}
+
+static char *output_file = NULL;
+
+void setoutput(base)
+char *base;
+{
+	if (output_file = malloc(strlen(base) + 1))
+		sprintf(output_file, "%s.", base);
+}
 
 /* The main program.  Parse the command line and do it... */
@@ -1185,7 +1207,12 @@
     {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
     {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
-    {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file"},
+    {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."},
+    {OPT_FSTR, "o", (char*)setoutput, "Set the dirname/basename for the "
+				"output file(s)."},
     {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
     {OPT_FLAG, "s", (char*)&statistics, "Print parser stats to standard output."},
+    {OPT_FSTR, "t", (char*)setlempar, "An alternative template -- instead of"
+				" ``./lempar.c''\n\t\tor the system wide ``"
+				LEMPAR"''."},
     {OPT_FLAG, "x", (char*)&version, "Print the version number."},
     {OPT_FLAG,0,0,0}
@@ -2305,4 +2332,5 @@
   }
 }
+
 /*********************** From the file "report.c" **************************/
 /*
@@ -2318,13 +2346,14 @@
 char *suffix;
 {
-  char *name;
-  char *cp;
+  char *name = NULL;
+  char *cp, *fname;
 
-  name = malloc( strlen(lemp->filename) + strlen(suffix) + 5 );
+  fname = output_file ? output_file : lemp->filename;
+  name = malloc( strlen(fname) + strlen(suffix));
   if( name==0 ){
     fprintf(stderr,"Can't allocate space for a filename.\n");
     exit(1);
   }
-  strcpy(name,lemp->filename);
+  strcpy(name, fname);
   cp = strrchr(name,'.');
   if( cp ) *cp = 0;
@@ -2524,47 +2553,14 @@
 }
 
-/* Search for the file "name" which is in the same directory as
-** the exacutable */
-PRIVATE char *pathsearch(argv0,name,modemask)
-char *argv0;
-char *name;
-int modemask;
+PRIVATE const char *pathsearch()
 {
-  char *pathlist;
-  char *path,*cp;
-  char c;
-  extern int access();
+  int i;
+  const char *path = NULL;
 
-#ifdef __WIN32__
-  cp = strrchr(argv0,'\\');
-#else
-  cp = strrchr(argv0,'/');
-#endif
-  if( cp ){
-    c = *cp;
-    *cp = 0;
-    path = (char *)malloc( strlen(argv0) + strlen(name) + 2 );
-    if( path ) sprintf(path,"%s/%s",argv0,name);
-    *cp = c;
-  }else{
-    extern char *getenv();
-    pathlist = getenv("PATH");
-    if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
-    path = (char *)malloc( strlen(pathlist)+strlen(name)+2 );
-    if( path!=0 ){
-      while( *pathlist ){
-        cp = strchr(pathlist,':');
-        if( cp==0 ) cp = &pathlist[strlen(pathlist)];
-        c = *cp;
-        *cp = 0;
-        sprintf(path,"%s/%s",pathlist,name);
-        *cp = c;
-        if( c==0 ) pathlist = "";
-        else pathlist = &cp[1];
-        if( access(path,modemask)==0 ) break;
-      }
-    }
-  }
-  return path;
+  for (i = 0; i < sizeof(lempar_locations)/sizeof(char *); i++)
+    if (lempar_locations[i] && access(lempar_locations[i], R_OK) == 0)
+      return lempar_locations[i];
+
+  return(NULL);
 }
 
@@ -2630,8 +2626,7 @@
 struct lemon *lemp;
 {
-  static char templatename[] = "lempar.c";
-  char buf[1000];
+  char buf[MAXPATHLEN];
   FILE *in;
-  char *tpltname;
+  const char *tpltname;
   char *cp;
 
@@ -2642,12 +2637,11 @@
     sprintf(buf,"%s.lt",lemp->filename);
   }
-  if( access(buf,004)==0 ){
+  if( access(buf, R_OK)==0 ){
     tpltname = buf;
   }else{
-    tpltname = pathsearch(lemp->argv0,templatename,0);
+    tpltname = pathsearch();
   }
   if( tpltname==0 ){
-    fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
-    templatename);
+    fprintf(stderr,"Can't find the parser driver template file.\n");
     lemp->errorcnt++;
     return 0;
@@ -2655,5 +2649,5 @@
   in = fopen(tpltname,"r");
   if( in==0 ){
-    fprintf(stderr,"Can't open the template file \"%s\".\n",templatename);
+    fprintf(stderr,"Can't open the template file \"%s\".\n", tpltname);
     lemp->errorcnt++;
     return 0;
