17c478bd9Sstevel@tonic-gate /* 2*6c02b4a4Smuffin * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 37c478bd9Sstevel@tonic-gate * Use is subject to license terms. 47c478bd9Sstevel@tonic-gate */ 57c478bd9Sstevel@tonic-gate 67c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 77c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 87c478bd9Sstevel@tonic-gate 97c478bd9Sstevel@tonic-gate /* 107c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California. 117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley Software License Agreement 127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 137c478bd9Sstevel@tonic-gate */ 147c478bd9Sstevel@tonic-gate 157c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 167c478bd9Sstevel@tonic-gate 177c478bd9Sstevel@tonic-gate #ifdef FILEC 187c478bd9Sstevel@tonic-gate /* 197c478bd9Sstevel@tonic-gate * Tenex style file name recognition, .. and more. 207c478bd9Sstevel@tonic-gate * History: 217c478bd9Sstevel@tonic-gate * Author: Ken Greer, Sept. 1975, CMU. 227c478bd9Sstevel@tonic-gate * Finally got around to adding to the Cshell., Ken Greer, Dec. 1981. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate #include "sh.h" 267c478bd9Sstevel@tonic-gate #include <sys/types.h> 277c478bd9Sstevel@tonic-gate #include <dirent.h> 287c478bd9Sstevel@tonic-gate #include <pwd.h> 297c478bd9Sstevel@tonic-gate #include "sh.tconst.h" 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #define TRUE 1 327c478bd9Sstevel@tonic-gate #define FALSE 0 337c478bd9Sstevel@tonic-gate #define ON 1 347c478bd9Sstevel@tonic-gate #define OFF 0 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #define ESC '\033' 377c478bd9Sstevel@tonic-gate 38*6c02b4a4Smuffin extern DIR *opendir_(tchar *); 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate static char *BELL = "\07"; 417c478bd9Sstevel@tonic-gate static char *CTRLR = "^R\n"; 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate typedef enum {LIST, RECOGNIZE} COMMAND; 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate static jmp_buf osetexit; /* saved setexit() state */ 467c478bd9Sstevel@tonic-gate static struct termios tty_save; /* saved terminal state */ 477c478bd9Sstevel@tonic-gate static struct termios tty_new; /* new terminal state */ 487c478bd9Sstevel@tonic-gate 49*6c02b4a4Smuffin static int is_prefix(tchar *, tchar *); 50*6c02b4a4Smuffin static int is_suffix(tchar *, tchar *); 51*6c02b4a4Smuffin static int ignored(tchar *); 52*6c02b4a4Smuffin 537c478bd9Sstevel@tonic-gate /* 547c478bd9Sstevel@tonic-gate * Put this here so the binary can be patched with adb to enable file 557c478bd9Sstevel@tonic-gate * completion by default. Filec controls completion, nobeep controls 567c478bd9Sstevel@tonic-gate * ringing the terminal bell on incomplete expansions. 577c478bd9Sstevel@tonic-gate */ 587c478bd9Sstevel@tonic-gate bool filec = 0; 597c478bd9Sstevel@tonic-gate 60*6c02b4a4Smuffin static void 61*6c02b4a4Smuffin setup_tty(int on) 627c478bd9Sstevel@tonic-gate { 637c478bd9Sstevel@tonic-gate int omask; 647c478bd9Sstevel@tonic-gate #ifdef TRACE 657c478bd9Sstevel@tonic-gate tprintf("TRACE- setup_tty()\n"); 667c478bd9Sstevel@tonic-gate #endif 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate omask = sigblock(sigmask(SIGINT)); 697c478bd9Sstevel@tonic-gate if (on) { 707c478bd9Sstevel@tonic-gate /* 717c478bd9Sstevel@tonic-gate * The shell makes sure that the tty is not in some weird state 727c478bd9Sstevel@tonic-gate * and fixes it if it is. But it should be noted that the 737c478bd9Sstevel@tonic-gate * tenex routine will not work correctly in CBREAK or RAW mode 747c478bd9Sstevel@tonic-gate * so this code below is, therefore, mandatory. 757c478bd9Sstevel@tonic-gate * 767c478bd9Sstevel@tonic-gate * Also, in order to recognize the ESC (filename-completion) 777c478bd9Sstevel@tonic-gate * character, set EOL to ESC. This way, ESC will terminate 787c478bd9Sstevel@tonic-gate * the line, but still be in the input stream. 797c478bd9Sstevel@tonic-gate * EOT (filename list) will also terminate the line, 807c478bd9Sstevel@tonic-gate * but will not appear in the input stream. 817c478bd9Sstevel@tonic-gate * 827c478bd9Sstevel@tonic-gate * The getexit/setexit contortions ensure that the 837c478bd9Sstevel@tonic-gate * tty state will be restored if the user types ^C. 847c478bd9Sstevel@tonic-gate */ 857c478bd9Sstevel@tonic-gate (void) ioctl(SHIN, TCGETS, (char *)&tty_save); 867c478bd9Sstevel@tonic-gate getexit(osetexit); 877c478bd9Sstevel@tonic-gate if (setjmp(reslab)) { 887c478bd9Sstevel@tonic-gate (void) ioctl(SHIN, TCSETSW, (char *)&tty_save); 897c478bd9Sstevel@tonic-gate resexit(osetexit); 907c478bd9Sstevel@tonic-gate reset(); 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate tty_new = tty_save; 937c478bd9Sstevel@tonic-gate tty_new.c_cc[VEOL] = ESC; 947c478bd9Sstevel@tonic-gate tty_new.c_iflag |= IMAXBEL | BRKINT | IGNPAR; 957c478bd9Sstevel@tonic-gate tty_new.c_lflag |= ICANON; 967c478bd9Sstevel@tonic-gate tty_new.c_lflag |= ECHOCTL; 977c478bd9Sstevel@tonic-gate tty_new.c_oflag &= ~OCRNL; 987c478bd9Sstevel@tonic-gate (void) ioctl(SHIN, TCSETSW, (char *)&tty_new); 997c478bd9Sstevel@tonic-gate } else { 1007c478bd9Sstevel@tonic-gate /* 1017c478bd9Sstevel@tonic-gate * Reset terminal state to what user had when invoked 1027c478bd9Sstevel@tonic-gate */ 1037c478bd9Sstevel@tonic-gate (void) ioctl(SHIN, TCSETSW, (char *)&tty_save); 1047c478bd9Sstevel@tonic-gate resexit(osetexit); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate (void) sigsetmask(omask); 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 109*6c02b4a4Smuffin static void 110*6c02b4a4Smuffin termchars(void) 1117c478bd9Sstevel@tonic-gate { 1127c478bd9Sstevel@tonic-gate extern char *tgetstr(); 1137c478bd9Sstevel@tonic-gate char bp[1024]; 1147c478bd9Sstevel@tonic-gate static char area[256]; 1157c478bd9Sstevel@tonic-gate static int been_here = 0; 1167c478bd9Sstevel@tonic-gate char *ap = area; 117*6c02b4a4Smuffin char *s; 1187c478bd9Sstevel@tonic-gate char *term; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate #ifdef TRACE 1217c478bd9Sstevel@tonic-gate tprintf("TRACE- termchars()\n"); 1227c478bd9Sstevel@tonic-gate #endif 1237c478bd9Sstevel@tonic-gate if (been_here) 1247c478bd9Sstevel@tonic-gate return; 1257c478bd9Sstevel@tonic-gate been_here = TRUE; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate if ((term = getenv("TERM")) == NULL) 1287c478bd9Sstevel@tonic-gate return; 1297c478bd9Sstevel@tonic-gate if (tgetent(bp, term) != 1) 1307c478bd9Sstevel@tonic-gate return; 1317c478bd9Sstevel@tonic-gate if (s = tgetstr("vb", &ap)) /* Visible Bell */ 1327c478bd9Sstevel@tonic-gate BELL = s; 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate /* 1367c478bd9Sstevel@tonic-gate * Move back to beginning of current line 1377c478bd9Sstevel@tonic-gate */ 138*6c02b4a4Smuffin static void 139*6c02b4a4Smuffin back_to_col_1(void) 1407c478bd9Sstevel@tonic-gate { 1417c478bd9Sstevel@tonic-gate int omask; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate #ifdef TRACE 1447c478bd9Sstevel@tonic-gate tprintf("TRACE- back_to_col_1()\n"); 1457c478bd9Sstevel@tonic-gate #endif 1467c478bd9Sstevel@tonic-gate omask = sigblock(sigmask(SIGINT)); 1477c478bd9Sstevel@tonic-gate (void) write(SHOUT, "\r", 1); 1487c478bd9Sstevel@tonic-gate (void) sigsetmask(omask); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* 1527c478bd9Sstevel@tonic-gate * Push string contents back into tty queue 1537c478bd9Sstevel@tonic-gate */ 154*6c02b4a4Smuffin static void 155*6c02b4a4Smuffin pushback(tchar *string, int echoflag) 1567c478bd9Sstevel@tonic-gate { 157*6c02b4a4Smuffin tchar *p; 1587c478bd9Sstevel@tonic-gate struct termios tty; 1597c478bd9Sstevel@tonic-gate int omask; 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate #ifdef TRACE 1627c478bd9Sstevel@tonic-gate tprintf("TRACE- pushback()\n"); 1637c478bd9Sstevel@tonic-gate #endif 1647c478bd9Sstevel@tonic-gate omask = sigblock(sigmask(SIGINT)); 1657c478bd9Sstevel@tonic-gate tty = tty_new; 1667c478bd9Sstevel@tonic-gate if (!echoflag) 1677c478bd9Sstevel@tonic-gate tty.c_lflag &= ~ECHO; 1687c478bd9Sstevel@tonic-gate (void) ioctl(SHIN, TCSETSF, (char *)&tty); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate for (p = string; *p; p++){ 1717c478bd9Sstevel@tonic-gate char mbc[MB_LEN_MAX]; 1727c478bd9Sstevel@tonic-gate int i, j = wctomb(mbc, (wchar_t)*p); 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate if (j < 0) { 1757c478bd9Sstevel@tonic-gate /* Error! But else what can we do? */ 1767c478bd9Sstevel@tonic-gate continue; 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate for (i = 0; i < j; ++i) { 1797c478bd9Sstevel@tonic-gate /* XXX: no error recovery provision. */ 1807c478bd9Sstevel@tonic-gate (void) ioctl(SHIN, TIOCSTI, mbc + i); 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate if (tty.c_lflag != tty_new.c_lflag) 1857c478bd9Sstevel@tonic-gate (void) ioctl(SHIN, TCSETS, (char *)&tty_new); 1867c478bd9Sstevel@tonic-gate (void) sigsetmask(omask); 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate /* 1907c478bd9Sstevel@tonic-gate * Concatenate src onto tail of des. 1917c478bd9Sstevel@tonic-gate * Des is a string whose maximum length is count. 1927c478bd9Sstevel@tonic-gate * Always null terminate. 1937c478bd9Sstevel@tonic-gate */ 194*6c02b4a4Smuffin void 195*6c02b4a4Smuffin catn(tchar *des, tchar *src, int count) 1967c478bd9Sstevel@tonic-gate { 1977c478bd9Sstevel@tonic-gate #ifdef TRACE 1987c478bd9Sstevel@tonic-gate tprintf("TRACE- catn()\n"); 1997c478bd9Sstevel@tonic-gate #endif 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate while (--count >= 0 && *des) 2027c478bd9Sstevel@tonic-gate des++; 2037c478bd9Sstevel@tonic-gate while (--count >= 0) 2047c478bd9Sstevel@tonic-gate if ((*des++ = *src++) == '\0') 2057c478bd9Sstevel@tonic-gate return; 2067c478bd9Sstevel@tonic-gate *des = '\0'; 2077c478bd9Sstevel@tonic-gate } 2087c478bd9Sstevel@tonic-gate 209*6c02b4a4Smuffin static int 2107c478bd9Sstevel@tonic-gate max(a, b) 2117c478bd9Sstevel@tonic-gate { 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate return (a > b ? a : b); 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate /* 2177c478bd9Sstevel@tonic-gate * Like strncpy but always leave room for trailing \0 2187c478bd9Sstevel@tonic-gate * and always null terminate. 2197c478bd9Sstevel@tonic-gate */ 220*6c02b4a4Smuffin void 221*6c02b4a4Smuffin copyn(tchar *des, tchar *src, int count) 2227c478bd9Sstevel@tonic-gate { 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate #ifdef TRACE 2257c478bd9Sstevel@tonic-gate tprintf("TRACE- copyn()\n"); 2267c478bd9Sstevel@tonic-gate #endif 2277c478bd9Sstevel@tonic-gate while (--count >= 0) 2287c478bd9Sstevel@tonic-gate if ((*des++ = *src++) == '\0') 2297c478bd9Sstevel@tonic-gate return; 2307c478bd9Sstevel@tonic-gate *des = '\0'; 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate /* 2347c478bd9Sstevel@tonic-gate * For qsort() 2357c478bd9Sstevel@tonic-gate */ 236*6c02b4a4Smuffin static int 237*6c02b4a4Smuffin fcompare(tchar **file1, tchar **file2) 2387c478bd9Sstevel@tonic-gate { 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate #ifdef TRACE 2417c478bd9Sstevel@tonic-gate tprintf("TRACE- fcompare()\n"); 2427c478bd9Sstevel@tonic-gate #endif 2437c478bd9Sstevel@tonic-gate return (strcoll_(*file1, *file2)); 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate static char 247*6c02b4a4Smuffin filetype(tchar *dir, tchar *file, int nosym) 2487c478bd9Sstevel@tonic-gate { 2497c478bd9Sstevel@tonic-gate tchar path[MAXPATHLEN + 1]; 2507c478bd9Sstevel@tonic-gate struct stat statb; 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate #ifdef TRACE 2537c478bd9Sstevel@tonic-gate tprintf("TRACE- filetype()\n"); 2547c478bd9Sstevel@tonic-gate #endif 2557c478bd9Sstevel@tonic-gate if (dir) { 2567c478bd9Sstevel@tonic-gate catn(strcpy_(path, dir), file, MAXPATHLEN); 2577c478bd9Sstevel@tonic-gate if (nosym) { 2587c478bd9Sstevel@tonic-gate if (stat_(path, &statb) < 0) 2597c478bd9Sstevel@tonic-gate return (' '); 2607c478bd9Sstevel@tonic-gate } else { 2617c478bd9Sstevel@tonic-gate if (lstat_(path, &statb) < 0) 2627c478bd9Sstevel@tonic-gate return (' '); 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate if ((statb.st_mode & S_IFMT) == S_IFLNK) 2657c478bd9Sstevel@tonic-gate return ('@'); 2667c478bd9Sstevel@tonic-gate if ((statb.st_mode & S_IFMT) == S_IFDIR) 2677c478bd9Sstevel@tonic-gate return ('/'); 2687c478bd9Sstevel@tonic-gate if (((statb.st_mode & S_IFMT) == S_IFREG) && 2697c478bd9Sstevel@tonic-gate (statb.st_mode & 011)) 2707c478bd9Sstevel@tonic-gate return ('*'); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate return (' '); 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate /* 2767c478bd9Sstevel@tonic-gate * Print sorted down columns 2777c478bd9Sstevel@tonic-gate */ 278*6c02b4a4Smuffin static void 279*6c02b4a4Smuffin print_by_column(tchar *dir, tchar *items[], int count, int looking_for_command) 2807c478bd9Sstevel@tonic-gate { 281*6c02b4a4Smuffin int i, rows, r, c, maxwidth = 0, columns; 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate #ifdef TRACE 2847c478bd9Sstevel@tonic-gate tprintf("TRACE- print_by_column()\n"); 2857c478bd9Sstevel@tonic-gate #endif 2867c478bd9Sstevel@tonic-gate for (i = 0; i < count; i++) 2877c478bd9Sstevel@tonic-gate maxwidth = max(maxwidth, tswidth(items[i])); 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate /* for the file tag and space */ 2907c478bd9Sstevel@tonic-gate maxwidth += looking_for_command ? 1 : 2; 2917c478bd9Sstevel@tonic-gate columns = max(78 / maxwidth, 1); 2927c478bd9Sstevel@tonic-gate rows = (count + (columns - 1)) / columns; 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate for (r = 0; r < rows; r++) { 2957c478bd9Sstevel@tonic-gate for (c = 0; c < columns; c++) { 2967c478bd9Sstevel@tonic-gate i = c * rows + r; 2977c478bd9Sstevel@tonic-gate if (i < count) { 298*6c02b4a4Smuffin int w; 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate /* 3017c478bd9Sstevel@tonic-gate * Print filename followed by 3027c478bd9Sstevel@tonic-gate * '@' or '/' or '*' or ' ' 3037c478bd9Sstevel@tonic-gate */ 3047c478bd9Sstevel@tonic-gate printf("%t", items[i]); 3057c478bd9Sstevel@tonic-gate w = tswidth(items[i]); 3067c478bd9Sstevel@tonic-gate if (!looking_for_command) { 3077c478bd9Sstevel@tonic-gate printf("%c", 3087c478bd9Sstevel@tonic-gate (tchar) filetype(dir, items[i], 0)); 3097c478bd9Sstevel@tonic-gate w++; 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate if (c < columns - 1) /* last column? */ 3127c478bd9Sstevel@tonic-gate for (; w < maxwidth; w++) 3137c478bd9Sstevel@tonic-gate printf(" "); 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate printf("\n"); 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate /* 3217c478bd9Sstevel@tonic-gate * Expand file name with possible tilde usage 3227c478bd9Sstevel@tonic-gate * ~person/mumble 3237c478bd9Sstevel@tonic-gate * expands to 3247c478bd9Sstevel@tonic-gate * home_directory_of_person/mumble 3257c478bd9Sstevel@tonic-gate */ 3267c478bd9Sstevel@tonic-gate tchar * 327*6c02b4a4Smuffin tilde(tchar *new, tchar *old) 3287c478bd9Sstevel@tonic-gate { 329*6c02b4a4Smuffin tchar *o, *p; 330*6c02b4a4Smuffin struct passwd *pw; 3317c478bd9Sstevel@tonic-gate static tchar person[40]; 3327c478bd9Sstevel@tonic-gate char person_[40]; /* work */ 3337c478bd9Sstevel@tonic-gate tchar *pw_dir; /* work */ 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate #ifdef TRACE 3367c478bd9Sstevel@tonic-gate tprintf("TRACE- tilde()\n"); 3377c478bd9Sstevel@tonic-gate #endif 3387c478bd9Sstevel@tonic-gate if (old[0] != '~') 3397c478bd9Sstevel@tonic-gate return (strcpy_(new, old)); 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate for (p = person, o = &old[1]; *o && *o != '/'; *p++ = *o++) 3427c478bd9Sstevel@tonic-gate ; 3437c478bd9Sstevel@tonic-gate *p = '\0'; 3447c478bd9Sstevel@tonic-gate if (person[0] == '\0') 3457c478bd9Sstevel@tonic-gate (void) strcpy_(new, value(S_home /*"home"*/)); 3467c478bd9Sstevel@tonic-gate else { 3477c478bd9Sstevel@tonic-gate pw = getpwnam(tstostr(person_,person)); 3487c478bd9Sstevel@tonic-gate if (pw == NULL) 3497c478bd9Sstevel@tonic-gate return (NULL); 3507c478bd9Sstevel@tonic-gate pw_dir = strtots((tchar *)NULL, pw->pw_dir); /* allocate */ 3517c478bd9Sstevel@tonic-gate (void) strcpy_(new, pw_dir); 3527c478bd9Sstevel@tonic-gate xfree(pw_dir); /* free it */ 3537c478bd9Sstevel@tonic-gate } 3547c478bd9Sstevel@tonic-gate (void) strcat_(new, o); 3557c478bd9Sstevel@tonic-gate return (new); 3567c478bd9Sstevel@tonic-gate } 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate /* 3597c478bd9Sstevel@tonic-gate * Cause pending line to be printed 3607c478bd9Sstevel@tonic-gate */ 361*6c02b4a4Smuffin static void 362*6c02b4a4Smuffin sim_retype(void) 3637c478bd9Sstevel@tonic-gate { 3647c478bd9Sstevel@tonic-gate #ifdef notdef 3657c478bd9Sstevel@tonic-gate struct termios tty_pending; 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate #ifdef TRACE 3687c478bd9Sstevel@tonic-gate tprintf("TRACE- sim_retypr()\n"); 3697c478bd9Sstevel@tonic-gate #endif 3707c478bd9Sstevel@tonic-gate tty_pending = tty_new; 3717c478bd9Sstevel@tonic-gate tty_pending.c_lflag |= PENDIN; 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate (void) ioctl(SHIN, TCSETS, (char *)&tty_pending); 3747c478bd9Sstevel@tonic-gate #else 3757c478bd9Sstevel@tonic-gate #ifdef TRACE 3767c478bd9Sstevel@tonic-gate tprintf("TRACE- sim_retype()\n"); 3777c478bd9Sstevel@tonic-gate #endif 3787c478bd9Sstevel@tonic-gate (void) write(SHOUT, CTRLR, strlen(CTRLR)); 3797c478bd9Sstevel@tonic-gate printprompt(); 3807c478bd9Sstevel@tonic-gate #endif 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate 383*6c02b4a4Smuffin static int 384*6c02b4a4Smuffin beep_outc(int c) 385*6c02b4a4Smuffin { 3867c478bd9Sstevel@tonic-gate char buf[1]; 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate buf[0] = c; 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate (void) write (SHOUT, buf, 1); 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate return 0; 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate 395*6c02b4a4Smuffin static void 396*6c02b4a4Smuffin beep(void) 3977c478bd9Sstevel@tonic-gate { 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate #ifdef TRACE 4007c478bd9Sstevel@tonic-gate tprintf("TRACE- beep()\n"); 4017c478bd9Sstevel@tonic-gate #endif 4027c478bd9Sstevel@tonic-gate if (adrof(S_nobeep /*"nobeep" */) == 0) 4037c478bd9Sstevel@tonic-gate (void) tputs (BELL, 0, beep_outc); 4047c478bd9Sstevel@tonic-gate } 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate /* 4077c478bd9Sstevel@tonic-gate * Erase that silly ^[ and print the recognized part of the string. 4087c478bd9Sstevel@tonic-gate */ 409*6c02b4a4Smuffin static void 410*6c02b4a4Smuffin print_recognized_stuff(tchar *recognized_part) 4117c478bd9Sstevel@tonic-gate { 4127c478bd9Sstevel@tonic-gate int unit = didfds ? 1 : SHOUT; 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate #ifdef TRACE 4157c478bd9Sstevel@tonic-gate tprintf("TRACE- print_recognized_stuff()\n"); 4167c478bd9Sstevel@tonic-gate #endif 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate /* 4197c478bd9Sstevel@tonic-gate * An optimized erasing of that silly ^[ 4207c478bd9Sstevel@tonic-gate * 4217c478bd9Sstevel@tonic-gate * One would think that line speeds have become fast enough that this 4227c478bd9Sstevel@tonic-gate * isn't necessary, but it turns out that the visual difference is 4237c478bd9Sstevel@tonic-gate * quite noticeable. 4247c478bd9Sstevel@tonic-gate */ 4257c478bd9Sstevel@tonic-gate flush(); 4267c478bd9Sstevel@tonic-gate switch (tswidth(recognized_part)) { 4277c478bd9Sstevel@tonic-gate case 0: 4287c478bd9Sstevel@tonic-gate /* erase two characters: ^[ */ 4297c478bd9Sstevel@tonic-gate write(unit, "\b\b \b\b", sizeof "\b\b \b\b" - 1); 4307c478bd9Sstevel@tonic-gate break; 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate case 1: 4337c478bd9Sstevel@tonic-gate /* overstrike the ^, erase the [ */ 4347c478bd9Sstevel@tonic-gate write(unit, "\b\b", 2); 4357c478bd9Sstevel@tonic-gate printf("%t", recognized_part); 4367c478bd9Sstevel@tonic-gate write(unit, " \b\b", 4); 4377c478bd9Sstevel@tonic-gate break; 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate default: 4407c478bd9Sstevel@tonic-gate /* overstrike both characters ^[ */ 4417c478bd9Sstevel@tonic-gate write(unit, "\b\b", 2); 4427c478bd9Sstevel@tonic-gate printf("%t", recognized_part); 4437c478bd9Sstevel@tonic-gate break; 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate flush(); 4467c478bd9Sstevel@tonic-gate } 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate /* 4497c478bd9Sstevel@tonic-gate * Parse full path in file into 2 parts: directory and file names 4507c478bd9Sstevel@tonic-gate * Should leave final slash (/) at end of dir. 4517c478bd9Sstevel@tonic-gate */ 452*6c02b4a4Smuffin static void 453*6c02b4a4Smuffin extract_dir_and_name(tchar *path, tchar *dir, tchar *name) 4547c478bd9Sstevel@tonic-gate { 455*6c02b4a4Smuffin tchar *p; 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate #ifdef TRACE 4587c478bd9Sstevel@tonic-gate tprintf("TRACE- extract_dir_and_name()\n"); 4597c478bd9Sstevel@tonic-gate #endif 4607c478bd9Sstevel@tonic-gate p = rindex_(path, '/'); 4617c478bd9Sstevel@tonic-gate if (p == NOSTR) { 4627c478bd9Sstevel@tonic-gate copyn(name, path, MAXNAMLEN); 4637c478bd9Sstevel@tonic-gate dir[0] = '\0'; 4647c478bd9Sstevel@tonic-gate } else { 4657c478bd9Sstevel@tonic-gate copyn(name, ++p, MAXNAMLEN); 4667c478bd9Sstevel@tonic-gate copyn(dir, path, p - path); 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate tchar * 471*6c02b4a4Smuffin getentry(DIR *dir_fd, int looking_for_lognames) 4727c478bd9Sstevel@tonic-gate { 473*6c02b4a4Smuffin struct passwd *pw; 474*6c02b4a4Smuffin struct dirent *dirp; 4757c478bd9Sstevel@tonic-gate /* 4767c478bd9Sstevel@tonic-gate * For char * -> tchar * Conversion 4777c478bd9Sstevel@tonic-gate */ 4787c478bd9Sstevel@tonic-gate static tchar strbuf[MAXNAMLEN+1]; 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate #ifdef TRACE 4817c478bd9Sstevel@tonic-gate tprintf("TRACE- getentry()\n"); 4827c478bd9Sstevel@tonic-gate #endif 4837c478bd9Sstevel@tonic-gate if (looking_for_lognames) { 4847c478bd9Sstevel@tonic-gate if ((pw = getpwent ()) == NULL) 4857c478bd9Sstevel@tonic-gate return (NULL); 4867c478bd9Sstevel@tonic-gate return (strtots(strbuf,pw->pw_name)); 4877c478bd9Sstevel@tonic-gate } 4887c478bd9Sstevel@tonic-gate if (dirp = readdir(dir_fd)) 4897c478bd9Sstevel@tonic-gate return (strtots(strbuf,dirp->d_name)); 4907c478bd9Sstevel@tonic-gate return (NULL); 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate 493*6c02b4a4Smuffin static void 494*6c02b4a4Smuffin free_items(tchar **items) 4957c478bd9Sstevel@tonic-gate { 496*6c02b4a4Smuffin int i; 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate #ifdef TRACE 4997c478bd9Sstevel@tonic-gate tprintf("TRACE- free_items()\n"); 5007c478bd9Sstevel@tonic-gate #endif 5017c478bd9Sstevel@tonic-gate for (i = 0; items[i]; i++) 5027c478bd9Sstevel@tonic-gate free(items[i]); 5037c478bd9Sstevel@tonic-gate free( (char *)items); 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate #define FREE_ITEMS(items) { \ 5077c478bd9Sstevel@tonic-gate int omask;\ 5087c478bd9Sstevel@tonic-gate \ 5097c478bd9Sstevel@tonic-gate omask = sigblock(sigmask(SIGINT));\ 5107c478bd9Sstevel@tonic-gate free_items(items);\ 5117c478bd9Sstevel@tonic-gate items = NULL;\ 5127c478bd9Sstevel@tonic-gate (void) sigsetmask(omask);\ 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate /* 5167c478bd9Sstevel@tonic-gate * Perform a RECOGNIZE or LIST command on string "word". 5177c478bd9Sstevel@tonic-gate */ 518*6c02b4a4Smuffin static int 519*6c02b4a4Smuffin search2(tchar *word, COMMAND command, int max_word_length) 5207c478bd9Sstevel@tonic-gate { 5217c478bd9Sstevel@tonic-gate static tchar **items = NULL; 522*6c02b4a4Smuffin DIR *dir_fd; 523*6c02b4a4Smuffin int numitems = 0, ignoring = TRUE, nignored = 0; 524*6c02b4a4Smuffin int name_length, looking_for_lognames; 5257c478bd9Sstevel@tonic-gate tchar tilded_dir[MAXPATHLEN + 1], dir[MAXPATHLEN + 1]; 5267c478bd9Sstevel@tonic-gate tchar name[MAXNAMLEN + 1], extended_name[MAXNAMLEN+1]; 5277c478bd9Sstevel@tonic-gate tchar *entry; 5287c478bd9Sstevel@tonic-gate #define MAXITEMS 1024 5297c478bd9Sstevel@tonic-gate #ifdef TRACE 5307c478bd9Sstevel@tonic-gate tprintf("TRACE- search2()\n"); 5317c478bd9Sstevel@tonic-gate #endif 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate if (items != NULL) 5347c478bd9Sstevel@tonic-gate FREE_ITEMS(items); 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate looking_for_lognames = (*word == '~') && (index_(word, '/') == NULL); 5377c478bd9Sstevel@tonic-gate if (looking_for_lognames) { 5387c478bd9Sstevel@tonic-gate (void) setpwent(); 5397c478bd9Sstevel@tonic-gate copyn(name, &word[1], MAXNAMLEN); /* name sans ~ */ 5407c478bd9Sstevel@tonic-gate } else { 5417c478bd9Sstevel@tonic-gate extract_dir_and_name(word, dir, name); 5427c478bd9Sstevel@tonic-gate if (tilde(tilded_dir, dir) == 0) 5437c478bd9Sstevel@tonic-gate return (0); 5447c478bd9Sstevel@tonic-gate dir_fd = opendir_(*tilded_dir ? tilded_dir : S_DOT /*"."*/); 5457c478bd9Sstevel@tonic-gate if (dir_fd == NULL) 5467c478bd9Sstevel@tonic-gate return (0); 5477c478bd9Sstevel@tonic-gate } 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate again: /* search for matches */ 5507c478bd9Sstevel@tonic-gate name_length = strlen_(name); 5517c478bd9Sstevel@tonic-gate for (numitems = 0; entry = getentry(dir_fd, looking_for_lognames); ) { 5527c478bd9Sstevel@tonic-gate if (!is_prefix(name, entry)) 5537c478bd9Sstevel@tonic-gate continue; 5547c478bd9Sstevel@tonic-gate /* Don't match . files on null prefix match */ 5557c478bd9Sstevel@tonic-gate if (name_length == 0 && entry[0] == '.' && 5567c478bd9Sstevel@tonic-gate !looking_for_lognames) 5577c478bd9Sstevel@tonic-gate continue; 5587c478bd9Sstevel@tonic-gate if (command == LIST) { 5597c478bd9Sstevel@tonic-gate if (numitems >= MAXITEMS) { 5607c478bd9Sstevel@tonic-gate printf ("\nYikes!! Too many %s!!\n", 5617c478bd9Sstevel@tonic-gate looking_for_lognames ? 5627c478bd9Sstevel@tonic-gate "names in password file":"files"); 5637c478bd9Sstevel@tonic-gate break; 5647c478bd9Sstevel@tonic-gate } 5657c478bd9Sstevel@tonic-gate if (items == NULL) 5667c478bd9Sstevel@tonic-gate items = (tchar **) calloc(sizeof (items[1]), 5677c478bd9Sstevel@tonic-gate MAXITEMS+1); 5687c478bd9Sstevel@tonic-gate items[numitems] = (tchar *)xalloc((unsigned)(strlen_(entry) + 1)*sizeof(tchar)); 5697c478bd9Sstevel@tonic-gate copyn(items[numitems], entry, MAXNAMLEN); 5707c478bd9Sstevel@tonic-gate numitems++; 5717c478bd9Sstevel@tonic-gate } else { /* RECOGNIZE command */ 5727c478bd9Sstevel@tonic-gate if (ignoring && ignored(entry)) 5737c478bd9Sstevel@tonic-gate nignored++; 5747c478bd9Sstevel@tonic-gate else if (recognize(extended_name, 5757c478bd9Sstevel@tonic-gate entry, name_length, ++numitems)) 5767c478bd9Sstevel@tonic-gate break; 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate } 5797c478bd9Sstevel@tonic-gate if (ignoring && numitems == 0 && nignored > 0) { 5807c478bd9Sstevel@tonic-gate ignoring = FALSE; 5817c478bd9Sstevel@tonic-gate nignored = 0; 5827c478bd9Sstevel@tonic-gate if (looking_for_lognames) 5837c478bd9Sstevel@tonic-gate (void)setpwent(); 5847c478bd9Sstevel@tonic-gate else 5857c478bd9Sstevel@tonic-gate rewinddir(dir_fd); 5867c478bd9Sstevel@tonic-gate goto again; 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate 5897c478bd9Sstevel@tonic-gate if (looking_for_lognames) 5907c478bd9Sstevel@tonic-gate (void) endpwent(); 5917c478bd9Sstevel@tonic-gate else { 5927c478bd9Sstevel@tonic-gate unsetfd(dir_fd->dd_fd); 5937c478bd9Sstevel@tonic-gate closedir_(dir_fd); 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate if (command == RECOGNIZE && numitems > 0) { 5967c478bd9Sstevel@tonic-gate if (looking_for_lognames) 5977c478bd9Sstevel@tonic-gate copyn(word, S_TIL /*"~" */, 1); 5987c478bd9Sstevel@tonic-gate else 5997c478bd9Sstevel@tonic-gate /* put back dir part */ 6007c478bd9Sstevel@tonic-gate copyn(word, dir, max_word_length); 6017c478bd9Sstevel@tonic-gate /* add extended name */ 6027c478bd9Sstevel@tonic-gate catn(word, extended_name, max_word_length); 6037c478bd9Sstevel@tonic-gate return (numitems); 6047c478bd9Sstevel@tonic-gate } 6057c478bd9Sstevel@tonic-gate if (command == LIST) { 6067c478bd9Sstevel@tonic-gate qsort( (char *)items, numitems, sizeof(items[1]), 6077c478bd9Sstevel@tonic-gate (int (*)(const void *, const void *))fcompare); 6087c478bd9Sstevel@tonic-gate /* 6097c478bd9Sstevel@tonic-gate * Never looking for commands in this version, so final 6107c478bd9Sstevel@tonic-gate * argument forced to 0. If command name completion is 6117c478bd9Sstevel@tonic-gate * reinstated, this must change. 6127c478bd9Sstevel@tonic-gate */ 6137c478bd9Sstevel@tonic-gate print_by_column(looking_for_lognames ? NULL : tilded_dir, 6147c478bd9Sstevel@tonic-gate items, numitems, 0); 6157c478bd9Sstevel@tonic-gate if (items != NULL) 6167c478bd9Sstevel@tonic-gate FREE_ITEMS(items); 6177c478bd9Sstevel@tonic-gate } 6187c478bd9Sstevel@tonic-gate return (0); 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate /* 6227c478bd9Sstevel@tonic-gate * Object: extend what user typed up to an ambiguity. 6237c478bd9Sstevel@tonic-gate * Algorithm: 6247c478bd9Sstevel@tonic-gate * On first match, copy full entry (assume it'll be the only match) 6257c478bd9Sstevel@tonic-gate * On subsequent matches, shorten extended_name to the first 6267c478bd9Sstevel@tonic-gate * character mismatch between extended_name and entry. 6277c478bd9Sstevel@tonic-gate * If we shorten it back to the prefix length, stop searching. 6287c478bd9Sstevel@tonic-gate */ 629*6c02b4a4Smuffin int 630*6c02b4a4Smuffin recognize(tchar *extended_name, tchar *entry, int name_length, int numitems) 6317c478bd9Sstevel@tonic-gate { 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate #ifdef TRACE 6347c478bd9Sstevel@tonic-gate tprintf("TRACE- recognize()\n"); 6357c478bd9Sstevel@tonic-gate #endif 6367c478bd9Sstevel@tonic-gate if (numitems == 1) /* 1st match */ 6377c478bd9Sstevel@tonic-gate copyn(extended_name, entry, MAXNAMLEN); 6387c478bd9Sstevel@tonic-gate else { /* 2nd and subsequent matches */ 639*6c02b4a4Smuffin tchar *x, *ent; 640*6c02b4a4Smuffin int len = 0; 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate x = extended_name; 6437c478bd9Sstevel@tonic-gate for (ent = entry; *x && *x == *ent++; x++, len++) 6447c478bd9Sstevel@tonic-gate ; 6457c478bd9Sstevel@tonic-gate *x = '\0'; /* Shorten at 1st char diff */ 6467c478bd9Sstevel@tonic-gate if (len == name_length) /* Ambiguous to prefix? */ 6477c478bd9Sstevel@tonic-gate return (-1); /* So stop now and save time */ 6487c478bd9Sstevel@tonic-gate } 6497c478bd9Sstevel@tonic-gate return (0); 6507c478bd9Sstevel@tonic-gate } 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate /* 6537c478bd9Sstevel@tonic-gate * Return true if check items initial chars in template 6547c478bd9Sstevel@tonic-gate * This differs from PWB imatch in that if check is null 6557c478bd9Sstevel@tonic-gate * it items anything 6567c478bd9Sstevel@tonic-gate */ 657*6c02b4a4Smuffin static int 658*6c02b4a4Smuffin is_prefix(tchar *check, tchar *template) 6597c478bd9Sstevel@tonic-gate { 6607c478bd9Sstevel@tonic-gate #ifdef TRACE 6617c478bd9Sstevel@tonic-gate tprintf("TRACE- is_prefix()\n"); 6627c478bd9Sstevel@tonic-gate #endif 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate do 6657c478bd9Sstevel@tonic-gate if (*check == 0) 6667c478bd9Sstevel@tonic-gate return (TRUE); 6677c478bd9Sstevel@tonic-gate while (*check++ == *template++); 6687c478bd9Sstevel@tonic-gate return (FALSE); 6697c478bd9Sstevel@tonic-gate } 6707c478bd9Sstevel@tonic-gate 6717c478bd9Sstevel@tonic-gate /* 6727c478bd9Sstevel@tonic-gate * Return true if the chars in template appear at the 6737c478bd9Sstevel@tonic-gate * end of check, i.e., are its suffix. 6747c478bd9Sstevel@tonic-gate */ 675*6c02b4a4Smuffin static int 676*6c02b4a4Smuffin is_suffix(tchar *check, tchar *template) 6777c478bd9Sstevel@tonic-gate { 678*6c02b4a4Smuffin tchar *c, *t; 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate #ifdef TRACE 6817c478bd9Sstevel@tonic-gate tprintf("TRACE- is_suffix()\n"); 6827c478bd9Sstevel@tonic-gate #endif 6837c478bd9Sstevel@tonic-gate for (c = check; *c++;) 6847c478bd9Sstevel@tonic-gate ; 6857c478bd9Sstevel@tonic-gate for (t = template; *t++;) 6867c478bd9Sstevel@tonic-gate ; 6877c478bd9Sstevel@tonic-gate for (;;) { 6887c478bd9Sstevel@tonic-gate if (t == template) 6897c478bd9Sstevel@tonic-gate return (TRUE); 6907c478bd9Sstevel@tonic-gate if (c == check || *--t != *--c) 6917c478bd9Sstevel@tonic-gate return (FALSE); 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate 695*6c02b4a4Smuffin int 696*6c02b4a4Smuffin tenex(tchar *inputline, int inputline_size) 6977c478bd9Sstevel@tonic-gate { 698*6c02b4a4Smuffin int numitems, num_read, should_retype; 6997c478bd9Sstevel@tonic-gate int i; 7007c478bd9Sstevel@tonic-gate 7017c478bd9Sstevel@tonic-gate #ifdef TRACE 7027c478bd9Sstevel@tonic-gate tprintf("TRACE- tenex()\n"); 7037c478bd9Sstevel@tonic-gate #endif 7047c478bd9Sstevel@tonic-gate setup_tty(ON); 7057c478bd9Sstevel@tonic-gate termchars(); 7067c478bd9Sstevel@tonic-gate num_read = 0; 7077c478bd9Sstevel@tonic-gate should_retype = FALSE; 7087c478bd9Sstevel@tonic-gate while ((i = read_(SHIN, inputline+num_read, inputline_size-num_read)) 7097c478bd9Sstevel@tonic-gate > 0) { 7107c478bd9Sstevel@tonic-gate static tchar *delims = S_DELIM /*" '\"\t;&<>()|`"*/; 711*6c02b4a4Smuffin tchar *str_end, *word_start, last_char; 712*6c02b4a4Smuffin int space_left; 7137c478bd9Sstevel@tonic-gate struct termios tty; 7147c478bd9Sstevel@tonic-gate COMMAND command; 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate num_read += i; 7177c478bd9Sstevel@tonic-gate inputline[num_read] = '\0'; 7187c478bd9Sstevel@tonic-gate last_char = inputline[num_read - 1] & TRIM; 7197c478bd9Sstevel@tonic-gate 7207c478bd9Sstevel@tonic-gate if ((num_read == inputline_size) || (last_char == '\n')) 7217c478bd9Sstevel@tonic-gate break; 7227c478bd9Sstevel@tonic-gate 7237c478bd9Sstevel@tonic-gate str_end = &inputline[num_read]; 7247c478bd9Sstevel@tonic-gate if (last_char == ESC) { 7257c478bd9Sstevel@tonic-gate command = RECOGNIZE; 7267c478bd9Sstevel@tonic-gate *--str_end = '\0'; /* wipe out trailing ESC */ 7277c478bd9Sstevel@tonic-gate } else 7287c478bd9Sstevel@tonic-gate command = LIST; 7297c478bd9Sstevel@tonic-gate 7307c478bd9Sstevel@tonic-gate tty = tty_new; 7317c478bd9Sstevel@tonic-gate tty.c_lflag &= ~ECHO; 7327c478bd9Sstevel@tonic-gate (void) ioctl(SHIN, TCSETSF, (char *)&tty); 7337c478bd9Sstevel@tonic-gate 7347c478bd9Sstevel@tonic-gate if (command == LIST) 7357c478bd9Sstevel@tonic-gate printf("\n"); 7367c478bd9Sstevel@tonic-gate /* 7377c478bd9Sstevel@tonic-gate * Find LAST occurence of a delimiter in the inputline. 7387c478bd9Sstevel@tonic-gate * The word start is one character past it. 7397c478bd9Sstevel@tonic-gate */ 7407c478bd9Sstevel@tonic-gate for (word_start = str_end; word_start > inputline; 7417c478bd9Sstevel@tonic-gate --word_start) { 7427c478bd9Sstevel@tonic-gate if (index_(delims, word_start[-1]) || 7437c478bd9Sstevel@tonic-gate isauxsp(word_start[-1])) 7447c478bd9Sstevel@tonic-gate break; 7457c478bd9Sstevel@tonic-gate } 7467c478bd9Sstevel@tonic-gate space_left = inputline_size - (word_start - inputline) - 1; 7477c478bd9Sstevel@tonic-gate numitems = search2(word_start, command, space_left); 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate /* 7507c478bd9Sstevel@tonic-gate * Tabs in the input line cause trouble after a pushback. 7517c478bd9Sstevel@tonic-gate * tty driver won't backspace over them because column 7527c478bd9Sstevel@tonic-gate * positions are now incorrect. This is solved by retyping 7537c478bd9Sstevel@tonic-gate * over current line. 7547c478bd9Sstevel@tonic-gate */ 7557c478bd9Sstevel@tonic-gate if (index_(inputline, '\t')) { /* tab tchar in input line? */ 7567c478bd9Sstevel@tonic-gate back_to_col_1(); 7577c478bd9Sstevel@tonic-gate should_retype = TRUE; 7587c478bd9Sstevel@tonic-gate } 7597c478bd9Sstevel@tonic-gate if (command == LIST) /* Always retype after a LIST */ 7607c478bd9Sstevel@tonic-gate should_retype = TRUE; 7617c478bd9Sstevel@tonic-gate if (should_retype) 7627c478bd9Sstevel@tonic-gate printprompt(); 7637c478bd9Sstevel@tonic-gate pushback(inputline, should_retype); 7647c478bd9Sstevel@tonic-gate num_read = 0; /* chars will be reread */ 7657c478bd9Sstevel@tonic-gate should_retype = FALSE; 7667c478bd9Sstevel@tonic-gate 7677c478bd9Sstevel@tonic-gate /* 7687c478bd9Sstevel@tonic-gate * Avoid a race condition by echoing what we're recognized 7697c478bd9Sstevel@tonic-gate * _after_ pushing back the command line. This way, if the 7707c478bd9Sstevel@tonic-gate * user waits until seeing this output before typing more 7717c478bd9Sstevel@tonic-gate * stuff, the resulting keystrokes won't race with the STIed 7727c478bd9Sstevel@tonic-gate * input we've pushed back. (Of course, if the user types 7737c478bd9Sstevel@tonic-gate * ahead, the race still exists and it's quite possible that 7747c478bd9Sstevel@tonic-gate * the pushed back input line will interleave with the 7757c478bd9Sstevel@tonic-gate * keystrokes in unexpected ways.) 7767c478bd9Sstevel@tonic-gate */ 7777c478bd9Sstevel@tonic-gate if (command == RECOGNIZE) { 7787c478bd9Sstevel@tonic-gate /* print from str_end on */ 7797c478bd9Sstevel@tonic-gate print_recognized_stuff(str_end); 7807c478bd9Sstevel@tonic-gate if (numitems != 1) /* Beep = No match/ambiguous */ 7817c478bd9Sstevel@tonic-gate beep(); 7827c478bd9Sstevel@tonic-gate } 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate setup_tty(OFF); 7857c478bd9Sstevel@tonic-gate return (num_read); 7867c478bd9Sstevel@tonic-gate } 7877c478bd9Sstevel@tonic-gate 788*6c02b4a4Smuffin static int 789*6c02b4a4Smuffin ignored(tchar *entry) 7907c478bd9Sstevel@tonic-gate { 7917c478bd9Sstevel@tonic-gate struct varent *vp; 792*6c02b4a4Smuffin tchar **cp; 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate #ifdef TRACE 7957c478bd9Sstevel@tonic-gate tprintf("TRACE- ignored()\n"); 7967c478bd9Sstevel@tonic-gate #endif 7977c478bd9Sstevel@tonic-gate if ((vp = adrof(S_fignore /*"fignore"*/)) == NULL || 7987c478bd9Sstevel@tonic-gate (cp = vp->vec) == NULL) 7997c478bd9Sstevel@tonic-gate return (FALSE); 8007c478bd9Sstevel@tonic-gate for (; *cp != NULL; cp++) 8017c478bd9Sstevel@tonic-gate if (is_suffix(entry, *cp)) 8027c478bd9Sstevel@tonic-gate return (TRUE); 8037c478bd9Sstevel@tonic-gate return (FALSE); 8047c478bd9Sstevel@tonic-gate } 805*6c02b4a4Smuffin #endif /* FILEC */ 806