2015-10-09 18:11:19 +02:00
|
|
|
|
/* $OpenBSD: c_ksh.c,v 1.37 2015/09/10 22:48:58 nicm Exp $ */
|
2015-08-13 23:04:12 +02:00
|
|
|
|
/* $OpenBSD: c_sh.c,v 1.46 2015/07/20 20:46:24 guenther Exp $ */
|
2009-03-22 18:58:58 +01:00
|
|
|
|
/* $OpenBSD: c_test.c,v 1.18 2009/03/01 20:11:06 otto Exp $ */
|
2013-11-30 18:33:51 +01:00
|
|
|
|
/* $OpenBSD: c_ulimit.c,v 1.19 2013/11/28 10:33:37 sobrado Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
|
/*-
|
2011-01-09 22:57:29 +01:00
|
|
|
|
* Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
|
2016-01-13 18:20:52 +01:00
|
|
|
|
* 2010, 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"
|
|
|
|
|
|
2011-02-11 01:41:38 +01:00
|
|
|
|
#if HAVE_SELECT
|
|
|
|
|
#if HAVE_SYS_BSDTYPES_H
|
|
|
|
|
#include <sys/bsdtypes.h>
|
|
|
|
|
#endif
|
|
|
|
|
#if HAVE_SYS_SELECT_H
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
#endif
|
|
|
|
|
#if HAVE_BSTRING_H
|
|
|
|
|
#include <bstring.h>
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-01-20 22:34:13 +01:00
|
|
|
|
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.293 2016/01/20 21:34:11 tg Exp $");
|
2009-06-08 22:34:40 +02:00
|
|
|
|
|
|
|
|
|
#if HAVE_KILLPG
|
|
|
|
|
/*
|
|
|
|
|
* use killpg if < -1 since -1 does special things
|
|
|
|
|
* for some non-killpg-endowed kills
|
|
|
|
|
*/
|
|
|
|
|
#define mksh_kill(p,s) ((p) < -1 ? killpg(-(p), (s)) : kill((p), (s)))
|
|
|
|
|
#else
|
|
|
|
|
/* cross fingers and hope kill is killpg-endowed */
|
|
|
|
|
#define mksh_kill kill
|
|
|
|
|
#endif
|
2007-08-20 00:06:27 +02:00
|
|
|
|
|
2009-07-25 23:31:27 +02:00
|
|
|
|
/* XXX conditions correct? */
|
2009-07-25 22:35:22 +02:00
|
|
|
|
#if !defined(RLIM_INFINITY) && !defined(MKSH_NO_LIMITS)
|
2011-08-27 19:30:07 +02:00
|
|
|
|
#define MKSH_NO_LIMITS 1
|
2009-06-08 22:52:28 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
2009-06-10 20:11:27 +02:00
|
|
|
|
#ifdef MKSH_NO_LIMITS
|
2011-08-27 19:30:07 +02:00
|
|
|
|
#define c_ulimit c_true
|
2009-06-10 20:11:27 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
2013-11-30 18:41:35 +01:00
|
|
|
|
#if !defined(MKSH_UNEMPLOYED) && HAVE_GETSID
|
2013-09-10 19:33:04 +02:00
|
|
|
|
static int c_suspend(const char **);
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-05-04 22:08:25 +02:00
|
|
|
|
/* getn() that prints error */
|
|
|
|
|
static int
|
|
|
|
|
bi_getn(const char *as, int *ai)
|
|
|
|
|
{
|
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
|
|
if (!(rv = getn(as, ai)))
|
|
|
|
|
bi_errorf("%s: %s", as, "bad number");
|
|
|
|
|
return (rv);
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-21 01:47:29 +02:00
|
|
|
|
static int
|
|
|
|
|
c_true(const char **wp MKSH_A_UNUSED)
|
|
|
|
|
{
|
|
|
|
|
return (0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
c_false(const char **wp MKSH_A_UNUSED)
|
|
|
|
|
{
|
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*
|
2013-04-26 23:22:50 +02:00
|
|
|
|
* A leading = means assignments before command are kept.
|
|
|
|
|
* A leading * means a POSIX special builtin.
|
2007-08-20 00:06:27 +02:00
|
|
|
|
*/
|
|
|
|
|
const struct builtin mkshbuiltins[] = {
|
|
|
|
|
{"*=.", c_dot},
|
2011-07-21 01:47:29 +02:00
|
|
|
|
{"*=:", c_true},
|
2007-08-20 00:06:27 +02:00
|
|
|
|
{"[", c_test},
|
2013-04-26 23:22:50 +02:00
|
|
|
|
/* no =: AT&T manual wrong */
|
|
|
|
|
{Talias, c_alias},
|
2007-08-20 00:06:27 +02:00
|
|
|
|
{"*=break", c_brkcont},
|
2011-09-07 17:24:22 +02:00
|
|
|
|
{Tgbuiltin, c_builtin},
|
2015-07-06 19:48:37 +02:00
|
|
|
|
{Tcat, c_cat},
|
2013-04-26 23:22:50 +02:00
|
|
|
|
{"cd", c_cd},
|
|
|
|
|
/* dash compatibility hack */
|
|
|
|
|
{"chdir", c_cd},
|
2016-01-20 00:12:15 +01:00
|
|
|
|
{Tcommand, c_command},
|
2007-08-20 00:06:27 +02:00
|
|
|
|
{"*=continue", c_brkcont},
|
2013-04-26 23:22:50 +02:00
|
|
|
|
{"echo", c_print},
|
2007-08-20 00:06:27 +02:00
|
|
|
|
{"*=eval", c_eval},
|
|
|
|
|
{"*=exec", c_exec},
|
|
|
|
|
{"*=exit", c_exitreturn},
|
2012-05-10 01:21:00 +02:00
|
|
|
|
{Tsgexport, c_typeset},
|
2013-04-26 23:22:50 +02:00
|
|
|
|
{"false", c_false},
|
|
|
|
|
{"fc", c_fc},
|
|
|
|
|
{"getopts", c_getopts},
|
2011-07-02 19:57:24 +02:00
|
|
|
|
{"=global", c_typeset},
|
2013-04-26 23:22:50 +02:00
|
|
|
|
{"jobs", c_jobs},
|
|
|
|
|
{"kill", c_kill},
|
2007-08-20 00:06:27 +02:00
|
|
|
|
{"let", c_let},
|
2013-04-26 23:22:50 +02:00
|
|
|
|
{"let]", c_let},
|
2007-08-20 00:06:27 +02:00
|
|
|
|
{"print", c_print},
|
|
|
|
|
{"pwd", c_pwd},
|
2013-04-26 23:22:50 +02:00
|
|
|
|
{"read", c_read},
|
2012-05-10 01:21:00 +02:00
|
|
|
|
{Tsgreadonly, c_typeset},
|
2013-04-26 23:22:50 +02:00
|
|
|
|
{"realpath", c_realpath},
|
|
|
|
|
{"rename", c_rename},
|
|
|
|
|
{"*=return", c_exitreturn},
|
|
|
|
|
{Tsgset, c_set},
|
|
|
|
|
{"*=shift", c_shift},
|
2015-10-09 23:36:59 +02:00
|
|
|
|
{"=source", c_dot},
|
2013-11-30 18:41:35 +01:00
|
|
|
|
#if !defined(MKSH_UNEMPLOYED) && HAVE_GETSID
|
2013-09-10 19:33:04 +02:00
|
|
|
|
{"suspend", c_suspend},
|
|
|
|
|
#endif
|
2013-04-26 23:22:50 +02:00
|
|
|
|
{"test", c_test},
|
|
|
|
|
{"*=times", c_times},
|
|
|
|
|
{"*=trap", c_trap},
|
|
|
|
|
{"true", c_true},
|
2011-09-07 17:24:22 +02:00
|
|
|
|
{T_typeset, c_typeset},
|
2013-04-26 23:22:50 +02:00
|
|
|
|
{"ulimit", c_ulimit},
|
|
|
|
|
{"umask", c_umask},
|
|
|
|
|
{Tunalias, c_unalias},
|
2015-03-08 22:30:22 +01:00
|
|
|
|
{"*=unset", c_unset},
|
2013-04-26 23:22:50 +02:00
|
|
|
|
{"=wait", c_wait},
|
2007-08-20 00:06:27 +02:00
|
|
|
|
{"whence", c_whence},
|
2009-04-05 14:35:32 +02:00
|
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2013-04-26 23:22:50 +02:00
|
|
|
|
{"bg", c_fgbg},
|
|
|
|
|
{"fg", c_fgbg},
|
2009-04-05 14:35:32 +02:00
|
|
|
|
#endif
|
2012-05-04 22:49:08 +02:00
|
|
|
|
#ifndef MKSH_NO_CMDLINE_EDITING
|
2007-08-20 00:06:27 +02:00
|
|
|
|
{"bind", c_bind},
|
2012-05-04 22:49:08 +02:00
|
|
|
|
#endif
|
2007-08-20 00:06:27 +02:00
|
|
|
|
#if HAVE_MKNOD
|
|
|
|
|
{"mknod", c_mknod},
|
2008-05-17 20:27:57 +02:00
|
|
|
|
#endif
|
2013-04-26 23:22:50 +02:00
|
|
|
|
#ifdef MKSH_PRINTF_BUILTIN
|
2015-07-09 22:20:45 +02:00
|
|
|
|
{Tprintf, c_printf},
|
2013-04-26 23:22:50 +02:00
|
|
|
|
#endif
|
2011-02-11 01:41:38 +01:00
|
|
|
|
#if HAVE_SELECT
|
|
|
|
|
{"sleep", c_sleep},
|
• 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
|
|
|
|
#endif
|
|
|
|
|
#ifdef __MirBSD__
|
|
|
|
|
/* alias to "true" for historical reasons */
|
2011-07-21 01:47:29 +02:00
|
|
|
|
{"domainname", c_true},
|
2015-07-09 21:46:43 +02:00
|
|
|
|
#endif
|
|
|
|
|
#ifdef __OS2__
|
|
|
|
|
{Textproc, c_true},
|
2011-02-11 01:41:38 +01:00
|
|
|
|
#endif
|
2007-08-20 00:06:27 +02:00
|
|
|
|
{NULL, (int (*)(const char **))NULL}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct kill_info {
|
|
|
|
|
int num_width;
|
|
|
|
|
int name_width;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct t_op {
|
2009-04-07 21:51:47 +02:00
|
|
|
|
char op_text[4];
|
|
|
|
|
Test_op op_num;
|
2007-08-20 00:06:27 +02:00
|
|
|
|
} u_ops[] = {
|
|
|
|
|
{"-a", TO_FILAXST },
|
|
|
|
|
{"-b", TO_FILBDEV },
|
|
|
|
|
{"-c", TO_FILCDEV },
|
|
|
|
|
{"-d", TO_FILID },
|
|
|
|
|
{"-e", TO_FILEXST },
|
|
|
|
|
{"-f", TO_FILREG },
|
|
|
|
|
{"-G", TO_FILGID },
|
|
|
|
|
{"-g", TO_FILSETG },
|
|
|
|
|
{"-h", TO_FILSYM },
|
|
|
|
|
{"-H", TO_FILCDF },
|
|
|
|
|
{"-k", TO_FILSTCK },
|
|
|
|
|
{"-L", TO_FILSYM },
|
|
|
|
|
{"-n", TO_STNZE },
|
|
|
|
|
{"-O", TO_FILUID },
|
|
|
|
|
{"-o", TO_OPTION },
|
|
|
|
|
{"-p", TO_FILFIFO },
|
|
|
|
|
{"-r", TO_FILRD },
|
|
|
|
|
{"-s", TO_FILGZ },
|
|
|
|
|
{"-S", TO_FILSOCK },
|
|
|
|
|
{"-t", TO_FILTT },
|
|
|
|
|
{"-u", TO_FILSETU },
|
|
|
|
|
{"-w", TO_FILWR },
|
|
|
|
|
{"-x", TO_FILEX },
|
|
|
|
|
{"-z", TO_STZER },
|
|
|
|
|
{"", TO_NONOP }
|
|
|
|
|
};
|
|
|
|
|
static const struct t_op b_ops[] = {
|
|
|
|
|
{"=", TO_STEQL },
|
|
|
|
|
{"==", TO_STEQL },
|
|
|
|
|
{"!=", TO_STNEQ },
|
|
|
|
|
{"<", TO_STLT },
|
|
|
|
|
{">", TO_STGT },
|
|
|
|
|
{"-eq", TO_INTEQ },
|
|
|
|
|
{"-ne", TO_INTNE },
|
|
|
|
|
{"-gt", TO_INTGT },
|
|
|
|
|
{"-ge", TO_INTGE },
|
|
|
|
|
{"-lt", TO_INTLT },
|
|
|
|
|
{"-le", TO_INTLE },
|
|
|
|
|
{"-ef", TO_FILEQ },
|
|
|
|
|
{"-nt", TO_FILNT },
|
|
|
|
|
{"-ot", TO_FILOT },
|
|
|
|
|
{"", TO_NONOP }
|
|
|
|
|
};
|
|
|
|
|
|
2008-04-02 00:20:20 +02:00
|
|
|
|
static int test_oexpr(Test_env *, bool);
|
|
|
|
|
static int test_aexpr(Test_env *, bool);
|
|
|
|
|
static int test_nexpr(Test_env *, bool);
|
|
|
|
|
static int test_primary(Test_env *, bool);
|
2010-07-18 00:09:40 +02:00
|
|
|
|
static Test_op ptest_isa(Test_env *, Test_meta);
|
2008-04-02 00:20:20 +02:00
|
|
|
|
static const char *ptest_getopnd(Test_env *, Test_op, bool);
|
2007-08-20 00:06:27 +02:00
|
|
|
|
static void ptest_error(Test_env *, int, const char *);
|
2015-07-05 16:43:08 +02:00
|
|
|
|
static void kill_fmt_entry(char *, size_t, unsigned int, const void *);
|
2009-05-20 12:10:02 +02:00
|
|
|
|
static void p_time(struct shf *, bool, long, int, int,
|
2012-12-01 02:36:30 +01:00
|
|
|
|
const char *, const char *);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_pwd(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
int optc;
|
2011-03-07 21:30:41 +01:00
|
|
|
|
bool physical = tobool(Flag(FPHYSICAL));
|
2008-05-17 20:47:03 +02:00
|
|
|
|
char *p, *allocd = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2006-05-10 20:54:13 +02:00
|
|
|
|
while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != -1)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
switch (optc) {
|
|
|
|
|
case 'L':
|
2008-04-01 23:50:58 +02:00
|
|
|
|
physical = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'P':
|
2008-04-01 23:50:58 +02:00
|
|
|
|
physical = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case '?':
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
|
|
|
|
|
if (wp[0]) {
|
|
|
|
|
bi_errorf("too many arguments");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2009-08-30 15:30:08 +02:00
|
|
|
|
p = current_wd[0] ? (physical ? allocd = do_realpath(current_wd) :
|
2008-05-17 20:47:03 +02:00
|
|
|
|
current_wd) : NULL;
|
2011-09-07 17:24:22 +02:00
|
|
|
|
/* LINTED use of access */
|
2006-11-10 07:16:25 +01:00
|
|
|
|
if (p && access(p, R_OK) < 0)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
p = NULL;
|
2011-03-24 20:05:49 +01:00
|
|
|
|
if (!p && !(p = allocd = ksh_get_wd())) {
|
2010-08-28 22:22:24 +02:00
|
|
|
|
bi_errorf("%s: %s", "can't determine current directory",
|
2012-12-18 00:18:11 +01:00
|
|
|
|
cstrerror(errno));
|
2008-05-17 20:47:03 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
shprintf("%s\n", p);
|
2008-05-17 20:47:03 +02:00
|
|
|
|
afree(allocd, ATEMP);
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-09-19 23:54:46 +02:00
|
|
|
|
static const char *s_ptr;
|
|
|
|
|
static int s_get(void);
|
|
|
|
|
static void s_put(int);
|
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_print(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2009-10-10 23:17:31 +02:00
|
|
|
|
int fd = 1, c;
|
2016-01-20 22:34:13 +01:00
|
|
|
|
const char *s;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
XString xs;
|
|
|
|
|
char *xp;
|
2016-01-20 22:34:13 +01:00
|
|
|
|
/* print newline; expand backslash sequences */
|
|
|
|
|
bool po_nl = true, po_exp = true;
|
|
|
|
|
/* print to history instead of file descriptor / stdout */
|
|
|
|
|
bool po_hist = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2009-10-10 23:17:31 +02:00
|
|
|
|
if (wp[0][0] == 'e') {
|
2016-01-20 22:34:13 +01:00
|
|
|
|
/* "echo" builtin */
|
|
|
|
|
++wp;
|
2013-02-18 23:47:32 +01:00
|
|
|
|
#ifdef MKSH_MIDNIGHTBSD01ASH_COMPAT
|
|
|
|
|
if (Flag(FSH)) {
|
|
|
|
|
/*
|
|
|
|
|
* MidnightBSD /bin/sh needs a BSD echo, that is,
|
|
|
|
|
* one that supports -e but does not enable it by
|
|
|
|
|
* default
|
|
|
|
|
*/
|
2016-01-20 22:34:13 +01:00
|
|
|
|
po_exp = false;
|
2013-02-18 23:47:32 +01:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (Flag(FPOSIX) ||
|
|
|
|
|
#ifndef MKSH_MIDNIGHTBSD01ASH_COMPAT
|
|
|
|
|
Flag(FSH) ||
|
|
|
|
|
#endif
|
|
|
|
|
Flag(FAS_BUILTIN)) {
|
2009-10-10 23:17:31 +02:00
|
|
|
|
/* Debian Policy 10.4 compliant "echo" builtin */
|
|
|
|
|
if (*wp && !strcmp(*wp, "-n")) {
|
2016-01-20 22:34:13 +01:00
|
|
|
|
/* recognise "-n" only as the first arg */
|
|
|
|
|
po_nl = false;
|
|
|
|
|
++wp;
|
|
|
|
|
}
|
|
|
|
|
/* print everything as-is */
|
|
|
|
|
po_exp = false;
|
2009-10-10 23:17:31 +02:00
|
|
|
|
} else {
|
2016-01-20 22:34:13 +01:00
|
|
|
|
bool new_exp = po_exp, new_nl = po_nl;
|
2009-10-10 23:17:31 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* a compromise between sysV and BSD echo commands:
|
|
|
|
|
* escape sequences are enabled by default, and -n,
|
|
|
|
|
* -e and -E are recognised if they appear in argu-
|
|
|
|
|
* ments with no illegal options (ie, echo -nq will
|
|
|
|
|
* print -nq).
|
|
|
|
|
* Different from sysV echo since options are reco-
|
|
|
|
|
* gnised, different from BSD echo since escape se-
|
|
|
|
|
* quences are enabled by default.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-01-20 22:34:13 +01:00
|
|
|
|
print_tradparse_arg:
|
|
|
|
|
if ((s = *wp) && *s++ == '-' && *s) {
|
|
|
|
|
print_tradparse_ch:
|
|
|
|
|
switch ((c = *s++)) {
|
|
|
|
|
case 'E':
|
|
|
|
|
new_exp = false;
|
|
|
|
|
goto print_tradparse_ch;
|
|
|
|
|
case 'e':
|
|
|
|
|
new_exp = true;
|
|
|
|
|
goto print_tradparse_ch;
|
|
|
|
|
case 'n':
|
|
|
|
|
new_nl = false;
|
|
|
|
|
goto print_tradparse_ch;
|
|
|
|
|
case '\0':
|
|
|
|
|
po_exp = new_exp;
|
|
|
|
|
po_nl = new_nl;
|
|
|
|
|
++wp;
|
|
|
|
|
goto print_tradparse_arg;
|
|
|
|
|
}
|
2009-03-22 18:52:48 +01:00
|
|
|
|
}
|
2009-10-10 23:17:31 +02:00
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
} else {
|
2016-01-20 22:34:13 +01:00
|
|
|
|
/* "print" builtin */
|
|
|
|
|
const char *opts = "npRrsu,";
|
|
|
|
|
const char *emsg;
|
|
|
|
|
/* print a "--" argument */
|
|
|
|
|
bool po_pminusminus = false;
|
|
|
|
|
|
|
|
|
|
while ((c = ksh_getopt(wp, &builtin_opt, opts)) != -1)
|
|
|
|
|
switch (c) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
case 'e':
|
2016-01-20 22:34:13 +01:00
|
|
|
|
po_exp = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'n':
|
2016-01-20 22:34:13 +01:00
|
|
|
|
po_nl = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'p':
|
|
|
|
|
if ((fd = coproc_getfd(W_OK, &emsg)) < 0) {
|
2016-01-20 22:34:13 +01:00
|
|
|
|
bi_errorf("-p: %s", emsg);
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
2016-01-20 22:34:13 +01:00
|
|
|
|
case 'R':
|
|
|
|
|
/* fake BSD echo command */
|
|
|
|
|
po_pminusminus = true;
|
|
|
|
|
po_exp = false;
|
|
|
|
|
opts = "en";
|
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
case 'r':
|
2016-01-20 22:34:13 +01:00
|
|
|
|
po_exp = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 's':
|
2016-01-20 22:34:13 +01:00
|
|
|
|
po_hist = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'u':
|
|
|
|
|
if (!*(s = builtin_opt.optarg))
|
|
|
|
|
fd = 0;
|
|
|
|
|
else if ((fd = check_fd(s, W_OK, &emsg)) < 0) {
|
2016-01-20 22:34:13 +01:00
|
|
|
|
bi_errorf("-u%s: %s", s, emsg);
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '?':
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2009-10-10 23:17:31 +02:00
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (!(builtin_opt.info & GI_MINUSMINUS)) {
|
2016-01-20 22:34:13 +01:00
|
|
|
|
/* treat a lone "-" like "--" */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (wp[builtin_opt.optind] &&
|
• 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
|
|
|
|
ksh_isdash(wp[builtin_opt.optind]))
|
2005-05-23 05:06:10 +02:00
|
|
|
|
builtin_opt.optind++;
|
2016-01-20 22:34:13 +01:00
|
|
|
|
} else if (po_pminusminus)
|
|
|
|
|
builtin_opt.optind--;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Xinit(xs, xp, 128, ATEMP);
|
|
|
|
|
|
2016-01-20 22:34:13 +01:00
|
|
|
|
if (*wp != NULL) {
|
|
|
|
|
print_read_arg:
|
2005-05-23 05:06:10 +02:00
|
|
|
|
s = *wp;
|
|
|
|
|
while ((c = *s++) != '\0') {
|
|
|
|
|
Xcheck(xs, xp);
|
2016-01-20 22:34:13 +01:00
|
|
|
|
if (po_exp && c == '\\') {
|
2009-09-19 23:54:46 +02:00
|
|
|
|
s_ptr = s;
|
|
|
|
|
c = unbksl(false, s_get, s_put);
|
|
|
|
|
s = s_ptr;
|
|
|
|
|
if (c == -1) {
|
2009-09-19 17:16:05 +02:00
|
|
|
|
/* rejected by generic function */
|
|
|
|
|
switch ((c = *s++)) {
|
|
|
|
|
case 'c':
|
2016-01-20 22:34:13 +01:00
|
|
|
|
po_nl = false;
|
2009-09-19 17:16:05 +02:00
|
|
|
|
/* AT&T brain damage */
|
|
|
|
|
continue;
|
|
|
|
|
case '\0':
|
2016-01-20 22:34:13 +01:00
|
|
|
|
--s;
|
2009-09-19 17:16:05 +02:00
|
|
|
|
c = '\\';
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Xput(xs, xp, '\\');
|
2008-02-24 16:48:43 +01:00
|
|
|
|
}
|
2009-09-19 23:54:46 +02:00
|
|
|
|
} else if ((unsigned int)c > 0xFF) {
|
2009-09-19 17:16:05 +02:00
|
|
|
|
/* generic function returned Unicode */
|
|
|
|
|
char ts[4];
|
|
|
|
|
|
2011-08-27 20:06:52 +02:00
|
|
|
|
ts[utf_wctomb(ts, c - 0x100)] = 0;
|
2015-10-24 21:46:10 +02:00
|
|
|
|
c = 0;
|
|
|
|
|
do {
|
2009-09-19 17:16:05 +02:00
|
|
|
|
Xput(xs, xp, ts[c]);
|
2015-10-24 21:46:10 +02:00
|
|
|
|
} while (ts[++c]);
|
2009-09-19 17:16:05 +02:00
|
|
|
|
continue;
|
2009-10-15 18:36:27 +02:00
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
Xput(xs, xp, c);
|
|
|
|
|
}
|
2016-01-20 22:34:13 +01:00
|
|
|
|
if (*++wp != NULL) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
Xput(xs, xp, ' ');
|
2016-01-20 22:34:13 +01:00
|
|
|
|
goto print_read_arg;
|
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2016-01-20 22:34:13 +01:00
|
|
|
|
if (po_nl)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
Xput(xs, xp, '\n');
|
|
|
|
|
|
2016-01-20 22:34:13 +01:00
|
|
|
|
c = 0;
|
|
|
|
|
if (po_hist) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
Xput(xs, xp, '\0');
|
2015-07-05 21:37:18 +02:00
|
|
|
|
histsave(&source->line, Xstring(xs, xp), HIST_STORE, false);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
Xfree(xs, xp);
|
|
|
|
|
} else {
|
2016-01-20 22:34:13 +01:00
|
|
|
|
size_t len = Xlength(xs, xp);
|
|
|
|
|
bool po_coproc = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
int opipe = 0;
|
|
|
|
|
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*
|
|
|
|
|
* Ensure we aren't killed by a SIGPIPE while writing to
|
2009-06-10 20:12:51 +02:00
|
|
|
|
* a coprocess. AT&T ksh doesn't seem to do this (seems
|
2006-01-30 13:37:24 +01:00
|
|
|
|
* to just check that the co-process is alive which is
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* not enough).
|
|
|
|
|
*/
|
|
|
|
|
if (coproc.write >= 0 && coproc.write == fd) {
|
2016-01-20 22:34:13 +01:00
|
|
|
|
po_coproc = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
opipe = block_pipe();
|
|
|
|
|
}
|
2016-01-20 22:34:13 +01:00
|
|
|
|
|
|
|
|
|
s = Xstring(xs, xp);
|
|
|
|
|
while (len > 0) {
|
|
|
|
|
ssize_t nwritten;
|
|
|
|
|
|
|
|
|
|
if ((nwritten = write(fd, s, len)) < 0) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (errno == EINTR) {
|
2016-01-20 22:34:13 +01:00
|
|
|
|
if (po_coproc)
|
|
|
|
|
restore_pipe(opipe);
|
|
|
|
|
/* give the user a chance to ^C out */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
intrcheck();
|
2016-01-20 22:34:13 +01:00
|
|
|
|
/* interrupted, try again */
|
|
|
|
|
if (po_coproc)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
opipe = block_pipe();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-01-20 22:34:13 +01:00
|
|
|
|
c = 1;
|
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2016-01-20 22:34:13 +01:00
|
|
|
|
s += nwritten;
|
|
|
|
|
len -= nwritten;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2016-01-20 22:34:13 +01:00
|
|
|
|
if (po_coproc)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
restore_pipe(opipe);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 22:34:13 +01:00
|
|
|
|
return (c);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-09-19 23:54:46 +02:00
|
|
|
|
static int
|
|
|
|
|
s_get(void)
|
|
|
|
|
{
|
|
|
|
|
return (*s_ptr++);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2009-12-12 23:27:10 +01:00
|
|
|
|
s_put(int c MKSH_A_UNUSED)
|
2009-09-19 23:54:46 +02:00
|
|
|
|
{
|
|
|
|
|
--s_ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_whence(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
struct tbl *tp;
|
2007-03-04 01:13:17 +01:00
|
|
|
|
const char *id;
|
2008-04-01 23:50:58 +02:00
|
|
|
|
bool pflag = false, vflag = false, Vflag = false;
|
2008-04-02 00:20:20 +02:00
|
|
|
|
int rv = 0, optc, fcflags;
|
|
|
|
|
bool iam_whence = wp[0][0] == 'w';
|
2005-05-23 05:06:10 +02:00
|
|
|
|
const char *opts = iam_whence ? "pv" : "pvV";
|
|
|
|
|
|
2006-05-10 20:54:13 +02:00
|
|
|
|
while ((optc = ksh_getopt(wp, &builtin_opt, opts)) != -1)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
switch (optc) {
|
|
|
|
|
case 'p':
|
2008-04-01 23:50:58 +02:00
|
|
|
|
pflag = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'v':
|
2008-04-01 23:50:58 +02:00
|
|
|
|
vflag = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'V':
|
2008-04-01 23:50:58 +02:00
|
|
|
|
Vflag = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case '?':
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
|
|
|
|
|
fcflags = FC_BI | FC_PATH | FC_FUNC;
|
|
|
|
|
if (!iam_whence) {
|
|
|
|
|
/* Note that -p on its own is deal with in comexec() */
|
|
|
|
|
if (pflag)
|
|
|
|
|
fcflags |= FC_DEFPATH;
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*
|
|
|
|
|
* Convert command options to whence options - note that
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* command -pV uses a different path search than whence -v
|
2009-06-10 20:12:51 +02:00
|
|
|
|
* or whence -pv. This should be considered a feature.
|
2005-05-23 05:06:10 +02:00
|
|
|
|
*/
|
|
|
|
|
vflag = Vflag;
|
|
|
|
|
}
|
|
|
|
|
if (pflag)
|
|
|
|
|
fcflags &= ~(FC_BI | FC_FUNC);
|
|
|
|
|
|
2008-04-02 00:20:20 +02:00
|
|
|
|
while ((vflag || rv == 0) && (id = *wp++) != NULL) {
|
2009-08-28 22:30:59 +02:00
|
|
|
|
uint32_t h = 0;
|
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
|
tp = NULL;
|
2015-07-05 19:50:40 +02:00
|
|
|
|
if (!pflag)
|
2009-08-28 22:30:59 +02:00
|
|
|
|
tp = ktsearch(&keywords, id, h = hash(id));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (!tp && !pflag) {
|
2009-08-28 22:30:59 +02:00
|
|
|
|
tp = ktsearch(&aliases, id, h ? h : hash(id));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (tp && !(tp->flag & ISSET))
|
|
|
|
|
tp = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (!tp)
|
|
|
|
|
tp = findcom(id, fcflags);
|
|
|
|
|
if (vflag || (tp->type != CALIAS && tp->type != CEXEC &&
|
|
|
|
|
tp->type != CTALIAS))
|
2008-04-01 23:50:58 +02:00
|
|
|
|
shf_puts(id, shl_stdout);
|
2015-01-04 22:08:10 +01:00
|
|
|
|
if (vflag) {
|
2010-08-28 20:50:58 +02:00
|
|
|
|
switch (tp->type) {
|
|
|
|
|
case CKEYWD:
|
|
|
|
|
case CALIAS:
|
|
|
|
|
case CFUNC:
|
|
|
|
|
case CSHELL:
|
|
|
|
|
shf_puts(" is a", shl_stdout);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-01-04 22:08:10 +01:00
|
|
|
|
switch (tp->type) {
|
|
|
|
|
case CKEYWD:
|
|
|
|
|
case CSHELL:
|
|
|
|
|
case CTALIAS:
|
|
|
|
|
case CEXEC:
|
|
|
|
|
shf_putc(' ', shl_stdout);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-08-28 20:50:58 +02:00
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
|
switch (tp->type) {
|
|
|
|
|
case CKEYWD:
|
|
|
|
|
if (vflag)
|
2015-01-04 22:08:10 +01:00
|
|
|
|
shf_puts("reserved word", shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case CALIAS:
|
|
|
|
|
if (vflag)
|
2010-08-28 20:50:58 +02:00
|
|
|
|
shprintf("n %s%s for ",
|
|
|
|
|
(tp->flag & EXPORT) ? "exported " : null,
|
2011-09-07 17:24:22 +02:00
|
|
|
|
Talias);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (!iam_whence && !vflag)
|
2011-09-07 17:24:22 +02:00
|
|
|
|
shprintf("%s %s=", Talias, id);
|
2011-12-29 23:54:22 +01:00
|
|
|
|
print_value_quoted(shl_stdout, tp->val.s);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case CFUNC:
|
|
|
|
|
if (vflag) {
|
|
|
|
|
if (tp->flag & EXPORT)
|
2008-04-01 23:50:58 +02:00
|
|
|
|
shf_puts("n exported", shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (tp->flag & TRACE)
|
2008-04-01 23:50:58 +02:00
|
|
|
|
shf_puts(" traced", shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (!(tp->flag & ISSET)) {
|
2008-04-01 23:50:58 +02:00
|
|
|
|
shf_puts(" undefined", shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (tp->u.fpath)
|
|
|
|
|
shprintf(" (autoload from %s)",
|
|
|
|
|
tp->u.fpath);
|
|
|
|
|
}
|
2011-09-07 17:24:22 +02:00
|
|
|
|
shf_puts(T_function, shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case CSHELL:
|
2015-01-04 22:08:10 +01:00
|
|
|
|
if (vflag) {
|
|
|
|
|
if (tp->flag & SPEC_BI)
|
|
|
|
|
shf_puts("special ", shl_stdout);
|
|
|
|
|
shprintf("%s %s", "shell", Tbuiltin);
|
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case CTALIAS:
|
|
|
|
|
case CEXEC:
|
|
|
|
|
if (tp->flag & ISSET) {
|
|
|
|
|
if (vflag) {
|
2015-01-04 21:37:59 +01:00
|
|
|
|
shf_puts("is ", shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (tp->type == CTALIAS)
|
2010-08-28 20:50:58 +02:00
|
|
|
|
shprintf("a tracked %s%s for ",
|
2005-05-23 05:06:10 +02:00
|
|
|
|
(tp->flag & EXPORT) ?
|
2010-08-28 20:50:58 +02:00
|
|
|
|
"exported " : null,
|
2011-09-07 17:24:22 +02:00
|
|
|
|
Talias);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2008-04-01 23:50:58 +02:00
|
|
|
|
shf_puts(tp->val.s, shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
} else {
|
|
|
|
|
if (vflag)
|
2015-01-04 21:37:59 +01:00
|
|
|
|
shf_puts("not found", shl_stdout);
|
2008-04-02 00:20:20 +02:00
|
|
|
|
rv = 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2015-01-04 22:08:10 +01:00
|
|
|
|
shf_puts(" is *GOK*", shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
2008-04-02 00:20:20 +02:00
|
|
|
|
if (vflag || !rv)
|
2007-07-22 15:34:52 +02:00
|
|
|
|
shf_putc('\n', shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Deal with command -vV - command -p dealt with in comexec() */
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_command(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*
|
|
|
|
|
* Let c_whence do the work. Note that c_command() must be
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* a distinct function from c_whence() (tested in comexec()).
|
|
|
|
|
*/
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (c_whence(wp));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-02 19:57:24 +02:00
|
|
|
|
/* typeset, global, export, and readonly */
|
2012-05-10 01:21:00 +02:00
|
|
|
|
static void c_typeset_vardump(struct tbl *, uint32_t, int, bool, bool);
|
|
|
|
|
static void c_typeset_vardump_recursive(struct block *, uint32_t, int, bool,
|
|
|
|
|
bool);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_typeset(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
struct tbl *vp, **p;
|
2011-07-05 22:12:20 +02:00
|
|
|
|
uint32_t fset = 0, fclr = 0, flag;
|
2012-05-10 01:21:00 +02:00
|
|
|
|
int thing = 0, field = 0, base = 0, i;
|
|
|
|
|
struct block *l;
|
2008-04-01 23:39:45 +02:00
|
|
|
|
const char *opts;
|
2011-12-09 21:40:26 +01:00
|
|
|
|
const char *fieldstr = NULL, *basestr = NULL;
|
2008-04-01 23:39:45 +02:00
|
|
|
|
bool localv = false, func = false, pflag = false, istset = true;
|
2014-05-27 15:22:46 +02:00
|
|
|
|
enum namerefflag new_refflag = SRF_NOP;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
switch (**wp) {
|
2011-01-22 21:33:14 +01:00
|
|
|
|
|
|
|
|
|
/* export */
|
|
|
|
|
case 'e':
|
2005-05-23 05:06:10 +02:00
|
|
|
|
fset |= EXPORT;
|
2008-04-01 23:39:45 +02:00
|
|
|
|
istset = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
2011-01-22 21:33:14 +01:00
|
|
|
|
|
|
|
|
|
/* readonly */
|
|
|
|
|
case 'r':
|
2005-05-23 05:06:10 +02:00
|
|
|
|
fset |= RDONLY;
|
2008-04-01 23:39:45 +02:00
|
|
|
|
istset = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
2011-01-22 21:33:14 +01:00
|
|
|
|
|
|
|
|
|
/* set */
|
|
|
|
|
case 's':
|
2005-05-23 05:06:10 +02:00
|
|
|
|
/* called with 'typeset -' */
|
|
|
|
|
break;
|
2011-01-22 21:33:14 +01:00
|
|
|
|
|
|
|
|
|
/* typeset */
|
|
|
|
|
case 't':
|
2008-04-01 23:39:45 +02:00
|
|
|
|
localv = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-01 23:39:45 +02:00
|
|
|
|
/* see comment below regarding possible opions */
|
2009-09-06 19:42:15 +02:00
|
|
|
|
opts = istset ? "L#R#UZ#afi#lnprtux" : "p";
|
2008-04-01 23:39:45 +02:00
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
|
builtin_opt.flags |= GF_PLUSOPT;
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*
|
|
|
|
|
* AT&T ksh seems to have 0-9 as options which are multiplied
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* to get a number that is used with -L, -R, -Z or -i (eg, -1R2
|
2009-06-10 20:12:51 +02:00
|
|
|
|
* sets right justify in a field of 12). This allows options
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* to be grouped in an order (eg, -Lu12), but disallows -i8 -L3 and
|
|
|
|
|
* does not allow the number to be specified as a separate argument
|
|
|
|
|
* Here, the number must follow the RLZi option, but is optional
|
|
|
|
|
* (see the # kludge in ksh_getopt()).
|
|
|
|
|
*/
|
2012-05-10 01:21:00 +02:00
|
|
|
|
while ((i = ksh_getopt(wp, &builtin_opt, opts)) != -1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
flag = 0;
|
2012-05-10 01:21:00 +02:00
|
|
|
|
switch (i) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
case 'L':
|
|
|
|
|
flag = LJUST;
|
|
|
|
|
fieldstr = builtin_opt.optarg;
|
|
|
|
|
break;
|
|
|
|
|
case 'R':
|
|
|
|
|
flag = RJUST;
|
|
|
|
|
fieldstr = builtin_opt.optarg;
|
|
|
|
|
break;
|
|
|
|
|
case 'U':
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*
|
|
|
|
|
* AT&T ksh uses u, but this conflicts with
|
2009-06-10 20:12:51 +02:00
|
|
|
|
* upper/lower case. If this option is changed,
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* need to change the -U below as well
|
|
|
|
|
*/
|
|
|
|
|
flag = INT_U;
|
|
|
|
|
break;
|
|
|
|
|
case 'Z':
|
|
|
|
|
flag = ZEROFIL;
|
|
|
|
|
fieldstr = builtin_opt.optarg;
|
|
|
|
|
break;
|
2009-08-28 23:51:52 +02:00
|
|
|
|
case 'a':
|
|
|
|
|
/*
|
|
|
|
|
* this is supposed to set (-a) or unset (+a) the
|
|
|
|
|
* indexed array attribute; it does nothing on an
|
|
|
|
|
* existing regular string or indexed array though
|
|
|
|
|
*/
|
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
case 'f':
|
2008-04-01 23:39:45 +02:00
|
|
|
|
func = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'i':
|
|
|
|
|
flag = INTEGER;
|
|
|
|
|
basestr = builtin_opt.optarg;
|
|
|
|
|
break;
|
|
|
|
|
case 'l':
|
|
|
|
|
flag = LCASEV;
|
|
|
|
|
break;
|
2009-09-06 19:42:15 +02:00
|
|
|
|
case 'n':
|
2014-05-27 15:22:46 +02:00
|
|
|
|
new_refflag = (builtin_opt.info & GI_PLUS) ?
|
2011-06-21 23:11:21 +02:00
|
|
|
|
SRF_DISABLE : SRF_ENABLE;
|
2009-09-06 19:42:15 +02:00
|
|
|
|
break;
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/* export, readonly: POSIX -p flag */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
case 'p':
|
2008-04-01 23:39:45 +02:00
|
|
|
|
/* typeset: show values as well */
|
|
|
|
|
pflag = true;
|
|
|
|
|
if (istset)
|
|
|
|
|
continue;
|
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
case 'r':
|
|
|
|
|
flag = RDONLY;
|
|
|
|
|
break;
|
|
|
|
|
case 't':
|
|
|
|
|
flag = TRACE;
|
|
|
|
|
break;
|
|
|
|
|
case 'u':
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/* upper case / autoload */
|
|
|
|
|
flag = UCASEV_AL;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'x':
|
|
|
|
|
flag = EXPORT;
|
|
|
|
|
break;
|
|
|
|
|
case '?':
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
if (builtin_opt.info & GI_PLUS) {
|
|
|
|
|
fclr |= flag;
|
|
|
|
|
fset &= ~flag;
|
|
|
|
|
thing = '+';
|
|
|
|
|
} else {
|
|
|
|
|
fset |= flag;
|
|
|
|
|
fclr &= ~flag;
|
|
|
|
|
thing = '-';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fieldstr && !bi_getn(fieldstr, &field))
|
2014-05-27 15:22:46 +02:00
|
|
|
|
return (1);
|
2014-12-16 00:18:47 +01:00
|
|
|
|
if (basestr) {
|
2015-04-19 16:40:23 +02:00
|
|
|
|
if (!getn(basestr, &base)) {
|
2014-12-16 00:18:47 +01:00
|
|
|
|
bi_errorf("%s: %s", "bad integer base", basestr);
|
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
if (base < 1 || base > 36)
|
|
|
|
|
base = 10;
|
2011-12-09 21:40:26 +01:00
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if (!(builtin_opt.info & GI_MINUSMINUS) && wp[builtin_opt.optind] &&
|
|
|
|
|
(wp[builtin_opt.optind][0] == '-' ||
|
|
|
|
|
wp[builtin_opt.optind][0] == '+') &&
|
|
|
|
|
wp[builtin_opt.optind][1] == '\0') {
|
|
|
|
|
thing = wp[builtin_opt.optind][0];
|
|
|
|
|
builtin_opt.optind++;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-21 23:11:21 +02:00
|
|
|
|
if (func && (((fset|fclr) & ~(TRACE|UCASEV_AL|EXPORT)) ||
|
2014-05-27 15:22:46 +02:00
|
|
|
|
new_refflag != SRF_NOP)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
bi_errorf("only -t, -u and -x options may be used with -f");
|
2014-05-27 15:22:46 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
if (wp[builtin_opt.optind]) {
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*
|
|
|
|
|
* Take care of exclusions.
|
2009-09-06 19:42:15 +02:00
|
|
|
|
* At this point, flags in fset are cleared in fclr and vice
|
2009-06-10 20:12:51 +02:00
|
|
|
|
* versa. This property should be preserved.
|
2005-05-23 05:06:10 +02:00
|
|
|
|
*/
|
2011-01-22 21:33:14 +01:00
|
|
|
|
if (fset & LCASEV)
|
|
|
|
|
/* LCASEV has priority over UCASEV_AL */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
fset &= ~UCASEV_AL;
|
2011-01-22 21:33:14 +01:00
|
|
|
|
if (fset & LJUST)
|
|
|
|
|
/* LJUST has priority over RJUST */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
fset &= ~RJUST;
|
2011-01-22 21:33:14 +01:00
|
|
|
|
if ((fset & (ZEROFIL|LJUST)) == ZEROFIL) {
|
|
|
|
|
/* -Z implies -ZR */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
fset |= RJUST;
|
|
|
|
|
fclr &= ~RJUST;
|
|
|
|
|
}
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*
|
|
|
|
|
* Setting these attributes clears the others, unless they
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* are also set in this command
|
|
|
|
|
*/
|
2009-09-06 19:42:15 +02:00
|
|
|
|
if ((fset & (LJUST | RJUST | ZEROFIL | UCASEV_AL | LCASEV |
|
2014-05-27 15:22:46 +02:00
|
|
|
|
INTEGER | INT_U | INT_L)) || new_refflag != SRF_NOP)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
fclr |= ~fset & (LJUST | RJUST | ZEROFIL | UCASEV_AL |
|
|
|
|
|
LCASEV | INTEGER | INT_U | INT_L);
|
|
|
|
|
}
|
2014-05-27 15:22:46 +02:00
|
|
|
|
if (new_refflag != SRF_NOP) {
|
|
|
|
|
fclr &= ~(ARRAY | ASSOC);
|
|
|
|
|
fset &= ~(ARRAY | ASSOC);
|
|
|
|
|
fclr |= EXPORT;
|
|
|
|
|
fset |= ASSOC;
|
|
|
|
|
if (new_refflag == SRF_DISABLE)
|
|
|
|
|
fclr |= ASSOC;
|
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
/* set variables and attributes */
|
2012-05-10 01:21:00 +02:00
|
|
|
|
if (wp[builtin_opt.optind] &&
|
|
|
|
|
/* not "typeset -p varname" */
|
|
|
|
|
!(!func && pflag && !(fset | fclr))) {
|
|
|
|
|
int rv = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
struct tbl *f;
|
|
|
|
|
|
|
|
|
|
if (localv && !func)
|
|
|
|
|
fset |= LOCAL;
|
|
|
|
|
for (i = builtin_opt.optind; wp[i]; i++) {
|
|
|
|
|
if (func) {
|
|
|
|
|
f = findfunc(wp[i], hash(wp[i]),
|
2011-03-07 21:30:41 +01:00
|
|
|
|
tobool(fset & UCASEV_AL));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (!f) {
|
2009-06-11 14:42:21 +02:00
|
|
|
|
/* AT&T ksh does ++rv: bogus */
|
2008-04-02 00:20:20 +02:00
|
|
|
|
rv = 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (fset | fclr) {
|
|
|
|
|
f->flag |= fset;
|
|
|
|
|
f->flag &= ~fclr;
|
2010-08-28 22:22:24 +02:00
|
|
|
|
} else {
|
2011-04-09 17:14:55 +02:00
|
|
|
|
fpFUNCTf(shl_stdout, 0,
|
|
|
|
|
tobool(f->flag & FKSH),
|
2010-08-28 22:22:24 +02:00
|
|
|
|
wp[i], f->val.t);
|
|
|
|
|
shf_putc('\n', shl_stdout);
|
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
} else if (!typeset(wp[i], fset, fclr, field, base)) {
|
2013-03-30 16:39:36 +01:00
|
|
|
|
bi_errorf("%s: %s", wp[i], "is not an identifier");
|
2014-05-27 15:22:46 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* list variables and attributes */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
|
|
|
|
|
/* no difference at this point.. */
|
|
|
|
|
flag = fset | fclr;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (func) {
|
|
|
|
|
for (l = e->loc; l; l = l->next) {
|
2006-01-29 21:04:54 +01:00
|
|
|
|
for (p = ktsort(&l->funs); (vp = *p++); ) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (flag && (vp->flag & flag) == 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (thing == '-')
|
2010-08-28 22:22:24 +02:00
|
|
|
|
fpFUNCTf(shl_stdout, 0,
|
2011-04-09 17:21:36 +02:00
|
|
|
|
tobool(vp->flag & FKSH),
|
2005-05-23 05:06:10 +02:00
|
|
|
|
vp->name, vp->val.t);
|
|
|
|
|
else
|
2010-08-28 22:22:24 +02:00
|
|
|
|
shf_puts(vp->name, shl_stdout);
|
|
|
|
|
shf_putc('\n', shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-10 01:21:00 +02:00
|
|
|
|
} else if (wp[builtin_opt.optind]) {
|
|
|
|
|
for (i = builtin_opt.optind; wp[i]; i++) {
|
|
|
|
|
varsearch(e->loc, &vp, wp[i], hash(wp[i]));
|
|
|
|
|
c_typeset_vardump(vp, flag, thing, pflag, istset);
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
c_typeset_vardump_recursive(e->loc, flag, thing, pflag, istset);
|
|
|
|
|
return (0);
|
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2012-05-10 01:21:00 +02:00
|
|
|
|
static void
|
|
|
|
|
c_typeset_vardump_recursive(struct block *l, uint32_t flag, int thing,
|
|
|
|
|
bool pflag, bool istset)
|
|
|
|
|
{
|
|
|
|
|
struct tbl **blockvars, *vp;
|
|
|
|
|
|
|
|
|
|
if (l->next)
|
|
|
|
|
c_typeset_vardump_recursive(l->next, flag, thing, pflag, istset);
|
|
|
|
|
blockvars = ktsort(&l->vars);
|
|
|
|
|
while ((vp = *blockvars++))
|
|
|
|
|
c_typeset_vardump(vp, flag, thing, pflag, istset);
|
|
|
|
|
/*XXX doesn’t this leak? */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
c_typeset_vardump(struct tbl *vp, uint32_t flag, int thing, bool pflag,
|
|
|
|
|
bool istset)
|
|
|
|
|
{
|
|
|
|
|
struct tbl *tvp;
|
|
|
|
|
int any_set = 0;
|
|
|
|
|
char *s;
|
|
|
|
|
|
2012-12-04 02:18:34 +01:00
|
|
|
|
if (!vp)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-05-10 01:21:00 +02:00
|
|
|
|
/*
|
|
|
|
|
* See if the parameter is set (for arrays, if any
|
|
|
|
|
* element is set).
|
|
|
|
|
*/
|
|
|
|
|
for (tvp = vp; tvp; tvp = tvp->u.array)
|
|
|
|
|
if (tvp->flag & ISSET) {
|
|
|
|
|
any_set = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Check attributes - note that all array elements
|
|
|
|
|
* have (should have?) the same attributes, so checking
|
|
|
|
|
* the first is sufficient.
|
|
|
|
|
*
|
|
|
|
|
* Report an unset param only if the user has
|
|
|
|
|
* explicitly given it some attribute (like export);
|
|
|
|
|
* otherwise, after "echo $FOO", we would report FOO...
|
|
|
|
|
*/
|
|
|
|
|
if (!any_set && !(vp->flag & USERATTRIB))
|
|
|
|
|
return;
|
|
|
|
|
if (flag && (vp->flag & flag) == 0)
|
|
|
|
|
return;
|
|
|
|
|
if (!(vp->flag & ARRAY))
|
|
|
|
|
/* optimise later conditionals */
|
|
|
|
|
any_set = 0;
|
|
|
|
|
do {
|
|
|
|
|
/*
|
|
|
|
|
* Ignore array elements that aren't set unless there
|
|
|
|
|
* are no set elements, in which case the first is
|
|
|
|
|
* reported on
|
|
|
|
|
*/
|
|
|
|
|
if (any_set && !(vp->flag & ISSET))
|
|
|
|
|
continue;
|
|
|
|
|
/* no arguments */
|
|
|
|
|
if (!thing && !flag) {
|
|
|
|
|
if (any_set == 1) {
|
|
|
|
|
shprintf("%s %s %s\n", Tset, "-A", vp->name);
|
|
|
|
|
any_set = 2;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2012-05-10 01:21:00 +02:00
|
|
|
|
/*
|
|
|
|
|
* AT&T ksh prints things like export, integer,
|
|
|
|
|
* leftadj, zerofill, etc., but POSIX says must
|
|
|
|
|
* be suitable for re-entry...
|
|
|
|
|
*/
|
|
|
|
|
shprintf("%s %s", Ttypeset, "");
|
|
|
|
|
if (((vp->flag & (ARRAY | ASSOC)) == ASSOC))
|
|
|
|
|
shprintf("%s ", "-n");
|
|
|
|
|
if ((vp->flag & INTEGER))
|
|
|
|
|
shprintf("%s ", "-i");
|
|
|
|
|
if ((vp->flag & EXPORT))
|
|
|
|
|
shprintf("%s ", "-x");
|
|
|
|
|
if ((vp->flag & RDONLY))
|
|
|
|
|
shprintf("%s ", "-r");
|
|
|
|
|
if ((vp->flag & TRACE))
|
|
|
|
|
shprintf("%s ", "-t");
|
|
|
|
|
if ((vp->flag & LJUST))
|
|
|
|
|
shprintf("-L%d ", vp->u2.field);
|
|
|
|
|
if ((vp->flag & RJUST))
|
|
|
|
|
shprintf("-R%d ", vp->u2.field);
|
|
|
|
|
if ((vp->flag & ZEROFIL))
|
|
|
|
|
shprintf("%s ", "-Z");
|
|
|
|
|
if ((vp->flag & LCASEV))
|
|
|
|
|
shprintf("%s ", "-l");
|
|
|
|
|
if ((vp->flag & UCASEV_AL))
|
|
|
|
|
shprintf("%s ", "-u");
|
|
|
|
|
if ((vp->flag & INT_U))
|
|
|
|
|
shprintf("%s ", "-U");
|
|
|
|
|
} else if (pflag) {
|
|
|
|
|
shprintf("%s %s", istset ? Ttypeset :
|
|
|
|
|
(flag & EXPORT) ? Texport : Treadonly, "");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2012-05-10 01:21:00 +02:00
|
|
|
|
if (any_set)
|
|
|
|
|
shprintf("%s[%lu]", vp->name, arrayindex(vp));
|
|
|
|
|
else
|
|
|
|
|
shf_puts(vp->name, shl_stdout);
|
|
|
|
|
if ((!thing && !flag && pflag) ||
|
|
|
|
|
(thing == '-' && (vp->flag & ISSET))) {
|
|
|
|
|
s = str_val(vp);
|
|
|
|
|
shf_putc('=', shl_stdout);
|
|
|
|
|
/* AT&T ksh can't have justified integers... */
|
|
|
|
|
if ((vp->flag & (INTEGER | LJUST | RJUST)) == INTEGER)
|
|
|
|
|
shf_puts(s, shl_stdout);
|
|
|
|
|
else
|
|
|
|
|
print_value_quoted(shl_stdout, s);
|
|
|
|
|
}
|
|
|
|
|
shf_putc('\n', shl_stdout);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Only report first 'element' of an array with
|
|
|
|
|
* no set elements.
|
|
|
|
|
*/
|
2012-10-03 18:16:15 +02:00
|
|
|
|
if (!any_set)
|
|
|
|
|
return;
|
|
|
|
|
} while ((vp = vp->u.array));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_alias(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
struct table *t = &aliases;
|
2008-04-01 23:50:58 +02:00
|
|
|
|
int rv = 0, prefix = 0;
|
|
|
|
|
bool rflag = false, tflag, Uflag = false, pflag = false;
|
2011-07-05 22:12:20 +02:00
|
|
|
|
uint32_t xflag = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
int optc;
|
|
|
|
|
|
|
|
|
|
builtin_opt.flags |= GF_PLUSOPT;
|
2006-05-10 20:54:13 +02:00
|
|
|
|
while ((optc = ksh_getopt(wp, &builtin_opt, "dprtUx")) != -1) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
prefix = builtin_opt.info & GI_PLUS ? '+' : '-';
|
|
|
|
|
switch (optc) {
|
|
|
|
|
case 'd':
|
2009-07-25 22:26:33 +02:00
|
|
|
|
#ifdef MKSH_NOPWNAM
|
|
|
|
|
t = NULL; /* fix "alias -dt" */
|
2007-01-11 01:32:31 +01:00
|
|
|
|
#else
|
2005-05-23 05:06:10 +02:00
|
|
|
|
t = &homedirs;
|
2007-01-11 01:32:31 +01:00
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'p':
|
2008-04-01 23:50:58 +02:00
|
|
|
|
pflag = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
2008-04-01 23:50:58 +02:00
|
|
|
|
rflag = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 't':
|
|
|
|
|
t = &taliases;
|
|
|
|
|
break;
|
|
|
|
|
case 'U':
|
|
|
|
|
/*
|
|
|
|
|
* kludge for tracked alias initialization
|
|
|
|
|
* (don't do a path search, just make an entry)
|
|
|
|
|
*/
|
2008-04-01 23:50:58 +02:00
|
|
|
|
Uflag = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'x':
|
|
|
|
|
xflag = EXPORT;
|
|
|
|
|
break;
|
|
|
|
|
case '?':
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2009-07-25 22:26:33 +02:00
|
|
|
|
#ifdef MKSH_NOPWNAM
|
|
|
|
|
if (t == NULL)
|
|
|
|
|
return (0);
|
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
|
|
|
|
|
if (!(builtin_opt.info & GI_MINUSMINUS) && *wp &&
|
|
|
|
|
(wp[0][0] == '-' || wp[0][0] == '+') && wp[0][1] == '\0') {
|
|
|
|
|
prefix = wp[0][0];
|
|
|
|
|
wp++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tflag = t == &taliases;
|
|
|
|
|
|
|
|
|
|
/* "hash -r" means reset all the tracked aliases.. */
|
|
|
|
|
if (rflag) {
|
2007-03-04 01:13:17 +01:00
|
|
|
|
static const char *args[] = {
|
2011-09-07 17:24:22 +02:00
|
|
|
|
Tunalias, "-ta", NULL
|
2007-03-04 01:13:17 +01:00
|
|
|
|
};
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if (!tflag || *wp) {
|
2010-08-28 20:50:58 +02:00
|
|
|
|
shprintf("%s: -r flag can only be used with -t"
|
2011-09-07 17:24:22 +02:00
|
|
|
|
" and without arguments\n", Talias);
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
ksh_getopt_reset(&builtin_opt, GF_ERROR);
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (c_unalias(args));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (*wp == NULL) {
|
|
|
|
|
struct tbl *ap, **p;
|
|
|
|
|
|
2006-01-29 21:04:54 +01:00
|
|
|
|
for (p = ktsort(t); (ap = *p++) != NULL; )
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if ((ap->flag & (ISSET|xflag)) == (ISSET|xflag)) {
|
|
|
|
|
if (pflag)
|
2011-09-07 17:24:22 +02:00
|
|
|
|
shprintf("%s ", Talias);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
shf_puts(ap->name, shl_stdout);
|
|
|
|
|
if (prefix != '+') {
|
|
|
|
|
shf_putc('=', shl_stdout);
|
2011-12-29 23:54:22 +01:00
|
|
|
|
print_value_quoted(shl_stdout, ap->val.s);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2007-07-22 15:34:52 +02:00
|
|
|
|
shf_putc('\n', shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (; *wp != NULL; wp++) {
|
2008-04-02 00:20:20 +02:00
|
|
|
|
const char *alias = *wp, *val, *newval;
|
2007-03-04 01:13:17 +01:00
|
|
|
|
char *xalias = NULL;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
struct tbl *ap;
|
2009-08-28 20:54:01 +02:00
|
|
|
|
uint32_t h;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2008-07-12 18:56:40 +02:00
|
|
|
|
if ((val = cstrchr(alias, '='))) {
|
2008-10-28 15:32:43 +01:00
|
|
|
|
strndupx(xalias, alias, val++ - alias, ATEMP);
|
|
|
|
|
alias = xalias;
|
2008-07-12 18:56:40 +02:00
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
h = hash(alias);
|
|
|
|
|
if (val == NULL && !tflag && !xflag) {
|
2009-08-28 22:30:59 +02:00
|
|
|
|
ap = ktsearch(t, alias, h);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (ap != NULL && (ap->flag&ISSET)) {
|
|
|
|
|
if (pflag)
|
2011-09-07 17:24:22 +02:00
|
|
|
|
shprintf("%s ", Talias);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
shf_puts(ap->name, shl_stdout);
|
|
|
|
|
if (prefix != '+') {
|
|
|
|
|
shf_putc('=', shl_stdout);
|
2011-12-29 23:54:22 +01:00
|
|
|
|
print_value_quoted(shl_stdout, ap->val.s);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2007-07-22 15:34:52 +02:00
|
|
|
|
shf_putc('\n', shl_stdout);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
} else {
|
2011-09-07 17:24:22 +02:00
|
|
|
|
shprintf("%s %s %s\n", alias, Talias,
|
2010-08-28 20:50:58 +02:00
|
|
|
|
"not found");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
rv = 1;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2009-08-28 22:30:59 +02:00
|
|
|
|
ap = ktenter(t, alias, h);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
ap->type = tflag ? CTALIAS : CALIAS;
|
|
|
|
|
/* Are we setting the value or just some flags? */
|
|
|
|
|
if ((val && !tflag) || (!val && tflag && !Uflag)) {
|
|
|
|
|
if (ap->flag&ALLOC) {
|
|
|
|
|
ap->flag &= ~(ALLOC|ISSET);
|
2008-11-12 01:54:52 +01:00
|
|
|
|
afree(ap->val.s, APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2009-06-11 14:42:21 +02:00
|
|
|
|
/* ignore values for -t (AT&T ksh does this) */
|
2011-09-07 17:24:22 +02:00
|
|
|
|
newval = tflag ?
|
|
|
|
|
search_path(alias, path, X_OK, NULL) :
|
|
|
|
|
val;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (newval) {
|
2008-10-28 15:32:43 +01:00
|
|
|
|
strdupx(ap->val.s, newval, APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
ap->flag |= ALLOC|ISSET;
|
|
|
|
|
} else
|
|
|
|
|
ap->flag &= ~ISSET;
|
|
|
|
|
}
|
|
|
|
|
ap->flag |= DEFINED;
|
|
|
|
|
if (prefix == '+')
|
|
|
|
|
ap->flag &= ~xflag;
|
|
|
|
|
else
|
|
|
|
|
ap->flag |= xflag;
|
2008-05-17 20:47:03 +02:00
|
|
|
|
afree(xalias, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_unalias(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
struct table *t = &aliases;
|
|
|
|
|
struct tbl *ap;
|
2008-04-01 23:50:58 +02:00
|
|
|
|
int optc, rv = 0;
|
|
|
|
|
bool all = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2006-05-10 20:54:13 +02:00
|
|
|
|
while ((optc = ksh_getopt(wp, &builtin_opt, "adt")) != -1)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
switch (optc) {
|
|
|
|
|
case 'a':
|
2008-04-01 23:50:58 +02:00
|
|
|
|
all = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'd':
|
2009-07-25 22:26:33 +02:00
|
|
|
|
#ifdef MKSH_NOPWNAM
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/* fix "unalias -dt" */
|
|
|
|
|
t = NULL;
|
2007-01-11 01:32:31 +01:00
|
|
|
|
#else
|
2005-05-23 05:06:10 +02:00
|
|
|
|
t = &homedirs;
|
2007-01-11 01:32:31 +01:00
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 't':
|
|
|
|
|
t = &taliases;
|
|
|
|
|
break;
|
|
|
|
|
case '?':
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2009-07-25 22:26:33 +02:00
|
|
|
|
#ifdef MKSH_NOPWNAM
|
|
|
|
|
if (t == NULL)
|
|
|
|
|
return (0);
|
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
|
|
|
|
|
for (; *wp != NULL; wp++) {
|
2009-08-28 22:30:59 +02:00
|
|
|
|
ap = ktsearch(t, *wp, hash(*wp));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (ap == NULL) {
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/* POSIX */
|
|
|
|
|
rv = 1;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (ap->flag&ALLOC) {
|
|
|
|
|
ap->flag &= ~(ALLOC|ISSET);
|
2008-11-12 01:54:52 +01:00
|
|
|
|
afree(ap->val.s, APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
ap->flag &= ~(DEFINED|ISSET|EXPORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (all) {
|
|
|
|
|
struct tstate ts;
|
|
|
|
|
|
2006-01-29 21:04:54 +01:00
|
|
|
|
for (ktwalk(&ts, t); (ap = ktnext(&ts)); ) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (ap->flag&ALLOC) {
|
|
|
|
|
ap->flag &= ~(ALLOC|ISSET);
|
2008-11-12 01:54:52 +01:00
|
|
|
|
afree(ap->val.s, APERM);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
ap->flag &= ~(DEFINED|ISSET|EXPORT);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_let(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
int rv = 1;
|
2009-03-14 19:12:55 +01:00
|
|
|
|
mksh_ari_t val;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2011-01-22 21:33:14 +01:00
|
|
|
|
if (wp[1] == NULL)
|
|
|
|
|
/* AT&T ksh does this */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
bi_errorf("no arguments");
|
|
|
|
|
else
|
|
|
|
|
for (wp++; *wp; wp++)
|
|
|
|
|
if (!evaluate(*wp, &val, KSH_RETURN_ERROR, true)) {
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/* distinguish error from zero result */
|
|
|
|
|
rv = 2;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
} else
|
|
|
|
|
rv = val == 0;
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_jobs(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2008-04-01 23:50:58 +02:00
|
|
|
|
int optc, flag = 0, nflag = 0, rv = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2006-05-10 20:54:13 +02:00
|
|
|
|
while ((optc = ksh_getopt(wp, &builtin_opt, "lpnz")) != -1)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
switch (optc) {
|
|
|
|
|
case 'l':
|
|
|
|
|
flag = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 'p':
|
|
|
|
|
flag = 2;
|
|
|
|
|
break;
|
|
|
|
|
case 'n':
|
|
|
|
|
nflag = 1;
|
|
|
|
|
break;
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case 'z':
|
|
|
|
|
/* debugging: print zombies */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
nflag = -1;
|
|
|
|
|
break;
|
|
|
|
|
case '?':
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
if (!*wp) {
|
|
|
|
|
if (j_jobs(NULL, flag, nflag))
|
|
|
|
|
rv = 1;
|
|
|
|
|
} else {
|
|
|
|
|
for (; *wp; wp++)
|
|
|
|
|
if (j_jobs(*wp, flag, nflag))
|
|
|
|
|
rv = 1;
|
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-04-05 14:35:32 +02:00
|
|
|
|
#ifndef MKSH_UNEMPLOYED
|
2005-05-23 05:06:10 +02:00
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_fgbg(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2008-04-01 23:50:58 +02:00
|
|
|
|
bool bg = strcmp(*wp, "bg") == 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
|
|
if (!Flag(FMONITOR)) {
|
|
|
|
|
bi_errorf("job control not enabled");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
if (ksh_getopt(wp, &builtin_opt, null) == '?')
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
if (*wp)
|
|
|
|
|
for (; *wp; wp++)
|
|
|
|
|
rv = j_resume(*wp, bg);
|
|
|
|
|
else
|
|
|
|
|
rv = j_resume("%%", bg);
|
2015-10-09 19:48:53 +02:00
|
|
|
|
/* fg returns $? of the job unless POSIX */
|
|
|
|
|
return ((bg | Flag(FPOSIX)) ? 0 : rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2009-04-05 14:35:32 +02:00
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
/* format a single kill item */
|
2015-07-05 16:43:08 +02:00
|
|
|
|
static void
|
2012-10-03 17:13:34 +02:00
|
|
|
|
kill_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 kill_info *ki = (const struct kill_info *)arg;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
i++;
|
2012-10-03 17:13:34 +02:00
|
|
|
|
shf_snprintf(buf, buflen, "%*u %*s %s",
|
Clean up the signal mess, saves 172 Bytes:
* 'sigseen' in Build.sh goes away
* Signal name existence is checked in this order:
have our own¹ -> sys_signame[] -> _sys_signame[] -> build our own²
* Signal description existence is checked in this order:
sys_siglist[] -> _sys_siglist[] -> strsignal() -> NULL³
¹ Predefined list of items, for operating systems where we
cannot build² them, i.e. Plan 9 and Minix 3 (e.g. no $CPP -dD)
² The usual cpp(1) stuff
³ Changed later, see below
* Make $CPP test dependent on $NEED_MKSH_SIGNAME (others can
be added here, this is not absolute)
* Make signal name list generation² dependent on $NEED_MKSH_SIGNAME
* Fix check if the generation worked
* Guarantee that sigtraps[*].name and sigtraps[*].mess are valid
C strings; this makes the code shorter *and* removes a few pos-
sible nil pointer dereferences
* Embed autoconf'd usages of sys_sig* / strsignal / mksh_sigpairs
into inittraps()
* Check for each signal 0<=i<=NSIG that
name is not NULL or "" -> replace with ("%d", i)
mess is not NULL or "" -> replace with ("Signal %d", i)
name does not start (case-insensitive) with "SIG" -> name += 3
* In gettrap(), fix check if signal name starts, case-sensitive
or case-insensitive, depending on need, with "SIG" (bug from millert@)
Other changes:
* Build.sh: ac_test[n]() are documented
* Build.sh: ac_test[n]() can have negative prereqs as well now
* Build.sh: use <<-'EOF' consistently
* bump patchlevel to today
2007-01-12 01:25:40 +01:00
|
|
|
|
ki->num_width, i,
|
|
|
|
|
ki->name_width, sigtraps[i].name,
|
|
|
|
|
sigtraps[i].mess);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_kill(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
Trap *t = NULL;
|
2007-03-04 01:13:17 +01:00
|
|
|
|
const char *p;
|
2008-04-01 23:50:58 +02:00
|
|
|
|
bool lflag = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
int i, n, rv, sig;
|
|
|
|
|
|
|
|
|
|
/* assume old style options if -digits or -UPPERCASE */
|
2006-11-10 08:52:04 +01:00
|
|
|
|
if ((p = wp[1]) && *p == '-' && (ksh_isdigit(p[1]) ||
|
|
|
|
|
ksh_isupper(p[1]))) {
|
2015-04-29 22:44:37 +02:00
|
|
|
|
if (!(t = gettrap(p + 1, false, false))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
bi_errorf("bad signal '%s'", p + 1);
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
i = (wp[2] && strcmp(wp[2], "--") == 0) ? 3 : 2;
|
|
|
|
|
} else {
|
|
|
|
|
int optc;
|
|
|
|
|
|
2006-05-10 20:54:13 +02:00
|
|
|
|
while ((optc = ksh_getopt(wp, &builtin_opt, "ls:")) != -1)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
switch (optc) {
|
|
|
|
|
case 'l':
|
2008-04-01 23:50:58 +02:00
|
|
|
|
lflag = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 's':
|
2015-04-29 22:44:37 +02:00
|
|
|
|
if (!(t = gettrap(builtin_opt.optarg,
|
|
|
|
|
true, false))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
bi_errorf("bad signal '%s'",
|
|
|
|
|
builtin_opt.optarg);
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '?':
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
i = builtin_opt.optind;
|
|
|
|
|
}
|
|
|
|
|
if ((lflag && t) || (!wp[i] && !lflag)) {
|
2006-11-10 00:19:52 +01:00
|
|
|
|
#ifndef MKSH_SMALL
|
2008-06-08 19:15:30 +02:00
|
|
|
|
shf_puts("usage:\tkill [-s signame | -signum | -signame]"
|
|
|
|
|
" { job | pid | pgrp } ...\n"
|
|
|
|
|
"\tkill -l [exit_status ...]\n", shl_out);
|
2006-11-10 00:19:52 +01:00
|
|
|
|
#endif
|
2007-07-22 16:01:50 +02:00
|
|
|
|
bi_errorfz();
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lflag) {
|
|
|
|
|
if (wp[i]) {
|
|
|
|
|
for (; wp[i]; i++) {
|
|
|
|
|
if (!bi_getn(wp[i], &n))
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2015-08-13 23:38:19 +02:00
|
|
|
|
#if (ksh_NSIG <= 128)
|
2015-04-29 22:44:37 +02:00
|
|
|
|
if (n > 128 && n < 128 + ksh_NSIG)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
n -= 128;
|
2014-06-09 15:25:53 +02:00
|
|
|
|
#endif
|
2015-04-29 22:44:37 +02:00
|
|
|
|
if (n > 0 && n < ksh_NSIG)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
shprintf("%s\n", sigtraps[n].name);
|
|
|
|
|
else
|
|
|
|
|
shprintf("%d\n", n);
|
|
|
|
|
}
|
2015-10-09 19:48:53 +02:00
|
|
|
|
} else if (Flag(FPOSIX)) {
|
|
|
|
|
n = 1;
|
|
|
|
|
while (n < ksh_NSIG) {
|
|
|
|
|
shf_puts(sigtraps[n].name, shl_stdout);
|
|
|
|
|
shf_putc(++n == ksh_NSIG ? '\n' : ' ',
|
|
|
|
|
shl_stdout);
|
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
} else {
|
2015-04-29 22:44:37 +02:00
|
|
|
|
ssize_t w, mess_cols = 0, mess_octs = 0;
|
2015-08-13 23:38:19 +02:00
|
|
|
|
int j = ksh_NSIG - 1;
|
2015-04-29 22:44:37 +02:00
|
|
|
|
struct kill_info ki = { 0, 0 };
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2015-04-29 22:44:37 +02:00
|
|
|
|
do {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
ki.num_width++;
|
2015-04-29 22:44:37 +02:00
|
|
|
|
} while ((j /= 10));
|
|
|
|
|
|
|
|
|
|
for (j = 1; j < ksh_NSIG; j++) {
|
Clean up the signal mess, saves 172 Bytes:
* 'sigseen' in Build.sh goes away
* Signal name existence is checked in this order:
have our own¹ -> sys_signame[] -> _sys_signame[] -> build our own²
* Signal description existence is checked in this order:
sys_siglist[] -> _sys_siglist[] -> strsignal() -> NULL³
¹ Predefined list of items, for operating systems where we
cannot build² them, i.e. Plan 9 and Minix 3 (e.g. no $CPP -dD)
² The usual cpp(1) stuff
³ Changed later, see below
* Make $CPP test dependent on $NEED_MKSH_SIGNAME (others can
be added here, this is not absolute)
* Make signal name list generation² dependent on $NEED_MKSH_SIGNAME
* Fix check if the generation worked
* Guarantee that sigtraps[*].name and sigtraps[*].mess are valid
C strings; this makes the code shorter *and* removes a few pos-
sible nil pointer dereferences
* Embed autoconf'd usages of sys_sig* / strsignal / mksh_sigpairs
into inittraps()
* Check for each signal 0<=i<=NSIG that
name is not NULL or "" -> replace with ("%d", i)
mess is not NULL or "" -> replace with ("Signal %d", i)
name does not start (case-insensitive) with "SIG" -> name += 3
* In gettrap(), fix check if signal name starts, case-sensitive
or case-insensitive, depending on need, with "SIG" (bug from millert@)
Other changes:
* Build.sh: ac_test[n]() are documented
* Build.sh: ac_test[n]() can have negative prereqs as well now
* Build.sh: use <<-'EOF' consistently
* bump patchlevel to today
2007-01-12 01:25:40 +01:00
|
|
|
|
w = strlen(sigtraps[j].name);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (w > ki.name_width)
|
|
|
|
|
ki.name_width = w;
|
|
|
|
|
w = strlen(sigtraps[j].mess);
|
2009-11-10 00:35:11 +01:00
|
|
|
|
if (w > mess_octs)
|
|
|
|
|
mess_octs = w;
|
2009-11-28 15:28:03 +01:00
|
|
|
|
w = utf_mbswidth(sigtraps[j].mess);
|
2009-11-10 00:35:11 +01:00
|
|
|
|
if (w > mess_cols)
|
|
|
|
|
mess_cols = w;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-29 22:44:37 +02:00
|
|
|
|
print_columns(shl_stdout, (unsigned int)(ksh_NSIG - 1),
|
Clean up the signal mess, saves 172 Bytes:
* 'sigseen' in Build.sh goes away
* Signal name existence is checked in this order:
have our own¹ -> sys_signame[] -> _sys_signame[] -> build our own²
* Signal description existence is checked in this order:
sys_siglist[] -> _sys_siglist[] -> strsignal() -> NULL³
¹ Predefined list of items, for operating systems where we
cannot build² them, i.e. Plan 9 and Minix 3 (e.g. no $CPP -dD)
² The usual cpp(1) stuff
³ Changed later, see below
* Make $CPP test dependent on $NEED_MKSH_SIGNAME (others can
be added here, this is not absolute)
* Make signal name list generation² dependent on $NEED_MKSH_SIGNAME
* Fix check if the generation worked
* Guarantee that sigtraps[*].name and sigtraps[*].mess are valid
C strings; this makes the code shorter *and* removes a few pos-
sible nil pointer dereferences
* Embed autoconf'd usages of sys_sig* / strsignal / mksh_sigpairs
into inittraps()
* Check for each signal 0<=i<=NSIG that
name is not NULL or "" -> replace with ("%d", i)
mess is not NULL or "" -> replace with ("Signal %d", i)
name does not start (case-insensitive) with "SIG" -> name += 3
* In gettrap(), fix check if signal name starts, case-sensitive
or case-insensitive, depending on need, with "SIG" (bug from millert@)
Other changes:
* Build.sh: ac_test[n]() are documented
* Build.sh: ac_test[n]() can have negative prereqs as well now
* Build.sh: use <<-'EOF' consistently
* bump patchlevel to today
2007-01-12 01:25:40 +01:00
|
|
|
|
kill_fmt_entry, (void *)&ki,
|
2009-11-10 00:35:11 +01:00
|
|
|
|
ki.num_width + 1 + ki.name_width + 1 + mess_octs,
|
|
|
|
|
ki.num_width + 1 + ki.name_width + 1 + mess_cols,
|
|
|
|
|
true);
|
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
|
|
|
|
}
|
|
|
|
|
rv = 0;
|
|
|
|
|
sig = t ? t->signal : SIGTERM;
|
|
|
|
|
for (; (p = wp[i]); i++) {
|
|
|
|
|
if (*p == '%') {
|
|
|
|
|
if (j_kill(p, sig))
|
|
|
|
|
rv = 1;
|
|
|
|
|
} else if (!getn(p, &n)) {
|
2010-08-28 20:50:58 +02:00
|
|
|
|
bi_errorf("%s: %s", p,
|
|
|
|
|
"arguments must be jobs or process IDs");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
rv = 1;
|
|
|
|
|
} else {
|
2009-06-08 22:34:40 +02:00
|
|
|
|
if (mksh_kill(n, sig) < 0) {
|
2012-12-18 00:18:11 +01:00
|
|
|
|
bi_errorf("%s: %s", p, cstrerror(errno));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
rv = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
getopts_reset(int val)
|
|
|
|
|
{
|
|
|
|
|
if (val >= 1) {
|
2015-10-09 19:48:53 +02:00
|
|
|
|
ksh_getopt_reset(&user_opt, GF_NONAME |
|
|
|
|
|
(Flag(FPOSIX) ? 0 : GF_PLUSOPT));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
user_opt.optind = user_opt.uoptind = val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_getopts(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2008-04-02 00:20:20 +02:00
|
|
|
|
int argc, optc, rv;
|
|
|
|
|
const char *opts, *var;
|
|
|
|
|
char buf[3];
|
2005-05-23 05:06:10 +02:00
|
|
|
|
struct tbl *vq, *voptarg;
|
|
|
|
|
|
|
|
|
|
if (ksh_getopt(wp, &builtin_opt, null) == '?')
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
|
|
|
|
|
opts = *wp++;
|
|
|
|
|
if (!opts) {
|
2010-08-28 20:50:58 +02:00
|
|
|
|
bi_errorf("missing %s argument", "options");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var = *wp++;
|
|
|
|
|
if (!var) {
|
2010-08-28 20:50:58 +02:00
|
|
|
|
bi_errorf("missing %s argument", "name");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
if (!*var || *skip_varname(var, true)) {
|
2010-08-28 20:50:58 +02:00
|
|
|
|
bi_errorf("%s: %s", var, "is not an identifier");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e->loc->next == NULL) {
|
2010-08-28 20:50:58 +02:00
|
|
|
|
internal_warningf("%s: %s", "c_getopts", "no argv");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
/* Which arguments are we parsing... */
|
|
|
|
|
if (*wp == NULL)
|
|
|
|
|
wp = e->loc->next->argv;
|
|
|
|
|
else
|
|
|
|
|
*--wp = e->loc->next->argv[0];
|
|
|
|
|
|
|
|
|
|
/* Check that our saved state won't cause a core dump... */
|
|
|
|
|
for (argc = 0; wp[argc]; argc++)
|
|
|
|
|
;
|
|
|
|
|
if (user_opt.optind > argc ||
|
|
|
|
|
(user_opt.p != 0 &&
|
|
|
|
|
user_opt.p > strlen(wp[user_opt.optind - 1]))) {
|
|
|
|
|
bi_errorf("arguments changed since last call");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user_opt.optarg = NULL;
|
|
|
|
|
optc = ksh_getopt(wp, &user_opt, opts);
|
|
|
|
|
|
|
|
|
|
if (optc >= 0 && optc != '?' && (user_opt.info & GI_PLUS)) {
|
|
|
|
|
buf[0] = '+';
|
|
|
|
|
buf[1] = optc;
|
|
|
|
|
buf[2] = '\0';
|
|
|
|
|
} else {
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*
|
|
|
|
|
* POSIX says var is set to ? at end-of-options, AT&T ksh
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* sets it to null - we go with POSIX...
|
|
|
|
|
*/
|
|
|
|
|
buf[0] = optc < 0 ? '?' : optc;
|
|
|
|
|
buf[1] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-15 18:32:50 +02:00
|
|
|
|
/* AT&T ksh93 in fact does change OPTIND for unknown options too */
|
|
|
|
|
user_opt.uoptind = user_opt.optind;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
voptarg = global("OPTARG");
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/* AT&T ksh clears ro and int */
|
|
|
|
|
voptarg->flag &= ~RDONLY;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
/* Paranoia: ensure no bizarre results. */
|
|
|
|
|
if (voptarg->flag & INTEGER)
|
|
|
|
|
typeset("OPTARG", 0, INTEGER, 0, 0);
|
|
|
|
|
if (user_opt.optarg == NULL)
|
Add “unset foo[*]” mksh extension, which allows to unset the *contents*
of foo[0] (but not its attributes), and the rest of the array, so that
later “set +A foo bar” will set foo[0]=bar but retain the attributes.
This is important, because, in the future, arrays will have different
attributes per element, instead of all the same (which, actually, is
not entirely true right now either, since “unset foo[0]” will not mo-
dify the attributes of a foo[1] existing at that point in time), where
foo[$newkey] will inherit from foo[0], but typeset foo will only affect
foo[0] no longer foo[*] in the future. (The rules about typeset=local
will still apply, as they affect creation of variables in a scope.)
2010-01-25 15:11:29 +01:00
|
|
|
|
unset(voptarg, 1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
else
|
|
|
|
|
/* This can't fail (have cleared readonly/integer) */
|
|
|
|
|
setstr(voptarg, user_opt.optarg, KSH_RETURN_ERROR);
|
|
|
|
|
|
2008-04-02 00:20:20 +02:00
|
|
|
|
rv = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
vq = global(var);
|
|
|
|
|
/* Error message already printed (integer, readonly) */
|
|
|
|
|
if (!setstr(vq, buf, KSH_RETURN_ERROR))
|
2011-03-27 20:50:06 +02:00
|
|
|
|
rv = 2;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (Flag(FEXPORT))
|
|
|
|
|
typeset(var, EXPORT, 0, 0, 0);
|
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (optc < 0 ? 1 : rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-05-04 22:49:08 +02:00
|
|
|
|
#ifndef MKSH_NO_CMDLINE_EDITING
|
2005-05-23 05:06:10 +02:00
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_bind(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2008-04-01 23:50:58 +02:00
|
|
|
|
int optc, rv = 0;
|
2009-09-23 20:04:58 +02:00
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
|
bool macro = false;
|
|
|
|
|
#endif
|
|
|
|
|
bool list = false;
|
• 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 *cp;
|
|
|
|
|
char *up;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2009-09-23 20:04:58 +02:00
|
|
|
|
while ((optc = ksh_getopt(wp, &builtin_opt,
|
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
|
"lm"
|
|
|
|
|
#else
|
|
|
|
|
"l"
|
|
|
|
|
#endif
|
|
|
|
|
)) != -1)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
switch (optc) {
|
|
|
|
|
case 'l':
|
2008-04-01 23:50:58 +02:00
|
|
|
|
list = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
2009-09-23 20:04:58 +02:00
|
|
|
|
#ifndef MKSH_SMALL
|
2005-05-23 05:06:10 +02:00
|
|
|
|
case 'm':
|
2008-04-01 23:50:58 +02:00
|
|
|
|
macro = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
2009-09-23 20:04:58 +02: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
|
|
|
|
}
|
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
|
2011-01-22 21:33:14 +01:00
|
|
|
|
if (*wp == NULL)
|
|
|
|
|
/* list all */
|
2009-09-23 20:04:58 +02:00
|
|
|
|
rv = x_bind(NULL, NULL,
|
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
|
false,
|
|
|
|
|
#endif
|
|
|
|
|
list);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
for (; *wp != NULL; wp++) {
|
• 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 ((cp = cstrchr(*wp, '=')) == NULL)
|
|
|
|
|
up = NULL;
|
|
|
|
|
else {
|
2008-10-28 15:32:43 +01:00
|
|
|
|
strdupx(up, *wp, ATEMP);
|
• 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
|
|
|
|
up[cp++ - *wp] = '\0';
|
|
|
|
|
}
|
2009-09-23 20:04:58 +02:00
|
|
|
|
if (x_bind(up ? up : *wp, cp,
|
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
|
macro,
|
|
|
|
|
#endif
|
|
|
|
|
false))
|
2005-05-23 05:06:10 +02:00
|
|
|
|
rv = 1;
|
2008-05-17 20:47:03 +02:00
|
|
|
|
afree(up, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2012-05-04 22:49:08 +02:00
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_shift(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
struct block *l = e->loc;
|
|
|
|
|
int n;
|
2009-03-14 19:12:55 +01:00
|
|
|
|
mksh_ari_t val;
|
2007-03-04 01:13:17 +01:00
|
|
|
|
const char *arg;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if (ksh_getopt(wp, &builtin_opt, null) == '?')
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
arg = wp[builtin_opt.optind];
|
|
|
|
|
|
2014-10-12 23:58:53 +02:00
|
|
|
|
if (!arg)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
n = 1;
|
2014-10-12 23:58:53 +02:00
|
|
|
|
else if (!evaluate(arg, &val, KSH_RETURN_ERROR, false)) {
|
|
|
|
|
/* error already printed */
|
|
|
|
|
bi_errorfz();
|
|
|
|
|
return (1);
|
|
|
|
|
} else if (!(n = val)) {
|
|
|
|
|
/* nothing to do */
|
|
|
|
|
return (0);
|
|
|
|
|
} else if (n < 0) {
|
2010-08-28 20:50:58 +02:00
|
|
|
|
bi_errorf("%s: %s", arg, "bad number");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
if (l->argc < n) {
|
|
|
|
|
bi_errorf("nothing to shift");
|
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
l->argv[n] = l->argv[0];
|
|
|
|
|
l->argv += n;
|
|
|
|
|
l->argc -= n;
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_umask(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2008-04-02 00:20:20 +02:00
|
|
|
|
int i, optc;
|
2007-03-04 01:13:17 +01:00
|
|
|
|
const char *cp;
|
2008-04-02 00:20:20 +02:00
|
|
|
|
bool symbolic = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
mode_t old_umask;
|
|
|
|
|
|
2006-05-10 20:54:13 +02:00
|
|
|
|
while ((optc = ksh_getopt(wp, &builtin_opt, "S")) != -1)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
switch (optc) {
|
|
|
|
|
case 'S':
|
2008-04-01 23:50:58 +02:00
|
|
|
|
symbolic = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case '?':
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
cp = wp[builtin_opt.optind];
|
|
|
|
|
if (cp == NULL) {
|
2009-08-08 15:08:53 +02:00
|
|
|
|
old_umask = umask((mode_t)0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
umask(old_umask);
|
|
|
|
|
if (symbolic) {
|
2007-03-04 01:13:17 +01:00
|
|
|
|
char buf[18], *p;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
int j;
|
|
|
|
|
|
|
|
|
|
old_umask = ~old_umask;
|
2007-03-04 01:13:17 +01:00
|
|
|
|
p = buf;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
for (i = 0; i < 3; i++) {
|
2007-03-04 01:13:17 +01:00
|
|
|
|
*p++ = "ugo"[i];
|
|
|
|
|
*p++ = '=';
|
2005-05-23 05:06:10 +02:00
|
|
|
|
for (j = 0; j < 3; j++)
|
|
|
|
|
if (old_umask & (1 << (8 - (3*i + j))))
|
2007-03-04 01:13:17 +01:00
|
|
|
|
*p++ = "rwx"[j];
|
|
|
|
|
*p++ = ',';
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2007-03-04 01:13:17 +01:00
|
|
|
|
p[-1] = '\0';
|
2005-05-23 05:06:10 +02:00
|
|
|
|
shprintf("%s\n", buf);
|
|
|
|
|
} else
|
• 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
|
|
|
|
shprintf("%#3.3o\n", (unsigned int)old_umask);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
} else {
|
|
|
|
|
mode_t new_umask;
|
|
|
|
|
|
2006-11-10 08:52:04 +01:00
|
|
|
|
if (ksh_isdigit(*cp)) {
|
2015-04-29 22:07:35 +02:00
|
|
|
|
new_umask = 0;
|
|
|
|
|
while (*cp >= ord('0') && *cp <= ord('7')) {
|
|
|
|
|
new_umask = new_umask * 8 + ksh_numdig(*cp);
|
|
|
|
|
++cp;
|
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (*cp) {
|
|
|
|
|
bi_errorf("bad number");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* symbolic format */
|
|
|
|
|
int positions, new_val;
|
|
|
|
|
char op;
|
|
|
|
|
|
2009-08-08 15:08:53 +02:00
|
|
|
|
old_umask = umask((mode_t)0);
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/* in case of error */
|
|
|
|
|
umask(old_umask);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
old_umask = ~old_umask;
|
|
|
|
|
new_umask = old_umask;
|
|
|
|
|
positions = 0;
|
|
|
|
|
while (*cp) {
|
• 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
|
|
|
|
while (*cp && vstrchr("augo", *cp))
|
2005-05-23 05:06:10 +02:00
|
|
|
|
switch (*cp++) {
|
|
|
|
|
case 'a':
|
|
|
|
|
positions |= 0111;
|
|
|
|
|
break;
|
|
|
|
|
case 'u':
|
|
|
|
|
positions |= 0100;
|
|
|
|
|
break;
|
|
|
|
|
case 'g':
|
|
|
|
|
positions |= 0010;
|
|
|
|
|
break;
|
|
|
|
|
case 'o':
|
|
|
|
|
positions |= 0001;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!positions)
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/* default is a */
|
|
|
|
|
positions = 0111;
|
• 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 (!vstrchr("=+-", op = *cp))
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
cp++;
|
|
|
|
|
new_val = 0;
|
• 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
|
|
|
|
while (*cp && vstrchr("rwxugoXs", *cp))
|
2005-05-23 05:06:10 +02:00
|
|
|
|
switch (*cp++) {
|
|
|
|
|
case 'r': new_val |= 04; break;
|
|
|
|
|
case 'w': new_val |= 02; break;
|
|
|
|
|
case 'x': new_val |= 01; break;
|
2009-06-10 20:12:51 +02:00
|
|
|
|
case 'u':
|
|
|
|
|
new_val |= old_umask >> 6;
|
|
|
|
|
break;
|
|
|
|
|
case 'g':
|
|
|
|
|
new_val |= old_umask >> 3;
|
|
|
|
|
break;
|
|
|
|
|
case 'o':
|
|
|
|
|
new_val |= old_umask >> 0;
|
|
|
|
|
break;
|
|
|
|
|
case 'X':
|
|
|
|
|
if (old_umask & 0111)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
new_val |= 01;
|
2009-06-10 20:12:51 +02:00
|
|
|
|
break;
|
|
|
|
|
case 's':
|
|
|
|
|
/* ignored */
|
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
new_val = (new_val & 07) * positions;
|
|
|
|
|
switch (op) {
|
|
|
|
|
case '-':
|
|
|
|
|
new_umask &= ~new_val;
|
|
|
|
|
break;
|
|
|
|
|
case '=':
|
|
|
|
|
new_umask = new_val |
|
|
|
|
|
(new_umask & ~(positions * 07));
|
|
|
|
|
break;
|
|
|
|
|
case '+':
|
|
|
|
|
new_umask |= new_val;
|
|
|
|
|
}
|
|
|
|
|
if (*cp == ',') {
|
|
|
|
|
positions = 0;
|
|
|
|
|
cp++;
|
• 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 (!vstrchr("=+-", *cp))
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (*cp) {
|
|
|
|
|
bi_errorf("bad mask");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
new_umask = ~new_umask;
|
|
|
|
|
}
|
|
|
|
|
umask(new_umask);
|
|
|
|
|
}
|
2008-02-24 16:57:20 +01:00
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_dot(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2008-04-02 00:20:20 +02:00
|
|
|
|
const char *file, *cp, **argv;
|
|
|
|
|
int argc, i, errcode;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if (ksh_getopt(wp, &builtin_opt, null) == '?')
|
2008-02-24 16:57:20 +01:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2008-02-24 16:57:20 +01:00
|
|
|
|
if ((cp = wp[builtin_opt.optind]) == NULL) {
|
|
|
|
|
bi_errorf("missing argument");
|
|
|
|
|
return (1);
|
|
|
|
|
}
|
2015-10-09 23:36:59 +02:00
|
|
|
|
file = search_path(cp, path, R_OK, &errcode);
|
|
|
|
|
if (!file && errcode == ENOENT && wp[0][0] == 's' &&
|
|
|
|
|
search_access(cp, R_OK) == 0)
|
|
|
|
|
file = cp;
|
|
|
|
|
if (!file) {
|
2012-12-18 00:18:11 +01:00
|
|
|
|
bi_errorf("%s: %s", cp, cstrerror(errcode));
|
2008-02-24 16:57:20 +01:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set positional parameters? */
|
|
|
|
|
if (wp[builtin_opt.optind + 1]) {
|
|
|
|
|
argv = wp + builtin_opt.optind;
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/* preserve $0 */
|
|
|
|
|
argv[0] = e->loc->argv[0];
|
2005-05-23 05:06:10 +02:00
|
|
|
|
for (argc = 0; argv[argc + 1]; argc++)
|
|
|
|
|
;
|
|
|
|
|
} else {
|
|
|
|
|
argc = 0;
|
|
|
|
|
argv = NULL;
|
|
|
|
|
}
|
2012-10-30 21:13:20 +01:00
|
|
|
|
if ((i = include(file, argc, argv, false)) < 0) {
|
2008-04-02 00:20:20 +02:00
|
|
|
|
/* should not happen */
|
2012-12-18 00:18:11 +01:00
|
|
|
|
bi_errorf("%s: %s", cp, cstrerror(errno));
|
2008-02-24 16:57:20 +01:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2008-02-24 16:57:20 +01:00
|
|
|
|
return (i);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_wait(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2008-04-02 00:20:20 +02:00
|
|
|
|
int rv = 0, sig;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if (ksh_getopt(wp, &builtin_opt, null) == '?')
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
if (*wp == NULL) {
|
|
|
|
|
while (waitfor(NULL, &sig) >= 0)
|
|
|
|
|
;
|
|
|
|
|
rv = sig;
|
|
|
|
|
} else {
|
|
|
|
|
for (; *wp; wp++)
|
|
|
|
|
rv = waitfor(*wp, &sig);
|
|
|
|
|
if (rv < 0)
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/* magic exit code: bad job-id */
|
|
|
|
|
rv = sig ? sig : 127;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-29 02:41:15 +01:00
|
|
|
|
static char REPLY[] = "REPLY";
|
2005-05-23 05:06:10 +02:00
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_read(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2011-05-29 04:18:57 +02:00
|
|
|
|
#define is_ifsws(c) (ctype((c), C_IFS) && ctype((c), C_IFSWS))
|
2015-04-11 23:18:47 +02:00
|
|
|
|
int c, fd = 0, rv = 0;
|
2011-05-29 04:18:57 +02:00
|
|
|
|
bool savehist = false, intoarray = false, aschars = false;
|
2011-05-29 07:13:12 +02:00
|
|
|
|
bool rawmode = false, expanding = false;
|
2015-04-11 23:18:47 +02:00
|
|
|
|
bool lastparmmode = false, lastparmused = false;
|
2011-05-29 04:18:57 +02:00
|
|
|
|
enum { LINES, BYTES, UPTO, READALL } readmode = LINES;
|
|
|
|
|
char delim = '\n';
|
|
|
|
|
size_t bytesleft = 128, bytesread;
|
2016-01-14 23:49:33 +01:00
|
|
|
|
struct tbl *vp /* FU gcc */ = NULL, *vq = NULL;
|
2011-05-29 04:18:57 +02:00
|
|
|
|
char *cp, *allocd = NULL, *xp;
|
|
|
|
|
const char *ccp;
|
|
|
|
|
XString xs;
|
2014-11-25 21:00:39 +01:00
|
|
|
|
size_t xsave = 0;
|
2012-05-05 00:18:27 +02:00
|
|
|
|
mksh_ttyst tios;
|
2011-05-29 04:18:57 +02:00
|
|
|
|
bool restore_tios = false;
|
2016-01-14 23:49:33 +01:00
|
|
|
|
/* to catch read -aN2 foo[i] */
|
|
|
|
|
bool subarray = false;
|
2011-05-29 04:18:57 +02:00
|
|
|
|
#if HAVE_SELECT
|
|
|
|
|
bool hastimeout = false;
|
|
|
|
|
struct timeval tv, tvlim;
|
|
|
|
|
#define c_read_opts "Aad:N:n:prst:u,"
|
|
|
|
|
#else
|
|
|
|
|
#define c_read_opts "Aad:N:n:prsu,"
|
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2011-05-29 04:18:57 +02:00
|
|
|
|
while ((c = ksh_getopt(wp, &builtin_opt, c_read_opts)) != -1)
|
|
|
|
|
switch (c) {
|
|
|
|
|
case 'a':
|
|
|
|
|
aschars = true;
|
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
case 'A':
|
|
|
|
|
intoarray = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'd':
|
|
|
|
|
delim = builtin_opt.optarg[0];
|
|
|
|
|
break;
|
|
|
|
|
case 'N':
|
|
|
|
|
case 'n':
|
|
|
|
|
readmode = c == 'N' ? BYTES : UPTO;
|
|
|
|
|
if (!bi_getn(builtin_opt.optarg, &c))
|
|
|
|
|
return (2);
|
|
|
|
|
if (c == -1) {
|
2015-03-20 22:01:41 +01:00
|
|
|
|
readmode = readmode == BYTES ? READALL : UPTO;
|
2011-05-29 04:18:57 +02:00
|
|
|
|
bytesleft = 1024;
|
|
|
|
|
} else
|
|
|
|
|
bytesleft = (unsigned int)c;
|
|
|
|
|
break;
|
|
|
|
|
case 'p':
|
|
|
|
|
if ((fd = coproc_getfd(R_OK, &ccp)) < 0) {
|
|
|
|
|
bi_errorf("%s: %s", "-p", ccp);
|
|
|
|
|
return (2);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
|
|
|
|
rawmode = true;
|
|
|
|
|
break;
|
|
|
|
|
case 's':
|
|
|
|
|
savehist = true;
|
|
|
|
|
break;
|
|
|
|
|
#if HAVE_SELECT
|
|
|
|
|
case 't':
|
|
|
|
|
if (parse_usec(builtin_opt.optarg, &tv)) {
|
2012-12-18 00:18:11 +01:00
|
|
|
|
bi_errorf("%s: %s '%s'", Tsynerr, cstrerror(errno),
|
2011-05-29 04:18:57 +02:00
|
|
|
|
builtin_opt.optarg);
|
|
|
|
|
return (2);
|
|
|
|
|
}
|
|
|
|
|
hastimeout = true;
|
|
|
|
|
break;
|
|
|
|
|
#endif
|
|
|
|
|
case 'u':
|
|
|
|
|
if (!builtin_opt.optarg[0])
|
|
|
|
|
fd = 0;
|
|
|
|
|
else if ((fd = check_fd(builtin_opt.optarg, R_OK, &ccp)) < 0) {
|
|
|
|
|
bi_errorf("%s: %s: %s", "-u", builtin_opt.optarg, ccp);
|
|
|
|
|
return (2);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2011-05-29 04:18:57 +02:00
|
|
|
|
break;
|
|
|
|
|
case '?':
|
|
|
|
|
return (2);
|
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
if (*wp == NULL)
|
|
|
|
|
*--wp = REPLY;
|
|
|
|
|
|
2011-05-29 04:18:57 +02:00
|
|
|
|
if (intoarray && wp[1] != NULL) {
|
|
|
|
|
bi_errorf("too many arguments");
|
|
|
|
|
return (2);
|
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2011-05-29 04:18:57 +02:00
|
|
|
|
if ((ccp = cstrchr(*wp, '?')) != NULL) {
|
|
|
|
|
strdupx(allocd, *wp, ATEMP);
|
|
|
|
|
allocd[ccp - *wp] = '\0';
|
|
|
|
|
*wp = allocd;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (isatty(fd)) {
|
2011-01-21 23:00:17 +01:00
|
|
|
|
/*
|
|
|
|
|
* AT&T ksh says it prints prompt on fd if it's open
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* for writing and is a tty, but it doesn't do it
|
|
|
|
|
* (it also doesn't check the interactive flag,
|
2011-01-21 23:00:17 +01:00
|
|
|
|
* as is indicated in the Korn Shell book).
|
2005-05-23 05:06:10 +02:00
|
|
|
|
*/
|
2011-05-29 04:18:57 +02:00
|
|
|
|
shf_puts(ccp + 1, shl_out);
|
2011-06-11 05:14:50 +02:00
|
|
|
|
shf_flush(shl_out);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-29 04:18:57 +02:00
|
|
|
|
Xinit(xs, xp, bytesleft, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2011-05-29 04:18:57 +02:00
|
|
|
|
if (readmode == LINES)
|
|
|
|
|
bytesleft = 1;
|
|
|
|
|
else if (isatty(fd)) {
|
|
|
|
|
x_mkraw(fd, &tios, true);
|
|
|
|
|
restore_tios = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if HAVE_SELECT
|
|
|
|
|
if (hastimeout) {
|
2012-05-04 23:47:04 +02:00
|
|
|
|
mksh_TIME(tvlim);
|
2011-05-29 04:18:57 +02:00
|
|
|
|
timeradd(&tvlim, &tv, &tvlim);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
c_read_readloop:
|
|
|
|
|
#if HAVE_SELECT
|
|
|
|
|
if (hastimeout) {
|
|
|
|
|
fd_set fdset;
|
|
|
|
|
|
|
|
|
|
FD_ZERO(&fdset);
|
2012-10-03 19:24:23 +02:00
|
|
|
|
FD_SET((unsigned int)fd, &fdset);
|
2012-05-04 23:47:04 +02:00
|
|
|
|
mksh_TIME(tv);
|
2011-05-29 04:18:57 +02:00
|
|
|
|
timersub(&tvlim, &tv, &tv);
|
|
|
|
|
if (tv.tv_sec < 0) {
|
|
|
|
|
/* timeout expired globally */
|
|
|
|
|
rv = 1;
|
|
|
|
|
goto c_read_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (select(fd + 1, &fdset, NULL, NULL, &tv)) {
|
|
|
|
|
case 1:
|
|
|
|
|
break;
|
|
|
|
|
case 0:
|
|
|
|
|
/* timeout expired for this call */
|
2015-06-28 16:57:25 +02:00
|
|
|
|
bytesread = 0;
|
|
|
|
|
/* fake EOF read; all cases return 1 */
|
|
|
|
|
goto c_read_didread;
|
2011-05-29 04:18:57 +02:00
|
|
|
|
default:
|
2012-12-18 00:18:11 +01:00
|
|
|
|
bi_errorf("%s: %s", Tselect, cstrerror(errno));
|
2011-05-29 04:18:57 +02:00
|
|
|
|
rv = 2;
|
|
|
|
|
goto c_read_out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
bytesread = blocking_read(fd, xp, bytesleft);
|
|
|
|
|
if (bytesread == (size_t)-1) {
|
|
|
|
|
/* interrupted */
|
|
|
|
|
if (errno == EINTR && fatal_trap_check()) {
|
|
|
|
|
/*
|
|
|
|
|
* Was the offending signal one that would
|
|
|
|
|
* normally kill a process? If so, pretend
|
|
|
|
|
* the read was killed.
|
|
|
|
|
*/
|
|
|
|
|
rv = 2;
|
|
|
|
|
goto c_read_out;
|
|
|
|
|
}
|
|
|
|
|
/* just ignore the signal */
|
|
|
|
|
goto c_read_readloop;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-28 16:57:25 +02:00
|
|
|
|
c_read_didread:
|
2011-05-29 04:18:57 +02:00
|
|
|
|
switch (readmode) {
|
|
|
|
|
case READALL:
|
|
|
|
|
if (bytesread == 0) {
|
|
|
|
|
/* end of file reached */
|
|
|
|
|
rv = 1;
|
|
|
|
|
goto c_read_readdone;
|
|
|
|
|
}
|
|
|
|
|
xp += bytesread;
|
|
|
|
|
XcheckN(xs, xp, bytesleft);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case UPTO:
|
|
|
|
|
if (bytesread == 0)
|
|
|
|
|
/* end of file reached */
|
|
|
|
|
rv = 1;
|
|
|
|
|
xp += bytesread;
|
|
|
|
|
goto c_read_readdone;
|
|
|
|
|
|
|
|
|
|
case BYTES:
|
|
|
|
|
if (bytesread == 0) {
|
|
|
|
|
/* end of file reached */
|
|
|
|
|
rv = 1;
|
2015-06-28 16:57:25 +02:00
|
|
|
|
/* may be partial read: $? = 1, but content */
|
2011-05-29 04:18:57 +02:00
|
|
|
|
goto c_read_readdone;
|
|
|
|
|
}
|
|
|
|
|
xp += bytesread;
|
|
|
|
|
if ((bytesleft -= bytesread) == 0)
|
|
|
|
|
goto c_read_readdone;
|
|
|
|
|
break;
|
|
|
|
|
case LINES:
|
|
|
|
|
if (bytesread == 0) {
|
|
|
|
|
/* end of file reached */
|
|
|
|
|
rv = 1;
|
|
|
|
|
goto c_read_readdone;
|
|
|
|
|
}
|
|
|
|
|
if ((c = *xp) == '\0' && !aschars && delim != '\0') {
|
|
|
|
|
/* skip any read NULs unless delimiter */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (expanding) {
|
|
|
|
|
expanding = false;
|
|
|
|
|
if (c == delim) {
|
|
|
|
|
if (Flag(FTALKING_I) && isatty(fd)) {
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*
|
2011-05-29 04:18:57 +02:00
|
|
|
|
* set prompt in case this is
|
|
|
|
|
* called from .profile or $ENV
|
2005-05-23 05:06:10 +02:00
|
|
|
|
*/
|
2011-05-29 04:18:57 +02:00
|
|
|
|
set_prompt(PS2, NULL);
|
|
|
|
|
pprompt(prompt, 0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2011-05-29 04:18:57 +02:00
|
|
|
|
/* drop the backslash */
|
|
|
|
|
--xp;
|
|
|
|
|
/* and the delimiter */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
2011-05-29 04:18:57 +02:00
|
|
|
|
} else if (c == delim) {
|
|
|
|
|
goto c_read_readdone;
|
|
|
|
|
} else if (!rawmode && c == '\\') {
|
|
|
|
|
expanding = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2011-05-29 04:18:57 +02:00
|
|
|
|
Xcheck(xs, xp);
|
|
|
|
|
++xp;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
goto c_read_readloop;
|
|
|
|
|
|
|
|
|
|
c_read_readdone:
|
|
|
|
|
bytesread = Xlength(xs, xp);
|
|
|
|
|
Xput(xs, xp, '\0');
|
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
|
* state: we finished reading the input and NUL terminated it
|
|
|
|
|
* Xstring(xs, xp) -> xp-1 = input string without trailing delim
|
|
|
|
|
* rv = 1 if EOF, 0 otherwise (errors handled already)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (rv == 1) {
|
|
|
|
|
/* clean up coprocess if needed, on EOF */
|
|
|
|
|
coproc_read_close(fd);
|
|
|
|
|
if (readmode == READALL)
|
|
|
|
|
/* EOF is no error here */
|
|
|
|
|
rv = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (savehist)
|
2015-07-05 21:37:18 +02:00
|
|
|
|
histsave(&source->line, Xstring(xs, xp), HIST_STORE, false);
|
2011-05-29 04:18:57 +02:00
|
|
|
|
|
|
|
|
|
ccp = cp = Xclose(xs, xp);
|
|
|
|
|
expanding = false;
|
|
|
|
|
XinitN(xs, 128, ATEMP);
|
|
|
|
|
if (intoarray) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
vp = global(*wp);
|
2016-01-14 23:49:33 +01:00
|
|
|
|
subarray = last_lookup_was_array;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (vp->flag & RDONLY) {
|
2011-05-29 04:18:57 +02:00
|
|
|
|
c_read_splitro:
|
2011-12-16 21:03:28 +01:00
|
|
|
|
bi_errorf("read-only: %s", *wp);
|
2011-05-29 04:18:57 +02:00
|
|
|
|
c_read_spliterr:
|
|
|
|
|
rv = 2;
|
|
|
|
|
afree(cp, ATEMP);
|
|
|
|
|
goto c_read_out;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2011-05-29 04:18:57 +02:00
|
|
|
|
/* counter for array index */
|
2016-01-14 23:49:33 +01:00
|
|
|
|
c = subarray ? arrayindex(vp) : 0;
|
|
|
|
|
/* exporting an array is currently pointless */
|
|
|
|
|
unset(vp, subarray ? 0 : 1);
|
2011-05-29 04:18:57 +02:00
|
|
|
|
}
|
2011-05-29 07:13:12 +02:00
|
|
|
|
if (!aschars) {
|
|
|
|
|
/* skip initial IFS whitespace */
|
|
|
|
|
while (bytesread && is_ifsws(*ccp)) {
|
|
|
|
|
++ccp;
|
|
|
|
|
--bytesread;
|
|
|
|
|
}
|
|
|
|
|
/* trim trailing IFS whitespace */
|
|
|
|
|
while (bytesread && is_ifsws(ccp[bytesread - 1])) {
|
|
|
|
|
--bytesread;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-29 04:18:57 +02:00
|
|
|
|
c_read_splitloop:
|
|
|
|
|
xp = Xstring(xs, xp);
|
|
|
|
|
/* generate next word */
|
|
|
|
|
if (!bytesread) {
|
|
|
|
|
/* no more input */
|
|
|
|
|
if (intoarray)
|
|
|
|
|
goto c_read_splitdone;
|
|
|
|
|
/* zero out next parameters */
|
|
|
|
|
goto c_read_gotword;
|
|
|
|
|
}
|
|
|
|
|
if (aschars) {
|
|
|
|
|
Xput(xs, xp, '1');
|
|
|
|
|
Xput(xs, xp, '#');
|
|
|
|
|
bytesleft = utf_ptradj(ccp);
|
|
|
|
|
while (bytesleft && bytesread) {
|
|
|
|
|
*xp++ = *ccp++;
|
|
|
|
|
--bytesleft;
|
|
|
|
|
--bytesread;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2011-05-29 04:18:57 +02:00
|
|
|
|
if (xp[-1] == '\0') {
|
|
|
|
|
xp[-1] = '0';
|
|
|
|
|
xp[-3] = '2';
|
|
|
|
|
}
|
|
|
|
|
goto c_read_gotword;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-29 04:18:57 +02:00
|
|
|
|
if (!intoarray && wp[1] == NULL)
|
2015-04-11 23:18:47 +02:00
|
|
|
|
lastparmmode = true;
|
2011-05-29 04:18:57 +02:00
|
|
|
|
|
2011-05-29 07:13:12 +02:00
|
|
|
|
c_read_splitlast:
|
2011-05-29 04:18:57 +02:00
|
|
|
|
/* copy until IFS character */
|
|
|
|
|
while (bytesread) {
|
|
|
|
|
char ch;
|
|
|
|
|
|
2011-05-29 07:13:12 +02:00
|
|
|
|
ch = *ccp;
|
2011-05-29 04:18:57 +02:00
|
|
|
|
if (expanding) {
|
|
|
|
|
expanding = false;
|
2011-05-29 07:13:12 +02:00
|
|
|
|
goto c_read_splitcopy;
|
2011-05-29 04:18:57 +02:00
|
|
|
|
} else if (ctype(ch, C_IFS)) {
|
2011-05-29 07:13:12 +02:00
|
|
|
|
break;
|
2011-05-29 04:18:57 +02:00
|
|
|
|
} else if (!rawmode && ch == '\\') {
|
|
|
|
|
expanding = true;
|
2011-05-29 07:13:12 +02:00
|
|
|
|
} else {
|
|
|
|
|
c_read_splitcopy:
|
|
|
|
|
Xcheck(xs, xp);
|
|
|
|
|
Xput(xs, xp, ch);
|
2011-05-29 04:18:57 +02:00
|
|
|
|
}
|
2011-05-29 07:13:12 +02:00
|
|
|
|
++ccp;
|
|
|
|
|
--bytesread;
|
|
|
|
|
}
|
|
|
|
|
xsave = Xsavepos(xs, xp);
|
|
|
|
|
/* copy word delimiter: IFSWS+IFS,IFSWS */
|
2015-04-11 23:18:47 +02:00
|
|
|
|
expanding = false;
|
2011-05-29 07:13:12 +02:00
|
|
|
|
while (bytesread) {
|
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
|
|
ch = *ccp;
|
|
|
|
|
if (!ctype(ch, C_IFS))
|
|
|
|
|
break;
|
2015-04-11 23:18:47 +02:00
|
|
|
|
if (lastparmmode && !expanding && !rawmode && ch == '\\') {
|
|
|
|
|
expanding = true;
|
|
|
|
|
} else {
|
|
|
|
|
Xcheck(xs, xp);
|
|
|
|
|
Xput(xs, xp, ch);
|
|
|
|
|
}
|
2011-05-29 07:13:12 +02:00
|
|
|
|
++ccp;
|
|
|
|
|
--bytesread;
|
2015-04-11 23:18:47 +02:00
|
|
|
|
if (expanding)
|
|
|
|
|
continue;
|
2011-05-29 07:13:12 +02:00
|
|
|
|
if (!ctype(ch, C_IFSWS))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
while (bytesread && is_ifsws(*ccp)) {
|
|
|
|
|
Xcheck(xs, xp);
|
|
|
|
|
Xput(xs, xp, *ccp);
|
|
|
|
|
++ccp;
|
|
|
|
|
--bytesread;
|
2011-05-29 04:18:57 +02:00
|
|
|
|
}
|
2011-05-29 07:13:12 +02:00
|
|
|
|
/* if no more parameters, rinse and repeat */
|
2015-04-11 23:18:47 +02:00
|
|
|
|
if (lastparmmode && bytesread) {
|
|
|
|
|
lastparmused = true;
|
2011-05-29 07:13:12 +02:00
|
|
|
|
goto c_read_splitlast;
|
2011-05-29 04:18:57 +02:00
|
|
|
|
}
|
2011-05-29 07:13:12 +02:00
|
|
|
|
/* get rid of the delimiter unless we pack the rest */
|
2015-04-11 23:18:47 +02:00
|
|
|
|
if (!lastparmused)
|
2011-05-29 07:13:12 +02:00
|
|
|
|
xp = Xrestpos(xs, xp, xsave);
|
2011-05-29 04:18:57 +02:00
|
|
|
|
c_read_gotword:
|
|
|
|
|
Xput(xs, xp, '\0');
|
|
|
|
|
if (intoarray) {
|
2016-01-14 23:49:33 +01:00
|
|
|
|
if (subarray) {
|
|
|
|
|
/* array element passed, accept first read */
|
|
|
|
|
if (vq) {
|
|
|
|
|
bi_errorf("nested arrays not yet supported");
|
|
|
|
|
goto c_read_spliterr;
|
|
|
|
|
}
|
|
|
|
|
vq = vp;
|
|
|
|
|
if (c)
|
|
|
|
|
/* [0] doesn't */
|
|
|
|
|
vq->flag |= AINDEX;
|
|
|
|
|
} else
|
|
|
|
|
vq = arraysearch(vp, c++);
|
2011-05-29 04:18:57 +02:00
|
|
|
|
} else {
|
|
|
|
|
vq = global(*wp);
|
|
|
|
|
/* must be checked before exporting */
|
|
|
|
|
if (vq->flag & RDONLY)
|
|
|
|
|
goto c_read_splitro;
|
|
|
|
|
if (Flag(FEXPORT))
|
|
|
|
|
typeset(*wp, EXPORT, 0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
if (!setstr(vq, Xstring(xs, xp), KSH_RETURN_ERROR))
|
|
|
|
|
goto c_read_spliterr;
|
|
|
|
|
if (aschars) {
|
|
|
|
|
setint_v(vq, vq, false);
|
|
|
|
|
/* protect from UTFMODE changes */
|
|
|
|
|
vq->type = 0;
|
|
|
|
|
}
|
|
|
|
|
if (intoarray || *++wp != NULL)
|
|
|
|
|
goto c_read_splitloop;
|
|
|
|
|
|
|
|
|
|
c_read_splitdone:
|
|
|
|
|
/* free up */
|
|
|
|
|
afree(cp, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2011-05-29 04:18:57 +02:00
|
|
|
|
c_read_out:
|
|
|
|
|
afree(allocd, ATEMP);
|
|
|
|
|
Xfree(xs, xp);
|
|
|
|
|
if (restore_tios)
|
2012-05-05 00:18:27 +02:00
|
|
|
|
mksh_tcset(fd, &tios);
|
2011-05-29 04:18:57 +02:00
|
|
|
|
return (rv);
|
|
|
|
|
#undef is_ifsws
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_eval(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2005-05-25 11:39:49 +02:00
|
|
|
|
struct source *s, *saves = source;
|
2009-08-08 15:08:53 +02:00
|
|
|
|
unsigned char savef;
|
2007-06-07 01:28:17 +02:00
|
|
|
|
int rv;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if (ksh_getopt(wp, &builtin_opt, null) == '?')
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
s = pushs(SWORDS, ATEMP);
|
|
|
|
|
s->u.strv = wp + builtin_opt.optind;
|
2005-07-04 14:27:28 +02:00
|
|
|
|
|
2009-10-15 18:36:27 +02:00
|
|
|
|
/*-
|
|
|
|
|
* The following code handles the case where the command is
|
|
|
|
|
* empty due to failed command substitution, for example by
|
|
|
|
|
* eval "$(false)"
|
|
|
|
|
* This has historically returned 1 by AT&T ksh88. In this
|
|
|
|
|
* case, shell() will not set or change exstat because the
|
|
|
|
|
* compiled tree is empty, so it will use the value we pass
|
|
|
|
|
* from subst_exstat, which is cleared in execute(), so it
|
|
|
|
|
* should have been 0 if there were no substitutions.
|
2005-07-04 14:27:28 +02:00
|
|
|
|
*
|
2009-10-15 18:36:27 +02:00
|
|
|
|
* POSIX however says we don't do this, even though it is
|
|
|
|
|
* traditionally done. AT&T ksh93 agrees with POSIX, so we
|
|
|
|
|
* do. The following is an excerpt from SUSv4 [1003.2-2008]:
|
2009-06-10 20:12:51 +02:00
|
|
|
|
*
|
2009-10-15 18:36:27 +02:00
|
|
|
|
* 2.9.1: Simple Commands
|
2005-07-04 14:27:28 +02:00
|
|
|
|
* ... If there is a command name, execution shall
|
2009-10-15 18:36:27 +02:00
|
|
|
|
* continue as described in 2.9.1.1 [Command Search
|
|
|
|
|
* and Execution]. If there is no command name, but
|
|
|
|
|
* the command contained a command substitution, the
|
|
|
|
|
* command shall complete with the exit status of the
|
|
|
|
|
* last command substitution performed.
|
|
|
|
|
* 2.9.1.1: Command Search and Execution
|
|
|
|
|
* (1) a. If the command name matches the name of a
|
|
|
|
|
* special built-in utility, that special built-in
|
2005-07-04 14:27:28 +02:00
|
|
|
|
* utility shall be invoked.
|
2009-10-15 18:36:27 +02:00
|
|
|
|
* 2.14.5: eval
|
|
|
|
|
* If there are no arguments, or only null arguments,
|
|
|
|
|
* eval shall return a zero exit status; ...
|
2005-07-04 14:27:28 +02:00
|
|
|
|
*/
|
2012-10-21 23:55:05 +02:00
|
|
|
|
/* AT&T ksh88: use subst_exstat */
|
|
|
|
|
/* exstat = subst_exstat; */
|
|
|
|
|
/* SUSv4: OR with a high value never written otherwise */
|
|
|
|
|
exstat |= 0x4000;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2005-05-25 11:39:49 +02:00
|
|
|
|
savef = Flag(FERREXIT);
|
2013-01-01 21:45:04 +01:00
|
|
|
|
Flag(FERREXIT) |= 0x80;
|
2005-05-25 11:39:49 +02:00
|
|
|
|
rv = shell(s, false);
|
|
|
|
|
Flag(FERREXIT) = savef;
|
|
|
|
|
source = saves;
|
2007-09-09 20:06:42 +02:00
|
|
|
|
afree(s, ATEMP);
|
2012-10-21 23:55:05 +02:00
|
|
|
|
if (exstat & 0x4000)
|
|
|
|
|
/* detect old exstat, use 0 in that case */
|
|
|
|
|
rv = 0;
|
2005-05-25 11:39:49 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_trap(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2015-04-29 22:44:37 +02:00
|
|
|
|
Trap *p = sigtraps;
|
2015-08-13 23:38:19 +02:00
|
|
|
|
int i = ksh_NSIG;
|
2007-03-04 01:13:17 +01:00
|
|
|
|
const char *s;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if (ksh_getopt(wp, &builtin_opt, null) == '?')
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
|
|
|
|
|
if (*wp == NULL) {
|
2015-04-29 22:44:37 +02:00
|
|
|
|
do {
|
|
|
|
|
if (p->trap) {
|
2008-04-01 23:50:58 +02:00
|
|
|
|
shf_puts("trap -- ", shl_stdout);
|
2011-12-29 23:54:22 +01:00
|
|
|
|
print_value_quoted(shl_stdout, p->trap);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
shprintf(" %s\n", p->name);
|
|
|
|
|
}
|
2015-04-29 22:44:37 +02:00
|
|
|
|
++p;
|
2015-08-13 23:38:19 +02:00
|
|
|
|
} while (i--);
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-29 22:44:37 +02:00
|
|
|
|
if (getn(*wp, &i)) {
|
|
|
|
|
/* first argument is a signal number, reset them all */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
s = NULL;
|
2015-04-29 22:44:37 +02:00
|
|
|
|
} else {
|
|
|
|
|
/* first argument must be a command, then */
|
|
|
|
|
s = *wp++;
|
|
|
|
|
/* reset traps? */
|
|
|
|
|
if (ksh_isdash(s))
|
|
|
|
|
s = NULL;
|
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2015-04-29 22:44:37 +02:00
|
|
|
|
/* set/clear the traps */
|
2011-01-30 02:36:00 +01:00
|
|
|
|
i = 0;
|
2015-04-29 22:44:37 +02:00
|
|
|
|
while (*wp)
|
|
|
|
|
if (!(p = gettrap(*wp++, true, true))) {
|
2011-01-30 02:36:00 +01:00
|
|
|
|
warningf(true, "%s: %s '%s'", builtin_argv0,
|
|
|
|
|
"bad signal", wp[-1]);
|
2015-04-29 22:44:37 +02:00
|
|
|
|
i = 1;
|
2011-01-30 02:36:00 +01:00
|
|
|
|
} else
|
|
|
|
|
settrap(p, s);
|
|
|
|
|
return (i);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_exitreturn(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2008-04-02 00:20:20 +02:00
|
|
|
|
int n, how = LEXIT;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2016-01-13 18:20:52 +01:00
|
|
|
|
if (wp[1]) {
|
|
|
|
|
if (wp[2])
|
|
|
|
|
goto c_exitreturn_err;
|
|
|
|
|
exstat = bi_getn(wp[1], &n) ? (n & 0xFF) : 1;
|
|
|
|
|
} else if (trap_exstat != -1)
|
2010-11-01 18:29:05 +01:00
|
|
|
|
exstat = trap_exstat;
|
2016-01-13 18:20:52 +01:00
|
|
|
|
|
2011-01-22 21:33:14 +01:00
|
|
|
|
if (wp[0][0] == 'r') {
|
|
|
|
|
/* return */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
struct env *ep;
|
|
|
|
|
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*
|
|
|
|
|
* need to tell if this is exit or return so trap exit will
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* work right (POSIX)
|
|
|
|
|
*/
|
|
|
|
|
for (ep = e; ep; ep = ep->oenv)
|
|
|
|
|
if (STOP_RETURN(ep->type)) {
|
|
|
|
|
how = LRETURN;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (how == LEXIT && !really_exit && j_stopped_running()) {
|
2012-05-04 23:15:34 +02:00
|
|
|
|
really_exit = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
how = LSHELL;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-13 18:20:52 +01:00
|
|
|
|
/* get rid of any I/O redirections */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
quitenv(NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
unwind(how);
|
2006-05-10 20:54:13 +02:00
|
|
|
|
/* NOTREACHED */
|
2012-03-29 21:23:01 +02:00
|
|
|
|
|
|
|
|
|
c_exitreturn_err:
|
2016-01-13 18:20:52 +01:00
|
|
|
|
bi_errorf("too many arguments");
|
2012-03-29 21:23:01 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_brkcont(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2012-10-03 19:24:23 +02:00
|
|
|
|
unsigned int quit;
|
|
|
|
|
int n;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
struct env *ep, *last_ep = NULL;
|
2007-03-04 01:13:17 +01:00
|
|
|
|
const char *arg;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if (ksh_getopt(wp, &builtin_opt, null) == '?')
|
2012-03-29 21:23:01 +02:00
|
|
|
|
goto c_brkcont_err;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
arg = wp[builtin_opt.optind];
|
|
|
|
|
|
|
|
|
|
if (!arg)
|
|
|
|
|
n = 1;
|
|
|
|
|
else if (!bi_getn(arg, &n))
|
2012-03-29 21:23:01 +02:00
|
|
|
|
goto c_brkcont_err;
|
|
|
|
|
if (n <= 0) {
|
2009-06-11 14:42:21 +02:00
|
|
|
|
/* AT&T ksh does this for non-interactive shells only - weird */
|
2010-08-28 20:50:58 +02:00
|
|
|
|
bi_errorf("%s: %s", arg, "bad value");
|
2012-03-29 21:23:01 +02:00
|
|
|
|
goto c_brkcont_err;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2012-10-03 19:24:23 +02:00
|
|
|
|
quit = (unsigned int)n;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
/* Stop at E_NONE, E_PARSE, E_FUNC, or E_INCL */
|
|
|
|
|
for (ep = e; ep && !STOP_BRKCONT(ep->type); ep = ep->oenv)
|
|
|
|
|
if (ep->type == E_LOOP) {
|
|
|
|
|
if (--quit == 0)
|
|
|
|
|
break;
|
|
|
|
|
ep->flags |= EF_BRKCONT_PASS;
|
|
|
|
|
last_ep = ep;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (quit) {
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*
|
|
|
|
|
* AT&T ksh doesn't print a message - just does what it
|
2009-06-10 20:12:51 +02:00
|
|
|
|
* can. We print a message 'cause it helps in debugging
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* scripts, but don't generate an error (ie, keep going).
|
|
|
|
|
*/
|
2012-10-03 19:24:23 +02:00
|
|
|
|
if ((unsigned int)n == quit) {
|
2010-08-28 22:22:24 +02:00
|
|
|
|
warningf(true, "%s: %s %s", wp[0], "can't", wp[0]);
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*
|
|
|
|
|
* POSIX says if n is too big, the last enclosing loop
|
2009-06-10 20:12:51 +02:00
|
|
|
|
* shall be used. Doesn't say to print an error but we
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* do anyway 'cause the user messed up.
|
|
|
|
|
*/
|
2007-05-13 20:15:25 +02:00
|
|
|
|
if (last_ep)
|
|
|
|
|
last_ep->flags &= ~EF_BRKCONT_PASS;
|
2012-10-03 19:24:23 +02:00
|
|
|
|
warningf(true, "%s: can only %s %u level(s)",
|
|
|
|
|
wp[0], wp[0], (unsigned int)n - quit);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unwind(*wp[0] == 'b' ? LBREAK : LCONTIN);
|
2006-05-10 20:54:13 +02:00
|
|
|
|
/* NOTREACHED */
|
2012-03-29 21:23:01 +02:00
|
|
|
|
|
|
|
|
|
c_brkcont_err:
|
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_set(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2009-12-05 18:43:50 +01:00
|
|
|
|
int argi;
|
|
|
|
|
bool setargs;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
struct block *l = e->loc;
|
2007-07-01 23:10:29 +02:00
|
|
|
|
const char **owp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if (wp[1] == NULL) {
|
2011-09-07 17:24:22 +02:00
|
|
|
|
static const char *args[] = { Tset, "-", NULL };
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (c_typeset(args));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-12 23:58:53 +02:00
|
|
|
|
if ((argi = parse_args(wp, OF_SET, &setargs)) < 0)
|
|
|
|
|
return (2);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
/* set $# and $* */
|
|
|
|
|
if (setargs) {
|
2009-12-05 18:43:50 +01:00
|
|
|
|
wp += argi - 1;
|
|
|
|
|
owp = wp;
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/* save $0 */
|
|
|
|
|
wp[0] = l->argv[0];
|
2005-05-23 05:06:10 +02:00
|
|
|
|
while (*++wp != NULL)
|
2008-12-13 18:02:18 +01:00
|
|
|
|
strdupx(*wp, *wp, &l->area);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
l->argc = wp - owp - 1;
|
2010-09-14 23:26:19 +02:00
|
|
|
|
l->argv = alloc2(l->argc + 2, sizeof(char *), &l->area);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
for (wp = l->argv; (*wp++ = *owp++) != NULL; )
|
|
|
|
|
;
|
|
|
|
|
}
|
2009-10-15 18:36:27 +02:00
|
|
|
|
/*-
|
2009-10-15 16:58:51 +02:00
|
|
|
|
* POSIX says set exit status is 0, but old scripts that use
|
2009-10-15 18:36:27 +02:00
|
|
|
|
* getopt(1) use the construct
|
|
|
|
|
* set -- $(getopt ab:c "$@")
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* which assumes the exit value set will be that of the $()
|
|
|
|
|
* (subst_exstat is cleared in execute() so that it will be 0
|
|
|
|
|
* if there are no command substitutions).
|
|
|
|
|
*/
|
2012-06-25 18:17:54 +02:00
|
|
|
|
#ifdef MKSH_LEGACY_MODE
|
2013-05-02 22:21:45 +02:00
|
|
|
|
/* traditional behaviour, unless set -o posix */
|
|
|
|
|
return (Flag(FPOSIX) ? 0 : subst_exstat);
|
2012-06-25 18:17:54 +02:00
|
|
|
|
#else
|
2013-05-02 22:21:45 +02:00
|
|
|
|
/* conformant behaviour, unless set -o sh +o posix */
|
|
|
|
|
return (Flag(FSH) && !Flag(FPOSIX) ? subst_exstat : 0);
|
2012-06-25 18:17:54 +02:00
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_unset(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2007-03-04 01:13:17 +01:00
|
|
|
|
const char *id;
|
2011-03-27 20:50:06 +02:00
|
|
|
|
int optc, rv = 0;
|
2007-09-09 20:06:42 +02:00
|
|
|
|
bool unset_var = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2006-05-10 20:54:13 +02:00
|
|
|
|
while ((optc = ksh_getopt(wp, &builtin_opt, "fv")) != -1)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
switch (optc) {
|
|
|
|
|
case 'f':
|
2007-09-09 20:06:42 +02:00
|
|
|
|
unset_var = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case 'v':
|
2007-09-09 20:06:42 +02:00
|
|
|
|
unset_var = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case '?':
|
2011-03-27 20:50:06 +02:00
|
|
|
|
/*XXX not reached due to GF_ERROR */
|
|
|
|
|
return (2);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
for (; (id = *wp) != NULL; wp++)
|
2011-01-22 21:33:14 +01:00
|
|
|
|
if (unset_var) {
|
|
|
|
|
/* unset variable */
|
Add “unset foo[*]” mksh extension, which allows to unset the *contents*
of foo[0] (but not its attributes), and the rest of the array, so that
later “set +A foo bar” will set foo[0]=bar but retain the attributes.
This is important, because, in the future, arrays will have different
attributes per element, instead of all the same (which, actually, is
not entirely true right now either, since “unset foo[0]” will not mo-
dify the attributes of a foo[1] existing at that point in time), where
foo[$newkey] will inherit from foo[0], but typeset foo will only affect
foo[0] no longer foo[*] in the future. (The rules about typeset=local
will still apply, as they affect creation of variables in a scope.)
2010-01-25 15:11:29 +01:00
|
|
|
|
struct tbl *vp;
|
|
|
|
|
char *cp = NULL;
|
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
|
|
n = strlen(id);
|
|
|
|
|
if (n > 3 && id[n-3] == '[' && id[n-2] == '*' &&
|
|
|
|
|
id[n-1] == ']') {
|
|
|
|
|
strndupx(cp, id, n - 3, ATEMP);
|
|
|
|
|
id = cp;
|
|
|
|
|
optc = 3;
|
|
|
|
|
} else
|
|
|
|
|
optc = vstrchr(id, '[') ? 0 : 1;
|
|
|
|
|
|
|
|
|
|
vp = global(id);
|
|
|
|
|
afree(cp, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if ((vp->flag&RDONLY)) {
|
2011-12-16 21:03:28 +01:00
|
|
|
|
warningf(true, "read-only: %s", vp->name);
|
2011-03-27 20:50:06 +02:00
|
|
|
|
rv = 1;
|
|
|
|
|
} else
|
|
|
|
|
unset(vp, optc);
|
2011-01-22 21:33:14 +01:00
|
|
|
|
} else
|
|
|
|
|
/* unset function */
|
2007-09-09 20:06:42 +02:00
|
|
|
|
define(id, NULL);
|
2011-03-27 20:50:06 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2009-05-20 12:10:02 +02:00
|
|
|
|
p_time(struct shf *shf, bool posix, long tv_sec, int tv_usec, int width,
|
2005-05-23 05:06:10 +02:00
|
|
|
|
const char *prefix, const char *suffix)
|
|
|
|
|
{
|
2009-05-20 12:10:02 +02:00
|
|
|
|
tv_usec /= 10000;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (posix)
|
2009-05-20 12:10:02 +02:00
|
|
|
|
shf_fprintf(shf, "%s%*ld.%02d%s", prefix, width,
|
|
|
|
|
tv_sec, tv_usec, suffix);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
else
|
2015-08-13 23:04:12 +02:00
|
|
|
|
shf_fprintf(shf, "%s%*ldm%02d.%02ds%s", prefix, width,
|
2009-05-20 12:10:02 +02:00
|
|
|
|
tv_sec / 60, (int)(tv_sec % 60), tv_usec, suffix);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2009-12-12 23:27:10 +01:00
|
|
|
|
c_times(const char **wp MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
struct rusage usage;
|
|
|
|
|
|
2009-04-03 11:39:07 +02:00
|
|
|
|
getrusage(RUSAGE_SELF, &usage);
|
2009-05-20 12:10:02 +02:00
|
|
|
|
p_time(shl_stdout, false, usage.ru_utime.tv_sec,
|
|
|
|
|
usage.ru_utime.tv_usec, 0, null, " ");
|
|
|
|
|
p_time(shl_stdout, false, usage.ru_stime.tv_sec,
|
|
|
|
|
usage.ru_stime.tv_usec, 0, null, "\n");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2009-04-03 11:39:07 +02:00
|
|
|
|
getrusage(RUSAGE_CHILDREN, &usage);
|
2009-05-20 12:10:02 +02:00
|
|
|
|
p_time(shl_stdout, false, usage.ru_utime.tv_sec,
|
|
|
|
|
usage.ru_utime.tv_usec, 0, null, " ");
|
|
|
|
|
p_time(shl_stdout, false, usage.ru_stime.tv_sec,
|
|
|
|
|
usage.ru_stime.tv_usec, 0, null, "\n");
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* time pipeline (really a statement, not a built-in command)
|
|
|
|
|
*/
|
|
|
|
|
int
|
2009-03-22 18:47:38 +01:00
|
|
|
|
timex(struct op *t, int f, volatile int *xerrok)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
#define TF_NOARGS BIT(0)
|
|
|
|
|
#define TF_NOREAL BIT(1) /* don't report real time */
|
2009-10-02 20:08:37 +02:00
|
|
|
|
#define TF_POSIX BIT(2) /* report in POSIX format */
|
2008-04-02 00:20:20 +02:00
|
|
|
|
int rv = 0, tf = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
struct rusage ru0, ru1, cru0, cru1;
|
|
|
|
|
struct timeval usrtime, systime, tv0, tv1;
|
|
|
|
|
|
2012-05-04 23:47:04 +02:00
|
|
|
|
mksh_TIME(tv0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
getrusage(RUSAGE_SELF, &ru0);
|
|
|
|
|
getrusage(RUSAGE_CHILDREN, &cru0);
|
|
|
|
|
if (t->left) {
|
|
|
|
|
/*
|
|
|
|
|
* Two ways of getting cpu usage of a command: just use t0
|
|
|
|
|
* and t1 (which will get cpu usage from other jobs that
|
|
|
|
|
* finish while we are executing t->left), or get the
|
2009-06-11 14:42:21 +02:00
|
|
|
|
* cpu usage of t->left. AT&T ksh does the former, while
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* pdksh tries to do the later (the j_usrtime hack doesn't
|
|
|
|
|
* really work as it only counts the last job).
|
|
|
|
|
*/
|
|
|
|
|
timerclear(&j_usrtime);
|
|
|
|
|
timerclear(&j_systime);
|
2009-03-22 18:47:38 +01:00
|
|
|
|
rv = execute(t->left, f | XTIME, xerrok);
|
2008-08-02 19:45:12 +02:00
|
|
|
|
if (t->left->type == TCOM)
|
|
|
|
|
tf |= t->left->str[0];
|
2012-05-04 23:47:04 +02:00
|
|
|
|
mksh_TIME(tv1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
getrusage(RUSAGE_SELF, &ru1);
|
|
|
|
|
getrusage(RUSAGE_CHILDREN, &cru1);
|
|
|
|
|
} else
|
|
|
|
|
tf = TF_NOARGS;
|
|
|
|
|
|
2011-01-22 21:33:14 +01:00
|
|
|
|
if (tf & TF_NOARGS) {
|
|
|
|
|
/* ksh93 - report shell times (shell+kids) */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
tf |= TF_NOREAL;
|
|
|
|
|
timeradd(&ru0.ru_utime, &cru0.ru_utime, &usrtime);
|
|
|
|
|
timeradd(&ru0.ru_stime, &cru0.ru_stime, &systime);
|
|
|
|
|
} else {
|
|
|
|
|
timersub(&ru1.ru_utime, &ru0.ru_utime, &usrtime);
|
|
|
|
|
timeradd(&usrtime, &j_usrtime, &usrtime);
|
|
|
|
|
timersub(&ru1.ru_stime, &ru0.ru_stime, &systime);
|
|
|
|
|
timeradd(&systime, &j_systime, &systime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(tf & TF_NOREAL)) {
|
|
|
|
|
timersub(&tv1, &tv0, &tv1);
|
|
|
|
|
if (tf & TF_POSIX)
|
2009-05-20 12:10:02 +02:00
|
|
|
|
p_time(shl_out, true, tv1.tv_sec, tv1.tv_usec,
|
|
|
|
|
5, "real ", "\n");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
else
|
2009-05-20 12:10:02 +02:00
|
|
|
|
p_time(shl_out, false, tv1.tv_sec, tv1.tv_usec,
|
|
|
|
|
5, null, " real ");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
if (tf & TF_POSIX)
|
2009-05-20 12:10:02 +02:00
|
|
|
|
p_time(shl_out, true, usrtime.tv_sec, usrtime.tv_usec,
|
|
|
|
|
5, "user ", "\n");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
else
|
2009-05-20 12:10:02 +02:00
|
|
|
|
p_time(shl_out, false, usrtime.tv_sec, usrtime.tv_usec,
|
|
|
|
|
5, null, " user ");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (tf & TF_POSIX)
|
2009-05-20 12:10:02 +02:00
|
|
|
|
p_time(shl_out, true, systime.tv_sec, systime.tv_usec,
|
|
|
|
|
5, "sys ", "\n");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
else
|
2009-05-20 12:10:02 +02:00
|
|
|
|
p_time(shl_out, false, systime.tv_sec, systime.tv_usec,
|
|
|
|
|
5, null, " system\n");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
shf_flush(shl_out);
|
|
|
|
|
|
2006-08-24 22:32:53 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
timex_hook(struct op *t, char **volatile *app)
|
|
|
|
|
{
|
|
|
|
|
char **wp = *app;
|
2008-04-02 00:20:20 +02:00
|
|
|
|
int optc, i, j;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
Getopt opt;
|
|
|
|
|
|
|
|
|
|
ksh_getopt_reset(&opt, 0);
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/* start at the start */
|
|
|
|
|
opt.optind = 0;
|
2007-03-04 01:13:17 +01:00
|
|
|
|
while ((optc = ksh_getopt((const char **)wp, &opt, ":p")) != -1)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
switch (optc) {
|
|
|
|
|
case 'p':
|
|
|
|
|
t->str[0] |= TF_POSIX;
|
|
|
|
|
break;
|
|
|
|
|
case '?':
|
2010-08-28 20:50:58 +02:00
|
|
|
|
errorf("time: -%s %s", opt.optarg,
|
|
|
|
|
"unknown option");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
case ':':
|
2010-08-28 20:50:58 +02:00
|
|
|
|
errorf("time: -%s %s", opt.optarg,
|
|
|
|
|
"requires an argument");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
/* Copy command words down over options. */
|
|
|
|
|
if (opt.optind != 0) {
|
|
|
|
|
for (i = 0; i < opt.optind; i++)
|
|
|
|
|
afree(wp[i], ATEMP);
|
|
|
|
|
for (i = 0, j = opt.optind; (wp[i] = wp[j]); i++, j++)
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
if (!wp[0])
|
|
|
|
|
t->str[0] |= TF_NOARGS;
|
|
|
|
|
*app = wp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* exec with no args - args case is taken care of in comexec() */
|
|
|
|
|
int
|
2009-12-12 23:27:10 +01:00
|
|
|
|
c_exec(const char **wp MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
/* make sure redirects stay in place */
|
|
|
|
|
if (e->savefd != NULL) {
|
|
|
|
|
for (i = 0; i < NUFILE; i++) {
|
|
|
|
|
if (e->savefd[i] > 0)
|
|
|
|
|
close(e->savefd[i]);
|
2012-06-25 18:34:58 +02:00
|
|
|
|
#ifndef MKSH_LEGACY_MODE
|
2010-01-28 16:18:51 +01:00
|
|
|
|
/*
|
|
|
|
|
* keep all file descriptors > 2 private for ksh,
|
|
|
|
|
* but not for POSIX or legacy/kludge sh
|
|
|
|
|
*/
|
|
|
|
|
if (!Flag(FPOSIX) && !Flag(FSH) && i > 2 &&
|
|
|
|
|
e->savefd[i])
|
2005-05-23 05:06:10 +02:00
|
|
|
|
fcntl(i, F_SETFD, FD_CLOEXEC);
|
2012-06-25 18:17:54 +02:00
|
|
|
|
#endif
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
e->savefd = NULL;
|
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-07-31 13:11:25 +02:00
|
|
|
|
#if HAVE_MKNOD
|
2007-08-20 00:06:27 +02:00
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_mknod(const char **wp)
|
2005-10-21 14:41:56 +02:00
|
|
|
|
{
|
|
|
|
|
int argc, optc, rv = 0;
|
|
|
|
|
bool ismkfifo = false;
|
2007-03-04 01:13:17 +01:00
|
|
|
|
const char **argv;
|
2005-10-21 14:41:56 +02:00
|
|
|
|
void *set = NULL;
|
|
|
|
|
mode_t mode = 0, oldmode = 0;
|
|
|
|
|
|
2006-05-10 20:54:13 +02:00
|
|
|
|
while ((optc = ksh_getopt(wp, &builtin_opt, "m:")) != -1) {
|
2005-10-21 14:41:56 +02:00
|
|
|
|
switch (optc) {
|
|
|
|
|
case 'm':
|
|
|
|
|
set = setmode(builtin_opt.optarg);
|
|
|
|
|
if (set == NULL) {
|
|
|
|
|
bi_errorf("invalid file mode");
|
|
|
|
|
return (1);
|
|
|
|
|
}
|
2009-08-08 15:08:53 +02:00
|
|
|
|
mode = getmode(set, (mode_t)(DEFFILEMODE));
|
2011-03-05 22:43:18 +01:00
|
|
|
|
free_ossetmode(set);
|
2005-10-21 14:41:56 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
goto c_mknod_usage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
argv = &wp[builtin_opt.optind];
|
2009-05-16 19:33:10 +02:00
|
|
|
|
if (argv[0] == NULL)
|
2005-10-21 14:41:56 +02:00
|
|
|
|
goto c_mknod_usage;
|
|
|
|
|
for (argc = 0; argv[argc]; argc++)
|
|
|
|
|
;
|
|
|
|
|
if (argc == 2 && argv[1][0] == 'p')
|
|
|
|
|
ismkfifo = true;
|
|
|
|
|
else if (argc != 4 || (argv[1][0] != 'b' && argv[1][0] != 'c'))
|
|
|
|
|
goto c_mknod_usage;
|
|
|
|
|
|
|
|
|
|
if (set != NULL)
|
2009-08-08 15:08:53 +02:00
|
|
|
|
oldmode = umask((mode_t)0);
|
2005-10-21 14:41:56 +02:00
|
|
|
|
else
|
|
|
|
|
mode = DEFFILEMODE;
|
|
|
|
|
|
|
|
|
|
mode |= (argv[1][0] == 'b') ? S_IFBLK :
|
|
|
|
|
(argv[1][0] == 'c') ? S_IFCHR : 0;
|
|
|
|
|
|
|
|
|
|
if (!ismkfifo) {
|
2007-06-05 21:18:11 +02:00
|
|
|
|
unsigned long majnum, minnum;
|
2005-10-21 14:41:56 +02:00
|
|
|
|
dev_t dv;
|
|
|
|
|
char *c;
|
|
|
|
|
|
2007-06-05 21:18:11 +02:00
|
|
|
|
majnum = strtoul(argv[2], &c, 0);
|
2005-10-21 14:41:56 +02:00
|
|
|
|
if ((c == argv[2]) || (*c != '\0')) {
|
2010-08-28 20:50:58 +02:00
|
|
|
|
bi_errorf("non-numeric %s %s '%s'", "device", "major", argv[2]);
|
2005-10-21 14:41:56 +02:00
|
|
|
|
goto c_mknod_err;
|
|
|
|
|
}
|
2007-06-05 21:18:11 +02:00
|
|
|
|
minnum = strtoul(argv[3], &c, 0);
|
2005-10-21 14:41:56 +02:00
|
|
|
|
if ((c == argv[3]) || (*c != '\0')) {
|
2010-08-28 20:50:58 +02:00
|
|
|
|
bi_errorf("non-numeric %s %s '%s'", "device", "minor", argv[3]);
|
2005-10-21 14:41:56 +02:00
|
|
|
|
goto c_mknod_err;
|
|
|
|
|
}
|
2007-06-05 21:18:11 +02:00
|
|
|
|
dv = makedev(majnum, minnum);
|
2010-07-18 00:09:40 +02:00
|
|
|
|
if ((unsigned long)(major(dv)) != majnum) {
|
2010-08-28 20:50:58 +02:00
|
|
|
|
bi_errorf("%s %s too large: %lu", "device", "major", majnum);
|
2005-10-21 14:41:56 +02:00
|
|
|
|
goto c_mknod_err;
|
|
|
|
|
}
|
2010-07-18 00:09:40 +02:00
|
|
|
|
if ((unsigned long)(minor(dv)) != minnum) {
|
2010-08-28 20:50:58 +02:00
|
|
|
|
bi_errorf("%s %s too large: %lu", "device", "minor", minnum);
|
2005-10-21 14:41:56 +02:00
|
|
|
|
goto c_mknod_err;
|
|
|
|
|
}
|
|
|
|
|
if (mknod(argv[0], mode, dv))
|
|
|
|
|
goto c_mknod_failed;
|
|
|
|
|
} else if (mkfifo(argv[0], mode)) {
|
2006-08-01 15:43:28 +02:00
|
|
|
|
c_mknod_failed:
|
2012-12-18 00:18:11 +01:00
|
|
|
|
bi_errorf("%s: %s", argv[0], cstrerror(errno));
|
2006-08-01 15:43:28 +02:00
|
|
|
|
c_mknod_err:
|
2005-10-21 14:41:56 +02:00
|
|
|
|
rv = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (set)
|
|
|
|
|
umask(oldmode);
|
|
|
|
|
return (rv);
|
2006-08-01 15:43:28 +02:00
|
|
|
|
c_mknod_usage:
|
2010-08-28 20:50:58 +02:00
|
|
|
|
bi_errorf("%s: %s", "usage", "mknod [-m mode] name b|c major minor");
|
|
|
|
|
bi_errorf("%s: %s", "usage", "mknod [-m mode] name p");
|
2005-10-21 14:41:56 +02:00
|
|
|
|
return (1);
|
|
|
|
|
}
|
2006-11-09 23:18:10 +01:00
|
|
|
|
#endif
|
2005-10-21 14:41:56 +02:00
|
|
|
|
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/*-
|
2011-12-02 23:55:49 +01:00
|
|
|
|
test(1) roughly accepts the following grammar:
|
2005-05-23 05:06:10 +02:00
|
|
|
|
oexpr ::= aexpr | aexpr "-o" oexpr ;
|
|
|
|
|
aexpr ::= nexpr | nexpr "-a" aexpr ;
|
|
|
|
|
nexpr ::= primary | "!" nexpr ;
|
|
|
|
|
primary ::= unary-operator operand
|
|
|
|
|
| operand binary-operator operand
|
|
|
|
|
| operand
|
|
|
|
|
| "(" oexpr ")"
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
unary-operator ::= "-a"|"-r"|"-w"|"-x"|"-e"|"-f"|"-d"|"-c"|"-b"|"-p"|
|
|
|
|
|
"-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|
|
|
|
|
|
"-L"|"-h"|"-S"|"-H";
|
|
|
|
|
|
|
|
|
|
binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
|
|
|
|
|
"-nt"|"-ot"|"-ef"|
|
2011-12-02 23:55:49 +01:00
|
|
|
|
"<"|">" # rules used for [[ ... ]] expressions
|
2005-05-23 05:06:10 +02:00
|
|
|
|
;
|
2011-12-02 23:55:49 +01:00
|
|
|
|
operand ::= <anything>
|
2005-05-23 05:06:10 +02:00
|
|
|
|
*/
|
|
|
|
|
|
2011-01-22 21:33:14 +01:00
|
|
|
|
/* POSIX says > 1 for errors */
|
2011-12-02 23:55:49 +01:00
|
|
|
|
#define T_ERR_EXIT 2
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_test(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2011-11-30 22:34:15 +01:00
|
|
|
|
int argc, rv, invert = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
Test_env te;
|
2011-11-30 22:34:15 +01:00
|
|
|
|
Test_op op;
|
|
|
|
|
const char *lhs, **swp;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
te.flags = 0;
|
|
|
|
|
te.isa = ptest_isa;
|
|
|
|
|
te.getopnd = ptest_getopnd;
|
2007-05-13 20:49:00 +02:00
|
|
|
|
te.eval = test_eval;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
te.error = ptest_error;
|
|
|
|
|
|
|
|
|
|
for (argc = 0; wp[argc]; argc++)
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
if (strcmp(wp[0], "[") == 0) {
|
|
|
|
|
if (strcmp(wp[--argc], "]") != 0) {
|
|
|
|
|
bi_errorf("missing ]");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (T_ERR_EXIT);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
te.pos.wp = wp + 1;
|
|
|
|
|
te.wp_end = wp + argc;
|
|
|
|
|
|
|
|
|
|
/*
|
2011-11-30 22:34:15 +01:00
|
|
|
|
* Attempt to conform to POSIX special cases. This is pretty
|
2015-12-12 20:27:36 +01:00
|
|
|
|
* dumb code straight-forward from the 2008 spec, but unlike
|
2011-11-30 22:34:15 +01:00
|
|
|
|
* the old pdksh code doesn't live from so many assumptions.
|
2011-12-03 00:05:18 +01:00
|
|
|
|
* It does, though, inline some calls to '(*te.funcname)()'.
|
2005-05-23 05:06:10 +02:00
|
|
|
|
*/
|
2011-11-30 22:34:15 +01:00
|
|
|
|
switch (argc - 1) {
|
|
|
|
|
case 0:
|
|
|
|
|
return (1);
|
|
|
|
|
case 1:
|
|
|
|
|
ptest_one:
|
|
|
|
|
op = TO_STNZE;
|
|
|
|
|
goto ptest_unary;
|
|
|
|
|
case 2:
|
|
|
|
|
ptest_two:
|
2011-12-03 00:05:18 +01:00
|
|
|
|
if (ptest_isa(&te, TM_NOT)) {
|
2011-11-30 22:34:15 +01:00
|
|
|
|
++invert;
|
|
|
|
|
goto ptest_one;
|
2011-05-06 17:41:25 +02:00
|
|
|
|
}
|
2011-12-03 00:05:18 +01:00
|
|
|
|
if ((op = ptest_isa(&te, TM_UNOP))) {
|
2011-11-30 22:34:15 +01:00
|
|
|
|
ptest_unary:
|
2011-12-03 00:05:18 +01:00
|
|
|
|
rv = test_eval(&te, op, *te.pos.wp++, NULL, true);
|
2011-11-30 22:34:15 +01:00
|
|
|
|
ptest_out:
|
2015-12-12 20:27:36 +01:00
|
|
|
|
if (te.flags & TEF_ERROR)
|
|
|
|
|
return (T_ERR_EXIT);
|
2011-11-30 22:34:15 +01:00
|
|
|
|
return ((invert & 1) ? rv : !rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2011-11-30 22:34:15 +01:00
|
|
|
|
/* let the parser deal with anything else */
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
ptest_three:
|
|
|
|
|
swp = te.pos.wp;
|
2011-12-03 00:05:18 +01:00
|
|
|
|
/* use inside knowledge of ptest_getopnd inlined below */
|
|
|
|
|
lhs = *te.pos.wp++;
|
|
|
|
|
if ((op = ptest_isa(&te, TM_BINOP))) {
|
2011-12-02 23:55:49 +01:00
|
|
|
|
/* test lhs op rhs */
|
2011-12-03 00:05:18 +01:00
|
|
|
|
rv = test_eval(&te, op, lhs, *te.pos.wp++, true);
|
2011-11-30 22:34:15 +01:00
|
|
|
|
goto ptest_out;
|
|
|
|
|
}
|
|
|
|
|
/* back up to lhs */
|
|
|
|
|
te.pos.wp = swp;
|
2011-12-03 00:05:18 +01:00
|
|
|
|
if (ptest_isa(&te, TM_NOT)) {
|
2011-11-30 22:34:15 +01:00
|
|
|
|
++invert;
|
|
|
|
|
goto ptest_two;
|
|
|
|
|
}
|
2011-12-03 00:05:18 +01:00
|
|
|
|
if (ptest_isa(&te, TM_OPAREN)) {
|
2011-11-30 22:34:15 +01:00
|
|
|
|
swp = te.pos.wp;
|
|
|
|
|
/* skip operand, without evaluation */
|
2011-12-03 00:05:18 +01:00
|
|
|
|
te.pos.wp++;
|
2011-11-30 22:34:15 +01:00
|
|
|
|
/* check for closing parenthesis */
|
2011-12-03 00:05:18 +01:00
|
|
|
|
op = ptest_isa(&te, TM_CPAREN);
|
2011-11-30 22:34:15 +01:00
|
|
|
|
/* back up to operand */
|
|
|
|
|
te.pos.wp = swp;
|
|
|
|
|
/* if there was a closing paren, handle it */
|
|
|
|
|
if (op)
|
|
|
|
|
goto ptest_one;
|
|
|
|
|
/* backing up is done before calling the parser */
|
|
|
|
|
}
|
|
|
|
|
/* let the parser deal with it */
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
2011-12-03 00:05:18 +01:00
|
|
|
|
if (ptest_isa(&te, TM_NOT)) {
|
2011-11-30 22:34:15 +01:00
|
|
|
|
++invert;
|
|
|
|
|
goto ptest_three;
|
|
|
|
|
}
|
2011-12-03 00:05:18 +01:00
|
|
|
|
if (ptest_isa(&te, TM_OPAREN)) {
|
2011-11-30 22:34:15 +01:00
|
|
|
|
swp = te.pos.wp;
|
|
|
|
|
/* skip two operands, without evaluation */
|
2011-12-03 00:05:18 +01:00
|
|
|
|
te.pos.wp++;
|
|
|
|
|
te.pos.wp++;
|
2011-11-30 22:34:15 +01:00
|
|
|
|
/* check for closing parenthesis */
|
2011-12-03 00:05:18 +01:00
|
|
|
|
op = ptest_isa(&te, TM_CPAREN);
|
2011-11-30 22:34:15 +01:00
|
|
|
|
/* back up to first operand */
|
|
|
|
|
te.pos.wp = swp;
|
|
|
|
|
/* if there was a closing paren, handle it */
|
|
|
|
|
if (op)
|
|
|
|
|
goto ptest_two;
|
|
|
|
|
/* backing up is done before calling the parser */
|
|
|
|
|
}
|
|
|
|
|
/* defer this to the parser */
|
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-30 22:34:15 +01:00
|
|
|
|
/* "The results are unspecified." */
|
|
|
|
|
te.pos.wp = wp + 1;
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (test_parse(&te));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Generic test routines.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
Test_op
|
2005-10-08 21:31:00 +02:00
|
|
|
|
test_isop(Test_meta meta, const char *s)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
char sc1;
|
2005-10-08 21:31:00 +02:00
|
|
|
|
const struct t_op *tbl;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2005-10-08 21:31:00 +02:00
|
|
|
|
tbl = meta == TM_UNOP ? u_ops : b_ops;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (*s) {
|
|
|
|
|
sc1 = s[1];
|
2005-10-08 21:31:00 +02:00
|
|
|
|
for (; tbl->op_text[0]; tbl++)
|
|
|
|
|
if (sc1 == tbl->op_text[1] && !strcmp(s, tbl->op_text))
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (tbl->op_num);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (TO_NONOP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
|
2008-04-02 00:20:20 +02:00
|
|
|
|
bool do_eval)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2008-04-02 00:20:20 +02:00
|
|
|
|
int i, s;
|
2007-05-13 20:33:29 +02:00
|
|
|
|
size_t k;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
struct stat b1, b2;
|
2009-03-14 19:12:55 +01:00
|
|
|
|
mksh_ari_t v1, v2;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if (!do_eval)
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2012-12-08 19:30:31 +01:00
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
switch (op) {
|
|
|
|
|
/* Binary operators */
|
|
|
|
|
case TO_STEQL:
|
|
|
|
|
case TO_STNEQ:
|
|
|
|
|
case TO_STLT:
|
|
|
|
|
case TO_STGT:
|
|
|
|
|
case TO_INTEQ:
|
|
|
|
|
case TO_INTNE:
|
|
|
|
|
case TO_INTGT:
|
|
|
|
|
case TO_INTGE:
|
|
|
|
|
case TO_INTLT:
|
|
|
|
|
case TO_INTLE:
|
|
|
|
|
case TO_FILEQ:
|
|
|
|
|
case TO_FILNT:
|
|
|
|
|
case TO_FILOT:
|
|
|
|
|
/* consistency check, but does not happen in practice */
|
|
|
|
|
if (!opnd2) {
|
|
|
|
|
te->flags |= TEF_ERROR;
|
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2012-12-18 00:09:15 +01:00
|
|
|
|
default:
|
|
|
|
|
/* for completeness of switch */
|
|
|
|
|
break;
|
2012-12-08 19:30:31 +01:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-03-26 22:46:06 +01:00
|
|
|
|
switch (op) {
|
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
|
/*
|
|
|
|
|
* Unary Operators
|
|
|
|
|
*/
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -n */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_STNZE:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (*opnd1 != '\0');
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -z */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_STZER:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (*opnd1 == '\0');
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -o */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_OPTION:
|
2009-09-07 19:24:49 +02:00
|
|
|
|
if ((i = *opnd1) == '!' || i == '?')
|
2005-05-23 05:06:10 +02:00
|
|
|
|
opnd1++;
|
2007-05-13 20:33:29 +02:00
|
|
|
|
if ((k = option(opnd1)) == (size_t)-1)
|
2009-08-08 15:08:53 +02:00
|
|
|
|
return (0);
|
2009-09-07 19:24:49 +02:00
|
|
|
|
return (i == '?' ? 1 : i == '!' ? !Flag(k) : Flag(k));
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -r */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILRD:
|
2011-09-07 17:24:22 +02:00
|
|
|
|
/* LINTED use of access */
|
|
|
|
|
return (access(opnd1, R_OK) == 0);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -w */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILWR:
|
2011-09-07 17:24:22 +02:00
|
|
|
|
/* LINTED use of access */
|
|
|
|
|
return (access(opnd1, W_OK) == 0);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -x */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILEX:
|
2011-09-07 17:24:22 +02:00
|
|
|
|
return (ksh_access(opnd1, X_OK) == 0);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -a */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILAXST:
|
2011-03-26 22:46:06 +01:00
|
|
|
|
/* -e */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILEXST:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (stat(opnd1, &b1) == 0);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -r */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILREG:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (stat(opnd1, &b1) == 0 && S_ISREG(b1.st_mode));
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -d */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILID:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (stat(opnd1, &b1) == 0 && S_ISDIR(b1.st_mode));
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -c */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILCDEV:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (stat(opnd1, &b1) == 0 && S_ISCHR(b1.st_mode));
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -b */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILBDEV:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (stat(opnd1, &b1) == 0 && S_ISBLK(b1.st_mode));
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -p */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILFIFO:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (stat(opnd1, &b1) == 0 && S_ISFIFO(b1.st_mode));
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -h or -L */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILSYM:
|
2012-05-05 00:05:02 +02:00
|
|
|
|
#ifdef MKSH__NO_SYMLINK
|
|
|
|
|
return (0);
|
|
|
|
|
#else
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (lstat(opnd1, &b1) == 0 && S_ISLNK(b1.st_mode));
|
2012-05-05 00:05:02 +02:00
|
|
|
|
#endif
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -S */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILSOCK:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (stat(opnd1, &b1) == 0 && S_ISSOCK(b1.st_mode));
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -H => HP context dependent files (directories) */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILCDF:
|
2011-03-26 22:46:06 +01:00
|
|
|
|
#ifdef S_ISCDF
|
|
|
|
|
{
|
|
|
|
|
char *nv;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Append a + to filename and check to see if result is
|
|
|
|
|
* a setuid directory. CDF stuff in general is hookey,
|
|
|
|
|
* since it breaks for, e.g., the following sequence:
|
|
|
|
|
* echo hi >foo+; mkdir foo; echo bye >foo/default;
|
|
|
|
|
* chmod u+s foo (foo+ refers to the file with hi in it,
|
|
|
|
|
* there is no way to get at the file with bye in it;
|
|
|
|
|
* please correct me if I'm wrong about this).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
nv = shf_smprintf("%s+", opnd1);
|
2011-03-27 03:30:38 +02:00
|
|
|
|
i = (stat(nv, &b1) == 0 && S_ISCDF(b1.st_mode));
|
|
|
|
|
afree(nv, ATEMP);
|
|
|
|
|
return (i);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
}
|
|
|
|
|
#else
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (0);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* -u */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILSETU:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (stat(opnd1, &b1) == 0 &&
|
|
|
|
|
(b1.st_mode & S_ISUID) == S_ISUID);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -g */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILSETG:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (stat(opnd1, &b1) == 0 &&
|
|
|
|
|
(b1.st_mode & S_ISGID) == S_ISGID);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -k */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILSTCK:
|
2006-08-24 22:32:53 +02:00
|
|
|
|
#ifdef S_ISVTX
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (stat(opnd1, &b1) == 0 &&
|
|
|
|
|
(b1.st_mode & S_ISVTX) == S_ISVTX);
|
2006-08-24 22:32:53 +02:00
|
|
|
|
#else
|
|
|
|
|
return (0);
|
|
|
|
|
#endif
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -s */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILGZ:
|
2012-03-29 21:23:01 +02:00
|
|
|
|
return (stat(opnd1, &b1) == 0 && (off_t)b1.st_size > (off_t)0);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -t */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILTT:
|
2007-05-13 20:33:29 +02:00
|
|
|
|
if (opnd1 && !bi_getn(opnd1, &i)) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
te->flags |= TEF_ERROR;
|
2007-05-13 20:33:29 +02:00
|
|
|
|
i = 0;
|
2005-07-04 14:27:28 +02:00
|
|
|
|
} else
|
2007-05-13 20:33:29 +02:00
|
|
|
|
i = isatty(opnd1 ? i : 0);
|
|
|
|
|
return (i);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -O */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILUID:
|
2011-11-19 18:42:24 +01:00
|
|
|
|
return (stat(opnd1, &b1) == 0 && (uid_t)b1.st_uid == ksheuid);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -G */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILGID:
|
2011-11-19 18:42:24 +01:00
|
|
|
|
return (stat(opnd1, &b1) == 0 && (gid_t)b1.st_gid == getegid());
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
|
/*
|
|
|
|
|
* Binary Operators
|
|
|
|
|
*/
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* = */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_STEQL:
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (te->flags & TEF_DBRACKET)
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (gmatchx(opnd1, opnd2, false));
|
|
|
|
|
return (strcmp(opnd1, opnd2) == 0);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* != */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_STNEQ:
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (te->flags & TEF_DBRACKET)
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (!gmatchx(opnd1, opnd2, false));
|
|
|
|
|
return (strcmp(opnd1, opnd2) != 0);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* < */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_STLT:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (strcmp(opnd1, opnd2) < 0);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* > */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_STGT:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (strcmp(opnd1, opnd2) > 0);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -eq */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_INTEQ:
|
2011-03-26 22:46:06 +01:00
|
|
|
|
/* -ne */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_INTNE:
|
2011-03-26 22:46:06 +01:00
|
|
|
|
/* -ge */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_INTGE:
|
2011-03-26 22:46:06 +01:00
|
|
|
|
/* -gt */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_INTGT:
|
2011-03-26 22:46:06 +01:00
|
|
|
|
/* -le */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_INTLE:
|
2011-03-26 22:46:06 +01:00
|
|
|
|
/* -lt */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_INTLT:
|
2008-04-02 00:20:20 +02:00
|
|
|
|
if (!evaluate(opnd1, &v1, KSH_RETURN_ERROR, false) ||
|
|
|
|
|
!evaluate(opnd2, &v2, KSH_RETURN_ERROR, false)) {
|
|
|
|
|
/* error already printed.. */
|
|
|
|
|
te->flags |= TEF_ERROR;
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2011-03-26 22:46:06 +01:00
|
|
|
|
switch (op) {
|
2008-04-02 00:20:20 +02:00
|
|
|
|
case TO_INTEQ:
|
|
|
|
|
return (v1 == v2);
|
|
|
|
|
case TO_INTNE:
|
|
|
|
|
return (v1 != v2);
|
|
|
|
|
case TO_INTGE:
|
|
|
|
|
return (v1 >= v2);
|
|
|
|
|
case TO_INTGT:
|
|
|
|
|
return (v1 > v2);
|
|
|
|
|
case TO_INTLE:
|
|
|
|
|
return (v1 <= v2);
|
|
|
|
|
case TO_INTLT:
|
|
|
|
|
return (v1 < v2);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
default:
|
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2011-03-26 22:46:06 +01:00
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
|
|
|
|
|
/* -nt */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILNT:
|
|
|
|
|
/*
|
|
|
|
|
* ksh88/ksh93 succeed if file2 can't be stated
|
2008-04-02 00:20:20 +02:00
|
|
|
|
* (subtly different from 'does not exist').
|
|
|
|
|
*/
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (stat(opnd1, &b1) == 0 &&
|
2008-04-02 00:20:20 +02:00
|
|
|
|
(((s = stat(opnd2, &b2)) == 0 &&
|
2009-06-08 22:06:50 +02:00
|
|
|
|
b1.st_mtime > b2.st_mtime) || s < 0));
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -ot */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILOT:
|
|
|
|
|
/*
|
|
|
|
|
* ksh88/ksh93 succeed if file1 can't be stated
|
2008-04-02 00:20:20 +02:00
|
|
|
|
* (subtly different from 'does not exist').
|
|
|
|
|
*/
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (stat(opnd2, &b2) == 0 &&
|
2008-04-02 00:20:20 +02:00
|
|
|
|
(((s = stat(opnd1, &b1)) == 0 &&
|
2009-06-08 22:06:50 +02:00
|
|
|
|
b1.st_mtime < b2.st_mtime) || s < 0));
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* -ef */
|
2011-01-22 21:33:14 +01:00
|
|
|
|
case TO_FILEQ:
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0 &&
|
|
|
|
|
b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino);
|
2011-03-26 22:46:06 +01:00
|
|
|
|
|
|
|
|
|
/* all other cases */
|
|
|
|
|
case TO_NONOP:
|
|
|
|
|
case TO_NONNULL:
|
|
|
|
|
/* throw the error */
|
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
(*te->error)(te, 0, "internal error: unknown op");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
test_parse(Test_env *te)
|
|
|
|
|
{
|
2008-04-02 00:20:20 +02:00
|
|
|
|
int rv;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2008-04-02 00:20:20 +02:00
|
|
|
|
rv = test_oexpr(te, 1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if (!(te->flags & TEF_ERROR) && !(*te->isa)(te, TM_END))
|
|
|
|
|
(*te->error)(te, 0, "unexpected operator/operand");
|
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return ((te->flags & TEF_ERROR) ? T_ERR_EXIT : !rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2008-04-02 00:20:20 +02:00
|
|
|
|
test_oexpr(Test_env *te, bool do_eval)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2008-04-02 00:20:20 +02:00
|
|
|
|
int rv;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2008-04-02 00:20:20 +02:00
|
|
|
|
if ((rv = test_aexpr(te, do_eval)))
|
|
|
|
|
do_eval = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_OR))
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (test_oexpr(te, do_eval) || rv);
|
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2008-04-02 00:20:20 +02:00
|
|
|
|
test_aexpr(Test_env *te, bool do_eval)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2008-04-02 00:20:20 +02:00
|
|
|
|
int rv;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2008-04-02 00:20:20 +02:00
|
|
|
|
if (!(rv = test_nexpr(te, do_eval)))
|
|
|
|
|
do_eval = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_AND))
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (test_aexpr(te, do_eval) && rv);
|
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2008-04-02 00:20:20 +02:00
|
|
|
|
test_nexpr(Test_env *te, bool do_eval)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_NOT))
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (!test_nexpr(te, do_eval));
|
|
|
|
|
return (test_primary(te, do_eval));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2008-04-02 00:20:20 +02:00
|
|
|
|
test_primary(Test_env *te, bool do_eval)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
const char *opnd1, *opnd2;
|
2008-04-02 00:20:20 +02:00
|
|
|
|
int rv;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
Test_op op;
|
|
|
|
|
|
|
|
|
|
if (te->flags & TEF_ERROR)
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if ((*te->isa)(te, TM_OPAREN)) {
|
2008-04-02 00:20:20 +02:00
|
|
|
|
rv = test_oexpr(te, do_eval);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (te->flags & TEF_ERROR)
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
if (!(*te->isa)(te, TM_CPAREN)) {
|
2010-08-28 20:50:58 +02:00
|
|
|
|
(*te->error)(te, 0, "missing )");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2009-03-22 18:58:58 +01:00
|
|
|
|
/*
|
|
|
|
|
* Binary should have precedence over unary in this case
|
|
|
|
|
* so that something like test \( -f = -f \) is accepted
|
|
|
|
|
*/
|
|
|
|
|
if ((te->flags & TEF_DBRACKET) || (&te->pos.wp[1] < te->wp_end &&
|
2009-03-22 19:28:35 +01:00
|
|
|
|
!test_isop(TM_BINOP, te->pos.wp[1]))) {
|
2009-03-22 18:58:58 +01:00
|
|
|
|
if ((op = (*te->isa)(te, TM_UNOP))) {
|
|
|
|
|
/* unary expression */
|
|
|
|
|
opnd1 = (*te->getopnd)(te, op, do_eval);
|
|
|
|
|
if (!opnd1) {
|
|
|
|
|
(*te->error)(te, -1, "missing argument");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (0);
|
2009-03-22 18:58:58 +01:00
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return ((*te->eval)(te, op, opnd1, NULL, do_eval));
|
2009-03-22 18:58:58 +01:00
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
opnd1 = (*te->getopnd)(te, TO_NONOP, do_eval);
|
|
|
|
|
if (!opnd1) {
|
|
|
|
|
(*te->error)(te, 0, "expression expected");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2008-03-28 19:46:59 +01:00
|
|
|
|
if ((op = (*te->isa)(te, TM_BINOP))) {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
/* binary expression */
|
|
|
|
|
opnd2 = (*te->getopnd)(te, op, do_eval);
|
|
|
|
|
if (!opnd2) {
|
|
|
|
|
(*te->error)(te, -1, "missing second argument");
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return ((*te->eval)(te, op, opnd1, opnd2, do_eval));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return ((*te->eval)(te, TO_STNZE, opnd1, NULL, do_eval));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Plain test (test and [ .. ]) specific routines.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-18 00:09:40 +02:00
|
|
|
|
/*
|
|
|
|
|
* Test if the current token is a whatever. Accepts the current token if
|
2009-06-10 20:12:51 +02:00
|
|
|
|
* it is. Returns 0 if it is not, non-zero if it is (in the case of
|
2005-05-23 05:06:10 +02:00
|
|
|
|
* TM_UNOP and TM_BINOP, the returned value is a Test_op).
|
|
|
|
|
*/
|
2010-07-18 00:09:40 +02:00
|
|
|
|
static Test_op
|
2005-05-23 05:06:10 +02:00
|
|
|
|
ptest_isa(Test_env *te, Test_meta meta)
|
|
|
|
|
{
|
|
|
|
|
/* Order important - indexed by Test_meta values */
|
2012-12-28 03:28:39 +01:00
|
|
|
|
static const char * const tokens[] = {
|
2005-05-23 05:06:10 +02:00
|
|
|
|
"-o", "-a", "!", "(", ")"
|
|
|
|
|
};
|
2010-07-18 00:09:40 +02:00
|
|
|
|
Test_op rv;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if (te->pos.wp >= te->wp_end)
|
2010-07-18 00:09:40 +02:00
|
|
|
|
return (meta == TM_END ? TO_NONNULL : TO_NONOP);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
if (meta == TM_UNOP || meta == TM_BINOP)
|
2008-04-02 00:20:20 +02:00
|
|
|
|
rv = test_isop(meta, *te->pos.wp);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
else if (meta == TM_END)
|
2010-07-18 00:09:40 +02:00
|
|
|
|
rv = TO_NONOP;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
else
|
2010-07-18 00:09:40 +02:00
|
|
|
|
rv = !strcmp(*te->pos.wp, tokens[(int)meta]) ?
|
|
|
|
|
TO_NONNULL : TO_NONOP;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
/* Accept the token? */
|
2010-07-18 00:09:40 +02:00
|
|
|
|
if (rv != TO_NONOP)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
te->pos.wp++;
|
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (rv);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const char *
|
2009-12-12 23:27:10 +01:00
|
|
|
|
ptest_getopnd(Test_env *te, Test_op op, bool do_eval MKSH_A_UNUSED)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
if (te->pos.wp >= te->wp_end)
|
2009-06-08 22:06:50 +02:00
|
|
|
|
return (op == TO_FILTT ? "1" : NULL);
|
|
|
|
|
return (*te->pos.wp++);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2008-04-02 00:20:20 +02:00
|
|
|
|
ptest_error(Test_env *te, int ofs, const char *msg)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2008-04-02 00:20:20 +02:00
|
|
|
|
const char *op;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
|
|
te->flags |= TEF_ERROR;
|
2008-04-02 00:20:20 +02:00
|
|
|
|
if ((op = te->pos.wp + ofs >= te->wp_end ? NULL : te->pos.wp[ofs]))
|
2005-05-23 05:06:10 +02:00
|
|
|
|
bi_errorf("%s: %s", op, msg);
|
|
|
|
|
else
|
|
|
|
|
bi_errorf("%s", msg);
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-08 22:52:28 +02:00
|
|
|
|
#ifndef MKSH_NO_LIMITS
|
2005-05-23 05:06:10 +02:00
|
|
|
|
#define SOFT 0x1
|
|
|
|
|
#define HARD 0x2
|
|
|
|
|
|
2010-03-27 17:51:39 +01:00
|
|
|
|
/* Magic to divine the 'm' and 'v' limits */
|
|
|
|
|
|
|
|
|
|
#ifdef RLIMIT_AS
|
|
|
|
|
#if !defined(RLIMIT_VMEM) || (RLIMIT_VMEM == RLIMIT_AS) || \
|
|
|
|
|
!defined(RLIMIT_RSS) || (RLIMIT_VMEM == RLIMIT_RSS)
|
|
|
|
|
#define ULIMIT_V_IS_AS
|
|
|
|
|
#elif defined(RLIMIT_VMEM)
|
|
|
|
|
#if !defined(RLIMIT_RSS) || (RLIMIT_RSS == RLIMIT_AS)
|
|
|
|
|
#define ULIMIT_V_IS_AS
|
|
|
|
|
#else
|
|
|
|
|
#define ULIMIT_V_IS_VMEM
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef RLIMIT_RSS
|
|
|
|
|
#ifdef ULIMIT_V_IS_VMEM
|
|
|
|
|
#define ULIMIT_M_IS_RSS
|
|
|
|
|
#elif defined(RLIMIT_VMEM) && (RLIMIT_VMEM == RLIMIT_RSS)
|
|
|
|
|
#define ULIMIT_M_IS_VMEM
|
|
|
|
|
#else
|
|
|
|
|
#define ULIMIT_M_IS_RSS
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(ULIMIT_M_IS_RSS) && defined(RLIMIT_AS) && (RLIMIT_RSS == RLIMIT_AS)
|
|
|
|
|
#undef ULIMIT_M_IS_RSS
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if !defined(RLIMIT_AS) && !defined(ULIMIT_M_IS_VMEM) && defined(RLIMIT_VMEM)
|
|
|
|
|
#define ULIMIT_V_IS_VMEM
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if !defined(ULIMIT_V_IS_VMEM) && defined(RLIMIT_VMEM) && \
|
|
|
|
|
(!defined(RLIMIT_RSS) || (defined(RLIMIT_AS) && (RLIMIT_RSS == RLIMIT_AS)))
|
|
|
|
|
#define ULIMIT_M_IS_VMEM
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(ULIMIT_M_IS_VMEM) && defined(RLIMIT_AS) && \
|
|
|
|
|
(RLIMIT_VMEM == RLIMIT_AS)
|
|
|
|
|
#undef ULIMIT_M_IS_VMEM
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-11-17 23:21:18 +01:00
|
|
|
|
#if defined(ULIMIT_M_IS_RSS) && defined(ULIMIT_M_IS_VMEM)
|
|
|
|
|
# error nonsensical m ulimit
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(ULIMIT_V_IS_VMEM) && defined(ULIMIT_V_IS_AS)
|
|
|
|
|
# error nonsensical v ulimit
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-05-02 01:16:31 +02:00
|
|
|
|
struct limits {
|
|
|
|
|
/* limit resource */
|
|
|
|
|
int resource;
|
|
|
|
|
/* multiply by to get rlim_{cur,max} values */
|
|
|
|
|
unsigned int factor;
|
|
|
|
|
/* getopts char */
|
|
|
|
|
char optchar;
|
|
|
|
|
/* limit name */
|
|
|
|
|
char name[1];
|
|
|
|
|
};
|
|
|
|
|
|
2013-11-17 23:21:18 +01:00
|
|
|
|
#define RLIMITS_DEFNS
|
2015-05-02 01:16:31 +02:00
|
|
|
|
#define FN(lname,lid,lfac,lopt) \
|
|
|
|
|
static const struct { \
|
|
|
|
|
int resource; \
|
|
|
|
|
unsigned int factor; \
|
|
|
|
|
char optchar; \
|
|
|
|
|
char name[sizeof(lname)]; \
|
|
|
|
|
} rlimits_ ## lid = { \
|
|
|
|
|
lid, lfac, lopt, lname \
|
|
|
|
|
};
|
2013-11-17 23:21:18 +01:00
|
|
|
|
#include "rlimits.gen"
|
|
|
|
|
|
|
|
|
|
static void print_ulimit(const struct limits *, int);
|
|
|
|
|
static int set_ulimit(const struct limits *, const char *, int);
|
|
|
|
|
|
|
|
|
|
static const struct limits * const rlimits[] = {
|
|
|
|
|
#define RLIMITS_ITEMS
|
|
|
|
|
#include "rlimits.gen"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const char rlimits_opts[] =
|
|
|
|
|
#define RLIMITS_OPTCS
|
|
|
|
|
#include "rlimits.gen"
|
|
|
|
|
;
|
2010-03-27 17:51:39 +01:00
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
|
int
|
2007-03-04 01:13:17 +01:00
|
|
|
|
c_ulimit(const char **wp)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
{
|
2013-11-17 23:21:18 +01:00
|
|
|
|
size_t i = 0;
|
2008-04-22 20:58:20 +02:00
|
|
|
|
int how = SOFT | HARD, optc, what = 'f';
|
|
|
|
|
bool all = false;
|
2008-04-02 00:20:20 +02:00
|
|
|
|
|
2013-11-17 23:21:18 +01:00
|
|
|
|
while ((optc = ksh_getopt(wp, &builtin_opt, rlimits_opts)) != -1)
|
2005-05-23 05:06:10 +02:00
|
|
|
|
switch (optc) {
|
|
|
|
|
case 'H':
|
|
|
|
|
how = HARD;
|
|
|
|
|
break;
|
|
|
|
|
case 'S':
|
|
|
|
|
how = SOFT;
|
|
|
|
|
break;
|
|
|
|
|
case 'a':
|
2008-04-02 00:20:20 +02:00
|
|
|
|
all = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
break;
|
|
|
|
|
case '?':
|
2013-11-17 23:21:18 +01:00
|
|
|
|
bi_errorf("usage: ulimit [-%s] [value]", rlimits_opts);
|
2008-04-16 23:56:03 +02:00
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
default:
|
2008-04-20 23:30:29 +02:00
|
|
|
|
what = optc;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-17 23:21:18 +01:00
|
|
|
|
while (i < NELEM(rlimits)) {
|
|
|
|
|
if (rlimits[i]->optchar == what)
|
|
|
|
|
goto found;
|
|
|
|
|
++i;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2013-11-17 23:21:18 +01:00
|
|
|
|
internal_warningf("ulimit: %c", what);
|
|
|
|
|
return (1);
|
|
|
|
|
found:
|
2008-04-22 20:58:20 +02:00
|
|
|
|
if (wp[builtin_opt.optind]) {
|
|
|
|
|
if (all || wp[builtin_opt.optind + 1]) {
|
2008-04-20 23:30:29 +02:00
|
|
|
|
bi_errorf("too many arguments");
|
2008-04-16 23:56:03 +02:00
|
|
|
|
return (1);
|
2008-04-20 23:30:29 +02:00
|
|
|
|
}
|
2013-11-17 23:21:18 +01:00
|
|
|
|
return (set_ulimit(rlimits[i], wp[builtin_opt.optind], how));
|
2008-04-20 23:30:29 +02:00
|
|
|
|
}
|
2008-04-22 20:58:20 +02:00
|
|
|
|
if (!all)
|
2013-11-17 23:21:18 +01:00
|
|
|
|
print_ulimit(rlimits[i], how);
|
|
|
|
|
else for (i = 0; i < NELEM(rlimits); ++i) {
|
|
|
|
|
shprintf("%-20s ", rlimits[i]->name);
|
|
|
|
|
print_ulimit(rlimits[i], how);
|
2008-04-22 20:58:20 +02:00
|
|
|
|
}
|
|
|
|
|
return (0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
set_ulimit(const struct limits *l, const char *v, int how)
|
|
|
|
|
{
|
|
|
|
|
rlim_t val = (rlim_t)0;
|
|
|
|
|
struct rlimit limit;
|
|
|
|
|
|
|
|
|
|
if (strcmp(v, "unlimited") == 0)
|
|
|
|
|
val = (rlim_t)RLIM_INFINITY;
|
|
|
|
|
else {
|
2012-03-26 23:10:44 +02:00
|
|
|
|
mksh_uari_t rval;
|
2008-04-22 20:58:20 +02:00
|
|
|
|
|
2012-03-26 23:10:44 +02:00
|
|
|
|
if (!evaluate(v, (mksh_ari_t *)&rval, KSH_RETURN_ERROR, false))
|
2008-04-22 20:58:20 +02:00
|
|
|
|
return (1);
|
|
|
|
|
/*
|
|
|
|
|
* Avoid problems caused by typos that evaluate misses due
|
|
|
|
|
* to evaluating unset parameters to 0...
|
|
|
|
|
* If this causes problems, will have to add parameter to
|
|
|
|
|
* evaluate() to control if unset params are 0 or an error.
|
|
|
|
|
*/
|
|
|
|
|
if (!rval && !ksh_isdigit(v[0])) {
|
|
|
|
|
bi_errorf("invalid %s limit: %s", l->name, v);
|
|
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2008-04-22 20:58:20 +02:00
|
|
|
|
val = (rlim_t)((rlim_t)rval * l->factor);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2008-04-22 20:58:20 +02:00
|
|
|
|
|
2008-04-16 23:56:03 +02:00
|
|
|
|
if (getrlimit(l->resource, &limit) < 0) {
|
2013-11-17 23:20:21 +01:00
|
|
|
|
#ifndef MKSH_SMALL
|
|
|
|
|
bi_errorf("limit %s could not be read, contact the mksh developers: %s",
|
|
|
|
|
l->name, cstrerror(errno));
|
|
|
|
|
#endif
|
|
|
|
|
/* some can't be read */
|
2008-04-16 23:56:03 +02:00
|
|
|
|
limit.rlim_cur = RLIM_INFINITY;
|
|
|
|
|
limit.rlim_max = RLIM_INFINITY;
|
|
|
|
|
}
|
|
|
|
|
if (how & SOFT)
|
|
|
|
|
limit.rlim_cur = val;
|
|
|
|
|
if (how & HARD)
|
|
|
|
|
limit.rlim_max = val;
|
2008-04-22 20:58:20 +02:00
|
|
|
|
if (!setrlimit(l->resource, &limit))
|
|
|
|
|
return (0);
|
|
|
|
|
if (errno == EPERM)
|
|
|
|
|
bi_errorf("%s exceeds allowable %s limit", v, l->name);
|
|
|
|
|
else
|
2012-12-18 00:18:11 +01:00
|
|
|
|
bi_errorf("bad %s limit: %s", l->name, cstrerror(errno));
|
2008-04-22 20:58:20 +02:00
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
print_ulimit(const struct limits *l, int how)
|
|
|
|
|
{
|
|
|
|
|
rlim_t val = (rlim_t)0;
|
|
|
|
|
struct rlimit limit;
|
|
|
|
|
|
|
|
|
|
if (getrlimit(l->resource, &limit)) {
|
|
|
|
|
shf_puts("unknown\n", shl_stdout);
|
|
|
|
|
return;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2008-04-22 20:58:20 +02:00
|
|
|
|
if (how & SOFT)
|
|
|
|
|
val = limit.rlim_cur;
|
|
|
|
|
else if (how & HARD)
|
|
|
|
|
val = limit.rlim_max;
|
2009-12-01 14:12:30 +01:00
|
|
|
|
if (val == (rlim_t)RLIM_INFINITY)
|
2008-04-22 20:58:20 +02:00
|
|
|
|
shf_puts("unlimited\n", shl_stdout);
|
|
|
|
|
else
|
2012-03-26 23:10:44 +02:00
|
|
|
|
shprintf("%lu\n", (unsigned long)(val / l->factor));
|
2005-05-23 05:06:10 +02:00
|
|
|
|
}
|
2009-04-03 11:45:23 +02:00
|
|
|
|
#endif
|
2007-08-20 00:06:27 +02:00
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
c_rename(const char **wp)
|
|
|
|
|
{
|
|
|
|
|
int rv = 1;
|
|
|
|
|
|
2010-09-05 21:51:35 +02:00
|
|
|
|
/* skip argv[0] */
|
|
|
|
|
++wp;
|
|
|
|
|
if (wp[0] && !strcmp(wp[0], "--"))
|
|
|
|
|
/* skip "--" (options separator) */
|
|
|
|
|
++wp;
|
|
|
|
|
|
|
|
|
|
/* check for exactly two arguments */
|
|
|
|
|
if (wp[0] == NULL /* first argument */ ||
|
|
|
|
|
wp[1] == NULL /* second argument */ ||
|
|
|
|
|
wp[2] != NULL /* no further args please */)
|
2011-09-07 17:24:22 +02:00
|
|
|
|
bi_errorf(Tsynerr);
|
2010-09-05 21:51:35 +02:00
|
|
|
|
else if ((rv = rename(wp[0], wp[1])) != 0) {
|
2007-08-20 00:06:27 +02:00
|
|
|
|
rv = errno;
|
2012-12-18 00:18:11 +01:00
|
|
|
|
bi_errorf("%s: %s", "failed", cstrerror(rv));
|
2007-08-20 00:06:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (rv);
|
|
|
|
|
}
|
2008-05-17 20:27:57 +02:00
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
c_realpath(const char **wp)
|
|
|
|
|
{
|
|
|
|
|
int rv = 1;
|
2009-08-30 15:22:39 +02:00
|
|
|
|
char *buf;
|
2008-05-17 20:27:57 +02:00
|
|
|
|
|
2010-09-05 21:51:35 +02:00
|
|
|
|
/* skip argv[0] */
|
|
|
|
|
++wp;
|
|
|
|
|
if (wp[0] && !strcmp(wp[0], "--"))
|
|
|
|
|
/* skip "--" (options separator) */
|
|
|
|
|
++wp;
|
2008-05-17 20:27:57 +02:00
|
|
|
|
|
2010-09-05 21:51:35 +02:00
|
|
|
|
/* check for exactly one argument */
|
|
|
|
|
if (wp[0] == NULL || wp[1] != NULL)
|
2011-09-07 17:24:22 +02:00
|
|
|
|
bi_errorf(Tsynerr);
|
2010-09-05 21:51:35 +02:00
|
|
|
|
else if ((buf = do_realpath(wp[0])) == NULL) {
|
2009-08-30 15:22:39 +02:00
|
|
|
|
rv = errno;
|
2012-12-18 00:18:11 +01:00
|
|
|
|
bi_errorf("%s: %s", wp[0], cstrerror(rv));
|
2009-08-30 15:22:39 +02:00
|
|
|
|
if ((unsigned int)rv > 255)
|
|
|
|
|
rv = 255;
|
|
|
|
|
} else {
|
|
|
|
|
shprintf("%s\n", buf);
|
2008-05-17 20:27:57 +02:00
|
|
|
|
afree(buf, ATEMP);
|
2010-09-05 21:51:35 +02:00
|
|
|
|
rv = 0;
|
2008-05-17 20:27:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (rv);
|
|
|
|
|
}
|
2010-08-28 19:21:46 +02:00
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
c_cat(const char **wp)
|
|
|
|
|
{
|
2016-01-20 21:29:48 +01:00
|
|
|
|
int fd = STDIN_FILENO, rv;
|
2010-08-28 19:21:46 +02:00
|
|
|
|
ssize_t n, w;
|
|
|
|
|
const char *fn = "<stdin>";
|
|
|
|
|
char *buf, *cp;
|
2016-01-20 21:29:48 +01:00
|
|
|
|
int opipe = 0;
|
2010-08-28 19:21:46 +02:00
|
|
|
|
#define MKSH_CAT_BUFSIZ 4096
|
|
|
|
|
|
2011-02-18 23:26:13 +01:00
|
|
|
|
/* parse options: POSIX demands we support "-u" as no-op */
|
|
|
|
|
while ((rv = ksh_getopt(wp, &builtin_opt, "u")) != -1) {
|
|
|
|
|
switch (rv) {
|
|
|
|
|
case 'u':
|
|
|
|
|
/* we already operate unbuffered */
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2011-09-07 17:24:22 +02:00
|
|
|
|
bi_errorf(Tsynerr);
|
2011-02-18 23:26:13 +01:00
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
wp += builtin_opt.optind;
|
|
|
|
|
rv = 0;
|
2010-08-28 19:21:46 +02:00
|
|
|
|
|
2012-06-24 22:00:51 +02:00
|
|
|
|
if ((buf = malloc_osfunc(MKSH_CAT_BUFSIZ)) == NULL) {
|
2013-06-04 00:28:33 +02:00
|
|
|
|
bi_errorf(Toomem, (size_t)MKSH_CAT_BUFSIZ);
|
2012-06-24 22:00:51 +02:00
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 21:29:48 +01:00
|
|
|
|
/* catch SIGPIPE */
|
|
|
|
|
opipe = block_pipe();
|
|
|
|
|
|
2010-08-28 19:21:46 +02:00
|
|
|
|
do {
|
|
|
|
|
if (*wp) {
|
|
|
|
|
fn = *wp++;
|
2015-04-29 20:32:44 +02:00
|
|
|
|
if (ksh_isdash(fn))
|
2010-08-28 19:21:46 +02:00
|
|
|
|
fd = STDIN_FILENO;
|
2015-07-09 22:52:43 +02:00
|
|
|
|
else if ((fd = binopen2(fn, O_RDONLY)) < 0) {
|
2016-01-20 21:29:48 +01:00
|
|
|
|
bi_errorf("%s: %s", fn, cstrerror(errno));
|
2010-08-28 19:21:46 +02:00
|
|
|
|
rv = 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-13 02:20:25 +01:00
|
|
|
|
while (/* CONSTCOND */ 1) {
|
2016-01-20 21:29:48 +01:00
|
|
|
|
if ((n = blocking_read(fd, (cp = buf),
|
|
|
|
|
MKSH_CAT_BUFSIZ)) == -1) {
|
|
|
|
|
if (errno == EINTR) {
|
|
|
|
|
restore_pipe(opipe);
|
|
|
|
|
/* give the user a chance to ^C out */
|
|
|
|
|
intrcheck();
|
2010-08-28 19:21:46 +02:00
|
|
|
|
/* interrupted, try again */
|
2016-01-20 21:29:48 +01:00
|
|
|
|
opipe = block_pipe();
|
2010-08-28 19:21:46 +02:00
|
|
|
|
continue;
|
2011-07-05 21:56:24 +02:00
|
|
|
|
}
|
2010-08-28 19:21:46 +02:00
|
|
|
|
/* an error occured during reading */
|
2016-01-20 21:29:48 +01:00
|
|
|
|
bi_errorf("%s: %s", fn, cstrerror(errno));
|
2010-08-28 19:21:46 +02:00
|
|
|
|
rv = 1;
|
|
|
|
|
break;
|
|
|
|
|
} else if (n == 0)
|
|
|
|
|
/* end of file reached */
|
|
|
|
|
break;
|
|
|
|
|
while (n) {
|
2016-01-20 21:29:48 +01:00
|
|
|
|
if ((w = write(STDOUT_FILENO, cp, n)) != -1) {
|
|
|
|
|
n -= w;
|
|
|
|
|
cp += w;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (errno == EINTR) {
|
|
|
|
|
restore_pipe(opipe);
|
|
|
|
|
/* give the user a chance to ^C out */
|
|
|
|
|
intrcheck();
|
|
|
|
|
/* interrupted, try again */
|
|
|
|
|
opipe = block_pipe();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (errno == EPIPE) {
|
|
|
|
|
/* fake receiving signel */
|
|
|
|
|
rv = ksh_sigmask(SIGPIPE);
|
|
|
|
|
} else {
|
2010-08-28 19:21:46 +02:00
|
|
|
|
/* an error occured during writing */
|
|
|
|
|
bi_errorf("%s: %s", "<stdout>",
|
2016-01-20 21:29:48 +01:00
|
|
|
|
cstrerror(errno));
|
2010-08-28 19:21:46 +02:00
|
|
|
|
rv = 1;
|
|
|
|
|
}
|
2016-01-20 21:29:48 +01:00
|
|
|
|
if (fd != STDIN_FILENO)
|
|
|
|
|
close(fd);
|
|
|
|
|
goto out;
|
2010-08-28 19:21:46 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (fd != STDIN_FILENO)
|
|
|
|
|
close(fd);
|
|
|
|
|
} while (*wp);
|
|
|
|
|
|
|
|
|
|
out:
|
2016-01-20 21:29:48 +01:00
|
|
|
|
restore_pipe(opipe);
|
2011-03-05 22:48:09 +01:00
|
|
|
|
free_osfunc(buf);
|
2010-08-28 19:21:46 +02:00
|
|
|
|
return (rv);
|
|
|
|
|
}
|
2011-02-11 01:41:38 +01:00
|
|
|
|
|
|
|
|
|
#if HAVE_SELECT
|
|
|
|
|
int
|
|
|
|
|
c_sleep(const char **wp)
|
|
|
|
|
{
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
int rv = 1;
|
|
|
|
|
|
|
|
|
|
/* skip argv[0] */
|
|
|
|
|
++wp;
|
|
|
|
|
if (wp[0] && !strcmp(wp[0], "--"))
|
|
|
|
|
/* skip "--" (options separator) */
|
|
|
|
|
++wp;
|
|
|
|
|
|
|
|
|
|
if (!wp[0] || wp[1])
|
2011-09-07 17:24:22 +02:00
|
|
|
|
bi_errorf(Tsynerr);
|
2011-02-11 01:41:38 +01:00
|
|
|
|
else if (parse_usec(wp[0], &tv))
|
2012-12-18 00:18:11 +01:00
|
|
|
|
bi_errorf("%s: %s '%s'", Tsynerr, cstrerror(errno), wp[0]);
|
2011-02-11 01:41:38 +01:00
|
|
|
|
else {
|
|
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2012-03-24 19:47:04 +01:00
|
|
|
|
sigset_t omask, bmask;
|
2011-02-11 01:41:38 +01:00
|
|
|
|
|
2012-03-24 19:47:04 +01:00
|
|
|
|
/* block a number of signals from interrupting us, though */
|
|
|
|
|
(void)sigemptyset(&bmask);
|
|
|
|
|
(void)sigaddset(&bmask, SIGPIPE);
|
|
|
|
|
(void)sigaddset(&bmask, SIGCHLD);
|
|
|
|
|
#ifdef SIGWINCH
|
|
|
|
|
(void)sigaddset(&bmask, SIGWINCH);
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGINFO
|
|
|
|
|
(void)sigaddset(&bmask, SIGINFO);
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGUSR1
|
|
|
|
|
(void)sigaddset(&bmask, SIGUSR1);
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SIGUSR2
|
|
|
|
|
(void)sigaddset(&bmask, SIGUSR2);
|
|
|
|
|
#endif
|
|
|
|
|
sigprocmask(SIG_BLOCK, &bmask, &omask);
|
2011-02-11 01:41:38 +01:00
|
|
|
|
#endif
|
2011-11-09 23:17:26 +01:00
|
|
|
|
if (select(1, NULL, NULL, NULL, &tv) == 0 || errno == EINTR)
|
2011-02-11 01:41:38 +01:00
|
|
|
|
/*
|
|
|
|
|
* strictly speaking only for SIGALRM, but the
|
|
|
|
|
* execution may be interrupted by other signals
|
|
|
|
|
*/
|
|
|
|
|
rv = 0;
|
|
|
|
|
else
|
2012-12-18 00:18:11 +01:00
|
|
|
|
bi_errorf("%s: %s", Tselect, cstrerror(errno));
|
2011-02-11 01:41:38 +01:00
|
|
|
|
#ifndef MKSH_NOPROSPECTOFWORK
|
2012-03-24 19:47:04 +01:00
|
|
|
|
/* this will re-schedule signal delivery */
|
2011-02-13 22:13:08 +01:00
|
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
2011-02-11 01:41:38 +01:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
return (rv);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2013-09-10 19:33:04 +02:00
|
|
|
|
|
2013-11-30 18:41:35 +01:00
|
|
|
|
#if !defined(MKSH_UNEMPLOYED) && HAVE_GETSID
|
2013-09-10 19:33:04 +02:00
|
|
|
|
static int
|
|
|
|
|
c_suspend(const char **wp)
|
|
|
|
|
{
|
|
|
|
|
if (wp[1] != NULL) {
|
|
|
|
|
bi_errorf("too many arguments");
|
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
if (Flag(FLOGIN)) {
|
|
|
|
|
/* Can't suspend an orphaned process group. */
|
|
|
|
|
if (getpgid(kshppid) == getpgid(0) ||
|
|
|
|
|
getsid(kshppid) != getsid(0)) {
|
|
|
|
|
bi_errorf("can't suspend a login shell");
|
|
|
|
|
return (1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
j_suspend();
|
|
|
|
|
return (0);
|
|
|
|
|
}
|
|
|
|
|
#endif
|