| 1 |
/************************************************************************** |
| 2 |
* This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is * |
| 3 |
* provided by Jonathan and Jovehacks without charge and without * |
| 4 |
* warranty. You may copy, modify, and/or distribute JOVE, provided that * |
| 5 |
* this notice is included in all the source files and documentation. * |
| 6 |
**************************************************************************/ |
| 7 |
|
| 8 |
#include "jove.h" |
| 9 |
#include "jctype.h" |
| 10 |
#include "chars.h" |
| 11 |
#include "disp.h" |
| 12 |
#include "fp.h" |
| 13 |
#include "scandir.h" |
| 14 |
#include "screen.h" /* for Placur */ |
| 15 |
#include "ask.h" |
| 16 |
#include "delete.h" |
| 17 |
#include "insert.h" |
| 18 |
#include "extend.h" |
| 19 |
#include "fmt.h" |
| 20 |
#include "marks.h" |
| 21 |
#include "move.h" |
| 22 |
#include "wind.h" |
| 23 |
|
| 24 |
#ifdef MAC |
| 25 |
# include "mac.h" |
| 26 |
#else /* !MAC */ |
| 27 |
# ifdef F_COMPLETION |
| 28 |
# include <sys/stat.h> |
| 29 |
# endif |
| 30 |
#endif /* !MAC */ |
| 31 |
|
| 32 |
ZXchar AbortChar = CTL('G'); /* VAR: cancels command input */ |
| 33 |
jbool DispDefFs = YES; /* VAR: display default filenames in prompt? */ |
| 34 |
|
| 35 |
jbool Asking = NO; |
| 36 |
int AskingWidth; |
| 37 |
|
| 38 |
char Minibuf[LBSIZE]; |
| 39 |
private LinePtr CurAskPtr = NULL; /* points at some line in mini-buffer */ |
| 40 |
private Buffer *AskBuffer = NULL; /* Askbuffer points to actual structure */ |
| 41 |
|
| 42 |
/* The way the mini-buffer works is this: The first line of the mini-buffer |
| 43 |
* is where the user does his stuff. The rest of the buffer contains |
| 44 |
* strings that the user often wants to use, for instance, file names, or |
| 45 |
* common search strings, etc. If he types ^N or ^P while in ask(), we |
| 46 |
* bump the point up or down a line and extract the contents (we make sure |
| 47 |
* is somewhere in the mini-buffer). |
| 48 |
*/ |
| 49 |
private Buffer * |
| 50 |
get_minibuf(void) |
| 51 |
{ |
| 52 |
if (AskBuffer) { /* make sure it still exists */ |
| 53 |
Buffer *b; |
| 54 |
|
| 55 |
for (b = world; b != NULL; b = b->b_next) |
| 56 |
if (b == AskBuffer) |
| 57 |
return b; |
| 58 |
} |
| 59 |
AskBuffer = do_select((Window *)NULL, "*minibuf*"); |
| 60 |
AskBuffer->b_type = B_SCRATCH; |
| 61 |
return AskBuffer; |
| 62 |
} |
| 63 |
|
| 64 |
/* Add a string to the mini-buffer. */ |
| 65 |
|
| 66 |
void |
| 67 |
minib_add(char *str, jbool movedown) |
| 68 |
{ |
| 69 |
Buffer *saveb = curbuf; |
| 70 |
|
| 71 |
SetBuf(get_minibuf()); |
| 72 |
LineInsert(1); |
| 73 |
ins_str(str); |
| 74 |
if (movedown) |
| 75 |
CurAskPtr = curline; |
| 76 |
SetBuf(saveb); |
| 77 |
} |
| 78 |
|
| 79 |
jbool InRealAsk = NO; |
| 80 |
|
| 81 |
private const char * |
| 82 |
real_ask(const char *delim, jbool (*d_proc)(ZXchar), |
| 83 |
const char *def, const char *prompt) |
| 84 |
{ |
| 85 |
jmp_buf savejmp; |
| 86 |
ZXchar c; |
| 87 |
int prompt_len, |
| 88 |
prompt_off = 0, |
| 89 |
line_off = 0; |
| 90 |
const char *prompt_leftchar = "", |
| 91 |
*line_leftchar = ""; |
| 92 |
Buffer *saveb = curbuf; |
| 93 |
volatile jbool aborted = NO; |
| 94 |
jbool no_typed = NO; |
| 95 |
const data_obj *push_cmd = LastCmd; |
| 96 |
int saved_as, saved_ac; |
| 97 |
|
| 98 |
#ifdef MAC |
| 99 |
menus_off(); |
| 100 |
#endif |
| 101 |
if (InRealAsk) { |
| 102 |
complain((char *) NULL); |
| 103 |
/* NOTREACHED */ |
| 104 |
} |
| 105 |
save_arg(saved_as, saved_ac); |
| 106 |
push_env(savejmp); |
| 107 |
InRealAsk = YES; |
| 108 |
SetBuf(get_minibuf()); |
| 109 |
if (!inlist(AskBuffer->b_first, CurAskPtr)) |
| 110 |
CurAskPtr = curline; |
| 111 |
prompt_len = strlen(prompt); |
| 112 |
ToFirst(); /* Beginning of buffer. */ |
| 113 |
linebuf[0] = '\0'; |
| 114 |
modify(); |
| 115 |
makedirty(curline); |
| 116 |
|
| 117 |
if (setjmp(mainjmp)) { |
| 118 |
if (InJoverc) { /* this is a kludge */ |
| 119 |
aborted = YES; |
| 120 |
goto cleanup; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
this_cmd = OTHER_CMD; /* probably redundant */ |
| 125 |
for (;;) { |
| 126 |
int COMAX = CO - 3, |
| 127 |
nw; |
| 128 |
cmd_sync(); |
| 129 |
jdbg("prompt=\"%s\" plen=%d poff=%d cc=%d loff=%d linebuf=\"%s\"\n", prompt, prompt_len, prompt_off, curchar, line_off, linebuf); |
| 130 |
if (line_off > 0 && curchar < line_off) { |
| 131 |
/* might need to scroll right, just force recalc */ |
| 132 |
line_off = 0; |
| 133 |
line_leftchar = ""; |
| 134 |
prompt_off = 0; |
| 135 |
prompt_leftchar = ""; |
| 136 |
} |
| 137 |
#define PRWIDTH (prompt_len - prompt_off + SIWIDTH(prompt_off)) |
| 138 |
#define ASKWIDTH (PRWIDTH + curchar - line_off + SIWIDTH(line_off)) |
| 139 |
nw = ASKWIDTH; |
| 140 |
if (nw > COMAX) { |
| 141 |
int COMID = COMAX/2; |
| 142 |
if (prompt_len > COMID) { |
| 143 |
/* offset into prompt so it ends at center */ |
| 144 |
prompt_off = prompt_len - COMID + 1; |
| 145 |
prompt_leftchar = "!"; |
| 146 |
} else { |
| 147 |
prompt_off = 0; |
| 148 |
prompt_leftchar = ""; |
| 149 |
} |
| 150 |
nw = ASKWIDTH; |
| 151 |
if (nw > COMAX) { |
| 152 |
/* scroll buf left */ |
| 153 |
line_off = PRWIDTH + curchar - COMID - COMID/2; |
| 154 |
line_leftchar = "!"; |
| 155 |
} else { |
| 156 |
line_off = 0; |
| 157 |
line_leftchar = ""; |
| 158 |
} |
| 159 |
nw = ASKWIDTH; |
| 160 |
} |
| 161 |
jdbg("prompt=\"%s\" plen=%d poff=%d cc=%d loff=%d nw=%d linebuf=\"%s\"\n", prompt, prompt_len, prompt_off, curchar, line_off, nw, linebuf); |
| 162 |
if (!InJoverc) |
| 163 |
s_mess("%s%s%s%s", prompt_leftchar, &prompt[prompt_off], |
| 164 |
line_leftchar, &linebuf[line_off]); |
| 165 |
Asking = YES; |
| 166 |
AskingWidth = nw; |
| 167 |
c = getch(); |
| 168 |
if (c != '\0' && strchr(delim, c) != NULL) { |
| 169 |
if (d_proc == NULL_ASK_EXT || !(*d_proc)(c)) |
| 170 |
break; |
| 171 |
} else if (c == AbortChar) { |
| 172 |
message("[Aborted]"); |
| 173 |
aborted = YES; |
| 174 |
break; |
| 175 |
} else switch (c) { |
| 176 |
case CTL('N'): |
| 177 |
case CTL('P'): |
| 178 |
if (CurAskPtr != NULL) { |
| 179 |
long n = (c == CTL('P') ? -arg_value() : arg_value()); |
| 180 |
|
| 181 |
CurAskPtr = next_line(CurAskPtr, n); |
| 182 |
if (CurAskPtr == curbuf->b_first && CurAskPtr->l_next != NULL) |
| 183 |
CurAskPtr = CurAskPtr->l_next; |
| 184 |
(void) ltobuf(CurAskPtr, linebuf); |
| 185 |
modify(); |
| 186 |
makedirty(curline); |
| 187 |
Eol(); |
| 188 |
this_cmd = OTHER_CMD; |
| 189 |
} |
| 190 |
break; |
| 191 |
|
| 192 |
case CTL('R'): |
| 193 |
if (def != NULL) |
| 194 |
ins_str(def); |
| 195 |
else |
| 196 |
rbell(); |
| 197 |
break; |
| 198 |
|
| 199 |
default: |
| 200 |
dispatch(c); |
| 201 |
break; |
| 202 |
} |
| 203 |
if (curbuf != AskBuffer) |
| 204 |
SetBuf(AskBuffer); |
| 205 |
if (curline != curbuf->b_first) { |
| 206 |
CurAskPtr = curline; |
| 207 |
curline = curbuf->b_first; /* with whatever is in linebuf */ |
| 208 |
} |
| 209 |
} |
| 210 |
cleanup: |
| 211 |
pop_env(savejmp); |
| 212 |
|
| 213 |
LastCmd = push_cmd; |
| 214 |
restore_arg(saved_as, saved_ac); |
| 215 |
no_typed = (linebuf[0] == '\0'); |
| 216 |
strcpy(Minibuf, linebuf); |
| 217 |
SetBuf(saveb); |
| 218 |
InRealAsk = Asking = Interactive = NO; |
| 219 |
if (!aborted) { |
| 220 |
if (!PreEmptOutput()) { |
| 221 |
Placur(ILI, 0); |
| 222 |
flushscreen(); |
| 223 |
} |
| 224 |
if (no_typed) |
| 225 |
return NULL; |
| 226 |
} else { |
| 227 |
complain((char *)NULL); |
| 228 |
/* NOTREACHED */ |
| 229 |
} |
| 230 |
return Minibuf; |
| 231 |
} |
| 232 |
|
| 233 |
const char * |
| 234 |
ask(const char *def, const char *fmt, ...) |
| 235 |
{ |
| 236 |
char prompt[128]; |
| 237 |
const char *ans; |
| 238 |
va_list ap; |
| 239 |
|
| 240 |
va_start(ap, fmt); |
| 241 |
format(prompt, sizeof prompt, fmt, ap); |
| 242 |
va_end(ap); |
| 243 |
ans = real_ask("\r\n", NULL_ASK_EXT, def, prompt); |
| 244 |
if (ans == NULL) { /* Typed nothing. */ |
| 245 |
if (def == NULL) { |
| 246 |
complain("[No default]"); |
| 247 |
/* NOTREACHED */ |
| 248 |
} |
| 249 |
ans = def; |
| 250 |
} |
| 251 |
return ans; |
| 252 |
} |
| 253 |
|
| 254 |
const char * |
| 255 |
do_ask(const char *delim, jbool (*d_proc)(ZXchar), const char *def, const char *fmt, ...) |
| 256 |
{ |
| 257 |
char prompt[128]; |
| 258 |
va_list ap; |
| 259 |
|
| 260 |
va_start(ap, fmt); |
| 261 |
format(prompt, sizeof prompt, fmt, ap); |
| 262 |
va_end(ap); |
| 263 |
return real_ask(delim, d_proc, def, prompt); |
| 264 |
} |
| 265 |
|
| 266 |
jbool OneKeyConfirmation = NO; /* VAR: single y or n keystroke sufficient? */ |
| 267 |
|
| 268 |
jbool |
| 269 |
yes_or_no_p(const char *fmt, ...) |
| 270 |
{ |
| 271 |
char prompt[128]; |
| 272 |
va_list ap; |
| 273 |
|
| 274 |
va_start(ap, fmt); |
| 275 |
format(prompt, sizeof prompt, fmt, ap); |
| 276 |
va_end(ap); |
| 277 |
|
| 278 |
for (;;) { |
| 279 |
if (OneKeyConfirmation) { |
| 280 |
ZXchar c; |
| 281 |
|
| 282 |
message(prompt); |
| 283 |
Asking = YES; /* so redisplay works */ |
| 284 |
AskingWidth = strlen(prompt); |
| 285 |
c = getch(); |
| 286 |
Asking = NO; |
| 287 |
if (c == AbortChar) { |
| 288 |
complain("[Aborted]"); |
| 289 |
/* NOTREACHED */ |
| 290 |
} |
| 291 |
switch (CharUpcase(c)) { |
| 292 |
case 'Y': |
| 293 |
return YES; |
| 294 |
|
| 295 |
case 'N': |
| 296 |
return NO; |
| 297 |
|
| 298 |
default: |
| 299 |
add_mess("[Type Y or N]"); |
| 300 |
Asking = YES; /* so cursor sits on question */ |
| 301 |
SitFor(10); |
| 302 |
break; |
| 303 |
} |
| 304 |
} else { |
| 305 |
const char *ans = ask((char *) NULL, prompt); |
| 306 |
|
| 307 |
if (caseeqn(ans, "yes", strlen(ans))) |
| 308 |
return YES; |
| 309 |
|
| 310 |
if (caseeqn(ans, "no", strlen(ans))) |
| 311 |
return NO; |
| 312 |
|
| 313 |
rbell(); |
| 314 |
} |
| 315 |
} |
| 316 |
/* NOTREACHED */ |
| 317 |
} |
| 318 |
|
| 319 |
#ifdef F_COMPLETION |
| 320 |
|
| 321 |
/* look for any substrings of the form $foo in linebuf, and expand |
| 322 |
* them according to their value in the environment (if possible) - |
| 323 |
* this munges all over curchar and linebuf without giving it a second |
| 324 |
* thought (I must be getting lazy in my old age) |
| 325 |
*/ |
| 326 |
# ifndef MAC /* no environment in MacOS */ |
| 327 |
|
| 328 |
jbool DoEVexpand = YES; /* VAR: should we expand evironment variables? */ |
| 329 |
|
| 330 |
private void |
| 331 |
EVexpand(void) |
| 332 |
{ |
| 333 |
char c, |
| 334 |
*lp = linebuf, |
| 335 |
*ep, |
| 336 |
varname[128], |
| 337 |
*vp, |
| 338 |
*lp_start; |
| 339 |
Mark *m = MakeMark(curline, curchar); |
| 340 |
|
| 341 |
while ((c = *lp++) != '\0') { |
| 342 |
if (c == '$') { |
| 343 |
lp_start = lp - 1; /* the $ */ |
| 344 |
vp = varname; |
| 345 |
/* Pick up env. variable name ([a-zA-Z0-9_]*) */ |
| 346 |
while (('a' <= (c = *lp++) && c <= 'z') |
| 347 |
|| ('A' <= c && c <= 'Z') |
| 348 |
|| ('0' <= c && c <= '9') |
| 349 |
|| c == '_') |
| 350 |
*vp++ = c; |
| 351 |
*vp = '\0'; |
| 352 |
/* if we find an env. variable with the right |
| 353 |
* name, we insert it in linebuf, and then delete |
| 354 |
* the variable name that we're replacing - and |
| 355 |
* then we continue in case there are others ... |
| 356 |
*/ |
| 357 |
if ((ep = getenv(varname)) != NULL) { |
| 358 |
curchar = lp_start - linebuf; |
| 359 |
ins_str(ep); |
| 360 |
del_char(FORWARD, (int)strlen(varname) + 1, NO); |
| 361 |
lp = linebuf + curchar; |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
ToMark(m); |
| 366 |
DelMark(m); |
| 367 |
} |
| 368 |
|
| 369 |
# endif /* !MAC */ |
| 370 |
|
| 371 |
private char *fc_filebase; |
| 372 |
jbool DispBadFs = YES; /* VAR: display filenames with bad extensions? */ |
| 373 |
char BadExtensions[sizeof(BadExtensions)] = /* VAR: extensions to ignore */ |
| 374 |
|
| 375 |
# ifndef MSFILESYSTEM |
| 376 |
/* .o: object code format |
| 377 |
* .Z .gz: compressed formats |
| 378 |
* .tar .zip .zoo: archive formats |
| 379 |
* .gif .jpg .jpeg .mpg .mpeg .tif .tiff .rgb: graphics formats |
| 380 |
* .dvi: TeX or ditroff output |
| 381 |
* .elc: compiled GNU EMACS code |
| 382 |
*/ |
| 383 |
".o .Z .gz .tar .zip .zoo .gif .jpg .jpeg .mpg .mpeg .tif .tiff .rgb .dvi .elc"; |
| 384 |
# else /* MSFILESYSTEM */ |
| 385 |
/* .obj .lib .exe .com .dll: object code files |
| 386 |
* .arc .zip .zoo: archive formats |
| 387 |
* .bmp .gif .jpg .mpg .tif .pcx: graphics formats |
| 388 |
* .wks .wk1 .xls: spreadsheet formats |
| 389 |
*/ |
| 390 |
".obj .lib .exe .com .dll .arc .zip .zoo .bmp .gif .jpg .mpg .tif .pcx .wks .wk1 .xls"; |
| 391 |
# endif /* MSFILESYSTEM */ |
| 392 |
|
| 393 |
private jbool |
| 394 |
bad_extension(const char *name) |
| 395 |
{ |
| 396 |
char *ip, |
| 397 |
*bads; |
| 398 |
size_t namelen = strlen(name), |
| 399 |
ext_len; |
| 400 |
|
| 401 |
# if defined(UNIX) || defined(MSFILESYSTEM) |
| 402 |
# ifdef DIRECTORY_ADD_SLASH |
| 403 |
if (strcmp(name, "./")==0 || strcmp(name, "../")==0) |
| 404 |
return YES; |
| 405 |
# else |
| 406 |
if (strcmp(name, ".")==0 || strcmp(name, "..")==0) |
| 407 |
return YES; |
| 408 |
# endif /* DIRECTORY_ADD_SLASH */ |
| 409 |
# endif /* UNIX || MSFILESYSTEM */ |
| 410 |
for (ip=bads=BadExtensions; *ip!='\0'; bads = ip+1) { |
| 411 |
if ((ip = strchr(bads, ' ')) == NULL) |
| 412 |
ip = bads + strlen(bads); |
| 413 |
ext_len = ip - bads; |
| 414 |
if (ext_len != 0 && ext_len < namelen |
| 415 |
# ifdef FILENAME_CASEINSENSITIVE |
| 416 |
&& caseeqn(&name[namelen - ext_len], bads, ext_len)) |
| 417 |
# else |
| 418 |
&& strncmp(&name[namelen - ext_len], bads, ext_len) == 0) |
| 419 |
# endif |
| 420 |
return YES; |
| 421 |
} |
| 422 |
return NO; |
| 423 |
} |
| 424 |
|
| 425 |
private jbool |
| 426 |
f_match(const char *file) |
| 427 |
{ |
| 428 |
size_t len = strlen(fc_filebase); |
| 429 |
|
| 430 |
if (!DispBadFs && bad_extension(file)) |
| 431 |
return NO; |
| 432 |
|
| 433 |
return len == 0 |
| 434 |
# ifdef FILENAME_CASEINSENSITIVE |
| 435 |
|| caseeqn(file, fc_filebase, len); |
| 436 |
# else |
| 437 |
|| strncmp(file, fc_filebase, len) == 0; |
| 438 |
# endif |
| 439 |
} |
| 440 |
|
| 441 |
# ifndef DIRECTORY_ADD_SLASH |
| 442 |
private jbool |
| 443 |
isdir(const char *name) |
| 444 |
{ |
| 445 |
(void) do_stat(name, (Buffer *) NULL, DS_DIR); |
| 446 |
return was_dir; |
| 447 |
} |
| 448 |
# endif /* !DIRECTORY_ADD_SLASH */ |
| 449 |
|
| 450 |
private void |
| 451 |
fill_in(char **dir_vec, int n) |
| 452 |
{ |
| 453 |
jbool filter; |
| 454 |
|
| 455 |
for (filter = DispBadFs; ; filter = NO) { |
| 456 |
int |
| 457 |
minmatch = 0, |
| 458 |
numfound = 0, |
| 459 |
lastmatch = -1, |
| 460 |
i; |
| 461 |
|
| 462 |
for (i = 0; i < n; i++) { |
| 463 |
/* If filter is NO, we accept all entries |
| 464 |
* (either DispBadFs is NO, in which case filtering |
| 465 |
* was done when dir_vec was built, or DispBadFs |
| 466 |
* is YES, but we are trying again, after finding |
| 467 |
* no files passed the filter. |
| 468 |
*/ |
| 469 |
if (!filter || !bad_extension(dir_vec[i])) { |
| 470 |
minmatch = numfound == 0 |
| 471 |
? (int)strlen(dir_vec[i]) |
| 472 |
#ifdef FILENAME_CASEINSENSITIVE |
| 473 |
: jmin(minmatch, numcompcase(dir_vec[lastmatch], dir_vec[i])); |
| 474 |
#else |
| 475 |
: jmin(minmatch, numcomp(dir_vec[lastmatch], dir_vec[i])); |
| 476 |
#endif |
| 477 |
lastmatch = i; |
| 478 |
numfound += 1; |
| 479 |
} |
| 480 |
} |
| 481 |
if (numfound == 0) { |
| 482 |
if (!filter) { |
| 483 |
/* hopeless: complain and give up */ |
| 484 |
rbell(); |
| 485 |
break; |
| 486 |
} |
| 487 |
} else { |
| 488 |
jbool the_same = YES; /* After filling in, are we the same |
| 489 |
* as when we were called? |
| 490 |
*/ |
| 491 |
|
| 492 |
if (minmatch > (int)strlen(fc_filebase)) { |
| 493 |
if (minmatch >= LBSIZE - (fc_filebase - linebuf)) { |
| 494 |
len_error(JMP_COMPLAIN); |
| 495 |
/* NOTREACHED */ |
| 496 |
} |
| 497 |
the_same = NO; |
| 498 |
null_ncpy(fc_filebase, dir_vec[lastmatch], (size_t) minmatch); |
| 499 |
modify(); |
| 500 |
makedirty(curline); |
| 501 |
} |
| 502 |
Eol(); |
| 503 |
# ifndef DIRECTORY_ADD_SLASH |
| 504 |
if (numfound == 1 && fc_filebase[0] != '\0' && isdir(linebuf)) { |
| 505 |
the_same = NO; |
| 506 |
insert_c('/', 1); |
| 507 |
} |
| 508 |
# endif |
| 509 |
if (the_same) { |
| 510 |
add_mess(" [%s%s]", |
| 511 |
!filter && DispBadFs? "!" : NullStr, |
| 512 |
numfound == 1? "Unique" : "Ambiguous"); |
| 513 |
SitFor(7); |
| 514 |
} |
| 515 |
break; /* we're done */ |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
/* called from do_ask() when one of "\r\n ?" is typed. Does the right |
| 521 |
* thing, depending on which. |
| 522 |
*/ |
| 523 |
private jbool |
| 524 |
f_complete(ZXchar c) |
| 525 |
{ |
| 526 |
char |
| 527 |
dir[FILESIZE], |
| 528 |
**dir_vec; |
| 529 |
int |
| 530 |
nentries; |
| 531 |
|
| 532 |
# ifndef MAC /* no environment in MacOS */ |
| 533 |
if (DoEVexpand) |
| 534 |
EVexpand(); |
| 535 |
# endif |
| 536 |
if (c == CR || c == LF) |
| 537 |
return NO; /* tells ask to return now */ |
| 538 |
|
| 539 |
fc_filebase = strrchr(linebuf, '/'); |
| 540 |
# ifdef MSFILESYSTEM |
| 541 |
{ |
| 542 |
char *p = strrchr(fc_filebase == NULL? linebuf : fc_filebase, '\\'); |
| 543 |
|
| 544 |
if (p != NULL) |
| 545 |
fc_filebase = p; |
| 546 |
if (fc_filebase == NULL) |
| 547 |
fc_filebase = strrchr(linebuf, ':'); |
| 548 |
} |
| 549 |
# endif /* MSFILESYSTEM */ |
| 550 |
if (fc_filebase != NULL) { |
| 551 |
char tmp[FILESIZE]; |
| 552 |
|
| 553 |
fc_filebase += 1; |
| 554 |
if ((size_t) (fc_filebase - linebuf) >= sizeof(tmp)) { |
| 555 |
len_error(JMP_COMPLAIN); |
| 556 |
/* NOTREACHED */ |
| 557 |
} |
| 558 |
null_ncpy(tmp, linebuf, (size_t) (fc_filebase - linebuf)); |
| 559 |
if (tmp[0] == '\0') |
| 560 |
strcpy(tmp, "/"); |
| 561 |
PathParse(tmp, dir); |
| 562 |
} else { |
| 563 |
fc_filebase = linebuf; |
| 564 |
strcpy(dir, "."); |
| 565 |
} |
| 566 |
if ((nentries = jscandir(dir, &dir_vec, f_match, fnamecomp)) == -1) { |
| 567 |
add_mess(" [Unknown directory: %s]", dir); |
| 568 |
SitFor(7); |
| 569 |
return YES; |
| 570 |
} |
| 571 |
if (nentries == 0) { |
| 572 |
add_mess(" [No match]"); |
| 573 |
SitFor(7); |
| 574 |
} else if (jiswhite(c)) { |
| 575 |
fill_in(dir_vec, nentries); |
| 576 |
} else { |
| 577 |
/* we're a '?' */ |
| 578 |
const int |
| 579 |
towidth = jmin((CO - 2), MAX_TYPEOUT); |
| 580 |
int |
| 581 |
i, |
| 582 |
maxlen = 0, |
| 583 |
ncols, |
| 584 |
line, |
| 585 |
entriespercol; |
| 586 |
|
| 587 |
TOstart("Completion"); |
| 588 |
Typeout("(! means file will not be chosen unless typed explicitly)"); |
| 589 |
Typeout("Possible completions (in %s):", dir); |
| 590 |
|
| 591 |
for (i = 0; i < nentries; i++) |
| 592 |
maxlen = jmax((int)strlen(dir_vec[i]), maxlen); |
| 593 |
maxlen += 4; /* pad each column with at least 4 spaces */ |
| 594 |
if (maxlen > towidth) |
| 595 |
maxlen = towidth; |
| 596 |
ncols = towidth / maxlen; |
| 597 |
entriespercol = (nentries + ncols - 1) / ncols; |
| 598 |
|
| 599 |
for (line = 0; line < entriespercol; line++) { |
| 600 |
int |
| 601 |
col, |
| 602 |
which = line, |
| 603 |
bufcol = 0; |
| 604 |
char buf[MAX_TYPEOUT + 1]; |
| 605 |
|
| 606 |
for (col = 0; col < ncols; col++) { |
| 607 |
if (which < nentries) { |
| 608 |
jbool isbad = DispBadFs && bad_extension(dir_vec[which]); |
| 609 |
|
| 610 |
swritef(buf+bufcol, sizeof(buf) - bufcol, "%s%-*s", |
| 611 |
isbad ? "!" : NullStr, |
| 612 |
maxlen - isbad, dir_vec[which]); |
| 613 |
} |
| 614 |
which += entriespercol; |
| 615 |
bufcol += maxlen; |
| 616 |
} |
| 617 |
Typeout("%s", buf); |
| 618 |
} |
| 619 |
TOstop(); |
| 620 |
} |
| 621 |
freedir(&dir_vec, nentries); |
| 622 |
return YES; |
| 623 |
} |
| 624 |
#endif /* F_COMPLETION */ |
| 625 |
|
| 626 |
/* ask for file or directory |
| 627 |
* |
| 628 |
* These are only different under MSFILESYSTEM. |
| 629 |
* Note: def and buf may be equal. |
| 630 |
*/ |
| 631 |
|
| 632 |
#ifdef MSFILESYSTEM |
| 633 |
private |
| 634 |
#endif |
| 635 |
char * |
| 636 |
ask_ford(const char *prmt, char *def, char *buf) |
| 637 |
{ |
| 638 |
/* Note: pr_name yields a pointer into its static buffer so |
| 639 |
* pretty_name will not be a pointer into def. This allows |
| 640 |
* us to accept *def and *buf being aliases. Otherwise we |
| 641 |
* could end up calling PathParse with aliases. (Yes, you |
| 642 |
* are looking at a battle scar.) |
| 643 |
*/ |
| 644 |
char |
| 645 |
*pretty_name = pr_name(def, YES), |
| 646 |
prompt[128]; |
| 647 |
const char |
| 648 |
*ans; |
| 649 |
|
| 650 |
if (prmt == NULL) |
| 651 |
swritef(prompt, sizeof(prompt), ProcFmt); |
| 652 |
else |
| 653 |
jamstr(prompt, prmt); |
| 654 |
if (DispDefFs && def != NULL && *def != '\0') { |
| 655 |
size_t pl = strlen(prompt); |
| 656 |
|
| 657 |
swritef(prompt + pl, sizeof(prompt)-pl, "(default %s) ", pretty_name); |
| 658 |
if ((int)strlen(prompt) * 2 >= CO) |
| 659 |
prompt[pl] = '\0'; |
| 660 |
} |
| 661 |
#ifdef F_COMPLETION |
| 662 |
ans = real_ask("\r\n \t?", f_complete, pretty_name, prompt); |
| 663 |
/* note: *pretty_name may have been overwritten -- we can't use it */ |
| 664 |
if (ans == NULL && (ans = pr_name(def, YES)) == NULL) { |
| 665 |
complain("[No default file name]"); |
| 666 |
/* NOTREACHED */ |
| 667 |
} |
| 668 |
#else |
| 669 |
ans = ask(pretty_name, prompt); |
| 670 |
#endif |
| 671 |
PathParse(ans, buf); |
| 672 |
|
| 673 |
return buf; |
| 674 |
} |
| 675 |
|
| 676 |
#ifdef MSFILESYSTEM |
| 677 |
char * |
| 678 |
ask_file(prmt, def, buf) |
| 679 |
const char *prmt; |
| 680 |
char |
| 681 |
*def, |
| 682 |
*buf; |
| 683 |
{ |
| 684 |
MatchDir = NO; |
| 685 |
return ask_ford(prmt, def, buf); |
| 686 |
} |
| 687 |
|
| 688 |
char * |
| 689 |
ask_dir(prmt, def, buf) |
| 690 |
const char *prmt; |
| 691 |
char |
| 692 |
*def, |
| 693 |
*buf; |
| 694 |
{ |
| 695 |
MatchDir = YES; |
| 696 |
return ask_ford(prmt, def, buf); |
| 697 |
} |
| 698 |
#endif |