summaryrefslogtreecommitdiff
path: root/bin/ksh/misc.c
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>1998-06-25 19:02:48 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>1998-06-25 19:02:48 +0000
commitf7502f1aee86c9a3ebb4d6814e47a52727bc2d42 (patch)
tree0bd2bbab7d507d7dab286df7970c0101a3cb1807 /bin/ksh/misc.c
parentcc7df4d09ccd66ffca19799a357c049e1b5bf030 (diff)
pdksh-5.2.13 + local changes
Diffstat (limited to 'bin/ksh/misc.c')
-rw-r--r--bin/ksh/misc.c105
1 files changed, 56 insertions, 49 deletions
diff --git a/bin/ksh/misc.c b/bin/ksh/misc.c
index 5287f4e9dfa..192fba81232 100644
--- a/bin/ksh/misc.c
+++ b/bin/ksh/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.6 1997/06/19 13:58:45 kstailey Exp $ */
+/* $OpenBSD: misc.c,v 1.7 1998/06/25 19:02:11 millert Exp $ */
/*
* Miscellaneous functions
@@ -173,7 +173,10 @@ const struct option options[] = {
{ "vi-esccomplete", 0, OF_ANY }, /* non-standard */
#endif
{ "xtrace", 'x', OF_ANY },
- { NULL, 0, 0 }
+ /* Anonymous flags: used internally by shell only
+ * (not visable to user)
+ */
+ { (char *) 0, 0, OF_INTERNAL }, /* FTALKING_I */
};
/*
@@ -185,8 +188,8 @@ option(n)
{
int i;
- for (i = 0; options[i].name; i++)
- if (strcmp(options[i].name, n) == 0)
+ for (i = 0; i < NELEM(options); i++)
+ if (options[i].name && strcmp(options[i].name, n) == 0)
return i;
return -1;
@@ -232,8 +235,8 @@ printoptions(verbose)
/* verbose version */
shprintf("Current option settings\n");
- for (i = n = oi.opt_width = 0; options[i].name; i++)
- if (options[i].name[0]) {
+ for (i = n = oi.opt_width = 0; i < NELEM(options); i++)
+ if (options[i].name) {
len = strlen(options[i].name);
oi.opts[n].name = options[i].name;
oi.opts[n++].flag = i;
@@ -245,8 +248,8 @@ printoptions(verbose)
} else {
/* short version ala ksh93 */
shprintf("set");
- for (i = 0; options[i].name; i++)
- if (Flag(i) && options[i].name[0])
+ for (i = 0; i < NELEM(options); i++)
+ if (Flag(i) && options[i].name)
shprintf(" -o %s", options[i].name);
shprintf(newline);
}
@@ -259,7 +262,7 @@ getoptions()
char m[FNFLAGS + 1];
register char *cp = m;
- for (i = 0; options[i].name; i++)
+ for (i = 0; i < NELEM(options); i++)
if (options[i].c && Flag(i))
*cp++ = options[i].c;
*cp = 0;
@@ -309,8 +312,8 @@ change_flag(f, what, newval)
#ifdef OS2
;
#else /* OS2 */
- seteuid(getuid());
- setuid(getuid());
+ seteuid(ksheuid = getuid());
+ setuid(ksheuid);
setegid(getgid());
setgid(getgid());
#endif /* OS2 */
@@ -320,6 +323,11 @@ change_flag(f, what, newval)
#endif /* BRACE_EXPAND */
;
}
+ /* Changing interactive flag? */
+ if (f == FTALKING) {
+ if ((what == OF_CMDLINE || what == OF_SET) && procpid == kshpid)
+ Flag(FTALKING_I) = newval;
+ }
}
/* parse command line & set command arguments. returns the index of
@@ -334,28 +342,28 @@ parse_args(argv, what, setargsp)
static char cmd_opts[NELEM(options) + 3]; /* o:\0 */
static char set_opts[NELEM(options) + 5]; /* Ao;s\0 */
char *opts;
- char *array;
+ char *array = (char *) 0;
Getopt go;
int i, optc, set, sortargs = 0, arrayset = 0;
/* First call? Build option strings... */
if (cmd_opts[0] == '\0') {
- char *p;
+ char *p, *q;
- /* c is also in options[], but it needs a trailing : */
strcpy(cmd_opts, "o:"); /* see cmd_opts[] declaration */
p = cmd_opts + strlen(cmd_opts);
- for (i = 0; options[i].name; i++)
- if (options[i].c && (options[i].flags & OF_CMDLINE))
- *p++ = options[i].c;
- *p = '\0';
-
- strcpy(set_opts, "Ao;s"); /* see set_opts[] declaration */
- p = set_opts + strlen(set_opts);
- for (i = 0; options[i].name; i++)
- if (options[i].c && (options[i].flags & OF_SET))
- *p++ = options[i].c;
+ strcpy(set_opts, "A:o;s"); /* see set_opts[] declaration */
+ q = set_opts + strlen(set_opts);
+ for (i = 0; i < NELEM(options); i++) {
+ if (options[i].c) {
+ if (options[i].flags & OF_CMDLINE)
+ *p++ = options[i].c;
+ if (options[i].flags & OF_SET)
+ *q++ = options[i].c;
+ }
+ }
*p = '\0';
+ *q = '\0';
}
if (what == OF_CMDLINE) {
@@ -375,6 +383,7 @@ parse_args(argv, what, setargsp)
switch (optc) {
case 'A':
arrayset = set ? 1 : -1;
+ array = go.optarg;
break;
case 'o':
@@ -413,7 +422,7 @@ parse_args(argv, what, setargsp)
sortargs = 1;
break;
}
- for (i = 0; options[i].name; i++)
+ for (i = 0; i < NELEM(options); i++)
if (optc == options[i].c
&& (what & options[i].flags))
{
@@ -421,7 +430,7 @@ parse_args(argv, what, setargsp)
set);
break;
}
- if (!options[i].name) {
+ if (i == NELEM(options)) {
internal_errorf(1, "parse_args: `%c'", optc);
return -1; /* not reached */
}
@@ -442,18 +451,10 @@ parse_args(argv, what, setargsp)
*setargsp = !arrayset && ((go.info & GI_MINUSMINUS)
|| argv[go.optind]);
- if (arrayset) {
- array = argv[go.optind++];
- if (!array) {
- bi_errorf("-A: missing array name");
- return -1;
- }
- if (!*array || *skip_varname(array, FALSE)) {
- bi_errorf("%s: is not an identifier", array);
- return -1;
- }
- } else
- array = (char *) 0; /* keep gcc happy */
+ if (arrayset && (!*array || *skip_varname(array, FALSE))) {
+ bi_errorf("%s: is not an identifier", array);
+ return -1;
+ }
if (sortargs) {
for (i = go.optind; argv[i]; i++)
;
@@ -588,7 +589,7 @@ has_globbing(xp, xpe)
return 0;
in_bracket = 0;
}
- } else if ((c & 0x80) && strchr("*+?@!", c & 0x7f)) {
+ } else if ((c & 0x80) && strchr("*+?@! ", c & 0x7f)) {
saw_glob = 1;
if (in_bracket)
bnest++;
@@ -657,8 +658,11 @@ do_gmatch(s, se, p, pe, isfile)
} while (s++ < se);
return 0;
-#ifdef KSH
- /* [*+?@!](pattern|pattern|..) */
+ /*
+ * [*+?@!](pattern|pattern|..)
+ *
+ * Not ifdef'd KSH as this is needed for ${..%..}, etc.
+ */
case 0x80|'+': /* matches one or more times */
case 0x80|'*': /* matches zero or more times */
if (!(prest = pat_scan(p, pe, 0)))
@@ -687,6 +691,7 @@ do_gmatch(s, se, p, pe, isfile)
case 0x80|'?': /* matches zero or once */
case 0x80|'@': /* matches one of the patterns */
+ case 0x80|' ': /* simile for @ */
if (!(prest = pat_scan(p, pe, 0)))
return 0;
s--;
@@ -732,7 +737,6 @@ do_gmatch(s, se, p, pe, isfile)
return 1;
}
return 0;
-#endif /* KSH */
default:
if (sc != p[-1])
@@ -757,8 +761,12 @@ cclass(p, sub)
c = *p++;
if (ISMAGIC(c)) {
c = *p++;
- if ((c & 0x80) && !ISMAGIC(c))
+ if ((c & 0x80) && !ISMAGIC(c)) {
c &= 0x7f;/* extended pattern matching: *+?@! */
+ /* XXX the ( char isn't handled as part of [] */
+ if (c == ' ') /* simile for @: plain (..) */
+ c = '(' /*)*/;
+ }
}
if (c == '\0')
/* No closing ] - act as if the opening [ was quoted */
@@ -800,7 +808,7 @@ pat_scan(p, pe, match_sep)
if ((*++p == /*(*/ ')' && nest-- == 0)
|| (*p == '|' && match_sep && nest == 0))
return ++p;
- if ((*p & 0x80) && strchr("*+?@!", *p & 0x7f))
+ if ((*p & 0x80) && strchr("*+?@! ", *p & 0x7f))
nest++;
}
return (const unsigned char *) 0;
@@ -945,8 +953,7 @@ ksh_getopt_reset(go, flags)
* Used for 'typeset -LZ4'.
* - accepts +c as well as -c IF the GF_PLUSOPT flag is present. If an
* option starting with + is accepted, the GI_PLUS flag will be set
- * in go->info. Once a - or + has been seen, all other options must
- * start with the same character.
+ * in go->info.
*/
int
ksh_getopt(argv, go, options)
@@ -968,15 +975,15 @@ ksh_getopt(argv, go, options)
return EOF;
}
if (arg == (char *) 0
- || ((flag != '-' || (go->info & GI_PLUS))
- && (!(go->flags & GF_PLUSOPT) || (go->info & GI_MINUS)
- || flag != '+'))
+ || ((flag != '-' ) /* neither a - nor a + (if + allowed) */
+ && (!(go->flags & GF_PLUSOPT) || flag != '+'))
|| (c = arg[1]) == '\0')
{
go->p = 0;
return EOF;
}
go->optind++;
+ go->info &= ~(GI_MINUS|GI_PLUS);
go->info |= flag == '-' ? GI_MINUS : GI_PLUS;
}
go->p++;