|
|
1.1 ! root 1: /* ! 2: * man.c ! 3: * 10/31/90 ! 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 32000 /* index buffer size */ ! 13: #define DEFPAGER "exec /bin/scat -1" /* default $PAGER */ ! 14: #define MANHELP "/usr/man/man.help" /* manual help file */ ! 15: #define MANINX "/usr/man/man.index" /* manual index file */ ! 16: #define NBUF 80 /* buf[] buffer size */ ! 17: #define NCMD 512 /* cmd[] buffer size */ ! 18: #define VERSION "1.3" /* 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: if (wflag) ! 62: printf("%s\n", MANHELP); ! 63: else { ! 64: cmdcat(" "); ! 65: cmdcat(MANHELP); ! 66: system(cmd); ! 67: } ! 68: exit(status); ! 69: } ! 70: ! 71: /* Args given. First read the index. */ ! 72: if ((fd = open(MANINX, 0)) == -1) ! 73: fatal("cannot open manual index %s, online manual probably not installed", MANINX); ! 74: else if ((i = read(fd, manindex, BBBIGBUF)) == -1) ! 75: fatal("cannot read index buffer"); ! 76: else if (i >= BBBIGBUF) ! 77: fatal("index file too large"); ! 78: close(fd); ! 79: #if DEBUG ! 80: nonfatal("%s=%d bytes", MANHELP, i); ! 81: #endif] ! 82: ! 83: /* Look up each arg. */ ! 84: nfound = 0; ! 85: for (i = 1; i < argc; i++) { ! 86: #if DEBUG ! 87: nonfatal("argv[%d]=%s", i, argv[i]); ! 88: #endif ! 89: indexp = manindex; ! 90: found = 0; ! 91: ! 92: /* Look up arg in index. May find multiple hits. */ ! 93: while ((cp = lookup(argv[i])) != NULL) { ! 94: found++; ! 95: nfound++; ! 96: #if DEBUG ! 97: nonfatal("index line=%s", cp); ! 98: #endif ! 99: if (wflag) ! 100: printf("/usr/man/%s\n", cp); ! 101: else { ! 102: cmdcat(" "); ! 103: cmdcat("/usr/man/"); ! 104: cmdcat(cp); ! 105: #if DEBUG ! 106: nonfatal("command=%s", cmd); ! 107: #endif ! 108: } ! 109: } ! 110: if (found == 0) { ! 111: nonfatal("%s not found in manual", argv[i]); ! 112: status = 1; ! 113: } ! 114: } ! 115: if (!wflag && nfound) ! 116: system(cmd); ! 117: exit(status); ! 118: } ! 119: ! 120: /* ! 121: * Concatenate given string to cmd buffer. ! 122: * Complain if too long. ! 123: */ ! 124: void ! 125: cmdcat(s) register char *s; ! 126: { ! 127: register int len; ! 128: ! 129: len = strlen(cmd); ! 130: if (len + strlen(s) + 1 >= NCMD) ! 131: {printf("cmd=%s\ns=%s\nlen=%d\n", cmd, s, len); ! 132: fatal("command buffer overflow"); ! 133: } ! 134: strcpy(&cmd[len], s); ! 135: } ! 136: ! 137: /* Cry and die. */ ! 138: void ! 139: fatal(s) char *s; ! 140: { ! 141: fprintf(stderr, "man: %r\n", &s); ! 142: exit(1); ! 143: } ! 144: ! 145: /* ! 146: * Look up string s in helpfile index starting at indexp. ! 147: * Return name of file on match, else NULL. ! 148: */ ! 149: char * ! 150: lookup(s) char *s; ! 151: { ! 152: register char *namep, *next; ! 153: ! 154: while ((next = strchr(indexp, '\n')) != NULL) { ! 155: if (next - indexp + 1 >= NBUF) ! 156: fatal("index buffer overflow"); ! 157: strncpy(buf, indexp, next - indexp); ! 158: buf[next-indexp] = '\0'; /* index line to buf */ ! 159: #if DEBUG ! 160: nonfatal("[%s]", buf); ! 161: #endif ! 162: namep = strchr(buf, ' '); ! 163: indexp = next + 1; /* bump to next index line */ ! 164: if (namep == NULL) ! 165: continue; ! 166: else ! 167: *namep++ = '\0'; /* NUL-terminate index entry */ ! 168: #if DEBUG ! 169: nonfatal("buf=%s namep=%s", buf, namep); ! 170: #endif ! 171: if (strcmp(namep, s) == 0) ! 172: return buf; /* gotcha */ ! 173: } ! 174: return NULL; /* no match */ ! 175: } ! 176: ! 177: void ! 178: nonfatal(s) char *s; ! 179: { ! 180: fprintf(stderr, "man: %r\n", &s); ! 181: } ! 182: ! 183: /* end of man.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.