xref: /titanic_54/usr/src/lib/krb5/ss/help.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2000-2003 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*7c478bd9Sstevel@tonic-gate 
8*7c478bd9Sstevel@tonic-gate /*
9*7c478bd9Sstevel@tonic-gate  * Copyright 1987, 1988 by MIT Student Information Processing Board
10*7c478bd9Sstevel@tonic-gate  *
11*7c478bd9Sstevel@tonic-gate  * For copyright info, see copyright.h.
12*7c478bd9Sstevel@tonic-gate  */
13*7c478bd9Sstevel@tonic-gate 
14*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
15*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
16*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
17*7c478bd9Sstevel@tonic-gate #include <fcntl.h>	/* just for O_* */
18*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
19*7c478bd9Sstevel@tonic-gate #include "ss_internal.h"
20*7c478bd9Sstevel@tonic-gate #include "copyright.h"
21*7c478bd9Sstevel@tonic-gate #include <libintl.h>
22*7c478bd9Sstevel@tonic-gate #include <errno.h>
23*7c478bd9Sstevel@tonic-gate 
24*7c478bd9Sstevel@tonic-gate extern void ss_list_requests();
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate void ss_help (argc, argv, sci_idx, info_ptr)
27*7c478bd9Sstevel@tonic-gate     int argc;
28*7c478bd9Sstevel@tonic-gate     char const * const *argv;
29*7c478bd9Sstevel@tonic-gate     int sci_idx;
30*7c478bd9Sstevel@tonic-gate     pointer info_ptr;
31*7c478bd9Sstevel@tonic-gate {
32*7c478bd9Sstevel@tonic-gate     char buffer[MAXPATHLEN];
33*7c478bd9Sstevel@tonic-gate     char const *request_name;
34*7c478bd9Sstevel@tonic-gate     int code;
35*7c478bd9Sstevel@tonic-gate     int fd, child;
36*7c478bd9Sstevel@tonic-gate     register int idx;
37*7c478bd9Sstevel@tonic-gate     register ss_data *info;
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate     request_name = ss_current_request(sci_idx, &code);
40*7c478bd9Sstevel@tonic-gate     if (code != 0) {
41*7c478bd9Sstevel@tonic-gate 	ss_perror(sci_idx, code, "");
42*7c478bd9Sstevel@tonic-gate 	return;		/* no ss_abort_line, if invalid invocation */
43*7c478bd9Sstevel@tonic-gate     }
44*7c478bd9Sstevel@tonic-gate     if (argc == 1) {
45*7c478bd9Sstevel@tonic-gate 	ss_list_requests(argc, argv, sci_idx, info_ptr);
46*7c478bd9Sstevel@tonic-gate 	return;
47*7c478bd9Sstevel@tonic-gate     }
48*7c478bd9Sstevel@tonic-gate     else if (argc != 2) {
49*7c478bd9Sstevel@tonic-gate 	/* should do something better than this */
50*7c478bd9Sstevel@tonic-gate 	snprintf(buffer, sizeof (buffer), (char *)dgettext(TEXT_DOMAIN,
51*7c478bd9Sstevel@tonic-gate 		"usage:\n\t%s [topic|command]\nor\t%s\n"),
52*7c478bd9Sstevel@tonic-gate 		request_name, request_name);
53*7c478bd9Sstevel@tonic-gate 	ss_perror(sci_idx, 0, buffer);
54*7c478bd9Sstevel@tonic-gate 	return;
55*7c478bd9Sstevel@tonic-gate     }
56*7c478bd9Sstevel@tonic-gate     info = ss_info(sci_idx);
57*7c478bd9Sstevel@tonic-gate     if (info->info_dirs == (char **)NULL) {
58*7c478bd9Sstevel@tonic-gate 	ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
59*7c478bd9Sstevel@tonic-gate 	return;
60*7c478bd9Sstevel@tonic-gate     }
61*7c478bd9Sstevel@tonic-gate     if (info->info_dirs[0] == (char *)NULL) {
62*7c478bd9Sstevel@tonic-gate 	ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
63*7c478bd9Sstevel@tonic-gate 	return;
64*7c478bd9Sstevel@tonic-gate     }
65*7c478bd9Sstevel@tonic-gate     for (idx = 0; info->info_dirs[idx] != (char *)NULL; idx++) {
66*7c478bd9Sstevel@tonic-gate 	(void) strcpy(buffer, info->info_dirs[idx]);
67*7c478bd9Sstevel@tonic-gate 	(void) strcat(buffer, "/");
68*7c478bd9Sstevel@tonic-gate 	(void) strcat(buffer, argv[1]);
69*7c478bd9Sstevel@tonic-gate 	(void) strcat(buffer, ".info");
70*7c478bd9Sstevel@tonic-gate 	if ((fd = open(&buffer[0], O_RDONLY)) >= 0) goto got_it;
71*7c478bd9Sstevel@tonic-gate     }
72*7c478bd9Sstevel@tonic-gate     if ((fd = open(&buffer[0], O_RDONLY)) < 0) {
73*7c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
74*7c478bd9Sstevel@tonic-gate 	strcpy(buf, "No info found for ");
75*7c478bd9Sstevel@tonic-gate 	strcat(buf, argv[1]);
76*7c478bd9Sstevel@tonic-gate 	ss_perror(sci_idx, 0, buf);
77*7c478bd9Sstevel@tonic-gate 	return;
78*7c478bd9Sstevel@tonic-gate     }
79*7c478bd9Sstevel@tonic-gate got_it:
80*7c478bd9Sstevel@tonic-gate     switch (child = fork()) {
81*7c478bd9Sstevel@tonic-gate     case -1:
82*7c478bd9Sstevel@tonic-gate 	ss_perror(sci_idx, errno, "Can't fork for pager");
83*7c478bd9Sstevel@tonic-gate 	return;
84*7c478bd9Sstevel@tonic-gate     case 0:
85*7c478bd9Sstevel@tonic-gate 	(void) dup2(fd, 0); /* put file on stdin */
86*7c478bd9Sstevel@tonic-gate 	ss_page_stdin();
87*7c478bd9Sstevel@tonic-gate     default:
88*7c478bd9Sstevel@tonic-gate 	(void) close(fd); /* what can we do if it fails? */
89*7c478bd9Sstevel@tonic-gate #ifdef WAIT_USES_INT
90*7c478bd9Sstevel@tonic-gate 	while (wait((int *)NULL) != child) {
91*7c478bd9Sstevel@tonic-gate #else
92*7c478bd9Sstevel@tonic-gate 	while (wait((union wait *)NULL) != child) {
93*7c478bd9Sstevel@tonic-gate #endif
94*7c478bd9Sstevel@tonic-gate 	    /* do nothing if wrong pid */
95*7c478bd9Sstevel@tonic-gate 	};
96*7c478bd9Sstevel@tonic-gate     }
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate #ifndef USE_DIRENT_H
100*7c478bd9Sstevel@tonic-gate #include <sys/dir.h>
101*7c478bd9Sstevel@tonic-gate #else
102*7c478bd9Sstevel@tonic-gate #include <dirent.h>
103*7c478bd9Sstevel@tonic-gate #endif
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate void ss_add_info_dir(sci_idx, info_dir, code_ptr)
106*7c478bd9Sstevel@tonic-gate     int sci_idx;
107*7c478bd9Sstevel@tonic-gate     char *info_dir;
108*7c478bd9Sstevel@tonic-gate     int *code_ptr;
109*7c478bd9Sstevel@tonic-gate {
110*7c478bd9Sstevel@tonic-gate     register ss_data *info;
111*7c478bd9Sstevel@tonic-gate     DIR *d;
112*7c478bd9Sstevel@tonic-gate     int n_dirs;
113*7c478bd9Sstevel@tonic-gate     register char **dirs;
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate     info = ss_info(sci_idx);
116*7c478bd9Sstevel@tonic-gate     if (info_dir == NULL && *info_dir) {
117*7c478bd9Sstevel@tonic-gate 	*code_ptr = SS_ET_NO_INFO_DIR;
118*7c478bd9Sstevel@tonic-gate 	return;
119*7c478bd9Sstevel@tonic-gate     }
120*7c478bd9Sstevel@tonic-gate     if ((d = opendir(info_dir)) == (DIR *)NULL) {
121*7c478bd9Sstevel@tonic-gate 	*code_ptr = errno;
122*7c478bd9Sstevel@tonic-gate 	return;
123*7c478bd9Sstevel@tonic-gate     }
124*7c478bd9Sstevel@tonic-gate     closedir(d);
125*7c478bd9Sstevel@tonic-gate     dirs = info->info_dirs;
126*7c478bd9Sstevel@tonic-gate     for (n_dirs = 0; dirs[n_dirs] != (char *)NULL; n_dirs++)
127*7c478bd9Sstevel@tonic-gate 	;		/* get number of non-NULL dir entries */
128*7c478bd9Sstevel@tonic-gate     dirs = (char **)realloc((char *)dirs,
129*7c478bd9Sstevel@tonic-gate 			    (unsigned)(n_dirs + 2)*sizeof(char *));
130*7c478bd9Sstevel@tonic-gate     if (dirs == (char **)NULL) {
131*7c478bd9Sstevel@tonic-gate 	info->info_dirs = (char **)NULL;
132*7c478bd9Sstevel@tonic-gate 	*code_ptr = errno;
133*7c478bd9Sstevel@tonic-gate 	return;
134*7c478bd9Sstevel@tonic-gate     }
135*7c478bd9Sstevel@tonic-gate     info->info_dirs = dirs;
136*7c478bd9Sstevel@tonic-gate     dirs[n_dirs + 1] = (char *)NULL;
137*7c478bd9Sstevel@tonic-gate     dirs[n_dirs] = malloc((unsigned)strlen(info_dir)+1);
138*7c478bd9Sstevel@tonic-gate     strcpy(dirs[n_dirs], info_dir);
139*7c478bd9Sstevel@tonic-gate     *code_ptr = 0;
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate void ss_delete_info_dir(sci_idx, info_dir, code_ptr)
143*7c478bd9Sstevel@tonic-gate     int sci_idx;
144*7c478bd9Sstevel@tonic-gate     char *info_dir;
145*7c478bd9Sstevel@tonic-gate     int *code_ptr;
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate     register char **i_d;
148*7c478bd9Sstevel@tonic-gate     register char **info_dirs;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate     info_dirs = ss_info(sci_idx)->info_dirs;
151*7c478bd9Sstevel@tonic-gate     for (i_d = info_dirs; *i_d; i_d++) {
152*7c478bd9Sstevel@tonic-gate 	if (!strcmp(*i_d, info_dir)) {
153*7c478bd9Sstevel@tonic-gate 	    while (*i_d) {
154*7c478bd9Sstevel@tonic-gate 		*i_d = *(i_d+1);
155*7c478bd9Sstevel@tonic-gate 		i_d++;
156*7c478bd9Sstevel@tonic-gate 	    }
157*7c478bd9Sstevel@tonic-gate 	    *code_ptr = 0;
158*7c478bd9Sstevel@tonic-gate 	    return;
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate     }
161*7c478bd9Sstevel@tonic-gate     *code_ptr = SS_ET_NO_INFO_DIR;
162*7c478bd9Sstevel@tonic-gate }
163