Page 1 of 1

An improvement around MANPATH of Bwana

Posted: Thu Feb 21, 2008 7:38 pm
by tkurita
Bwana is very useful, but one of the problem is that adding MANPATH is not easy. I think editing man.conf should be avoided if possible.

I guess almost people needs same MANPATHes to a login shell. The better way is sharing MAPATH setting with login shell.

I make a patch to do this. All of information is obtained through a login shell.
I hope same behavior is implemented in future version.

--- MyController.m.orig 2007-12-09 20:03:24.000000000 +0900
+++ MyController.m 2008-02-22 02:30:22.000000000 +0900
@@ -470,22 +470,21 @@

[webPage appendString:[NSString stringWithFormat:HTML_HEADER_INDEX, indexFilter ? indexFilter : @""]];

- //Get all the man paths from /usr/share/misc/man.conf
- NSString *manConfFile;
- if ([self isLeopard])
- manConfFile = [NSString stringWithContentsOfFile:@"/private/etc/man.conf"];
- else
- manConfFile = [NSString stringWithContentsOfFile:@"/usr/share/misc/man.conf"];
-
- NSEnumerator *linesOfFileEnum = [[manConfFile componentsSeparatedByString:@"\n"] objectEnumerator];
- NSString *nextLine;
- NSMutableArray *manPathsToUse = [NSMutableArray array];
-
- while (nextLine = [linesOfFileEnum nextObject]) {
- if (![nextLine hasPrefix:@"#"] && ([nextLine hasPrefix:@"MANPATH "] || [nextLine hasPrefix:@"MANPATH\t"]))
- [manPathsToUse addObject:[nextLine substringFromIndex:8]];
- }
-
+ //Get all the man paths from man -w in login shell
+ NSTask *listManPathTask = [[[NSTask alloc] init] autorelease];
+ NSPipe *listManPathPipe = [NSPipe pipe];
+ NSFileHandle *listManPathHandle = [listManPathPipe fileHandleForReading];
+ [listManPathTask setStandardOutput:listManPathPipe];
+
+ char *login_shell = getenv("SHELL");
+ [listManPathTask setLaunchPath:[NSString stringWithUTF8String:login_shell]];
+ [listManPathTask setArguments:[NSArray arrayWithObjects:@"-lc", @"/usr/bin/man -w", nil]];
+ [listManPathTask launch];
+
+ NSData *dataRecieved = [listManPathHandle readDataToEndOfFile];
+ NSString *manPathes = [[[NSString alloc] initWithData:dataRecieved encoding:NSUTF8StringEncoding] autorelease];
+
+ NSArray *manPathsToUse = [manPathes componentsSeparatedByString:@":"];
int i;
NSEnumerator *manPagePaths = [manPathsToUse objectEnumerator];
NSString *manDirectory;
@@ -512,7 +511,7 @@
NSString *currItem;
while (currItem = [e nextObject]) {
NSRange sections;
- if ((sections = [currItem rangeOfString:@"."]).location != NSNotFound) {
+ if ( ![currItem hasPrefix:@"."] && ((sections = [currItem rangeOfString:@"."]).location != NSNotFound)) {
NSString *manName = [currItem substringToIndex:sections.location];

NSRange filterRange = {NSNotFound, 0};
@@ -666,11 +665,14 @@
NSPipe *findPathPipe = [NSPipe pipe];
NSFileHandle *findPathHandle = [findPathPipe fileHandleForReading];
[findPathTask setStandardOutput:findPathPipe];
- [findPathTask setLaunchPath:@"/usr/bin/man"];
+
+ char *login_shell = getenv("SHELL");
+ [findPathTask setLaunchPath:[NSString stringWithUTF8String:login_shell]];
+
if (section)
- [findPathTask setArguments:[NSArray arrayWithObjects:@"-w", section, manualPage, nil]];
+ [findPathTask setArguments:[NSArray arrayWithObjects:@"-lc", @"/usr/bin/man -w $0 $1", section, manualPage, nil]];
else
- [findPathTask setArguments:[NSArray arrayWithObjects:@"-w", manualPage, nil]];
+ [findPathTask setArguments:[NSArray arrayWithObjects:@"-lc", @"/usr/bin/man -w $0", manualPage, nil]];
[findPathTask launch];

NSString *pathToManual = nil;

Posted: Fri Feb 22, 2008 10:18 am
by Conor
With the inclusion of the changes to the source the behavior can be included in a future version. Thank you for improving Bwana, I never knew there was a secret "getenv" function, nor did I ever think of running the shell in login mode to get it to set up the environment variables. Version 2.4 is out, but since the changes are yours you don't need it. Although 2.4 does find a few extra descriptions for the index that were of the format .SH "NAME" with quotes. Since I didn't have a name I had to credit you as "tkurita" I hope you don't mind, otherwise let me know.

Posted: Fri Feb 22, 2008 10:39 am
by rbpeirce
I have several man directories and changing the config file is actually much easier for me than a lot of the hard-wiring people do. In fact, I changed a couple of scripts called by cron, such as makewhatis.local, to use `manpath` instead of the hard-wired approach they were using. I also discovered that bwana uses the directories in the same order they appear in the config file, which is a big convenience as far as I am concerned.

If bwana elects to use some built-in method other than reading the config file, either directly or through manpath (or man -w or man --path, which are the same), to acquire paths to man directories, it should take this into consideration. Perhaps this code does, but I have never moved beyond C, so I am not sure what is going on here. It looks vaguely familiar, but not quite!

Posted: Fri Feb 22, 2008 12:30 pm
by tkurita
Thanks for accepting my code.

My name is Tetsuro KURITA.
Also I have a web page.
http://homepage.mac.com/tkurita/scriptfactory/en/
I hope this URL used as my pointer.

One more suggestion

Posted: Fri Feb 22, 2008 1:05 pm
by tkurita
Please consider using XSLT for HTML formatting.

I think generating HTML by Objective-C is painful and it is difficult to improve HTML output.

Mac OS X and Safari have a XSLT processor.
We can use a shell command "xsltproc" which is in any Macintosh.

Posted: Sat Feb 23, 2008 5:03 am
by Conor
Thank you, I have changed your nickname for your name and added the link to the information. There is a lot that can be done with Bwana, like any program it can always improve, I like to clean up the HTML and add some CSS, I don't think I would go as far as using the XSLT processor for such a simple page, but it's always an option.