changeset 4448:fd8a9a4d81d9

Support #!, to allow XEmacs to be called as a script interpreter. ChangeLog addition: 2008-01-20 Aidan Kehoe <kehoea@parhasard.net> * Makefile.in.in (SHEBANG_PROGNAME): New variable; a symbol link to the XEmacs binary to tell it it should read a script from stdin. * configure.ac (XE_EXPAND_VARIABLE): Set SHEBANG_PROGNAME. * configure: Regenerate. src/ChangeLog addition: 2008-01-20 Aidan Kehoe <kehoea@parhasard.net> * config.h.in: Add SHEBANG_PROGNAME. * emacs.c (main_1): If we've been called using SHEBANG_PROGNAME, rewrite our arguments to add a --script argument. Also, handle the --script argument by setting noninteractive and vanilla. * lread.c (Fload_internal): If the first two characters of a file are #!, replace them with ;! before they get to the Lisp reader. lisp/ChangeLog addition: 2008-01-20 Aidan Kehoe <kehoea@parhasard.net> * startup.el (command-line-do-script): New. (command-switch-alist): Use command-line-do-script. New argument, compatible with GNU; --script or -script says "treat the following argument as a file to load in batch mode". man/ChangeLog addition: 2008-01-20 Aidan Kehoe <kehoea@parhasard.net> * xemacs/cmdargs.texi (Command Switches): Describe --script, -script.
author Aidan Kehoe <kehoea@parhasard.net>
date Sun, 20 Jan 2008 19:53:54 +0100
parents 8bbabcab2c42
children 13ce402e1736
files ChangeLog Makefile.in.in configure configure.ac lisp/ChangeLog lisp/startup.el man/ChangeLog man/xemacs/cmdargs.texi src/ChangeLog src/config.h.in src/emacs.c src/lread.c
diffstat 12 files changed, 150 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Jan 20 13:09:58 2008 +0100
+++ b/ChangeLog	Sun Jan 20 19:53:54 2008 +0100
@@ -1,3 +1,13 @@
+2008-01-20  Aidan Kehoe  <kehoea@parhasard.net>
+
+	* Makefile.in.in (SHEBANG_PROGNAME): 
+	New variable; a symbol link to the XEmacs binary to tell it it
+	should read a script from stdin. 
+	* configure.ac (XE_EXPAND_VARIABLE): 
+	Set SHEBANG_PROGNAME. 
+	* configure: 
+	Regenerate.
+
 2008-01-17  Aidan Kehoe  <kehoea@parhasard.net>
 
 	* configure.ac:
--- a/Makefile.in.in	Sun Jan 20 13:09:58 2008 +0100
+++ b/Makefile.in.in	Sun Jan 20 19:53:54 2008 +0100
@@ -92,6 +92,7 @@
 ## This will be the name of the generated binary and is set automatically
 ## by configure.
 PROGNAME=@PROGNAME@
+SHEBANG_PROGNAME=@SHEBANG_PROGNAME@
 
 ## ==================== Where To Install Things ====================
 
@@ -407,10 +408,12 @@
 	${INSTALL_PROGRAM} src/${PROGNAME} ${bindir}/${PROGNAME}-${version}.exe
 	-chmod 0755 ${bindir}/${PROGNAME}-${version}.exe
 	cd ${bindir} && $(RM) ./${PROGNAME} && ${LN_S} ${PROGNAME}-${version}.exe ./${PROGNAME}
+	cd ${bindir} && $(RM) ./${SHEBANG_PROGNAME} && ${LN_S} ${PROGNAME}-${version}.exe ./${SHEBANG_PROGNAME}
 # else
 	${INSTALL_PROGRAM} src/${PROGNAME} ${bindir}/${PROGNAME}-${version}
 	-chmod 0755 ${bindir}/${PROGNAME}-${version}
 	cd ${bindir} && $(RM) ./${PROGNAME} && ${LN_S} ${PROGNAME}-${version} ./${PROGNAME}
+	cd ${bindir} && $(RM) ./${SHEBANG_PROGNAME} && ${LN_S} ${PROGNAME}-${version}.exe ./${SHEBANG_PROGNAME}
 # endif /* CYGWIN */
 #endif /* WIN32_NATIVE */
 	if test "${prefix}" != "${exec_prefix}"; then \
--- a/configure	Sun Jan 20 13:09:58 2008 +0100
+++ b/configure	Sun Jan 20 19:53:54 2008 +0100
@@ -4851,9 +4851,11 @@
 
   version=${infodock_major_version}.${infodock_minor_version}.${infodock_build_version}
   PROGNAME=infodock
+  SHEBANG_PROGNAME=infodock-script
   CPPFLAGS="$CPPFLAGS -DINFODOCK"
 else
   PROGNAME=xemacs
+  SHEBANG_PROGNAME=xemacs-script
 fi
 
 cat >>confdefs.h <<_ACEOF
@@ -4861,6 +4863,11 @@
 _ACEOF
 
 
+cat >>confdefs.h <<_ACEOF
+#define SHEBANG_PROGNAME "${PROGNAME}-script"
+_ACEOF
+
+
 if test -n "$emacs_is_beta"; then beta=yes; else beta=no; fi
 test "${enable_error_checking_extents:=$beta}"    = yes && cat >>confdefs.h <<\_ACEOF
 #define ERROR_CHECK_EXTENTS 1
--- a/configure.ac	Sun Jan 20 13:09:58 2008 +0100
+++ b/configure.ac	Sun Jan 20 19:53:54 2008 +0100
@@ -1219,13 +1219,17 @@
   AC_DEFINE_UNQUOTED(INFODOCK_BUILD_VERSION, $infodock_build_version)
   version=${infodock_major_version}.${infodock_minor_version}.${infodock_build_version}
   PROGNAME=infodock
+  SHEBANG_PROGNAME=infodock-script
   CPPFLAGS="$CPPFLAGS -DINFODOCK"
 else
   PROGNAME=xemacs
+  SHEBANG_PROGNAME=xemacs-script
 fi
 
 AC_DEFINE_UNQUOTED(EMACS_PROGNAME, "$PROGNAME")
 
+AC_DEFINE_UNQUOTED(SHEBANG_PROGNAME, "${PROGNAME}-script")
+
 dnl ----------------------------------
 dnl Error checking and debugging flags
 dnl ----------------------------------
--- a/lisp/ChangeLog	Sun Jan 20 13:09:58 2008 +0100
+++ b/lisp/ChangeLog	Sun Jan 20 19:53:54 2008 +0100
@@ -1,3 +1,10 @@
+2008-01-20  Aidan Kehoe  <kehoea@parhasard.net>
+
+	* startup.el (command-line-do-script): New.
+	(command-switch-alist): Use command-line-do-script.
+	New argument, compatible with GNU; --script or -script says "treat
+	the following argument as a file to load in batch mode".
+
 2008-01-17  Mike Sperber  <mike@xemacs.org>
 
 	* files.el (insert-directory): Bind `coding-system-for-read' to
--- a/lisp/startup.el	Sun Jan 20 13:09:58 2008 +0100
+++ b/lisp/startup.el	Sun Jan 20 19:53:54 2008 +0100
@@ -234,6 +234,8 @@
     ("-eval"	. command-line-do-eval)
     ("-load"	. command-line-do-load)
     ("-l"	. command-line-do-load)
+    ("--script"	. command-line-do-script)
+    ("-script"	. command-line-do-script)
     ("-insert"	. command-line-do-insert)
     ("-i"	. command-line-do-insert)
     ("-kill"	. command-line-do-kill)
@@ -437,6 +439,12 @@
 	(setq file (expand-file-name file)))
     (load file nil t)))
 
+(defun command-line-do-script (arg)
+  "Load the named file of Lisp code into XEmacs.
+<file>"
+  (let ((file (pop command-line-args-left)))
+    (load file nil t t)))
+
 (defun command-line-do-insert (arg)
   "Insert file into the current buffer.
 <file>"
--- a/man/ChangeLog	Sun Jan 20 13:09:58 2008 +0100
+++ b/man/ChangeLog	Sun Jan 20 19:53:54 2008 +0100
@@ -1,3 +1,8 @@
+2008-01-20  Aidan Kehoe  <kehoea@parhasard.net>
+
+	* xemacs/cmdargs.texi (Command Switches): 
+	Describe --script, -script. 
+
 2007-12-17  Aidan Kehoe  <kehoea@parhasard.net>
 
 	* lispref/strings.texi (Formatting Strings):
--- a/man/xemacs/cmdargs.texi	Sun Jan 20 13:09:58 2008 +0100
+++ b/man/xemacs/cmdargs.texi	Sun Jan 20 19:53:54 2008 +0100
@@ -171,6 +171,27 @@
 Do not process early packages.  (For more information on startup issues
 concerning the package system, @xref{Startup Paths}.)
 
+@item -script @var{file}
+@item --script @var{file}
+Load @var{file} as a (compiled or interpreted) Lisp file; do not load
+any window-system or TTY code, do not load the user init file, the site
+file, or the early packages.  This is comparable to running
+@samp{xemacs} @samp{-batch} @samp{-l} @var{file}.
+
+You can also specify this flag implicitly by calling the XEmacs binary
+with a value for @samp{argv[0]} of @samp{xemacs-script}, normally by
+means of a symbolic link.  On a POSIX system, this means that adding the
+line:
+
+@example
+#!/usr/bin/env xemacs-script
+@end example
+
+at the start of an XEmacs Lisp file, and changing that file's
+permissions to executable, creates a script that can be invoked by
+typing the path to the file.  XEmacs has logic to ignore the #! line at
+the start of the script, so that won't cause an error.
+
 @item -vanilla
 This is equivalent to @samp{-q -no-site-file -no-early-packages}.
 
--- a/src/ChangeLog	Sun Jan 20 13:09:58 2008 +0100
+++ b/src/ChangeLog	Sun Jan 20 19:53:54 2008 +0100
@@ -1,3 +1,15 @@
+2008-01-20  Aidan Kehoe  <kehoea@parhasard.net>
+
+	* config.h.in: Add SHEBANG_PROGNAME.
+	* emacs.c (main_1): 
+	If we've been called using SHEBANG_PROGNAME, rewrite our arguments
+	to add a --script argument. 
+	Also, handle the --script argument by setting noninteractive and
+	vanilla. 
+	* lread.c (Fload_internal): 
+	If the first two characters of a file are #!, replace them with ;!
+	before they get to the Lisp reader. 
+
 2007-12-26  Aidan Kehoe  <kehoea@parhasard.net>
 
 	* casetab.c:
--- a/src/config.h.in	Sun Jan 20 13:09:58 2008 +0100
+++ b/src/config.h.in	Sun Jan 20 19:53:54 2008 +0100
@@ -91,6 +91,9 @@
 /* Program name */
 #undef EMACS_PROGNAME
 
+/* Name of the link to the program to be used with #! scripts */
+#undef SHEBANG_PROGNAME
+
 /* Allow s&m files to differentiate OS versions without having
    multiple files to maintain. */
 #undef OS_RELEASE
--- a/src/emacs.c	Sun Jan 20 13:09:58 2008 +0100
+++ b/src/emacs.c	Sun Jan 20 19:53:54 2008 +0100
@@ -955,6 +955,42 @@
   init_free_hook ();
 #endif
 
+#define SHEBANG_PROGNAME_LENGTH                                         \
+  (int)((sizeof (WEXTSTRING (SHEBANG_PROGNAME)) - sizeof (WEXTSTRING (""))))
+#define SHEBANG_EXE_PROGNAME_LENGTH                                     \
+  (int)(sizeof (WEXTSTRING (SHEBANG_PROGNAME ".exe"))                   \
+        - sizeof (WEXTSTRING ("")))
+
+  {
+    int progname_len = wext_strlen (argv[0]);
+    if (progname_len >= SHEBANG_PROGNAME_LENGTH)
+      {
+	if (!wext_strcmp_ascii (argv[0] +
+				(progname_len - SHEBANG_PROGNAME_LENGTH),
+				SHEBANG_PROGNAME)
+	    /* Allow trailing .exe. Don't check it, it could also be
+	       .com.  */
+	    || (progname_len >= SHEBANG_EXE_PROGNAME_LENGTH && 
+		!wext_strncmp_ascii
+		(argv[0] + (progname_len - SHEBANG_EXE_PROGNAME_LENGTH),
+		 SHEBANG_PROGNAME,
+		 SHEBANG_PROGNAME_LENGTH)))
+	  {
+	    Wexttext **newarr = alloca_array (Wexttext *, argc + 2);
+	    int j;
+
+	    newarr[0] = argv[0];
+	    newarr[1] = WEXTSTRING ("--script");
+	    for (j = 1; j < argc; ++j)
+	      {
+		newarr[j + 1] = argv[j];
+	      }
+	    argv = newarr;
+	    argc++;
+	  }
+      }
+  }
+
   sort_args (argc, argv);
 
 #if 0 /* defined (_SCO_DS)
@@ -1048,6 +1084,20 @@
       noninteractive = 1;
     }
 
+  {
+    int count_before = skip_args;
+    /* Handle the -script switch, which implies batch and vanilla. The -l
+       part of its functionality is implemented in Lisp. */
+    if (argmatch (argv, argc, "-script", "--script", 0, NULL,
+		  &skip_args))
+      {
+	noninteractive = 1;
+	vanilla_inhibiting = 1;
+      }
+
+    /* Don't actually discard this argument. */
+    skip_args = count_before;
+  }
 #ifdef WIN32_NATIVE
   {
     /* Since we aren't a console application, we can't easily be terminated
@@ -2690,6 +2740,7 @@
   { "-sd", "--show-dump-id", 105, 0 },
   { "-nd", "--no-dump-file", 95, 0 },
   { "-batch", "--batch", 88, 0 },
+  { "-script", "--script", 89, 1 },
 #ifdef WIN32_NATIVE
   { "-mswindows-termination-handle", 0, 84, 1 },
   { "-nuni", "--no-unicode-lib-calls", 83, 0 },
--- a/src/lread.c	Sun Jan 20 13:09:58 2008 +0100
+++ b/src/lread.c	Sun Jan 20 19:53:54 2008 +0100
@@ -744,6 +744,25 @@
     internal_bind_lisp_object (&Vfile_domain, Qnil);
 #endif
 
+    /* Is there a #!? If so, read it, and unread ;!.
+
+       GNU implement this by treating any #! anywhere in the source text as
+       commenting out the whole line. */
+    {
+      char shebangp[2];
+      int num_read;
+
+      num_read = Lstream_read (XLSTREAM (lispstream), shebangp,
+                               sizeof(shebangp));
+      if (sizeof(shebangp) == num_read
+	  && 0 == strncmp("#!", shebangp, sizeof(shebangp)))
+	{
+          shebangp[0] = ';';
+	}
+
+      Lstream_unread (XLSTREAM (lispstream), shebangp, num_read);
+    }
+
     /* Now determine what sort of ELC file we're reading in. */
     internal_bind_int (&load_byte_code_version, load_byte_code_version);
     if (reading_elc)