add type builtin.

This commit is contained in:
christos 1997-02-06 23:24:52 +00:00
parent 5851e327c5
commit 680690d33c
4 changed files with 90 additions and 5 deletions

View File

@ -1,5 +1,5 @@
#!/bin/sh -
# $NetBSD: builtins.def,v 1.12 1995/05/11 21:28:48 christos Exp $
# $NetBSD: builtins.def,v 1.13 1997/02/06 23:24:52 christos Exp $
#
# Copyright (c) 1991, 1993
# The Regents of the University of California. All rights reserved.
@ -82,6 +82,7 @@ setvarcmd setvar
shiftcmd shift
trapcmd trap
truecmd : true
typecmd type
umaskcmd umask
unaliascmd unalias
unsetcmd unset

View File

@ -1,4 +1,4 @@
/* $NetBSD: exec.c,v 1.21 1997/01/11 02:04:31 tls Exp $ */
/* $NetBSD: exec.c,v 1.22 1997/02/06 23:24:52 christos Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)exec.c 8.4 (Berkeley) 6/8/95";
#else
static char rcsid[] = "$NetBSD: exec.c,v 1.21 1997/01/11 02:04:31 tls Exp $";
static char rcsid[] = "$NetBSD: exec.c,v 1.22 1997/02/06 23:24:52 christos Exp $";
#endif
#endif /* not lint */
@ -79,6 +79,7 @@ static char rcsid[] = "$NetBSD: exec.c,v 1.21 1997/01/11 02:04:31 tls Exp $";
#include "mystring.h"
#include "show.h"
#include "jobs.h"
#include "alias.h"
#define CMDTABLESIZE 31 /* should be prime */
@ -843,3 +844,77 @@ unsetfunc(name)
}
return (1);
}
/*
* Locate and print what a word is...
*/
int
typecmd(argc, argv)
int argc;
char **argv;
{
struct cmdentry entry;
struct tblentry *cmdp;
char **pp;
struct alias *ap;
int i;
int error = 0;
extern char *const parsekwd[];
for (i = 1; i < argc; i++) {
out1str(argv[i]);
/* First look at the keywords */
for (pp = (char **)parsekwd; *pp; pp++)
if (**pp == *argv[i] && equal(*pp, argv[i]))
break;
if (*pp) {
out1str(" is a shell keyword\n");
continue;
}
/* Then look at the aliases */
if ((ap = lookupalias(argv[i], 1)) != NULL) {
out1fmt(" is an alias for %s\n", ap->val);
continue;
}
/* Then check if it is a tracked alias */
if ((cmdp = cmdlookup(argv[i], 0)) != NULL) {
entry.cmdtype = cmdp->cmdtype;
entry.u = cmdp->param;
}
else {
/* Finally use brute force */
find_command(argv[i], &entry, 0, pathval());
}
switch (entry.cmdtype) {
case CMDNORMAL: {
int j = entry.u.index;
char *path = pathval(), *name;
do {
name = padvance(&path, argv[i]);
stunalloc(name);
} while (--j >= 0);
out1fmt(" is%s %s\n",
cmdp ? " a tracked alias for" : "", name);
break;
}
case CMDFUNCTION:
out1str(" is a shell function\n");
break;
case CMDBUILTIN:
out1str(" is a shell builtin\n");
break;
default:
out1str(" not found\n");
error |= 127;
break;
}
}
return error;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: exec.h,v 1.11 1996/10/16 14:35:45 christos Exp $ */
/* $NetBSD: exec.h,v 1.12 1997/02/06 23:24:53 christos Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -69,3 +69,4 @@ void getcmdentry __P((char *, struct cmdentry *));
void addcmdentry __P((char *, struct cmdentry *));
void defun __P((char *, union node *));
int unsetfunc __P((char *));
int typecmd __P((int, char **));

View File

@ -1,4 +1,4 @@
.\" $NetBSD: sh.1,v 1.17 1996/10/16 15:20:01 christos Exp $
.\" $NetBSD: sh.1,v 1.18 1997/02/06 23:24:54 christos Exp $
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
@ -1337,6 +1337,14 @@ resets trapped (but not ignored) signals to the
default action. The trap command has no effect on
signals that were ignored on entry to the shell.
.TP
type [name]...
Interpret each name as a command and print the
resolution of the command search. Possible resolutions are:
shell keyword, alias, shell builtin, command, tracked alias
and not found. For aliases the alias expansion is printed;
for commands and tracked aliases the complete pathname of
the command is printed.
.TP
umask [ mask ]
Set the value of umask (see umask(2)) to the specified
octal value. If the argument is omitted, the