2015-10-09 18:11:19 +02:00
|
|
|
/* $OpenBSD: misc.c,v 1.41 2015/09/10 22:48:58 nicm Exp $ */
|
2015-09-05 21:19:12 +02:00
|
|
|
/* $OpenBSD: path.c,v 1.13 2015/09/05 09:47:08 jsg Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
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,
|
2016-01-21 19:24:45 +01:00
|
|
|
* 2011, 2012, 2013, 2014, 2015, 2016
|
2015-10-09 18:11:19 +02:00
|
|
|
* mirabilos <m@mirbsd.org>
|
2009-05-16 18:59:42 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2016-01-21 19:24:45 +01:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.241 2016/01/21 18:24:43 tg Exp $");
|
2012-06-26 21:22:19 +02:00
|
|
|
|
|
|
|
#define KSH_CHVT_FLAG
|
|
|
|
#ifdef MKSH_SMALL
|
|
|
|
#undef KSH_CHVT_FLAG
|
|
|
|
#endif
|
|
|
|
#ifdef TIOCSCTTY
|
|
|
|
#define KSH_CHVT_CODE
|
|
|
|
#define KSH_CHVT_FLAG
|
|
|
|
#endif
|
|
|
|
#ifdef MKSH_LEGACY_MODE
|
|
|
|
#undef KSH_CHVT_CODE
|
|
|
|
#undef KSH_CHVT_FLAG
|
|
|
|
#endif
|
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 *,
|
2014-01-05 22:57:29 +01:00
|
|
|
const unsigned char *, bool) MKSH_A_PURE;
|
2005-05-23 05:06:10 +02:00
|
|
|
static int do_gmatch(const unsigned char *, const unsigned char *,
|
2014-01-05 22:57:29 +01:00
|
|
|
const unsigned char *, const unsigned char *) MKSH_A_PURE;
|
2015-04-29 20:38:54 +02:00
|
|
|
static const unsigned char *gmatch_cclass(const unsigned char *, unsigned char)
|
2014-01-05 22:57:29 +01:00
|
|
|
MKSH_A_PURE;
|
2012-06-26 21:22:19 +02:00
|
|
|
#ifdef KSH_CHVT_CODE
|
2013-06-04 00:28:06 +02:00
|
|
|
static void chvt(const Getopt *);
|
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)
|
|
|
|
{
|
2015-04-29 22:21:35 +02:00
|
|
|
setctypes(letters_uc, C_ALPHA);
|
|
|
|
setctypes(letters_lc, C_ALPHA);
|
2005-05-23 05:06:10 +02:00
|
|
|
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 */
|
2012-07-30 23:37:17 +02:00
|
|
|
setctypes(TC_LEX1, C_LEX1);
|
2005-05-23 05:06:10 +02:00
|
|
|
setctypes("*@#!$-?", C_VAR1);
|
2012-07-30 23:37:17 +02:00
|
|
|
setctypes(TC_IFSWS, C_IFSWS);
|
2005-05-23 05:06:10 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-08-11 16:57:11 +02:00
|
|
|
|
2009-09-20 18:40:58 +02:00
|
|
|
#define SHFLAGS_DEFNS
|
2015-05-02 01:16:31 +02:00
|
|
|
#define FN(sname,cname,flags,ochar) \
|
|
|
|
static const struct { \
|
|
|
|
/* character flag (if any) */ \
|
|
|
|
char c; \
|
|
|
|
/* OF_* */ \
|
|
|
|
unsigned char optflags; \
|
|
|
|
/* long name of option */ \
|
|
|
|
char name[sizeof(sname)]; \
|
|
|
|
} shoptione_ ## cname = { \
|
|
|
|
ochar, flags, sname \
|
|
|
|
};
|
2013-11-17 23:22:56 +01:00
|
|
|
#include "sh_flags.gen"
|
2009-09-20 18:40:58 +02:00
|
|
|
|
2013-08-11 16:57:11 +02:00
|
|
|
#define OFC(i) (options[i][-2])
|
|
|
|
#define OFF(i) (((const unsigned char *)options[i])[-1])
|
|
|
|
#define OFN(i) (options[i])
|
|
|
|
|
|
|
|
const char * const options[] = {
|
2009-09-20 18:40:58 +02:00
|
|
|
#define SHFLAGS_ITEMS
|
2013-11-17 23:22:56 +01:00
|
|
|
#include "sh_flags.gen"
|
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)
|
|
|
|
{
|
2013-08-11 16:57:11 +02:00
|
|
|
size_t i = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2013-08-11 16:57:11 +02:00
|
|
|
if ((n[0] == '-' || n[0] == '+') && n[1] && !n[2])
|
|
|
|
while (i < NELEM(options)) {
|
|
|
|
if (OFC(i) == n[1])
|
2010-01-01 19:02:01 +01:00
|
|
|
return (i);
|
2013-08-11 16:57:11 +02:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
while (i < NELEM(options)) {
|
|
|
|
if (!strcmp(OFN(i), n))
|
|
|
|
return (i);
|
|
|
|
++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
|
|
|
};
|
|
|
|
|
2015-07-05 16:43:08 +02:00
|
|
|
static void options_fmt_entry(char *, size_t, unsigned 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 */
|
2015-07-05 16:43:08 +02:00
|
|
|
static void
|
2012-10-03 17:13:34 +02:00
|
|
|
options_fmt_entry(char *buf, size_t buflen, unsigned 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",
|
2013-08-11 16:57:11 +02:00
|
|
|
oi->opt_width, OFN(oi->opts[i]),
|
2006-11-10 02:44:40 +01:00
|
|
|
Flag(oi->opts[i]) ? "on" : "off");
|
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) {
|
2012-10-03 17:13:34 +02:00
|
|
|
size_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)) {
|
2013-08-11 16:57:11 +02:00
|
|
|
if ((len = strlen(OFN(i)))) {
|
2006-11-10 02:44:40 +01:00
|
|
|
oi.opts[n++] = i;
|
2009-11-10 00:35:11 +01:00
|
|
|
if (len > octs)
|
|
|
|
octs = len;
|
2013-08-11 16:57:11 +02:00
|
|
|
len = utf_mbswidth(OFN(i));
|
2012-10-03 17:13:34 +02:00
|
|
|
if ((int)len > oi.opt_width)
|
|
|
|
oi.opt_width = (int)len;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
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);
|
2013-08-11 16:57:11 +02:00
|
|
|
while (i < NELEM(options)) {
|
|
|
|
if (Flag(i) && OFN(i)[0])
|
|
|
|
shprintf(" -o %s", OFN(i));
|
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)
|
|
|
|
{
|
2013-08-11 16:57:11 +02:00
|
|
|
size_t i = 0;
|
|
|
|
char c, m[(int)FNFLAGS + 1];
|
2005-05-23 05:06:10 +02:00
|
|
|
char *cp = m;
|
|
|
|
|
2013-08-11 16:57:11 +02:00
|
|
|
while (i < NELEM(options)) {
|
|
|
|
if ((c = OFC(i)) && Flag(i))
|
|
|
|
*cp++ = c;
|
|
|
|
++i;
|
|
|
|
}
|
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
|
2012-12-05 20:38:25 +01:00
|
|
|
change_flag(enum sh_flag f, int what, bool newset)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2009-08-08 15:08:53 +02:00
|
|
|
unsigned char oldval;
|
2013-07-21 20:47:24 +02:00
|
|
|
unsigned char newval = (newset ? 1 : 0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2013-07-21 20:47:24 +02:00
|
|
|
if (f == FXTRACE) {
|
|
|
|
change_xtrace(newval, true);
|
|
|
|
return;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
oldval = Flag(f);
|
2012-12-05 20:38:25 +01:00
|
|
|
Flag(f) = newval = (newset ? 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
|
2012-05-04 22:49:08 +02:00
|
|
|
#ifndef MKSH_NO_CMDLINE_EDITING
|
2009-04-05 14:35:32 +02:00
|
|
|
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;
|
2012-12-05 20:38:25 +01:00
|
|
|
Flag(f) = newval;
|
2012-05-04 22:49:08 +02:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (f == FPRIVILEGED && oldval && !newval) {
|
2005-05-23 05:06:10 +02:00
|
|
|
/* 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();
|
2013-08-23 16:07:39 +02:00
|
|
|
ksheuid = kshuid = getuid();
|
2011-01-21 22:04:48 +01:00
|
|
|
#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));
|
2013-08-23 16:07:39 +02:00
|
|
|
#else /* !HAVE_SETRESUGID */
|
2015-01-02 09:18:44 +01:00
|
|
|
/* setgid, setegid, seteuid don't EAGAIN on Linux */
|
|
|
|
setgid(kshegid);
|
2012-05-04 23:57:38 +02:00
|
|
|
#ifndef MKSH__NO_SETEUGID
|
2015-01-02 09:18:44 +01:00
|
|
|
setegid(kshegid);
|
2012-04-01 18:55:16 +02:00
|
|
|
#endif
|
2010-09-14 23:26:19 +02:00
|
|
|
DO_SETUID(setuid, (ksheuid));
|
2012-05-04 23:57:38 +02:00
|
|
|
#ifndef MKSH__NO_SETEUGID
|
2015-01-02 09:18:44 +01:00
|
|
|
seteuid(ksheuid);
|
2012-04-01 18:55:16 +02:00
|
|
|
#endif
|
2013-08-23 16:07:39 +02:00
|
|
|
#endif /* !HAVE_SETRESUGID */
|
2010-01-28 16:18:51 +01:00
|
|
|
} else if ((f == FPOSIX || f == FSH) && newval) {
|
2013-05-02 22:21:45 +02:00
|
|
|
/* Turning on -o posix or -o sh? */
|
|
|
|
Flag(FBRACEEXPAND) = 0;
|
|
|
|
} else if (f == FTALKING) {
|
|
|
|
/* Changing interactive flag? */
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((what == OF_CMDLINE || what == OF_SET) && procpid == kshpid)
|
2012-12-05 20:38:25 +01:00
|
|
|
Flag(FTALKING_I) = newval;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-21 20:47:24 +02:00
|
|
|
void
|
|
|
|
change_xtrace(unsigned char newval, bool dosnapshot)
|
|
|
|
{
|
2015-03-01 16:23:05 +01:00
|
|
|
static bool in_xtrace;
|
|
|
|
|
|
|
|
if (in_xtrace)
|
|
|
|
return;
|
|
|
|
|
2013-07-21 20:47:24 +02:00
|
|
|
if (!dosnapshot && newval == Flag(FXTRACE))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Flag(FXTRACE) == 2) {
|
|
|
|
shf_putc('\n', shl_xtrace);
|
|
|
|
Flag(FXTRACE) = 1;
|
|
|
|
shf_flush(shl_xtrace);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dosnapshot && Flag(FXTRACE) == 1)
|
|
|
|
switch (newval) {
|
|
|
|
case 1:
|
|
|
|
return;
|
|
|
|
case 2:
|
|
|
|
goto changed_xtrace;
|
|
|
|
}
|
|
|
|
|
|
|
|
shf_flush(shl_xtrace);
|
|
|
|
if (shl_xtrace->fd != 2)
|
|
|
|
close(shl_xtrace->fd);
|
|
|
|
if (!newval || (shl_xtrace->fd = savefd(2)) == -1)
|
|
|
|
shl_xtrace->fd = 2;
|
|
|
|
|
|
|
|
changed_xtrace:
|
2015-03-01 16:23:05 +01:00
|
|
|
if ((Flag(FXTRACE) = newval) == 2) {
|
|
|
|
in_xtrace = true;
|
|
|
|
Flag(FXTRACE) = 0;
|
2013-07-21 20:47:24 +02:00
|
|
|
shf_puts(substitute(str_val(global("PS4")), 0), shl_xtrace);
|
2015-03-01 16:23:05 +01:00
|
|
|
Flag(FXTRACE) = 2;
|
|
|
|
in_xtrace = false;
|
|
|
|
}
|
2013-07-21 20:47:24 +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
|
|
|
{
|
2013-11-17 23:22:56 +01:00
|
|
|
static const char cmd_opts[] =
|
|
|
|
#define SHFLAGS_NOT_SET
|
|
|
|
#define SHFLAGS_OPTCS
|
|
|
|
#include "sh_flags.gen"
|
|
|
|
#undef SHFLAGS_NOT_SET
|
|
|
|
;
|
|
|
|
static const char set_opts[] =
|
|
|
|
#define SHFLAGS_NOT_CMD
|
|
|
|
#define SHFLAGS_OPTCS
|
|
|
|
#include "sh_flags.gen"
|
|
|
|
#undef SHFLAGS_NOT_CMD
|
|
|
|
;
|
2012-12-05 20:38:25 +01:00
|
|
|
bool set;
|
2013-11-17 23:22:56 +01:00
|
|
|
const char *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;
|
2013-05-02 22:21:45 +02:00
|
|
|
bool fcompatseen = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
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) {
|
2012-12-05 20:38:25 +01:00
|
|
|
set = tobool(!(go.info & GI_PLUS));
|
2005-05-23 05:06:10 +02:00
|
|
|
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);
|
2013-05-02 22:21:45 +02:00
|
|
|
if ((i == FPOSIX || i == FSH) && set && !fcompatseen) {
|
|
|
|
/*
|
|
|
|
* If running 'set -o posix' or
|
|
|
|
* 'set -o sh', turn off the other;
|
|
|
|
* if running 'set -o posix -o sh'
|
|
|
|
* allow both to be set though.
|
|
|
|
*/
|
|
|
|
Flag(FPOSIX) = 0;
|
|
|
|
Flag(FSH) = 0;
|
|
|
|
fcompatseen = true;
|
|
|
|
}
|
2012-12-05 20:38:25 +01:00
|
|
|
if ((i != (size_t)-1) && (set ? 1U : 0U) == 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.
|
|
|
|
*/
|
|
|
|
;
|
2013-08-11 16:57:11 +02:00
|
|
|
else if ((i != (size_t)-1) && (OFF(i) & what))
|
2007-05-13 20:33:29 +02:00
|
|
|
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;
|
|
|
|
|
2012-06-26 21:22:19 +02:00
|
|
|
#ifdef KSH_CHVT_FLAG
|
2007-01-26 19:27:34 +01:00
|
|
|
case 'T':
|
2005-05-23 05:06:10 +02:00
|
|
|
if (what != OF_FIRSTTIME)
|
|
|
|
break;
|
2012-06-26 21:22:19 +02:00
|
|
|
#ifndef KSH_CHVT_CODE
|
2006-11-10 03:10:45 +01:00
|
|
|
errorf("no TIOCSCTTY ioctl");
|
2006-11-10 02:13:52 +01:00
|
|
|
#else
|
2012-12-05 20:38:25 +01:00
|
|
|
change_flag(FTALKING, OF_CMDLINE, true);
|
2013-06-04 00:28:06 +02:00
|
|
|
chvt(&go);
|
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++)
|
2013-08-11 16:57:11 +02:00
|
|
|
if (optc == OFC(i) &&
|
|
|
|
(what & OFF(i))) {
|
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 */
|
2013-07-21 20:47:24 +02:00
|
|
|
if (argv[go.optind][0] == '-') {
|
|
|
|
Flag(FVERBOSE) = 0;
|
|
|
|
change_xtrace(0, false);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
/* 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
|
|
|
{
|
2012-12-04 02:12:11 +01:00
|
|
|
char c;
|
2013-04-26 21:10:58 +02:00
|
|
|
mksh_ari_u num;
|
2006-11-10 03:05:06 +01:00
|
|
|
bool neg = false;
|
|
|
|
|
2013-04-26 21:10:58 +02:00
|
|
|
num.u = 0;
|
|
|
|
|
2006-11-10 03:05:06 +01:00
|
|
|
do {
|
|
|
|
c = *s++;
|
2006-11-10 08:18:58 +01:00
|
|
|
} while (ksh_isspace(c));
|
2012-12-04 02:12:11 +01:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case '-':
|
2006-11-10 03:05:06 +01:00
|
|
|
neg = true;
|
2012-12-04 02:12:11 +01:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case '+':
|
2006-11-10 03:05:06 +01:00
|
|
|
c = *s++;
|
2012-12-04 02:12:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-11-10 03:05:06 +01:00
|
|
|
do {
|
2006-11-10 07:53:27 +01:00
|
|
|
if (!ksh_isdigit(c))
|
2012-12-04 02:12:11 +01:00
|
|
|
/* not numeric */
|
|
|
|
return (0);
|
2013-04-26 21:10:58 +02:00
|
|
|
if (num.u > 214748364U)
|
2012-12-04 02:12:11 +01:00
|
|
|
/* overflow on multiplication */
|
|
|
|
return (0);
|
2015-04-29 22:07:35 +02:00
|
|
|
num.u = num.u * 10U + (unsigned int)ksh_numdig(c);
|
2013-04-26 21:10:58 +02:00
|
|
|
/* now: num.u <= 2147483649U */
|
2006-11-10 03:05:06 +01:00
|
|
|
} while ((c = *s++));
|
|
|
|
|
2013-04-26 21:10:58 +02:00
|
|
|
if (num.u > (neg ? 2147483648U : 2147483647U))
|
2012-12-04 02:12:11 +01:00
|
|
|
/* overflow for signed 32-bit int */
|
|
|
|
return (0);
|
|
|
|
|
2013-04-26 21:10:58 +02:00
|
|
|
if (neg)
|
|
|
|
num.u = -num.u;
|
|
|
|
*ai = num.i;
|
2012-12-04 02:12:11 +01:00
|
|
|
return (1);
|
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)
|
|
|
|
{
|
2012-12-05 20:38:25 +01:00
|
|
|
unsigned char sc, pc;
|
2005-05-23 05:06:10 +02:00
|
|
|
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 '[':
|
2015-04-29 20:38:54 +02:00
|
|
|
if (sc == 0 || (p = gmatch_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 *
|
2015-04-29 20:38:54 +02:00
|
|
|
gmatch_cclass(const unsigned char *p, unsigned char sub)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2012-12-05 20:38:25 +01:00
|
|
|
unsigned char c, d;
|
|
|
|
bool notp, found = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
const unsigned char *orig_p = p;
|
|
|
|
|
2012-12-05 20:38:25 +01:00
|
|
|
if ((notp = tobool(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))
|
2012-12-05 20:38:25 +01:00
|
|
|
found = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
} 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;
|
2015-04-29 20:32:44 +02:00
|
|
|
if (flag == '-' && ksh_isdash(arg + 1)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
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 '\'':
|
2012-07-20 23:18:45 +02:00
|
|
|
shf_fprintf(shf, "\\%03o", 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
|
2012-10-03 17:13:34 +02:00
|
|
|
print_columns(struct shf *shf, unsigned int n,
|
2015-07-05 16:43:08 +02:00
|
|
|
void (*func)(char *, size_t, unsigned 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
|
|
|
{
|
2012-10-03 17:13:34 +02:00
|
|
|
unsigned int i, r, c, rows, cols, nspace, max_col;
|
2009-11-10 00:35:11 +01:00
|
|
|
char *str;
|
|
|
|
|
2012-10-03 17:13:34 +02:00
|
|
|
if (!n)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (max_colz > 2147483646) {
|
2009-11-10 00:35:11 +01:00
|
|
|
#ifndef MKSH_SMALL
|
2012-10-03 17:13:34 +02:00
|
|
|
internal_warningf("print_columns called with %s=%zu >= INT_MAX",
|
|
|
|
"max_col", max_colz);
|
2009-11-10 00:35:11 +01:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2012-10-03 17:13:34 +02:00
|
|
|
max_col = (unsigned int)max_colz;
|
2009-11-10 00:35:11 +01:00
|
|
|
|
2012-10-03 17:13:34 +02:00
|
|
|
if (max_oct > 2147483646) {
|
2011-08-27 20:06:52 +02:00
|
|
|
#ifndef MKSH_SMALL
|
2012-10-03 17:13:34 +02:00
|
|
|
internal_warningf("print_columns called with %s=%zu >= INT_MAX",
|
|
|
|
"max_oct", max_oct);
|
2011-08-27 20:06:52 +02:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
/*
|
2015-07-05 16:58:33 +02:00
|
|
|
* We use (max_col + 2) to consider the separator space.
|
|
|
|
* Note that no spaces are printed after the last column
|
|
|
|
* to avoid problems with terminals that have auto-wrap,
|
|
|
|
* but we need to also take this into account in x_cols.
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2015-07-05 16:58:33 +02:00
|
|
|
cols = (x_cols + 1) / (max_col + 2);
|
2009-11-10 00:35:11 +01:00
|
|
|
|
2009-02-22 19:02:31 +01:00
|
|
|
/* if we can only print one column anyway, skip the goo */
|
|
|
|
if (cols < 2) {
|
2015-07-05 16:43:08 +02:00
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
(*func)(str, max_oct, i, arg);
|
|
|
|
shf_puts(str, shf);
|
|
|
|
shf_putc('\n', shf);
|
|
|
|
}
|
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) {
|
2013-02-24 15:22:44 +01:00
|
|
|
cols = rows;
|
|
|
|
rows = (n + cols - 1) / cols;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2013-04-27 21:09:13 +02:00
|
|
|
nspace = (x_cols - max_col * cols) / cols;
|
2015-07-05 16:58:33 +02:00
|
|
|
if (nspace < 2)
|
|
|
|
nspace = 2;
|
2009-11-10 00:35:11 +01:00
|
|
|
max_col = -max_col;
|
2005-05-23 05:06:10 +02:00
|
|
|
for (r = 0; r < rows; r++) {
|
|
|
|
for (c = 0; c < cols; c++) {
|
2015-07-05 16:47:41 +02:00
|
|
|
if ((i = c * rows + r) >= n)
|
|
|
|
break;
|
|
|
|
(*func)(str, max_oct, i, arg);
|
|
|
|
if (i + rows >= n)
|
|
|
|
shf_puts(str, shf);
|
|
|
|
else
|
|
|
|
shf_fprintf(shf, "%*s%*s",
|
|
|
|
max_col, str, nspace, null);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
shf_putchar('\n', shf);
|
|
|
|
}
|
2009-02-22 19:02:31 +01:00
|
|
|
out:
|
2005-05-23 05:06:10 +02:00
|
|
|
afree(str, ATEMP);
|
|
|
|
}
|
|
|
|
|
2015-03-20 22:46:41 +01:00
|
|
|
/* strip all NUL bytes from buf; output is NUL-terminated if stripped */
|
2006-11-10 04:45:57 +01:00
|
|
|
void
|
2015-03-20 22:46:41 +01:00
|
|
|
strip_nuls(char *buf, size_t len)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2015-03-20 22:46:41 +01:00
|
|
|
char *cp, *dp, *ep;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2015-03-20 22:46:41 +01:00
|
|
|
if (!len || !(dp = memchr(buf, '\0', len)))
|
|
|
|
return;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2015-03-20 22:46:41 +01:00
|
|
|
ep = buf + len;
|
|
|
|
cp = dp;
|
|
|
|
|
|
|
|
cp_has_nul_byte:
|
|
|
|
while (cp++ < ep && *cp == '\0')
|
|
|
|
; /* nothing */
|
|
|
|
while (cp < ep && *cp != '\0')
|
|
|
|
*dp++ = *cp++;
|
|
|
|
if (cp < ep)
|
|
|
|
goto cp_has_nul_byte;
|
|
|
|
|
|
|
|
*dp = '\0';
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-05-05 19:37:44 +02:00
|
|
|
#ifdef MKSH__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;
|
2014-11-25 21:00:39 +01:00
|
|
|
size_t pos, len;
|
2011-03-24 20:05:49 +01:00
|
|
|
int llen;
|
|
|
|
struct stat sb;
|
2012-05-05 19:37:44 +02:00
|
|
|
#ifdef MKSH__NO_PATH_MAX
|
2011-03-24 20:05:49 +01:00
|
|
|
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;
|
|
|
|
|
2015-07-10 21:36:38 +02:00
|
|
|
if (mksh_abspath(upath)) {
|
2011-03-24 20:05:49 +01:00
|
|
|
/* upath is an absolute pathname */
|
|
|
|
strdupx(ipath, upath, ATEMP);
|
|
|
|
} else {
|
|
|
|
/* upath is a relative pathname, prepend cwd */
|
2015-07-10 21:36:38 +02:00
|
|
|
if ((tp = ksh_get_wd()) == NULL || !mksh_abspath(tp))
|
2011-03-24 20:05:49 +01:00
|
|
|
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 */
|
2012-05-05 00:05:02 +02:00
|
|
|
if (mksh_lstat(Xstring(xs, xp), &sb)) {
|
2011-03-24 20:05:49 +01:00
|
|
|
/* 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)) {
|
2012-05-05 17:10:39 +02:00
|
|
|
#ifndef MKSH__NO_SYMLINK
|
2011-03-24 20:05:49 +01:00
|
|
|
/* reached maximum recursion depth? */
|
|
|
|
if (!symlinks--) {
|
|
|
|
/* yep, prevent infinite loops */
|
|
|
|
errno = ELOOP;
|
|
|
|
goto notfound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get symlink(7) target */
|
|
|
|
if (pathcnd) {
|
2012-05-05 19:37:44 +02:00
|
|
|
#ifdef MKSH__NO_PATH_MAX
|
2011-03-24 20:05:49 +01:00
|
|
|
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;
|
2015-07-10 21:36:38 +02:00
|
|
|
if (!mksh_abspath(ldest)) {
|
2011-03-24 20:05:49 +01:00
|
|
|
/* symlink target is a relative path */
|
|
|
|
xp = Xrestpos(xs, xp, pos);
|
2012-05-05 17:10:39 +02:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2011-03-24 20:05:49 +01:00
|
|
|
/* 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 */
|
2015-09-05 21:19:12 +02:00
|
|
|
afree(ldest, ATEMP);
|
2011-03-24 20:05:49 +01:00
|
|
|
afree(ipath, ATEMP);
|
|
|
|
return (Xclose(xs, xp));
|
|
|
|
|
|
|
|
notfound:
|
|
|
|
/* save; freeing memory might trash it */
|
|
|
|
llen = errno;
|
2015-09-05 21:19:12 +02:00
|
|
|
afree(ldest, ATEMP);
|
2011-03-24 20:05:49 +01:00
|
|
|
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;
|
|
|
|
|
2015-07-10 21:36:38 +02:00
|
|
|
if (mksh_abspath(file)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
*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) {
|
2015-07-10 21:36:38 +02:00
|
|
|
char *pend = plist;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2015-07-10 21:36:38 +02:00
|
|
|
while (*pend && *pend != MKSH_PATHSEPC)
|
|
|
|
++pend;
|
2005-05-23 05:06:10 +02:00
|
|
|
plen = pend - plist;
|
2008-10-14 01:06:04 +02:00
|
|
|
*cdpathp = *pend ? pend + 1 : NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 21:36:38 +02:00
|
|
|
if ((!use_cdpath || !plen || !mksh_abspath(plist)) &&
|
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? */
|
2015-07-10 21:36:38 +02:00
|
|
|
if (mksh_abspath(p))
|
2011-03-26 22:09:09 +01:00
|
|
|
/* 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) {
|
2016-01-21 19:24:45 +01:00
|
|
|
bi_errorf(Tbadsubst);
|
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
|
|
|
}
|
|
|
|
|
2012-05-05 19:37:44 +02:00
|
|
|
#ifdef MKSH__NO_PATH_MAX
|
2011-03-24 20:05:49 +01:00
|
|
|
/* 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
|
2012-12-18 00:18:11 +01:00
|
|
|
bi_errorf("%s: %s", tryp, cstrerror(errno));
|
2011-03-24 20:05:49 +01:00
|
|
|
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);
|
|
|
|
|
2015-07-10 21:36:38 +02:00
|
|
|
if (!mksh_abspath(Xstring(xs, xp))) {
|
2011-03-24 20:05:49 +01:00
|
|
|
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
|
|
|
|
2012-06-26 21:22:19 +02:00
|
|
|
#ifdef KSH_CHVT_CODE
|
2009-10-30 15:37:43 +01:00
|
|
|
extern void chvt_reinit(void);
|
|
|
|
|
2006-11-10 02:13:52 +01:00
|
|
|
static void
|
2013-06-04 00:28:06 +02:00
|
|
|
chvt(const Getopt *go)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2013-06-04 00:28:06 +02:00
|
|
|
const char *dv = go->optarg;
|
|
|
|
char *cp = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
int fd;
|
|
|
|
|
2013-06-04 00:28:06 +02:00
|
|
|
switch (*dv) {
|
|
|
|
case '-':
|
|
|
|
dv = "/dev/null";
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
++dv;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default: {
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(dv, &sb)) {
|
|
|
|
cp = shf_smprintf("/dev/ttyC%s", dv);
|
|
|
|
dv = cp;
|
2008-03-23 23:09:59 +01:00
|
|
|
if (stat(dv, &sb)) {
|
2013-06-04 00:28:06 +02:00
|
|
|
memmove(cp + 1, cp, /* /dev/tty */ 8);
|
|
|
|
dv = cp + 1;
|
|
|
|
if (stat(dv, &sb)) {
|
|
|
|
errorf("%s: %s: %s", "chvt",
|
|
|
|
"can't find tty", go->optarg);
|
|
|
|
}
|
2008-03-23 23:09:59 +01:00
|
|
|
}
|
2006-11-10 02:13:52 +01:00
|
|
|
}
|
2008-03-23 23:09:59 +01:00
|
|
|
if (!(sb.st_mode & S_IFCHR))
|
2013-06-04 00:28:06 +02:00
|
|
|
errorf("%s: %s: %s", "chvt", "not a char device", dv);
|
|
|
|
#ifndef MKSH_DISABLE_REVOKE_WARNING
|
2007-01-18 02:01:25 +01:00
|
|
|
#if HAVE_REVOKE
|
2013-06-04 00:28:06 +02:00
|
|
|
if (revoke(dv))
|
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",
|
2013-06-04 00:28:06 +02:00
|
|
|
dv);
|
|
|
|
#endif
|
|
|
|
}
|
2008-03-23 23:09:59 +01:00
|
|
|
}
|
2015-07-09 22:52:43 +02:00
|
|
|
if ((fd = binopen2(dv, O_RDWR)) < 0) {
|
2005-05-23 05:06:10 +02:00
|
|
|
sleep(1);
|
2015-07-09 22:52:43 +02:00
|
|
|
if ((fd = binopen2(dv, O_RDWR)) < 0) {
|
2013-06-04 00:28:06 +02:00
|
|
|
errorf("%s: %s %s", "chvt", "can't open", dv);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2013-06-04 00:28:06 +02:00
|
|
|
if (go->optarg[0] != '!') {
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
errorf("%s: %s %s", "chvt", "fork", "failed");
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
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");
|
2013-06-04 00:28:06 +02:00
|
|
|
if (go->optarg[0] != '-') {
|
2008-04-11 21:55:24 +02:00
|
|
|
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);
|
2013-06-04 00:28:06 +02:00
|
|
|
rndset((unsigned long)chvt_rndsetup(go, sizeof(Getopt)));
|
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
|
|
|
|
|
2012-07-01 17:55:00 +02:00
|
|
|
#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
|
2008-10-28 15:51:06 +01:00
|
|
|
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--)
|
2015-04-29 22:07:35 +02:00
|
|
|
if ((c = (*fg)()) >= ord('0') && c <= ord('7'))
|
|
|
|
wc = (wc << 3) + ksh_numdig(c);
|
2009-09-19 23:54:46 +02:00
|
|
|
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)
|
2015-04-29 22:07:35 +02:00
|
|
|
/* FALLTHROUGH */
|
2009-09-19 17:16:05 +02:00
|
|
|
case 'u':
|
2015-04-29 22:07:35 +02:00
|
|
|
i = 4;
|
2011-03-13 02:20:25 +01:00
|
|
|
if (/* CONSTCOND */ 0)
|
2015-04-29 22:07:35 +02:00
|
|
|
/* FALLTHROUGH */
|
2009-09-19 17:16:05 +02:00
|
|
|
case 'x':
|
2015-04-29 22:07:35 +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;
|
2015-04-29 22:07:35 +02:00
|
|
|
if ((c = (*fg)()) >= ord('0') && c <= ord('9'))
|
|
|
|
wc += ksh_numdig(c);
|
|
|
|
else if (c >= ord('A') && c <= ord('F'))
|
|
|
|
wc += ksh_numuc(c) + 10;
|
|
|
|
else if (c >= ord('a') && c <= ord('f'))
|
|
|
|
wc += ksh_numlc(c) + 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);
|
|
|
|
}
|