1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
|
/*
* filec.c
*
* Filename prompting and completion routines
* Written by T.E.Dickey for vile (march 1993).
*
*
* $Id: filec.c,v 1.138 2025/01/26 14:11:02 tom Exp $
*/
#include "estruct.h"
#include "edef.h"
#undef USE_QSORT
#if SYS_OS2
# define USE_QSORT 0
# define INCL_DOSFILEMGR
# define INCL_ERRORS
# include <os2.h>
# define FoundDirectory(path) ((fb.attrFile & FILE_DIRECTORY) != 0)
#endif
#ifndef USE_QSORT
#define USE_QSORT 1
#endif
/* Systems that can have leafnames beginning with '.' (this doesn't include
* VMS, which does allow this as unlikely as that may seem, because the logic
* that masks dots simply won't work in conjunction with the other translations
* to/from hybrid form).
*/
#if (SYS_UNIX || SYS_WINNT) && ! OPT_VMS_PATH
#define USE_DOTNAMES 1
#else
#define USE_DOTNAMES 0
#endif
#define isDotname(leaf) (!strcmp(leaf, ".") || !strcmp(leaf, ".."))
#if defined(MISSING_EXTERN_ENVIRON) || (defined(__DJGPP__) && __DJGPP__ >= 2)
extern char **environ;
#endif
#if CC_LCC_WIN32
#define environ _environ
#endif
#define SLASH (EOS+1) /* less than everything but EOS */
#if OPT_VMS_PATH
#define KBD_FLAGS KBD_NORMAL|KBD_UPPERC
#endif
#ifndef KBD_FLAGS
#define KBD_FLAGS KBD_NORMAL
#endif
#ifndef FoundDirectory
# define FoundDirectory(path) is_directory(path)
#endif
static char **MyGlob; /* expanded list */
static int in_glob; /* index into MyGlob[] */
static int only_dir; /* can only match real directories */
static void
free_expansion(void)
{
MyGlob = glob_free(MyGlob);
in_glob = -1;
}
#if COMPLETE_DIRS || COMPLETE_FILES
#include "dirstuff.h"
#if OPT_VMS_PATH
static void vms2hybrid(char *path);
#endif
/*--------------------------------------------------------------------------*/
/*
* Test if the path has a trailing slash-delimiter (i.e., can be syntactically
* distinguished from non-directory paths).
*/
static int
trailing_slash(const char *path)
{
size_t len = strlen(path);
if (len != 0) {
#if OPT_VMS_PATH
if (is_vms_pathname(path, TRUE))
return TRUE;
#endif
return (is_slashc(path[len - 1]));
}
return FALSE;
}
/*
* Force a trailing slash on the end of the path, returns the length of the
* resulting path.
*/
static size_t
force_slash(char *path)
{
size_t len = strlen(path);
#if OPT_VMS_PATH
if (!is_vms_pathname(path, -TRUE)) /* must be unix-style name */
#endif
if (!trailing_slash(path)) {
path[len++] = SLASHC;
path[len] = EOS;
}
return len;
}
/*
* Compare two paths lexically.
*/
static int
pathcmp(const LINE *lp, const char *text)
{
const char *l, *t;
int lc, tc;
if (lp == NULL
|| llength(lp) <= 0) /* (This happens on the first insertion) */
return -1;
l = lvalue(lp);
t = text;
for_ever {
lc = *l++;
tc = *t++;
if (global_g_val(GMDFILENAME_IC)) {
if (isUpper(lc))
lc = toLower(lc);
if (isUpper(tc))
tc = toLower(tc);
}
if (lc == tc) {
if (tc == EOS)
return 0;
} else {
if (is_slashc(lc)) {
lc = (*l != EOS) ? SLASH : EOS;
}
if (is_slashc(tc)) {
tc = (*t != EOS) ? SLASH : EOS;
}
return lc - tc;
}
}
}
/*
* Insert a pathname at the given line-pointer.
* Allocate up to three extra bytes for possible trailing slash, EOS, and
* directory scan indicator. The latter is only used when there is a trailing
* slash.
*/
static LINE *
makeString(BUFFER *bp, LINE *lp, char *text, size_t len)
{
LINE *np;
int extra = (len != 0 && is_slashc(text[len - 1])) ? 2 : 3;
beginDisplay();
if ((np = lalloc((int) len + extra, bp)) == NULL) {
lp = NULL;
} else {
#if !OPT_MSDOS_PATH
/*
* If we are NOT processing MSDOS pathnames, we may have literal
* backslashes in the pathnames. Escape those by doubling them.
*/
text = add_backslashes(text);
#endif
(void) strcpy(lvalue(np), text);
lvalue(np)[len + (size_t) extra - 1] = 0; /* clear scan indicator */
llength(np) -= extra; /* hide the null and scan indicator */
set_lforw(lback(lp), np);
set_lback(np, lback(lp));
set_lback(lp, np);
set_lforw(np, lp);
lp = np;
}
endofDisplay();
return lp;
}
/*
* Create a buffer to store null-terminated strings.
*
* The file (or directory) completion buffer is initialized at the beginning of
* each command. Wildcard expansion causes entries to be read for a given path
* on demand. Resetting the buffer in this fashion is a tradeoff between
* efficiency (allows reuse of a pattern in NAMEC/TESTC operations) and speed
* (directory scanning is slow).
*
* The tags buffer is initialized only once for a given tags-file.
*/
static BUFFER *
bs_init(const char *name)
{
BUFFER *bp;
if ((bp = bfind(name, BFINVS)) != NULL) {
b_clr_scratch(bp); /* make it nonvolatile */
(void) bclear(bp);
bp->b_active = TRUE;
}
return bp;
}
/*
* Look for or insert a pathname string into the given buffer. Start looking
* at the given line if non-null. The pathname is expected to be in
* canonical form.
*
* fname - pathname to find
* len - ...its length
* bp - buffer to search
* lpp - in/out line pointer, for iteration
*/
static int
bs_find(char *fname, size_t len, BUFFER *bp, LINE **lpp)
{
LINE *lp;
int doit = FALSE;
#if OPT_VMS_PATH
char temp[NFILEN];
if (!is_slashc(*fname))
vms2hybrid(fname = vl_strncpy(temp, fname, sizeof(temp)));
#endif
if (lpp == NULL || (lp = *lpp) == NULL)
lp = buf_head(bp);
lp = lforw(lp);
for_ever {
int r = pathcmp(lp, fname);
if (r == 0) {
if (trailing_slash(fname)
&& !trailing_slash(lvalue(lp))) {
/* reinsert so it is sorted properly! */
lremove(bp, lp);
return bs_find(fname, len, bp, lpp);
}
break;
} else if (r > 0) {
doit = TRUE;
break;
}
lp = lforw(lp);
if (lp == buf_head(bp)) {
doit = TRUE;
break;
}
}
if (doit) {
lp = makeString(bp, lp, fname, len);
b_clr_counted(bp);
}
if (lpp)
*lpp = lp;
return TRUE;
}
#endif /* COMPLETE_DIRS || COMPLETE_FILES */
#if COMPLETE_DIRS || COMPLETE_FILES
static BUFFER *MyBuff; /* the buffer containing pathnames */
static const char *MyName; /* name of buffer for name-completion */
/*
* Returns true if the string looks like an environment variable (i.e.,
* a '$' followed by an optional name.
*/
static int
is_environ(const char *s)
{
if (*s++ == '$') {
while (*s != EOS) {
if (!isAlnum(*s) && (*s != '_'))
return FALSE;
s++;
}
return TRUE;
}
return FALSE;
}
/*
* Tests if the given path has been scanned during this prompt/reply operation
*
* If there is anything else in the list that we can do completion with, return
* true. This allows the case in which we scan a directory (for directory
* completion and then look at the subdirectories. It should not permit
* directories which have already been scanned to be rescanned.
*/
static int
already_scanned(BUFFER *bp, char *path)
{
LINE *lp;
size_t len;
char fname[NFILEN];
LINE *slp;
len = force_slash(vl_strncpy(fname, path, sizeof(fname)));
for_each_line(lp, bp) {
if (cs_strcmp(global_g_val(GMDFILENAME_IC), fname, lvalue(lp)) == 0) {
if (lvalue(lp)[llength(lp) + 1])
return TRUE;
else
break; /* name should not occur more than once */
}
}
/* force the name in with a trailing slash */
slp = buf_head(bp);
(void) bs_find(fname, len, bp, &slp);
/*
* mark name as scanned (since that is what we're about to do after
* returning)
*/
lp = slp;
lvalue(lp)[llength(lp) + 1] = 1;
return FALSE;
}
#if USE_DOTNAMES
/*
* Before canonicalizing a pathname, check for a leaf consisting of trailing
* dots. Convert this to another form so that it won't be combined with
* other leaves. We do this because we don't want to prevent the user from
* doing filename completion with leaves that begin with dots.
*/
static void
mask_dots(char *path, size_t *dots)
{
char *leaf = pathleaf(path);
if (isDotname(leaf)) {
*dots = strlen(leaf);
memset(leaf, '?', *dots);
} else
*dots = 0;
}
/*
* Restore the leaf to its original form.
*/
static void
add_dots(char *path, size_t dots)
{
if (dots != 0) {
memset(pathleaf(path), '.', dots);
}
}
static void
strip_dots(char *path, size_t *dots)
{
char *leaf = pathleaf(path);
if (isDotname(leaf)) {
*dots = strlen(leaf);
*leaf = EOS;
}
}
#define if_dots(path, dots) if (!strncmp(path, "..", dots))
#else
#define mask_dots(path, dots) /* nothing */
#define add_dots(path, dots) /* nothing */
#define strip_dots(path, dots) /* nothing */
#define if_dots(path, dots) /* nothing */
#endif /* USE_DOTNAMES */
#if OPT_VMS_PATH
/*
* Convert a canonical VMS pathname to a hybrid form, in which the leaf (e.g.,
* "name.type;ver") is left untouched, but the directory portion is in UNIX
* form. This alleviates a sticky problem with VMS's pathname delimiters by
* making them all '/' characters.
*/
static void
vms2hybrid(char *path)
{
char leaf[NFILEN];
char head[NFILEN];
char *s = vl_strncpy(head, path, sizeof(head));
char *t;
TRACE(("vms2hybrid '%s'\n", path));
(void) vl_strncpy(leaf, s = pathleaf(head), sizeof(leaf));
if ((t = is_vms_dirtype(leaf)) != 0)
(void) strcpy(t, "/");
*s = EOS;
if (s == path) /* a non-canonical name got here somehow */
(void) vl_strncpy(head, current_directory(FALSE), sizeof(head));
pathcat(path, mkupper(vms2unix_path(head, head)), leaf);
TRACE((" -> '%s' (vms2hybrid)\n", path));
}
static void
hybrid2vms(char *path)
{
char leaf[NFILEN];
char head[NFILEN];
char *s = vl_strncpy(head, path, sizeof(head));
TRACE(("hybrid2vms '%s'\n", path));
(void) vl_strncpy(leaf, s = pathleaf(head), sizeof(leaf));
*s = EOS;
if (s == head) /* a non-canonical name got here somehow */
(void) vms2unix_path(head, current_directory(FALSE));
(void) pathcat(path, head, leaf);
if (isDotname(leaf)) {
force_slash(path); /* ...so we'll interpret as directory */
lengthen_path(path); /* ...makes VMS-style absolute-path */
} else {
unix2vms_path(path, path);
}
TRACE((" -> '%s' hybrid2vms\n", path));
}
static void
hybrid2unix(char *path)
{
char leaf[NFILEN];
char head[NFILEN];
char *s = vl_strncpy(head, path, sizeof(head));
TRACE(("hybrid2unix '%s'\n", path));
(void) vl_strncpy(leaf, s = pathleaf(head), sizeof(leaf));
*s = EOS;
if (s == path) /* a non-canonical name got here somehow */
(void) vms2unix_path(head, current_directory(FALSE));
pathcat(path, head, leaf);
/* The semicolon that indicates the version is a little tricky. When
* simulating via fakevms.c on UNIX, we've got to trim the version
* marker at this point. Otherwise, it'll be changed to a dollar sign
* the next time the string is converted from UNIX to VMS form. A
* side-effect is that the name-completion echos (for UNIX only!) the
* version, but doesn't store it in the buffer returned to the caller.
*/
if ((s = strrchr(path, ';')) != 0) {
#if SYS_UNIX
*s = EOS; /* ...we're only simulating */
#else
*s = '.'; /* this'll be interpreted as version-mark */
#endif
}
TRACE((" -> '%s' hybrid2unix\n", path));
}
#endif /* OPT_VMS_PATH */
#if USE_QSORT
/*
* Compare two lines (containing paths) for quicksort by calling pathcmp().
* If the paths compare equal force the one with the trailing slash to be
* less than. This'll make deleting the ones without slashes easier in
* sortMyBuff().
*/
static int
qs_pathcmp(const void *lpp1, const void *lpp2)
{
const LINE *lp1 = *(const LINE *const *) lpp1;
int r = pathcmp(lp1, lvalue(*(const LINE *const *) lpp2));
if (r == 0) {
if (llength(lp1) > 0 && is_slashc(lgetc(lp1, llength(lp1) - 1)))
return -1;
else /* Don't care if the other one has slash or not... */
return 1; /* ...returning 1 for two equal elements won't do */
/* any harm. */
} else
return r;
}
static void
remove_duplicates(BUFFER *bp)
{
LINE *plp = lforw(buf_head(bp));
LINE *lp;
while (plp != buf_head(bp)) {
if ((lp = lforw(plp)) != buf_head(bp)) {
if (pathcmp(plp, lvalue(lp)) == 0) {
lremove2(bp, lp);
continue;
}
}
plp = lforw(plp);
}
}
static void
sortMyBuff(BUFFER *bp)
{
L_NUM n;
LINE **sortvec;
LINE *lp, *plp;
LINE **slp;
b_clr_counted(bp);
if ((n = vl_line_count(bp)) > 0) {
beginDisplay();
if ((sortvec = typecallocn(LINE *, (size_t) n)) != NULL) {
slp = sortvec;
for_each_line(lp, bp) {
*slp++ = lp;
}
qsort((char *) sortvec, (size_t) n, sizeof(LINE *), qs_pathcmp);
plp = buf_head(bp);
slp = sortvec;
while (n-- > 0) {
lp = *slp++;
set_lforw(plp, lp);
set_lback(lp, plp);
plp = lp;
}
lp = buf_head(bp);
set_lforw(plp, lp);
set_lback(lp, plp);
remove_duplicates(bp);
b_clr_counted(bp);
free(sortvec);
}
endofDisplay();
}
}
#endif /* USE_QSORT */
int
fill_directory_buffer(BUFFER *bp, char *path, size_t dots GCC_UNUSED)
{
int count = 0;
char *s;
#if SYS_OS2
FILEFINDBUF3 fb;
ULONG entries;
HDIR hdir;
APIRET rc;
int case_preserving = is_case_preserving(path);
#else /* UNIX, VMS or MSDOS */
char *leaf;
DIR *dp;
DIRENT *de;
#endif
char temp[NFILEN];
path = vl_strncpy(temp, path, sizeof(temp));
TRACE(("fill_directory_buffer '%s'\n", path));
#if SYS_OS2
s = path + force_slash(path);
(void) strcat(path, "*.*");
hdir = HDIR_CREATE;
entries = 1;
rc = DosFindFirst(SL_TO_BSL(path), &hdir,
FILE_DIRECTORY | FILE_READONLY,
&fb, sizeof(fb), &entries, FIL_STANDARD);
if (rc == NO_ERROR) {
do {
(void) strcpy(s, fb.achName);
if (!case_preserving)
(void) mklower(s);
if (isDotname(s))
continue;
if (only_dir) {
if (!FoundDirectory(path))
continue;
(void) force_slash(path);
}
#if COMPLETE_DIRS
else {
if (global_g_val(GMDDIRC) && FoundDirectory(path))
(void) force_slash(path);
}
#endif
TRACE(("> '%s'\n", path));
if_dots(s, dots) count++;
(void) bs_find(path, strlen(path), bp, (LINE **) 0);
} while (entries = 1,
DosFindNext(hdir, &fb, sizeof(fb), &entries) == NO_ERROR
&& entries == 1);
DosFindClose(hdir);
}
#else /* UNIX, VMS or MSDOS */
/* For MS-DOS pathnames, force the use of '\' instead of '/' in the
* open-directory operation to allow for runtime libraries that
* don't allow using UNIX-style '/' pathnames.
*/
if ((dp = opendir(SL_TO_BSL(path))) != NULL) {
s = path;
#if !OPT_VMS_PATH
s += force_slash(path);
#endif
leaf = s;
while ((de = readdir(dp)) != NULL) {
#if SYS_UNIX || SYS_VMS || SYS_WINNT
# if USE_D_NAMLEN
(void) strncpy(leaf, de->d_name, (size_t) (de->d_namlen));
leaf[de->d_namlen] = EOS;
# else
(void) strcpy(leaf, de->d_name);
# endif
#else
# if SYS_MSDOS
(void) mklower(strcpy(leaf, de->d_name));
# else
problem with ifdef;
# endif
#endif
#if OPT_VMS_PATH
vms_dir2path(path);
#else
# if SYS_UNIX || SYS_MSDOS || SYS_OS2 || SYS_WINNT
if (isDotname(leaf))
continue;
# endif
#endif
if (only_dir) {
if (!FoundDirectory(path))
continue;
(void) force_slash(path);
}
#if COMPLETE_DIRS
else {
if (global_g_val(GMDDIRC) && FoundDirectory(path))
(void) force_slash(path);
}
#endif
TRACE(("> '%s'\n", path));
if_dots(leaf, dots) count++;
#if USE_QSORT
#if OPT_VMS_PATH
if (temp != path)
vl_strncpy(temp, path, sizeof(temp));
vms2hybrid(s = temp);
#else
s = path;
#endif
(void) makeString(bp, buf_head(bp), s, strlen(s));
#else /* !USE_QSORT */
(void) bs_find(path, strlen(path), bp, (LINE **) 0);
#endif /* USE_QSORT/!USE_QSORT */
}
(void) closedir(dp);
#if USE_QSORT
sortMyBuff(bp);
#endif
}
#endif /* SYS_OS2/!SYS_OS2 */
TRACE(("...fill_directory_buffer returns %d\n", count));
return count;
}
/*
* If the given path is not in the completion-buffer, expand it, and add the
* expanded paths to the buffer. Because the user may be trying to get an
* intermediate directory-name, we must 'stat()' each name, so that we can
* provide the trailing slash in the completion. This is slow.
*/
static int
fillMyBuff(BUFFER *bp, char *name)
{
size_t dots = 0;
int count = 0;
char *s;
char path[NFILEN + 2];
char temp[NFILEN + 2];
#if OPT_VMS_PATH
int trim_leaf = FALSE;
#endif
TRACE(("fillMyBuff '%s'\n", name));
/**********************************************************************/
if (is_environ(name)) {
LINE *lp;
int n;
size_t len = strlen(name) - 1;
/*
* If an environment variable happens to evaluate to a
* directory name, this chunk of logic returns a '1' to tell
* our caller that it's time to add a slash.
*/
for (n = 0; (s = environ[n]) != NULL; n++) {
if (!strncmp(s, name + 1, len)
&& s[len] == '=') {
return already_scanned(bp, name) ? 1 : 0;
}
}
/*
* The presence of any environment variable in the list is
* sufficient indication that we've copied the environment.
*/
for_each_line(lp, bp) {
if (is_environ(lvalue(lp)))
return 0;
}
/*
* Copy all of the environment-variable names, prefixed with
* the '$' that indicates what they are.
*/
for (n = 0; environ[n] != NULL; n++) {
char *d = path;
s = environ[n];
*d++ = name[0];
while (((*d = *s++) != '=') && (*d != EOS)) {
if ((size_t) (d++ - path) > sizeof(path) - 2)
break;
}
*d = EOS;
#if SYS_MSDOS || SYS_OS2
mklower(path); /* glob.c will uppercase for getenv */
#endif
#if USE_QSORT
(void) makeString(bp, buf_head(bp), path, strlen(path));
#else /* !USE_QSORT */
(void) bs_find(path, strlen(path), bp, (LINE **) 0);
#endif /* USE_QSORT/!USE_QSORT */
TRACE(("> '%s'\n", path));
}
#if USE_QSORT
sortMyBuff(bp);
#endif
} else {
(void) vl_strncpy(path, name, sizeof(path));
#if OPT_MSDOS_PATH
bsl_to_sl_inplace(path);
#endif
#if OPT_VMS_PATH
hybrid2vms(path); /* convert to canonical VMS name */
#else
strip_dots(path, &dots); /* ignore trailing /. or /.. */
#endif
if (!is_environ(path) && !is_directory(path)) {
#if OPT_VMS_PATH
trim_leaf = TRUE;
#endif
*pathleaf(path) = EOS;
if (!is_directory(path))
return 1;
}
#if OPT_VMS_PATH
(void) vl_strncpy(temp, name, sizeof(temp));
/* will match the hybrid name */
if (trim_leaf)
*pathleaf(temp) = EOS;
#else
(void) vl_strncpy(temp, path, sizeof(temp));
#endif
if (already_scanned(bp, temp)) {
#if OPT_VMS_PATH
count = 1;
#else
#if USE_DOTNAMES
/*
* Handle the special cases where we're continuing a completion
* with ".." on the end of the path. We have to distinguish
* the return value so that we can drive a second scan for the
* case where there's no dot-names found.
*/
if (dots) {
LINE *lp;
size_t need, want;
while (dots--)
strcat(path, ".");
(void) lengthen_path(vl_strncpy(temp, path, sizeof(temp)));
need = strlen(temp);
want = strlen(path);
for_each_line(lp, bp) {
size_t have = (size_t) llength(lp);
if (have == need
&& !memcmp(lvalue(lp), temp, need))
count = -1;
else if (have >= want
&& !memcmp(lvalue(lp), path, want)) {
count = 1;
break;
}
}
}
#endif
#endif
} else {
count = fill_directory_buffer(bp, path, dots);
}
}
TRACE(("...fillMyBuff returns %d\n", count));
return count;
}
/*
* Make the list of names needed for name-completion
*/
static void
makeMyList(BUFFER *bp, char *name)
{
size_t need;
int n;
LINE *lp;
char *slashocc;
int len = (int) strlen(name);
beginDisplay();
if (len != 0 && is_slashc(name[len - 1]))
len++;
(void) bsizes(bp);
need = (size_t) bp->b_linecount + 2;
if (bp->b_index_size < need) {
bp->b_index_size = need * 2;
if (bp->b_index_list == NULL) {
bp->b_index_list = typeallocn(char *, bp->b_index_size);
} else {
safe_typereallocn(char *, bp->b_index_list, bp->b_index_size);
}
}
if (bp->b_index_list != NULL) {
n = 0;
for_each_line(lp, bp) {
/* exclude listings of subdirectories below
current directory */
if (llength(lp) >= len
&& ((slashocc = strchr(lvalue(lp) + len, SLASHC)) == NULL
|| slashocc[1] == EOS))
bp->b_index_list[n++] = lvalue(lp);
}
bp->b_index_list[n] = NULL;
} else {
bp->b_index_size = 0;
}
endofDisplay();
}
#if NO_LEAKS
static void
freeMyList(BUFFER *bp)
{
if (valid_buffer(bp)) {
beginDisplay();
FreeAndNull(bp->b_index_list);
bp->b_index_size = 0;
endofDisplay();
}
}
#else
#define freeMyList(bp) /* nothing */
#endif
static void
force_output(int c, char *buf, size_t *pos)
{
kbd_putc(c);
term.flush();
buf[*pos] = (char) c;
*pos += 1;
buf[*pos] = EOS;
}
/*
* Initialize the file-completion module. We'll only do either file- or
* directory-completion during any given command, and they use different
* buffers (and slightly different parsing).
*/
void
init_filec(const char *buffer_name)
{
MyBuff = NULL;
MyName = buffer_name;
}
/*
* Perform the name-completion/display. Note that we must convert a copy of
* the pathname to absolute form so that we can match against the strings that
* are stored in the completion table. However, the characters that might be
* added are always applicable to the original buffer.
*
* We only do name-completion if asked; if we did it when the user typed a
* return it would be too slow.
*/
int
path_completion(DONE_ARGS)
{
int code = FALSE;
int action = (c == NAMEC || c == TESTC);
int ignore;
#if USE_DOTNAMES
size_t dots = 0;
int count;
#endif
TRACE(("path_completion(%#x '%c' %d:\"%s\")\n",
flags, c, (int) *pos, visible_buff(buf, (int) *pos, TRUE)));
(void) flags;
if (buf == NULL)
return FALSE;
ignore = (*buf != EOS && isInternalName(buf));
#if OPT_VMS_PATH
if (ignore && action) { /* resolve scratch-name conflict */
if (is_vms_pathname(buf, -TRUE))
ignore = FALSE;
}
#endif
if (ignore) {
if (action) { /* completion-chars have no meaning */
force_output(c, buf, pos);
}
} else if (action) {
char *s;
char path[NFILEN];
size_t oldlen;
size_t newlen;
/* initialize only on demand */
if (MyBuff == NULL) {
if (MyName == NULL
|| (MyBuff = bs_init(MyName)) == NULL)
return FALSE;
}
/*
* Copy 'buf' into 'path', making it canonical-form.
*/
#if OPT_VMS_PATH
if (*vl_strncpy(path, buf, sizeof(path)) == EOS) {
(void) vl_strncpy(path, current_directory(FALSE), sizeof(path));
} else if (!is_environ(path)) {
char frac[NFILEN];
if (is_vms_pathname(path, -TRUE)) {
s = vms_pathleaf(path);
(void) strcpy(frac, s);
*s = EOS;
} else {
s = pathleaf(path);
if (strcmp(s, ".")
&& is_vms_pathname(s, -TRUE)) {
(void) strcpy(frac, s);
*s = EOS;
} else { /* e.g., path=".." */
*frac = EOS;
}
}
if (*path == EOS)
(void) vl_strncpy(path, current_directory(FALSE), sizeof(path));
else {
if (!is_vms_pathname(path, -TRUE)) {
unix2vms_path(path, path);
if (*path == EOS) {
(void) vl_strncpy(path,
current_directory(FALSE),
sizeof(path));
}
}
(void) lengthen_path(path);
}
(void) strcat(path, frac);
}
if (is_vms_pathname(path, -TRUE)) {
int pad = is_vms_pathname(path, TRUE);
vms2hybrid(path);
/*
* FIXME: This compensates for the hack in canonpath
*/
if (!strcmp(buf, "/")) {
while ((size_t) *pos < strlen(path))
force_output(path[*pos], buf, pos);
} else if (pad && *buf != EOS && !trailing_slash(buf)) {
force_output(SLASHC, buf, pos);
}
}
#else
if (is_environ(buf)) {
(void) vl_strncpy(path, buf, sizeof(path));
} else {
# if SYS_UNIX || OPT_MSDOS_PATH
char **expand;
/*
* Expand _unique_ wildcards and environment variables.
* Like 'doglob()', but without the prompt.
*/
expand = glob_string(vl_strncpy(path, buf, sizeof(path)));
switch (glob_length(expand)) {
default:
(void) glob_free(expand);
kbd_alarm();
return FALSE;
case 1:
(void) vl_strncpy(path, expand[0], sizeof(path));
/*FALLTHRU */
case 0:
(void) glob_free(expand);
break;
}
mask_dots(path, &dots);
(void) lengthen_path(path);
add_dots(path, dots);
# if OPT_MSDOS_PATH
/*
* Pick up slash (special case) when we've just expanded a
* device such as "c:" to "c:/".
*/
if ((newlen = strlen(path)) == 3
&& (oldlen = strlen(buf)) == 2
&& is_slashc(path[newlen - 1])
&& path[newlen - 2] == ':') {
force_output(SLASHC, buf, pos);
}
# endif
# endif
}
#endif
if ((s = is_appendname(buf)) == NULL)
s = buf;
if ((*s == EOS) || trailing_slash(s)) {
if (*path == EOS)
(void) vl_strncpy(path, ".", sizeof(path));
(void) force_slash(path);
}
if ((s = is_appendname(path)) != NULL) {
char *d;
for (d = path; (*d++ = *s++) != EOS;) {
/* EMPTY */ ;
}
}
#if OPT_MSDOS_PATH
/* if the user typed a back-slash, we need to
* convert it, since it's stored as '/' in the file
* completion buffer to avoid conflicts with the use of
* backslash for escaping special characters.
*/
bsl_to_sl_inplace(path);
#endif
newlen =
oldlen = strlen(path);
#if USE_DOTNAMES
/*
* If we're on a filesystem that allows names beginning with
* ".", try to handle the ambiguity between .name and ./ by
* preferring matches on the former. If we get a zero return
* from the first scan, it means that we've only the latter
* case to consider.
*/
count = fillMyBuff(MyBuff, path);
if (dots == 2) {
if (count == 0) {
force_slash(path);
lengthen_path(path);
newlen =
oldlen = strlen(path);
(void) fillMyBuff(MyBuff, path);
} else if (count < 0) {
lengthen_path(path);
newlen =
oldlen = strlen(path);
}
} else if (dots == 1) {
if (count == 0) {
lengthen_path(path);
newlen =
oldlen = strlen(path);
}
}
#else
(void) fillMyBuff(MyBuff, path);
#endif
makeMyList(MyBuff, path);
/* FIXME: should also force-dot to the matched line, as in history.c */
/* FIXME: how can I force buffer-update to show? */
code = kbd_complete(global_g_val(GMDFILENAME_IC) ? KBD_CASELESS : 0,
c, path, &newlen,
(const char *) &MyBuff->b_index_list[0],
sizeof(MyBuff->b_index_list[0]));
(void) strcat(buf, path + oldlen);
#if OPT_VMS_PATH
if (*buf != EOS
&& !is_vms_pathname(buf, -TRUE))
hybrid2unix(buf);
#endif
*pos = strlen(buf);
/* avoid accidentally picking up directory names for files */
if ((code == TRUE)
&& !only_dir
&& !trailing_slash(path)
&& is_directory(path)) {
force_output(SLASHC, buf, pos);
code = FALSE;
}
}
TRACE((" -> '%s' path_completion\n", buf));
return code;
}
#else /* no filename-completion */
#define freeMyList(bp) /* nothing */
#endif /* filename-completion */
/******************************************************************************/
#ifdef GMDWARNBLANKS
static int
has_non_graphics(char *path)
{
int ch;
while ((ch = *path++) != EOS) {
if (isSpace(ch) || !isPrint(ch))
return TRUE;
}
return FALSE;
}
static int
strip_non_graphics(char *path)
{
char *t = path;
int ch;
int rc = FALSE;
while ((ch = *path++) != EOS) {
if (!isSpace(ch) && isPrint(ch)) {
*t++ = (char) ch;
rc = TRUE;
}
}
*t = 0;
return rc;
}
#endif
/******************************************************************************/
/*
* Prompt for a file name, allowing completion via tab and '?'
*
* flag - +1 to read, -1 to write, 0 don't care
*/
int
mlreply_file(const char *prompt, TBUFF **buffer, UINT flag, char *result)
{
int status;
static TBUFF *last;
char Reply[NFILEN];
int (*complete) (DONE_ARGS) = no_completion;
int had_fname = (valid_buffer(curbp)
&& curbp->b_fname != NULL
&& curbp->b_fname[0] != EOS);
int do_prompt = (clexec || isnamedcmd || (flag & FILEC_PROMPT));
int ok_expand = ((flag & FILEC_EXPAND) != 0);
flag &= (UINT) (~(FILEC_PROMPT | FILEC_EXPAND));
#if COMPLETE_FILES
if (do_prompt && !clexec) {
complete = shell_complete;
init_filec(FILECOMPLETION_BufName);
}
#endif
/* use the current filename if none given */
if (buffer == NULL) {
(void) tb_scopy(buffer = &last,
had_fname && is_pathname(curbp->b_fname)
? shorten_path(vl_strncpy(Reply,
curbp->b_fname,
sizeof(Reply)),
FALSE)
: "");
}
if (do_prompt) {
char *t1 = tb_values(*buffer);
char *t2 = is_appendname(t1);
if (t1 != NULL)
(void) vl_strncpy(Reply, (t2 != NULL) ? t2 : t1, sizeof(Reply));
else
*Reply = EOS;
status = kbd_string(prompt, Reply, sizeof(Reply),
'\n', KBD_FLAGS | KBD_MAYBEC, complete);
freeMyList(MyBuff);
if (status == ABORT)
return status;
if (status != TRUE) {
if ((flag == FILEC_REREAD)
&& had_fname
&& (!global_g_val(GMDWARNREREAD)
|| ((status = mlyesno("Reread current buffer")) == TRUE)))
(void) vl_strncpy(Reply, curbp->b_fname, sizeof(Reply));
else
return status;
} else if (kbd_is_pushed_back() && isShellOrPipe(Reply)) {
/*
* The first call on 'kbd_string()' split the text off
* the shell command. This is needed for the logic of
* colon-commands, but is inappropriate for filename
* prompting. Read the rest of the text into Reply.
*/
status = kbd_string(prompt, Reply, sizeof(Reply),
'\n', KBD_FLAGS | KBD_MAYBEC, complete);
}
/*
* Not everyone expects to be able to write files whose names
* have embedded (or leading/trailing) blanks.
*/
#ifdef GMDWARNBLANKS
if (status == TRUE
&& global_g_val(GMDWARNBLANKS)
&& has_non_graphics(Reply)) {
if ((status = mlyesno("Strip nonprinting chars?")) == TRUE)
status = strip_non_graphics(Reply);
}
if (status != TRUE)
return status;
#endif
} else if (!screen_to_bname(Reply, sizeof(Reply))) {
return FALSE;
}
if (flag >= FILEC_UNKNOWN && is_appendname(Reply) != NULL) {
mlforce("[file is not a legal input]");
return FALSE;
}
free_expansion();
if (ok_expand) {
if ((MyGlob = glob_string(Reply)) == NULL
|| (status = glob_length(MyGlob)) == 0) {
mlforce("[No files found] %s", Reply);
return FALSE;
}
if (status > 1) {
char tmp[80];
(void) lsprintf(tmp, "Will create %d buffers. Okay", status);
status = mlyesno(tmp);
mlerase();
if (status != TRUE)
return status;
}
} else if (doglob(Reply) != TRUE) {
return FALSE;
}
(void) strcpy(result, Reply);
if (flag <= FILEC_WRITE) { /* we want to write a file */
if (!isInternalName(Reply)
&& !same_fname(Reply, curbp, TRUE)
&& cfg_locate(Reply, FL_CDIR | FL_READABLE)) {
if (mlyesno("File exists, okay to overwrite") != TRUE) {
mlwrite("File not written");
return FALSE;
}
}
}
(void) tb_scopy(buffer, Reply);
return TRUE;
}
/******************************************************************************/
/*
* Prompt for a directory name, allowing completion via tab and '?'
*/
int
mlreply_dir(const char *prompt, TBUFF **buffer, char *result)
{
int status;
static TBUFF *last;
char Reply[NFILEN];
int (*complete) (DONE_ARGS) = no_completion;
#if COMPLETE_DIRS
if (isnamedcmd && !clexec) {
complete = path_completion;
init_filec(DIRCOMPLETION_BufName);
}
#endif
/* use the current directory if none given */
if (buffer == NULL) {
(void) tb_scopy(buffer = &last,
vl_strncpy(Reply, current_directory(TRUE), sizeof(Reply)));
}
if (clexec || isnamedcmd) {
if (tb_values(*buffer) != NULL)
(void) vl_strncpy(Reply, tb_values(*buffer), sizeof(Reply));
else
*Reply = EOS;
only_dir = TRUE;
status = kbd_string(prompt, Reply, sizeof(Reply), '\n',
KBD_FLAGS | KBD_MAYBEC, complete);
freeMyList(MyBuff);
only_dir = FALSE;
if (status != TRUE)
return status;
} else if (!screen_to_bname(Reply, sizeof(Reply))) {
return FALSE;
}
(void) tb_scopy(buffer, strcpy(result, Reply));
return TRUE;
}
/******************************************************************************/
/*
* This is called after 'mlreply_file()' to iterate over the list of files
* that are matched by a glob-expansion.
*/
char *
filec_expand(void)
{
if (MyGlob != NULL) {
if (MyGlob[++in_glob] != NULL)
return MyGlob[in_glob];
free_expansion();
}
return NULL;
}
|