1 /*- 2 * Copyright (c) 1980, 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 /*static char sccsid[] = "from: @(#)file.c 5.17 (Berkeley) 6/8/91";*/ 36 static char rcsid[] = "$Id: file.c,v 1.7 1994/04/28 15:57:41 pk Exp $"; 37 #endif /* not lint */ 38 39 #ifdef FILEC 40 41 #include <sys/param.h> 42 #include <sys/ioctl.h> 43 #include <sys/stat.h> 44 #include <termios.h> 45 #include <dirent.h> 46 #include <pwd.h> 47 #include <stdlib.h> 48 #include <unistd.h> 49 #if __STDC__ 50 # include <stdarg.h> 51 #else 52 # include <varargs.h> 53 #endif 54 55 #include "csh.h" 56 #include "extern.h" 57 58 /* 59 * Tenex style file name recognition, .. and more. 60 * History: 61 * Author: Ken Greer, Sept. 1975, CMU. 62 * Finally got around to adding to the Cshell., Ken Greer, Dec. 1981. 63 */ 64 65 #define ON 1 66 #define OFF 0 67 #ifndef TRUE 68 #define TRUE 1 69 #endif 70 #ifndef FALSE 71 #define FALSE 0 72 #endif 73 74 #define ESC '\033' 75 76 typedef enum { 77 LIST, RECOGNIZE 78 } COMMAND; 79 80 static void setup_tty __P((int)); 81 static void back_to_col_1 __P((void)); 82 static void pushback __P((Char *)); 83 static void catn __P((Char *, Char *, int)); 84 static void copyn __P((Char *, Char *, int)); 85 static Char filetype __P((Char *, Char *)); 86 static void print_by_column __P((Char *, Char *[], int)); 87 static Char *tilde __P((Char *, Char *)); 88 static void retype __P((void)); 89 static void beep __P((void)); 90 static void print_recognized_stuff __P((Char *)); 91 static void extract_dir_and_name __P((Char *, Char *, Char *)); 92 static Char *getentry __P((DIR *, int)); 93 static void free_items __P((Char **)); 94 static int tsearch __P((Char *, COMMAND, int)); 95 static int recognize __P((Char *, Char *, int, int)); 96 static int is_prefix __P((Char *, Char *)); 97 static int is_suffix __P((Char *, Char *)); 98 static int ignored __P((Char *)); 99 100 /* 101 * Put this here so the binary can be patched with adb to enable file 102 * completion by default. Filec controls completion, nobeep controls 103 * ringing the terminal bell on incomplete expansions. 104 */ 105 bool filec = 0; 106 107 static void 108 setup_tty(on) 109 int on; 110 { 111 struct termios tchars; 112 113 if (on) { 114 (void) tcgetattr(SHIN, &tchars); 115 tchars.c_cc[VEOL] = ESC; 116 if (tchars.c_lflag & ICANON) 117 on = TCSADRAIN; 118 else { 119 on = TCSAFLUSH; 120 tchars.c_lflag |= ICANON; 121 } 122 (void) tcsetattr(SHIN, on, &tchars); 123 } 124 else { 125 (void) tcgetattr(SHIN, &tchars); 126 tchars.c_cc[VEOL] = _POSIX_VDISABLE; 127 (void) tcsetattr(SHIN, TCSADRAIN, &tchars); 128 } 129 } 130 131 /* 132 * Move back to beginning of current line 133 */ 134 static void 135 back_to_col_1() 136 { 137 struct termios tty, tty_normal; 138 int omask; 139 140 omask = sigblock(sigmask(SIGINT)); 141 (void) tcgetattr(SHOUT, &tty); 142 tty_normal = tty; 143 tty.c_iflag &= ~INLCR; 144 tty.c_oflag &= ~ONLCR; 145 (void) tcsetattr(SHOUT, TCSANOW, &tty); 146 (void) write(SHOUT, "\r", 1); 147 (void) tcsetattr(SHOUT, TCSANOW, &tty_normal); 148 (void) sigsetmask(omask); 149 } 150 151 /* 152 * Push string contents back into tty queue 153 */ 154 static void 155 pushback(string) 156 Char *string; 157 { 158 register Char *p; 159 struct termios tty, tty_normal; 160 int omask; 161 char c; 162 163 omask = sigblock(sigmask(SIGINT)); 164 (void) tcgetattr(SHOUT, &tty); 165 tty_normal = tty; 166 tty.c_lflag &= ~(ECHOKE | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOCTL); 167 (void) tcsetattr(SHOUT, TCSANOW, &tty); 168 169 for (p = string; c = *p; p++) 170 (void) ioctl(SHOUT, TIOCSTI, (ioctl_t) & c); 171 (void) tcsetattr(SHOUT, TCSANOW, &tty_normal); 172 (void) sigsetmask(omask); 173 } 174 175 /* 176 * Concatenate src onto tail of des. 177 * Des is a string whose maximum length is count. 178 * Always null terminate. 179 */ 180 static void 181 catn(des, src, count) 182 register Char *des, *src; 183 register int count; 184 { 185 while (--count >= 0 && *des) 186 des++; 187 while (--count >= 0) 188 if ((*des++ = *src++) == 0) 189 return; 190 *des = '\0'; 191 } 192 193 /* 194 * Like strncpy but always leave room for trailing \0 195 * and always null terminate. 196 */ 197 static void 198 copyn(des, src, count) 199 register Char *des, *src; 200 register int count; 201 { 202 while (--count >= 0) 203 if ((*des++ = *src++) == 0) 204 return; 205 *des = '\0'; 206 } 207 208 static Char 209 filetype(dir, file) 210 Char *dir, *file; 211 { 212 Char path[MAXPATHLEN]; 213 struct stat statb; 214 215 catn(Strcpy(path, dir), file, sizeof(path) / sizeof(Char)); 216 if (lstat(short2str(path), &statb) == 0) { 217 switch (statb.st_mode & S_IFMT) { 218 case S_IFDIR: 219 return ('/'); 220 221 case S_IFLNK: 222 if (stat(short2str(path), &statb) == 0 && /* follow it out */ 223 S_ISDIR(statb.st_mode)) 224 return ('>'); 225 else 226 return ('@'); 227 228 case S_IFSOCK: 229 return ('='); 230 231 default: 232 if (statb.st_mode & 0111) 233 return ('*'); 234 } 235 } 236 return (' '); 237 } 238 239 static struct winsize win; 240 241 /* 242 * Print sorted down columns 243 */ 244 static void 245 print_by_column(dir, items, count) 246 Char *dir, *items[]; 247 int count; 248 { 249 register int i, rows, r, c, maxwidth = 0, columns; 250 251 if (ioctl(SHOUT, TIOCGWINSZ, (ioctl_t) & win) < 0 || win.ws_col == 0) 252 win.ws_col = 80; 253 for (i = 0; i < count; i++) 254 maxwidth = maxwidth > (r = Strlen(items[i])) ? maxwidth : r; 255 maxwidth += 2; /* for the file tag and space */ 256 columns = win.ws_col / maxwidth; 257 if (columns == 0) 258 columns = 1; 259 rows = (count + (columns - 1)) / columns; 260 for (r = 0; r < rows; r++) { 261 for (c = 0; c < columns; c++) { 262 i = c * rows + r; 263 if (i < count) { 264 register int w; 265 266 xprintf("%s", short2str(items[i])); 267 xputchar(dir ? filetype(dir, items[i]) : ' '); 268 if (c < columns - 1) { /* last column? */ 269 w = Strlen(items[i]) + 1; 270 for (; w < maxwidth; w++) 271 xputchar(' '); 272 } 273 } 274 } 275 xputchar('\r'); 276 xputchar('\n'); 277 } 278 } 279 280 /* 281 * Expand file name with possible tilde usage 282 * ~person/mumble 283 * expands to 284 * home_directory_of_person/mumble 285 */ 286 static Char * 287 tilde(new, old) 288 Char *new, *old; 289 { 290 register Char *o, *p; 291 register struct passwd *pw; 292 static Char person[40]; 293 294 if (old[0] != '~') 295 return (Strcpy(new, old)); 296 297 for (p = person, o = &old[1]; *o && *o != '/'; *p++ = *o++); 298 *p = '\0'; 299 if (person[0] == '\0') 300 (void) Strcpy(new, value(STRhome)); 301 else { 302 pw = getpwnam(short2str(person)); 303 if (pw == NULL) 304 return (NULL); 305 (void) Strcpy(new, str2short(pw->pw_dir)); 306 } 307 (void) Strcat(new, o); 308 return (new); 309 } 310 311 /* 312 * Cause pending line to be printed 313 */ 314 static void 315 retype() 316 { 317 struct termios tty; 318 319 (void) tcgetattr(SHOUT, &tty); 320 tty.c_lflag |= PENDIN; 321 (void) tcsetattr(SHOUT, TCSANOW, &tty); 322 } 323 324 static void 325 beep() 326 { 327 if (adrof(STRnobeep) == 0) 328 (void) write(SHOUT, "\007", 1); 329 } 330 331 /* 332 * Erase that silly ^[ and 333 * print the recognized part of the string 334 */ 335 static void 336 print_recognized_stuff(recognized_part) 337 Char *recognized_part; 338 { 339 /* An optimized erasing of that silly ^[ */ 340 putraw('\b'); 341 putraw('\b'); 342 switch (Strlen(recognized_part)) { 343 344 case 0: /* erase two Characters: ^[ */ 345 putraw(' '); 346 putraw(' '); 347 putraw('\b'); 348 putraw('\b'); 349 break; 350 351 case 1: /* overstrike the ^, erase the [ */ 352 xprintf("%s", short2str(recognized_part)); 353 putraw(' '); 354 putraw('\b'); 355 break; 356 357 default: /* overstrike both Characters ^[ */ 358 xprintf("%s", short2str(recognized_part)); 359 break; 360 } 361 flush(); 362 } 363 364 /* 365 * Parse full path in file into 2 parts: directory and file names 366 * Should leave final slash (/) at end of dir. 367 */ 368 static void 369 extract_dir_and_name(path, dir, name) 370 Char *path, *dir, *name; 371 { 372 register Char *p; 373 374 p = Strrchr(path, '/'); 375 if (p == NULL) { 376 copyn(name, path, MAXNAMLEN); 377 dir[0] = '\0'; 378 } 379 else { 380 copyn(name, ++p, MAXNAMLEN); 381 copyn(dir, path, p - path); 382 } 383 } 384 385 static Char * 386 getentry(dir_fd, looking_for_lognames) 387 DIR *dir_fd; 388 int looking_for_lognames; 389 { 390 register struct passwd *pw; 391 register struct dirent *dirp; 392 393 if (looking_for_lognames) { 394 if ((pw = getpwent()) == NULL) 395 return (NULL); 396 return (str2short(pw->pw_name)); 397 } 398 if (dirp = readdir(dir_fd)) 399 return (str2short(dirp->d_name)); 400 return (NULL); 401 } 402 403 static void 404 free_items(items) 405 register Char **items; 406 { 407 register int i; 408 409 for (i = 0; items[i]; i++) 410 xfree((ptr_t) items[i]); 411 xfree((ptr_t) items); 412 } 413 414 #define FREE_ITEMS(items) { \ 415 int omask;\ 416 \ 417 omask = sigblock(sigmask(SIGINT));\ 418 free_items(items);\ 419 items = NULL;\ 420 (void) sigsetmask(omask);\ 421 } 422 423 /* 424 * Perform a RECOGNIZE or LIST command on string "word". 425 */ 426 static int 427 tsearch(word, command, max_word_length) 428 Char *word; 429 COMMAND command; 430 int max_word_length; 431 { 432 static Char **items = NULL; 433 register DIR *dir_fd; 434 register numitems = 0, ignoring = TRUE, nignored = 0; 435 register name_length, looking_for_lognames; 436 Char tilded_dir[MAXPATHLEN + 1], dir[MAXPATHLEN + 1]; 437 Char name[MAXNAMLEN + 1], extended_name[MAXNAMLEN + 1]; 438 Char *entry; 439 440 #define MAXITEMS 1024 441 442 if (items != NULL) 443 FREE_ITEMS(items); 444 445 looking_for_lognames = (*word == '~') && (Strchr(word, '/') == NULL); 446 if (looking_for_lognames) { 447 (void) setpwent(); 448 copyn(name, &word[1], MAXNAMLEN); /* name sans ~ */ 449 dir_fd = NULL; 450 } 451 else { 452 extract_dir_and_name(word, dir, name); 453 if (tilde(tilded_dir, dir) == 0) 454 return (0); 455 dir_fd = opendir(*tilded_dir ? short2str(tilded_dir) : "."); 456 if (dir_fd == NULL) 457 return (0); 458 } 459 460 again: /* search for matches */ 461 name_length = Strlen(name); 462 for (numitems = 0; entry = getentry(dir_fd, looking_for_lognames);) { 463 if (!is_prefix(name, entry)) 464 continue; 465 /* Don't match . files on null prefix match */ 466 if (name_length == 0 && entry[0] == '.' && 467 !looking_for_lognames) 468 continue; 469 if (command == LIST) { 470 if (numitems >= MAXITEMS) { 471 xprintf("\nYikes!! Too many %s!!\n", 472 looking_for_lognames ? 473 "names in password file" : "files"); 474 break; 475 } 476 if (items == NULL) 477 items = (Char **) xcalloc(sizeof(items[0]), MAXITEMS); 478 items[numitems] = (Char *) xmalloc((size_t) (Strlen(entry) + 1) * 479 sizeof(Char)); 480 copyn(items[numitems], entry, MAXNAMLEN); 481 numitems++; 482 } 483 else { /* RECOGNIZE command */ 484 if (ignoring && ignored(entry)) 485 nignored++; 486 else if (recognize(extended_name, 487 entry, name_length, ++numitems)) 488 break; 489 } 490 } 491 if (ignoring && numitems == 0 && nignored > 0) { 492 ignoring = FALSE; 493 nignored = 0; 494 if (looking_for_lognames) 495 (void) setpwent(); 496 else 497 rewinddir(dir_fd); 498 goto again; 499 } 500 501 if (looking_for_lognames) 502 (void) endpwent(); 503 else 504 (void) closedir(dir_fd); 505 if (numitems == 0) 506 return (0); 507 if (command == RECOGNIZE) { 508 if (looking_for_lognames) 509 copyn(word, STRtilde, 1); 510 else 511 /* put back dir part */ 512 copyn(word, dir, max_word_length); 513 /* add extended name */ 514 catn(word, extended_name, max_word_length); 515 return (numitems); 516 } 517 else { /* LIST */ 518 qsort((ptr_t) items, numitems, sizeof(items[0]), 519 (int (*)(const void *, const void *)) sortscmp); 520 print_by_column(looking_for_lognames ? NULL : tilded_dir, 521 items, numitems); 522 if (items != NULL) 523 FREE_ITEMS(items); 524 } 525 return (0); 526 } 527 528 /* 529 * Object: extend what user typed up to an ambiguity. 530 * Algorithm: 531 * On first match, copy full entry (assume it'll be the only match) 532 * On subsequent matches, shorten extended_name to the first 533 * Character mismatch between extended_name and entry. 534 * If we shorten it back to the prefix length, stop searching. 535 */ 536 static int 537 recognize(extended_name, entry, name_length, numitems) 538 Char *extended_name, *entry; 539 int name_length, numitems; 540 { 541 if (numitems == 1) /* 1st match */ 542 copyn(extended_name, entry, MAXNAMLEN); 543 else { /* 2nd & subsequent matches */ 544 register Char *x, *ent; 545 register int len = 0; 546 547 x = extended_name; 548 for (ent = entry; *x && *x == *ent++; x++, len++); 549 *x = '\0'; /* Shorten at 1st Char diff */ 550 if (len == name_length) /* Ambiguous to prefix? */ 551 return (-1); /* So stop now and save time */ 552 } 553 return (0); 554 } 555 556 /* 557 * Return true if check matches initial Chars in template. 558 * This differs from PWB imatch in that if check is null 559 * it matches anything. 560 */ 561 static int 562 is_prefix(check, template) 563 register Char *check, *template; 564 { 565 do 566 if (*check == 0) 567 return (TRUE); 568 while (*check++ == *template++); 569 return (FALSE); 570 } 571 572 /* 573 * Return true if the Chars in template appear at the 574 * end of check, I.e., are it's suffix. 575 */ 576 static int 577 is_suffix(check, template) 578 Char *check, *template; 579 { 580 register Char *c, *t; 581 582 for (c = check; *c++;); 583 for (t = template; *t++;); 584 for (;;) { 585 if (t == template) 586 return 1; 587 if (c == check || *--t != *--c) 588 return 0; 589 } 590 } 591 592 int 593 tenex(inputline, inputline_size) 594 Char *inputline; 595 int inputline_size; 596 { 597 register int numitems, num_read; 598 char tinputline[BUFSIZ]; 599 600 601 setup_tty(ON); 602 603 while ((num_read = read(SHIN, tinputline, BUFSIZ)) > 0) { 604 int i; 605 static Char delims[] = {' ', '\'', '"', '\t', ';', '&', '<', 606 '>', '(', ')', '|', '^', '%', '\0'}; 607 register Char *str_end, *word_start, last_Char, should_retype; 608 register int space_left; 609 COMMAND command; 610 611 for (i = 0; i < num_read; i++) 612 inputline[i] = (unsigned char) tinputline[i]; 613 last_Char = inputline[num_read - 1] & ASCII; 614 615 if (last_Char == '\n' || num_read == inputline_size) 616 break; 617 command = (last_Char == ESC) ? RECOGNIZE : LIST; 618 if (command == LIST) 619 xputchar('\n'); 620 str_end = &inputline[num_read]; 621 if (last_Char == ESC) 622 --str_end; /* wipeout trailing cmd Char */ 623 *str_end = '\0'; 624 /* 625 * Find LAST occurence of a delimiter in the inputline. The word start 626 * is one Character past it. 627 */ 628 for (word_start = str_end; word_start > inputline; --word_start) 629 if (Strchr(delims, word_start[-1])) 630 break; 631 space_left = inputline_size - (word_start - inputline) - 1; 632 numitems = tsearch(word_start, command, space_left); 633 634 if (command == RECOGNIZE) { 635 /* print from str_end on */ 636 print_recognized_stuff(str_end); 637 if (numitems != 1) /* Beep = No match/ambiguous */ 638 beep(); 639 } 640 641 /* 642 * Tabs in the input line cause trouble after a pushback. tty driver 643 * won't backspace over them because column positions are now 644 * incorrect. This is solved by retyping over current line. 645 */ 646 should_retype = FALSE; 647 if (Strchr(inputline, '\t')) { /* tab Char in input line? */ 648 back_to_col_1(); 649 should_retype = TRUE; 650 } 651 if (command == LIST) /* Always retype after a LIST */ 652 should_retype = TRUE; 653 if (should_retype) 654 printprompt(); 655 pushback(inputline); 656 if (should_retype) 657 retype(); 658 } 659 setup_tty(OFF); 660 return (num_read); 661 } 662 663 static int 664 ignored(entry) 665 register Char *entry; 666 { 667 struct varent *vp; 668 register Char **cp; 669 670 if ((vp = adrof(STRfignore)) == NULL || (cp = vp->vec) == NULL) 671 return (FALSE); 672 for (; *cp != NULL; cp++) 673 if (is_suffix(entry, *cp)) 674 return (TRUE); 675 return (FALSE); 676 } 677 #endif /* FILEC */ 678