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