1 /*- 2 * Copyright (c) 1980 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1980 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)getNAME.c 5.6 (Berkeley) 06/14/93"; 16 #endif /* not lint */ 17 18 /* 19 * Get name sections from manual pages. 20 * -t for building toc 21 * -i for building intro entries 22 * other apropos database 23 */ 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 int tocrc; 29 int intro; 30 int typeflag; 31 32 void doname __P((char *)); 33 void dorefname __P((char *)); 34 void getfrom __P((char *)); 35 void split __P((char *, char *)); 36 void trimln __P((char *)); 37 void usage __P((void)); 38 39 int 40 main(argc, argv) 41 int argc; 42 char *argv[]; 43 { 44 extern int optind; 45 int ch; 46 47 while ((ch = getopt(argc, argv, "itw")) != EOF) 48 switch(ch) { 49 case 'i': 50 intro = 1; 51 break; 52 case 't': 53 tocrc = 1; 54 break; 55 case 'w': 56 typeflag = 1; 57 break; 58 case '?': 59 default: 60 usage(); 61 } 62 argc -= optind; 63 argv += optind; 64 65 if (!*argv) 66 usage(); 67 68 for (; *argv; ++argv) 69 getfrom(*argv); 70 exit(0); 71 } 72 73 void 74 getfrom(pathname) 75 char *pathname; 76 { 77 int i = 0; 78 char *name, *loc; 79 char headbuf[BUFSIZ]; 80 char linbuf[BUFSIZ]; 81 82 if (freopen(pathname, "r", stdin) == 0) { 83 perror(pathname); 84 return; 85 } 86 if (name = strrchr(pathname, '/')) 87 name++; 88 else 89 name = pathname; 90 for (;;) { 91 if (fgets(headbuf, sizeof headbuf, stdin) == NULL) { 92 if (typeflag) 93 printf("%-60s UNKNOWN\n", pathname); 94 return; 95 } 96 if (headbuf[0] != '.') 97 continue; 98 if ((headbuf[1] == 'T' && headbuf[2] == 'H') || 99 (headbuf[1] == 't' && headbuf[2] == 'h')) 100 break; 101 if (headbuf[1] == 'D' && headbuf[2] == 't') { 102 if (typeflag) { 103 printf("%-60s NEW\n", pathname); 104 return; 105 } 106 goto newman; 107 } 108 } 109 if (typeflag) { 110 printf("%-60s OLD\n", pathname); 111 return; 112 } 113 for (;;) { 114 if (fgets(linbuf, sizeof linbuf, stdin) == NULL) 115 return; 116 if (linbuf[0] != '.') 117 continue; 118 if (linbuf[1] == 'S' && linbuf[2] == 'H') 119 break; 120 if (linbuf[1] == 's' && linbuf[2] == 'h') 121 break; 122 } 123 trimln(headbuf); 124 if (tocrc) 125 doname(name); 126 if (!intro) 127 printf("%s\t", headbuf); 128 linbuf[0] = '\0'; 129 for (;;) { 130 if (fgets(headbuf, sizeof headbuf, stdin) == NULL) 131 break; 132 if (headbuf[0] == '.') { 133 if (headbuf[1] == 'S' && headbuf[2] == 'H') 134 break; 135 if (headbuf[1] == 's' && headbuf[2] == 'h') 136 break; 137 } 138 if (i != 0) 139 strcat(linbuf, " "); 140 i++; 141 trimln(headbuf); 142 strcat(linbuf, headbuf); 143 } 144 if (intro) 145 split(linbuf, name); 146 else 147 printf("%s\n", linbuf); 148 return; 149 150 newman: 151 for (;;) { 152 if (fgets(linbuf, sizeof linbuf, stdin) == NULL) 153 return; 154 if (linbuf[0] != '.') 155 continue; 156 if (linbuf[1] == 'S' && linbuf[2] == 'h') 157 break; 158 } 159 trimln(headbuf); 160 if (tocrc) 161 doname(name); 162 if (!tocrc && !intro) 163 printf(".TH%s\t", &headbuf[3]); 164 linbuf[0] = '\0'; 165 for (;;) { 166 if (fgets(headbuf, sizeof headbuf, stdin) == NULL) 167 break; 168 if (headbuf[0] == '.') { 169 if (headbuf[1] == 'S' && headbuf[2] == 'h') 170 break; 171 } 172 if (i != 0) 173 strcat(linbuf, " "); 174 i++; 175 trimln(headbuf); 176 for (loc = headbuf; loc; loc = strchr(loc, ' ')) 177 if (loc[1] == ',') 178 strcpy(loc, &loc[1]); 179 else 180 loc++; 181 if (headbuf[0] != '.') { 182 strcat(linbuf, headbuf); 183 } else { 184 if (headbuf[1] == 'N' && headbuf[2] == 'd') 185 strcat(linbuf, "\\- "); 186 strcat(linbuf, &headbuf[4]); 187 } 188 } 189 if (intro) 190 split(linbuf, name); 191 else 192 printf("%s\n", linbuf); 193 } 194 195 void 196 trimln(cp) 197 register char *cp; 198 { 199 200 while (*cp) 201 cp++; 202 if (*--cp == '\n') 203 *cp = 0; 204 } 205 206 void 207 doname(name) 208 char *name; 209 { 210 register char *dp = name, *ep; 211 212 again: 213 while (*dp && *dp != '.') 214 putchar(*dp++); 215 if (*dp) 216 for (ep = dp+1; *ep; ep++) 217 if (*ep == '.') { 218 putchar(*dp++); 219 goto again; 220 } 221 putchar('('); 222 if (*dp) 223 dp++; 224 while (*dp) 225 putchar (*dp++); 226 putchar(')'); 227 putchar(' '); 228 } 229 230 void 231 split(line, name) 232 char *line, *name; 233 { 234 register char *cp, *dp; 235 char *sp, *sep; 236 237 cp = strchr(line, '-'); 238 if (cp == 0) 239 return; 240 sp = cp + 1; 241 for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--) 242 ; 243 *++cp = '\0'; 244 while (*sp && (*sp == ' ' || *sp == '\t')) 245 sp++; 246 for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") { 247 cp = strchr(dp, ','); 248 if (cp) { 249 register char *tp; 250 251 for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--) 252 ; 253 *++tp = '\0'; 254 for (++cp; *cp == ' ' || *cp == '\t'; cp++) 255 ; 256 } 257 printf("%s%s\t", sep, dp); 258 dorefname(name); 259 printf("\t%s", sp); 260 } 261 } 262 263 void 264 dorefname(name) 265 char *name; 266 { 267 register char *dp = name, *ep; 268 269 again: 270 while (*dp && *dp != '.') 271 putchar(*dp++); 272 if (*dp) 273 for (ep = dp+1; *ep; ep++) 274 if (*ep == '.') { 275 putchar(*dp++); 276 goto again; 277 } 278 putchar('.'); 279 if (*dp) 280 dp++; 281 while (*dp) 282 putchar (*dp++); 283 } 284 285 void 286 usage() 287 { 288 (void)fprintf(stderr, "usage: getNAME [-it] file ...\n"); 289 exit(1); 290 } 291