1*13912Ssam #ifndef lint 2*13912Ssam static char *sccsid = "@(#)getNAME.c 4.2 (Berkeley) 07/10/83"; 3*13912Ssam #endif 4*13912Ssam 5*13912Ssam /* 6*13912Ssam * Get name sections from manual pages. 7*13912Ssam * -t for building toc 8*13912Ssam * -i for building intro entries 9*13912Ssam * other apropos database 10*13912Ssam */ 11*13912Ssam #include <strings.h> 121022Sbill #include <stdio.h> 131022Sbill 141022Sbill int tocrc; 15*13912Ssam int intro; 161022Sbill 171022Sbill main(argc, argv) 181022Sbill int argc; 191022Sbill char *argv[]; 201022Sbill { 211022Sbill 221022Sbill argc--, argv++; 231022Sbill if (!strcmp(argv[0], "-t")) 241022Sbill argc--, argv++, tocrc++; 25*13912Ssam if (!strcmp(argv[0], "-i")) 26*13912Ssam argc--, argv++, intro++; 271022Sbill while (argc > 0) 281022Sbill getfrom(*argv++), argc--; 291022Sbill exit(0); 301022Sbill } 311022Sbill 321022Sbill getfrom(name) 331022Sbill char *name; 341022Sbill { 351022Sbill char headbuf[BUFSIZ]; 361022Sbill char linbuf[BUFSIZ]; 371022Sbill register char *cp; 381022Sbill int i = 0; 391022Sbill 401022Sbill if (freopen(name, "r", stdin) == 0) { 411022Sbill perror(name); 421022Sbill exit(1); 431022Sbill } 441022Sbill for (;;) { 451022Sbill if (fgets(headbuf, sizeof headbuf, stdin) == NULL) 461022Sbill return; 471022Sbill if (headbuf[0] != '.') 481022Sbill continue; 491022Sbill if (headbuf[1] == 'T' && headbuf[2] == 'H') 501022Sbill break; 511022Sbill if (headbuf[1] == 't' && headbuf[2] == 'h') 521022Sbill break; 531022Sbill } 541022Sbill for (;;) { 551022Sbill if (fgets(linbuf, sizeof linbuf, stdin) == NULL) 561022Sbill return; 571022Sbill if (linbuf[0] != '.') 581022Sbill continue; 591022Sbill if (linbuf[1] == 'S' && linbuf[2] == 'H') 601022Sbill break; 611022Sbill if (linbuf[1] == 's' && linbuf[2] == 'h') 621022Sbill break; 631022Sbill } 641022Sbill trimln(headbuf); 65*13912Ssam if (tocrc) 66*13912Ssam doname(name); 67*13912Ssam if (!intro) 68*13912Ssam printf("%s\t", headbuf); 691022Sbill for (;;) { 701022Sbill if (fgets(linbuf, sizeof linbuf, stdin) == NULL) 711022Sbill break; 721022Sbill if (linbuf[0] == '.') { 731022Sbill if (linbuf[1] == 'S' && linbuf[2] == 'H') 741022Sbill break; 751022Sbill if (linbuf[1] == 's' && linbuf[2] == 'h') 761022Sbill break; 771022Sbill } 781022Sbill trimln(linbuf); 79*13912Ssam if (intro) { 80*13912Ssam split(linbuf, name); 81*13912Ssam continue; 82*13912Ssam } 831022Sbill if (i != 0) 841022Sbill printf(" "); 851022Sbill i++; 861022Sbill printf("%s", linbuf); 871022Sbill } 881022Sbill printf("\n"); 891022Sbill } 901022Sbill 911022Sbill trimln(cp) 921022Sbill register char *cp; 931022Sbill { 941022Sbill 951022Sbill while (*cp) 961022Sbill cp++; 971022Sbill if (*--cp == '\n') 981022Sbill *cp = 0; 991022Sbill } 100*13912Ssam 101*13912Ssam doname(name) 102*13912Ssam char *name; 103*13912Ssam { 104*13912Ssam register char *dp = name, *ep; 105*13912Ssam 106*13912Ssam again: 107*13912Ssam while (*dp && *dp != '.') 108*13912Ssam putchar(*dp++); 109*13912Ssam if (*dp) 110*13912Ssam for (ep = dp+1; *ep; ep++) 111*13912Ssam if (*ep == '.') { 112*13912Ssam putchar(*dp++); 113*13912Ssam goto again; 114*13912Ssam } 115*13912Ssam putchar('('); 116*13912Ssam if (*dp) 117*13912Ssam dp++; 118*13912Ssam while (*dp) 119*13912Ssam putchar (*dp++); 120*13912Ssam putchar(')'); 121*13912Ssam putchar(' '); 122*13912Ssam } 123*13912Ssam 124*13912Ssam split(line, name) 125*13912Ssam char *line, *name; 126*13912Ssam { 127*13912Ssam register char *cp, *dp; 128*13912Ssam char *sp, *sep; 129*13912Ssam 130*13912Ssam cp = index(line, '-'); 131*13912Ssam if (cp == 0) 132*13912Ssam return; 133*13912Ssam sp = cp + 1; 134*13912Ssam for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--) 135*13912Ssam ; 136*13912Ssam *++cp = '\0'; 137*13912Ssam while (*sp && (*sp == ' ' || *sp == '\t')) 138*13912Ssam sp++; 139*13912Ssam for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") { 140*13912Ssam cp = index(dp, ','); 141*13912Ssam if (cp) { 142*13912Ssam register char *tp; 143*13912Ssam 144*13912Ssam for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--) 145*13912Ssam ; 146*13912Ssam *++tp = '\0'; 147*13912Ssam for (++cp; *cp == ' ' || *cp == '\t'; cp++) 148*13912Ssam ; 149*13912Ssam } 150*13912Ssam printf("%s%s\t", sep, dp); 151*13912Ssam doname(name); 152*13912Ssam printf("\t%s", sp); 153*13912Ssam } 154*13912Ssam } 155