1 /*
2  * cmd_help.c - User command to display help information
3  *
4  * Copyright (C) 1995, 1996 by Scott C. Gray
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, write to the Free Software
18  * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * You may contact the author :
21  *   e-mail:  gray@voicenet.com
22  *            grays@xtend-tech.com
23  *            gray@xenotropic.com
24  */
25 #include <stdio.h>
26 #include <sys/param.h>
27 #include "sqsh_config.h"
28 #include "sqsh_global.h"
29 #include "sqsh_error.h"
30 #include "sqsh_env.h"
31 #include "sqsh_cmd.h"
32 #include "cmd.h"
33 
34 /*-- Current Version --*/
35 #if !defined(lint) && !defined(__LINT__)
36 static char RCS_Id[] = "cmd_help.c,v 1.1.1.1 1996/02/14 02:36:43 gray Exp" ;
37 USE(RCS_Id)
38 #endif /* !defined(lint) */
39 
40 /*
41  * cmd_help:
42  */
43 int cmd_help( argc, argv )
44 	int    argc ;
45 	char  *argv[] ;
46 {
47 	FILE    *fp ;
48 	char     help_file[SQSH_MAXPATH+1] ;
49 	char     help_line[512] ;
50 	char    *help_dir ;
51 	cmd_t   *c ;
52 	int      max_len ;
53 	int      len ;
54 	int      nperline ;
55 	char    *width ;
56 	int      count ;
57 
58 	/*-- Always check your arguments --*/
59 	if( argc > 2 ) {
60 		fprintf( stderr, "Use: \\help [command]\n" ) ;
61 		return CMD_FAIL ;
62 	}
63 
64 	/*
65 	 * If the user requests help on a particular command then we need
66 	 * to see if there is a help file on the command.
67 	 */
68 	if( argc == 2 ) {
69 		/*
70 		 * First ast the environment prior to falling back on the hard
71 		 * coded default.
72 		 */
73 		env_get( g_env, "help_dir", &help_dir ) ;
74 		if( help_dir == NULL || *help_dir == '\0' )
75 			help_dir = SQSH_HELP ;
76 
77 		printf(
78 			"\\help: Sorry, individual help pages haven't been written yet.\n" ) ;
79 		printf(
80 			"\\help: Please refer to the manual page for more information.\n" ) ;
81 
82 		return CMD_LEAVEBUF ;
83 
84 		/*
85 		 * First, check to see if the help file actually exists.  If it
86 		 * doesn't then we are done.
87 		 */
88 		sprintf( help_file, "%s/%s.1", help_dir, argv[1] ) ;
89 
90 		if( (fp = fopen( help_file, "r" )) == NULL ) {
91 			fprintf( stderr, "\\help: Unable to access %s: %s\n",
92 			         help_file, strerror(errno) ) ;
93 			return CMD_FAIL ;
94 		}
95 
96 		/*
97 		 * Now, simply read lines from the help file and display them
98 		 * to the screen. It's as simple as that!
99 		 */
100 	 	while( fgets( help_line, sizeof(help_line), fp ) != NULL )
101 	 		fputs( help_line, stdout ) ;
102 		fclose( fp ) ;
103 
104 		return CMD_LEAVEBUF ;
105 	}
106 
107 	/*
108 	 * Nope, the user just asked for general help on the commands.
109 	 */
110  	printf( "Available commands:\n" ) ;
111 
112 	/*
113 	 * First, determine the length of the longest command.
114 	 */
115  	max_len = 1 ;
116 	for( c = (cmd_t*)avl_first( g_cmdset->cs_cmd ) ;
117 	     c != NULL ;
118 		  c = (cmd_t*)avl_next( g_cmdset->cs_cmd ) ) {
119  		len = strlen( c->cmd_name ) ;
120  		max_len = max( max_len, len ) ;
121 	}
122 
123 	/*
124 	 * Now, print the commands out in the prettiest fashion possible.
125 	 */
126  	env_get( g_env, "width", &width ) ;
127 
128  	if( width == NULL )
129  		len = 80 ;
130 	else {
131 		len = atoi( width ) ;
132 		if( len < 40 )
133 			len = 40 ;
134 	}
135 
136  	nperline = len / (max_len + 2) ;
137  	count = 1 ;
138 	for( c = (cmd_t*)avl_first( g_cmdset->cs_cmd ) ;
139 	     c != NULL ;
140 		  c = (cmd_t*)avl_next( g_cmdset->cs_cmd ) ) {
141 
142 		if( (count % nperline) == 0 )
143 			printf( "%-*s\n", max_len, c->cmd_name ) ;
144 		else
145 			printf( "%-*s  ", max_len, c->cmd_name ) ;
146 		++count ;
147 	}
148 
149 	if( (count % nperline) == 0 )
150 		printf( "\n" ) ;
151 
152 	printf("\nUse '\\help [command]' for more details\n" ) ;
153 
154 	return CMD_LEAVEBUF ;
155 }
156