17c478bd9Sstevel@tonic-gate /* 2*56a424ccSmp153739 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 37c478bd9Sstevel@tonic-gate * Use is subject to license terms. 47c478bd9Sstevel@tonic-gate */ 57c478bd9Sstevel@tonic-gate 67c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 77c478bd9Sstevel@tonic-gate 87c478bd9Sstevel@tonic-gate /* 97c478bd9Sstevel@tonic-gate * Copyright 1987, 1988 by MIT Student Information Processing Board 107c478bd9Sstevel@tonic-gate * 117c478bd9Sstevel@tonic-gate * For copyright info, see copyright.h. 127c478bd9Sstevel@tonic-gate */ 137c478bd9Sstevel@tonic-gate 147c478bd9Sstevel@tonic-gate #include <sys/param.h> 157c478bd9Sstevel@tonic-gate #include <sys/types.h> 16*56a424ccSmp153739 #include <errno.h> 177c478bd9Sstevel@tonic-gate #include <sys/file.h> 187c478bd9Sstevel@tonic-gate #include <fcntl.h> /* just for O_* */ 197c478bd9Sstevel@tonic-gate #include <sys/wait.h> 207c478bd9Sstevel@tonic-gate #include "ss_internal.h" 217c478bd9Sstevel@tonic-gate #include "copyright.h" 227c478bd9Sstevel@tonic-gate #include <libintl.h> 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate extern void ss_list_requests(); 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate void ss_help (argc, argv, sci_idx, info_ptr) 277c478bd9Sstevel@tonic-gate int argc; 287c478bd9Sstevel@tonic-gate char const * const *argv; 297c478bd9Sstevel@tonic-gate int sci_idx; 307c478bd9Sstevel@tonic-gate pointer info_ptr; 317c478bd9Sstevel@tonic-gate { 327c478bd9Sstevel@tonic-gate char buffer[MAXPATHLEN]; 337c478bd9Sstevel@tonic-gate char const *request_name; 347c478bd9Sstevel@tonic-gate int code; 357c478bd9Sstevel@tonic-gate int fd, child; 367c478bd9Sstevel@tonic-gate register int idx; 377c478bd9Sstevel@tonic-gate register ss_data *info; 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate request_name = ss_current_request(sci_idx, &code); 407c478bd9Sstevel@tonic-gate if (code != 0) { 417c478bd9Sstevel@tonic-gate ss_perror(sci_idx, code, ""); 427c478bd9Sstevel@tonic-gate return; /* no ss_abort_line, if invalid invocation */ 437c478bd9Sstevel@tonic-gate } 447c478bd9Sstevel@tonic-gate if (argc == 1) { 457c478bd9Sstevel@tonic-gate ss_list_requests(argc, argv, sci_idx, info_ptr); 467c478bd9Sstevel@tonic-gate return; 477c478bd9Sstevel@tonic-gate } 487c478bd9Sstevel@tonic-gate else if (argc != 2) { 497c478bd9Sstevel@tonic-gate /* should do something better than this */ 507c478bd9Sstevel@tonic-gate snprintf(buffer, sizeof (buffer), (char *)dgettext(TEXT_DOMAIN, 517c478bd9Sstevel@tonic-gate "usage:\n\t%s [topic|command]\nor\t%s\n"), 527c478bd9Sstevel@tonic-gate request_name, request_name); 537c478bd9Sstevel@tonic-gate ss_perror(sci_idx, 0, buffer); 547c478bd9Sstevel@tonic-gate return; 557c478bd9Sstevel@tonic-gate } 567c478bd9Sstevel@tonic-gate info = ss_info(sci_idx); 577c478bd9Sstevel@tonic-gate if (info->info_dirs == (char **)NULL) { 587c478bd9Sstevel@tonic-gate ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL); 597c478bd9Sstevel@tonic-gate return; 607c478bd9Sstevel@tonic-gate } 617c478bd9Sstevel@tonic-gate if (info->info_dirs[0] == (char *)NULL) { 627c478bd9Sstevel@tonic-gate ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL); 637c478bd9Sstevel@tonic-gate return; 647c478bd9Sstevel@tonic-gate } 657c478bd9Sstevel@tonic-gate for (idx = 0; info->info_dirs[idx] != (char *)NULL; idx++) { 66*56a424ccSmp153739 (void) strncpy(buffer, info->info_dirs[idx], sizeof(buffer) - 1); 67*56a424ccSmp153739 buffer[sizeof(buffer) - 1] = '\0'; 68*56a424ccSmp153739 (void) strncat(buffer, "/", sizeof(buffer) - 1 - strlen(buffer)); 69*56a424ccSmp153739 (void) strncat(buffer, argv[1], sizeof(buffer) - 1 - strlen(buffer)); 70*56a424ccSmp153739 (void) strncat(buffer, ".info", sizeof(buffer) - 1 - strlen(buffer)); 717c478bd9Sstevel@tonic-gate if ((fd = open(&buffer[0], O_RDONLY)) >= 0) goto got_it; 727c478bd9Sstevel@tonic-gate } 737c478bd9Sstevel@tonic-gate if ((fd = open(&buffer[0], O_RDONLY)) < 0) { 747c478bd9Sstevel@tonic-gate char buf[MAXPATHLEN]; 75*56a424ccSmp153739 strncpy(buf, "No info found for ", sizeof(buf) - 1); 76*56a424ccSmp153739 buf[sizeof(buf) - 1] = '\0'; 77*56a424ccSmp153739 strncat(buf, argv[1], sizeof(buf) - 1 - strlen(buf)); 787c478bd9Sstevel@tonic-gate ss_perror(sci_idx, 0, buf); 797c478bd9Sstevel@tonic-gate return; 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate got_it: 827c478bd9Sstevel@tonic-gate switch (child = fork()) { 837c478bd9Sstevel@tonic-gate case -1: 847c478bd9Sstevel@tonic-gate ss_perror(sci_idx, errno, "Can't fork for pager"); 857c478bd9Sstevel@tonic-gate return; 867c478bd9Sstevel@tonic-gate case 0: 877c478bd9Sstevel@tonic-gate (void) dup2(fd, 0); /* put file on stdin */ 887c478bd9Sstevel@tonic-gate ss_page_stdin(); 897c478bd9Sstevel@tonic-gate default: 907c478bd9Sstevel@tonic-gate (void) close(fd); /* what can we do if it fails? */ 917c478bd9Sstevel@tonic-gate #ifdef WAIT_USES_INT 927c478bd9Sstevel@tonic-gate while (wait((int *)NULL) != child) { 937c478bd9Sstevel@tonic-gate #else 947c478bd9Sstevel@tonic-gate while (wait((union wait *)NULL) != child) { 957c478bd9Sstevel@tonic-gate #endif 967c478bd9Sstevel@tonic-gate /* do nothing if wrong pid */ 977c478bd9Sstevel@tonic-gate }; 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate #ifndef USE_DIRENT_H 1027c478bd9Sstevel@tonic-gate #include <sys/dir.h> 1037c478bd9Sstevel@tonic-gate #else 1047c478bd9Sstevel@tonic-gate #include <dirent.h> 1057c478bd9Sstevel@tonic-gate #endif 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate void ss_add_info_dir(sci_idx, info_dir, code_ptr) 1087c478bd9Sstevel@tonic-gate int sci_idx; 1097c478bd9Sstevel@tonic-gate char *info_dir; 1107c478bd9Sstevel@tonic-gate int *code_ptr; 1117c478bd9Sstevel@tonic-gate { 1127c478bd9Sstevel@tonic-gate register ss_data *info; 1137c478bd9Sstevel@tonic-gate DIR *d; 1147c478bd9Sstevel@tonic-gate int n_dirs; 1157c478bd9Sstevel@tonic-gate register char **dirs; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate info = ss_info(sci_idx); 1187c478bd9Sstevel@tonic-gate if (info_dir == NULL && *info_dir) { 1197c478bd9Sstevel@tonic-gate *code_ptr = SS_ET_NO_INFO_DIR; 1207c478bd9Sstevel@tonic-gate return; 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate if ((d = opendir(info_dir)) == (DIR *)NULL) { 1237c478bd9Sstevel@tonic-gate *code_ptr = errno; 1247c478bd9Sstevel@tonic-gate return; 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate closedir(d); 1277c478bd9Sstevel@tonic-gate dirs = info->info_dirs; 1287c478bd9Sstevel@tonic-gate for (n_dirs = 0; dirs[n_dirs] != (char *)NULL; n_dirs++) 1297c478bd9Sstevel@tonic-gate ; /* get number of non-NULL dir entries */ 1307c478bd9Sstevel@tonic-gate dirs = (char **)realloc((char *)dirs, 1317c478bd9Sstevel@tonic-gate (unsigned)(n_dirs + 2)*sizeof(char *)); 1327c478bd9Sstevel@tonic-gate if (dirs == (char **)NULL) { 1337c478bd9Sstevel@tonic-gate info->info_dirs = (char **)NULL; 1347c478bd9Sstevel@tonic-gate *code_ptr = errno; 1357c478bd9Sstevel@tonic-gate return; 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate info->info_dirs = dirs; 1387c478bd9Sstevel@tonic-gate dirs[n_dirs + 1] = (char *)NULL; 1397c478bd9Sstevel@tonic-gate dirs[n_dirs] = malloc((unsigned)strlen(info_dir)+1); 1407c478bd9Sstevel@tonic-gate strcpy(dirs[n_dirs], info_dir); 1417c478bd9Sstevel@tonic-gate *code_ptr = 0; 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate void ss_delete_info_dir(sci_idx, info_dir, code_ptr) 1457c478bd9Sstevel@tonic-gate int sci_idx; 1467c478bd9Sstevel@tonic-gate char *info_dir; 1477c478bd9Sstevel@tonic-gate int *code_ptr; 1487c478bd9Sstevel@tonic-gate { 1497c478bd9Sstevel@tonic-gate register char **i_d; 1507c478bd9Sstevel@tonic-gate register char **info_dirs; 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate info_dirs = ss_info(sci_idx)->info_dirs; 1537c478bd9Sstevel@tonic-gate for (i_d = info_dirs; *i_d; i_d++) { 1547c478bd9Sstevel@tonic-gate if (!strcmp(*i_d, info_dir)) { 1557c478bd9Sstevel@tonic-gate while (*i_d) { 1567c478bd9Sstevel@tonic-gate *i_d = *(i_d+1); 1577c478bd9Sstevel@tonic-gate i_d++; 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate *code_ptr = 0; 1607c478bd9Sstevel@tonic-gate return; 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate *code_ptr = SS_ET_NO_INFO_DIR; 1647c478bd9Sstevel@tonic-gate } 165