xref: /onnv-gate/usr/src/cmd/ksh/builtins/alias.c (revision 8900:a91874fffcad)
18810SCasper.Dik@Sun.COM /*
28810SCasper.Dik@Sun.COM  * CDDL HEADER START
38810SCasper.Dik@Sun.COM  *
48810SCasper.Dik@Sun.COM  * The contents of this file are subject to the terms of the
58810SCasper.Dik@Sun.COM  * Common Development and Distribution License (the "License").
68810SCasper.Dik@Sun.COM  * You may not use this file except in compliance with the License.
78810SCasper.Dik@Sun.COM  *
88810SCasper.Dik@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
98810SCasper.Dik@Sun.COM  * or http://www.opensolaris.org/os/licensing.
108810SCasper.Dik@Sun.COM  * See the License for the specific language governing permissions
118810SCasper.Dik@Sun.COM  * and limitations under the License.
128810SCasper.Dik@Sun.COM  *
138810SCasper.Dik@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
148810SCasper.Dik@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
158810SCasper.Dik@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
168810SCasper.Dik@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
178810SCasper.Dik@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
188810SCasper.Dik@Sun.COM  *
198810SCasper.Dik@Sun.COM  * CDDL HEADER END
208810SCasper.Dik@Sun.COM  */
218810SCasper.Dik@Sun.COM 
228810SCasper.Dik@Sun.COM /*
238810SCasper.Dik@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
248810SCasper.Dik@Sun.COM  * Use is subject to license terms.
258810SCasper.Dik@Sun.COM  */
268810SCasper.Dik@Sun.COM 
278810SCasper.Dik@Sun.COM /*
288810SCasper.Dik@Sun.COM  * alias.c is a C version of the alias.sh wrapper (which links ksh
298810SCasper.Dik@Sun.COM  * builtins to commands in /usr/bin/, e.g. calling this wrapper as
308810SCasper.Dik@Sun.COM  * /usr/bin/alias will call the ksh "alias" builtin, running it as
318810SCasper.Dik@Sun.COM  * /usr/bin/cut will call the ksh "cut" builtin etc.
328810SCasper.Dik@Sun.COM  */
338810SCasper.Dik@Sun.COM 
348810SCasper.Dik@Sun.COM #include <shell.h>
358810SCasper.Dik@Sun.COM #include <nval.h>
368810SCasper.Dik@Sun.COM #include <stdio.h>
378810SCasper.Dik@Sun.COM 
388810SCasper.Dik@Sun.COM /* Builtin script, original derived from alias.sh */
398810SCasper.Dik@Sun.COM static const char *script = "\n"
408810SCasper.Dik@Sun.COM /* Get name of builtin */
41*8900SCasper.Dik@Sun.COM "typeset cmd=\"${0##*/}\"\n"
428810SCasper.Dik@Sun.COM /*
438810SCasper.Dik@Sun.COM  * If the requested command is not an alias load it explicitly
448810SCasper.Dik@Sun.COM  * to make sure it is not bound to a path (those built-ins which
458810SCasper.Dik@Sun.COM  * are mapped via shell aliases point to commands which are
468810SCasper.Dik@Sun.COM  * "special shell built-ins" which cannot be bound to a specific
478810SCasper.Dik@Sun.COM  * PATH element) - otherwise we may execute the wrong command
488810SCasper.Dik@Sun.COM  * if an executable with the same name sits in a PATH element
498810SCasper.Dik@Sun.COM  * before /usr/bin (e.g. /usr/xpg4/bin/ls would be executed
508810SCasper.Dik@Sun.COM  * before /usr/bin/ls if the path was something like
518810SCasper.Dik@Sun.COM  * PATH=/usr/xpg4/bin:/usr/bin).
528810SCasper.Dik@Sun.COM  */
538810SCasper.Dik@Sun.COM "if [[ \"${cmd}\" != ~(Elr)(alias|unalias|command) ]] && "
548810SCasper.Dik@Sun.COM 	"! alias \"${cmd}\" >/dev/null 2>&1 ; then\n"
55*8900SCasper.Dik@Sun.COM 	"builtin \"${cmd}\"\n"
568810SCasper.Dik@Sun.COM "fi\n"
578810SCasper.Dik@Sun.COM /* command is a keyword and needs to be handled separately */
588810SCasper.Dik@Sun.COM "if [[ \"${cmd}\" == \"command\" ]] ; then\n"
59*8900SCasper.Dik@Sun.COM 	"command \"$@\"\n"
608810SCasper.Dik@Sun.COM "else\n"
61*8900SCasper.Dik@Sun.COM 	"\"${cmd}\" \"$@\"\n"
628810SCasper.Dik@Sun.COM "fi\n"
638810SCasper.Dik@Sun.COM "exitval=$?";
648810SCasper.Dik@Sun.COM 
658810SCasper.Dik@Sun.COM int
668810SCasper.Dik@Sun.COM main(int argc, char *argv[])
678810SCasper.Dik@Sun.COM {
688810SCasper.Dik@Sun.COM 	int i;
698810SCasper.Dik@Sun.COM 	Shell_t *shp;
708810SCasper.Dik@Sun.COM 	Namval_t *np;
718810SCasper.Dik@Sun.COM 	int exitval;
728810SCasper.Dik@Sun.COM 
738810SCasper.Dik@Sun.COM 	/*
748810SCasper.Dik@Sun.COM 	 * Create copy of |argv| array shifted by one position to
758810SCasper.Dik@Sun.COM 	 * emulate $ /usr/bin/sh <scriptname> <args1> <arg2> ... #.
768810SCasper.Dik@Sun.COM 	 * First position is set to "/usr/bin/sh" since other
778810SCasper.Dik@Sun.COM 	 * values may trigger special shell modes (e.g. *rsh* will
788810SCasper.Dik@Sun.COM 	 * trigger "restricted" shell mode etc.).
798810SCasper.Dik@Sun.COM 	 */
808810SCasper.Dik@Sun.COM 	char *xargv[argc+2];
818810SCasper.Dik@Sun.COM 	xargv[0] = "/usr/bin/sh";
828810SCasper.Dik@Sun.COM 	xargv[1] = "scriptname";
838810SCasper.Dik@Sun.COM 	for (i = 0; i < argc; i++) {
848810SCasper.Dik@Sun.COM 		xargv[i+1] = argv[i];
858810SCasper.Dik@Sun.COM 	}
868810SCasper.Dik@Sun.COM 	xargv[i+1] = NULL;
878810SCasper.Dik@Sun.COM 
888810SCasper.Dik@Sun.COM 	shp = sh_init(argc+1, xargv, 0);
898810SCasper.Dik@Sun.COM 	if (!shp)
908810SCasper.Dik@Sun.COM 		error(ERROR_exit(1), "shell initialisation failed.");
918810SCasper.Dik@Sun.COM 	(void) sh_trap(script, 0);
928810SCasper.Dik@Sun.COM 
938810SCasper.Dik@Sun.COM 	np = nv_open("exitval", shp->var_tree, 0);
948810SCasper.Dik@Sun.COM 	if (!np)
958810SCasper.Dik@Sun.COM 		error(ERROR_exit(1), "variable %s not found.", "exitval");
968810SCasper.Dik@Sun.COM 	exitval = (int)nv_getnum(np);
978810SCasper.Dik@Sun.COM 	nv_close(np);
988810SCasper.Dik@Sun.COM 
998810SCasper.Dik@Sun.COM 	return (exitval);
1008810SCasper.Dik@Sun.COM }
101