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