Quote:
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.
Code:
#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) {
perror(progname);
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) {
printf(line);
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;
}
<edit>
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.
</edit>