-   Programming (
-   -   howto access man and info pages? (

SciYro 07-22-2004 02:42 PM

howto access man and info pages?
is there some lib or something out there that can access man or info pages?, i need to be able to get a program to search thru these pages from a single program, so i don't have to keep using "man" and "info" trying to see what one contains what i want to know

LogicG8 07-22-2004 04:45 PM

Why not just use the program directly and pipe the info to where you need it?
man itself doesn't work as one program. Man pages are uncompressed with
gzip formatted with nroff and piped through to a pager.

Example here:

Hko 07-22-2004 04:59 PM

Yes. man pages and info pages are basically normal ascii files (with some groff/texi tags for formatting), often gzipped.. For the man pages you can find out where they are on the system with:

popen("manpath", "r");
or by parsing /etc/manpath.config yourself.

SciYro 07-22-2004 07:23 PM

so i should be able to read both man and info pages by using groff to give me human readable output?

Hko 07-23-2004 07:17 AM


Originally posted by SciYro
so i should be able to read both man and info pages by using groff to give me human readable output?
Yes, you could (at least for man, I'm not sure about info). Also, when you just open de file directly from your program (after ugzipping them, if needed), they will be more or less human-readable as well, except for some formatting code.

Anyways it's easier to use the "man" or "info" themselves. When you redirect (man ls >file.txt) or pipe (man ls | cat) "man" as well as "info" will output as human-readable text (ascii), with formatting tags removed. A small drawback is that you will also lose the bold/italic attributes: it will be plain ascii.

To do this from a program (assuming your are using the C language, please mention your programming language the next time), you can read the man/info pages into your program when you execute "man" or "info" with the popen() function. Popen() will fork() the man/info command and open a pipe to your program from which you can read the output.

I've written a complete program to demonstrate (I think I may need this myself one day, so..).
I hope you can use this as well.

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define MAXLINELEN 100
#define MAXCOMMAND 100
#define DISCARDSTDERR " 2>/dev/null"

static char *progname;

int slowdisplay(char *docprog, char *docname)
        char line[MAXLINELEN+1];
        char command[MAXCOMMAND+1];
        FILE *pipe;

        /* Construct a command line, (e.g. "man ls 2>/dev/null") */
        /* with safety checks for string length      */
        strncpy(command, docprog, MAXCOMMAND); command[MAXCOMMAND] = '\0';
        if (strlen(command) + strlen(docname)  + strlen(DISCARDSTDERR) > MAXCOMMAND) {
                fprintf(stderr, "Error: Command would be too long (max = %d).\n", MAXCOMMAND);
                return 0;
        strcat(command, " ");
        strcat(command, docname);
        strcat(command, DISCARDSTDERR); /* we do not want to see the output to stderr */

        /* Execute the command, setting up a pipe for getting its output. */
        if ((pipe = popen(command, "r")) == NULL) {
                return 0;

        /* Read the doc from the pipe line-by-line, with a short */
        /* delay between de lines for demonstration.            */
        while (fgets(line, MAXLINELEN+1, pipe) != NULL) {
                usleep(100000); /* Delay-per-line = 0.1 sec ( = 100 ms = 100000 us ) */
        if (pclose(pipe) == -1) return 0;
        return 1;

int main(int argc, char *argv[])
        if ((progname = strrchr(argv[0], '/')) == NULL) progname = argv[0];
        else progname++;       
        if (argc != 2) {
                fprintf(stderr, "Usage: %s <name of man or info page>\n", progname);
                return 1;

        /* First try man page */
        if (slowdisplay("man", argv[1])) {
                return 0;      /* If successfull, end program */

        /* else try info page */
        if (!slowdisplay("info", argv[1])) {
                fprintf(stderr, "Document not found in neither man of info system.\n");
                return 1;  /* Not succesfull, return exit code 1 to indicate failure */
        return 0;

Bug: I noticed that the program does not detect correctly the exit status of man and info. So it does not report (as you would expect when reading my code) if the document is not found.

SciYro 07-23-2004 11:21 AM

thanks, that helps me out a lot

All times are GMT -5. The time now is 09:24 AM.