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
|
/*
* Copyright (c) Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#if HAVE_SYS_QUEUE
# include <sys/queue.h>
#endif
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lowdown.h"
#include "extern.h"
#include "roff.h"
static ssize_t
roff_manpage_synopsis_prog_fl(struct nroff *, struct bnodeq *, size_t,
const struct lowdown_buf *, char **, int);
static ssize_t
roff_manpage_synopsis_prog_ar(struct nroff *, struct bnodeq *, size_t,
const struct lowdown_buf *, char **);
/*
* Concatenate the formatted output of "fmt" onto "buf", which may or
* may not already have been allocated. Returns TRUE on succes, FALSE
* on failure. On failure, "buf" is freed and set to NULL.
*/
static int __attribute__((format(printf, 2, 3)))
concatv(char **buf, const char *fmt, ...)
{
va_list ap;
char *fbuf, *outbuf;
int rc;
assert(buf != NULL);
/* First, create the desired output. */
va_start(ap, fmt);
rc = vasprintf(&fbuf, fmt, ap);
va_end(ap);
if (rc == -1) {
free(*buf);
*buf = NULL;
return 0;
}
/* If the destination is not set, just copy into it. */
if (*buf == NULL) {
*buf = fbuf;
return 1;
}
/* Otherwise, concatenate into a new buffer and return it. */
rc = asprintf(&outbuf, "%s%s", *buf, fbuf);
free(fbuf);
free(*buf);
if (rc == -1) {
*buf = NULL;
return 0;
}
*buf = outbuf;
return 1;
}
/*
* Parse a sequence [xxx -yyy zzz]. Returns <0 on failure (memory), 0
* if parsing fails, and the position otherwise. Set "out" to be the
* allocated content or NULL if there's no content.
*/
static ssize_t
roff_manpage_synopsis_prog_op(struct nroff *st, struct bnodeq *nq,
size_t pos, const struct lowdown_buf *buf, char **out)
{
ssize_t ssz;
char *cp, *ncp;
int rc;
size_t mspace = 0;
*out = NULL;
/* Skip past open bracket and opening space... */
for (++pos; pos < buf->size; pos++)
if (!isspace((unsigned char)buf->data[pos]))
break;
/* Continue parsing expressions til the closing bracket. */
while (pos < buf->size && buf->data[pos] != ']') {
if (hbuf_strncasecmpat(buf, "\\(en", pos) ||
buf->data[pos] == '-') {
ssz = roff_manpage_synopsis_prog_fl
(st, nq, pos, buf, &cp, 0);
if (ssz <= 0)
return ssz;
rc = st->type == LOWDOWN_MDOC ?
asprintf(&ncp, "Fl %s",
cp == NULL ? "" : cp) :
asprintf(&ncp, "\\fB%s\\fR",
cp == NULL ? "" : cp);
} else if (buf->data[pos] == '[') {
ssz = roff_manpage_synopsis_prog_op
(st, nq, pos, buf, &cp);
if (ssz <= 0)
return ssz;
rc = st->type == LOWDOWN_MDOC ?
asprintf(&ncp, "Oo %s Oc",
cp == NULL ? "" : cp) :
asprintf(&ncp, "[%s]",
cp == NULL ? "" : cp);
} else if (buf->data[pos] == '|') {
cp = NULL;
rc = (ncp = strdup("|")) == NULL ? -1 : 1;
ssz = pos + 1;
} else {
ssz = roff_manpage_synopsis_prog_ar
(st, nq, pos, buf, &cp);
if (ssz <= 0)
return ssz;
rc = st->type == LOWDOWN_MDOC ?
asprintf(&ncp, "Ar %s",
cp == NULL ? "" : cp) :
asprintf(&ncp, "\\fI%s\\fR",
cp == NULL ? "" : cp);
}
free(cp);
if (rc == -1) {
free(*out);
return -1;
}
/* Either set "out" or concatenate to it. */
if (*out != NULL) {
rc = st->type == LOWDOWN_MDOC ?
concatv(out, "%s%s", mspace == 0 ?
" Ns " : " ", ncp) :
concatv(out, "%s%s", mspace == 0 ?
"" : " ", ncp);
free(ncp);
if (rc == 0)
return -1;
} else
*out = ncp;
/* Skip to the next non-space. */
mspace = 0;
pos = ssz;
while (pos < buf->size &&
isspace((unsigned char)buf->data[pos])) {
mspace++;
pos++;
}
}
/* Either have a matching bracket or bad data. */
if (pos < buf->size && buf->data[pos] == ']')
return ++pos;
free(*out);
*out = NULL;
return 0;
}
/*
* Parse an opaque argument. There are some special cases like
* "file..." and such, and also the vertical bar. Returns <0 on failure
* (memory), 0 if parsing fails, and the position otherwise. Set "out"
* to be the allocated content or NULL if there's no content.
*/
static ssize_t
roff_manpage_synopsis_prog_ar(struct nroff *st, struct bnodeq *nq,
size_t pos, const struct lowdown_buf *buf, char **out)
{
size_t start;
char *cp = NULL;
*out = NULL;
/* Skip past opening space... */
for ( ; pos < buf->size; pos++)
if (!isspace((unsigned char)buf->data[pos]))
break;
/*
* Intercept special cases: `file...` and equivalents go
* directly back with no content for mdoc(7). FIXME: this
* catches the fancy ellipsis for the case of `file...`, but not
* for the general case of e.g. `arg...`.
*/
if (hbuf_strncasecmpat(buf, "file \\[u2026]", pos)) {
pos += 13;
if (st->type == LOWDOWN_MAN &&
(cp = strdup("file ...")) == NULL)
return -1;
} else if (hbuf_strncasecmpat(buf, "file ...", pos)) {
pos += 8;
if (st->type == LOWDOWN_MAN &&
(cp = strdup("file ...")) == NULL)
return -1;
} else if (hbuf_strncasecmpat(buf, "file\\[u2026]", pos)) {
pos += 12;
if (st->type == LOWDOWN_MAN &&
(cp = strdup("file...")) == NULL)
return -1;
} else if (hbuf_strncasecmpat(buf, "file...", pos)) {
pos += 7;
if (st->type == LOWDOWN_MAN &&
(cp = strdup("file...")) == NULL)
return -1;
} else if (hbuf_strncasecmpat(buf, "\\[u2026]", pos)) {
pos += 8;
if ((cp = strdup("...")) == NULL)
return -1;
} else {
for (start = pos; pos < buf->size; pos++) {
if ('\\' == buf->data[pos] &&
pos + 1 < buf->size &&
buf->data[pos + 1] == '[') {
for (++pos; pos < buf->size; pos++)
if (buf->data[pos] == ']')
break;
if (pos == buf->size)
break;
continue;
}
if (isspace((unsigned char)buf->data[pos]) ||
buf->data[pos] == '[' ||
buf->data[pos] == ']' ||
buf->data[pos] == '|')
break;
}
if ((cp = hbuf_stringn(buf, start, pos)) == NULL)
return -1;
}
*out = cp;
return pos;
}
/*
* Parse a flag. Flags are longform if they start with \(en or --
* instead of -. There are some special cases like the vertical bar.
* Returns <0 on failure (memory), 0 if parsing fails, and the position
* otherwise. The position should NOT skip over white-space.
*/
static ssize_t
roff_manpage_synopsis_prog_fl(struct nroff *st, struct bnodeq *nq,
size_t pos, const struct lowdown_buf *buf, char **out, int first)
{
size_t start, save_end;
ssize_t ssz;
char *ncp;
int ns = 0, rc, longform = 0;
*out = NULL;
/*
* Whether starting with "-" or "--". If this is man(7), print
* all the "-". In mdoc(7), the surrounding `Fl` will output
* one.
*/
if (hbuf_strncasecmpat(buf, "\\(en", pos)) {
longform = 1;
pos += 4;
} else if (hbuf_strncasecmpat(buf, "--", pos)) {
longform = 1;
pos += 2;
} else
pos += 1;
if (st->type == LOWDOWN_MAN)
longform++;
/*
* Read until initial end-of-expression and allocate the flag.
* The endpoint might be adjusted later when we test if there
* are trailing flag expressions.
*/
for (start = pos; pos < buf->size; pos++)
if (isspace((unsigned char)buf->data[pos]) ||
buf->data[pos] == '=' ||
buf->data[pos] == '|' ||
buf->data[pos] == '[' ||
buf->data[pos] == ']')
break;
save_end = pos;
if ((ncp = hbuf_stringn(buf, start, pos)) == NULL)
return -1;
if (asprintf(out, "%s%s%s", longform > 1 ? "\\-" : "",
longform > 0 ? "\\-" : "", ncp) == -1) {
*out = NULL;
free(ncp);
return -1;
}
free(ncp);
/*
* Scan ahead: is there something after that's part of the flag?
* For example, `-f flag` or, in the no-space case, `-f=bar`?
* Or even worse, `-f[=foo]`?
*/
ns = pos < buf->size && !isspace((unsigned char)buf->data[pos]);
while (pos < buf->size && isspace((unsigned char)buf->data[pos]))
pos++;
/*
* If invoked at the beginning of subexpressions, don't continue
* trying to consume arguments. This is a common idiom where a
* command is broken into multiple expressions with a different
* leading flag (e.g., cpio). But only do so if there's space
* between the flag and argument.
*/
if (first && !ns)
return pos;
/*
* Special-case: if there's an equal sign flush-left like -f=foo
* or -f=[bar], or really anything that's not white-space after
* the equal sign, then don't render the equal sign as part of
* the argument.
*/
if (ns && pos + 1 < buf->size && buf->data[pos] == '=' &&
!isspace((unsigned char)buf->data[pos + 1])) {
pos++;
rc = st->type == LOWDOWN_MDOC ?
concatv(out, " Ns =") : concatv(out, "=");
}
/* If followed by an option, consider it part of the flag. */
if (pos < buf->size && buf->data[pos] == '[') {
ssz = roff_manpage_synopsis_prog_op(st, nq, pos,
buf, &ncp);
if (ssz <= 0)
return ssz;
rc = st->type == LOWDOWN_MDOC ?
concatv(out, "%sOo %s Oc", ns ? " Ns " : " ",
ncp == NULL ? "" : ncp) :
concatv(out, "%s[%s]", ns ? "" : " ",
ncp == NULL ? "" : ncp);
free(ncp);
if (rc == 0)
return -1;
return ssz;
}
/* If followed by an argument, consider it part of the flag. */
if (pos < buf->size &&
buf->data[pos] != '[' && buf->data[pos] != ']' &&
buf->data[pos] != '|' && buf->data[pos] != '-' &&
!hbuf_strncasecmpat(buf, "\\(en", pos)) {
ssz = roff_manpage_synopsis_prog_ar(st, nq, pos,
buf, &ncp);
if (ssz <= 0)
return ssz;
rc = st->type == LOWDOWN_MDOC ?
concatv(out, "%sAr %s", ns ? " Ns " : " ",
ncp == NULL ? "" : ncp) :
concatv(out, "%s\\fI%s\\fR", ns ? "" : " ",
ncp == NULL ? "" : ncp);
free(ncp);
if (rc == 0)
return -1;
return ssz;
}
/*
* If neither trailing options are correct, revert to the
* initial saved ending point.
*/
return save_end;
}
/*
* Parse a subexpression, which is basically anything that comes after
* the name invocation. Returns <0 on failure (memory), 0 if parsing
* fails, and the position otherwise.
*/
static ssize_t
roff_manpage_synopsis_prog_subexpr(struct nroff *st, struct bnodeq *nq,
size_t pos, const struct lowdown_buf *buf, int first)
{
ssize_t ssz = -1;
struct bnode *bn;
char *cp = NULL;
/* Strip away leading space. */
for ( ; pos < buf->size; pos++)
if (!isspace((unsigned char)buf->data[pos]))
break;
if (pos == buf->size)
return pos;
/* Check illegal characters. */
if (buf->data[pos] == ']')
return 0;
/*
* A difficult case. The vertical bar is on the top-level of the
* flags, so something like `foo -s | -v`. This isn't handled
* in the Fl case because it doesn't look ahead to the
* subsequent -v, so it's handled here. Don't use just a
* vertical bar because that will confuse the "magic" in
* bqueue_flush(), which looks at the span after a semiblock to
* see if it should output `Ns`.
*/
if (buf->data[pos] == '|') {
if (st->type == LOWDOWN_MDOC &&
bqueue_sblock(nq, ".No |") == NULL)
return -1;
if (st->type == LOWDOWN_MAN &&
bqueue_span(nq, "| ") == NULL)
return -1;
return pos + 1;
}
/*
* Subexpressions can either begin with a hyphen (or a long
* hyphen) for Fl, a square bracket for Oo/Oc, or anything else
* is routed to Ar.
*/
if (st->type == LOWDOWN_MDOC) {
if (hbuf_strncasecmpat(buf, "\\(en", pos) ||
buf->data[pos] == '-') {
if ((bn = bqueue_sblock(nq, ".Fl")) == NULL)
goto out;
ssz = roff_manpage_synopsis_prog_fl
(st, nq, pos, buf, &cp, first);
} else if (buf->data[pos] == '[') {
if ((bn = bqueue_sblock(nq, ".Op")) == NULL)
goto out;
ssz = roff_manpage_synopsis_prog_op
(st, nq, pos, buf, &cp);
} else {
if ((bn = bqueue_sblock(nq, ".Ar")) == NULL)
goto out;
ssz = roff_manpage_synopsis_prog_ar
(st, nq, pos, buf, &cp);
}
if (ssz > 0)
bn->nargs = cp;
cp = NULL;
} else {
if (hbuf_strncasecmpat(buf, "\\(en", pos) ||
buf->data[pos] == '-') {
ssz = roff_manpage_synopsis_prog_fl
(st, nq, pos, buf, &cp, first);
if (ssz <= 0)
goto out;
if (!bqueue_font_mod(st, nq, 0, NFONT_BOLD) ||
(cp != NULL && bqueue_spann(nq, cp) == NULL))
goto out;
cp = NULL;
if (!bqueue_font_mod(st, nq, 1, NFONT_BOLD) ||
(first && bqueue_span(nq, "\n") == NULL))
goto out;
} else if (buf->data[pos] == '[') {
ssz = roff_manpage_synopsis_prog_op
(st, nq, pos, buf, &cp);
if (ssz <= 0)
goto out;
if (bqueue_span(nq, "[") == NULL ||
(cp != NULL && bqueue_spann(nq, cp) == NULL))
goto out;
cp = NULL;
if (bqueue_span(nq, "]") == NULL ||
(first && bqueue_span(nq, "\n") == NULL))
goto out;
} else {
ssz = roff_manpage_synopsis_prog_ar
(st, nq, pos, buf, &cp);
if (ssz <= 0)
goto out;
if (!bqueue_font_mod(st, nq, 0, NFONT_ITALIC) ||
(cp != NULL && bqueue_spann(nq, cp) == NULL))
goto out;
cp = NULL;
if (!bqueue_font_mod(st, nq, 1, NFONT_ITALIC) ||
(first && bqueue_span(nq, "\n") == NULL))
goto out;
}
}
out:
free(cp);
return ssz;
}
/*
* Parse a single #include statement. Returns the position in the
* buffer if >0, 0 if the parse failed, or -1 on failure.
*/
static ssize_t
roff_manpage_synopsis_func_incl(struct nroff *st, struct bnodeq *nq,
size_t pos, const struct lowdown_buf *buf)
{
char *cp;
size_t start;
pos += 9;
while (pos < buf->size &&
isspace((unsigned char)buf->data[pos]))
pos++;
if (pos < buf->size && buf->data[pos] == '<')
pos++;
for (start = pos; pos < buf->size; pos++)
if (buf->data[pos] == '>' ||
isspace((unsigned char)buf->data[pos]))
break;
if ((cp = hbuf_stringn(buf, start, pos)) == NULL)
return -1;
if (st->type == LOWDOWN_MDOC) {
if (bqueue_blockn(nq, ".In", cp) == NULL)
return -1;
} else {
if (bqueue_block(nq, ".sp") == NULL) {
free(cp);
return -1;
}
if (!bqueue_font_mod(st, nq, 0, NFONT_BOLD) ||
bqueue_span(nq, "#include <") == NULL ||
bqueue_span(nq, cp) == NULL ||
bqueue_span(nq, ">") == NULL) {
free(cp);
return -1;
}
if (!bqueue_font_mod(st, nq, 1, NFONT_BOLD) ||
bqueue_span(nq, "\n") == NULL)
return -1;
}
if (pos < buf->size && buf->data[pos] == '>')
pos++;
while (pos < buf->size &&
isspace((unsigned char)buf->data[pos]))
pos++;
return pos;
}
/*
* Parse a single function declaration. Returns the position in the
* buffer if >0, 0 if the parse failed, or -1 on failure.
*/
static ssize_t
roff_manpage_synopsis_func_decl(struct nroff *st, struct bnodeq *nq,
size_t pos, const struct lowdown_buf *buf)
{
size_t i, paren, arg, nest;
char *cp, *ncp;
int first = 1;
/* Skip over leading space. */
while (pos < buf->size &&
isspace((unsigned char)buf->data[pos]))
pos++;
/*
* Get the start of the function part. If it doesn't exist,
* assume that this is a variable type.
*/
for (paren = pos; paren < buf->size; paren++)
if (buf->data[paren] == '(')
break;
if (paren == buf->size || paren == pos) {
if (bqueue_blockn(nq, ".Vt",
hbuf_stringn(buf, pos, buf->size)) == NULL)
return -1;
return buf->size;
}
/* Backtrack to the function name start. */
for (i = paren; i > pos; i--)
if (isspace((unsigned char)buf->data[i]))
break;
/*
* The type is what came before the function name from the start
* of the expression. If doesn't need to exist.
*/
if (st->type == LOWDOWN_MAN)
if (bqueue_block(nq, ".sp") == NULL)
return -1;
if (i > pos) {
if ((cp = hbuf_stringn(buf, pos, i)) == NULL)
return -1;
if (st->type == LOWDOWN_MDOC) {
if (bqueue_blockn(nq, ".Ft", cp) == NULL)
return -1;
} else {
if (!bqueue_font_mod(st, nq, 0, NFONT_ITALIC)) {
free(cp);
return -1;
}
if (bqueue_span(nq, cp) == NULL ||
!bqueue_font_mod(st, nq, 1, NFONT_ITALIC) ||
bqueue_span(nq, "\n") == NULL ||
bqueue_block(nq, ".br") == NULL ||
bqueue_block(nq, ".in +4") == NULL ||
bqueue_block(nq, ".ti -4") == NULL)
return -1;
}
pos = i + 1;
}
/* Output the function name start. */
if ((cp = hbuf_stringn(buf, pos, paren)) == NULL)
return -1;
if (st->type == LOWDOWN_MDOC) {
if (bqueue_blockn(nq, ".Fo", cp) == NULL)
return -1;
} else {
if (!bqueue_font_mod(st, nq, 0, NFONT_BOLD) ||
bqueue_span(nq, cp) == NULL) {
free(cp);
return -1;
}
if (!bqueue_font_mod(st, nq, 1, NFONT_BOLD))
return -1;
}
/* If the function is truncated, just close it out. */
if (paren == buf->size) {
if (st->type == LOWDOWN_MDOC) {
if (bqueue_block(nq, ".Fc") == NULL)
return -1;
} else {
if (bqueue_span(nq, "\n") == NULL)
return -1;
}
return paren;
}
/* Read each comma-separated argument. */
nest = 0;
for (pos = paren + 1, arg = pos; pos < buf->size; pos++) {
if (buf->data[pos] == ')' && nest > 0) {
nest--;
continue;
}
if (buf->data[pos] == '(')
nest++;
if (buf->data[pos] != ',' && buf->data[pos] != ')')
continue;
/*
* The end of the sequence is a closing parenthesis
* followed by optional space, then an optional
* semicolon. Preserve our position, because this
* ending part shouldn't be in the trailing `Fa`.
*/
i = pos;
if (buf->data[i] == ')') {
assert(nest == 0);
for (++i; i < buf->size; i++)
if (!isspace
((unsigned char)buf->data[i]))
break;
if (i < buf->size && buf->data[i] == ';')
i++;
for ( ; i < buf->size; i++)
if (!isspace
((unsigned char)buf->data[i]))
break;
if (i < buf->size)
return 0;
}
/*
* From the last saved position, copy the text from then
* til the comma and strip away any whitespace. Elide
* empty arguments.
*/
if ((cp = hbuf_stringn_trim(buf, arg, pos)) == NULL)
return -1;
if (*cp != '\0' && st->type == LOWDOWN_MDOC) {
if (bqueue_blocknv(nq, ".Fa", "\"%s\"", cp) ==
NULL) {
free(cp);
return -1;
}
} else if (*cp != '\0' && st->type == LOWDOWN_MAN) {
if (asprintf(&ncp, "%s\\fI%s\\fR",
first ? "(" : ", ", cp) == -1) {
free(cp);
return -1;
} else if (bqueue_span(nq, ncp) == NULL) {
free(cp);
free(ncp);
return -1;
}
}
free(cp);
first = 0;
/*
* Set the position to the end of the sequence, and arg
* as the start of the next one.
*/
pos = i;
arg = pos + 1;
}
if (st->type == LOWDOWN_MDOC) {
if (bqueue_block(nq, ".Fc") == NULL)
return -1;
return pos;
}
if (bqueue_span(nq, ");\n") == NULL ||
bqueue_block(nq, ".in -4") == NULL)
return -1;
return pos;
}
/*
* Parse an expression in a function SYNOPSIS, that is, an include
* statement or a function declaration. Return -1 on failure (memory),
* 0 if the parse failed (not a valid expression), or the current
* position if >0 on success.
*/
static ssize_t
roff_manpage_synopsis_func_expr(struct nroff *st, struct bnodeq *nq,
size_t pos, const struct lowdown_buf *buf)
{
ssize_t ssz;
for ( ; pos < buf->size; pos++)
if (!isspace((unsigned char)buf->data[pos]))
break;
/*
* Following the name and any whitespace are the subexpressions
* that define the arguments. Consume all of them.
*/
while (pos < buf->size) {
if (hbuf_strncasecmpat(buf, "#include ", pos))
ssz = roff_manpage_synopsis_func_incl(st, nq, pos, buf);
else
ssz = roff_manpage_synopsis_func_decl(st, nq, pos, buf);
if (ssz <= 0)
return ssz;
pos = ssz;
}
return pos;
}
/*
* Parse a full synopsis expression. Returns <0 on failure (memory), 0
* if parsing fails, and the position otherwise.
*/
static ssize_t
roff_manpage_synopsis_prog_expr(struct nroff *st, struct bnodeq *nq,
size_t pos, const struct lowdown_buf *buf)
{
size_t sta;
ssize_t ssz;
/*
* Each synopsis expression consists of a name at the beginning
* of the line, possibly following whitespace.
*/
for ( ; pos < buf->size; pos++)
if (!isspace((unsigned char)buf->data[pos]))
break;
for (sta = pos; pos < buf->size; pos++)
if (isspace((unsigned char)buf->data[pos]))
break;
if (pos == buf->size)
return pos;
/* mdoc(7) has Nm followed by stuff; man(7) has SY. */
/* FIXME: man(7) use IP if traditional. */
if (bqueue_blockn(nq, st->type == LOWDOWN_MDOC ? ".Nm" : ".SY",
hbuf_stringn(buf, sta, pos)) == NULL)
return -1;
/*
* Following the name and any whitespace are the subexpressions
* that define the arguments. Consume all of them.
*/
while (pos < buf->size) {
ssz = roff_manpage_synopsis_prog_subexpr(st, nq, pos, buf, 1);
if (ssz <= 0)
return ssz;
pos = ssz;
}
/* FIXME: don't use if traditional. */
if (st->type == LOWDOWN_MAN && bqueue_block(nq, ".YS") == NULL)
return -1;
return pos;
}
/*
* Route into roff_manpage_synopsis_func_expr() with parsed buffer.
* Returns 0 on failure (memory), 1 on success. On success, the
* paragraph content may have been replaced.
*/
static int
roff_manpage_synopsis_func(struct nroff *st, const struct lowdown_node *n,
struct bnodeq *obq, const struct bnodeq *nbq)
{
int rc = -1;
struct lowdown_buf *buf = NULL;
struct bnodeq nq;
ssize_t ret;
TAILQ_INIT(&nq);
if ((buf = hbuf_new(32)) != NULL &&
bqueue_flush(st, buf, nbq, 2)) {
ret = roff_manpage_synopsis_func_expr(st, &nq, 0, buf);
if (ret > 0) {
TAILQ_CONCAT(obq, &nq, entries);
rc = 1;
} else if (ret == 0)
rc = 0;
}
hbuf_free(buf);
bqueue_free(&nq);
return rc;
}
/*
* Route into roff_manpage_synopsis_prog_expr() with parsed buffer.
* Returns 0 on failure (memory), 1 on success. On success, the
* paragraph content may have been replaced.
*/
static int
roff_manpage_synopsis_prog(struct nroff *st, const struct lowdown_node *n,
struct bnodeq *obq, const struct bnodeq *nbq)
{
int rc = -1;
struct lowdown_buf *buf = NULL;
struct bnodeq nq;
ssize_t ret;
TAILQ_INIT(&nq);
if ((buf = hbuf_new(32)) != NULL &&
bqueue_flush(st, buf, nbq, 2)) {
ret = roff_manpage_synopsis_prog_expr(st, &nq, 0, buf);
if (ret > 0) {
TAILQ_CONCAT(obq, &nq, entries);
rc = 1;
} else if (ret == 0)
rc = 0;
}
hbuf_free(buf);
bqueue_free(&nq);
return rc;
}
/*
* SEE ALSO sections in mdoc(7) conventionally contain only lists of
* other manpages. They can, however, also contain actual content. Try
* to catch the list of manpages and format them with `Xr` here. Return
* -1 on total failure (memory), 0 if the paragraph was not a list of
* manpages, and 1 if it was.
*/
static int
rndr_manpage_see_also(struct nroff *st, const struct lowdown_node *n,
struct bnodeq *obq, const struct bnodeq *nbq)
{
int rc = 0;
struct lowdown_buf *buf = NULL;
struct bnodeq nq;
size_t pos, end, start;
const char *openp;
struct bnode *bn;
TAILQ_INIT(&nq);
if ((buf = hbuf_new(32)) == NULL ||
!bqueue_flush(st, buf, nbq, 2))
goto out;
for (pos = 0; pos < buf->size; pos++)
if (!isspace((unsigned char)buf->data[pos]))
break;
/*
* Tokenise along commas or the eoln, then break apart the
* contents to see if it matches foo(section). If a single
* token doesn't, then bail out and assume this is a regular
* paragraph. Otherwise, format the link in an Xr. Skip over
* empty tokens.
*/
for (start = pos; pos <= buf->size; ) {
if (pos == buf->size || buf->data[pos] == ',') {
if (pos == start) {
/* Skip empty tokens. */
for (++pos; pos < buf->size; pos++)
if (!isspace((unsigned char)
buf->data[pos]))
break;
start = pos;
continue;
}
/* Trim trailing spaces. */
for (end = pos--; pos > start; pos--)
if (!isspace((unsigned char)
buf->data[pos]))
break;
/* Make sure there are parentheses. */
if (buf->data[pos] != ')')
goto out;
openp = memchr(&buf->data[start], '(',
pos - start);
if (openp == NULL)
goto out;
bn = st->type == LOWDOWN_MDOC ?
bqueue_sblock(obq, ".Xr") :
bqueue_sblock(obq, ".MR");
if (bn == NULL) {
rc = -1;
goto out;
}
if (asprintf(&bn->nargs, "%.*s %.*s%s",
(int)(openp - &buf->data[start]),
&buf->data[start],
(int)(&buf->data[pos] - openp - 1),
openp + 1,
end < buf->size ? " ," : "") == -1) {
bn->nargs = NULL;
rc = -1;
goto out;
}
if (end == buf->size)
break;
/* Trim leading spaces. */
for (pos = end + 1; pos < buf->size; pos++)
if (!isspace((unsigned char)
buf->data[pos]))
break;
start = pos;
} else
pos++;
}
TAILQ_CONCAT(obq, &nq, entries);
rc = 1;
out:
hbuf_free(buf);
bqueue_free(&nq);
return rc;
}
/*
* The NAME section consists of one or more comma-separated names
* followed by a dash (or fancy dash) then a description. Split the
* names into `Nm` (or bold) statements and, if found, the description
* into an `Nd` (or undecorated). The regressive case of a single word
* with no hyphens is just the `Nm` (or nothing). This returns -1 on
* failure (memory), 1 on success, and 0 if the section failed to parse
* as a NAME. (This currently doesn't actually happen.)
*/
static int
rndr_manpage_name(struct nroff *st, const struct lowdown_node *n,
struct bnodeq *obq, const struct bnodeq *nbq)
{
struct bnode *bn;
struct lowdown_buf *buf = NULL;
int rc = -1, has_next;
size_t pos, start;
char *cp = NULL;
void *p;
/* Flush everything into a single, un-decorated line. */
if ((buf = hbuf_new(32)) == NULL ||
!bqueue_flush(st, buf, nbq, 2))
goto out;
/* In man(7), break after the NAME section. */
if (st->type == LOWDOWN_MAN && bqueue_span(obq, "\n") == NULL)
goto out;
/*
* Skip white-space then output an name for each token leading
* up to a comma, a hyphen (or pretty hyphen), or the end of
* line.
*/
for (pos = 0; pos < buf->size; pos++)
if (!isspace((unsigned char)buf->data[pos]))
break;
for (start = pos; pos <= buf->size; ) {
if (pos != buf->size &&
buf->data[pos] != ',' &&
buf->data[pos] != '-' &&
!hbuf_strncasecmpat(buf, "\\(en", pos) &&
!hbuf_strncasecmpat(buf, "\\(em", pos)) {
pos++;
continue;
}
has_next = pos < buf->size && buf->data[pos] == ',';
cp = hbuf_stringn_trim(buf, start, pos);
if (cp == NULL)
goto out;
p = reallocarray(st->names, st->namesz + 1, sizeof(char *));
if (p == NULL)
goto out;
st->names = p;
if ((st->names[st->namesz++] = strdup(cp)) == NULL)
goto out;
/* For mdoc(7), use `Nm`; for man(7), bold. */
if (*cp != '\0' && st->type == LOWDOWN_MDOC) {
if (bqueue_blocknv(obq, ".Nm", "%s%s",
cp, has_next ? " ," : "") == NULL)
goto out;
free(cp);
cp = NULL;
} else if (*cp != '\0' && st->type == LOWDOWN_MAN) {
if (!bqueue_font_mod(st, obq, 0, NFONT_BOLD))
goto out;
if (bqueue_spann(obq, cp) == NULL) {
cp = NULL;
goto out;
}
cp = NULL;
if (!bqueue_font_mod(st, obq, 1, NFONT_BOLD))
goto out;
if (has_next &&
bqueue_span(obq, ",\n") == NULL)
goto out;
} else {
free(cp);
cp = NULL;
}
/* Stop if no more name tokens. */
if (pos == buf->size || buf->data[pos] != ',')
break;
/* Read after whitespace, then try again... */
for (++pos; pos < buf->size; pos++)
if (!isspace((unsigned char)buf->data[pos]))
break;
start = pos;
}
/*
* Read past delimiter (which is either a sequence of hyphens or
* a fancy en or em, length 4) between name and description.
*/
if (pos < buf->size && buf->data[pos] == '-') {
while (pos < buf->size && buf->data[pos] == '-')
pos++;
} else if (pos < buf->size)
pos += 4;
/* In man(7), add the delimiter. */
if (st->type == LOWDOWN_MAN &&
bqueue_span(obq, " \\(en ") == NULL)
goto out;
/* Conditionally output `Nd` or just text. */
if (pos < buf->size) {
cp = hbuf_stringn_trim(buf, pos, buf->size);
if (cp == NULL)
goto out;
if (*cp != '\0' && st->type == LOWDOWN_MDOC) {
if ((bn = bqueue_block(obq, ".Nd")) == NULL)
goto out;
bn->nargs = cp;
} else if (*cp != '\0' && st->type == LOWDOWN_MAN) {
if (bqueue_span(obq, cp) == NULL)
goto out;
free(cp);
} else
free(cp);
cp = NULL;
}
rc = 1;
out:
hbuf_free(buf);
free(cp);
return rc;
}
static ssize_t
roff_manpage_inline_func(struct nroff *st, struct bnodeq *nq,
size_t pos, const struct lowdown_buf *buf)
{
char *paren, *cp, *start;
struct bnode *bn;
for ( ; pos < buf->size; pos++)
if (!isspace((unsigned char)buf->data[pos]))
break;
if (pos == buf->size)
return pos;
if (hbuf_ismanpage(buf)) {
paren = memchr(&buf->data[pos], '(', buf->size - pos);
assert(paren != NULL);
bn = st->type == LOWDOWN_MDOC ?
bqueue_sblock(nq, ".Xr") :
bqueue_block(nq, ".MR");
if (bn == NULL)
return -1;
if (asprintf(&bn->nargs, "%.*s %.*s",
(int)(paren - &buf->data[pos]),
&buf->data[pos],
(int)(&buf->data[buf->size - 1] - (paren + 1)),
paren + 1) == -1) {
bn->nargs = NULL;
return -1;
}
return buf->size;
}
if (buf->data[buf->size - 1] == ')') {
paren = memchr(&buf->data[pos], '(', buf->size - pos);
if (paren == NULL)
return 0;
cp = NULL;
if (!concatv(&cp, st->type == LOWDOWN_MDOC ?
"%.*s" : "\\fB%.*s\\fP(",
(int)(paren - &buf->data[pos]), &buf->data[pos]))
return -1;
paren++;
while (*paren != ')') {
while (isspace((unsigned char)*paren))
paren++;
start = paren;
while (*paren != ',' && *paren != ')')
paren++;
if (!concatv(&cp, st->type == LOWDOWN_MDOC ?
" \"%.*s\"" : "\\fI%.*s\\fP,",
(int)(paren - start), start))
return -1;
if (*paren != ')')
start = ++paren;
}
if (st->type == LOWDOWN_MAN) {
if (cp[strlen(cp) - 1] == ',')
cp[strlen(cp) - 1] = '\0';
if (!concatv(&cp, ")"))
return -1;
if (bqueue_span(nq, cp) == NULL) {
free(cp);
return -1;
}
free(cp);
} else
if (bqueue_sblockn(nq, ".Fn", cp) == NULL)
return -1;
return buf->size;
}
if (st->type == LOWDOWN_MDOC &&
bqueue_sblockn(nq, ".Fa", hbuf_string_trim(buf)) == NULL)
return -1;
if (st->type == LOWDOWN_MAN) {
if ((cp = hbuf_string_trim(buf)) == NULL)
return -1;
bn = bqueue_spanv(nq, "\\fI%s\\fP", cp);
free(cp);
if (bn == NULL)
return -1;
}
return buf->size;
}
/*
* Parse a full synopsis expression. Returns <0 on failure (memory), 0
* if parsing fails, and the position otherwise.
*/
static ssize_t
roff_manpage_inline_prog(struct nroff *st, struct bnodeq *nq,
size_t pos, const struct lowdown_buf *buf)
{
ssize_t ssz;
size_t start;
char *paren;
struct bnode *bn;
for ( ; pos < buf->size; pos++)
if (!isspace((unsigned char)buf->data[pos]))
break;
if (pos == buf->size)
return pos;
if (hbuf_ismanpage(buf)) {
paren = memchr(&buf->data[pos], '(', buf->size - pos);
assert(paren != NULL);
bn = st->type == LOWDOWN_MDOC ?
bqueue_sblock(nq, ".Xr") :
bqueue_block(nq, ".MR");
if (bn == NULL)
return -1;
if (asprintf(&bn->nargs, "%.*s %.*s",
(int)(paren - &buf->data[pos]),
&buf->data[pos],
(int)(&buf->data[buf->size - 1] - (paren + 1)),
paren + 1) == -1) {
bn->nargs = NULL;
return -1;
}
return buf->size;
}
start = pos;
while (pos < buf->size) {
/* In-line man(7) needs a separator. */
if (pos > start && st->type == LOWDOWN_MAN &&
bqueue_span(nq, " ") == NULL)
return -1;
ssz = roff_manpage_synopsis_prog_subexpr(st, nq, pos, buf, 0);
if (ssz <= 0)
return ssz;
pos = ssz;
}
return pos;
}
/*
* Check if the buffer matches any of the names for the manpage. If it
* does, replace it with the correct invocation. Returns -1 on failure,
* 0 if nothing was replaced, 1 if it was replaced.
*/
static int
roff_manpage_inline_check_names(struct nroff *st, struct bnodeq *obq,
const struct lowdown_buf *buf)
{
size_t i;
for (i = 0; i < st->namesz; i++) {
if (!hbuf_streq(buf, st->names[i]))
continue;
if (st->type == LOWDOWN_MDOC &&
bqueue_sblockn(obq, ".Nm",
strdup(st->names[i])) == NULL)
return -1;
if (st->type == LOWDOWN_MAN &&
(!bqueue_font_mod(st, obq, 0, NFONT_BOLD) ||
bqueue_span(obq, st->names[i]) == NULL ||
!bqueue_font_mod(st, obq, 1, NFONT_BOLD)))
return -1;
return 1;
}
return 0;
}
/*
* Manpage paragraphs may have special handling depending on their prior
* section, the manual section, phase of the moon, etc. If these
* conditions are met, "nbq" may be drained and new content generated
* and appended to "obq". This returns >0 if the paragraph content was
* replaced and no paragraph macro should be output, <0 on fatal error,
* and 0 if the paragraph should be processed as usual.
*/
int
roff_manpage_paragraph(struct nroff *st, const struct lowdown_node *n,
struct bnodeq *obq, const struct bnodeq *nbq)
{
const struct lowdown_node *prev, *next;
ssize_t rc;
if (!(st->flags & LOWDOWN_ROFF_MANPAGE))
return 0;
/* NAME for manpages is always a single paragraph. */
if ((prev = TAILQ_PREV(n, lowdown_nodeq, entries)) != NULL &&
prev->type == LOWDOWN_HEADER &&
(next = TAILQ_NEXT(n, entries)) != NULL &&
next->type == LOWDOWN_HEADER &&
roff_in_section(st, "NAME")) {
rc = rndr_manpage_name(st, n, obq, nbq);
return rc < 0 ? -1 : rc > 0 ? 1 : 0;
}
/* SEE ALSO manpage listings (can have multiple paragraphs). */
if (roff_in_section(st, "SEE ALSO")) {
rc = rndr_manpage_see_also(st, n, obq, nbq);
return rc < 0 ? -1 : rc > 0 ? 1 : 0;
}
/*
* Paragraphs within the SYNOPSIS are broken down in very
* specific ways according to the manual section.
*/
if (st->headers_sec != NULL &&
(st->headers_sec[0] == '1' ||
st->headers_sec[0] == '6' ||
st->headers_sec[0] == '8') &&
roff_in_section(st, "SYNOPSIS")) {
rc = roff_manpage_synopsis_prog(st, n, obq, nbq);
return rc < 0 ? -1 : rc > 0 ? 1 : 0;
}
if (st->headers_sec != NULL &&
(st->headers_sec[0] == '2' ||
st->headers_sec[0] == '3' ||
st->headers_sec[0] == '9') &&
roff_in_section(st, "SYNOPSIS")) {
rc = roff_manpage_synopsis_func(st, n, obq, nbq);
return rc < 0 ? -1 : rc > 0 ? 1 : 0;
}
return 0;
}
/*
* Predicating on the section type and the requested decoration, try to
* process the span as a flag, argument, or option. If these conditions
* are met, generate new content and append to "obq". This returns >0 if
* content was added and the "nbq" should be dropped, <0 on fatal error,
* and 0 if no content was processed and the span should be processed by
* the caller.
*/
int
roff_manpage_inline(struct nroff *st, const struct lowdown_node *n,
struct bnodeq *obq, const struct bnodeq *nbq)
{
ssize_t rc = -1;
struct bnodeq nq;
struct lowdown_buf *buf;
if (!(st->flags & LOWDOWN_ROFF_MANPAGE))
return 0;
/* Ignore anything in a special section. */
if (roff_in_section(st, "SEE ALSO") ||
roff_in_section(st, "NAME") ||
roff_in_section(st, "SYNOPSIS"))
return 0;
/* Ignore anything without a section. */
if (st->headers_sec == NULL)
return 0;
if ((buf = hbuf_new(32)) == NULL ||
!bqueue_flush(st, buf, nbq, 2)) {
hbuf_free(buf);
return -1;
}
if ((rc = roff_manpage_inline_check_names(st, obq, buf)) != 0)
goto out;
if (st->headers_sec[0] == '1' ||
st->headers_sec[0] == '6' ||
st->headers_sec[0] == '8') {
TAILQ_INIT(&nq);
rc = roff_manpage_inline_prog(st, &nq, 0, buf);
if (rc > 0)
TAILQ_CONCAT(obq, &nq, entries);
bqueue_free(&nq);
goto out;
}
if (st->headers_sec[0] == '2' ||
st->headers_sec[0] == '3' ||
st->headers_sec[0] == '9') {
TAILQ_INIT(&nq);
rc = roff_manpage_inline_func(st, &nq, 0, buf);
if (rc > 0)
TAILQ_CONCAT(obq, &nq, entries);
bqueue_free(&nq);
goto out;
}
rc = 0;
out:
hbuf_free(buf);
return rc < 0 ? -1 : rc > 0 ? 1 : 0;
}
|