1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*	$NetBSD: alias.c,v 1.12 2003/08/07 09:05:29 agc Exp $	*/
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*-
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Copyright (c) 1993
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *	The Regents of the University of California.  All rights reserved.
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * This code is derived from software contributed to Berkeley by
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Kenneth Almquist.
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Redistribution and use in source and binary forms, with or without
11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * modification, are permitted provided that the following conditions
12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * are met:
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *    documentation and/or other materials provided with the distribution.
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * 3. Neither the name of the University nor the names of its contributors
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *    may be used to endorse or promote products derived from this software
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *    without specific prior written permission.
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * SUCH DAMAGE.
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
34dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
35dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/cdefs.h>
36dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifndef lint
37dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#if 0
38dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic char sccsid[] = "@(#)alias.c	8.3 (Berkeley) 5/4/95";
39dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#else
40dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project__RCSID("$NetBSD: alias.c,v 1.12 2003/08/07 09:05:29 agc Exp $");
41dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
42dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif /* not lint */
43dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
44dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
45dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "shell.h"
46dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "input.h"
47dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "output.h"
48dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "error.h"
49dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "memalloc.h"
50dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "mystring.h"
51dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "alias.h"
52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "options.h"	/* XXX for argptr (should remove?) */
53dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "var.h"
54dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
55dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define ATABSIZE 39
56dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
57dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstruct alias *atab[ATABSIZE];
58dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
59dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source ProjectSTATIC void setalias(char *, char *);
60dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source ProjectSTATIC int unalias(char *);
61dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source ProjectSTATIC struct alias **hashalias(char *);
62dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
63dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source ProjectSTATIC
64dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid
65dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectsetalias(char *name, char *val)
66dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
67dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	struct alias *ap, **app;
68dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
69dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	app = hashalias(name);
70dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	for (ap = *app; ap; ap = ap->next) {
71dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		if (equal(name, ap->name)) {
72dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			INTOFF;
73dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			ckfree(ap->val);
74dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			ap->val	= savestr(val);
75dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			INTON;
76dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			return;
77dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		}
78dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	}
79dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	/* not found */
80dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	INTOFF;
81dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	ap = ckmalloc(sizeof (struct alias));
82dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	ap->name = savestr(name);
83dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	/*
84dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * XXX - HACK: in order that the parser will not finish reading the
85dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * alias value off the input before processing the next alias, we
86dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * dummy up an extra space at the end of the alias.  This is a crock
87dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * and should be re-thought.  The idea (if you feel inclined to help)
88dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * is to avoid alias recursions.  The mechanism used is: when
89dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * expanding an alias, the value of the alias is pushed back on the
90dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * input as a string and a pointer to the alias is stored with the
91dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * string.  The alias is marked as being in use.  When the input
92dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * routine finishes reading the string, it markes the alias not
93dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * in use.  The problem is synchronization with the parser.  Since
94dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * it reads ahead, the alias is marked not in use before the
95dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * resulting token(s) is next checked for further alias sub.  The
96dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * H A C K is that we add a little fluff after the alias value
97dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * so that the string will not be exhausted.  This is a good
98dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 * idea ------- ***NOT***
99dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	 */
100dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef notyet
101dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	ap->val = savestr(val);
102dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#else /* hack */
103dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	{
104dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	int len = strlen(val);
105dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	ap->val = ckmalloc(len + 2);
106dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	memcpy(ap->val, val, len);
107dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	ap->val[len] = ' ';	/* fluff */
108dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	ap->val[len+1] = '\0';
109dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	}
110dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
111dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	ap->next = *app;
112dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	*app = ap;
113dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	INTON;
114dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
115dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
116dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source ProjectSTATIC int
117dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectunalias(char *name)
118dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
119dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	struct alias *ap, **app;
120dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
121dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	app = hashalias(name);
122dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
123dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	for (ap = *app; ap; app = &(ap->next), ap = ap->next) {
124dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		if (equal(name, ap->name)) {
125dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			/*
126dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			 * if the alias is currently in use (i.e. its
127dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			 * buffer is being used by the input routine) we
128dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			 * just null out the name instead of freeing it.
129dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			 * We could clear it out later, but this situation
130dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			 * is so rare that it hardly seems worth it.
131dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			 */
132dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			if (ap->flag & ALIASINUSE)
133dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				*ap->name = '\0';
134dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			else {
135dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				INTOFF;
136dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				*app = ap->next;
137dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				ckfree(ap->name);
138dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				ckfree(ap->val);
139dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				ckfree(ap);
140dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				INTON;
141dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			}
142dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			return (0);
143dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		}
144dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	}
145dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
146dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	return (1);
147dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
148dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
149dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef mkinit
150dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source ProjectMKINIT void rmaliases(void);
151dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
152dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source ProjectSHELLPROC {
153dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	rmaliases();
154dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
155dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
156dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
157dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid
158dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectrmaliases(void)
159dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
160dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	struct alias *ap, *tmp;
161dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	int i;
162dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
163dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	INTOFF;
164dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	for (i = 0; i < ATABSIZE; i++) {
165dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		ap = atab[i];
166dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		atab[i] = NULL;
167dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		while (ap) {
168dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			ckfree(ap->name);
169dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			ckfree(ap->val);
170dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			tmp = ap;
171dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			ap = ap->next;
172dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			ckfree(tmp);
173dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		}
174dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	}
175dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	INTON;
176dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
177dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
178dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstruct alias *
179dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectlookupalias(char *name, int check)
180dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
181dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	struct alias *ap = *hashalias(name);
182dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
183dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	for (; ap; ap = ap->next) {
184dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		if (equal(name, ap->name)) {
185dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			if (check && (ap->flag & ALIASINUSE))
186dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				return (NULL);
187dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			return (ap);
188dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		}
189dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	}
190dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
191dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	return (NULL);
192dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
193dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
194dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectchar *
195dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectget_alias_text(char *name)
196dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
197dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	struct alias *ap;
198dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
199dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	ap = lookupalias(name, 0);
200dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	if (ap == NULL)
201dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		return NULL;
202dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	return ap->val;
203dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
204dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
205dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*
206dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * TODO - sort output
207dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
208dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint
209dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectaliascmd(int argc, char **argv)
210dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
211dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	char *n, *v;
212dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	int ret = 0;
213dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	struct alias *ap;
214dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
215dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	if (argc == 1) {
216dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		int i;
217dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
218dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		for (i = 0; i < ATABSIZE; i++)
219dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			for (ap = atab[i]; ap; ap = ap->next) {
220dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				if (*ap->name != '\0') {
221dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project					out1fmt("alias %s=", ap->name);
222dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project					print_quoted(ap->val);
223dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project					out1c('\n');
224dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				}
225dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			}
226dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		return (0);
227dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	}
228dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	while ((n = *++argv) != NULL) {
229dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */
230dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			if ((ap = lookupalias(n, 0)) == NULL) {
231dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				outfmt(out2, "alias: %s not found\n", n);
232dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				ret = 1;
233dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			} else {
234dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				out1fmt("alias %s=", n);
235dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				print_quoted(ap->val);
236dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project				out1c('\n');
237dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			}
238dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		} else {
239dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			*v++ = '\0';
240dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			setalias(n, v);
241dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		}
242dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	}
243dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
244dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	return (ret);
245dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
246dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
247dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint
248dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectunaliascmd(int argc, char **argv)
249dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
250dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	int i;
251dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
252dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	while ((i = nextopt("a")) != '\0') {
253dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		if (i == 'a') {
254dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			rmaliases();
255dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project			return (0);
256dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		}
257dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	}
258dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	for (i = 0; *argptr; argptr++)
259dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		i = unalias(*argptr);
260dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
261dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	return (i);
262dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
263dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
264dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source ProjectSTATIC struct alias **
265dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projecthashalias(char *p)
266dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
267dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	unsigned int hashval;
268dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
269dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	hashval = *p << 4;
270dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	while (*p)
271dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project		hashval+= *p++;
272dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project	return &atab[hashval % ATABSIZE];
273dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
274