about summary refs log tree commit diff
path: root/Src/init.c
diff options
context:
space:
mode:
authorBart Schaefer <barts@users.sourceforge.net>2012-03-07 06:10:42 +0000
committerBart Schaefer <barts@users.sourceforge.net>2012-03-07 06:10:42 +0000
commitfa8a0e241c8ac9eb48ab9fc2d8bb3105088e0db9 (patch)
treee0369814c4201b9dfe4ca087620b574f4fb35059 /Src/init.c
parenta0d24f0bf3bfc7509557089bd6e0336f8da0494f (diff)
downloadzsh-fa8a0e241c8ac9eb48ab9fc2d8bb3105088e0db9.tar.gz
zsh-fa8a0e241c8ac9eb48ab9fc2d8bb3105088e0db9.tar.xz
zsh-fa8a0e241c8ac9eb48ab9fc2d8bb3105088e0db9.zip
30320: "emulate" accepts invocation-time flags; other small doc tweaks.
Diffstat (limited to 'Src/init.c')
-rw-r--r--Src/init.c119
1 files changed, 71 insertions, 48 deletions
diff --git a/Src/init.c b/Src/init.c
index 9820070fb..1212a48d9 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -215,6 +215,7 @@ loop(int toplevel, int justonce)
     return LOOP_OK;
 }
 
+/* Shared among parseargs(), parseopts(), init_io(), and init_misc() */
 static char *cmd;
 static int restricted;
 
@@ -222,9 +223,7 @@ static int restricted;
 static void
 parseargs(char **argv, char **runscript)
 {
-    int optionbreak = 0;
     char **x;
-    int action, optno;
     LinkList paramlist;
 
     argzero = *argv++;
@@ -247,6 +246,49 @@ parseargs(char **argv, char **runscript)
     opts[SHINSTDIN] = 0;
     opts[SINGLECOMMAND] = 0;
 
+    cmd = parseopts(NULL, &argv);
+
+    paramlist = znewlinklist();
+    if (*argv) {
+	if (unset(SHINSTDIN)) {
+	    if (cmd)
+		argzero = *argv;
+	    else
+		*runscript = *argv;
+	    opts[INTERACTIVE] &= 1;
+	    argv++;
+	}
+	while (*argv)
+	    zaddlinknode(paramlist, ztrdup(*argv++));
+    } else if (!cmd)
+	opts[SHINSTDIN] = 1;
+    if(isset(SINGLECOMMAND))
+	opts[INTERACTIVE] &= 1;
+    opts[INTERACTIVE] = !!opts[INTERACTIVE];
+    if (opts[MONITOR] == 2)
+	opts[MONITOR] = opts[INTERACTIVE];
+    if (opts[HASHDIRS] == 2)
+	opts[HASHDIRS] = opts[INTERACTIVE];
+    pparams = x = (char **) zshcalloc((countlinknodes(paramlist) + 1) * sizeof(char *));
+
+    while ((*x++ = (char *)getlinknode(paramlist)));
+    free(paramlist);
+    argzero = ztrdup(argzero);
+}
+
+/**/
+mod_export char *
+parseopts(char *nam, char ***argvp)
+{
+    int optionbreak = 0;
+    int action, optno;
+    char *cmd = 0;	/* deliberately hides static */
+    char **argv = *argvp;
+
+#define WARN_OPTION(F, S) if (nam) zwarnnam(nam, F, S); else zerr(F, S)
+#define LAST_OPTION(N) \
+	if (nam) { if (*argv) argv++; goto doneargv; } else exit(N)
+
     /* loop through command line options (begins with "-" or "+") */
     while (!optionbreak && *argv && (**argv == '-' || **argv == '+')) {
 	char *args = *argv;
@@ -267,11 +309,11 @@ parseargs(char **argv, char **runscript)
 		if (!strcmp(*argv, "version")) {
 		    printf("zsh %s (%s-%s-%s)\n",
 			    ZSH_VERSION, MACHTYPE, VENDOR, OSTYPE);
-		    exit(0);
+		    LAST_OPTION(0);
 		}
 		if (!strcmp(*argv, "help")) {
 		    printhelp();
-		    exit(0);
+		    LAST_OPTION(0);
 		}
 		/* `-' characters are allowed in long options */
 		for(args = *argv; *args; args++)
@@ -292,73 +334,54 @@ parseargs(char **argv, char **runscript)
 		if (!*++*argv)
 		    argv++;
 		if (!*argv) {
-		    zerr("string expected after -o");
-		    exit(1);
+		    WARN_OPTION("string expected after -o", NULL);
+		    LAST_OPTION(1);
 		}
 	    longoptions:
 		if (!(optno = optlookup(*argv))) {
-		    zerr("no such option: %s", *argv);
-		    exit(1);
-		} else if (optno == RESTRICTED)
+		    WARN_OPTION("no such option: %s", *argv);
+		    LAST_OPTION(1);
+		} else if (optno == RESTRICTED && !nam)
 		    restricted = action;
-		else
-		    dosetopt(optno, action, 1);
+		else if ((optno == EMACSMODE || optno == VIMODE) && nam)
+		    WARN_OPTION("can't change option: %s", *argv);
+		else if (dosetopt(optno, action, !nam) && nam)
+		    WARN_OPTION("can't change option: %s", *argv);
               break;
 	    } else if (isspace(STOUC(**argv))) {
 		/* zsh's typtab not yet set, have to use ctype */
 		while (*++*argv)
 		    if (!isspace(STOUC(**argv))) {
-		    badoptionstring:
-			zerr("bad option string: `%s'", args);
-			exit(1);
+		     badoptionstring:
+			WARN_OPTION("bad option string: '%s'", args);
+			LAST_OPTION(1);
 		    }
 		break;
 	    } else {
 	    	if (!(optno = optlookupc(**argv))) {
-		    zerr("bad option: -%c", **argv);
-		    exit(1);
-		} else if (optno == RESTRICTED)
+		    WARN_OPTION("bad option: -%c", **argv);
+		    LAST_OPTION(1);
+		} else if (optno == RESTRICTED && !nam)
 		    restricted = action;
-		else
-		    dosetopt(optno, action, 1);
+		else if ((optno == EMACSMODE || optno == VIMODE) && nam)
+		    WARN_OPTION("can't change option: %s", *argv);
+		else if (dosetopt(optno, action, !nam) && nam)
+		    WARN_OPTION("can't change option: -%c", **argv);
 	    }
 	}
 	argv++;
     }
-    doneoptions:
-    paramlist = znewlinklist();
+ doneoptions:
     if (cmd) {
 	if (!*argv) {
-	    zerr("string expected after -%s", cmd);
-	    exit(1);
+	    WARN_OPTION("string expected after -%s", cmd);
+	    LAST_OPTION(1);
 	}
 	cmd = *argv++;
     }
-    if (*argv) {
-	if (unset(SHINSTDIN)) {
-	    if (cmd)
-		argzero = *argv;
-	    else
-		*runscript = *argv;
-	    opts[INTERACTIVE] &= 1;
-	    argv++;
-	}
-	while (*argv)
-	    zaddlinknode(paramlist, ztrdup(*argv++));
-    } else if (!cmd)
-	opts[SHINSTDIN] = 1;
-    if(isset(SINGLECOMMAND))
-	opts[INTERACTIVE] &= 1;
-    opts[INTERACTIVE] = !!opts[INTERACTIVE];
-    if (opts[MONITOR] == 2)
-	opts[MONITOR] = opts[INTERACTIVE];
-    if (opts[HASHDIRS] == 2)
-	opts[HASHDIRS] = opts[INTERACTIVE];
-    pparams = x = (char **) zshcalloc((countlinknodes(paramlist) + 1) * sizeof(char *));
-
-    while ((*x++ = (char *)getlinknode(paramlist)));
-    free(paramlist);
-    argzero = ztrdup(argzero);
+ doneargv:
+    *argvp = argv;
+    return cmd;
 }
 
 /**/