|
|
1.1 ! root 1: /* ! 2: * man.c ! 3: * 9/2/93 ! 4: * Usage: man [ -w ] [ topic ... ] ! 5: * Quick and dirty man hack. ! 6: * Read manual index, print manual sections to screen via $PAGER. ! 7: */ ! 8: ! 9: #include <stdio.h> ! 10: #include <string.h> ! 11: ! 12: #define BBBIGBUF 64000 /* index buffer size */ ! 13: #define DEFPAGER "exec /bin/scat -1" /* default $PAGER */ ! 14: #define MANINX "/usr/man/man.index" /* manual index file */ ! 15: #define NBUF 160 /* buf[] buffer size */ ! 16: #define NCMD 512 /* cmd[] buffer size */ ! 17: #define USAGE "man article ..." ! 18: #define VERSION "1.5" /* version id */ ! 19: ! 20: extern char *getenv(); ! 21: ! 22: /* Globals. */ ! 23: char buf[NBUF]; ! 24: char cmd[NCMD]; ! 25: char *indexp; ! 26: char manindex[BBBIGBUF]; ! 27: int wflag; ! 28: ! 29: /* Forward. */ ! 30: void cmdcat(); ! 31: void fatal(); ! 32: char *lookup(); ! 33: void nonfatal(); ! 34: ! 35: main(argc, argv) int argc; char *argv[]; ! 36: { ! 37: int i, fd, status, found, nfound; ! 38: char *cp; ! 39: ! 40: status = 0; ! 41: ! 42: /* Look for environmental $PAGER. */ ! 43: if ((cp = getenv("PAGER")) == NULL) ! 44: cp = DEFPAGER; ! 45: strcpy(cmd, cp); ! 46: ! 47: /* Parse option args. */ ! 48: if (argc > 1 && strcmp(argv[1], "-w") == 0) { ! 49: ++wflag; ! 50: --argc; ! 51: ++argv; ! 52: } ! 53: if (argc > 1 && strcmp(argv[1], "-V") == 0) { ! 54: fprintf(stderr, "man: V%s\n", VERSION); ! 55: --argc; ! 56: ++argv; ! 57: } ! 58: ! 59: /* If no args, print manual help information. */ ! 60: if (argc == 1) { ! 61: fprintf(stderr, "%s\n", USAGE); ! 62: exit(status); ! 63: } ! 64: ! 65: /* Args given. First read the index. */ ! 66: if ((fd = open(MANINX, 0)) == -1) ! 67: fatal("cannot open manual index %s: on-line manual probably not installed", MANINX); ! 68: else if ((i = read(fd, manindex, BBBIGBUF)) == -1) ! 69: fatal("cannot read index buffer"); ! 70: else if (i >= BBBIGBUF) ! 71: fatal("index file too large"); ! 72: close(fd); ! 73: #if DEBUG ! 74: nonfatal("%s=%d bytes", MANHELP, i); ! 75: #endif] ! 76: ! 77: /* Look up each arg. */ ! 78: nfound = 0; ! 79: for (i = 1; i < argc; i++) { ! 80: #if DEBUG ! 81: nonfatal("argv[%d]=%s", i, argv[i]); ! 82: #endif ! 83: indexp = manindex; ! 84: found = 0; ! 85: ! 86: /* Look up arg in index. May find multiple hits. */ ! 87: while ((cp = lookup(argv[i])) != NULL) { ! 88: found++; ! 89: nfound++; ! 90: #if DEBUG ! 91: nonfatal("index line=%s", cp); ! 92: #endif ! 93: if (wflag) ! 94: printf("/usr/man/%s\n", cp); ! 95: else { ! 96: cmdcat(" "); ! 97: cmdcat("/usr/man/"); ! 98: cmdcat(cp); ! 99: #if DEBUG ! 100: nonfatal("command=%s", cmd); ! 101: #endif ! 102: } ! 103: } ! 104: if (found == 0) { ! 105: nonfatal("%s not found in manual", argv[i]); ! 106: status = 1; ! 107: } ! 108: } ! 109: if (!wflag && nfound) ! 110: system(cmd); ! 111: exit(status); ! 112: } ! 113: ! 114: /* ! 115: * Concatenate given string to cmd buffer. ! 116: * Complain if too long. ! 117: */ ! 118: void ! 119: cmdcat(s) register char *s; ! 120: { ! 121: register int len; ! 122: ! 123: len = strlen(cmd); ! 124: if (len + strlen(s) + 1 >= NCMD) ! 125: {printf("cmd=%s\ns=%s\nlen=%d\n", cmd, s, len); ! 126: fatal("command buffer overflow"); ! 127: } ! 128: strcpy(&cmd[len], s); ! 129: } ! 130: ! 131: /* Cry and die. */ ! 132: void ! 133: fatal(s) char *s; ! 134: { ! 135: fprintf(stderr, "man: %r\n", &s); ! 136: exit(1); ! 137: } ! 138: ! 139: /* ! 140: * Look up string s in helpfile index starting at indexp. ! 141: * Return name of file on match, else NULL. ! 142: */ ! 143: char * ! 144: lookup(s) char *s; ! 145: { ! 146: register char *namep, *next, *endptr; ! 147: ! 148: while ((next = strchr(indexp, '\n')) != NULL) { ! 149: if (next - indexp + 1 >= NBUF) ! 150: fatal("index buffer overflow"); ! 151: strncpy(buf, indexp, next - indexp); ! 152: ! 153: /* crop off description - fwb, 4/13/93 */ ! 154: if ((endptr = strrchr(buf, '\t')) != NULL) ! 155: *endptr = '\0'; ! 156: else ! 157: buf[next-indexp] = '\0'; ! 158: ! 159: /* crop off terminal parentheses, if any - fwb, 4/13/93 */ ! 160: if ((endptr = strrchr(buf, '(')) != NULL) ! 161: *endptr = '\0'; ! 162: ! 163: #if DEBUG ! 164: nonfatal("[%s]", buf); ! 165: #endif ! 166: /* split the article name from the file name */ ! 167: if ((namep = strchr(buf, '\t')) == NULL) ! 168: if ((namep = strrchr(buf, ' ')) == NULL) ! 169: continue; ! 170: ! 171: indexp = next + 1; /* bump to next index line */ ! 172: ! 173: *namep++ = '\0'; /* NUL-terminate index entry */ ! 174: ! 175: /* crop off terminal parentheses, if any - fwb, 4/13/93 */ ! 176: if ((endptr = strrchr(namep, '(')) != NULL) ! 177: *endptr = '\0'; ! 178: ! 179: #if DEBUG ! 180: nonfatal("buf=%s namep=%s", buf, namep); ! 181: #endif ! 182: if (strcmp(namep, s) == 0) ! 183: return buf; /* gotcha */ ! 184: } ! 185: return NULL; /* no match */ ! 186: } ! 187: ! 188: void ! 189: nonfatal(s) char *s; ! 190: { ! 191: fprintf(stderr, "man: %r\n", &s); ! 192: } ! 193: ! 194: /* end of man.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.