181af778eSCasper H.S. Dik /* 281af778eSCasper H.S. Dik * CDDL HEADER START 381af778eSCasper H.S. Dik * 481af778eSCasper H.S. Dik * The contents of this file are subject to the terms of the 581af778eSCasper H.S. Dik * Common Development and Distribution License (the "License"). 681af778eSCasper H.S. Dik * You may not use this file except in compliance with the License. 781af778eSCasper H.S. Dik * 881af778eSCasper H.S. Dik * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 981af778eSCasper H.S. Dik * or http://www.opensolaris.org/os/licensing. 1081af778eSCasper H.S. Dik * See the License for the specific language governing permissions 1181af778eSCasper H.S. Dik * and limitations under the License. 1281af778eSCasper H.S. Dik * 1381af778eSCasper H.S. Dik * When distributing Covered Code, include this CDDL HEADER in each 1481af778eSCasper H.S. Dik * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1581af778eSCasper H.S. Dik * If applicable, add the following below this CDDL HEADER, with the 1681af778eSCasper H.S. Dik * fields enclosed by brackets "[]" replaced with your own identifying 1781af778eSCasper H.S. Dik * information: Portions Copyright [yyyy] [name of copyright owner] 1881af778eSCasper H.S. Dik * 1981af778eSCasper H.S. Dik * CDDL HEADER END 2081af778eSCasper H.S. Dik */ 2181af778eSCasper H.S. Dik 2281af778eSCasper H.S. Dik /* 2381af778eSCasper H.S. Dik * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 2481af778eSCasper H.S. Dik * Use is subject to license terms. 2581af778eSCasper H.S. Dik */ 2681af778eSCasper H.S. Dik 2781af778eSCasper H.S. Dik /* 2881af778eSCasper H.S. Dik * alias.c is a C version of the alias.sh wrapper (which links ksh 2981af778eSCasper H.S. Dik * builtins to commands in /usr/bin/, e.g. calling this wrapper as 3081af778eSCasper H.S. Dik * /usr/bin/alias will call the ksh "alias" builtin, running it as 3181af778eSCasper H.S. Dik * /usr/bin/cut will call the ksh "cut" builtin etc. 3281af778eSCasper H.S. Dik */ 3381af778eSCasper H.S. Dik 3481af778eSCasper H.S. Dik #include <shell.h> 3581af778eSCasper H.S. Dik #include <nval.h> 3681af778eSCasper H.S. Dik #include <stdio.h> 3781af778eSCasper H.S. Dik 3881af778eSCasper H.S. Dik /* Builtin script, original derived from alias.sh */ 3981af778eSCasper H.S. Dik static const char *script = "\n" 4081af778eSCasper H.S. Dik /* Get name of builtin */ 41*9a6f360eSCasper H.S. Dik "typeset cmd=\"${0##*/}\"\n" 4281af778eSCasper H.S. Dik /* 4381af778eSCasper H.S. Dik * If the requested command is not an alias load it explicitly 4481af778eSCasper H.S. Dik * to make sure it is not bound to a path (those built-ins which 4581af778eSCasper H.S. Dik * are mapped via shell aliases point to commands which are 4681af778eSCasper H.S. Dik * "special shell built-ins" which cannot be bound to a specific 4781af778eSCasper H.S. Dik * PATH element) - otherwise we may execute the wrong command 4881af778eSCasper H.S. Dik * if an executable with the same name sits in a PATH element 4981af778eSCasper H.S. Dik * before /usr/bin (e.g. /usr/xpg4/bin/ls would be executed 5081af778eSCasper H.S. Dik * before /usr/bin/ls if the path was something like 5181af778eSCasper H.S. Dik * PATH=/usr/xpg4/bin:/usr/bin). 5281af778eSCasper H.S. Dik */ 5381af778eSCasper H.S. Dik "if [[ \"${cmd}\" != ~(Elr)(alias|unalias|command) ]] && " 5481af778eSCasper H.S. Dik "! alias \"${cmd}\" >/dev/null 2>&1 ; then\n" 5581af778eSCasper H.S. Dik "builtin \"${cmd}\"\n" 5681af778eSCasper H.S. Dik "fi\n" 5781af778eSCasper H.S. Dik /* command is a keyword and needs to be handled separately */ 5881af778eSCasper H.S. Dik "if [[ \"${cmd}\" == \"command\" ]] ; then\n" 5981af778eSCasper H.S. Dik "command \"$@\"\n" 6081af778eSCasper H.S. Dik "else\n" 6181af778eSCasper H.S. Dik "\"${cmd}\" \"$@\"\n" 6281af778eSCasper H.S. Dik "fi\n" 6381af778eSCasper H.S. Dik "exitval=$?"; 6481af778eSCasper H.S. Dik 6581af778eSCasper H.S. Dik int 6681af778eSCasper H.S. Dik main(int argc, char *argv[]) 6781af778eSCasper H.S. Dik { 6881af778eSCasper H.S. Dik int i; 6981af778eSCasper H.S. Dik Shell_t *shp; 7081af778eSCasper H.S. Dik Namval_t *np; 7181af778eSCasper H.S. Dik int exitval; 7281af778eSCasper H.S. Dik 7381af778eSCasper H.S. Dik /* 7481af778eSCasper H.S. Dik * Create copy of |argv| array shifted by one position to 7581af778eSCasper H.S. Dik * emulate $ /usr/bin/sh <scriptname> <args1> <arg2> ... #. 7681af778eSCasper H.S. Dik * First position is set to "/usr/bin/sh" since other 7781af778eSCasper H.S. Dik * values may trigger special shell modes (e.g. *rsh* will 7881af778eSCasper H.S. Dik * trigger "restricted" shell mode etc.). 7981af778eSCasper H.S. Dik */ 8081af778eSCasper H.S. Dik char *xargv[argc+2]; 8181af778eSCasper H.S. Dik xargv[0] = "/usr/bin/sh"; 8281af778eSCasper H.S. Dik xargv[1] = "scriptname"; 8381af778eSCasper H.S. Dik for (i = 0; i < argc; i++) { 8481af778eSCasper H.S. Dik xargv[i+1] = argv[i]; 8581af778eSCasper H.S. Dik } 8681af778eSCasper H.S. Dik xargv[i+1] = NULL; 8781af778eSCasper H.S. Dik 8881af778eSCasper H.S. Dik shp = sh_init(argc+1, xargv, 0); 8981af778eSCasper H.S. Dik if (!shp) 9081af778eSCasper H.S. Dik error(ERROR_exit(1), "shell initialisation failed."); 9181af778eSCasper H.S. Dik (void) sh_trap(script, 0); 9281af778eSCasper H.S. Dik 9381af778eSCasper H.S. Dik np = nv_open("exitval", shp->var_tree, 0); 9481af778eSCasper H.S. Dik if (!np) 9581af778eSCasper H.S. Dik error(ERROR_exit(1), "variable %s not found.", "exitval"); 9681af778eSCasper H.S. Dik exitval = (int)nv_getnum(np); 9781af778eSCasper H.S. Dik nv_close(np); 9881af778eSCasper H.S. Dik 9981af778eSCasper H.S. Dik return (exitval); 10081af778eSCasper H.S. Dik } 101