2009-02-14 10:12:29 +03:00
|
|
|
/* $NetBSD: file.c,v 1.28 2009/02/14 07:12:29 lukem Exp $ */
|
1995-03-21 12:01:59 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1994-09-21 04:10:23 +04:00
|
|
|
* Copyright (c) 1980, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:05:01 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-05 01:23:50 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-03-21 12:01:59 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)file.c 8.2 (Berkeley) 3/19/94";
|
|
|
|
#else
|
2009-02-14 10:12:29 +03:00
|
|
|
__RCSID("$NetBSD: file.c,v 1.28 2009/02/14 07:12:29 lukem Exp $");
|
1995-03-21 12:01:59 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifdef FILEC
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
2001-09-14 18:03:59 +04:00
|
|
|
#include <sys/param.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/stat.h>
|
1999-03-23 12:29:51 +03:00
|
|
|
#include <sys/tty.h>
|
2001-09-14 18:03:59 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <pwd.h>
|
2001-09-14 18:03:59 +04:00
|
|
|
#include <termios.h>
|
2002-05-26 03:29:16 +04:00
|
|
|
#include <stdarg.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2001-09-14 18:03:59 +04:00
|
|
|
|
1994-09-21 04:10:23 +04:00
|
|
|
#ifndef SHORT_STRINGS
|
|
|
|
#include <string.h>
|
|
|
|
#endif /* SHORT_STRINGS */
|
2001-09-14 18:03:59 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "csh.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tenex style file name recognition, .. and more.
|
|
|
|
* History:
|
|
|
|
* Author: Ken Greer, Sept. 1975, CMU.
|
|
|
|
* Finally got around to adding to the Cshell., Ken Greer, Dec. 1981.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ON 1
|
|
|
|
#define OFF 0
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE 1
|
|
|
|
#endif
|
|
|
|
#ifndef FALSE
|
|
|
|
#define FALSE 0
|
|
|
|
#endif
|
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
#define ESC '\033'
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
LIST, RECOGNIZE
|
|
|
|
} COMMAND;
|
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
static void setup_tty(int);
|
|
|
|
static void back_to_col_1(void);
|
|
|
|
static int pushback(Char *);
|
|
|
|
static void catn(Char *, Char *, int);
|
|
|
|
static void copyn(Char *, Char *, int);
|
|
|
|
static Char filetype(Char *, Char *);
|
|
|
|
static void print_by_column(Char *, Char *[], int);
|
|
|
|
static Char *tilde(Char *, Char *);
|
|
|
|
static void retype(void);
|
|
|
|
static void beep(void);
|
|
|
|
static void print_recognized_stuff(Char *);
|
|
|
|
static void extract_dir_and_name(Char *, Char *, Char *);
|
|
|
|
static Char *getentry(DIR *, int);
|
2003-02-08 22:05:19 +03:00
|
|
|
static void free_items(Char **, size_t);
|
2001-09-14 18:03:59 +04:00
|
|
|
static int tsearch(Char *, COMMAND, int);
|
|
|
|
static int recognize(Char *, Char *, int, int);
|
|
|
|
static int is_prefix(Char *, Char *);
|
|
|
|
static int is_suffix(Char *, Char *);
|
|
|
|
static int ignored(Char *);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Put this here so the binary can be patched with adb to enable file
|
|
|
|
* completion by default. Filec controls completion, nobeep controls
|
|
|
|
* ringing the terminal bell on incomplete expansions.
|
|
|
|
*/
|
2007-07-16 22:26:09 +04:00
|
|
|
int filec = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
setup_tty(int on)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-03-09 07:23:37 +03:00
|
|
|
struct termios tchars;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)tcgetattr(SHIN, &tchars);
|
1994-09-21 04:10:23 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (on) {
|
|
|
|
tchars.c_cc[VEOL] = ESC;
|
|
|
|
if (tchars.c_lflag & ICANON)
|
1994-04-28 19:57:41 +04:00
|
|
|
on = TCSADRAIN;
|
1993-03-21 12:45:37 +03:00
|
|
|
else {
|
|
|
|
tchars.c_lflag |= ICANON;
|
1994-09-21 04:10:23 +04:00
|
|
|
on = TCSAFLUSH;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tchars.c_cc[VEOL] = _POSIX_VDISABLE;
|
1994-09-21 04:10:23 +04:00
|
|
|
on = TCSADRAIN;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-09-21 04:10:23 +04:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)tcsetattr(SHIN, on, &tchars);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move back to beginning of current line
|
|
|
|
*/
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
back_to_col_1(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct termios tty, tty_normal;
|
2003-01-16 12:38:37 +03:00
|
|
|
sigset_t nsigset, osigset;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-01-16 12:38:37 +03:00
|
|
|
sigemptyset(&nsigset);
|
|
|
|
(void)sigaddset(&nsigset, SIGINT);
|
|
|
|
(void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)tcgetattr(SHOUT, &tty);
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_normal = tty;
|
|
|
|
tty.c_iflag &= ~INLCR;
|
|
|
|
tty.c_oflag &= ~ONLCR;
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)tcsetattr(SHOUT, TCSADRAIN, &tty);
|
|
|
|
(void)write(SHOUT, "\r", 1);
|
|
|
|
(void)tcsetattr(SHOUT, TCSADRAIN, &tty_normal);
|
|
|
|
(void)sigprocmask(SIG_SETMASK, &osigset, NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Push string contents back into tty queue
|
|
|
|
*/
|
1998-12-26 05:11:39 +03:00
|
|
|
static int
|
2001-09-14 18:03:59 +04:00
|
|
|
pushback(Char *string)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct termios tty, tty_normal;
|
2001-09-14 18:03:59 +04:00
|
|
|
char buf[TTYHOG], svchars[TTYHOG];
|
2003-01-16 12:38:37 +03:00
|
|
|
sigset_t nsigset, osigset;
|
2001-09-14 18:03:59 +04:00
|
|
|
Char *p;
|
|
|
|
int bufidx, i, len_str, nbuf, nsv, onsv, retrycnt;
|
|
|
|
char c;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
nsv = 0;
|
2003-01-16 12:38:37 +03:00
|
|
|
sigemptyset(&nsigset);
|
|
|
|
(void)sigaddset(&nsigset, SIGINT);
|
|
|
|
(void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)tcgetattr(SHOUT, &tty);
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_normal = tty;
|
|
|
|
tty.c_lflag &= ~(ECHOKE | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOCTL);
|
1998-12-26 05:11:39 +03:00
|
|
|
/* FIONREAD works only in noncanonical mode. */
|
|
|
|
tty.c_lflag &= ~ICANON;
|
|
|
|
tty.c_cc[VMIN] = 0;
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)tcsetattr(SHOUT, TCSADRAIN, &tty);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-12-26 05:11:39 +03:00
|
|
|
for (retrycnt = 5; ; retrycnt--) {
|
|
|
|
/*
|
|
|
|
* Push back characters.
|
|
|
|
*/
|
|
|
|
for (p = string; (c = *p) != '\0'; p++)
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)ioctl(SHOUT, TIOCSTI, (ioctl_t) &c);
|
1998-12-26 05:11:39 +03:00
|
|
|
for (i = 0; i < nsv; i++)
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)ioctl(SHOUT, TIOCSTI, (ioctl_t) &svchars[i]);
|
1998-12-26 05:11:39 +03:00
|
|
|
|
|
|
|
if (retrycnt == 0)
|
|
|
|
break; /* give up salvaging characters */
|
|
|
|
|
|
|
|
len_str = p - string;
|
|
|
|
|
|
|
|
if (ioctl(SHOUT, FIONREAD, (ioctl_t) &nbuf) ||
|
|
|
|
nbuf <= len_str + nsv || /* The string fit. */
|
|
|
|
nbuf > TTYHOG) /* For future binary compatibility
|
|
|
|
(and safety). */
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* User has typed characters before the pushback finished.
|
|
|
|
* Salvage the characters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This read() should be in noncanonical mode. */
|
|
|
|
if (read(SHOUT, &buf, nbuf) != nbuf)
|
|
|
|
continue; /* hangup? */
|
|
|
|
|
|
|
|
onsv = nsv;
|
|
|
|
for (bufidx = 0, i = 0; bufidx < nbuf; bufidx++, i++) {
|
|
|
|
c = buf[bufidx];
|
2001-09-14 18:03:59 +04:00
|
|
|
if ((i < len_str) ? c != (char)string[i] :
|
1998-12-26 05:11:39 +03:00
|
|
|
(i < len_str + onsv) ? c != svchars[i - len_str] : 1) {
|
|
|
|
/* Salvage a character. */
|
2001-09-14 18:03:59 +04:00
|
|
|
if (nsv < (int)(sizeof svchars / sizeof svchars[0])) {
|
1998-12-26 05:11:39 +03:00
|
|
|
svchars[nsv++] = c;
|
|
|
|
i--; /* try this comparison with the next char */
|
|
|
|
} else
|
1999-03-23 12:29:51 +03:00
|
|
|
break; /* too many */
|
1998-12-26 05:11:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
/*
|
|
|
|
* XXX Is this a bug or a feature of kernel tty driver?
|
|
|
|
*
|
|
|
|
* FIONREAD in canonical mode does not return correct byte count
|
|
|
|
* in tty input queue, but this is required to avoid unwanted echo.
|
|
|
|
*/
|
|
|
|
tty.c_lflag |= ICANON;
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)tcsetattr(SHOUT, TCSADRAIN, &tty);
|
|
|
|
(void)ioctl(SHOUT, FIONREAD, (ioctl_t) &i);
|
1998-12-26 05:11:39 +03:00
|
|
|
#endif
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)tcsetattr(SHOUT, TCSADRAIN, &tty_normal);
|
|
|
|
(void)sigprocmask(SIG_SETMASK, &osigset, NULL);
|
1998-12-26 05:11:39 +03:00
|
|
|
|
|
|
|
return nsv;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Concatenate src onto tail of des.
|
|
|
|
* Des is a string whose maximum length is count.
|
|
|
|
* Always null terminate.
|
|
|
|
*/
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
catn(Char *des, Char *src, int count)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
while (--count >= 0 && *des)
|
|
|
|
des++;
|
|
|
|
while (--count >= 0)
|
|
|
|
if ((*des++ = *src++) == 0)
|
|
|
|
return;
|
|
|
|
*des = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Like strncpy but always leave room for trailing \0
|
|
|
|
* and always null terminate.
|
|
|
|
*/
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
copyn(Char *des, Char *src, int count)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
while (--count >= 0)
|
|
|
|
if ((*des++ = *src++) == 0)
|
|
|
|
return;
|
|
|
|
*des = '\0';
|
|
|
|
}
|
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
static Char
|
|
|
|
filetype(Char *dir, Char *file)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct stat statb;
|
2001-09-14 18:03:59 +04:00
|
|
|
Char path[MAXPATHLEN];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
catn(Strcpy(path, dir), file, sizeof(path) / sizeof(Char));
|
|
|
|
if (lstat(short2str(path), &statb) == 0) {
|
|
|
|
switch (statb.st_mode & S_IFMT) {
|
|
|
|
case S_IFDIR:
|
|
|
|
return ('/');
|
|
|
|
case S_IFLNK:
|
|
|
|
if (stat(short2str(path), &statb) == 0 && /* follow it out */
|
|
|
|
S_ISDIR(statb.st_mode))
|
|
|
|
return ('>');
|
|
|
|
else
|
|
|
|
return ('@');
|
|
|
|
case S_IFSOCK:
|
|
|
|
return ('=');
|
|
|
|
default:
|
|
|
|
if (statb.st_mode & 0111)
|
|
|
|
return ('*');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct winsize win;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print sorted down columns
|
|
|
|
*/
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
print_by_column(Char *dir, Char *items[], int count)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
int c, columns, i, maxwidth, r, rows;
|
|
|
|
|
|
|
|
maxwidth = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (ioctl(SHOUT, TIOCGWINSZ, (ioctl_t) & win) < 0 || win.ws_col == 0)
|
|
|
|
win.ws_col = 80;
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
maxwidth = maxwidth > (r = Strlen(items[i])) ? maxwidth : r;
|
|
|
|
maxwidth += 2; /* for the file tag and space */
|
|
|
|
columns = win.ws_col / maxwidth;
|
|
|
|
if (columns == 0)
|
|
|
|
columns = 1;
|
|
|
|
rows = (count + (columns - 1)) / columns;
|
|
|
|
for (r = 0; r < rows; r++) {
|
|
|
|
for (c = 0; c < columns; c++) {
|
|
|
|
i = c * rows + r;
|
|
|
|
if (i < count) {
|
1997-01-13 20:53:15 +03:00
|
|
|
int w;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fprintf(cshout, "%s", vis_str(items[i]));
|
|
|
|
(void)fputc(dir ? filetype(dir, items[i]) : ' ', cshout);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (c < columns - 1) { /* last column? */
|
|
|
|
w = Strlen(items[i]) + 1;
|
|
|
|
for (; w < maxwidth; w++)
|
1994-09-21 04:10:23 +04:00
|
|
|
(void) fputc(' ', cshout);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fputc('\r', cshout);
|
|
|
|
(void)fputc('\n', cshout);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand file name with possible tilde usage
|
|
|
|
* ~person/mumble
|
|
|
|
* expands to
|
|
|
|
* home_directory_of_person/mumble
|
|
|
|
*/
|
|
|
|
static Char *
|
2001-09-14 18:03:59 +04:00
|
|
|
tilde(Char *new, Char *old)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
static Char person[40];
|
2001-09-14 18:03:59 +04:00
|
|
|
struct passwd *pw;
|
|
|
|
Char *o, *p;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (old[0] != '~')
|
|
|
|
return (Strcpy(new, old));
|
|
|
|
|
1994-09-21 04:10:23 +04:00
|
|
|
for (p = person, o = &old[1]; *o && *o != '/'; *p++ = *o++)
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
*p = '\0';
|
|
|
|
if (person[0] == '\0')
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)Strcpy(new, value(STRhome));
|
1993-03-21 12:45:37 +03:00
|
|
|
else {
|
|
|
|
pw = getpwnam(short2str(person));
|
|
|
|
if (pw == NULL)
|
|
|
|
return (NULL);
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)Strcpy(new, str2short(pw->pw_dir));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)Strcat(new, o);
|
1993-03-21 12:45:37 +03:00
|
|
|
return (new);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cause pending line to be printed
|
|
|
|
*/
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
retype(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct termios tty;
|
|
|
|
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)tcgetattr(SHOUT, &tty);
|
1993-03-21 12:45:37 +03:00
|
|
|
tty.c_lflag |= PENDIN;
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)tcsetattr(SHOUT, TCSADRAIN, &tty);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
beep(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
if (adrof(STRnobeep) == 0)
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)write(SHOUT, "\007", 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Erase that silly ^[ and
|
|
|
|
* print the recognized part of the string
|
|
|
|
*/
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
print_recognized_stuff(Char *recognized_part)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
/* An optimized erasing of that silly ^[ */
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fputc('\b', cshout);
|
|
|
|
(void)fputc('\b', cshout);
|
1993-03-21 12:45:37 +03:00
|
|
|
switch (Strlen(recognized_part)) {
|
|
|
|
case 0: /* erase two Characters: ^[ */
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fputc(' ', cshout);
|
|
|
|
(void)fputc(' ', cshout);
|
|
|
|
(void)fputc('\b', cshout);
|
|
|
|
(void)fputc('\b', cshout);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case 1: /* overstrike the ^, erase the [ */
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fprintf(cshout, "%s", vis_str(recognized_part));
|
|
|
|
(void)fputc(' ', cshout);
|
|
|
|
(void)fputc('\b', cshout);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
default: /* overstrike both Characters ^[ */
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fprintf(cshout, "%s", vis_str(recognized_part));
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fflush(cshout);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse full path in file into 2 parts: directory and file names
|
|
|
|
* Should leave final slash (/) at end of dir.
|
|
|
|
*/
|
|
|
|
static void
|
2001-09-14 18:03:59 +04:00
|
|
|
extract_dir_and_name(Char *path, Char *dir, Char *name)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
Char *p;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
p = Strrchr(path, '/');
|
|
|
|
if (p == NULL) {
|
|
|
|
copyn(name, path, MAXNAMLEN);
|
|
|
|
dir[0] = '\0';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
copyn(name, ++p, MAXNAMLEN);
|
|
|
|
copyn(dir, path, p - path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Char *
|
2001-09-14 18:03:59 +04:00
|
|
|
getentry(DIR *dir_fd, int looking_for_lognames)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
struct dirent *dirp;
|
2001-09-14 18:03:59 +04:00
|
|
|
struct passwd *pw;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (looking_for_lognames) {
|
|
|
|
if ((pw = getpwent()) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
return (str2short(pw->pw_name));
|
|
|
|
}
|
1994-09-21 04:10:23 +04:00
|
|
|
if ((dirp = readdir(dir_fd)) != NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
return (str2short(dirp->d_name));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-02-08 22:05:19 +03:00
|
|
|
free_items(Char **items, size_t numitems)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2003-02-08 22:05:19 +03:00
|
|
|
size_t i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-02-08 22:05:19 +03:00
|
|
|
for (i = 0; i < numitems; i++)
|
1993-03-21 12:45:37 +03:00
|
|
|
xfree((ptr_t) items[i]);
|
|
|
|
xfree((ptr_t) items);
|
|
|
|
}
|
|
|
|
|
2003-02-08 22:05:19 +03:00
|
|
|
#define FREE_ITEMS(items, numitems) { \
|
2003-01-16 12:38:37 +03:00
|
|
|
sigset_t nsigset, osigset;\
|
1993-03-21 12:45:37 +03:00
|
|
|
\
|
2003-01-16 12:38:37 +03:00
|
|
|
sigemptyset(&nsigset);\
|
|
|
|
(void) sigaddset(&nsigset, SIGINT);\
|
|
|
|
(void) sigprocmask(SIG_BLOCK, &nsigset, &osigset);\
|
2003-02-08 22:05:19 +03:00
|
|
|
free_items(items, numitems);\
|
1998-07-28 06:47:19 +04:00
|
|
|
(void) sigprocmask(SIG_SETMASK, &osigset, NULL);\
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform a RECOGNIZE or LIST command on string "word".
|
|
|
|
*/
|
|
|
|
static int
|
2001-09-14 18:03:59 +04:00
|
|
|
tsearch(Char *word, COMMAND command, int max_word_length)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
Char dir[MAXPATHLEN + 1], extended_name[MAXNAMLEN + 1];
|
|
|
|
Char name[MAXNAMLEN + 1], tilded_dir[MAXPATHLEN + 1];
|
1997-01-13 20:53:15 +03:00
|
|
|
DIR *dir_fd;
|
2001-09-14 18:03:59 +04:00
|
|
|
Char *entry;
|
|
|
|
int ignoring, looking_for_lognames, name_length, nignored, numitems;
|
2003-02-08 22:05:19 +03:00
|
|
|
Char **items = NULL;
|
|
|
|
size_t maxitems = 0;
|
2001-09-14 18:03:59 +04:00
|
|
|
|
|
|
|
numitems = 0;
|
|
|
|
ignoring = TRUE;
|
|
|
|
nignored = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
looking_for_lognames = (*word == '~') && (Strchr(word, '/') == NULL);
|
|
|
|
if (looking_for_lognames) {
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)setpwent();
|
1993-03-21 12:45:37 +03:00
|
|
|
copyn(name, &word[1], MAXNAMLEN); /* name sans ~ */
|
|
|
|
dir_fd = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
extract_dir_and_name(word, dir, name);
|
|
|
|
if (tilde(tilded_dir, dir) == 0)
|
|
|
|
return (0);
|
|
|
|
dir_fd = opendir(*tilded_dir ? short2str(tilded_dir) : ".");
|
|
|
|
if (dir_fd == NULL)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
again: /* search for matches */
|
|
|
|
name_length = Strlen(name);
|
1994-09-21 04:10:23 +04:00
|
|
|
for (numitems = 0; (entry = getentry(dir_fd, looking_for_lognames)) != NULL;) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!is_prefix(name, entry))
|
|
|
|
continue;
|
|
|
|
/* Don't match . files on null prefix match */
|
|
|
|
if (name_length == 0 && entry[0] == '.' &&
|
|
|
|
!looking_for_lognames)
|
|
|
|
continue;
|
|
|
|
if (command == LIST) {
|
2009-02-14 10:12:29 +03:00
|
|
|
if ((size_t)numitems >= maxitems) {
|
2003-02-08 22:05:19 +03:00
|
|
|
maxitems += 1024;
|
|
|
|
if (items == NULL)
|
|
|
|
items = (Char **) xmalloc(sizeof(*items) * maxitems);
|
|
|
|
else
|
|
|
|
items = (Char **) xrealloc((ptr_t) items,
|
|
|
|
sizeof(*items) * maxitems);
|
|
|
|
}
|
2001-09-14 18:03:59 +04:00
|
|
|
items[numitems] = (Char *)xmalloc((size_t) (Strlen(entry) + 1) *
|
|
|
|
sizeof(Char));
|
1993-03-21 12:45:37 +03:00
|
|
|
copyn(items[numitems], entry, MAXNAMLEN);
|
|
|
|
numitems++;
|
|
|
|
}
|
|
|
|
else { /* RECOGNIZE command */
|
|
|
|
if (ignoring && ignored(entry))
|
|
|
|
nignored++;
|
|
|
|
else if (recognize(extended_name,
|
2001-09-14 18:03:59 +04:00
|
|
|
entry, name_length, ++numitems))
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ignoring && numitems == 0 && nignored > 0) {
|
|
|
|
ignoring = FALSE;
|
|
|
|
nignored = 0;
|
|
|
|
if (looking_for_lognames)
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)setpwent();
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
|
|
|
rewinddir(dir_fd);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (looking_for_lognames)
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)endpwent();
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)closedir(dir_fd);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (numitems == 0)
|
|
|
|
return (0);
|
|
|
|
if (command == RECOGNIZE) {
|
|
|
|
if (looking_for_lognames)
|
|
|
|
copyn(word, STRtilde, 1);
|
|
|
|
else
|
|
|
|
/* put back dir part */
|
|
|
|
copyn(word, dir, max_word_length);
|
|
|
|
/* add extended name */
|
|
|
|
catn(word, extended_name, max_word_length);
|
|
|
|
return (numitems);
|
|
|
|
}
|
|
|
|
else { /* LIST */
|
|
|
|
qsort((ptr_t) items, numitems, sizeof(items[0]),
|
2001-09-14 18:03:59 +04:00
|
|
|
(int (*) (const void *, const void *)) sortscmp);
|
1993-03-21 12:45:37 +03:00
|
|
|
print_by_column(looking_for_lognames ? NULL : tilded_dir,
|
|
|
|
items, numitems);
|
|
|
|
if (items != NULL)
|
2003-02-08 22:05:19 +03:00
|
|
|
FREE_ITEMS(items, numitems);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Object: extend what user typed up to an ambiguity.
|
|
|
|
* Algorithm:
|
|
|
|
* On first match, copy full entry (assume it'll be the only match)
|
|
|
|
* On subsequent matches, shorten extended_name to the first
|
|
|
|
* Character mismatch between extended_name and entry.
|
|
|
|
* If we shorten it back to the prefix length, stop searching.
|
|
|
|
*/
|
|
|
|
static int
|
2001-09-14 18:03:59 +04:00
|
|
|
recognize(Char *extended_name, Char *entry, int name_length, int numitems)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
if (numitems == 1) /* 1st match */
|
|
|
|
copyn(extended_name, entry, MAXNAMLEN);
|
|
|
|
else { /* 2nd & subsequent matches */
|
2001-09-14 18:03:59 +04:00
|
|
|
Char *ent, *x;
|
1997-01-13 20:53:15 +03:00
|
|
|
int len = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
x = extended_name;
|
1994-09-21 04:10:23 +04:00
|
|
|
for (ent = entry; *x && *x == *ent++; x++, len++)
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
*x = '\0'; /* Shorten at 1st Char diff */
|
|
|
|
if (len == name_length) /* Ambiguous to prefix? */
|
|
|
|
return (-1); /* So stop now and save time */
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return true if check matches initial Chars in template.
|
|
|
|
* This differs from PWB imatch in that if check is null
|
|
|
|
* it matches anything.
|
|
|
|
*/
|
|
|
|
static int
|
2001-09-14 18:03:59 +04:00
|
|
|
is_prefix(Char *check, Char *template)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
do
|
|
|
|
if (*check == 0)
|
|
|
|
return (TRUE);
|
|
|
|
while (*check++ == *template++);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return true if the Chars in template appear at the
|
2006-04-24 23:00:29 +04:00
|
|
|
* end of check, I.e., are its suffix.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
static int
|
2001-09-14 18:03:59 +04:00
|
|
|
is_suffix(Char *check, Char *template)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-01-13 20:53:15 +03:00
|
|
|
Char *c, *t;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-09-21 04:10:23 +04:00
|
|
|
for (c = check; *c++;)
|
|
|
|
continue;
|
|
|
|
for (t = template; *t++;)
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
for (;;) {
|
|
|
|
if (t == template)
|
|
|
|
return 1;
|
|
|
|
if (c == check || *--t != *--c)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-14 18:03:59 +04:00
|
|
|
tenex(Char *inputline, int inputline_size)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2001-09-14 18:03:59 +04:00
|
|
|
char tinputline[BUFSIZE];
|
|
|
|
int num_read, numitems;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
setup_tty(ON);
|
|
|
|
|
2000-06-01 02:48:44 +04:00
|
|
|
while ((num_read = read(SHIN, tinputline, BUFSIZE)) > 0) {
|
2001-09-14 18:03:59 +04:00
|
|
|
int i;
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
static Char delims[] = {' ', '\'', '"', '\t', ';', '&', '<',
|
|
|
|
'>', '(', ')', '|', '^', '%', '\0'};
|
1997-01-13 20:53:15 +03:00
|
|
|
Char *str_end, *word_start, last_Char, should_retype;
|
|
|
|
int space_left;
|
1993-03-21 12:45:37 +03:00
|
|
|
COMMAND command;
|
|
|
|
|
|
|
|
for (i = 0; i < num_read; i++)
|
|
|
|
inputline[i] = (unsigned char) tinputline[i];
|
|
|
|
last_Char = inputline[num_read - 1] & ASCII;
|
|
|
|
|
|
|
|
if (last_Char == '\n' || num_read == inputline_size)
|
|
|
|
break;
|
|
|
|
command = (last_Char == ESC) ? RECOGNIZE : LIST;
|
|
|
|
if (command == LIST)
|
2001-09-14 18:03:59 +04:00
|
|
|
(void)fputc('\n', cshout);
|
1993-03-21 12:45:37 +03:00
|
|
|
str_end = &inputline[num_read];
|
|
|
|
if (last_Char == ESC)
|
|
|
|
--str_end; /* wipeout trailing cmd Char */
|
|
|
|
*str_end = '\0';
|
|
|
|
/*
|
|
|
|
* Find LAST occurence of a delimiter in the inputline. The word start
|
|
|
|
* is one Character past it.
|
|
|
|
*/
|
|
|
|
for (word_start = str_end; word_start > inputline; --word_start)
|
|
|
|
if (Strchr(delims, word_start[-1]))
|
|
|
|
break;
|
|
|
|
space_left = inputline_size - (word_start - inputline) - 1;
|
|
|
|
numitems = tsearch(word_start, command, space_left);
|
|
|
|
|
|
|
|
if (command == RECOGNIZE) {
|
|
|
|
/* print from str_end on */
|
|
|
|
print_recognized_stuff(str_end);
|
|
|
|
if (numitems != 1) /* Beep = No match/ambiguous */
|
|
|
|
beep();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tabs in the input line cause trouble after a pushback. tty driver
|
|
|
|
* won't backspace over them because column positions are now
|
|
|
|
* incorrect. This is solved by retyping over current line.
|
|
|
|
*/
|
|
|
|
should_retype = FALSE;
|
|
|
|
if (Strchr(inputline, '\t')) { /* tab Char in input line? */
|
|
|
|
back_to_col_1();
|
|
|
|
should_retype = TRUE;
|
|
|
|
}
|
|
|
|
if (command == LIST) /* Always retype after a LIST */
|
|
|
|
should_retype = TRUE;
|
1998-12-26 05:11:39 +03:00
|
|
|
if (pushback(inputline))
|
|
|
|
should_retype = TRUE;
|
|
|
|
if (should_retype) {
|
|
|
|
if (command == RECOGNIZE)
|
|
|
|
(void) fputc('\n', cshout);
|
1993-03-21 12:45:37 +03:00
|
|
|
printprompt();
|
1998-12-26 05:11:39 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
if (should_retype)
|
|
|
|
retype();
|
|
|
|
}
|
|
|
|
setup_tty(OFF);
|
|
|
|
return (num_read);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-14 18:03:59 +04:00
|
|
|
ignored(Char *entry)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct varent *vp;
|
1997-01-13 20:53:15 +03:00
|
|
|
Char **cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if ((vp = adrof(STRfignore)) == NULL || (cp = vp->vec) == NULL)
|
|
|
|
return (FALSE);
|
|
|
|
for (; *cp != NULL; cp++)
|
|
|
|
if (is_suffix(entry, *cp))
|
|
|
|
return (TRUE);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
#endif /* FILEC */
|