help.c (38590) | help.c (38918) |
---|---|
1/*- | 1/*- |
2 * Copyright (c) 1998 Andrzej Bialecki | 2 * Copyright (c) 1998 Eric P. Scott <eps@sirius.com> 3 * Copyright (c) 1998 Andrzej Bialecki <abial@nask.pl> |
3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright --- 7 unchanged lines hidden (view full) --- 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * | 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright --- 7 unchanged lines hidden (view full) --- 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * |
26 * $Id: help.c,v 1.1.1.1 1998/07/14 07:30:53 abial Exp $ 27 * | 27 * $Id$ |
28 */ 29 30 31#include <stdio.h> 32#include <string.h> | 28 */ 29 30 31#include <stdio.h> 32#include <string.h> |
33#include <sys/types.h> 34#include <dirent.h> | 33#include <ar.h> 34#include <sys/ioctl.h> |
35 | 35 |
36void 37display(char *fname) | 36int display(FILE *, const char *); 37 38static int cnt, crt=-1; 39 40int 41main(int argc, char *argv[]) |
38{ | 42{ |
43 register int i, s; |
|
39 FILE *fd; | 44 FILE *fd; |
40 DIR *dirp; 41 struct dirent *d; 42 char buf[100],junk[5],c, *dot; 43 int i; | 45 struct ttysize ts; |
44 | 46 |
45 snprintf(buf,99,"/help/%s.hlp",fname); 46 if((fd=fopen(buf,"r"))==NULL) { 47 printf("No help available for '%s'.\n",fname); | 47 if (!(fd=fopen("/help.a", "r"))) { 48 (void)fputs("Couldn't open help archive.\n", stderr); |
48 exit(1); 49 } | 49 exit(1); 50 } |
50 printf("\n"); 51 i=0; 52 while(!feof(fd)) { 53 if(fgets(buf,99,fd)==NULL) continue; 54 if(i<23) { 55 printf("%s",buf); 56 i++; 57 } else { 58 printf("[7mPress Enter to continue[m"); 59 fgets(junk,5,stdin); 60 printf("%s",buf); 61 i=0; 62 } | 51 cnt=0; 52 if (ioctl(fileno(stdout), TIOCGWINSZ, &ts)>=0) { 53 crt=ts.ts_lines-1; |
63 } | 54 } |
64 printf("\n"); 65 i=0; 66 if(strcmp(fname,"help")==0) { 67 printf("The following help items are available:\n\n"); 68 dirp=opendir("/help/."); 69 while((d=readdir(dirp))!=NULL) { 70 if(d->d_name[0]=='.') continue; 71 if((dot=strchr(d->d_name,'.'))!=NULL) { 72 *dot='\0'; 73 } 74 printf("%-13s",d->d_name); 75 i++; 76 if(i>5) { 77 printf("\n"); 78 i=0; 79 } | 55 if (crt<3) crt=23; 56 s=display(fd, argc>1 ? argv[1] : "help"); 57 if (s<0) s=0; 58 else for (i=2;i<argc;) { 59 rewind(fd); 60 s|=display(fd, argv[i++]); 61 if (s<0) { 62 s=0; 63 break; |
80 } | 64 } |
81 closedir(dirp); 82 printf("\n"); | |
83 } | 65 } |
84 return; | 66 (void)fclose(fd); 67 exit(s); |
85} 86 | 68} 69 |
70int 71more(void) 72{ 73 char buf[8]; |
|
87 | 74 |
75 (void)fflush(stdout); 76 (void)fputs("\033[7mPress Enter to continue\033[m", stderr); 77 (void)fflush(stderr); 78 cnt=0; 79 if (fgets(buf, sizeof buf, stdin)) return 0; 80 (void)fputc('\n', stderr); 81 return 1; 82} 83 |
|
88int | 84int |
89main(int argc, char *argv[]) | 85display(FILE *fd, const char *fname) |
90{ | 86{ |
91 if(argc==1) { 92 display("help"); 93 } else { 94 display(argv[1]); | 87 register char *p; 88 register int c, n, o; 89 struct ar_hdr ar; 90 char aname[20]; 91 92 if (!fgets(aname, sizeof aname, fd)) { 93 return 1; |
95 } | 94 } |
96 exit(0); | 95 if (strncmp(aname, ARMAG, SARMAG)) return 1; 96 (void)sprintf(aname, "%-16.16s", fname); 97 for (;;) { 98 if (fread((void *)&ar, sizeof ar, 1, fd)!=1) return 1; 99 if (strncmp(ar.ar_fmag, ARFMAG, 2)) return 1; 100 n=0; 101 p=ar.ar_size; 102 do { 103 if ((c=(int)(*p++-'0'))<0||c>9) break; 104 n*=10; n+=c; 105 } while (p<&ar.ar_size[sizeof ar.ar_size]); 106 if (!strncmp(ar.ar_name, aname, 16)) break; 107 if (fseek(fd, (long)n, SEEK_CUR)<0) return 1; 108 if ((n&1)&&fgetc(fd)!='\n') return 1; 109 } 110 if (cnt>=crt&&more()) return -1; 111 (void)fputc('\n', stdout); 112 cnt++; 113 o=0; while (o<n&&(c=fgetc(fd))!=EOF) { 114 per: 115 o++; 116 (void)fputc(c, stdout); 117 if (c!='\n') continue; 118 if (++cnt<crt) continue; 119 if (o>=n||(c=fgetc(fd))==EOF) break; 120 if (more()) return -1; 121 goto per; 122 } 123 if (cnt>=crt&&more()) return -1; 124 (void)fputc('\n', stdout); 125 cnt++; 126 if (!strcmp(fname, "help")) { 127 rewind(fd); 128 (void)fgets(aname, sizeof aname, fd); 129 if (cnt>=crt&&more()) return -1; 130 (void)fputs("The following help items are available:\n", 131 stdout); 132 cnt++; 133 o=0; 134 while (fread((void *)&ar, sizeof ar, 1, fd)==1) { 135 if (strncmp(ar.ar_fmag, ARFMAG, 2)) break; 136 if ((o%6)==0) { 137 (void)fputc('\n', stdout); 138 if (++cnt>=crt&&more()) return -1; 139 } 140 (void)printf("%.13s", ar.ar_name); 141 ++o; 142 n=0; 143 p=ar.ar_size; 144 do { 145 if ((c=(int)(*p++-'0'))<0||c>9) break; 146 n*=10; n+=c; 147 } while (p<&ar.ar_size[sizeof ar.ar_size]); 148 if (fseek(fd, (long)n, SEEK_CUR)<0) break; 149 if ((n&1)&&fgetc(fd)!='\n') break; 150 } 151 if (cnt>=crt&&more()) return -1; 152 (void)fputc('\n', stdout); 153 cnt++; 154 } 155 return 0; |
97} | 156} |