LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-22-2004, 02:42 PM   #1
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
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
 
Old 07-22-2004, 04:45 PM   #2
LogicG8
Member
 
Registered: Jun 2003
Location: Long Island, NY
Distribution: Gentoo Unstable (what a misnomer)
Posts: 380

Rep: Reputation: 30
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:
http://64.233.161.104/search?q=cache...ipe+exec&hl=en
 
Old 07-22-2004, 04:59 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
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:
Code:
popen("manpath", "r");
or by parsing /etc/manpath.config yourself.
 
Old 07-22-2004, 07:23 PM   #4
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Original Poster
Rep: Reputation: 51
so i should be able to read both man and info pages by using groff to give me human readable output?
 
Old 07-23-2004, 07:17 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
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>

Last edited by Hko; 07-23-2004 at 07:29 AM.
 
Old 07-23-2004, 11:21 AM   #6
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Original Poster
Rep: Reputation: 51
thanks, that helps me out a lot
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C man pages sono2 Linux - Software 2 09-09-2005 12:24 PM
Looking for man ppp-on and info ppp-on pages. rvijay Linux - General 1 02-26-2005 08:49 PM
how to access man pages using C++ ? ambitionless Programming 4 09-27-2004 02:25 AM
How to quit man (less) and keep man info on screen? peb Linux - Newbie 7 03-25-2004 10:02 PM
man/info pages no help dunbar Linux - Newbie 25 01-21-2003 12:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration