Feature, implement :main command

Neil Mitchell <[email protected]>
Newsgroups gmane.comp.lang.haskell.cvs.hugs
Message-ID <[email protected]>
Hi,

Attached is a patch that adds a :main command to hugs. :main arguments
is equivalent to arguments `withArgs` main, but easier to use because
you don't need to import anything extra, and the arguments are parsed
as if they were arguments - i.e. :main my test "of this", instead of
["my","test","of this"] `withArgs` main.

Since i don't know a whole lot about this area of the compiler, and it
is changing the user interface for everyone, I thought it best that
someone checks its at least sane before i do anything with it.

Thanks

Neil

_______________________________________________
Cvs-hugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/cvs-hugs
main.patch (application/octet-stream, 2.8 KB)
Index: src/hugs.c
===================================================================
RCS file: /home/cvs/root/hugs98/src/hugs.c,v
retrieving revision 1.145
diff -u -r1.145 hugs.c
--- src/hugs.c	16 Sep 2005 16:55:10 -0000	1.145
+++ src/hugs.c	30 Oct 2005 12:20:51 -0000
@@ -245,8 +245,8 @@
  {":type",   TYPEOF}, {":!",    SYSTEM},  {":load",    LOAD},
  {":reload", RELOAD}, {":gc",   COLLECT}, {":edit",    EDIT},
  {":quit",   QUIT},   {":set",  SET},     {":find",    FIND},
  {":names",  NAMES},  {":info", INFO},    {":module",  SETMODULE}, 
- {":browse", BROWSE},
+ {":browse", BROWSE}, {":main", MAIN},
 #if EXPLAIN_INSTANCE_RESOLUTION
  {":xplain", XPLAIN},
 #endif
@@ -276,6 +276,7 @@
     Printf(":names [pat]        list names currently in scope\n");
     Printf(":info <names>       describe named objects\n");
     Printf(":browse <modules>   browse names exported by <modules>\n");
+    Printf(":main <aruments>    run the main function with the given arguments\n");
 #if EXPLAIN_INSTANCE_RESOLUTION
     Printf(":xplain <context>   explain instance resolution for <context>\n");
 #endif
@@ -539,6 +540,50 @@
 }
 #endif
 
+static Void local runmain() {
+    int MaxArgs = 10;
+    int ArgPos = 1;
+    int State = 0;
+    String s = readLine();
+    String c;
+    String args[11];
+    args[0] = "Hugs";
+
+    /*
+	parse the command line
+	repeated spaces are ignored
+	quotes are the delimiters, if not then spaces
+
+	State:
+	0 - outside of everything
+	1 - inside a space delimited lump
+	2 - inside a quote delimited lump
+    */
+    for (c = s; *c != 0; c++) {
+	if (State == 0) {
+	    if (*c != ' ') {
+		if (*c == '\"') {
+		    c++;
+		    State = 2;
+		} else
+		    State = 1;
+		if (ArgPos < MaxArgs)
+		    args[ArgPos++] = c;
+	    }
+	} else if ((State == 1 && *c == ' ') ||
+	           (State == 2 && *c == '\"')) {
+	    State = 0;
+	    *c = 0;
+	}
+    }
+
+    setHugsArgs(ArgPos, args);
+
+    stringInput((LPSTR) "main");
+    input(BREAK);
+    doCommand();
+}
+
 /* --------------------------------------------------------------------------
  * Enhanced help system:  print current list of scripts or give information
  * about an object.
@@ -1098,6 +1143,8 @@
 				 cellsRecovered);
 			  break;
 	    case NOCMD  : break;
+	    case MAIN: runmain();
+	    	  break;
 #ifdef __SYMBIAN32__
         case PRNDIR : printDir();
               break;
Index: src/command.h
===================================================================
RCS file: /home/cvs/root/hugs98/src/command.h,v
retrieving revision 1.14
diff -u -r1.14 command.h
--- src/command.h	14 Oct 2003 13:56:21 -0000	1.14
+++ src/command.h	30 Oct 2005 12:20:51 -0000
@@ -46,6 +46,7 @@
 #ifdef __SYMBIAN32__
 #define PRNDIR  21
 #endif
+#define MAIN	22
 
 #if OBSERVATIONS
 /*-------------------------------------------------------------------------*
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.