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