components/ksh93/patches/solaris_alias.patch
changeset 4196 d697072a92f5
parent 4195 d88c5d15a4af
child 4197 67d7270f6817
equal deleted inserted replaced
4195:d88c5d15a4af 4196:d697072a92f5
     1 diff -rupN b/lib/package/ast-base.pkg a/lib/package/ast-base.pkg
       
     2 --- b/lib/package/ast-base.pkg	2009-09-21 20:35:51.000000000 +0000
       
     3 +++ a/lib/package/ast-base.pkg	2011-11-10 16:24:52.515495613 +0000
       
     4 @@ -3,7 +3,7 @@ ast-base :PACKAGE: \
       
     5  		libdll libexpr libodelta librecsort libsum libuu libvdelta \
       
     6  		libbz libz tests 3d coshell cpp cs mam msgcc nmake probe ss \
       
     7  		libcoshell libcs libmam libpp libcodex paxlib codexlib \
       
     8 -		libdss libpz dsslib
       
     9 +		libdss libpz dsslib alias
       
    10  
       
    11  :COVERS: ast-make ast-ksh ast-ast
       
    12  
       
    13 diff -rupN b/src/cmd/alias/alias.c a/src/cmd/alias/alias.c
       
    14 --- b/src/cmd/alias/alias.c	1970-01-01 00:00:00.000000000 +0000
       
    15 +++ a/src/cmd/alias/alias.c	2011-11-10 16:24:28.356925339 +0000
       
    16 @@ -0,0 +1,255 @@
       
    17 +/*
       
    18 + * CDDL HEADER START
       
    19 + *
       
    20 + * The contents of this file are subject to the terms of the
       
    21 + * Common Development and Distribution License (the "License").
       
    22 + * You may not use this file except in compliance with the License.
       
    23 + *
       
    24 + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
    25 + * or http://www.opensolaris.org/os/licensing.
       
    26 + * See the License for the specific language governing permissions
       
    27 + * and limitations under the License.
       
    28 + *
       
    29 + * When distributing Covered Code, include this CDDL HEADER in each
       
    30 + * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    31 + * If applicable, add the following below this CDDL HEADER, with the
       
    32 + * fields enclosed by brackets "[]" replaced with your own identifying
       
    33 + * information: Portions Copyright [yyyy] [name of copyright owner]
       
    34 + *
       
    35 + * CDDL HEADER END
       
    36 + */
       
    37 +
       
    38 +/*
       
    39 + * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    40 + */
       
    41 +
       
    42 +/*
       
    43 + * alias.c is a C version of the alias.sh wrapper (which links ksh
       
    44 + * builtins to commands in /usr/bin/, e.g. calling this wrapper as
       
    45 + * /usr/bin/alias will call the ksh "alias" builtin, running it as
       
    46 + * /usr/bin/cut will call the ksh "cut" builtin etc.
       
    47 + */
       
    48 +
       
    49 +#include <shell.h>
       
    50 +#include <nval.h>
       
    51 +#include <stdio.h>
       
    52 +#include <alias.h>
       
    53 +
       
    54 +typedef struct {
       
    55 +	const char *name;
       
    56 +	int (* func)(int, char **, void *);
       
    57 +} bfastpathrec;
       
    58 +
       
    59 +/*
       
    60 + * We've disabled the "fastpath" codepath for some commands below
       
    61 + * because it causes a paradoxon for large input files (as used by
       
    62 + * ON PerfPIT for testing). For /usr/bin/rev (where the issue was
       
    63 + * first discovered) it looks like this:
       
    64 + * - for small files like /etc/profile the fastpath is faster in a loop
       
    65 + *   with 1000 iterations (8 seconds with fastpath, 14 seconds without
       
    66 + *   fastpath)
       
    67 + * - for large files (/usr/pub/UTF-8 replicated until the test file
       
    68 + *   reaches 24884706 bytes) the benchmark reverses: The fastpath now
       
    69 + *   needs 40 seconds and without fastpath it needs 30 seconds (for 100
       
    70 + *   iterations).
       
    71 + */
       
    72 +#if 0
       
    73 +#define	ENABLE_PERFORMANCE_PARADOXON 1
       
    74 +#endif
       
    75 +
       
    76 +/*
       
    77 + * List of libcmd builtins which do not require a |Shell_t| context.
       
    78 + * This list was automatically generated from <ast/cmdext.h>
       
    79 + */
       
    80 +static const
       
    81 +bfastpathrec fastpath_builtins[] =
       
    82 +{
       
    83 +	/* This list must be alphabetically sorted for |strcmp()| usage */
       
    84 +	{ "basename",	b_basename	},
       
    85 +	{ "cat",	b_cat		},
       
    86 +	{ "chgrp",	b_chgrp		},
       
    87 +	{ "chmod",	b_chmod		},
       
    88 +	{ "chown",	b_chown		},
       
    89 +#ifdef ENABLE_PERFORMANCE_PARADOXON
       
    90 +	{ "cksum",	b_cksum		},
       
    91 +#endif /* ENABLE_PERFORMANCE_PARADOXON */
       
    92 +	{ "cmp",	b_cmp		},
       
    93 +	{ "comm",	b_comm		},
       
    94 +	{ "cp",		b_cp		},
       
    95 +	{ "cut",	b_cut		},
       
    96 +	{ "date",	b_date		},
       
    97 +	{ "dirname",	b_dirname	},
       
    98 +	{ "expr",	b_expr		},
       
    99 +	{ "fds",	b_fds		},
       
   100 +	{ "fmt",	b_fmt		},
       
   101 +	{ "fold",	b_fold		},
       
   102 +	{ "getconf",	b_getconf	},
       
   103 +	{ "head",	b_head		},
       
   104 +	{ "id",		b_id		},
       
   105 +	{ "join",	b_join		},
       
   106 +	{ "ln",		b_ln		},
       
   107 +	{ "logname",	b_logname	},
       
   108 +	{ "md5sum",	b_md5sum	},
       
   109 +	{ "mkdir",	b_mkdir		},
       
   110 +	{ "mkfifo",	b_mkfifo	},
       
   111 +	{ "mktemp",	b_mktemp	},
       
   112 +	{ "mv",		b_mv		},
       
   113 +	{ "paste",	b_paste 	},
       
   114 +	{ "pathchk",	b_pathchk	},
       
   115 +	{ "pids",	b_pids		},
       
   116 +#ifdef ENABLE_PERFORMANCE_PARADOXON
       
   117 +	{ "rev",	b_rev		},
       
   118 +#endif /* ENABLE_PERFORMANCE_PARADOXON */
       
   119 +	{ "rm",		b_rm		},
       
   120 +	{ "rmdir",	b_rmdir		},
       
   121 +	{ "stty",	b_stty		},
       
   122 +#ifdef ENABLE_PERFORMANCE_PARADOXON
       
   123 +	{ "sum",	b_sum		},
       
   124 +#endif /* ENABLE_PERFORMANCE_PARADOXON */
       
   125 +	{ "sync",	b_sync		},
       
   126 +	{ "tail",	b_tail		},
       
   127 +	{ "tee",	b_tee		},
       
   128 +	{ "tty",	b_tty		},
       
   129 +	{ "uname",	b_uname		},
       
   130 +	{ "uniq",	b_uniq		},
       
   131 +	{ "wc",		b_wc		},
       
   132 +	{ NULL, 	(int (*)(int, char **, void *))NULL }
       
   133 +};
       
   134 +
       
   135 +static inline
       
   136 +const bfastpathrec *
       
   137 +find_bfastpathrec(const char *name)
       
   138 +{
       
   139 +	unsigned int i;
       
   140 +	signed int cmpres;
       
   141 +	for (i = 0; fastpath_builtins[i].name != NULL; i++) {
       
   142 +		cmpres = strcmp(fastpath_builtins[i].name, name);
       
   143 +		if (cmpres == 0)
       
   144 +			return (&fastpath_builtins[i]);
       
   145 +		else if (cmpres > 0)
       
   146 +			return (NULL);
       
   147 +
       
   148 +	}
       
   149 +	return (NULL);
       
   150 +}
       
   151 +
       
   152 +static inline
       
   153 +int
       
   154 +fastpath_builtin_main(const bfastpathrec *brec, int argc, char *argv[])
       
   155 +{
       
   156 +	setlocale(LC_ALL, ""); /* calls |_ast_setlocale()| */
       
   157 +
       
   158 +	return ((*brec->func)(argc, argv, NULL));
       
   159 +}
       
   160 +
       
   161 +
       
   162 +/* Builtin script, original derived from alias.sh */
       
   163 +static const char *script = "\n"
       
   164 +/* Get name of builtin */
       
   165 +"typeset cmd=\"${0##*/}\"\n"
       
   166 +/*
       
   167 + * If the requested command is not an alias load it explicitly
       
   168 + * to make sure it is not bound to a path (those built-ins which
       
   169 + * are mapped via shell aliases point to commands which are
       
   170 + * "special shell built-ins" which cannot be bound to a specific
       
   171 + * PATH element) - otherwise we may execute the wrong command
       
   172 + * if an executable with the same name sits in a PATH element
       
   173 + * before /usr/bin (e.g. /usr/xpg4/bin/ls would be executed
       
   174 + * before /usr/bin/ls if the path was something like
       
   175 + * PATH=/usr/xpg4/bin:/usr/bin).
       
   176 + */
       
   177 +"if [[ \"${cmd}\" != ~(Elr)(alias|unalias|command) ]] && "
       
   178 +	"! alias \"${cmd}\" >/dev/null 2>&1 ; then\n"
       
   179 +	"PATH='' builtin \"${cmd}\"\n"
       
   180 +"fi\n"
       
   181 +/* command is a keyword and needs to be handled separately */
       
   182 +"if [[ \"${cmd}\" == \"command\" ]] ; then\n"
       
   183 +	"command \"$@\"\n"
       
   184 +"else\n"
       
   185 +#ifdef WORKAROUND_FOR_ALIAS_CRASH
       
   186 +/*
       
   187 + * Work around a crash in /usr/bin/alias when invalid options are
       
   188 + * passed (e.g. $ /usr/bin/alias -c #). The shell code will call
       
   189 + * an error handler which does a |longjmp()| but somehow the code
       
   190 + * failed to do the |setjmp()| before this point.
       
   191 + * Putting the "alias" command in a subshell avoids the crash.
       
   192 + * Real cause of the issue is under investigation and a fix be
       
   193 + * delivered with the next ast-ksh update.
       
   194 + */
       
   195 +	"( \"${cmd}\" \"$@\" )\n"
       
   196 +#else
       
   197 +	"\"${cmd}\" \"$@\"\n"
       
   198 +#endif /* WORKAROUND_FOR_ALIAS_CRASH */
       
   199 +"fi\n"
       
   200 +"exitval=$?";
       
   201 +
       
   202 +
       
   203 +static inline
       
   204 +int
       
   205 +script_builtin_main(int argc, char *argv[])
       
   206 +{
       
   207 +	int i;
       
   208 +	Shell_t *shp;
       
   209 +	Namval_t *np;
       
   210 +	int exitval;
       
   211 +
       
   212 +	/*
       
   213 +	 * Create copy of |argv| array shifted by one position to
       
   214 +	 * emulate $ /usr/bin/sh <scriptname> <args1> <arg2> ... #.
       
   215 +	 * First position is set to "/usr/bin/sh" since other
       
   216 +	 * values may trigger special shell modes (e.g. *rsh* will
       
   217 +	 * trigger "restricted" shell mode etc.).
       
   218 +	 */
       
   219 +	char *xargv[argc+2];
       
   220 +	xargv[0] = "/usr/bin/sh";
       
   221 +	xargv[1] = "scriptname";
       
   222 +	for (i = 0; i < argc; i++) {
       
   223 +		xargv[i+1] = argv[i];
       
   224 +	}
       
   225 +	xargv[i+1] = NULL;
       
   226 +
       
   227 +	shp = sh_init(argc+1, xargv, 0);
       
   228 +	if (!shp)
       
   229 +		error(ERROR_exit(1), "shell initialisation failed.");
       
   230 +	(void) sh_trap(script, 0);
       
   231 +
       
   232 +	np = nv_open("exitval", shp->var_tree, 0);
       
   233 +	if (!np)
       
   234 +		error(ERROR_exit(1), "variable %s not found.", "exitval");
       
   235 +	exitval = (int)nv_getnum(np);
       
   236 +	nv_close(np);
       
   237 +
       
   238 +	return (exitval);
       
   239 +}
       
   240 +
       
   241 +int
       
   242 +main(int argc, char *argv[])
       
   243 +{
       
   244 +	const char *progname;
       
   245 +	const bfastpathrec *brec;
       
   246 +	char execnamebuff[PATH_MAX+1];
       
   247 +
       
   248 +	/* Get program name */
       
   249 +	if (pathprog(argv[0], execnamebuff, sizeof (execnamebuff)) <= 0)
       
   250 +		error(ERROR_exit(1), "could not determinate exec name.");
       
   251 +
       
   252 +	progname = (const char *)strrchr(execnamebuff, '/');
       
   253 +	if (progname != NULL) {
       
   254 +		progname++;
       
   255 +	}
       
   256 +	else
       
   257 +	{
       
   258 +		progname = execnamebuff;
       
   259 +	}
       
   260 +
       
   261 +	/* Execute command... */
       
   262 +	if (brec = find_bfastpathrec(progname)) {
       
   263 +		/* ... either via a fast path (calling the code directly) ... */
       
   264 +		return (fastpath_builtin_main(brec, argc, argv));
       
   265 +	}
       
   266 +	else
       
   267 +	{
       
   268 +		/* ... or from within a full shell. */
       
   269 +		return (script_builtin_main(argc, argv));
       
   270 +	}
       
   271 +}
       
   272 diff -rupN b/src/cmd/alias/alias.h a/src/cmd/alias/alias.h
       
   273 --- b/src/cmd/alias/alias.h	1970-01-01 00:00:00.000000000 +0000
       
   274 +++ a/src/cmd/alias/alias.h	2011-11-10 16:24:28.357387725 +0000
       
   275 @@ -0,0 +1,79 @@
       
   276 +/***********************************************************************
       
   277 +*                                                                      *
       
   278 +*               This software is part of the ast package               *
       
   279 +*          Copyright (c) 1992-2011 AT&T Intellectual Property          *
       
   280 +*                      and is licensed under the                       *
       
   281 +*                  Common Public License, Version 1.0                  *
       
   282 +*                    by AT&T Intellectual Property                     *
       
   283 +*                                                                      *
       
   284 +*                A copy of the License is available at                 *
       
   285 +*            http://www.opensource.org/licenses/cpl1.0.txt             *
       
   286 +*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
       
   287 +*                                                                      *
       
   288 +*              Information and Software Systems Research               *
       
   289 +*                            AT&T Research                             *
       
   290 +*                           Florham Park NJ                            *
       
   291 +*                                                                      *
       
   292 +*                 Glenn Fowler <[email protected]>                  *
       
   293 +*                  David Korn <[email protected]>                   *
       
   294 +*                                                                      *
       
   295 +***********************************************************************/
       
   296 +
       
   297 +extern int b_asa (int, char**, void *);
       
   298 +extern int b_basename (int, char**, void *);
       
   299 +extern int b_cat (int, char**, void *);
       
   300 +extern int b_chgrp (int, char**, void *);
       
   301 +extern int b_chmod (int, char**, void *);
       
   302 +extern int b_chown (int, char**, void *);
       
   303 +extern int b_cksum (int, char**, void *);
       
   304 +extern int b_cmp (int, char**, void *);
       
   305 +extern int b_comm (int, char**, void *);
       
   306 +extern int b_cp (int, char**, void *);
       
   307 +extern int b_cut (int, char**, void *);
       
   308 +extern int b_date (int, char**, void *);
       
   309 +extern int b_dirname (int, char**, void *);
       
   310 +extern int b_egrep (int, char**, void *);
       
   311 +extern int b_expr (int, char**, void *);
       
   312 +extern int b_fds (int, char**, void *);
       
   313 +extern int b_fgrep (int, char**, void *);
       
   314 +extern int b_find (int, char**, void *);
       
   315 +extern int b_fmt (int, char**, void *);
       
   316 +extern int b_fold (int, char**, void *);
       
   317 +extern int b_getconf (int, char**, void *);
       
   318 +extern int b_grep (int, char**, void *);
       
   319 +extern int b_head (int, char**, void *);
       
   320 +extern int b_id (int, char**, void *);
       
   321 +extern int b_join (int, char**, void *);
       
   322 +extern int b_line (int, char**, void *);
       
   323 +extern int b_ln (int, char**, void *);
       
   324 +extern int b_logname (int, char**, void *);
       
   325 +extern int b_ls (int, char**, void *);
       
   326 +extern int b_md5sum (int, char**, void *);
       
   327 +extern int b_mkdir (int, char**, void *);
       
   328 +extern int b_mkfifo (int, char**, void *);
       
   329 +extern int b_mktemp (int, char**, void *);
       
   330 +extern int b_mv (int, char**, void *);
       
   331 +extern int b_paste (int, char**, void *);
       
   332 +extern int b_od (int, char**, void *);
       
   333 +extern int b_pathchk (int, char**, void *);
       
   334 +extern int b_pids (int, char**, void *);
       
   335 +extern int b_pr (int, char**, void *);
       
   336 +extern int b_rev (int, char**, void *);
       
   337 +extern int b_readlink (int, char**, void *);
       
   338 +extern int b_rm (int, char**, void *);
       
   339 +extern int b_rmdir (int, char**, void *);
       
   340 +extern int b_stty (int, char**, void *);
       
   341 +extern int b_sum (int, char**, void *);
       
   342 +extern int b_sync (int, char**, void *);
       
   343 +extern int b_strings (int, char**, void *);
       
   344 +extern int b_tail (int, char**, void *);
       
   345 +extern int b_tee (int, char**, void *);
       
   346 +extern int b_tr (int, char**, void *);
       
   347 +extern int b_tty (int, char**, void *);
       
   348 +extern int b_uname (int, char**, void *);
       
   349 +extern int b_uniq (int, char**, void *);
       
   350 +extern int b_vmstate (int, char**, void *);
       
   351 +extern int b_wc (int, char**, void *);
       
   352 +extern int b_who (int, char**, void *);
       
   353 +extern int b_xgrep (int, char**, void *);
       
   354 +extern int b_xargs (int, char**, void *);
       
   355 diff -rupN b/src/cmd/alias/Makefile a/src/cmd/alias/Makefile
       
   356 --- b/src/cmd/alias/Makefile	1970-01-01 00:00:00.000000000 +0000
       
   357 +++ a/src/cmd/alias/Makefile	2011-11-10 16:24:28.357746164 +0000
       
   358 @@ -0,0 +1,5 @@
       
   359 +:PACKAGE: ast:static
       
   360 +
       
   361 +LICENSE = cddl
       
   362 +libtype = :static
       
   363 +alias :: RELEASE alias.c +lshell