2009-04-22 18:46:11 +02:00
|
|
|
/* $OpenBSD: misc.c,v 1.37 2009/04/19 20:34:05 sthen Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
/* $OpenBSD: path.c,v 1.12 2005/03/30 17:16:37 deraadt Exp $ */
|
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
2012-01-04 23:38:33 +01:00
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
|
|
|
|
* 2011, 2012
|
2009-05-16 18:59:42 +02:00
|
|
|
* Thorsten Glaser <tg@mirbsd.org>
|
|
|
|
*
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
|
|
|
* is granted to deal in this work without restriction, including un-
|
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
*
|
|
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
|
|
* damage or existence of a defect, except proven that it results out
|
|
|
|
* of said person's immediate fault when using the work as intended.
|
|
|
|
*/
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
#include "sh.h"
|
2009-04-03 11:39:07 +02:00
|
|
|
#if !HAVE_GETRUSAGE
|
|
|
|
#include <sys/times.h>
|
|
|
|
#endif
|
2007-01-18 16:50:32 +01:00
|
|
|
#if HAVE_GRP_H
|
2006-11-12 14:35:29 +01:00
|
|
|
#include <grp.h>
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2012-05-04 22:08:25 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.188 2012/05/04 20:08:25 tg Exp $");
|
2006-11-10 05:07:59 +01:00
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* type bits for unsigned char */
|
|
|
|
unsigned char chtypes[UCHAR_MAX + 1];
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-17 22:59:30 +01:00
|
|
|
static const unsigned char *pat_scan(const unsigned char *,
|
|
|
|
const unsigned char *, bool);
|
2005-05-23 05:06:10 +02:00
|
|
|
static int do_gmatch(const unsigned char *, const unsigned char *,
|
|
|
|
const unsigned char *, const unsigned char *);
|
|
|
|
static const unsigned char *cclass(const unsigned char *, int);
|
2009-10-30 15:37:43 +01:00
|
|
|
#ifdef TIOCSCTTY
|
2007-01-18 21:54:30 +01:00
|
|
|
static void chvt(const char *);
|
2006-11-10 02:13:52 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-24 20:05:49 +01:00
|
|
|
/*XXX this should go away */
|
|
|
|
static int make_path(const char *, const char *, char **, XString *, int *);
|
|
|
|
|
2010-09-14 23:26:19 +02:00
|
|
|
#ifdef SETUID_CAN_FAIL_WITH_EAGAIN
|
|
|
|
/* we don't need to check for other codes, EPERM won't happen */
|
|
|
|
#define DO_SETUID(func, argvec) do { \
|
2010-09-19 21:28:23 +02:00
|
|
|
if ((func argvec) && errno == EAGAIN) \
|
|
|
|
errorf("%s failed with EAGAIN, probably due to a" \
|
|
|
|
" too low process limit; aborting", #func); \
|
2010-09-14 23:26:19 +02:00
|
|
|
} while (/* CONSTCOND */ 0)
|
|
|
|
#else
|
|
|
|
#define DO_SETUID(func, argvec) func argvec
|
|
|
|
#endif
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
|
|
|
* Fast character classes
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
setctypes(const char *s, int t)
|
|
|
|
{
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
unsigned int i;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (t & C_IFS) {
|
2008-02-27 02:00:10 +01:00
|
|
|
for (i = 0; i < UCHAR_MAX + 1; i++)
|
2005-05-23 05:06:10 +02:00
|
|
|
chtypes[i] &= ~C_IFS;
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* include \0 in C_IFS */
|
|
|
|
chtypes[0] |= C_IFS;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
while (*s != 0)
|
2008-02-27 02:00:10 +01:00
|
|
|
chtypes[(unsigned char)*s++] |= t;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
initctypes(void)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
for (c = 'a'; c <= 'z'; c++)
|
|
|
|
chtypes[c] |= C_ALPHA;
|
|
|
|
for (c = 'A'; c <= 'Z'; c++)
|
|
|
|
chtypes[c] |= C_ALPHA;
|
|
|
|
chtypes['_'] |= C_ALPHA;
|
|
|
|
setctypes("0123456789", C_DIGIT);
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* \0 added automatically */
|
|
|
|
setctypes(" \t\n|&;<>()", C_LEX1);
|
2005-05-23 05:06:10 +02:00
|
|
|
setctypes("*@#!$-?", C_VAR1);
|
|
|
|
setctypes(" \t\n", C_IFSWS);
|
|
|
|
setctypes("=-+?", C_SUBOP1);
|
2009-10-30 01:57:39 +01:00
|
|
|
setctypes("\t\n \"#$&'()*;<=>?[\\]`|", C_QUOTE);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* called from XcheckN() to grow buffer */
|
|
|
|
char *
|
2011-10-26 00:25:07 +02:00
|
|
|
Xcheck_grow(XString *xsp, const char *xp, size_t more)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2007-03-04 01:13:17 +01:00
|
|
|
const char *old_beg = xsp->beg;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2010-09-14 23:26:19 +02:00
|
|
|
if (more < xsp->len)
|
|
|
|
more = xsp->len;
|
|
|
|
/* (xsp->len + X_EXTRA) never overflows */
|
|
|
|
checkoktoadd(more, xsp->len + X_EXTRA);
|
|
|
|
xsp->beg = aresize(xsp->beg, (xsp->len += more) + X_EXTRA, xsp->areap);
|
2005-05-23 05:06:10 +02:00
|
|
|
xsp->end = xsp->beg + xsp->len;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (xsp->beg + (xp - old_beg));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-09-20 18:40:58 +02:00
|
|
|
#define SHFLAGS_DEFNS
|
|
|
|
#include "sh_flags.h"
|
|
|
|
|
2007-07-01 18:49:18 +02:00
|
|
|
const struct shoption options[] = {
|
2009-09-20 18:40:58 +02:00
|
|
|
#define SHFLAGS_ITEMS
|
|
|
|
#include "sh_flags.h"
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* translate -o option into F* constant (also used for test -o option)
|
|
|
|
*/
|
2007-05-13 20:33:29 +02:00
|
|
|
size_t
|
2005-05-23 05:06:10 +02:00
|
|
|
option(const char *n)
|
|
|
|
{
|
2007-05-13 20:33:29 +02:00
|
|
|
size_t i;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2010-01-01 19:02:01 +01:00
|
|
|
if ((n[0] == '-' || n[0] == '+') && n[1] && !n[2]) {
|
|
|
|
for (i = 0; i < NELEM(options); i++)
|
|
|
|
if (options[i].c == n[1])
|
|
|
|
return (i);
|
|
|
|
} else for (i = 0; i < NELEM(options); i++)
|
2005-05-23 05:06:10 +02:00
|
|
|
if (options[i].name && strcmp(options[i].name, n) == 0)
|
2007-05-13 20:33:29 +02:00
|
|
|
return (i);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-05-13 20:33:29 +02:00
|
|
|
return ((size_t)-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct options_info {
|
|
|
|
int opt_width;
|
2006-11-10 02:44:40 +01:00
|
|
|
int opts[NELEM(options)];
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
2011-08-27 20:06:52 +02:00
|
|
|
static char *options_fmt_entry(char *, size_t, int, const void *);
|
2009-11-10 00:35:11 +01:00
|
|
|
static void printoptions(bool);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* format a single select menu item */
|
|
|
|
static char *
|
2011-08-27 20:06:52 +02:00
|
|
|
options_fmt_entry(char *buf, size_t buflen, int i, const void *arg)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-12 15:58:16 +01:00
|
|
|
const struct options_info *oi = (const struct options_info *)arg;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
shf_snprintf(buf, buflen, "%-*s %s",
|
2006-11-10 02:44:40 +01:00
|
|
|
oi->opt_width, options[oi->opts[i]].name,
|
|
|
|
Flag(oi->opts[i]) ? "on" : "off");
|
2009-06-08 22:06:50 +02:00
|
|
|
return (buf);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-11-10 00:35:11 +01:00
|
|
|
printoptions(bool verbose)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-08-27 20:06:52 +02:00
|
|
|
size_t i = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (verbose) {
|
2011-08-27 20:06:52 +02:00
|
|
|
ssize_t n = 0, len, octs = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
struct options_info oi;
|
|
|
|
|
|
|
|
/* verbose version */
|
2008-04-01 23:50:58 +02:00
|
|
|
shf_puts("Current option settings\n", shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-11-10 00:35:11 +01:00
|
|
|
oi.opt_width = 0;
|
2011-08-27 20:06:52 +02:00
|
|
|
while (i < NELEM(options)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (options[i].name) {
|
2006-11-10 02:44:40 +01:00
|
|
|
oi.opts[n++] = i;
|
2009-11-10 00:35:11 +01:00
|
|
|
len = strlen(options[i].name);
|
|
|
|
if (len > octs)
|
|
|
|
octs = len;
|
2009-11-28 15:28:03 +01:00
|
|
|
len = utf_mbswidth(options[i].name);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (len > oi.opt_width)
|
|
|
|
oi.opt_width = len;
|
|
|
|
}
|
2009-11-10 00:35:11 +01:00
|
|
|
++i;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
print_columns(shl_stdout, n, options_fmt_entry, &oi,
|
2009-11-10 00:35:11 +01:00
|
|
|
octs + 4, oi.opt_width + 4, true);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
2010-08-28 22:22:24 +02:00
|
|
|
/* short version like AT&T ksh93 */
|
2011-09-07 17:24:22 +02:00
|
|
|
shf_puts(Tset, shl_stdout);
|
2009-11-10 00:35:11 +01:00
|
|
|
while (i < (int)NELEM(options)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (Flag(i) && options[i].name)
|
2010-08-28 20:50:58 +02:00
|
|
|
shprintf("%s %s %s", null, "-o",
|
|
|
|
options[i].name);
|
2009-11-10 00:35:11 +01:00
|
|
|
++i;
|
|
|
|
}
|
2007-07-22 15:34:52 +02:00
|
|
|
shf_putc('\n', shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
getoptions(void)
|
|
|
|
{
|
2011-08-27 20:06:52 +02:00
|
|
|
size_t i;
|
2011-03-26 22:46:06 +01:00
|
|
|
char m[(int)FNFLAGS + 1];
|
2005-05-23 05:06:10 +02:00
|
|
|
char *cp = m;
|
|
|
|
|
|
|
|
for (i = 0; i < NELEM(options); i++)
|
|
|
|
if (options[i].c && Flag(i))
|
|
|
|
*cp++ = options[i].c;
|
2008-10-28 15:32:43 +01:00
|
|
|
strndupx(cp, m, cp - m, ATEMP);
|
|
|
|
return (cp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* change a Flag(*) value; takes care of special actions */
|
|
|
|
void
|
2009-08-08 15:08:53 +02:00
|
|
|
change_flag(enum sh_flag f, int what, unsigned int newval)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2009-08-08 15:08:53 +02:00
|
|
|
unsigned char oldval;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
oldval = Flag(f);
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* needed for tristates */
|
|
|
|
Flag(f) = newval ? 1 : 0;
|
2009-04-05 14:35:32 +02:00
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
if (f == FMONITOR) {
|
|
|
|
if (what != OF_CMDLINE && newval != oldval)
|
|
|
|
j_change();
|
2009-04-05 14:35:32 +02:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if ((
|
2009-09-24 19:15:33 +02:00
|
|
|
#if !MKSH_S_NOVI
|
2007-06-15 23:55:20 +02:00
|
|
|
f == FVI ||
|
|
|
|
#endif
|
|
|
|
f == FEMACS || f == FGMACS) && newval) {
|
2009-09-24 19:15:33 +02:00
|
|
|
#if !MKSH_S_NOVI
|
2007-06-15 23:55:20 +02:00
|
|
|
Flag(FVI) =
|
|
|
|
#endif
|
|
|
|
Flag(FEMACS) = Flag(FGMACS) = 0;
|
2009-08-08 15:08:53 +02:00
|
|
|
Flag(f) = (unsigned char)newval;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (f == FPRIVILEGED && oldval && !newval) {
|
|
|
|
/* Turning off -p? */
|
2006-11-10 07:27:09 +01:00
|
|
|
|
2011-01-21 22:04:48 +01:00
|
|
|
/*XXX this can probably be optimised */
|
|
|
|
kshegid = kshgid = getgid();
|
|
|
|
#if HAVE_SETRESUGID
|
2010-09-14 23:26:19 +02:00
|
|
|
DO_SETUID(setresgid, (kshegid, kshegid, kshegid));
|
2006-11-12 13:56:10 +01:00
|
|
|
#if HAVE_SETGROUPS
|
2010-09-14 23:26:19 +02:00
|
|
|
/* setgroups doesn't EAGAIN on Linux */
|
2006-11-10 07:27:09 +01:00
|
|
|
setgroups(1, &kshegid);
|
2006-11-12 13:56:10 +01:00
|
|
|
#endif
|
2010-09-14 23:26:19 +02:00
|
|
|
DO_SETUID(setresuid, (ksheuid, ksheuid, ksheuid));
|
2006-11-12 13:56:10 +01:00
|
|
|
#else
|
2010-09-14 23:26:19 +02:00
|
|
|
/* seteuid, setegid, setgid don't EAGAIN on Linux */
|
2012-04-01 18:55:16 +02:00
|
|
|
ksheuid = kshuid = getuid();
|
|
|
|
#ifndef __BEOS__
|
|
|
|
/* BeOS doesn't have different UIDs */
|
|
|
|
seteuid(ksheuid);
|
|
|
|
#endif
|
2010-09-14 23:26:19 +02:00
|
|
|
DO_SETUID(setuid, (ksheuid));
|
2012-04-01 18:55:16 +02:00
|
|
|
#ifndef __BEOS__
|
|
|
|
/* BeOS doesn't have different GIDs */
|
2011-01-21 22:04:48 +01:00
|
|
|
setegid(kshegid);
|
2012-04-01 18:55:16 +02:00
|
|
|
#endif
|
2006-11-16 14:35:07 +01:00
|
|
|
setgid(kshegid);
|
2006-11-12 13:56:10 +01:00
|
|
|
#endif
|
2010-01-28 16:18:51 +01:00
|
|
|
} else if ((f == FPOSIX || f == FSH) && newval) {
|
|
|
|
Flag(FPOSIX) = Flag(FSH) = Flag(FBRACEEXPAND) = 0;
|
|
|
|
Flag(f) = (unsigned char)newval;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
/* Changing interactive flag? */
|
|
|
|
if (f == FTALKING) {
|
|
|
|
if ((what == OF_CMDLINE || what == OF_SET) && procpid == kshpid)
|
2009-08-08 15:08:53 +02:00
|
|
|
Flag(FTALKING_I) = (unsigned char)newval;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* Parse command line and set command arguments. Returns the index of
|
2005-05-23 05:06:10 +02:00
|
|
|
* non-option arguments, -1 if there is an error.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
parse_args(const char **argv,
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* OF_CMDLINE or OF_SET */
|
|
|
|
int what,
|
2009-12-05 18:43:50 +01:00
|
|
|
bool *setargsp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
static char cmd_opts[NELEM(options) + 5]; /* o:T:\0 */
|
2006-11-10 02:52:23 +01:00
|
|
|
static char set_opts[NELEM(options) + 6]; /* A:o;s\0 */
|
2007-06-07 01:28:17 +02:00
|
|
|
char set, *opts;
|
2007-03-04 01:13:17 +01:00
|
|
|
const char *array = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
Getopt go;
|
2007-05-13 20:33:29 +02:00
|
|
|
size_t i;
|
2011-05-05 01:16:05 +02:00
|
|
|
int optc, arrayset = 0;
|
|
|
|
bool sortargs = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
/* First call? Build option strings... */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (cmd_opts[0] == '\0') {
|
2006-11-10 02:52:23 +01:00
|
|
|
char *p = cmd_opts, *q = set_opts;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* see cmd_opts[] declaration */
|
2006-11-10 02:52:23 +01:00
|
|
|
*p++ = 'o';
|
|
|
|
*p++ = ':';
|
2009-10-30 15:37:43 +01:00
|
|
|
#if !defined(MKSH_SMALL) || defined(TIOCSCTTY)
|
2006-11-10 02:52:23 +01:00
|
|
|
*p++ = 'T';
|
|
|
|
*p++ = ':';
|
2007-01-26 19:27:34 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
/* see set_opts[] declaration */
|
2006-11-10 02:52:23 +01:00
|
|
|
*q++ = 'A';
|
|
|
|
*q++ = ':';
|
|
|
|
*q++ = 'o';
|
|
|
|
*q++ = ';';
|
|
|
|
*q++ = 's';
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
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) {
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
const char *p = argv[0], *q;
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* Set FLOGIN before parsing options so user can clear
|
2005-05-23 05:06:10 +02:00
|
|
|
* flag using +l.
|
|
|
|
*/
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
if (*p != '-')
|
|
|
|
for (q = p; *q; )
|
|
|
|
if (*q++ == '/')
|
|
|
|
p = q;
|
|
|
|
Flag(FLOGIN) = (*p == '-');
|
2005-05-23 05:06:10 +02:00
|
|
|
opts = cmd_opts;
|
|
|
|
} else if (what == OF_FIRSTTIME) {
|
|
|
|
opts = cmd_opts;
|
|
|
|
} else
|
|
|
|
opts = set_opts;
|
|
|
|
ksh_getopt_reset(&go, GF_ERROR|GF_PLUSOPT);
|
2006-05-10 20:54:13 +02:00
|
|
|
while ((optc = ksh_getopt(argv, &go, opts)) != -1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
set = (go.info & GI_PLUS) ? 0 : 1;
|
|
|
|
switch (optc) {
|
|
|
|
case 'A':
|
|
|
|
if (what == OF_FIRSTTIME)
|
|
|
|
break;
|
|
|
|
arrayset = set ? 1 : -1;
|
|
|
|
array = go.optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
if (what == OF_FIRSTTIME)
|
|
|
|
break;
|
|
|
|
if (go.optarg == NULL) {
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* lone -o: print options
|
2005-05-23 05:06:10 +02:00
|
|
|
*
|
|
|
|
* Note that on the command line, -o requires
|
|
|
|
* an option (ie, can't get here if what is
|
|
|
|
* OF_CMDLINE).
|
|
|
|
*/
|
|
|
|
printoptions(set);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = option(go.optarg);
|
2007-05-13 20:33:29 +02:00
|
|
|
if ((i != (size_t)-1) && set == Flag(i))
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* Don't check the context if the flag
|
2005-05-23 05:06:10 +02:00
|
|
|
* isn't changing - makes "set -o interactive"
|
2009-06-10 20:12:51 +02:00
|
|
|
* work if you're already interactive. Needed
|
2005-05-23 05:06:10 +02:00
|
|
|
* if the output of "set +o" is to be used.
|
|
|
|
*/
|
|
|
|
;
|
2007-05-13 20:33:29 +02:00
|
|
|
else if ((i != (size_t)-1) && (options[i].flags & what))
|
|
|
|
change_flag((enum sh_flag)i, what, set);
|
2005-05-23 05:06:10 +02:00
|
|
|
else {
|
2010-08-28 20:50:58 +02:00
|
|
|
bi_errorf("%s: %s", go.optarg, "bad option");
|
2007-05-13 20:33:29 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-10-30 15:37:43 +01:00
|
|
|
#if !defined(MKSH_SMALL) || defined(TIOCSCTTY)
|
2007-01-26 19:27:34 +01:00
|
|
|
case 'T':
|
2005-05-23 05:06:10 +02:00
|
|
|
if (what != OF_FIRSTTIME)
|
|
|
|
break;
|
2009-10-30 15:37:43 +01:00
|
|
|
#ifndef TIOCSCTTY
|
2006-11-10 03:10:45 +01:00
|
|
|
errorf("no TIOCSCTTY ioctl");
|
2006-11-10 02:13:52 +01:00
|
|
|
#else
|
2005-05-23 05:06:10 +02:00
|
|
|
change_flag(FTALKING, OF_CMDLINE, 1);
|
2007-01-18 21:54:30 +01:00
|
|
|
chvt(go.optarg);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2007-07-26 15:23:52 +02:00
|
|
|
#endif
|
2007-01-26 19:27:34 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
case '?':
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
if (what == OF_FIRSTTIME)
|
|
|
|
break;
|
2009-06-11 14:42:21 +02:00
|
|
|
/* -s: sort positional params (AT&T ksh stupidity) */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (what == OF_SET && optc == 's') {
|
2011-05-05 01:16:05 +02:00
|
|
|
sortargs = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (i = 0; i < NELEM(options); i++)
|
|
|
|
if (optc == options[i].c &&
|
|
|
|
(what & options[i].flags)) {
|
2007-06-07 01:28:17 +02:00
|
|
|
change_flag((enum sh_flag)i, what, set);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
2007-05-13 19:51:24 +02:00
|
|
|
if (i == NELEM(options))
|
|
|
|
internal_errorf("parse_args: '%c'", optc);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(go.info & GI_MINUSMINUS) && argv[go.optind] &&
|
|
|
|
(argv[go.optind][0] == '-' || argv[go.optind][0] == '+') &&
|
|
|
|
argv[go.optind][1] == '\0') {
|
|
|
|
/* lone - clears -v and -x flags */
|
2005-07-04 14:27:28 +02:00
|
|
|
if (argv[go.optind][0] == '-')
|
2005-05-23 05:06:10 +02:00
|
|
|
Flag(FVERBOSE) = Flag(FXTRACE) = 0;
|
|
|
|
/* set skips lone - or + option */
|
|
|
|
go.optind++;
|
|
|
|
}
|
|
|
|
if (setargsp)
|
|
|
|
/* -- means set $#/$* even if there are no arguments */
|
|
|
|
*setargsp = !arrayset && ((go.info & GI_MINUSMINUS) ||
|
|
|
|
argv[go.optind]);
|
|
|
|
|
2011-05-05 01:16:05 +02:00
|
|
|
if (arrayset) {
|
|
|
|
const char *ccp = NULL;
|
|
|
|
|
|
|
|
if (*array)
|
|
|
|
ccp = skip_varname(array, false);
|
|
|
|
if (!ccp || !(!ccp[0] || (ccp[0] == '+' && !ccp[1]))) {
|
|
|
|
bi_errorf("%s: %s", array, "is not an identifier");
|
|
|
|
return (-1);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (sortargs) {
|
|
|
|
for (i = go.optind; argv[i]; i++)
|
|
|
|
;
|
2009-06-08 22:06:50 +02:00
|
|
|
qsort(&argv[go.optind], i - go.optind, sizeof(void *),
|
2005-05-23 05:06:10 +02:00
|
|
|
xstrcmp);
|
|
|
|
}
|
2009-08-28 23:07:27 +02:00
|
|
|
if (arrayset)
|
2011-03-07 21:30:41 +01:00
|
|
|
go.optind += set_array(array, tobool(arrayset > 0),
|
2009-08-28 23:07:27 +02:00
|
|
|
argv + go.optind);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (go.optind);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* parse a decimal number: returns 0 if string isn't a number, 1 otherwise */
|
|
|
|
int
|
2006-11-10 03:05:06 +01:00
|
|
|
getn(const char *s, int *ai)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-10 05:03:59 +01:00
|
|
|
int i, c, rv = 0;
|
2006-11-10 03:05:06 +01:00
|
|
|
bool neg = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
c = *s++;
|
2006-11-10 08:18:58 +01:00
|
|
|
} while (ksh_isspace(c));
|
2006-11-10 03:05:06 +01:00
|
|
|
if (c == '-') {
|
|
|
|
neg = true;
|
|
|
|
c = *s++;
|
|
|
|
} else if (c == '+')
|
|
|
|
c = *s++;
|
2006-11-10 05:03:59 +01:00
|
|
|
*ai = i = 0;
|
2006-11-10 03:05:06 +01:00
|
|
|
do {
|
2006-11-10 07:53:27 +01:00
|
|
|
if (!ksh_isdigit(c))
|
2006-11-10 05:03:59 +01:00
|
|
|
goto getn_out;
|
2006-11-10 03:05:06 +01:00
|
|
|
i *= 10;
|
|
|
|
if (i < *ai)
|
|
|
|
/* overflow */
|
2006-11-10 05:03:59 +01:00
|
|
|
goto getn_out;
|
2006-11-10 03:05:06 +01:00
|
|
|
i += c - '0';
|
2006-11-10 05:03:59 +01:00
|
|
|
*ai = i;
|
2006-11-10 03:05:06 +01:00
|
|
|
} while ((c = *s++));
|
2006-11-10 05:03:59 +01:00
|
|
|
rv = 1;
|
2006-11-10 03:05:06 +01:00
|
|
|
|
2006-11-10 05:03:59 +01:00
|
|
|
getn_out:
|
|
|
|
if (neg)
|
|
|
|
*ai = -*ai;
|
2006-11-10 06:21:38 +01:00
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-03-17 23:00:45 +01:00
|
|
|
/**
|
|
|
|
* pattern simplifications:
|
|
|
|
* - @(x) -> x (not @(x|y) though)
|
|
|
|
* - ** -> *
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
simplify_gmatch_pattern(const unsigned char *sp)
|
|
|
|
{
|
|
|
|
uint8_t c;
|
|
|
|
unsigned char *cp, *dp;
|
|
|
|
const unsigned char *ps, *se;
|
|
|
|
|
2011-03-17 23:09:23 +01:00
|
|
|
cp = alloc(strlen((const void *)sp) + 1, ATEMP);
|
2011-03-17 23:00:45 +01:00
|
|
|
goto simplify_gmatch_pat1a;
|
|
|
|
|
|
|
|
/* foo@(b@(a)r)b@(a|a)z -> foobarb@(a|a)z */
|
|
|
|
simplify_gmatch_pat1:
|
|
|
|
sp = cp;
|
|
|
|
simplify_gmatch_pat1a:
|
|
|
|
dp = cp;
|
|
|
|
se = sp + strlen((const void *)sp);
|
|
|
|
while ((c = *sp++)) {
|
|
|
|
if (!ISMAGIC(c)) {
|
|
|
|
*dp++ = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch ((c = *sp++)) {
|
|
|
|
case 0x80|'@':
|
|
|
|
/* simile for @ */
|
|
|
|
case 0x80|' ':
|
|
|
|
/* check whether it has only one clause */
|
|
|
|
ps = pat_scan(sp, se, true);
|
|
|
|
if (!ps || ps[-1] != /*(*/ ')')
|
|
|
|
/* nope */
|
|
|
|
break;
|
|
|
|
/* copy inner clause until matching close */
|
|
|
|
ps -= 2;
|
|
|
|
while ((const unsigned char *)sp < ps)
|
|
|
|
*dp++ = *sp++;
|
|
|
|
/* skip MAGIC and closing parenthesis */
|
|
|
|
sp += 2;
|
|
|
|
/* copy the rest of the pattern */
|
2011-03-17 23:09:23 +01:00
|
|
|
memmove(dp, sp, strlen((const void *)sp) + 1);
|
2011-03-17 23:00:45 +01:00
|
|
|
/* redo from start */
|
|
|
|
goto simplify_gmatch_pat1;
|
|
|
|
}
|
|
|
|
*dp++ = MAGIC;
|
|
|
|
*dp++ = c;
|
|
|
|
}
|
|
|
|
*dp = '\0';
|
|
|
|
|
|
|
|
/* collapse adjacent asterisk wildcards */
|
|
|
|
sp = dp = cp;
|
|
|
|
while ((c = *sp++)) {
|
|
|
|
if (!ISMAGIC(c)) {
|
|
|
|
*dp++ = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch ((c = *sp++)) {
|
|
|
|
case '*':
|
|
|
|
while (ISMAGIC(sp[0]) && sp[1] == c)
|
|
|
|
sp += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*dp++ = MAGIC;
|
|
|
|
*dp++ = c;
|
|
|
|
}
|
|
|
|
*dp = '\0';
|
|
|
|
|
|
|
|
/* return the result, allocated from ATEMP */
|
|
|
|
return (cp);
|
|
|
|
}
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/* -------- gmatch.c -------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* int gmatch(string, pattern)
|
|
|
|
* char *string, *pattern;
|
|
|
|
*
|
|
|
|
* Match a pattern as in sh(1).
|
|
|
|
* pattern character are prefixed with MAGIC by expand.
|
|
|
|
*/
|
|
|
|
int
|
2008-02-27 02:00:10 +01:00
|
|
|
gmatchx(const char *s, const char *p, bool isfile)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
const char *se, *pe;
|
2011-03-17 23:00:45 +01:00
|
|
|
char *pnew;
|
|
|
|
int rv;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (s == NULL || p == NULL)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2009-02-22 19:53:03 +01:00
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
se = s + strlen(s);
|
|
|
|
pe = p + strlen(p);
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* isfile is false iff no syntax check has been done on
|
2009-06-10 20:12:51 +02:00
|
|
|
* the pattern. If check fails, just to a strcmp().
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
if (!isfile && !has_globbing(p, pe)) {
|
|
|
|
size_t len = pe - p + 1;
|
|
|
|
char tbuf[64];
|
2008-12-13 18:02:18 +01:00
|
|
|
char *t = len <= sizeof(tbuf) ? tbuf : alloc(len, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
debunk(t, p, len);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (!strcmp(t, s));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-03-17 23:00:45 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* since the do_gmatch() engine sucks so much, we must do some
|
|
|
|
* pattern simplifications
|
|
|
|
*/
|
|
|
|
pnew = simplify_gmatch_pattern((const unsigned char *)p);
|
|
|
|
pe = pnew + strlen(pnew);
|
|
|
|
|
|
|
|
rv = do_gmatch((const unsigned char *)s, (const unsigned char *)se,
|
|
|
|
(const unsigned char *)pnew, (const unsigned char *)pe);
|
|
|
|
afree(pnew, ATEMP);
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/**
|
|
|
|
* Returns if p is a syntacticly correct globbing pattern, false
|
2005-05-23 05:06:10 +02:00
|
|
|
* if it contains no pattern characters or if there is a syntax error.
|
|
|
|
* Syntax errors are:
|
|
|
|
* - [ with no closing ]
|
|
|
|
* - imbalanced $(...) expression
|
|
|
|
* - [...] and *(...) not nested (eg, [a$(b|]c), *(a[b|c]d))
|
|
|
|
*/
|
|
|
|
/*XXX
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
* - if no magic,
|
|
|
|
* if dest given, copy to dst
|
|
|
|
* return ?
|
|
|
|
* - if magic && (no globbing || syntax error)
|
|
|
|
* debunk to dst
|
|
|
|
* return ?
|
|
|
|
* - return ?
|
|
|
|
*/
|
2005-05-23 05:06:10 +02:00
|
|
|
int
|
|
|
|
has_globbing(const char *xp, const char *xpe)
|
|
|
|
{
|
|
|
|
const unsigned char *p = (const unsigned char *) xp;
|
|
|
|
const unsigned char *pe = (const unsigned char *) xpe;
|
|
|
|
int c;
|
|
|
|
int nest = 0, bnest = 0;
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
bool saw_glob = false;
|
|
|
|
/* inside [...] */
|
|
|
|
bool in_bracket = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
for (; p < pe; p++) {
|
|
|
|
if (!ISMAGIC(*p))
|
|
|
|
continue;
|
|
|
|
if ((c = *++p) == '*' || c == '?')
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
saw_glob = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (c == '[') {
|
|
|
|
if (!in_bracket) {
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
saw_glob = true;
|
|
|
|
in_bracket = true;
|
2012-03-23 21:07:13 +01:00
|
|
|
if (ISMAGIC(p[1]) && p[2] == '!')
|
2005-05-23 05:06:10 +02:00
|
|
|
p += 2;
|
|
|
|
if (ISMAGIC(p[1]) && p[2] == ']')
|
|
|
|
p += 2;
|
|
|
|
}
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*XXX Do we need to check ranges here? POSIX Q */
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (c == ']') {
|
|
|
|
if (in_bracket) {
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
if (bnest)
|
|
|
|
/* [a*(b]) */
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
in_bracket = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
} else if ((c & 0x80) && vstrchr("*+?@! ", c & 0x7f)) {
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
saw_glob = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (in_bracket)
|
|
|
|
bnest++;
|
|
|
|
else
|
|
|
|
nest++;
|
|
|
|
} else if (c == '|') {
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
if (in_bracket && !bnest)
|
|
|
|
/* *(a[foo|bar]) */
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (c == /*(*/ ')') {
|
|
|
|
if (in_bracket) {
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
if (!bnest--)
|
|
|
|
/* *(a[b)c] */
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (nest)
|
|
|
|
nest--;
|
|
|
|
}
|
2010-01-29 10:34:31 +01:00
|
|
|
/*
|
|
|
|
* else must be a MAGIC-MAGIC, or MAGIC-!,
|
|
|
|
* MAGIC--, MAGIC-], MAGIC-{, MAGIC-, MAGIC-}
|
|
|
|
*/
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (saw_glob && !in_bracket && !nest);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Function must return either 0 or 1 (assumed by code for 0x80|'!') */
|
|
|
|
static int
|
|
|
|
do_gmatch(const unsigned char *s, const unsigned char *se,
|
|
|
|
const unsigned char *p, const unsigned char *pe)
|
|
|
|
{
|
|
|
|
int sc, pc;
|
|
|
|
const unsigned char *prest, *psub, *pnext;
|
|
|
|
const unsigned char *srest;
|
|
|
|
|
|
|
|
if (s == NULL || p == NULL)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
while (p < pe) {
|
|
|
|
pc = *p++;
|
|
|
|
sc = s < se ? *s : '\0';
|
|
|
|
s++;
|
|
|
|
if (!ISMAGIC(pc)) {
|
|
|
|
if (sc != pc)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (*p++) {
|
|
|
|
case '[':
|
|
|
|
if (sc == 0 || (p = cclass(p, sc)) == NULL)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
if (sc == 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2009-11-21 23:30:36 +01:00
|
|
|
if (UTFMODE) {
|
|
|
|
--s;
|
2009-11-23 13:48:19 +01:00
|
|
|
s += utf_ptradj((const void *)s);
|
2009-11-21 23:30:36 +01:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
if (p == pe)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
s--;
|
|
|
|
do {
|
|
|
|
if (do_gmatch(s, se, p, pe))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
} while (s++ < se);
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
/**
|
|
|
|
* [*+?@!](pattern|pattern|..)
|
|
|
|
* This is also needed for ${..%..}, etc.
|
|
|
|
*/
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
|
|
|
|
/* matches one or more times */
|
|
|
|
case 0x80|'+':
|
|
|
|
/* matches zero or more times */
|
|
|
|
case 0x80|'*':
|
2011-03-17 22:59:30 +01:00
|
|
|
if (!(prest = pat_scan(p, pe, false)))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
s--;
|
|
|
|
/* take care of zero matches */
|
|
|
|
if (p[-1] == (0x80 | '*') &&
|
|
|
|
do_gmatch(s, se, prest, pe))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (psub = p; ; psub = pnext) {
|
2011-03-17 22:59:30 +01:00
|
|
|
pnext = pat_scan(psub, pe, true);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (srest = s; srest <= se; srest++) {
|
|
|
|
if (do_gmatch(s, srest, psub, pnext - 2) &&
|
|
|
|
(do_gmatch(srest, se, prest, pe) ||
|
|
|
|
(s != srest && do_gmatch(srest,
|
|
|
|
se, p - 2, pe))))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (pnext == prest)
|
|
|
|
break;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* matches zero or once */
|
|
|
|
case 0x80|'?':
|
|
|
|
/* matches one of the patterns */
|
|
|
|
case 0x80|'@':
|
|
|
|
/* simile for @ */
|
|
|
|
case 0x80|' ':
|
2011-03-17 22:59:30 +01:00
|
|
|
if (!(prest = pat_scan(p, pe, false)))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
s--;
|
|
|
|
/* Take care of zero matches */
|
|
|
|
if (p[-1] == (0x80 | '?') &&
|
|
|
|
do_gmatch(s, se, prest, pe))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (psub = p; ; psub = pnext) {
|
2011-03-17 22:59:30 +01:00
|
|
|
pnext = pat_scan(psub, pe, true);
|
2005-05-23 05:06:10 +02:00
|
|
|
srest = prest == pe ? se : s;
|
|
|
|
for (; srest <= se; srest++) {
|
|
|
|
if (do_gmatch(s, srest, psub, pnext - 2) &&
|
|
|
|
do_gmatch(srest, se, prest, pe))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (pnext == prest)
|
|
|
|
break;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* matches none of the patterns */
|
|
|
|
case 0x80|'!':
|
2011-03-17 22:59:30 +01:00
|
|
|
if (!(prest = pat_scan(p, pe, false)))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
s--;
|
|
|
|
for (srest = s; srest <= se; srest++) {
|
|
|
|
int matched = 0;
|
|
|
|
|
|
|
|
for (psub = p; ; psub = pnext) {
|
2011-03-17 22:59:30 +01:00
|
|
|
pnext = pat_scan(psub, pe, true);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (do_gmatch(s, srest, psub,
|
|
|
|
pnext - 2)) {
|
|
|
|
matched = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pnext == prest)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!matched &&
|
|
|
|
do_gmatch(srest, se, prest, pe))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
if (sc != p[-1])
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (s == se);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const unsigned char *
|
|
|
|
cclass(const unsigned char *p, int sub)
|
|
|
|
{
|
2010-07-18 00:09:40 +02:00
|
|
|
int c, d, notp, found = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
const unsigned char *orig_p = p;
|
|
|
|
|
2012-03-23 21:07:13 +01:00
|
|
|
if ((notp = (ISMAGIC(*p) && *++p == '!')))
|
2005-05-23 05:06:10 +02:00
|
|
|
p++;
|
|
|
|
do {
|
|
|
|
c = *p++;
|
|
|
|
if (ISMAGIC(c)) {
|
|
|
|
c = *p++;
|
|
|
|
if ((c & 0x80) && !ISMAGIC(c)) {
|
2010-08-28 17:48:20 +02:00
|
|
|
/* extended pattern matching: *+?@! */
|
|
|
|
c &= 0x7F;
|
2005-05-23 05:06:10 +02:00
|
|
|
/* XXX the ( char isn't handled as part of [] */
|
2010-08-28 17:48:20 +02:00
|
|
|
if (c == ' ')
|
|
|
|
/* simile for @: plain (..) */
|
2005-05-23 05:06:10 +02:00
|
|
|
c = '(' /*)*/;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c == '\0')
|
|
|
|
/* No closing ] - act as if the opening [ was quoted */
|
2009-06-08 22:06:50 +02:00
|
|
|
return (sub == '[' ? orig_p : NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (ISMAGIC(p[0]) && p[1] == '-' &&
|
|
|
|
(!ISMAGIC(p[2]) || p[3] != ']')) {
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/* MAGIC- */
|
|
|
|
p += 2;
|
2005-05-23 05:06:10 +02:00
|
|
|
d = *p++;
|
|
|
|
if (ISMAGIC(d)) {
|
|
|
|
d = *p++;
|
|
|
|
if ((d & 0x80) && !ISMAGIC(d))
|
|
|
|
d &= 0x7f;
|
|
|
|
}
|
|
|
|
/* POSIX says this is an invalid expression */
|
|
|
|
if (c > d)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else
|
|
|
|
d = c;
|
|
|
|
if (c == sub || (c <= sub && sub <= d))
|
|
|
|
found = 1;
|
|
|
|
} while (!(ISMAGIC(p[0]) && p[1] == ']'));
|
|
|
|
|
2010-07-18 00:09:40 +02:00
|
|
|
return ((found != notp) ? p+2 : NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Look for next ) or | (if match_sep) in *(foo|bar) pattern */
|
2011-06-12 16:45:34 +02:00
|
|
|
static const unsigned char *
|
2011-03-17 22:59:30 +01:00
|
|
|
pat_scan(const unsigned char *p, const unsigned char *pe, bool match_sep)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
int nest = 0;
|
|
|
|
|
|
|
|
for (; p < pe; p++) {
|
|
|
|
if (!ISMAGIC(*p))
|
|
|
|
continue;
|
|
|
|
if ((*++p == /*(*/ ')' && nest-- == 0) ||
|
|
|
|
(*p == '|' && match_sep && nest == 0))
|
2008-10-14 01:06:04 +02:00
|
|
|
return (p + 1);
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
if ((*p & 0x80) && vstrchr("*+?@! ", *p & 0x7f))
|
2005-05-23 05:06:10 +02:00
|
|
|
nest++;
|
|
|
|
}
|
2008-10-14 01:06:04 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-11-10 04:23:50 +01:00
|
|
|
xstrcmp(const void *p1, const void *p2)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2009-08-08 15:08:53 +02:00
|
|
|
return (strcmp(*(const char * const *)p1, *(const char * const *)p2));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2006-11-10 04:45:57 +01:00
|
|
|
/* Initialise a Getopt structure */
|
2005-05-23 05:06:10 +02:00
|
|
|
void
|
|
|
|
ksh_getopt_reset(Getopt *go, int flags)
|
|
|
|
{
|
|
|
|
go->optind = 1;
|
|
|
|
go->optarg = NULL;
|
|
|
|
go->p = 0;
|
|
|
|
go->flags = flags;
|
|
|
|
go->info = 0;
|
|
|
|
go->buf[1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/**
|
|
|
|
* getopt() used for shell built-in commands, the getopts command, and
|
2005-05-23 05:06:10 +02:00
|
|
|
* command line options.
|
|
|
|
* A leading ':' in options means don't print errors, instead return '?'
|
|
|
|
* or ':' and set go->optarg to the offending option character.
|
|
|
|
* If GF_ERROR is set (and option doesn't start with :), errors result in
|
|
|
|
* a call to bi_errorf().
|
|
|
|
*
|
|
|
|
* Non-standard features:
|
|
|
|
* - ';' is like ':' in options, except the argument is optional
|
|
|
|
* (if it isn't present, optarg is set to 0).
|
|
|
|
* Used for 'set -o'.
|
|
|
|
* - ',' is like ':' in options, except the argument always immediately
|
|
|
|
* follows the option character (optarg is set to the null string if
|
|
|
|
* the option is missing).
|
|
|
|
* Used for 'read -u2', 'print -u2' and fc -40.
|
|
|
|
* - '#' is like ':' in options, expect that the argument is optional
|
2009-06-10 20:12:51 +02:00
|
|
|
* and must start with a digit. If the argument doesn't start with a
|
2008-04-22 21:00:01 +02:00
|
|
|
* digit, it is assumed to be missing and normal option processing
|
|
|
|
* continues (optarg is set to 0 if the option is missing).
|
|
|
|
* Used for 'typeset -LZ4'.
|
2009-06-10 20:12:51 +02:00
|
|
|
* - accepts +c as well as -c IF the GF_PLUSOPT flag is present. If an
|
2005-05-23 05:06:10 +02:00
|
|
|
* option starting with + is accepted, the GI_PLUS flag will be set
|
|
|
|
* in go->info.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
ksh_getopt(const char **argv, Getopt *go, const char *optionsp)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
char c;
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
const char *o;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (go->p == 0 || (c = argv[go->optind - 1][go->p]) == '\0') {
|
2007-03-04 01:13:17 +01:00
|
|
|
const char *arg = argv[go->optind], flag = arg ? *arg : '\0';
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
go->p = 1;
|
|
|
|
if (flag == '-' && arg[1] == '-' && arg[2] == '\0') {
|
|
|
|
go->optind++;
|
|
|
|
go->p = 0;
|
|
|
|
go->info |= GI_MINUSMINUS;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (arg == NULL ||
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
((flag != '-' ) &&
|
|
|
|
/* neither a - nor a + (if + allowed) */
|
2005-05-23 05:06:10 +02:00
|
|
|
(!(go->flags & GF_PLUSOPT) || flag != '+')) ||
|
|
|
|
(c = arg[1]) == '\0') {
|
|
|
|
go->p = 0;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
go->optind++;
|
|
|
|
go->info &= ~(GI_MINUS|GI_PLUS);
|
|
|
|
go->info |= flag == '-' ? GI_MINUS : GI_PLUS;
|
|
|
|
}
|
|
|
|
go->p++;
|
|
|
|
if (c == '?' || c == ':' || c == ';' || c == ',' || c == '#' ||
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
!(o = cstrchr(optionsp, c))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (optionsp[0] == ':') {
|
|
|
|
go->buf[0] = c;
|
|
|
|
go->optarg = go->buf;
|
|
|
|
} else {
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(true, "%s%s-%c: %s",
|
2005-05-23 05:06:10 +02:00
|
|
|
(go->flags & GF_NONAME) ? "" : argv[0],
|
2010-08-28 22:22:24 +02:00
|
|
|
(go->flags & GF_NONAME) ? "" : ": ", c,
|
|
|
|
"unknown option");
|
2005-05-23 05:06:10 +02:00
|
|
|
if (go->flags & GF_ERROR)
|
2007-07-22 16:01:50 +02:00
|
|
|
bi_errorfz();
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return ('?');
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/**
|
|
|
|
* : means argument must be present, may be part of option argument
|
2005-05-23 05:06:10 +02:00
|
|
|
* or the next argument
|
|
|
|
* ; same as : but argument may be missing
|
|
|
|
* , means argument is part of option argument, and may be null.
|
|
|
|
*/
|
|
|
|
if (*++o == ':' || *o == ';') {
|
|
|
|
if (argv[go->optind - 1][go->p])
|
|
|
|
go->optarg = argv[go->optind - 1] + go->p;
|
|
|
|
else if (argv[go->optind])
|
|
|
|
go->optarg = argv[go->optind++];
|
|
|
|
else if (*o == ';')
|
|
|
|
go->optarg = NULL;
|
|
|
|
else {
|
|
|
|
if (optionsp[0] == ':') {
|
|
|
|
go->buf[0] = c;
|
|
|
|
go->optarg = go->buf;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (':');
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(true, "%s%s-%c: %s",
|
2005-05-23 05:06:10 +02:00
|
|
|
(go->flags & GF_NONAME) ? "" : argv[0],
|
2010-08-28 20:50:58 +02:00
|
|
|
(go->flags & GF_NONAME) ? "" : ": ", c,
|
|
|
|
"requires an argument");
|
2005-05-23 05:06:10 +02:00
|
|
|
if (go->flags & GF_ERROR)
|
2007-07-22 16:01:50 +02:00
|
|
|
bi_errorfz();
|
2009-06-08 22:06:50 +02:00
|
|
|
return ('?');
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
go->p = 0;
|
|
|
|
} else if (*o == ',') {
|
|
|
|
/* argument is attached to option character, even if null */
|
|
|
|
go->optarg = argv[go->optind - 1] + go->p;
|
|
|
|
go->p = 0;
|
|
|
|
} else if (*o == '#') {
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* argument is optional and may be attached or unattached
|
2009-06-10 20:12:51 +02:00
|
|
|
* but must start with a digit. optarg is set to 0 if the
|
2005-05-23 05:06:10 +02:00
|
|
|
* argument is missing.
|
|
|
|
*/
|
|
|
|
if (argv[go->optind - 1][go->p]) {
|
2008-04-22 21:00:01 +02:00
|
|
|
if (ksh_isdigit(argv[go->optind - 1][go->p])) {
|
2005-05-23 05:06:10 +02:00
|
|
|
go->optarg = argv[go->optind - 1] + go->p;
|
|
|
|
go->p = 0;
|
|
|
|
} else
|
|
|
|
go->optarg = NULL;
|
|
|
|
} else {
|
2008-04-22 21:00:01 +02:00
|
|
|
if (argv[go->optind] && ksh_isdigit(argv[go->optind][0])) {
|
2005-05-23 05:06:10 +02:00
|
|
|
go->optarg = argv[go->optind++];
|
|
|
|
go->p = 0;
|
|
|
|
} else
|
|
|
|
go->optarg = NULL;
|
|
|
|
}
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (c);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* print variable/alias value using necessary quotes
|
2005-05-23 05:06:10 +02:00
|
|
|
* (POSIX says they should be suitable for re-entry...)
|
|
|
|
* No trailing newline is printed.
|
|
|
|
*/
|
|
|
|
void
|
2011-12-29 23:54:22 +01:00
|
|
|
print_value_quoted(struct shf *shf, const char *s)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-10-24 21:40:46 +02:00
|
|
|
unsigned char c;
|
|
|
|
const unsigned char *p = (const unsigned char *)s;
|
|
|
|
bool inquote = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-01-09 22:57:29 +01:00
|
|
|
/* first, check whether any quotes are needed */
|
2012-01-04 20:09:36 +01:00
|
|
|
while ((c = *p++) >= 32)
|
|
|
|
if (ctype(c, C_QUOTE))
|
2011-10-24 21:40:46 +02:00
|
|
|
inquote = false;
|
|
|
|
|
|
|
|
p = (const unsigned char *)s;
|
|
|
|
if (c == 0) {
|
|
|
|
if (inquote) {
|
|
|
|
/* nope, use the shortcut */
|
2011-12-29 23:54:22 +01:00
|
|
|
shf_puts(s, shf);
|
2011-10-24 21:40:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-01-09 22:57:29 +01:00
|
|
|
|
2011-10-24 21:40:46 +02:00
|
|
|
/* otherwise, quote nicely via state machine */
|
|
|
|
while ((c = *p++) != 0) {
|
|
|
|
if (c == '\'') {
|
|
|
|
/*
|
|
|
|
* multiple single quotes or any of them
|
|
|
|
* at the beginning of a string look nicer
|
|
|
|
* this way than when simply substituting
|
|
|
|
*/
|
|
|
|
if (inquote) {
|
2011-12-29 23:54:22 +01:00
|
|
|
shf_putc('\'', shf);
|
2011-10-24 21:40:46 +02:00
|
|
|
inquote = false;
|
|
|
|
}
|
2011-12-29 23:54:22 +01:00
|
|
|
shf_putc('\\', shf);
|
2011-10-24 21:40:46 +02:00
|
|
|
} else if (!inquote) {
|
2011-12-29 23:54:22 +01:00
|
|
|
shf_putc('\'', shf);
|
2011-10-24 21:40:46 +02:00
|
|
|
inquote = true;
|
|
|
|
}
|
2011-12-29 23:54:22 +01:00
|
|
|
shf_putc(c, shf);
|
2011-10-24 21:40:46 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unsigned int wc;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
/* use $'...' quote format */
|
2011-12-29 23:54:22 +01:00
|
|
|
shf_putc('$', shf);
|
|
|
|
shf_putc('\'', shf);
|
2011-10-24 21:40:46 +02:00
|
|
|
while ((c = *p) != 0) {
|
|
|
|
if (c >= 0xC2) {
|
2011-12-03 01:03:25 +01:00
|
|
|
n = utf_mbtowc(&wc, (const char *)p);
|
2011-10-24 21:40:46 +02:00
|
|
|
if (n != (size_t)-1) {
|
|
|
|
p += n;
|
2011-12-29 23:54:22 +01:00
|
|
|
shf_fprintf(shf, "\\u%04X", wc);
|
2011-10-24 21:40:46 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
switch (c) {
|
|
|
|
/* see unbksl() in this file for comments */
|
|
|
|
case 7:
|
|
|
|
c = 'a';
|
|
|
|
if (0)
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '\b':
|
|
|
|
c = 'b';
|
|
|
|
if (0)
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '\f':
|
|
|
|
c = 'f';
|
|
|
|
if (0)
|
|
|
|
/* FALLTHROUGH */
|
2011-10-24 21:40:58 +02:00
|
|
|
case '\n':
|
|
|
|
c = 'n';
|
|
|
|
if (0)
|
|
|
|
/* FALLTHROUGH */
|
2011-10-24 21:40:46 +02:00
|
|
|
case '\r':
|
|
|
|
c = 'r';
|
|
|
|
if (0)
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '\t':
|
|
|
|
c = 't';
|
|
|
|
if (0)
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 11:
|
|
|
|
c = 'v';
|
|
|
|
if (0)
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '\033':
|
|
|
|
/* take E not e because \e is \ in *roff */
|
|
|
|
c = 'E';
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '\\':
|
2011-12-29 23:54:22 +01:00
|
|
|
shf_putc('\\', shf);
|
2011-10-24 21:40:58 +02:00
|
|
|
|
|
|
|
if (0)
|
|
|
|
/* FALLTHROUGH */
|
2011-10-24 21:40:46 +02:00
|
|
|
default:
|
2011-10-24 21:40:58 +02:00
|
|
|
if (c < 32 || c > 0x7E) {
|
|
|
|
/* FALLTHROUGH */
|
2011-10-24 21:40:46 +02:00
|
|
|
case '\'':
|
2011-12-29 23:54:22 +01:00
|
|
|
shf_fprintf(shf, "\\x%02X", c);
|
2011-10-24 21:40:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-29 23:54:22 +01:00
|
|
|
shf_putc(c, shf);
|
2011-10-24 21:40:46 +02:00
|
|
|
break;
|
2011-01-09 22:57:29 +01:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-10-24 21:40:46 +02:00
|
|
|
inquote = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (inquote)
|
2011-12-29 23:54:22 +01:00
|
|
|
shf_putc('\'', shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-11-10 00:35:11 +01:00
|
|
|
/*
|
|
|
|
* Print things in columns and rows - func() is called to format
|
|
|
|
* the i-th element
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
void
|
2006-11-12 15:58:16 +01:00
|
|
|
print_columns(struct shf *shf, int n,
|
2011-08-27 20:06:52 +02:00
|
|
|
char *(*func)(char *, size_t, int, const void *),
|
2011-09-07 17:24:22 +02:00
|
|
|
const void *arg, size_t max_oct, size_t max_colz, bool prefcol)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-08-27 20:06:52 +02:00
|
|
|
int i, r, c, rows, cols, nspace, max_col;
|
2009-11-10 00:35:11 +01:00
|
|
|
char *str;
|
|
|
|
|
|
|
|
if (n <= 0) {
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
internal_warningf("print_columns called with n=%d <= 0", n);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-07 17:24:22 +02:00
|
|
|
if (max_colz > 2147483647) {
|
2011-08-27 20:06:52 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2011-09-07 17:24:22 +02:00
|
|
|
internal_warningf("print_columns called with max_col=%zu > INT_MAX",
|
|
|
|
max_colz);
|
2011-08-27 20:06:52 +02:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2011-09-07 17:24:22 +02:00
|
|
|
max_col = (int)max_colz;
|
2011-08-27 20:06:52 +02:00
|
|
|
|
2009-11-10 00:35:11 +01:00
|
|
|
++max_oct;
|
|
|
|
str = alloc(max_oct, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-11-10 00:35:11 +01:00
|
|
|
/*
|
|
|
|
* We use (max_col + 1) to consider the space separator.
|
|
|
|
* Note that no space is printed after the last column
|
|
|
|
* to avoid problems with terminals that have auto-wrap.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2009-11-10 00:35:11 +01:00
|
|
|
cols = x_cols / (max_col + 1);
|
|
|
|
|
2009-02-22 19:02:31 +01:00
|
|
|
/* if we can only print one column anyway, skip the goo */
|
|
|
|
if (cols < 2) {
|
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
shf_fprintf(shf, "%s \n",
|
2009-11-10 00:35:11 +01:00
|
|
|
(*func)(str, max_oct, i, arg));
|
2009-02-22 19:02:31 +01:00
|
|
|
goto out;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-11-10 00:35:11 +01:00
|
|
|
rows = (n + cols - 1) / cols;
|
|
|
|
if (prefcol && cols > rows) {
|
|
|
|
i = rows;
|
|
|
|
rows = cols > n ? n : cols;
|
|
|
|
cols = i;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-11-10 00:35:11 +01:00
|
|
|
max_col = -max_col;
|
|
|
|
nspace = (x_cols + max_col * cols) / cols;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (nspace <= 0)
|
|
|
|
nspace = 1;
|
|
|
|
for (r = 0; r < rows; r++) {
|
|
|
|
for (c = 0; c < cols; c++) {
|
|
|
|
i = c * rows + r;
|
|
|
|
if (i < n) {
|
2009-11-10 00:35:11 +01:00
|
|
|
shf_fprintf(shf, "%*s", max_col,
|
|
|
|
(*func)(str, max_oct, i, arg));
|
2005-05-23 05:06:10 +02:00
|
|
|
if (c + 1 < cols)
|
|
|
|
shf_fprintf(shf, "%*s", nspace, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
shf_putchar('\n', shf);
|
|
|
|
}
|
2009-02-22 19:02:31 +01:00
|
|
|
out:
|
2005-05-23 05:06:10 +02:00
|
|
|
afree(str, ATEMP);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Strip any nul bytes from buf - returns new length (nbytes - # of nuls) */
|
2006-11-10 04:45:57 +01:00
|
|
|
void
|
2005-05-23 05:06:10 +02:00
|
|
|
strip_nuls(char *buf, int nbytes)
|
|
|
|
{
|
|
|
|
char *dst;
|
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/*
|
|
|
|
* nbytes check because some systems (older FreeBSDs) have a
|
|
|
|
* buggy memchr()
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
if (nbytes && (dst = memchr(buf, '\0', nbytes))) {
|
|
|
|
char *end = buf + nbytes;
|
|
|
|
char *p, *q;
|
|
|
|
|
|
|
|
for (p = dst; p < end; p = q) {
|
|
|
|
/* skip a block of nulls */
|
|
|
|
while (++p < end && *p == '\0')
|
|
|
|
;
|
|
|
|
/* find end of non-null block */
|
|
|
|
if (!(q = memchr(p, '\0', end - p)))
|
|
|
|
q = end;
|
|
|
|
memmove(dst, p, q - p);
|
|
|
|
dst += q - p;
|
|
|
|
}
|
|
|
|
*dst = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-28 18:47:11 +02:00
|
|
|
/*
|
|
|
|
* Like read(2), but if read fails due to non-blocking flag,
|
|
|
|
* resets flag and restarts read.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2010-08-28 18:47:11 +02:00
|
|
|
ssize_t
|
|
|
|
blocking_read(int fd, char *buf, size_t nbytes)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2010-08-28 18:47:11 +02:00
|
|
|
ssize_t ret;
|
|
|
|
bool tried_reset = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
while ((ret = read(fd, buf, nbytes)) < 0) {
|
|
|
|
if (!tried_reset && errno == EAGAIN) {
|
|
|
|
if (reset_nonblock(fd) > 0) {
|
2010-08-28 18:47:11 +02:00
|
|
|
tried_reset = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
2006-11-10 04:45:57 +01:00
|
|
|
errno = EAGAIN;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ret);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2010-08-28 18:47:11 +02:00
|
|
|
/*
|
|
|
|
* Reset the non-blocking flag on the specified file descriptor.
|
2005-05-23 05:06:10 +02:00
|
|
|
* Returns -1 if there was an error, 0 if non-blocking wasn't set,
|
|
|
|
* 1 if it was.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
reset_nonblock(int fd)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!(flags & O_NONBLOCK))
|
2009-06-08 22:06:50 +02:00
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
if (fcntl(fd, F_SETFL, flags) < 0)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (-1);
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-03-24 20:05:49 +01:00
|
|
|
/* getcwd(3) equivalent, allocates from ATEMP but doesn't resize */
|
2005-05-23 05:06:10 +02:00
|
|
|
char *
|
2011-03-24 20:05:49 +01:00
|
|
|
ksh_get_wd(void)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2009-10-27 18:00:02 +01:00
|
|
|
#ifdef NO_PATH_MAX
|
2011-03-24 20:05:49 +01:00
|
|
|
char *rv, *cp;
|
|
|
|
|
|
|
|
if ((cp = get_current_dir_name())) {
|
|
|
|
strdupx(rv, cp, ATEMP);
|
|
|
|
free_gnu_gcdn(cp);
|
2009-10-27 18:00:02 +01:00
|
|
|
} else
|
2011-03-24 20:05:49 +01:00
|
|
|
rv = NULL;
|
2009-10-27 18:00:02 +01:00
|
|
|
#else
|
2011-03-24 20:05:49 +01:00
|
|
|
char *rv;
|
|
|
|
|
|
|
|
if (!getcwd((rv = alloc(PATH_MAX + 1, ATEMP)), PATH_MAX)) {
|
|
|
|
afree(rv, ATEMP);
|
|
|
|
rv = NULL;
|
|
|
|
}
|
2009-10-27 18:00:02 +01:00
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-24 20:05:49 +01:00
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
2012-03-26 22:04:05 +02:00
|
|
|
#ifndef ELOOP
|
|
|
|
#define ELOOP E2BIG
|
|
|
|
#endif
|
|
|
|
|
2011-03-24 20:05:49 +01:00
|
|
|
char *
|
|
|
|
do_realpath(const char *upath)
|
|
|
|
{
|
|
|
|
char *xp, *ip, *tp, *ipath, *ldest = NULL;
|
|
|
|
XString xs;
|
|
|
|
ptrdiff_t pos;
|
|
|
|
size_t len;
|
|
|
|
int llen;
|
|
|
|
struct stat sb;
|
|
|
|
#ifdef NO_PATH_MAX
|
|
|
|
size_t ldestlen = 0;
|
|
|
|
#define pathlen sb.st_size
|
|
|
|
#define pathcnd (ldestlen < (pathlen + 1))
|
|
|
|
#else
|
|
|
|
#define pathlen PATH_MAX
|
|
|
|
#define pathcnd (!ldest)
|
|
|
|
#endif
|
|
|
|
/* max. recursion depth */
|
|
|
|
int symlinks = 32;
|
|
|
|
|
|
|
|
if (upath[0] == '/') {
|
|
|
|
/* upath is an absolute pathname */
|
|
|
|
strdupx(ipath, upath, ATEMP);
|
|
|
|
} else {
|
|
|
|
/* upath is a relative pathname, prepend cwd */
|
|
|
|
if ((tp = ksh_get_wd()) == NULL || tp[0] != '/')
|
|
|
|
return (NULL);
|
|
|
|
ipath = shf_smprintf("%s%s%s", tp, "/", upath);
|
|
|
|
afree(tp, ATEMP);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ipath and upath are in memory at the same time -> unchecked */
|
|
|
|
Xinit(xs, xp, strlen(ip = ipath) + 1, ATEMP);
|
|
|
|
|
|
|
|
/* now jump into the deep of the loop */
|
|
|
|
goto beginning_of_a_pathname;
|
|
|
|
|
|
|
|
while (*ip) {
|
|
|
|
/* skip slashes in input */
|
|
|
|
while (*ip == '/')
|
|
|
|
++ip;
|
|
|
|
if (!*ip)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* get next pathname component from input */
|
|
|
|
tp = ip;
|
|
|
|
while (*ip && *ip != '/')
|
|
|
|
++ip;
|
|
|
|
len = ip - tp;
|
|
|
|
|
|
|
|
/* check input for "." and ".." */
|
|
|
|
if (tp[0] == '.') {
|
|
|
|
if (len == 1)
|
|
|
|
/* just continue with the next one */
|
|
|
|
continue;
|
|
|
|
else if (len == 2 && tp[1] == '.') {
|
|
|
|
/* strip off last pathname component */
|
|
|
|
while (xp > Xstring(xs, xp))
|
|
|
|
if (*--xp == '/')
|
|
|
|
break;
|
|
|
|
/* then continue with the next one */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store output position away, then append slash to output */
|
|
|
|
pos = Xsavepos(xs, xp);
|
|
|
|
/* 1 for the '/' and len + 1 for tp and the NUL from below */
|
|
|
|
XcheckN(xs, xp, 1 + len + 1);
|
|
|
|
Xput(xs, xp, '/');
|
|
|
|
|
|
|
|
/* append next pathname component to output */
|
|
|
|
memcpy(xp, tp, len);
|
|
|
|
xp += len;
|
|
|
|
*xp = '\0';
|
|
|
|
|
|
|
|
/* lstat the current output, see if it's a symlink */
|
|
|
|
if (lstat(Xstring(xs, xp), &sb)) {
|
|
|
|
/* lstat failed */
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
/* because the pathname does not exist */
|
|
|
|
while (*ip == '/')
|
|
|
|
/* skip any trailing slashes */
|
|
|
|
++ip;
|
|
|
|
/* no more components left? */
|
|
|
|
if (!*ip)
|
|
|
|
/* we can still return successfully */
|
|
|
|
break;
|
|
|
|
/* more components left? fall through */
|
|
|
|
}
|
|
|
|
/* not ENOENT or not at the end of ipath */
|
|
|
|
goto notfound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if we encountered a symlink? */
|
|
|
|
if (S_ISLNK(sb.st_mode)) {
|
|
|
|
/* reached maximum recursion depth? */
|
|
|
|
if (!symlinks--) {
|
|
|
|
/* yep, prevent infinite loops */
|
|
|
|
errno = ELOOP;
|
|
|
|
goto notfound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get symlink(7) target */
|
|
|
|
if (pathcnd) {
|
|
|
|
#ifdef NO_PATH_MAX
|
|
|
|
if (notoktoadd(pathlen, 1)) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
goto notfound;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ldest = aresize(ldest, pathlen + 1, ATEMP);
|
|
|
|
}
|
|
|
|
llen = readlink(Xstring(xs, xp), ldest, pathlen);
|
|
|
|
if (llen < 0)
|
|
|
|
/* oops... */
|
|
|
|
goto notfound;
|
|
|
|
ldest[llen] = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* restart if symlink target is an absolute path,
|
|
|
|
* otherwise continue with currently resolved prefix
|
|
|
|
*/
|
|
|
|
/* append rest of current input path to link target */
|
|
|
|
tp = shf_smprintf("%s%s%s", ldest, *ip ? "/" : "", ip);
|
|
|
|
afree(ipath, ATEMP);
|
|
|
|
ip = ipath = tp;
|
|
|
|
if (ldest[0] != '/') {
|
|
|
|
/* symlink target is a relative path */
|
|
|
|
xp = Xrestpos(xs, xp, pos);
|
|
|
|
} else {
|
|
|
|
/* symlink target is an absolute path */
|
|
|
|
xp = Xstring(xs, xp);
|
|
|
|
beginning_of_a_pathname:
|
|
|
|
/* assert: (ip == ipath)[0] == '/' */
|
|
|
|
/* assert: xp == xs.beg => start of path */
|
|
|
|
|
2011-03-28 23:31:01 +02:00
|
|
|
/* exactly two leading slashes? (SUSv4 3.266) */
|
2011-03-24 20:05:49 +01:00
|
|
|
if (ip[1] == '/' && ip[2] != '/') {
|
2011-03-28 23:31:01 +02:00
|
|
|
/* keep them, e.g. for UNC pathnames */
|
2011-03-24 20:05:49 +01:00
|
|
|
Xput(xs, xp, '/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* otherwise (no symlink) merely go on */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* either found the target and successfully resolved it,
|
|
|
|
* or found its parent directory and may create it
|
|
|
|
*/
|
|
|
|
if (Xlength(xs, xp) == 0)
|
|
|
|
/*
|
|
|
|
* if the resolved pathname is "", make it "/",
|
|
|
|
* otherwise do not add a trailing slash
|
|
|
|
*/
|
|
|
|
Xput(xs, xp, '/');
|
|
|
|
Xput(xs, xp, '\0');
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if source path had a trailing slash, check if target path
|
|
|
|
* is not a non-directory existing file
|
|
|
|
*/
|
|
|
|
if (ip > ipath && ip[-1] == '/') {
|
|
|
|
if (stat(Xstring(xs, xp), &sb)) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
goto notfound;
|
|
|
|
} else if (!S_ISDIR(sb.st_mode)) {
|
|
|
|
errno = ENOTDIR;
|
|
|
|
goto notfound;
|
|
|
|
}
|
|
|
|
/* target now either does not exist or is a directory */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return target path */
|
|
|
|
if (ldest != NULL)
|
|
|
|
afree(ldest, ATEMP);
|
|
|
|
afree(ipath, ATEMP);
|
|
|
|
return (Xclose(xs, xp));
|
|
|
|
|
|
|
|
notfound:
|
|
|
|
/* save; freeing memory might trash it */
|
|
|
|
llen = errno;
|
|
|
|
if (ldest != NULL)
|
|
|
|
afree(ldest, ATEMP);
|
|
|
|
afree(ipath, ATEMP);
|
|
|
|
Xfree(xs, xp);
|
|
|
|
errno = llen;
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
#undef pathlen
|
|
|
|
#undef pathcnd
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/**
|
2005-05-23 05:06:10 +02:00
|
|
|
* Makes a filename into result using the following algorithm.
|
|
|
|
* - make result NULL
|
|
|
|
* - if file starts with '/', append file to result & set cdpathp to NULL
|
|
|
|
* - if file starts with ./ or ../ append cwd and file to result
|
|
|
|
* and set cdpathp to NULL
|
|
|
|
* - if the first element of cdpathp doesnt start with a '/' xx or '.' xx
|
|
|
|
* then cwd is appended to result.
|
|
|
|
* - the first element of cdpathp is appended to result
|
|
|
|
* - file is appended to result
|
|
|
|
* - cdpathp is set to the start of the next element in cdpathp (or NULL
|
|
|
|
* if there are no more elements.
|
|
|
|
* The return value indicates whether a non-null element from cdpathp
|
|
|
|
* was appended to result.
|
|
|
|
*/
|
2011-03-24 20:05:49 +01:00
|
|
|
static int
|
2005-05-23 05:06:10 +02:00
|
|
|
make_path(const char *cwd, const char *file,
|
2011-03-24 20:05:49 +01:00
|
|
|
/* pointer to colon-separated list */
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
char **cdpathp,
|
2005-05-23 05:06:10 +02:00
|
|
|
XString *xsp,
|
|
|
|
int *phys_pathp)
|
|
|
|
{
|
2009-04-07 21:51:47 +02:00
|
|
|
int rval = 0;
|
|
|
|
bool use_cdpath = true;
|
|
|
|
char *plist;
|
2011-08-27 20:06:52 +02:00
|
|
|
size_t len, plen = 0;
|
2009-04-07 21:51:47 +02:00
|
|
|
char *xp = Xstring(*xsp, xp);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (!file)
|
|
|
|
file = null;
|
|
|
|
|
|
|
|
if (file[0] == '/') {
|
|
|
|
*phys_pathp = 0;
|
2009-04-07 21:51:47 +02:00
|
|
|
use_cdpath = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
if (file[0] == '.') {
|
|
|
|
char c = file[1];
|
|
|
|
|
|
|
|
if (c == '.')
|
|
|
|
c = file[2];
|
|
|
|
if (c == '/' || c == '\0')
|
2009-04-07 21:51:47 +02:00
|
|
|
use_cdpath = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
plist = *cdpathp;
|
|
|
|
if (!plist)
|
2009-04-07 21:51:47 +02:00
|
|
|
use_cdpath = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (use_cdpath) {
|
|
|
|
char *pend;
|
|
|
|
|
|
|
|
for (pend = plist; *pend && *pend != ':'; pend++)
|
|
|
|
;
|
|
|
|
plen = pend - plist;
|
2008-10-14 01:06:04 +02:00
|
|
|
*cdpathp = *pend ? pend + 1 : NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2009-04-07 21:51:47 +02:00
|
|
|
if ((!use_cdpath || !plen || plist[0] != '/') &&
|
2005-05-23 05:06:10 +02:00
|
|
|
(cwd && *cwd)) {
|
|
|
|
len = strlen(cwd);
|
|
|
|
XcheckN(*xsp, xp, len);
|
|
|
|
memcpy(xp, cwd, len);
|
|
|
|
xp += len;
|
|
|
|
if (cwd[len - 1] != '/')
|
|
|
|
Xput(*xsp, xp, '/');
|
|
|
|
}
|
|
|
|
*phys_pathp = Xlength(*xsp, xp);
|
|
|
|
if (use_cdpath && plen) {
|
|
|
|
XcheckN(*xsp, xp, plen);
|
|
|
|
memcpy(xp, plist, plen);
|
|
|
|
xp += plen;
|
|
|
|
if (plist[plen - 1] != '/')
|
|
|
|
Xput(*xsp, xp, '/');
|
|
|
|
rval = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(file) + 1;
|
|
|
|
XcheckN(*xsp, xp, len);
|
|
|
|
memcpy(xp, file, len);
|
|
|
|
|
|
|
|
if (!use_cdpath)
|
|
|
|
*cdpathp = NULL;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (rval);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-03-26 22:09:09 +01:00
|
|
|
/*-
|
2005-05-23 05:06:10 +02:00
|
|
|
* Simplify pathnames containing "." and ".." entries.
|
2011-03-26 22:09:09 +01:00
|
|
|
*
|
|
|
|
* simplify_path(this) = that
|
|
|
|
* /a/b/c/./../d/.. /a/b
|
2011-03-28 23:31:01 +02:00
|
|
|
* //./C/foo/bar/../baz //C/foo/baz
|
2011-03-26 22:09:09 +01:00
|
|
|
* /foo/ /foo
|
|
|
|
* /foo/../../bar /bar
|
|
|
|
* /foo/./blah/.. /foo
|
|
|
|
* . .
|
|
|
|
* .. ..
|
|
|
|
* ./foo foo
|
|
|
|
* foo/../../../bar ../../bar
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
void
|
2011-03-26 22:09:09 +01:00
|
|
|
simplify_path(char *p)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-03-26 22:09:09 +01:00
|
|
|
char *dp, *ip, *sp, *tp;
|
|
|
|
size_t len;
|
|
|
|
bool needslash;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-26 22:09:09 +01:00
|
|
|
switch (*p) {
|
|
|
|
case 0:
|
2011-03-26 20:29:41 +01:00
|
|
|
return;
|
2011-03-26 22:09:09 +01:00
|
|
|
case '/':
|
|
|
|
/* exactly two leading slashes? (SUSv4 3.266) */
|
|
|
|
if (p[1] == '/' && p[2] != '/')
|
2011-03-28 23:31:01 +02:00
|
|
|
/* keep them, e.g. for UNC pathnames */
|
|
|
|
++p;
|
2011-03-26 22:09:09 +01:00
|
|
|
needslash = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
needslash = false;
|
|
|
|
}
|
|
|
|
dp = ip = sp = p;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-26 22:09:09 +01:00
|
|
|
while (*ip) {
|
|
|
|
/* skip slashes in input */
|
|
|
|
while (*ip == '/')
|
|
|
|
++ip;
|
|
|
|
if (!*ip)
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
|
2011-03-26 22:09:09 +01:00
|
|
|
/* get next pathname component from input */
|
|
|
|
tp = ip;
|
|
|
|
while (*ip && *ip != '/')
|
|
|
|
++ip;
|
|
|
|
len = ip - tp;
|
|
|
|
|
|
|
|
/* check input for "." and ".." */
|
|
|
|
if (tp[0] == '.') {
|
|
|
|
if (len == 1)
|
|
|
|
/* just continue with the next one */
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
2011-03-26 22:09:09 +01:00
|
|
|
else if (len == 2 && tp[1] == '.') {
|
|
|
|
/* parent level, but how? */
|
|
|
|
if (*p == '/')
|
|
|
|
/* absolute path, only one way */
|
|
|
|
goto strip_last_component;
|
|
|
|
else if (dp > sp) {
|
|
|
|
/* relative path, with subpaths */
|
|
|
|
needslash = false;
|
|
|
|
strip_last_component:
|
|
|
|
/* strip off last pathname component */
|
|
|
|
while (dp > sp)
|
|
|
|
if (*--dp == '/')
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* relative path, at its beginning */
|
|
|
|
if (needslash)
|
|
|
|
/* or already dotdot-slash'd */
|
|
|
|
*dp++ = '/';
|
|
|
|
/* keep dotdot-slash if not absolute */
|
|
|
|
*dp++ = '.';
|
|
|
|
*dp++ = '.';
|
|
|
|
needslash = true;
|
|
|
|
sp = dp;
|
|
|
|
}
|
|
|
|
/* then continue with the next one */
|
2005-05-23 05:06:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-26 22:09:09 +01:00
|
|
|
if (needslash)
|
|
|
|
*dp++ = '/';
|
|
|
|
|
|
|
|
/* append next pathname component to output */
|
|
|
|
memmove(dp, tp, len);
|
|
|
|
dp += len;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-26 22:09:09 +01:00
|
|
|
/* append slash if we continue */
|
|
|
|
needslash = true;
|
|
|
|
/* try next component */
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-03-26 22:09:09 +01:00
|
|
|
if (dp == p)
|
|
|
|
/* empty path -> dot */
|
|
|
|
*dp++ = needslash ? '/' : '.';
|
|
|
|
*dp = '\0';
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-03-26 20:43:49 +01:00
|
|
|
set_current_wd(const char *nwd)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-03-24 20:05:49 +01:00
|
|
|
char *allocd = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-24 20:05:49 +01:00
|
|
|
if (nwd == NULL) {
|
|
|
|
allocd = ksh_get_wd();
|
|
|
|
nwd = allocd ? allocd : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
afree(current_wd, APERM);
|
|
|
|
strdupx(current_wd, nwd, APERM);
|
|
|
|
|
|
|
|
afree(allocd, ATEMP);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-24 20:05:49 +01:00
|
|
|
int
|
|
|
|
c_cd(const char **wp)
|
|
|
|
{
|
|
|
|
int optc, rv, phys_path;
|
|
|
|
bool physical = tobool(Flag(FPHYSICAL));
|
|
|
|
/* was a node from cdpath added in? */
|
|
|
|
int cdnode;
|
2011-05-29 04:18:57 +02:00
|
|
|
/* show where we went?, error for $PWD */
|
|
|
|
bool printpath = false, eflag = false;
|
2011-03-24 20:05:49 +01:00
|
|
|
struct tbl *pwd_s, *oldpwd_s;
|
|
|
|
XString xs;
|
|
|
|
char *dir, *allocd = NULL, *tryp, *pwd, *cdpath;
|
|
|
|
|
2011-05-29 04:18:57 +02:00
|
|
|
while ((optc = ksh_getopt(wp, &builtin_opt, "eLP")) != -1)
|
2011-03-24 20:05:49 +01:00
|
|
|
switch (optc) {
|
2011-05-29 04:18:57 +02:00
|
|
|
case 'e':
|
|
|
|
eflag = true;
|
|
|
|
break;
|
2011-03-24 20:05:49 +01:00
|
|
|
case 'L':
|
|
|
|
physical = false;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
physical = true;
|
|
|
|
break;
|
|
|
|
case '?':
|
2011-05-29 04:18:57 +02:00
|
|
|
return (2);
|
2011-03-24 20:05:49 +01:00
|
|
|
}
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
|
|
|
if (Flag(FRESTRICTED)) {
|
|
|
|
bi_errorf("restricted shell - can't cd");
|
2011-05-29 04:18:57 +02:00
|
|
|
return (2);
|
2008-11-12 01:54:52 +01:00
|
|
|
}
|
2011-03-24 20:05:49 +01:00
|
|
|
|
|
|
|
pwd_s = global("PWD");
|
|
|
|
oldpwd_s = global("OLDPWD");
|
|
|
|
|
|
|
|
if (!wp[0]) {
|
|
|
|
/* No arguments - go home */
|
|
|
|
if ((dir = str_val(global("HOME"))) == null) {
|
|
|
|
bi_errorf("no home directory (HOME not set)");
|
2011-05-29 04:18:57 +02:00
|
|
|
return (2);
|
2011-03-24 20:05:49 +01:00
|
|
|
}
|
|
|
|
} else if (!wp[1]) {
|
|
|
|
/* One argument: - or dir */
|
|
|
|
strdupx(allocd, wp[0], ATEMP);
|
|
|
|
if (ksh_isdash((dir = allocd))) {
|
|
|
|
afree(allocd, ATEMP);
|
|
|
|
allocd = NULL;
|
|
|
|
dir = str_val(oldpwd_s);
|
|
|
|
if (dir == null) {
|
|
|
|
bi_errorf("no OLDPWD");
|
2011-05-29 04:18:57 +02:00
|
|
|
return (2);
|
2011-03-24 20:05:49 +01:00
|
|
|
}
|
|
|
|
printpath = true;
|
|
|
|
}
|
|
|
|
} else if (!wp[2]) {
|
|
|
|
/* Two arguments - substitute arg1 in PWD for arg2 */
|
|
|
|
size_t ilen, olen, nlen, elen;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
if (!current_wd[0]) {
|
|
|
|
bi_errorf("can't determine current directory");
|
2011-05-29 04:18:57 +02:00
|
|
|
return (2);
|
2011-03-24 20:05:49 +01:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* substitute arg1 for arg2 in current path.
|
|
|
|
* if the first substitution fails because the cd fails
|
|
|
|
* we could try to find another substitution. For now
|
|
|
|
* we don't
|
|
|
|
*/
|
|
|
|
if ((cp = strstr(current_wd, wp[0])) == NULL) {
|
|
|
|
bi_errorf("bad substitution");
|
2011-05-29 04:18:57 +02:00
|
|
|
return (2);
|
2011-03-24 20:05:49 +01:00
|
|
|
}
|
|
|
|
/*-
|
|
|
|
* ilen = part of current_wd before wp[0]
|
|
|
|
* elen = part of current_wd after wp[0]
|
|
|
|
* because current_wd and wp[1] need to be in memory at the
|
|
|
|
* same time beforehand the addition can stay unchecked
|
|
|
|
*/
|
|
|
|
ilen = cp - current_wd;
|
|
|
|
olen = strlen(wp[0]);
|
|
|
|
nlen = strlen(wp[1]);
|
|
|
|
elen = strlen(current_wd + ilen + olen) + 1;
|
|
|
|
dir = allocd = alloc(ilen + nlen + elen, ATEMP);
|
|
|
|
memcpy(dir, current_wd, ilen);
|
|
|
|
memcpy(dir + ilen, wp[1], nlen);
|
|
|
|
memcpy(dir + ilen + nlen, current_wd + ilen + olen, elen);
|
|
|
|
printpath = true;
|
|
|
|
} else {
|
|
|
|
bi_errorf("too many arguments");
|
2011-05-29 04:18:57 +02:00
|
|
|
return (2);
|
2011-03-24 20:05:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NO_PATH_MAX
|
|
|
|
/* only a first guess; make_path will enlarge xs if necessary */
|
|
|
|
XinitN(xs, 1024, ATEMP);
|
|
|
|
#else
|
|
|
|
XinitN(xs, PATH_MAX, ATEMP);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cdpath = str_val(global("CDPATH"));
|
|
|
|
do {
|
|
|
|
cdnode = make_path(current_wd, dir, &cdpath, &xs, &phys_path);
|
|
|
|
if (physical)
|
|
|
|
rv = chdir(tryp = Xstring(xs, xp) + phys_path);
|
|
|
|
else {
|
|
|
|
simplify_path(Xstring(xs, xp));
|
|
|
|
rv = chdir(tryp = Xstring(xs, xp));
|
|
|
|
}
|
|
|
|
} while (rv < 0 && cdpath != NULL);
|
|
|
|
|
|
|
|
if (rv < 0) {
|
|
|
|
if (cdnode)
|
|
|
|
bi_errorf("%s: %s", dir, "bad directory");
|
|
|
|
else
|
|
|
|
bi_errorf("%s: %s", tryp, strerror(errno));
|
|
|
|
afree(allocd, ATEMP);
|
2011-05-29 04:18:57 +02:00
|
|
|
Xfree(xs, xp);
|
|
|
|
return (2);
|
2011-03-24 20:05:49 +01:00
|
|
|
}
|
|
|
|
|
2011-05-29 04:18:57 +02:00
|
|
|
rv = 0;
|
|
|
|
|
2011-03-24 20:05:49 +01:00
|
|
|
/* allocd (above) => dir, which is no longer used */
|
|
|
|
afree(allocd, ATEMP);
|
|
|
|
allocd = NULL;
|
|
|
|
|
|
|
|
/* Clear out tracked aliases with relative paths */
|
|
|
|
flushcom(false);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set OLDPWD (note: unsetting OLDPWD does not disable this
|
|
|
|
* setting in AT&T ksh)
|
|
|
|
*/
|
|
|
|
if (current_wd[0])
|
|
|
|
/* Ignore failure (happens if readonly or integer) */
|
|
|
|
setstr(oldpwd_s, current_wd, KSH_RETURN_ERROR);
|
|
|
|
|
|
|
|
if (Xstring(xs, xp)[0] != '/') {
|
|
|
|
pwd = NULL;
|
2011-05-29 04:18:57 +02:00
|
|
|
} else if (!physical) {
|
|
|
|
goto norealpath_PWD;
|
|
|
|
} else if ((pwd = allocd = do_realpath(Xstring(xs, xp))) == NULL) {
|
|
|
|
if (eflag)
|
|
|
|
rv = 1;
|
|
|
|
norealpath_PWD:
|
2011-03-24 20:05:49 +01:00
|
|
|
pwd = Xstring(xs, xp);
|
2011-05-29 04:18:57 +02:00
|
|
|
}
|
2011-03-24 20:05:49 +01:00
|
|
|
|
|
|
|
/* Set PWD */
|
|
|
|
if (pwd) {
|
|
|
|
char *ptmp = pwd;
|
|
|
|
|
|
|
|
set_current_wd(ptmp);
|
|
|
|
/* Ignore failure (happens if readonly or integer) */
|
|
|
|
setstr(pwd_s, ptmp, KSH_RETURN_ERROR);
|
|
|
|
} else {
|
|
|
|
set_current_wd(null);
|
|
|
|
pwd = Xstring(xs, xp);
|
|
|
|
/* XXX unset $PWD? */
|
2011-05-29 04:18:57 +02:00
|
|
|
if (eflag)
|
|
|
|
rv = 1;
|
2011-03-24 20:05:49 +01:00
|
|
|
}
|
|
|
|
if (printpath || cdnode)
|
|
|
|
shprintf("%s\n", pwd);
|
|
|
|
|
|
|
|
afree(allocd, ATEMP);
|
2011-05-29 04:18:57 +02:00
|
|
|
Xfree(xs, xp);
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-03-24 20:05:49 +01:00
|
|
|
|
2009-10-30 15:37:43 +01:00
|
|
|
#ifdef TIOCSCTTY
|
|
|
|
extern void chvt_reinit(void);
|
|
|
|
|
2006-11-10 02:13:52 +01:00
|
|
|
static void
|
2007-01-18 21:54:30 +01:00
|
|
|
chvt(const char *fn)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2006-11-10 02:13:52 +01:00
|
|
|
char dv[20];
|
|
|
|
struct stat sb;
|
2005-05-23 05:06:10 +02:00
|
|
|
int fd;
|
|
|
|
|
2008-03-23 23:09:59 +01:00
|
|
|
if (*fn == '-') {
|
2009-06-08 22:06:50 +02:00
|
|
|
memcpy(dv, "-/dev/null", sizeof("-/dev/null"));
|
2008-03-23 23:09:59 +01:00
|
|
|
fn = dv + 1;
|
|
|
|
} else {
|
|
|
|
if (stat(fn, &sb)) {
|
|
|
|
memcpy(dv, "/dev/ttyC", 9);
|
2009-08-01 22:31:47 +02:00
|
|
|
strlcpy(dv + 9, fn, sizeof(dv) - 9);
|
2008-03-23 23:09:59 +01:00
|
|
|
if (stat(dv, &sb)) {
|
2009-08-01 22:31:47 +02:00
|
|
|
strlcpy(dv + 8, fn, sizeof(dv) - 8);
|
2008-03-23 23:09:59 +01:00
|
|
|
if (stat(dv, &sb))
|
2010-08-28 20:50:58 +02:00
|
|
|
errorf("%s: %s %s", "chvt",
|
|
|
|
"can't find tty", fn);
|
2008-03-23 23:09:59 +01:00
|
|
|
}
|
|
|
|
fn = dv;
|
2006-11-10 02:13:52 +01:00
|
|
|
}
|
2008-03-23 23:09:59 +01:00
|
|
|
if (!(sb.st_mode & S_IFCHR))
|
2010-08-28 20:50:58 +02:00
|
|
|
errorf("%s %s %s", "chvt: not a char", "device", fn);
|
2008-03-23 23:09:59 +01:00
|
|
|
if ((sb.st_uid != 0) && chown(fn, 0, 0))
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(false, "%s: %s %s", "chvt", "can't chown root", fn);
|
2009-08-08 15:08:53 +02:00
|
|
|
if (((sb.st_mode & 07777) != 0600) && chmod(fn, (mode_t)0600))
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(false, "%s: %s %s", "chvt", "can't chmod 0600", fn);
|
2007-01-18 02:01:25 +01:00
|
|
|
#if HAVE_REVOKE
|
2008-03-23 23:09:59 +01:00
|
|
|
if (revoke(fn))
|
2005-05-23 05:06:10 +02:00
|
|
|
#endif
|
2010-08-28 22:22:24 +02:00
|
|
|
warningf(false, "%s: %s %s", "chvt",
|
|
|
|
"new shell is potentially insecure, can't revoke",
|
|
|
|
fn);
|
2008-03-23 23:09:59 +01:00
|
|
|
}
|
2006-11-10 02:13:52 +01:00
|
|
|
if ((fd = open(fn, O_RDWR)) == -1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
sleep(1);
|
2006-11-10 02:13:52 +01:00
|
|
|
if ((fd = open(fn, O_RDWR)) == -1)
|
2010-08-28 22:22:24 +02:00
|
|
|
errorf("%s: %s %s", "chvt", "can't open", fn);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
2010-08-28 20:50:58 +02:00
|
|
|
errorf("%s: %s %s", "chvt", "fork", "failed");
|
2005-05-23 05:06:10 +02:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
2006-11-10 04:54:38 +01:00
|
|
|
exit(0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (setsid() == -1)
|
2010-08-28 20:50:58 +02:00
|
|
|
errorf("%s: %s %s", "chvt", "setsid", "failed");
|
2008-04-11 21:55:24 +02:00
|
|
|
if (fn != dv + 1) {
|
|
|
|
if (ioctl(fd, TIOCSCTTY, NULL) == -1)
|
2010-08-28 20:50:58 +02:00
|
|
|
errorf("%s: %s %s", "chvt", "TIOCSCTTY", "failed");
|
2008-04-11 21:55:24 +02:00
|
|
|
if (tcflush(fd, TCIOFLUSH))
|
2010-08-28 20:50:58 +02:00
|
|
|
errorf("%s: %s %s", "chvt", "TCIOFLUSH", "failed");
|
2008-04-11 21:55:24 +02:00
|
|
|
}
|
2008-04-01 22:40:22 +02:00
|
|
|
ksh_dup2(fd, 0, false);
|
|
|
|
ksh_dup2(fd, 1, false);
|
|
|
|
ksh_dup2(fd, 2, false);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (fd > 2)
|
|
|
|
close(fd);
|
2011-01-21 22:04:48 +01:00
|
|
|
{
|
|
|
|
register uint32_t h;
|
|
|
|
|
2011-07-07 00:22:02 +02:00
|
|
|
NZATInit(h);
|
|
|
|
NZATUpdateMem(h, &rndsetupstate, sizeof(rndsetupstate));
|
|
|
|
NZAATFinish(h);
|
2011-01-21 22:04:48 +01:00
|
|
|
rndset((long)h);
|
|
|
|
}
|
2009-10-30 15:37:43 +01:00
|
|
|
chvt_reinit();
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
#endif
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
char *
|
|
|
|
strchr(char *p, int ch)
|
|
|
|
{
|
|
|
|
for (;; ++p) {
|
|
|
|
if (*p == ch)
|
|
|
|
return (p);
|
|
|
|
if (!*p)
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
strstr(char *b, const char *l)
|
|
|
|
{
|
|
|
|
char first, c;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if ((first = *l++) == '\0')
|
|
|
|
return (b);
|
|
|
|
n = strlen(l);
|
|
|
|
strstr_look:
|
|
|
|
while ((c = *b++) != first)
|
|
|
|
if (c == '\0')
|
|
|
|
return (NULL);
|
|
|
|
if (strncmp(b, l, n))
|
|
|
|
goto strstr_look;
|
|
|
|
return (b - 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-10-28 15:51:06 +01:00
|
|
|
#ifdef MKSH_SMALL
|
|
|
|
char *
|
2011-10-26 00:36:39 +02:00
|
|
|
strndup_i(const char *src, size_t len, Area *ap)
|
2008-10-28 15:51:06 +01:00
|
|
|
{
|
|
|
|
char *dst = NULL;
|
|
|
|
|
|
|
|
if (src != NULL) {
|
2009-08-01 22:29:25 +02:00
|
|
|
dst = alloc(len + 1, ap);
|
|
|
|
memcpy(dst, src, len);
|
|
|
|
dst[len] = '\0';
|
2008-10-28 15:51:06 +01:00
|
|
|
}
|
|
|
|
return (dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2011-10-26 00:36:39 +02:00
|
|
|
strdup_i(const char *src, Area *ap)
|
2008-10-28 15:51:06 +01:00
|
|
|
{
|
2011-10-26 00:36:39 +02:00
|
|
|
return (src == NULL ? NULL : strndup_i(src, strlen(src), ap));
|
2008-10-28 15:51:06 +01:00
|
|
|
}
|
|
|
|
#endif
|
2009-04-03 11:39:07 +02:00
|
|
|
|
|
|
|
#if !HAVE_GETRUSAGE
|
|
|
|
#define INVTCK(r,t) do { \
|
|
|
|
r.tv_usec = ((t) % (1000000 / CLK_TCK)) * (1000000 / CLK_TCK); \
|
|
|
|
r.tv_sec = (t) / CLK_TCK; \
|
|
|
|
} while (/* CONSTCOND */ 0)
|
|
|
|
|
|
|
|
int
|
|
|
|
getrusage(int what, struct rusage *ru)
|
|
|
|
{
|
|
|
|
struct tms tms;
|
|
|
|
clock_t u, s;
|
|
|
|
|
|
|
|
if (/* ru == NULL || */ times(&tms) == (clock_t)-1)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
switch (what) {
|
|
|
|
case RUSAGE_SELF:
|
|
|
|
u = tms.tms_utime;
|
|
|
|
s = tms.tms_stime;
|
|
|
|
break;
|
|
|
|
case RUSAGE_CHILDREN:
|
|
|
|
u = tms.tms_cutime;
|
|
|
|
s = tms.tms_cstime;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
2009-04-03 12:54:58 +02:00
|
|
|
INVTCK(ru->ru_utime, u);
|
|
|
|
INVTCK(ru->ru_stime, s);
|
2009-04-03 11:39:07 +02:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
#endif
|
2009-09-19 17:16:05 +02:00
|
|
|
|
|
|
|
/*
|
2009-09-19 23:54:46 +02:00
|
|
|
* process the string available via fg (get a char)
|
|
|
|
* and fp (put back a char) for backslash escapes,
|
|
|
|
* assuming the first call to *fg gets the char di-
|
|
|
|
* rectly after the backslash; return the character
|
|
|
|
* (0..0xFF), Unicode (wc + 0x100), or -1 if no known
|
|
|
|
* escape sequence was found
|
2009-09-19 17:16:05 +02:00
|
|
|
*/
|
|
|
|
int
|
2009-09-19 23:54:46 +02:00
|
|
|
unbksl(bool cstyle, int (*fg)(void), void (*fp)(int))
|
2009-09-19 17:16:05 +02:00
|
|
|
{
|
2009-09-19 23:54:46 +02:00
|
|
|
int wc, i, c, fc;
|
2009-09-19 17:16:05 +02:00
|
|
|
|
2009-09-19 23:54:46 +02:00
|
|
|
fc = (*fg)();
|
|
|
|
switch (fc) {
|
2009-09-19 17:16:05 +02:00
|
|
|
case 'a':
|
|
|
|
/*
|
|
|
|
* according to the comments in pdksh, \007 seems
|
|
|
|
* to be more portable than \a (due to HP-UX cc,
|
|
|
|
* Ultrix cc, old pcc, etc.) so we avoid the escape
|
|
|
|
* sequence altogether in mksh and assume ASCII
|
|
|
|
*/
|
|
|
|
wc = 7;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
wc = '\b';
|
|
|
|
break;
|
2009-09-19 23:54:46 +02:00
|
|
|
case 'c':
|
|
|
|
if (!cstyle)
|
|
|
|
goto unknown_escape;
|
|
|
|
c = (*fg)();
|
|
|
|
wc = CTRL(c);
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
case 'e':
|
|
|
|
wc = 033;
|
|
|
|
break;
|
2009-09-19 17:16:05 +02:00
|
|
|
case 'f':
|
|
|
|
wc = '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
wc = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
wc = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
wc = '\t';
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
/* assume ASCII here as well */
|
|
|
|
wc = 11;
|
|
|
|
break;
|
2009-09-19 21:08:48 +02:00
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
if (!cstyle)
|
2009-09-19 23:54:46 +02:00
|
|
|
goto unknown_escape;
|
2009-09-19 21:08:48 +02:00
|
|
|
/* FALLTHROUGH */
|
2009-09-19 17:16:05 +02:00
|
|
|
case '0':
|
2009-09-19 21:08:48 +02:00
|
|
|
if (cstyle)
|
2009-09-19 23:54:46 +02:00
|
|
|
(*fp)(fc);
|
2009-09-19 17:16:05 +02:00
|
|
|
/*
|
|
|
|
* look for an octal number with up to three
|
|
|
|
* digits, not counting the leading zero;
|
|
|
|
* convert it to a raw octet
|
|
|
|
*/
|
|
|
|
wc = 0;
|
|
|
|
i = 3;
|
2009-09-19 23:54:46 +02:00
|
|
|
while (i--)
|
|
|
|
if ((c = (*fg)()) >= '0' && c <= '7')
|
|
|
|
wc = (wc << 3) + (c - '0');
|
|
|
|
else {
|
|
|
|
(*fp)(c);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-19 17:16:05 +02:00
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
i = 8;
|
2011-03-13 02:20:25 +01:00
|
|
|
if (/* CONSTCOND */ 0)
|
2009-09-19 17:16:05 +02:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 'u':
|
|
|
|
i = 4;
|
2011-03-13 02:20:25 +01:00
|
|
|
if (/* CONSTCOND */ 0)
|
2009-09-19 17:16:05 +02:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 'x':
|
2009-09-19 21:08:48 +02:00
|
|
|
i = cstyle ? -1 : 2;
|
• more comment and int→bool cleanup, add and improve some comments
• in interactive mode, always look up {LC_{ALL,CTYPE},LANG} environment
variables if setlocale/nl_langinfo(CODESET) doesn’t suffice
• add the ability to call any builtin (some don't make sense or wouldn't
work) directly by analysing argv[0]
• for direct builtin calls, the {LC_{ALL,CTYPE},LANG} environment
variables determine utf8-mode, even if MKSH_ASSUME_UTF8 was set
• when called as builtin, echo behaves POSIXish
• add domainname as alias for true on MirBSD only, to be able to link it
• sync mksh Makefiles with Build.sh output
• adjust manpage wrt release plans
• link some things to mksh now that we have callable builtins:
bin/echo bin/kill bin/pwd bin/sleep (exact matches)
bin/test bin/[ (were scripts before)
bin/domainname=usr/bin/true usr/bin/false (move to /bin/ now)
• drop linked utilities and, except for echo and kill, their manpages
• adjust instbin and link a few more there as well
2011-02-11 02:18:23 +01:00
|
|
|
/**
|
2009-09-19 21:08:48 +02:00
|
|
|
* x: look for a hexadecimal number with up to
|
|
|
|
* two (C style: arbitrary) digits; convert
|
2009-09-20 00:32:23 +02:00
|
|
|
* to raw octet (C style: Unicode if >0xFF)
|
2009-09-19 21:08:48 +02:00
|
|
|
* u/U: look for a hexadecimal number with up to
|
|
|
|
* four (U: eight) digits; convert to Unicode
|
2009-09-19 17:16:05 +02:00
|
|
|
*/
|
|
|
|
wc = 0;
|
|
|
|
while (i--) {
|
|
|
|
wc <<= 4;
|
2009-09-19 23:54:46 +02:00
|
|
|
if ((c = (*fg)()) >= '0' && c <= '9')
|
|
|
|
wc += c - '0';
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
wc += c - 'A' + 10;
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
wc += c - 'a' + 10;
|
2009-09-19 17:16:05 +02:00
|
|
|
else {
|
|
|
|
wc >>= 4;
|
2009-09-19 23:54:46 +02:00
|
|
|
(*fp)(c);
|
2009-09-19 17:16:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-20 00:32:23 +02:00
|
|
|
if ((cstyle && wc > 0xFF) || fc != 'x')
|
2009-09-19 17:16:05 +02:00
|
|
|
/* Unicode marker */
|
|
|
|
wc += 0x100;
|
|
|
|
break;
|
2009-09-19 23:54:46 +02:00
|
|
|
case '\'':
|
|
|
|
if (!cstyle)
|
|
|
|
goto unknown_escape;
|
|
|
|
wc = '\'';
|
|
|
|
break;
|
2009-09-19 17:16:05 +02:00
|
|
|
case '\\':
|
|
|
|
wc = '\\';
|
|
|
|
break;
|
|
|
|
default:
|
2009-09-19 23:54:46 +02:00
|
|
|
unknown_escape:
|
|
|
|
(*fp)(fc);
|
2009-09-19 17:16:05 +02:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (wc);
|
|
|
|
}
|