xaizek / pinfo (License: GPLv2 only) (since 2018-12-07)
Console-based info and manual pages reader, which adds interactive navigation to man pages.
<root> / src / manual.c (aaca68790ee15b03d049ccf8c3ce79d88e688594) (49KiB) (mode 100644) [raw]
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 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
/***************************************************************************
 *  Pinfo is a ncurses based lynx style info documentation browser
 *
 *  Copyright (C) 1999  Przemek Borys <pborys@dione.ids.pl>
 *  Copyright (C) 2005  Bas Zoetekouw <bas@debian.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of version 2 of the GNU General Public License as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 *  USA
 ***************************************************************************/
#include "common_includes.h"

#include <ctype.h>
#include <sys/stat.h>
#include <stdlib.h>

#define HTTPSECTION 100
#define FTPSECTION 101
#define MAILSECTION 102

/* check if a char is a hyphen character */
int ishyphen(unsigned char ch);
/* load manual */
void loadmanual(FILE * id);
/* handle keyboard */
int manualwork();
void rescan_selected();	/* scan for potential link to select on
							   viewed manual page */
/* self explanatory */
void showmanualscreen();
/* mvaddstr with bold/italic */
void mvaddstr_manual(int y, int x, char *str);
/* adds highlights to a painted screen */
void add_highlights();
/* strips line from formatting characters */
void strip_manual(char *buf);
/*
 * Initialize links in a line .  Links are entries of form reference(section),
 * and are stored in `manuallinks' var, described bellow.
 */
void man_initializelinks(char *line, int carry);
int is_in_manlinks(char *in, char *find);

void printmanual(char **Message, long Lines);

/* line by line stored manual */
char **manual = 0;
/* number of lines in manual */
unsigned ManualLines = 0;
int selected = -1;		/* number of selected link(offset in 'manuallinks',
						   bellow) */
unsigned manualpos = 0;	/* number of the first line, which is painted on
						   screen */

unsigned manualcol = 0;	/* the first displayed column of manpage--
						   for moving the screen left/right */

int manual_aftersearch = 0;	/* this is set if man page is now after search
							   operation */
int manwidthChanged = 0;	/* this flag indicates whether the env variable
							   $MANWIDTH was changed by pinfo */

typedef struct
{
	/* name of a manual */
	char name[128];
	/* section */
	char sect[32];
	/* what was last selected on this page */
	int selected;
	/* what was the last manualpos */
	int pos;
}
manhistory;			/*
					 * type for the `lastread' history entries, when viewing
					 * man pages.
					 */

/* manual lastread history */
manhistory *manualhistory = 0;
/* length of the above table - 1 */
int manualhistorylength = 0;

/* this structure describes a hyperlink in manual viewer */
typedef struct
{			/* struct for hypertext references */
	unsigned int line;		/* line of the manpage, where the reference is */
	/* column of that line */
	unsigned int col;
	/* name of the reference */
	char *name;
	/* section of the reference */
	char section[32];
	int section_mark;
	/* determine whether there is a hyphen above */
	int carry;
}
manuallink;

/* a set of manual references of man page */
manuallink *manuallinks = 0;

/* number of found manual references in man page */
unsigned ManualLinks = 0;

/* semaphore for checking if it's a history(left arrow) call */
int historical = 0;


void
/* free buffers allocated by current man page */
manual_free_buffers()
{
	unsigned int i;
	/* first free previously allocated memory */
	/* for the manual itself... */
	if (manual)
	{
		for (i = 0; i <= ManualLines; i++)
		{
			xfree(manual[i]);
		}
		xfree(manual);
		manual = 0;
		ManualLines = 0;
	}
	/* ...and for the list of manual hypertext */
	if (manuallinks)
	{				/* links */
		for (i = 0; i < ManualLinks; i++)
		{
			xfree(manuallinks[i].name);
		}
		xfree(manuallinks);
		manuallinks = 0;
		ManualLinks = 0;
		selected = -1;
	}
}

/* initialize history variables for manual pages.  */
void
set_initial_history(char *name)
{
	int len = strlen(name), i;
	char *name1 = strdup(name);

	/* one object of array */
	manualhistory = xmalloc(sizeof(manhistory));
	/* filter trailing spaces */
	while ((len > 1) &&(isspace(name1[len - 1])))
	{
		name1[len - 1] = 0;
		len--;
	}
	i = len;
	/* find the beginning of the last token */
	for (i = len - 1;(i > 0) &&(!isspace(name1[i])); i--);

	/* if we've found space, then we move to the first nonspace character */
	if (i > 0)
		i++;

	/* filename->name */
	strcpy(manualhistory[0].name, &name1[i]);
	/* section unknown */
	strcpy(manualhistory[0].sect, "");
	/* selected unknown */
	manualhistory[0].selected = -1;
	/* pos=0 */
	manualhistory[0].pos = 0;
	free(name1);
}

/* construct man name; take care about carry */
void
construct_manualname(char *buf, int which)
{
	if (!manuallinks[which].carry)
	{
		/* workaround for names starting with '(' */
		if (manuallinks[which].name[0] == '(') strcpy(buf, manuallinks[which].name + 1);
		else strcpy(buf, manuallinks[which].name);
		return;
	}
	else
	{
		/* normal manual reference */
		if (manuallinks[which].section_mark < HTTPSECTION)
		{
			char *base = xmalloc(1024);
			char *ptr;
			int tmppos;
			strncpy(base, manual[manuallinks[which].line - 1],1023);
			strip_manual(base);
			ptr = base + strlen(base) - 3;
			while (((isalpha(*ptr)) ||(*ptr == '.') ||(*ptr == '_')) &&(ptr > base))
				ptr--;
			/* workaround for man pages with leading '(' see svgalib man pages */
			if (*ptr == '(')
				ptr++;
			strcpy(buf, ptr);
			tmppos = strlen(buf);
			if (tmppos > 1)
				buf[tmppos - 2] = 0;
			strcat(buf, manuallinks[which].name);
			xfree(base);
		}
		/* url reference */
		else
		{
			char *base = xmalloc(1024);
			char *ptr, *eptr;
			int namelen = strlen(manuallinks[which].name);
			strcpy(base, manual[manuallinks[which].line + 1]);
			strip_manual(base);
			ptr = base;
			/* skip whitespace */
			while (isspace(*ptr))
				ptr++;
			eptr = findurlend(ptr);
			*eptr = 0;
			strcpy(buf, manuallinks[which].name);
			/* cut the hyphen */
			buf[namelen - 1] = 0;
			strcat(buf, ptr);
			xfree(base);
		}
	}
}

/* this is something like main() function for the manual viewer code.  */
int
handlemanual(char *name)
{
	int return_value = 0;
	struct stat statbuf;
	FILE *id;

	char manualname[256];
	char cmd[256];
	char *raw_tempfilename = 0;
	char *apropos_tempfilename = 0;

	if (tmpfilename1)
	{
		unlink(tmpfilename1);
		xfree(tmpfilename1);
	}
	tmpfilename1 = make_tempfile();

#ifdef getmaxyx
	init_curses();
	/* if ncurses, get maxx and maxy */
	getmaxyx(stdscr, maxy, maxx);
	myendwin();
	if ((!getenv("MANWIDTH")) ||(manwidthChanged))
	{
		/* set MANWIDTH environment variable */
		static char tmp[24];
		snprintf(tmp, 24, "MANWIDTH=%d", maxx);
		putenv(tmp);
		manwidthChanged = 1;
	}
#else
	/* otherwise hardcode 80x25... */
	maxx = 80;
	maxy = 25;
#endif /* getmaxyx */
#ifdef NIETS
	/****************************************************************************
	 *                    Ignore macros part: BEGIN                             *
	 * PS: Siewca: I still expect that you'll isolate it to a single procedure  *
	 * Description(by PB): This code opens a manpage file, and filters it from *
	 * dangerous macros. The output is put into a temporary file, which is then *
	 * used as the `name' filename argument of this(handlemanual) procedure.   *
	 * There is a stored variable raw_tempfilename to allow unlinking this temp *
	 * file after usage							    *
	 ****************************************************************************/
	FILE *source;
	char **ignored_entries;
	char location[256];
	char line[1025];
	char *end, *prev;
	size_t macroline_size;
	int ignored_items = 0, i = 0;
	char zipped = 0;

	/* if the pointer is non-null */
	if (ignoredmacros)
		/* if there are some macros */
		if (*ignoredmacros && strlen(ignoredmacros))
		{				/* that should be ignored   */
			*location = '\0';
			/* we need to know the path */
			snprintf(cmd, 255, "man -W %s %s",
					ManOptions,
					name);
			id = popen(cmd, "r");
			if (!id)
			{
				printf(_("Error: Cannot call man command.\n"));
				return 1;
			}
			fflush(id);
			fgets(location, 255, id);
			pclose(id);

			if (*location == '\0')
			{
				printf(_("Error: No manual page found either.\n"));
				if (use_apropos)
				{
					printf(_("Apropos pages:\n"));
					snprintf(cmd, 255, "apropos %s|cat %s", name, StderrRedirection);
					system(cmd);
				}
				return 1;
			}


			ignored_items++;
			prev = ignoredmacros;
			/* counting items */
			while ((end = strchr(prev, ':')))
			{
				ignored_items++;
				prev = end + 1;
			}

			ignored_entries =(char **) xmalloc(ignored_items * sizeof(char **));
			ignored_entries[0] = ignoredmacros;
			prev = ignoredmacros;
			i = 0;
			/* creating pointers */
			while ((end = strchr(prev, ':')))
			{
				*end = '\0';
				prev = end + 1;
				i++;
				ignored_entries[i] = prev;
			}

			/* removing newline */
			if ((prev = rindex(location, '\n')))
				*prev = '\0';

			/* checking if it's compressed */
			prev = index(location, '\0');
			if ((strlen(location)) > 3
					&&((*(prev - 1) == 'Z' && *(prev - 2) == '.')
						||(*(prev - 1) == 'z' && *(prev - 2) == 'g' && *(prev - 3) == '.')
					   )
			   )
			{
				if (verbose)
					printf("%s %s\n", _("Calling gunzip for"), location);
				snprintf(cmd, 255, "gunzip -c %s", location);
				source = popen(cmd, "r");
				zipped = 1;
				if (!source)
				{
					printf(_("Couldn't call gunzip.\n"));
					return 1;
				}
			}
			else /* from cmd output  */
				source = fopen(location, "r");
			name = make_tempfile();
			raw_tempfilename = name;
			id = fopen(name, "w");

			/* we read until eof */
			while (!feof(source))
			{
				if (fgets(line, 1024, source) == NULL)
					line[0] = '\0';

				/* macro starts with a dot*/
				if (line[0] != '.' ||(strlen(line)) <(size_t) 2)
				{
					fprintf(id, "%s", line);
					continue;
				}
				else
					while (i >= 0)
					{
						macroline_size = strlen(ignored_entries[i]);
						if (strlen(line + 1) < macroline_size)
							macroline_size = strlen(line + 1);
						if ((strncmp(ignored_entries[i], line + 1, macroline_size)) == 0
								&&(*(line + 1 +(int) macroline_size) == ' '
									|| *(line + 1 +(int) macroline_size) == '\n'
									|| *(line + 1 +(int) macroline_size) == '\t'))
						{
							if (quote_ignored)
							{
								if ((prev = rindex(line, '\n')))
									*prev = '\0';
								sprintf(cmd, "\n.br\n.nf\n[ [pinfo] - %s: %.42s", _("IGNORING"), line);
								if ((strlen(line)) >(size_t) 42)
									strcat(cmd, "(...)]\n.fi\n");
								else
									strcat(cmd, " ]\n.fi\n");
							}
							else
							{
								sprintf(cmd, ".\\\" removed macro: %.42s", line);
								if ((strlen(line)) >(size_t) 42)
									strcat(cmd, "(...)");
							}
							strcpy(line, cmd);
							break;
						}
						i--;
					}

				fprintf(id, "%s", line);
				i = ignored_items - 1;
			}			/* while (!feof(source)) */
			if (zipped)
				pclose(source);
			else
				fclose(source);
			fclose(id);
			free(ignored_entries);
		}				/* if (ignored_macros... */
	/****************************************************************************
	 *                    Ignore macros part: END                               *
	 ****************************************************************************/
#endif
	if (!plain_apropos)
		snprintf(cmd, 255, "man %s %s %s > %s",
				ManOptions,
				name,
				StderrRedirection,
				tmpfilename1);
	if ((plain_apropos) ||(system(cmd) != 0))
	{
		if (!plain_apropos)
		{
			unlink(tmpfilename1);
			printf(_("Error: No manual page found\n"));
		}
		plain_apropos = 0;
		if (use_apropos)
		{
			printf(_("Calling apropos \n"));
			apropos_tempfilename = make_tempfile();
			snprintf(cmd, 255, "apropos %s > %s", name, apropos_tempfilename);
			if (system(cmd) != 0)
			{
				printf(_("Nothing appropiate\n"));
				unlink(apropos_tempfilename);
				return 1;
			}
			id = fopen(apropos_tempfilename, "r");
		}
		else
			return 1;
	}
	else
		id = fopen(tmpfilename1, "r");
	init_curses();


	set_initial_history(name);
	/* load manual to memory */
	loadmanual(id);
	fclose(id);
	do
	{
		/* manualwork handles all actions when viewing man page */
		return_value = manualwork();
#ifdef getmaxyx
		/* if ncurses, get maxx and maxy */
		getmaxyx(stdscr, maxy, maxx);
		if ((!getenv("MANWIDTH")) ||(manwidthChanged))
		{
			/* set MANWIDTH environment variable */
			static char tmp[24];
			snprintf(tmp, 24, "MANWIDTH=%d", maxx);
			putenv(tmp);
			manwidthChanged = 1;
		}
#endif
		manual_aftersearch = 0;
		/* -1 is quit key */
		if (return_value != -1)
		{
			if (tmpfilename2)
			{
				unlink(tmpfilename2);
				xfree(tmpfilename2);
			}
			tmpfilename2 = make_tempfile();
			/*
			 * key_back is not pressed; and return_value is an offset to
			 * manuallinks
			 */
			if (return_value != -2)
			{
				construct_manualname(manualname, return_value);
				snprintf(cmd, 255, "man %s %s %s %s > %s",
						ManOptions,
						manuallinks[return_value].section,
						manualname,
						StderrRedirection,
						tmpfilename2);
			}
			else /* key_back was pressed */
			{
				manualhistorylength--;
				if (manualhistorylength == 0 && apropos_tempfilename)
				{
					id = fopen(apropos_tempfilename, "r");
					loadmanual(id);
					fclose(id);
					continue;
				}
				if (manualhistory[manualhistorylength].sect[0] == 0)
					snprintf(cmd, 255, "man %s %s %s > %s",
							ManOptions,
							manualhistory[manualhistorylength].name,
							StderrRedirection,
							tmpfilename2);
				else
					snprintf(cmd, 255, "man %s %s %s %s > %s",
							ManOptions,
							manualhistory[manualhistorylength].sect,
							manualhistory[manualhistorylength].name,
							StderrRedirection,
							tmpfilename2);
				/*
				 * flag to make sure, that
				 * manualwork will refresh the variables manualpos and selected
				 * when going back to this page
				 */
				historical = 1;
			}
			system(cmd);
			stat(tmpfilename2, &statbuf);
			if (statbuf.st_size > 0)
			{
				snprintf(cmd, 255, "mv %s %s", tmpfilename2, tmpfilename1);
				/* create tmp file containing man page */
				system(cmd);
				/* open man page */
				id = fopen(tmpfilename1, "r");
				if (id != NULL)
				{
					/* now we create history entry for new page */
					if (!historical)
					{
						manualhistorylength++;
						manualhistory = xrealloc(manualhistory,(manualhistorylength + 2) * sizeof(manhistory));
						/*
						 * we can write so since this code applies
						 * only when it's not a history call
						 */
						strcpy(manualhistory[manualhistorylength].name,
								manualname);
						strcpy(manualhistory[manualhistorylength].sect,
								manuallinks[return_value].section);
					}
					/* loading manual page and its defaults... */
					loadmanual(id);
					fclose(id);
					/* continuing with creation of history */
					if (!historical)
					{
						manualhistory[manualhistorylength].pos = manualpos;
						manualhistory[manualhistorylength].selected = selected;
					}
					else
						historical = 0;
				}
				else
					return_value = -1;
			}
		}
	}
	while (return_value != -1);
	if (apropos_tempfilename)
		unlink(apropos_tempfilename);
	/* we were using temporary */
	if (raw_tempfilename)
		unlink(raw_tempfilename);
	/* raw-manpage for scaning */
	return 0;
}

void
/* loads manual from given filedescriptor */
loadmanual(FILE * id)
{
	char prevlinechar = 0;
	/* tmp variable, set after reading first nonempty line of input */
	int cutheader = 0;
	int carryflag = 0;
	manualpos = 0;
	manual_free_buffers();
	manual = xmalloc(sizeof(char *));
	manuallinks = xmalloc(sizeof(manuallinks));
	manual[ManualLines] = xmalloc(1024);

	/* we read until eof */
	while (!feof(id))
	{
		char *tmp;
		/*
		 * it happens sometimes, that the last line is weird
		 * and causes sigsegvs by not entering anything to buffer, what
		 * confuses strlen
		 */
		if (fgets(manual[ManualLines], 1024, id) == NULL)
			manual[ManualLines][0] = 0;

		if (cutheader)
		{
			if (strcmp(manual[cutheader], manual[ManualLines]) == 0)
			{
				manual[ManualLines][0] = '\n';
				manual[ManualLines][1] = 0;
			}
		}
		if (FilterB7)
		{
			char *filter_pos = index(manual[ManualLines], 0xb7);
			if (filter_pos)
				*filter_pos = 'o';
		}
		if (CutManHeaders)
			if (!cutheader)
			{
				if (strlen(manual[ManualLines]) > 1)
				{
					cutheader = ManualLines;
				}
			}
		if ((CutEmptyManLines) &&((manual[ManualLines][0]) == '\n') &&
				(prevlinechar == '\n'))
			;			/* do nothing :)) */
		else
		{
			int manlinelen = strlen(manual[ManualLines]);
			manual[ManualLines] = xrealloc(manual[ManualLines],
					manlinelen + 10);

			/* temporary variable for determining hypertextuality of fields */
			tmp = xmalloc(manlinelen + 10);

			strcpy(tmp, manual[ManualLines]);

			/* remove formatting chars */
			strip_manual(tmp);
			man_initializelinks(tmp, carryflag);
			carryflag = 0;
			if (manlinelen > 1)
				if (ishyphen(manual[ManualLines][manlinelen - 2]))
					carryflag = 1;
			/* free temporary buffer */
			xfree(tmp);
			prevlinechar = manual[ManualLines][0];
			/* increase the number of man lines */
			ManualLines++;
			/*
			 * and realloc manual to add an empty space for
			 * next entry of manual line
			 */
			manual = xrealloc(manual,(ManualLines + 5) * sizeof(char *));
			manual[ManualLines] = xmalloc(1024);
		}
	}

}

int
compare_manuallink(const void *a, const void *b)
{
	return ((manuallink *) a)->col -((manuallink *) b)->col;
}

void
sort_manuallinks_from_current_line(long startlink, long endlink)
{
	qsort(manuallinks + startlink, endlink - startlink, sizeof(manuallink), compare_manuallink);
}


/* initializes hyperlinks in manual */
void
man_initializelinks(char *tmp, int carry)
{
	/* set tmpcnt to the trailing zero of tmp */
	int tmpcnt = strlen(tmp) + 1;
	char *mylink = tmp;
	char *urlstart, *urlend;
	long initialManualLinks = ManualLinks;
	int i, b;
	/******************************************************************************
	 * handle url refrences                                                       *
	 *****************************************************************************/
	urlend = tmp;
	while ((urlstart = strstr(urlend, "http://")) != NULL)
	{
		/* always successfull */
		urlend = findurlend(urlstart);
		manuallinks = xrealloc(manuallinks, sizeof(manuallink) *(ManualLinks + 3));
		manuallinks[ManualLinks].line = ManualLines;
		manuallinks[ManualLinks].col = width_of_string(tmp, urlstart - tmp);
		strcpy(manuallinks[ManualLinks].section, "HTTPSECTION");
		manuallinks[ManualLinks].section_mark = HTTPSECTION;
		manuallinks[ManualLinks].name = xmalloc(urlend - urlstart + 10);
		strncpy(manuallinks[ManualLinks].name, urlstart, urlend - urlstart);
		manuallinks[ManualLinks].name[urlend - urlstart] = 0;
		if (ishyphen(manuallinks[ManualLinks].name[urlend - urlstart - 1]))
			manuallinks[ManualLinks].carry = 1;
		else
			manuallinks[ManualLinks].carry = 0;
		ManualLinks++;
	}
	urlend = tmp;
	while ((urlstart = strstr(urlend, "ftp://")) != NULL)
	{
		/* always successfull */
		urlend = findurlend(urlstart);
		manuallinks = xrealloc(manuallinks, sizeof(manuallink) *(ManualLinks + 3));
		manuallinks[ManualLinks].line = ManualLines;
		manuallinks[ManualLinks].col = width_of_string(tmp, urlstart - tmp);
		strcpy(manuallinks[ManualLinks].section, "FTPSECTION");
		manuallinks[ManualLinks].section_mark = FTPSECTION;
		manuallinks[ManualLinks].name = xmalloc(urlend - urlstart + 10);
		strncpy(manuallinks[ManualLinks].name, urlstart, urlend - urlstart);
		manuallinks[ManualLinks].name[urlend - urlstart] = 0;
		if (ishyphen(manuallinks[ManualLinks].name[urlend - urlstart - 1]))
			manuallinks[ManualLinks].carry = 1;
		else
			manuallinks[ManualLinks].carry = 0;
		ManualLinks++;
	}
	urlend = tmp;
	while ((urlstart = findemailstart(urlend)) != NULL)
	{
		/* always successfull */
		urlend = findurlend(urlstart);
		manuallinks = xrealloc(manuallinks, sizeof(manuallink) *(ManualLinks + 3));
		manuallinks[ManualLinks].line = ManualLines;
		manuallinks[ManualLinks].col = width_of_string(tmp, urlstart - tmp);
		strcpy(manuallinks[ManualLinks].section, "MAILSECTION");
		manuallinks[ManualLinks].section_mark = MAILSECTION;
		manuallinks[ManualLinks].name = xmalloc(urlend - urlstart + 10);
		strncpy(manuallinks[ManualLinks].name, urlstart, urlend - urlstart);
		manuallinks[ManualLinks].name[urlend - urlstart] = 0;
		if (ishyphen(manuallinks[ManualLinks].name[urlend - urlstart - 1]))
			manuallinks[ManualLinks].carry = 1;
		else
			manuallinks[ManualLinks].carry = 0;

		/* there should be a dot in e-mail domain */
		if (strchr(manuallinks[ManualLinks].name, '.') != NULL)
			ManualLinks++;
	}
	/******************************************************************************
	 * handle normal manual refrences -- reference(section)                       *
	 ******************************************************************************/
	do
	{
		/* we look for '(', since manual link */
		mylink = strchr(mylink, '(');
		/* has form of  'blah(x)' */
		if (mylink != NULL)
		{
			char *temp;
			/* look for the closing bracket */
			if ((temp = strchr(mylink, ')')))
			{
				char *p_t1, *p_t;
				p_t = p_t1 = xmalloc((strlen(mylink) + 10) * sizeof(char));
				for (++mylink; mylink != temp; *p_t++ = *mylink++);
				*p_t = '\0';
				mylink -=(strlen(p_t1) + sizeof(char));

				if ((!strchr(p_t1, '(')) &&(!is_in_manlinks(manlinks, p_t1)))
				{
					char tempchar;
					int breakpos, cols_before_link;
					i = mylink - tmp - 1;
					if (i < 0)
						i++;
					for (; i > 0; --i)
					{
						if (!isspace(tmp[i]))
							/* ignore spaces between linkname and '(x)' */
							break;
					}
					/* we'll put zero on the last non-textual character of link */
					breakpos = i + 1;
					/* but remember the cleared char for the future */
					tempchar = tmp[breakpos];
					tmp[breakpos] = 0;
					/*
					 * scan to the first space sign or to 0 -- that means go to
					 * the beginning of the scanned token
					 */
					for (i = breakpos; i > 0; --i)
					{
						if (isspace(tmp[i]))
						{
							i++;
							break;
						}
					}
					/* now we have needed string in i..breakpos. We need now to
					 * realloc the
					 * manuallinks table to make free space for new entry
					 */

					/* calculate the number of columns in front of the link */
					cols_before_link = width_of_string(tmp, i-1);

					/* a small check */
					if (!((use_apropos) &&(manualhistorylength == 0)))
					{
						/*
						 * In English: if the name of the link is the name of
						 * the current page and the section of the link is the
						 * current section or if we don't know the current
						 * section, then...
						 */
						if ((!strcasecmp(&tmp[i], manualhistory[manualhistorylength].name))
								&&((!strcasecmp(p_t1, manualhistory[manualhistorylength].sect))
									||(manualhistory[manualhistorylength].sect[0] == 0)
									||(!strcmp(manualhistory[manualhistorylength].sect, " "))))

							break;
					}
					manuallinks = xrealloc(manuallinks, sizeof(manuallink) *(ManualLinks + 3));
					manuallinks[ManualLinks].line = ManualLines;
					manuallinks[ManualLinks].col = cols_before_link + 1;
					if (LongManualLinks)
					{
						for (b = 1; mylink[b] != ')'; b++)
							manuallinks[ManualLinks].section[b - 1] = tolower(mylink[b]);
						manuallinks[ManualLinks].section[b - 1] = 0;
					}
					else
					{
						manuallinks[ManualLinks].section[0] = mylink[1];
						manuallinks[ManualLinks].section[1] = 0;
					}
					manuallinks[ManualLinks].section_mark = 0;
					manuallinks[ManualLinks].name = xmalloc((breakpos - i) + 10);
					strcpy(manuallinks[ManualLinks].name, tmp + i);
					tmp[breakpos] = tempchar;

					/* check whether this is a carry'ed entry(i.e. in the
					 * previous line there was `-' at end, and this is the
					 * first word of this line */
					for (b = i - 1; b >= 0; b--)
					{
						if (b > 0)
							if (!isspace(tmp[b]))
								break;
					}
					if (b >= 0)
						manuallinks[ManualLinks].carry = 0;
					else
						manuallinks[ManualLinks].carry = carry;
					/* increase the number of entries */
					ManualLinks++;
				}		/*... if (in man links) */
				xfree((void *) p_t1);
			}
		}
		if (mylink)
			mylink++;
		if (mylink >(tmp + tmpcnt))
		{
			break;
		}
	}
	/* do this line until strchr() won't find a '(' in string */
	while (mylink != NULL);
	if (initialManualLinks != ManualLinks)
		sort_manuallinks_from_current_line(initialManualLinks, ManualLinks);
}

/* viewer function. Handles keyboard actions--main event loop */
int
manualwork()
{
	/* for user's shell commands */
	FILE *mypipe;
	/* a temporary buffer */
	char *token;
	/* key, which contains the value entered by user */
	int key = 0;
	/* tmp values */
	int selectedchanged;
	int statusline = FREE;
#ifdef getmaxyx
	/* if ncurses, get maxx and maxy */
	getmaxyx(stdscr, maxy, maxx);
	if ((!getenv("MANWIDTH")) ||(manwidthChanged))
	{
		/* set MANWIDTH environment variable */
		static char tmp[24];
		snprintf(tmp, 24, "MANWIDTH=%d", maxx);
		putenv(tmp);
		manwidthChanged = 1;
	}
#else
	maxx = 80;
	/* otherwise hardcode 80x25... */
	maxy = 25;
#endif /* getmaxyx */


	/* get manualpos from history.  it is set in handlemanual() */
	manualpos = manualhistory[manualhistorylength].pos;
	/* if there was a valid selected entry, apply it */
	if (manualhistory[manualhistorylength].selected != -1)
		selected = manualhistory[manualhistorylength].selected;
	else /* otherwise scan for selected on currently viewed page */
		rescan_selected();

	/* clean screen */
	erase();

	/* user events loop. finish when key_quit */
	while (1)
	{
		/* make getch not wait for user */
		nodelay(stdscr, TRUE);
		/* action -- return ERR */
		key = pinfo_getch();
		/* if there was nothing in buffer */
		if (key == ERR)
		{
			/* then show screen */
			if (statusline == FREE)
				showmanualscreen();
			wrefresh(stdscr);
			waitforgetch();
			key = pinfo_getch();
		}
		nodelay(stdscr, FALSE);
		statusline = FREE;
		if (winchanged)
		{
			handlewinch();
			winchanged = 0;
			key = pinfo_getch();
		}
		/************************ keyboard handling **********************************/
		if (key > 0)
		{
			if ((key == keys.print_1) ||
					(key == keys.print_2))
			{
				if (yesno(_("Are you sure you want to print?"), 0))
					printmanual(manual, ManualLines);
			}
			/*====================================================*/
			if ((key == keys.goto_1) ||
					(key == keys.goto_2))
			{
				manuallinks = xrealloc(manuallinks,(ManualLinks + 1) *(sizeof(manuallink) + 3));

				/* get user's value */
				attrset(bottomline);
				move(maxy - 1, 0);
				echo();
				curs_set(1);
				manuallinks[ManualLinks].name = getstring(_("Enter manual name: "));
				curs_set(0);
				noecho();
				move(maxy - 1, 0);
#ifdef HAVE_BKGDSET
				bkgdset(' ' | bottomline);
				clrtoeol();
				bkgdset(0);
#else
				myclrtoeol();
#endif
				attrset(normal);

				manuallinks[ManualLinks].carry = 0;
				manuallinks[ManualLinks].section_mark = 0;
				strcpy(manuallinks[ManualLinks].section, " ");
				manuallinks[ManualLinks].line = -1;
				manuallinks[ManualLinks].col = -1;
				ManualLinks++;
				return ManualLinks - 1;
			}
			/*====================================================*/
			if ((key == keys.goline_1) ||
					(key == keys.goline_2))
			{
				long newpos;
				/* get user's value */
				attrset(bottomline);
				move(maxy - 1, 0);
				echo();
				curs_set(1);
				token = getstring(_("Enter line: "));
				curs_set(0);
				noecho();
				move(maxy - 1, 0);
#ifdef HAVE_BKGDSET
				bkgdset(' ' | bottomline);
				clrtoeol();
				bkgdset(0);
#else
				myclrtoeol();
#endif
				attrset(normal);
				/* convert string to long.  careful with nondigit strings.  */
				if (token)
				{
					int digit_val = 1;
					for (unsigned i = 0; token[i] != 0; i++)
					{
						if (!isdigit(token[i]))
							digit_val = 0;
					}
					/* move cursor position */
					if (digit_val)
					{
						newpos = atol(token);
						newpos -=(maxy - 1);
						if ((newpos >= 0) &&(newpos < ManualLines -(maxy - 2)))
							manualpos = newpos;
						else if (newpos > 0)
							manualpos = ManualLines -(maxy - 2);
						else
							manualpos = 0;
					}
					xfree(token);
					token = 0;
				}
			}
			/*=====================================================*/
			if ((key == keys.shellfeed_1) ||
					(key == keys.shellfeed_2))
			{
				/* get command name */
				curs_set(1);
				attrset(bottomline);
				move(maxy - 1, 0);
				echo();
				/* get users cmd */
				token = getstring(_("Enter command: "));
				noecho();
				move(maxy - 1, 0);
#ifdef HAVE_BKGDSET
				bkgdset(' ' | bottomline);
				clrtoeol();
				bkgdset(0);
#else
				myclrtoeol();
#endif
				attrset(normal);

				myendwin();
				system("clear");
				/* open mypipe */
				mypipe = popen(token, "w");
				if (mypipe != NULL)
				{
					/* and flush the msg to stdin */
					for (unsigned i = 0; i < ManualLines; i++)
						fprintf(mypipe, "%s", manual[i]);
					pclose(mypipe);
				}
				getchar();
				doupdate();
				curs_set(0);
			}
			/*=====================================================*/
			if ((key == keys.refresh_1) ||
					(key == keys.refresh_2))
			{
				myendwin();
				doupdate();
				refresh();
				curs_set(0);
			}
			/*=====================================================*/
			/* search in current node */
			if ((key == keys.search_1) ||
					(key == keys.search_2))
			{
				int success = 0;
				/* procedure of getting regexp string */
				move(maxy - 1, 0);
				attrset(bottomline);
				echo();
				curs_set(1);
				/*
				 * searchagain handler. see keys.totalsearch at mainfunction.c
				 * for comments
				 */
				if (!searchagain.search)
				{
					token = getstring(_("Enter regular expression: "));
					strcpy(searchagain.lastsearch, token);
					searchagain.type = key;
				}
				else
				{
					token = xmalloc(strlen(searchagain.lastsearch) + 1);
					strcpy(token, searchagain.lastsearch);
					searchagain.search = 0;
				}		/* end of searchagain handler */
				if (strlen(token) == 0)
				{
					xfree(token);
					goto skip_search;
				}
				curs_set(0);
				noecho();
				move(maxy - 1, 0);
#ifdef HAVE_BKGDSET
				bkgdset(' ' | bottomline);
				clrtoeol();
				bkgdset(0);
#else
				myclrtoeol();
#endif
				attrset(normal);
				/* compile regexp expression */
				if (pinfo_re_comp(token) != 0)
				{
					/* We're not in a search! */
					aftersearch = 0;
					/* print error message */
					attrset(bottomline);
					mymvhline(maxy - 1, 0, ' ', maxx);
					move(maxy - 1, 0);
					printw(_("Invalid regular expression;"));
					printw(" ");
					printw(_("Press any key to continue..."));
					getch();
					goto skip_search;
				}
				/* and search for it in all subsequential lines */
				for (unsigned i = manualpos + 1; i < ManualLines - 1; i++)
				{
					char *tmp;
					tmp = xmalloc(strlen(manual[i]) + strlen(manual[i + 1]) + 10);
					/*
					 * glue two following lines together, to find expres- sions
					 * split up into two lines
					 */
					strcpy(tmp, manual[i]);
					strcat(tmp, manual[i + 1]);
					strip_manual(tmp);

					/* execute search */
					if (pinfo_re_exec(tmp))
					{		/* if found, enter here... */
						success = 1;
						strcpy(tmp, manual[i + 1]);
						strip_manual(tmp);
						/*
						 * if it was found in the second line of the glued
						 * expression.
						 */
						if (pinfo_re_exec(tmp))
							manualpos = i + 1;
						else
							manualpos = i;
						xfree(tmp);
						break;
					}
					xfree(tmp);
				}
				xfree(token);
				rescan_selected();
				if (!success)
				{
					attrset(bottomline);
					mvaddstr(maxy - 1, 0, _("Search string not found..."));
					statusline = LOCKED;
				}

				manual_aftersearch = 1;
			}
			/*=====================================================*/
			/* search again */
			/* see mainfunction.c for comments */
			if ((key == keys.search_again_1) ||
					(key == keys.search_again_2))
			{
				if (searchagain.type != 0)
				{
					searchagain.search = 1;
					ungetch(searchagain.type);
				}
			}
skip_search:
			/*=====================================================*/
			if ((key == keys.twoup_1) ||
					(key == keys.twoup_2))
			{
				ungetch(keys.up_1);
				ungetch(keys.up_1);
			}
			/*=====================================================*/
			if ((key == keys.up_1) ||
					(key == keys.up_2))
			{
				selectedchanged = 0;
				/* if there are links at all */
				if (selected != -1)
				{
					/* if one is selected */
					if (selected > 0)
						/*
						 * scan for a next visible one, which is above the
						 * current.
						 */
						for (int i = selected - 1; i >= 0; i--)
						{
							if ((manuallinks[i].line >= manualpos) &&
									(manuallinks[i].line < manualpos +(maxy - 1)))
							{
								selected = i;
								selectedchanged = 1;
								break;
							}
						}
				}
				/* if new link not found */
				if (!selectedchanged)
				{
					/* move one position up */
					if (manualpos >= 1)
						manualpos--;
					/* and scan for selected again :) */
					for (unsigned i = 0; i < ManualLinks; i++)
					{
						if (manuallinks[i].line == manualpos)
						{
							selected = i;
							break;
						}
					}
				}
			}
			/*=====================================================*/
			if ((key == keys.end_1) ||
					(key == keys.end_2))
			{
				if (ManualLines < maxy - 1)
					manualpos = 0;
				else
					manualpos = ManualLines -(maxy - 1);

				selected = ManualLinks - 1;
			}
			/*=====================================================*/
			if ((key == keys.nextnode_1) ||
					(key == keys.nextnode_2))
			{
				for (unsigned i = manualpos + 1; i < ManualLines; i++)
				{
					if (manual[i][1] == 8)
					{
						manualpos = i;
						break;
					}
				}
			}
			/*=====================================================*/
			if ((key == keys.prevnode_1) ||
					(key == keys.prevnode_2))
			{
				for (unsigned i = manualpos - 1; i > 0; i--)
				{
					if (manual[i][1] == 8)
					{
						manualpos = i;
						break;
					}
				}
			}
			/*=====================================================*/
			if ((key == keys.pgdn_1) ||
					(key == keys.pgdn_2))
			{
				if (manualpos +(maxy - 2) < ManualLines -(maxy - 1))
				{
					manualpos +=(maxy - 2);
					rescan_selected();
				}
				else if (ManualLines -(maxy - 1) >= 1)
				{
					manualpos = ManualLines -(maxy - 1);
					selected = ManualLinks - 1;
				}
				else
				{
					manualpos = 0;
					selected = ManualLinks - 1;
				}
			}
			/*=====================================================*/
			if ((key == keys.home_1) || (key == keys.home_2))
			{
				manualpos = 0;
				rescan_selected();
			}
			/*=====================================================*/
			if ((key == keys.pgup_1) | (key == keys.pgup_2))
			{
				if (manualpos >(maxy - 1))
					manualpos -=(maxy - 1);
				else
					manualpos = 0;
				rescan_selected();
			}
			/*=====================================================*/
			/* top+bottom line \|/ */
			/* see keys.up for comments */
			if ((key == keys.twodown_1) || (key == keys.twodown_2))
			{
				ungetch(keys.down_1);
				ungetch(keys.down_1);
			}
			/*=====================================================*/
			/* top+bottom line \|/ */
			/* see keys.up for comments */
			if ((key == keys.down_1) || (key == keys.down_2))
			{
				selectedchanged = 0;
				for (unsigned i = selected + 1; i < ManualLinks; i++)
				{
					if ((manuallinks[i].line >= manualpos) &&
							(manuallinks[i].line < manualpos +(maxy - 2)))
					{
						selected = i;
						selectedchanged = 1;
						break;
					}
				}
				if (!selectedchanged)
				{
					if (manualpos < ManualLines -(maxy - 1))
						manualpos++;
					for (unsigned i = selected + 1; i < ManualLinks; i++)
					{
						if ((manuallinks[i].line >= manualpos) &&
								(manuallinks[i].line < manualpos +(maxy - 2)))
						{
							selected = i;
							selectedchanged = 1;
							break;
						}
					}
				}
			}
			/*=====================================================*/
			if ((key == keys.back_1) ||
					(key == keys.back_2))
			{
				if (manualhistorylength)
					return -2;
			}
			/*=====================================================*/
			if ((key == keys.followlink_1) ||
					(key == keys.followlink_2))
			{
				manualhistory[manualhistorylength].pos = manualpos;
				manualhistory[manualhistorylength].selected = selected;
				if (selected >= 0)
					if ((manuallinks[selected].line >= manualpos) &&
							(manuallinks[selected].line < manualpos +(maxy - 1)))
					{
						if (!strncmp(manuallinks[selected].section, "HTTPSECTION", 11))
						{
							int buflen;
							char *tempbuf = xmalloc(1024);
							strcpy(tempbuf, httpviewer);
							strcat(tempbuf, " ");
							buflen = strlen(tempbuf);
							construct_manualname(tempbuf + buflen, selected);
							myendwin();
							system(tempbuf);
							doupdate();
							xfree(tempbuf);
						}
						else if (!strncmp(manuallinks[selected].section, "FTPSECTION", 10))
						{
							int buflen;
							char *tempbuf = xmalloc(1024);
							strcpy(tempbuf, ftpviewer);
							strcat(tempbuf, " ");
							buflen = strlen(tempbuf);
							construct_manualname(tempbuf + buflen, selected);
							myendwin();
							system(tempbuf);
							doupdate();
							xfree(tempbuf);
						}
						else if (!strncmp(manuallinks[selected].section, "MAILSECTION", 11))
						{
							int buflen;
							char *tempbuf = xmalloc(1024);
							strcpy(tempbuf, maileditor);
							strcat(tempbuf, " ");
							buflen = strlen(tempbuf);
							construct_manualname(tempbuf + buflen, selected);
							myendwin();
							system(tempbuf);
							doupdate();
							xfree(tempbuf);
						}
						else
						{
							return selected;
						}
					}
			}
			/*=====================================================*/
			if ((key==keys.left_1)||(key==keys.left_2))
				if (manualcol>0) manualcol--;
			if ((key==keys.right_1)||(key==keys.right_2))
				manualcol++;
			/*=====================================================*/
			/********* end of keyboard handling *********************/
			/********* mouse handler ********************************/
#ifdef CURSES_MOUSE
			if (key == KEY_MOUSE)
			{
				MEVENT mouse;
				int done = 0;
				getmouse(&mouse);
				if (mouse.x<0 || mouse.y<0) /* should never happen, according to curses docs */
					continue;

				/* copy to unsigned vars to avoid all kinds of signed/unsigned comparison unpleasantness below */
				unsigned mouse_x = mouse.x;
				unsigned mouse_y = mouse.x;

				if (mouse.bstate == BUTTON1_CLICKED)
				{
					if ((mouse_y > 0) &&(mouse_y < maxy - 1))
					{
						for (int i = selected; i >= 0; i--)
						{
							if (manuallinks[i].line == mouse_y + manualpos - 1)
							{
								if (manuallinks[i].col <= mouse_x - 1)
								{
									if (manuallinks[i].col + strlen(manuallinks[i].name) >= mouse_x - 1)
									{
										selected = i;
										done = 1;
										break;
									}
								}
							}
						}
						if (!done)
							for (unsigned i = selected; i < ManualLinks; i++)
							{
								if (manuallinks[i].line == mouse_y + manualpos - 1)
								{
									if (manuallinks[i].col <= mouse_x - 1)
									{
										if (manuallinks[i].col + strlen(manuallinks[i].name) >= mouse_x - 1)
										{
											selected = i;
											done = 1;
											break;
										}
									}
								}
							}
					}		/* end: mouse not on top/bottom line */
					if (mouse_y == 0)
						ungetch(keys.up_1);
					if (mouse_y == maxy - 1)
						ungetch(keys.down_1);
				}		/* end: button_clicked */
				if (mouse.bstate == BUTTON1_DOUBLE_CLICKED)
				{
					if ((mouse_y > 0) &&(mouse_y < maxy - 1))
					{
						for (int i = selected; i >= 0; i--)
						{
							if (manuallinks[i].line == mouse_y + manualpos - 1)
							{
								if (manuallinks[i].col <= mouse_x - 1)
								{
									if (manuallinks[i].col + strlen(manuallinks[i].name) >= mouse_x - 1)
									{
										selected = i;
										done = 1;
										break;
									}
								}
							}
						}
						if (!done)
							for (unsigned i = selected; i < ManualLinks; i++)
							{
								if (manuallinks[i].line == mouse_y + manualpos - 1)
								{
									if (manuallinks[i].col <= mouse_x - 1)
									{
										if (manuallinks[i].col + strlen(manuallinks[i].name) >= mouse_x - 1)
										{
											selected = i;
											done = 1;
											break;
										}
									}
								}
							}
						if (done)
							ungetch(keys.followlink_1);
					}		/* end: mouse not at top/bottom line */
					if (mouse_y == 0)
						ungetch(keys.pgup_1);
					if (mouse_y == maxy - 1)
						ungetch(keys.pgdn_1);
				}		/* end: button doubleclicked */
			}
#endif /* CURSES_MOUSE */
			/*****************************************************************************/
		}
		if ((key == keys.quit_2) ||(key == keys.quit_1))
		{
			if (!ConfirmQuit)
				break;
			else
			{
				if (yesno(_("Are you sure you want to quit?"), QuitConfirmDefault))
					break;
			}
		}
	}
	closeprogram();
	return -1;
}

void
/* scan for some hyperlink, available on current screen */
rescan_selected()
{
	for (unsigned i = 0; i < ManualLinks; i++)
	{
		if ((manuallinks[i].line >= manualpos) &&
				(manuallinks[i].line < manualpos +(maxy - 1)))
		{
			selected = i;
			break;
		}
	}
}

/*
 * calculate from which to start displaying of manual line *man. Skip `mancol'
 * columns. But remember, that *man contains also nonprinteble characters for
 * boldface etc.
 */
char *getmancolumn(char *man, int mancol)
{
	if (mancol==0) return man;
	while (mancol>0)
	{ if (*(man+1) == 8) man+=3; else man++; mancol--; }
	return man;
}

/* show the currently visible part of manpage */
void
showmanualscreen()
{
#ifdef getmaxyx
	/* refresh maxy, maxx values */
	getmaxyx(stdscr, maxy, maxx);
#endif
	attrset(normal);
	/* print all visible text lines */
	for (unsigned i = manualpos;(i < manualpos +(maxy - 2)) &&(i < ManualLines); i++)
	{
		size_t len = strlen(manual[i]);
		if (len)
			manual[i][len - 1] = ' ';
		/* if we have something to display */
		if (len>manualcol)
			mvaddstr_manual((i - manualpos) + 1, 0, getmancolumn(manual[i],manualcol));
		else	/* otherwise, just clear the line to eol */
		{
			move((i - manualpos) + 1, 0);
			bkgdset(' ' | normal);
			clrtoeol();
		}
		if (len)
			manual[i][len - 1] = '\n';
	}
#ifdef HAVE_BKGDSET
	bkgdset(' ' | normal);
#endif
	/* and clear to bottom */
	clrtobot();
#ifdef HAVE_BKGDSET
	bkgdset(0);
#endif
	attrset(normal);
	/* add highlights */
	add_highlights();
	/* draw bottomline with user informations */
	attrset(bottomline);
	mymvhline(0, 0, ' ', maxx);
	mymvhline(maxy - 1, 0, ' ', maxx);
	move(maxy - 1, 0);
	if (((manualpos + maxy) < ManualLines) &&(ManualLines > maxy - 2))
		printw(_("Viewing line %d/%d, %d%%"),(manualpos - 1 + maxy), ManualLines,((manualpos - 1 + maxy) * 100) / ManualLines);
	else
		printw(_("Viewing line %d/%d, 100%%"), ManualLines, ManualLines);
	move(maxy - 1, 0);
	attrset(normal);
}

void
/* print a manual line */
mvaddstr_manual(int y, int x, char *str)
{
	int i, j, len = strlen(str);
	static char strippedline[1024];
	if ((h_regexp_num) ||(manual_aftersearch))
	{
		memcpy(strippedline, str, len + 1);
		strip_manual(strippedline);
	}
	move(y, x);
	for (i = 0; i < len; i++)
	{
		if ((i > 0) &&(i < len - 1))
		{
			/* handle bold highlight */
			if ((str[i] == 8) &&(str[i - 1] == '_'))
			{
				attrset(manualbold);
				addch(str[i] & 0xff);
				addch(str[i + 1] & 0xff);
				attrset(normal);
				i++;
				goto label_skip_other;
			}
			/*
			 * if it wasn't bold, check italic, before default, unhighlighted
			 * line will be painted.  We can do it only if i<len-3.
			 */
			else if (i < len - 3)
				goto label_check_italic;
			else /* unhighlighted */
			{
				addch(str[i] & 0xff);
				goto label_skip_other;
			}
		}
		/* italic highlight */
		if (i < len - 3)
		{
label_check_italic:
			if ((str[i + 1] == 8) &&(str[i + 2] == str[i]))
			{
				attrset(manualitalic);
				addch(str[i] & 0xff);
				i += 2;
				attrset(normal);
			}
			else
			{
				addch(str[i] & 0xff);
			}
		}
label_skip_other:;
	}
#ifdef HAVE_BKGDSET
	bkgdset(' ' | normal);
	clrtoeol();
	bkgdset(0);
#else
	myclrtoeol();
#endif
	attrset(normal);
#ifndef ___DONT_USE_REGEXP_SEARCH___
	if ((h_regexp_num) ||(manual_aftersearch))
	{
		regmatch_t pmatch[1];
		int maxregexp = manual_aftersearch ? h_regexp_num + 1 : h_regexp_num;

		/* if it is after search, then we have user defined regexps+
		   a searched regexp to highlight */
		for (j = 0; j < maxregexp; j++)
		{
			char *tmpstr = strippedline;
			while (!regexec(&h_regexp[j], tmpstr, 1, pmatch, 0))
			{
				int n = pmatch[0].rm_eo - pmatch[0].rm_so;
				int rx = pmatch[0].rm_so + tmpstr - strippedline;
				int curY, curX;
				char tmpchr;
				getyx(stdscr, curY, curX);
				tmpchr = strippedline[rx + n];
				strippedline[rx + n] = 0;
				attrset(searchhighlight);
				mvaddstr(y, rx, strippedline + rx);
				attrset(normal);
				strippedline[rx + n] = tmpchr;
				tmpstr = tmpstr + pmatch[0].rm_eo;
				move(curY, curX);
			}
		}
	}
#endif
}

/* add hyperobject highlights */
void
add_highlights()
{
	int i;
	/* scan through the visible objects */
	for (i = 0; (unsigned) i < ManualLinks; i++)
	{
		/* if the object is on the current screen */
		if ((manuallinks[i].line >= manualpos) &&
				(manuallinks[i].line < manualpos +(maxy - 2)))
		{
			/* if it's a simple man link */
			if (manuallinks[i].section_mark < HTTPSECTION)
			{
				if (i == selected)
					attrset(noteselected);
				else
					attrset(note);

				/* if it's a link split into two lines */
				if (manuallinks[i].carry == 1)
				{
					int x, y, ltline = manuallinks[i].line - 1;
					/* find the line, where starts the split link */
					char *tmpstr = strdup(manual[ltline]);
					size_t ltlinelen;
					char *newlinemark;
					/* remove boldfaces&italics */
					strip_manual(tmpstr);
					/* calculate the length of this line */
					ltlinelen = strlen(tmpstr);
					/* set this var to the last character of this line(to an '\n')*/
					newlinemark = tmpstr + ltlinelen - 1;
					getyx(stdscr, y, x);
					if (y > 2)
					{
#define TestCh tmpstr[ltlinelen]
						/* skip \n, -, and the at least one char... */
						if (ltlinelen > 2)
							ltlinelen -= 3;

						/*
						 * positon ltlinelen to the beginning of the link to be
						 * highlighted
						 */
						while ((isalpha(TestCh)) ||(TestCh == '.') ||(TestCh == '_'))
							ltlinelen--;

						*newlinemark = 0;
						/* OK, link horizontally fits into screen */
						if (ltlinelen>manualcol)
							mvaddstr(manuallinks[i].line - manualpos + 1 - 1,
									ltlinelen-manualcol, &tmpstr[ltlinelen]);
						/*
						 * we cut here a part of the link, and draw only what's
						 * visible on screen
						 */
						else if (ltlinelen+strlen(&tmpstr[ltlinelen])>manualcol)
							mvaddstr(manuallinks[i].line - manualpos + 1 - 1,
									ltlinelen-manualcol, &tmpstr[manualcol]);

						*newlinemark = '\n';
#undef TestCh
					}
					xfree(tmpstr);
					move(y, x);
				}
			}
			else
			{
				if (i == selected)
					attrset(urlselected);
				else
					attrset(url);
				if (manuallinks[i].carry == 1)
				{
					int ltline = manuallinks[i].line + 1;
					/*
					 * the split part to find is lying down
					 * to the line defined in manlinks(line+1)
					 */
					char *tmpstr = strdup(manual[ltline]);
					char *wsk = tmpstr, *wskend;
					strip_manual(tmpstr);
					/* skip spaces */
					while (isspace(*wsk))
						wsk++;
					/* find the end of url */
					wskend = findurlend(wsk);
					/* add end of string, and print */
					*wskend = 0;
					if (wsk-tmpstr<manualcol)
						mvaddstr(manuallinks[i].line - manualpos + 2, wsk - tmpstr - manualcol, wsk);
					else if (wskend-tmpstr<manualcol)
						mvaddstr(manuallinks[i].line - manualpos + 2, 0, wsk+manualcol);
				}
			}
			if (manuallinks[i].col>manualcol)
				mvaddstr(1 + manuallinks[i].line - manualpos,
						manuallinks[i].col - manualcol, manuallinks[i].name);
			else if (manuallinks[i].col+strlen(manuallinks[i].name)>manualcol)
				mvaddstr(1 + manuallinks[i].line - manualpos, 0,
						manuallinks[i].name+(manualcol-manuallinks[i].col));
			attrset(normal);
		}
	}
}

/* all variables passed here must have, say 10 bytes of overrun buffer */
void
strip_manual(char *buf)
{
	int i, tmpcnt = 0;
	/* in general, tmp buffer will hold a line stripped from highlight marks */
	for (i = 0; buf[i] != 0; i++)
	{
		/* so we strip the line from "'_',0x8" -- bold marks */
		if ((buf[i] == '_') &&(buf[i + 1] == 8))
		{
			buf[tmpcnt++] = buf[i + 2];
			i += 2;
		}
		/* and 0x8 -- italic marks */
		else if ((buf[i + 1] == 8) &&(buf[i + 2] == buf[i]))
		{
			buf[tmpcnt++] = buf[i];
			i += 2;
		}
		else /* else we don't do anything */
			buf[tmpcnt++] = buf[i];
	}
	buf[tmpcnt] = 0;
}

/*
 * checks if a construction, which looks like hyperlink, belongs to the allowed
 * manual sections.
 */
int
is_in_manlinks(char *in, char *find)
{
	char *copy, *token;
	const char delimiters[] = ":";

	copy = strdup(in);
	if ((strcmp(find,(token = strtok(copy, delimiters))) != 0))
	{
		while ((token = strtok(NULL, delimiters)))
		{
#ifdef HAVE_STRCASECMP
			if (!strcasecmp(token, find))
#else
				if (!strcmp(token, find))
#endif
				{
					xfree((void *) copy);
					return 0;
				}
		}
		xfree((void *) copy);
		return 1;
	}
	else
	{
		xfree((void *) copy);
		return 0;
	}
}

void
printmanual(char **Message, long Lines)
{
	/* printer fd */
	FILE *prnFD;
	int i;

	prnFD = popen(printutility, "w");

	/* scan through all lines */
	for (i = 0; i < Lines; i++)
	{
		fprintf(prnFD, "\r%s", Message[i]);
	}
	pclose(prnFD);
}

int
ishyphen(unsigned char ch)
{
	if ((ch == '-') ||(ch == SOFT_HYPHEN))
		return 1;
	return 0;
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/pinfo

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/pinfo

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master