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 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
|
/*****************************************************************************
NAME
browse.c -- curses(3)-using display front end for vh database browser
SYNOPSIS
main(argc, argv) --- browser main sequence
int argc; char *argv[][];
DESCRIPTION
This module defines the main and browse loop for the vh browser;
all user-interface decisions are made here. Screen, keyboard and
mouse handling support may be found in screen.c.
AUTHORS
Written by Eric S. Raymond <eric@snark.thyrsus.com> for a UNIX
port of the 1.1 version of Raymond Gardner's MS-DOS browser, October
1991. Please see the source distribution's READ.ME for license
terms.
*****************************************************************************/
/*LINTLIBRARY*/
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <assert.h>
#ifdef ATT
#include <termio.h>
#endif /* ATT */
#include "screen.h"
#include "vh.h"
#ifndef CONST
#define const
#endif /* CONST */
#ifdef MSDOS
/* emulate Borland-style scrollbar */
#define SBARCH ACS_CKBOARD /* scrollbar character */
#define SBARTOPCH 30 /* scrollbar up arrow */
#define SBARBOTCH 31 /* scrollbar down arrow */
#else
/* approximate X-style scrollbar */
#define SBARCH ACS_VLINE /* scrollbar character */
#define SBARTOPCH ACS_TTEE /* scrollbar up arrow */
#define SBARBOTCH ACS_BTEE /* scrollbar down arrow */
#endif
#define SBARTHUMBCH ACS_BULLET /* scrollbar thumb character */
#ifdef A_COLOR
#define NORMPAIR 1
#define HILITEPAIR 2
#define SELPAIR 3
#define PROMPTPAIR 4
#define FKEYPAIR 5
#define SBARPAIR 6
#define SBARENDPAIR 7
#define SBTHUMBPAIR 8
#endif /* A_COLOR */
#define SUPPRESS 0
#define OPTIONAL 1
#define FORCED 2
#ifndef UNIX
#define erasechar() BS
#endif /* UNIX */
#define CUTFILE "%s.cut"
/*
* When paging forward or back, it helps the reader to page by a few less
* lines than the page depth, so a few of the old ones show to give context.
* This controls how many old lines will show.
*/
#define OVERLAP 4
/* dialogue/message window parameters */
#define DTOP 10
#define DLEFT 10
#define DHEIGHT 7
#define DWIDTH 42
/*******************************************************************
*
* All message texts declared here for internationalization purposes
*
******************************************************************/
#ifdef MSDOS
#define USAGE "usage: jargon [-cigms] [-b key] [srcname] [indexname]\n\
-c --- check textfile/index pair for consistency\n\
-i --- enable incremental-lookup and search\n\
-g --- generate new index from textfile\n\
-m --- force monochrome operation\n\
-s --- compensate for snowy EGA monitor\n\
-b --- write single entry specified by following key to stdout\n\
-r --- display random entry\n"
#define TBANNER "TAB-Index F1-help F10-exit"
#define IBANNER "TAB-Text F1-help F10-exit"
#else
#define USAGE "usage: jargon [-cigmr] [-b key] [srcname] [indexname]\n"
#define TBANNER "TAB-Index ^Q-help ^C-exit"
#define IBANNER "TAB-Text ^Q-help ^C-exit"
#endif
typedef struct
{
int left; /* left boundary of pushbutton */
int right; /* right boundary of pushbutton */
char cmd; /* command character associated with pushbutton */
}
strip;
#ifdef MOUSE
static strip panel[] = {{0, 8, TAB}, {11, 17, CTL('Q')}, {21, 27, CTL('C')}};
#endif
/*
* Note: I had to descrement this by one to get around a wraparound bug in
* ncurses. Doesn't seems to have had any visible effect on the SVr4
* version, but...
*/
#define PANELSTART (COLS - 1 - strlen((cf==&vhi) ? IBANNER : TBANNER))
#define NOREFS "No references on this page"
#define NOSRCH "No previous search"
#define CUTNOP "Can't open cut file"
#define CUTNOW "Cutting to jargon.cut..."
#define CUTDNE "Cutting to jargon.cut...Done"
#define BRSMES "Browsing %s...\n"
#define IDXMES "Generating index for %s...\n"
#define CHKMES "Performing consistency check for %s...\n"
/*
* These have to be declared static, not macros; we use the address of the
* current prompt as a mode indicator.
*/
const static char NOMESG[] = " ";
#ifndef POPUP
/* we want these fixed-length so the entry line looks good */
const static char SEARCH[] = "String search for: ";
const static char INSRCH[] = "Search proceeding: ";
const static char NOTFND[] = "Search failed: ";
const static char LOOKUP[] = "Looking up entry: ";
const static char LOOKING[] = "Lookup in progress:";
#else
/* the popup windows will get sized to these */
const static char SEARCH[] = "String search for";
const static char INSRCH[] = "Search proceeding";
const static char NOTFND[] = "Search failed";
const static char LOOKUP[] = "Looking up entry";
const static char LOOKING[] = "Lookup in progress";
const static char BDLOOK[] = "Lookup failed";
#endif /* POPUP */
const static char *help_screen[] =
{
" Jargon File Browser v. 1.6",
" Copyright 1991 by Raymond D. Gardner & Eric S. Raymond",
" All Rights Reserved; Free Redistribution Encouraged",
"",
" ^L redraw screen (Refresh)",
"",
" space page forward (PgDn)",
" ^U page back (PgUp)",
" ^N / ^P scroll forward / back (Down/Up Arrow)",
" ^A / ^O go to top / end of file (Home/End)",
"",
" ^J or ^M chase highlighted reference (Reference)",
" ^F / ^B next/previous reference (Right / Left Arrow)",
"",
" ^E lookup entry by name (Select)",
" ^S search for string (Find)",
" ^R repeat previous search (Shift-Find)",
#ifdef UNIX /* keypad() seems to disable recognition of ESC */
" ^H undo last search or lookup (Undo)",
#else
" ^H or ESC undo last search or lookup (Undo)",
#endif
"",
" ^I toggle between word list and text (Tab)",
" ^Y append selected entry to cut file (Print)",
"",
" Typing an entry name followed by Enter works like an ^E command.",
" Press any key to return to the browser.",
NULL,
};
/*******************************************************************
*
* Shared global data
*
******************************************************************/
static char *execname; /* name under which program was invoked */
static FILEINFO *cf; /* pointer to current fileinfo block */
/* highlights */
static chtype normattr, hiliteattr, selattr, promptattr, fkeyattr, sbarattr;
static chtype sbarendattr, sbthumbattr;
static char lasthit[LNSZ + 1]; /* last key searched for */
static char linbuf[LNSZ + 1]; /* line input scratch buffer */
#ifdef POPUP
static WINDOW *dwin; /* dialogue & message window */
#endif /* POPUP */
static bool incrsearch; /* do incremental search? */
/*******************************************************************
*
* Message and dialogue code
*
******************************************************************/
static void waitforit(requeue)
/* wait for keystroke or mouse click */
bool requeue;
{
int c;
#ifdef MOUSE
int hsy, hsx;
/* wait for keypress or button click */
while ((c = egetch()) == KEY_MOUSE)
if (A_BUTTON_CHANGED & mouse_status(&hsy, &hsx))
break;
#else
c = egetch();
#endif /* MOUSE */
if (requeue)
ungetch(c);
}
#if defined(POPUP) && defined(CURSES)
static void draw_box(win, ty,tx, by,bx)
/* draw a box on the screen using best possible forms chars */
WINDOW *win;
int ty, tx, by, bx;
{
int j;
mvwaddch(win, ty, tx, ACS_ULCORNER);
mvwaddch(win, by, bx, ACS_LRCORNER);
mvwaddch(win, by, tx, ACS_LLCORNER);
mvwaddch(win, ty, bx, ACS_URCORNER);
wmove(win, ty, tx+1);
for (j = tx + 1; j <= bx - 1; j++)
waddch(win, ACS_HLINE);
wmove(win, by, tx+1);
for (j = tx + 1; j <= bx - 1; j++)
waddch(win, ACS_HLINE);
for (j = ty + 1; j <= by - 1; j++)
{
wmove(win, j, tx);
waddch(win, ACS_VLINE);
}
for (j = ty + 1; j <= by - 1; j++)
{
wmove(win, j, bx);
waddch(win, ACS_VLINE);
}
wrefresh(win);
}
static char *get_dialogue(prompt)
/* get string from user, with prompts */
char *prompt;
{
static char buf[MAXCOLS + 1];
if (prompt == (char *)NULL)
{
delwin(dwin);
dwin = (WINDOW *)NULL;
}
else
{
/* create a dialogue window */
if (dwin == (WINDOW *)NULL)
dwin = newwin(DHEIGHT, DWIDTH, DTOP, DLEFT);
wclear(dwin);
draw_box(dwin, 0, 0, DHEIGHT-1, DWIDTH-1);
draw_box(dwin, 2, 1, DHEIGHT-3, DWIDTH-2);
wattron(dwin, A_BOLD);
mvwaddstr(dwin, 1, 10, prompt);
wattroff(dwin, A_BOLD);
echo();
mvwgetstr(dwin, 3, 3, buf);
noecho();
return(buf);
}
}
#endif /* defined(POPUP) && defined(CURSES) */
static void message(s)
/* write message to prompt line */
char *s;
{
#ifndef POPUP
attrset(promptattr);
mvaddstr(LINES-1, 0, s);
attrset(normattr);
refresh();
#else
WINDOW *mwin;
if (dwin)
{
mvwaddstr(dwin, 5, 10, NOMESG);
wattron(dwin, A_BOLD);
mvwaddstr(dwin, 5, 10, s);
wattroff(dwin, A_BOLD);
wrefresh(dwin);
}
else
{
mwin = newwin(3, strlen(s) + 2, DTOP, DLEFT);
wclear(mwin);
draw_box(mwin, 0, 0, 2, strlen(s) + 1);
wattron(mwin, A_BOLD);
mvwaddstr(mwin, 1, 1, s);
wattroff(mwin, A_BOLD);
wrefresh(mwin);
waitforit(TRUE);
overwrite(stdscr, mwin);
delwin(mwin);
touchline(DTOP, stdscr);
wrefresh(stdscr);
}
#endif /* POPUP */
}
/*******************************************************************
*
* The browser itself
*
******************************************************************/
static void uninitbrowse(doclear)
/* end the browse, either normally or due to signal */
bool doclear;
{
mouse_hide();
if (doclear)
{
clear();
refresh();
}
#ifdef CURSES
/* weird sex with the tty driver */
(void)resetterm();
(void)echo();
#ifndef OLDCURSES
(void)flushinp();
#endif /* OLDCURSES */
#endif /* CURSES */
(void)endwin();
exit(0);
}
static void paintselect(rp, attr)
/* turn highlight on a selection on or off */
region *rp; /* which to highlight */
chtype attr; /* attribute to use */
{
int x, y;
attrset(attr);
if (rp->yl == rp->yr)
{
move(rp->yl, rp->xl);
for (x = rp->xl; x <= rp->xr; x++)
addch(inch() & A_CHARTEXT);
}
else
{
int /*c,*/ lastnsp;
/* don't highlight trailing spaces on line-wrapped references */
for (lastnsp = COLS - 1; lastnsp > 0; lastnsp--)
{
move(rp->yl, lastnsp);
if ((inch() & A_CHARTEXT) != ' ')
break;
}
/* write trailing-line part of highlight */
move(rp->yl, rp->xl);
for (x = rp->xl; x <= lastnsp; x++)
addch(inch() & A_CHARTEXT);
/* highlight everything *between* 1st and last lines in region */
for (y = rp->yl + 1; y < rp->yr; y++)
{
move(y, x);
for (x = 0; x < COLS - 1; x++)
addch(inch() & A_CHARTEXT);
}
/* don't highlight leading spaces on line-wrapped references */
for (x = 0; x <= COLS - 1; x++)
{
move(rp->yr, x);
if ((inch() & A_CHARTEXT) != ' ')
break;
}
/* write leading-line part of highlight */
move(rp->yr, x);
for (; x <= rp->xr; x++)
addch(inch() & A_CHARTEXT);
}
attrset(normattr);
}
static bool chase(x, y, isindex)
/* try to chase a link at this location */
int x, y;
bool isindex;
{
long pos; /* find target */
int dummy;
enqueue(cf); /* make sure we can un-chase it */
pos = iflink(x, y, &dummy, &dummy, isindex);
if (pos <= 0)
{
dequeue(cf);
return(FALSE);
}
else
{
if (isindex) /* if index, switch to text */
{
cf = vht;
cf->dsptoppos = cf->dspnextpos = NOWHERE;
}
cf->toppos = pos; /* set for new display */
return(TRUE);
}
}
static bool search(str, c)
/* search for given string in file */
char *str;
{
int i;
message(INSRCH);
enqueue(cf);
if (c)
cf->hitpos = ifind(cf->fp, cf->dspnextpos, c);
else
cf->hitpos = ffind(cf->fp, cf->dspnextpos, str);
if (cf->hitpos == NOWHERE)
{
message(NOTFND);
dequeue(cf);
return(FALSE);
}
else
{
/* else display hit on fourth line */
cf->toppos = cf->hitpos;
for (i = 0; i < OVERLAP; i++)
cf->toppos = getprevln(cf->fp, cf->toppos, linbuf);
if (str != lasthit)
(void) strcpy(lasthit, str);
return(TRUE);
}
}
static bool lookup(str, c)
/* search for given entry in file */
char *str, c;
{
long pos; /* find target */
enqueue(cf);
if (c)
pos = ilocate(c);
else
{
message(LOOKING);
pos = xlocate(str);
}
if (cf == &vhi) /* if index, switch to text */
{
cf = vht;
cf->dsptoppos = cf->dspnextpos = cf->endpos;
}
if (pos < 0)
{
cf->toppos = -pos; /* set for new display */
return(FALSE);
}
else
{
cf->toppos = pos; /* set for new display */
return(TRUE);
}
}
static daddr_t byname(prompt, str, c)
/* search or lookup given entry in file, depending on prompt value */
char *prompt, *str, c;
{
if (prompt == SEARCH)
return(search(str, c));
else
return(lookup(str, c));
}
/* entry buffer for lookup and search commands */
static char entrybuf[LNSZ + 1];
static int entrycnt = 0;
static void enterchar(c, prompt, row, col, maxcol)
/* accept a character into the entry buffer */
char c;
char *prompt;
int row, col;
int maxcol;
{
int es;
if (c == '\0')
{
entrybuf[entrycnt = 0] = '\0';
return;
}
es = col + strlen(prompt);
attrset(promptattr);
if (c == erasechar())
{
if (entrycnt > 0)
{
mvaddch(row, es + --entrycnt, ' ');
entrybuf[entrycnt] = '\0';
move(row, es + entrycnt);
ilocate(BS);
if (incrsearch)
dequeue(cf);
}
}
#ifdef CURSES
else if (c == killchar())
{
/* delete everything back to the end of the prompt */
while (entrycnt > 0)
{
mvaddch(row, es + --entrycnt, ' ');
entrybuf[entrycnt] = '\0';
move(row, es + entrycnt);
if (incrsearch)
dequeue(cf);
}
ilocate(DEL);
}
#endif /* CURSES */
else if (isprint(c))
{
mvaddstr(row, col, prompt);
if (es + entrycnt < maxcol
&& (!incrsearch || byname(prompt, (char *)NULL, c) != NOWHERE))
{
mvaddch(row, es + entrycnt, c);
entrybuf[entrycnt++] = c;
entrybuf[entrycnt] = '\0';
}
else if (c == '\0')
entrybuf[entrycnt = 0] = '\0';
else
beep();
}
attrset(normattr);
refresh();
}
static void cutentry(pos, fp)
/* send the text of the entry starting at pos to the gin file pointer */
daddr_t pos;
FILE *fp;
{
(void) fseek(vht->fp, pos, SEEK_SET);
/* always want first line */
(void) fgets(linbuf, LNSZ, vht->fp);
(void) fputs(linbuf, fp);
/* copy succeeding lines till we get to next entry */
while (fgets(linbuf, LNSZ, vht->fp) != (char *)NULL)
if (linbuf[0] == '=' || headword(linbuf))
break;
else
(void) fputs(linbuf, fp);
}
static char *nblanks(n)
/* blank-string return for fast fills; return up to 132 = MAXCOLS */
int n;
{
/* MAXCOLS+1 blanks */
const static char blanks[] = " ";
return(&blanks[sizeof(blanks) - 1 - n]);
}
static void browse(name, cok)
/* interactive browser loop */
char *name;
bool cok;
{
int i, c;
char **hp;
bool restore_select = FALSE;
FILE *cutfp;
int lightup = FORCED;
#ifdef MOUSE
int hsy, hsx, mstat; /* mouse hot spot coordinates */
bool thumbdrag = FALSE; /* are we in a thumbdrag? */
#endif /* MOUSE */
#ifndef POPUP
char *prompt = NOMESG;
#else
char *cp;
#endif /* POPUP */
#ifdef ATT
struct termio tty;
int saveintr;
#endif /* ATT */
if (!initbrowse(name))
exit(1);
cf = vht;
#ifdef UNIX
(void) signal(SIGINT,uninitbrowse);
(void) signal(SIGQUIT,uninitbrowse);
(void) signal(SIGTERM,uninitbrowse);
(void) signal(SIGIOT,uninitbrowse); /* for assert(3) */
#ifdef ATT
(void) ioctl(0, TCGETA, &tty);
saveintr = tty.c_cc[VINTR];
#endif /* ATT */
#endif /* UNIX */
(void) initscr();
setlastpage();
#ifdef A_COLOR
if ((cok = (cok && has_colors())))
{
start_color();
init_pair(NORMPAIR, COLOR_WHITE, COLOR_BLUE);
init_pair(HILITEPAIR, COLOR_RED, COLOR_WHITE);
init_pair(SELPAIR, COLOR_YELLOW, COLOR_BLUE);
init_pair(PROMPTPAIR, COLOR_BLACK, COLOR_CYAN);
init_pair(FKEYPAIR, COLOR_RED, COLOR_CYAN);
init_pair(SBARPAIR, COLOR_WHITE, COLOR_BLACK);
init_pair(SBARENDPAIR, COLOR_BLUE, COLOR_WHITE);
init_pair(SBTHUMBPAIR, COLOR_BLUE, COLOR_BLACK);
}
mouse_color(cok);
normattr = cok ? (COLOR_PAIR(NORMPAIR) | A_BOLD) : A_NORMAL;
hiliteattr = cok ? COLOR_PAIR(HILITEPAIR) : A_REVERSE;
selattr = cok ? (COLOR_PAIR(SELPAIR) | A_BOLD) : A_BOLD;
promptattr = cok ? COLOR_PAIR(PROMPTPAIR) : A_REVERSE;
fkeyattr = cok ? COLOR_PAIR(FKEYPAIR) : A_BOLD;
#ifndef MSDOS
sbarendattr = sbthumbattr =
sbarattr = cok ? COLOR_PAIR(SBARPAIR) : A_NORMAL;
#else
sbarattr = cok ? COLOR_PAIR(SBARPAIR) : A_REVERSE;
sbarendattr = cok ? (COLOR_PAIR(SBARENDPAIR) | A_BOLD) : A_REVERSE;
sbthumbattr = cok ? (COLOR_PAIR(SBTHUMBPAIR) | A_BOLD) : A_NORMAL;
#endif
#else
normattr = A_NORMAL;
hiliteattr = A_REVERSE;
selattr = A_BOLD;
promptattr = A_REVERSE;
fkeyattr = A_BOLD;
sbarendattr = sbthumbattr =
sbarattr = A_NORMAL;
#ifdef MSDOS
sbarattr = A_REVERSE;
sbarendattr = A_REVERSE;
sbthumbattr = A_NORMAL;
#endif /* MSDOS */
#endif /* A_COLOR */
mouse_init();
#ifdef MOUSE
mouse_move(LINES - 1, COLS - 1);
#endif /* MOUSE */
#ifdef CURSES
(void)saveterm();
(void)nonl();
(void)raw();
(void)noecho();
#ifndef OLDCURSES
(void)keypad(stdscr, TRUE);
#endif /* OLDCURSES */
#endif /* CURSES */
#ifdef ATT
(void) ioctl(0, TCGETA, &tty);
tty.c_cc[VINTR] = saveintr;
tty.c_iflag |= BRKINT;
tty.c_iflag &=~ IGNBRK;
tty.c_lflag |= ISIG;
(void) ioctl(0, TCSETA, &tty);
#endif /* ATT */
lasthit[0] = '\0';
for (c = KEY_REFRESH; c != CTL('C') && c != KEY_EXIT; c = egetch())
{
#ifdef DEBUG
if (isprint(c))
(void) fprintf(stderr, "command: %c\n", c);
else if (iscntrl(c))
(void) fprintf(stderr, "command: ^%c\n", c + '@');
else if (c >= 0x80 & c <= 0x9f)
(void) fprintf(stderr, "command: M-^%c\n", (c &~ 0x80) + '@');
else if (c >= 0x80)
(void) fprintf(stderr, "command: M-%c\n", c &~ 0x80);
else
(void) fprintf(stderr, "command: 0x%02x\n", c);
#endif /* DEBUG */
switch(c)
{
#ifdef MSDOS
case KEY_F(10):
(void) ungetch(KEY_EXIT);
break;
#endif /* MSDOS */
#ifndef OLDCURSES
case KEY_HELP: /* go to help screen */
#endif /* OLDCURSES */
case CTL('Q'):
#ifdef MSDOS
case KEY_F(1):
#endif /* MSDOS */
restore_select = TRUE;
i = 0;
for (hp = help_screen; *hp; hp++)
{
mvaddstr(i++, 0, *hp);
addstr(nblanks(COLS - strlen(*hp)));
}
refresh();
cf->dsptoppos = NOWHERE;
waitforit(FALSE);
/* FALL THROUGH */
#ifndef OLDCURSES
case KEY_REFRESH: /* redraw screen */
#endif /* OLDCURSES */
case CTL('L'):
restore_select = TRUE;
#ifdef CURSES
clearok(stdscr, TRUE);
#endif /* CURSES */
break;
case SP:
restore_select = FALSE;
#ifndef POPUP
if (prompt != NOMESG)
goto medialspace;
/* FALL THROUGH */
#endif /* POPUP */
#ifndef OLDCURSES
case KEY_NPAGE: /* page forward */
#endif /* OLDCURSES */
#ifdef MOUSE
pagefwd:
#endif
enqueue(cf);
restore_select = FALSE;
for (i = 0; i < LINES - OVERLAP; i++)
{
if (cf->toppos == cf->lastpagetoppos)
break;
cf->toppos = getnextln(cf->fp, cf->toppos, linbuf);
}
if (cf->sel.xl != NOPLACE)
{
cf->sel.yl -= i; cf->sel.yr -= i;
if (!(restore_select = (cf->sel.yl >= 0)))
cf->sel.xl = NOPLACE;
}
break;
#ifndef OLDCURSES
case KEY_PPAGE: /* page back */
#endif /* OLDCURSES */
case CTL('U'):
#ifdef MOUSE
pagebak:
#endif
enqueue(cf);
restore_select = FALSE;
for (i = 0; i < LINES - OVERLAP; i++)
{
if (cf->toppos == 0)
break;
cf->toppos = getprevln(cf->fp, cf->toppos, linbuf);
}
if (cf->sel.xl != NOPLACE)
{
cf->sel.yl += i; cf->sel.yr += i;
if (!(restore_select = (cf->sel.yr < LINES - 1)))
cf->sel.xl = NOPLACE;
}
break;
#ifndef OLDCURSES
case KEY_UP:
#endif /* OLDCURSES */
case CTL('N'):
#ifdef MOUSE
linebak:
#endif
restore_select = FALSE;
if (cf->toppos)
{
cf->toppos = getprevln(cf->fp, cf->toppos, linbuf);
if (cf->sel.xl != NOPLACE)
{
cf->sel.yl++; cf->sel.yr++;
if (!(restore_select = (cf->sel.yr < LINES - 1)))
cf->sel.xl = NOPLACE;
}
}
break;
#ifndef OLDCURSES
case KEY_DOWN:
#endif /* OLDCURSES */
case CTL('P'):
#ifdef MOUSE
linefwd:
#endif
restore_select = FALSE;
if (cf->toppos < cf->lastpagetoppos)
{
cf->toppos = getnextln(cf->fp, cf->toppos, linbuf);
if (cf->sel.xl != NOPLACE)
{
cf->sel.yl--; cf->sel.yr--;
if (!(restore_select = (cf->sel.yl >= 0)))
cf->sel.xl = NOPLACE;
}
}
break;
#ifndef OLDCURSES
case KEY_ENTER: /* accept keyboard input */
#endif /* OLDCURSES */
case CTL('J'):
case CTL('M'):
lightup = FORCED;
#ifndef POPUP
restore_select = FALSE;
if (prompt == SEARCH || prompt == LOOKUP)
{
if (!incrsearch && byname(prompt, entrybuf, '\0') == NOWHERE)
beep();
enterchar('\0', LOOKUP, LINES - 1, 0, PANELSTART);
prompt = NOMESG;
break;
}
/* nothing in prompt buffer, no search active: FALL THROUGH */
#endif /* POPUP */
#ifndef OLDCURSES
case KEY_REFERENCE: /* chase reference */
#endif /* OLDCURSES */
restore_select = FALSE;
if (cf->sel.xl == NOPLACE) /* any link? */
{
message(NOREFS);
continue;
}
else
(void) chase(cf->sel.xl, cf->sel.yl, cf == &vhi);
break;
#ifndef OLDCURSES
case KEY_RIGHT: /* next reference */
#endif /* OLDCURSES */
case CTL('F'):
restore_select = FALSE;
if (cf->sel.xl == NOPLACE)
message(NOREFS);
else
{
paintselect(&cf->sel, (cf == &vhi) ? normattr : selattr);
cf->sel = findnextsel(cf->sel.xl, cf->sel.yl, cf == &vhi);
paintselect(&cf->sel, hiliteattr);
}
refresh();
continue;
#ifndef OLDCURSES
case KEY_LEFT: /* previous reference */
#endif /* OLDCURSES */
case CTL('B'):
restore_select = FALSE;
if (cf->sel.xl == NOPLACE)
message(NOREFS);
else
{
paintselect(&cf->sel, (cf == &vhi) ? normattr : selattr);
/*
* The -1 offset on xsel is important. It insures that
* when findprevsel() starts looking for a previous
* tag, it doesn't find the current one again.
*/
cf->sel = findprevsel(cf->sel.xl-1, cf->sel.yl, cf == &vhi);
paintselect(&cf->sel, hiliteattr);
}
refresh();
continue;
#ifndef OLDCURSES
case KEY_FIND: /* search for string */
#endif /* OLDCURSES */
case CTL('S'):
restore_select = FALSE;
#ifndef POPUP
prompt = SEARCH;
#else
if (cp = get_dialogue(SEARCH))
if (search(cp, '\0') != NOWHERE)
{
get_dialogue((char *)NULL);
break;
}
else
continue;
#endif /* POPUP */
break;
#ifndef OLDCURSES
case KEY_SFIND: /* search for last string again */
#endif /* OLDCURSES */
case CTL('R'):
restore_select = FALSE;
if (lasthit[0] == '\0')
{
message(NOSRCH);
continue;
}
else
#ifdef POPUP
(void) search(lasthit, '\0');
#else
{
prompt = SEARCH;
(void) strcpy(entrybuf, lasthit);
entrycnt = strlen(lasthit);
(void) ungetch(CTL('J'));
}
#endif /* POPUP */
break;
#ifndef OLDCURSES
case KEY_SELECT: /* search for entry */
#endif /* OLDCURSES */
case CTL('E'):
lookfor:
restore_select = FALSE;
#ifndef POPUP
prompt = LOOKUP;
#else
if (cp = get_dialogue(LOOKUP))
{
if (lookup(cp, '\0') == NOWHERE)
beep();
get_dialogue((char *)NULL);
break;
}
#endif /* POPUP */
break;
#ifndef OLDCURSES
case KEY_HOME: /* go to beginning of file */
case KEY_BEG:
#endif /* OLDCURSES */
case CTL('A'):
enqueue(cf);
restore_select = FALSE;
cf->toppos = 0;
break;
#ifndef OLDCURSES
case KEY_END: /* go to end of file */
#endif /* OLDCURSES */
case CTL('O'):
enqueue(cf);
restore_select = FALSE;
cf->toppos = cf->lastpagetoppos;
break;
case BS: /* this is context-sensitive */
#ifndef POPUP
if (prompt != NOMESG)
goto medialspace;
/* FALL THROUGH */
#endif /* POPUP */
#ifndef OLDCURSES
case KEY_UNDO: /* backtrack into the location stack */
#endif /* OLDCURSES */
case ESC:
restore_select = TRUE;
dequeue(cf);
break;
case TAB: /* toggle between text and index */
restore_select = TRUE;
vhi.dsptoppos = vht->dsptoppos = NOWHERE;
if (cf == vht)
cf = &vhi;
else
cf = vht;
break;
#ifndef OLDCURSES
case KEY_PRINT: /* append selected entry to file */
#endif /* OLDCURSES */
case CTL('Y'):
restore_select = FALSE;
(void) sprintf(linbuf, CUTFILE, execname);
if (cf->sel.xl == NOPLACE)
{
message(NOREFS);
continue;
}
else if ((cutfp = fopen(linbuf, "a")) == (FILE *)NULL)
{
message(CUTNOP);
continue;
}
else
{
long pos; /* find target */
int dummy;
message(CUTNOW);
pos = iflink(cf->sel.xl, cf->sel.yl, &dummy,&dummy, cf == &vhi);
if (pos >= 0) /* how can this fail? */
cutentry(pos, cutfp);
(void) fclose(cutfp);
message(CUTDNE);
continue;
}
break;
#ifdef MOUSE
/*
* Note: we treat a single-button mouse as having the left
* button only.
*
* The mouse_status() function should return the key-status defines
* implied below when appropriate. It should return the zero-origin
* mouse hotspot coordinates at character-cell resolution, with
* CMDLINE and THUMBCOL as special values designating the command
* line and thumb column respectively (on character displays these
* would be LINES - 1 and COLS - 1 respectively).
*/
case KEY_MOUSE: /* mouse gesture has occurred */
mstat = mouse_status(&hsy, &hsx);
#ifndef POPUP
hsy = (hsy == LINES - 1) ? CMDLINE : hsy;
#endif /* POPUP */
switch(mstat)
{
case BUTTON1_PRESSED: /* left button down; chase link, else page */
restore_select = FALSE;
if (hsy == CMDLINE) /* clicked on entry line */
{
hsx -= PANELSTART;
for (i = 0; i < sizeof(panel) / sizeof(strip); i++)
if (hsx >= panel[i].left && hsx <= panel[i].right)
(void) ungetch(panel[i].cmd);
continue;
}
else if (hsx == THUMBCOL) /* click in thumb column */
{
if (hsy == 0) /* clicked top of bar */
goto linebak;
else if (hsy == LASTLINE) /* clicked bottom of bar */
goto linefwd;
else if (hsy < cf->ythumb) /* clicked above thumb */
goto pagebak;
else if (hsy > cf->ythumb) /* clicked below thumb */
goto pagefwd;
else /* clicked on the thumb */
thumbdrag = TRUE;
}
else if (!chase(hsx, hsy, cf == &vhi))
goto pagefwd; /* click in text somewhere */
break;
case BUTTON1_RELEASED: /* left button up */
restore_select = FALSE;
if (thumbdrag && 0 < hsy && hsy < LASTLINE)
{
cf->toppos = /* compute new position */
cf->lastpagetoppos *
(hsy - 1) / (LASTLINE - 2);
assert(0 <= cf->toppos &&
cf->toppos <= cf->lastpagetoppos);
/* ensure on line boundary */
if (0 < cf->toppos && cf->toppos < cf->lastpagetoppos)
cf->toppos = getnextln(cf->fp, cf->toppos, linbuf);
}
thumbdrag = FALSE;
break;
case BUTTON3_PRESSED: /* right button down; backtrack */
restore_select = TRUE;
dequeue(cf);
break;
case BUTTON3_RELEASED: /* right button up */
/* reserved for future expansion */
break;
}
break;
#endif /* MOUSE */
default:
#ifdef POPUP
if (isprint(c))
{
(void) ungetch(c);
goto lookfor;
}
#else
medialspace:
restore_select = FALSE;
if (prompt == NOMESG)
{
ungetch(c);
goto lookfor;
}
enterchar(c, prompt, LINES - 1, 0, PANELSTART);
#endif /* POPUP */
lightup = SUPPRESS;
}
#ifndef POPUP
/* regenerate prompt */
attrset(promptattr);
move(LINES - 1, 0);
addstr(prompt);
addstr(entrybuf);
addstr(nblanks(PANELSTART - strlen(prompt) - strlen(entrybuf)));
mvaddstr(LINES - 1, PANELSTART, (cf == &vhi) ? IBANNER : TBANNER);
attrset(normattr);
#endif /* POPUP */
if (cf->toppos != cf->dsptoppos)
{
int i;
long newpos = cf->toppos;
#ifdef DEBUG
(void) fprintf(stderr,
"screen update triggered, toppos = %ld\n",
cf->toppos);
#endif /* DEBUG */
attrset(normattr);
for (i = 0; i <= LASTLINE; i++)
if ((newpos = getnextln(cf->fp, newpos, linbuf)) != NOWHERE)
{
mvaddstr(i, 0, linbuf);
/*
* This is actually faster, and creates less
* flicker, than clearing the line.
*/
addstr(nblanks(COLS - strlen(linbuf)));
}
cf->dspnextpos = ftell(cf->fp);
#ifdef SCROLLBAR
/*
* Generate the scroll bar. Yes, we really do want the denominator
* to be lastpagetoppos and not endpos. This means the thumb only
* bottoms out if we're really at the end.
*/
cf->ythumb = 1 + cf->toppos * (LASTLINE - 2) / cf->lastpagetoppos;
assert(0 < cf->ythumb && cf->ythumb < LASTLINE);
mvaddch(0, COLS, SBARTOPCH | sbarendattr);
for (i = 1; i < LASTLINE; i++) /* set up scrollbar */
mvaddch(i, COLS, SBARCH | sbarattr);
mvaddch(LASTLINE, COLS, SBARBOTCH | sbarendattr);
mvaddch(cf->ythumb, COLS, SBARTHUMBCH | sbthumbattr);
#endif /* SCROLLBAR */
}
/*
* This has to be done whether or not lightup is going to happen.
* Otherwise, a following command may enqueue a bogus selection
* box as part of state.
*/
cf->sel = findnextsel(0, -1, cf == &vhi);
if (lightup == SUPPRESS)
lightup = OPTIONAL;
else
{
if (incrsearch)
(void) byname(prompt, (char *)NULL, DEL);
if (lightup == FORCED || cf->toppos != cf->dsptoppos)
{
lightup = OPTIONAL;
/* highlight the current select, if any */
if (restore_select && cf->sel.xl != NOPLACE)
paintselect(&cf->sel, hiliteattr); /* restore existing select */
else
{
/* highlight the first selection, if there is one */
if (cf->sel.xl != NOPLACE)
paintselect(&cf->sel, hiliteattr);
}
/* if in text, boldface all potential selects */
if (!(cf == &vhi) && cf->sel.xl != NOPLACE)
{
region hibox;
#ifdef DEBUG
(void) fprintf(stderr,
"current: x = %2d, y = %2d, pos = %ld.\n",
cf->sel.xl, cf->sel.yl, cf->toppos);
#endif /* DEBUG */
hibox.xl = cf->sel.xl;
hibox.yl = cf->sel.yl;
for (;;)
{
hibox = findnextsel(hibox.xl, hibox.yl, cf == &vhi);
#ifdef DEBUG
(void) fprintf(stderr,
"hibox: x = %2d, y = %2d.\n",
hibox.xl, hibox.yl);
#endif /* DEBUG */
if (hibox.xl == cf->sel.xl && hibox.yl == cf->sel.yl)
break;
paintselect(&hibox, selattr);
}
}
}
}
move(LINES - 1, (entrycnt != 0) * strlen(prompt) + entrycnt);
cf->dsptoppos = cf->toppos;
refresh();
}
uninitbrowse(TRUE);
}
#ifdef MSDOS
static char *basename(s)
/* isolate base name of fully qualified filename (modifies the name passed!) */
char *s;
{
char *p;
p = strrchr(s, '.'); /* find rightmost dot */
if ( strchr(p, '\\') ) /* if a \ comes after it, find string end */
p = strchr(p, 0);
*p = 0; /* terminate at last dot after a \ (if any)*/
if ( (p = strrchr(s, '\\')) == NULL ) /* find last \ (if any) */
p = s; /* take whole string if no \ */
else
++p; /* else take string after \ */
strlwr(p);
return p;
}
#else
#define basename(x) x /* don't need to strip argv[0] under UNIX */
#endif /* MSDOS */
/*******************************************************************
*
* Main sequence
*
******************************************************************/
int
main(argc, argv)
int argc;
char **argv;
{
char *dbname, *batchkey = NULL;
int i;
bool doindex = FALSE, docheck = FALSE, dofilter = FALSE;
bool forcemono = FALSE, atrandom = FALSE;
execname = argv[0];
#ifdef UNIX
/*
* Weird! It seems bash under linux passes in a full pathname, even if
* argv[0] was relative! Foil this.
*/
if ((dbname = strrchr(execname, '/')) != (char *)NULL)
execname = dbname + 1;
#endif /* UNIX */
while ((++argv, --argc) && *argv[0] == '-')
for (i = 1; argv[0][i]; i++)
switch(argv[0][i])
{
case 'b':
batchkey = argv[1];
break;
case 'c':
docheck = TRUE;
break;
case 'f':
dofilter = TRUE;
break;
case 'g':
doindex = TRUE;
break;
case 'i':
incrsearch = TRUE;
break;
case 'm':
forcemono = TRUE;
break;
case 'r':
atrandom = TRUE;
break;
#ifdef __TURBOC__
case 's':
{ /* if CGA that snows: */
extern bool has_snowy_CGA;
has_snowy_CGA = TRUE;
}
break;
#endif /* __TURBOC__ */
default:
(void) fprintf(stderr, USAGE);
exit(1);
}
#ifdef DEBUG
(void) freopen("ERRLOG", "w", stderr);
#endif /* DEBUG */
/*
* User specified a batch-retrieval option.
* OK, snarf the keyword from the next argument.
*/
if (batchkey)
++argv, --argc;
dbname = argc ? argv[0] : basename(execname);
if (!doindex && !docheck && !atrandom && !dofilter
&& batchkey == (char *)NULL)
{
(void) printf(BRSMES, dbname);
browse(dbname, !forcemono);
}
if (doindex)
{
(void) printf(IDXMES, dbname);
if (argc)
mkindex(argc, argv);
else
mkindex(1, &dbname);
}
if (docheck)
{
(void) printf(CHKMES, dbname);
chkindex(dbname);
}
if (batchkey)
{
daddr_t pos;
if (!initbrowse(dbname))
exit(1);
if ((pos = xlocate(batchkey)) >= 0)
cutentry(pos, stdout);
}
if (dofilter)
{
daddr_t pos;
char batchkey[81];
if (!initbrowse(dbname))
exit(1);
while (fgets(batchkey, sizeof(batchkey) - 1, stdin) != (char *)NULL)
{
char *ep = &batchkey[strlen(batchkey) - 1];
if (*ep == '\n')
{
*ep = '\0';
if (*--ep == '\r')
*ep = '\0';
}
if ((pos = xlocate(batchkey)) >= 0)
cutentry(pos, stdout);
}
}
if (atrandom)
{
daddr_t pos;
if (!initbrowse(dbname))
exit(1);
if ((pos = jrandom()) >= 0)
cutentry(pos, stdout);
}
exit(0);
}
/* browse.c ends here */
|