Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / release / picobsd / tinyware / help / help.c
1 /*-
2  * Copyright (c) 1998 Eric P. Scott <eps@sirius.com>
3  * Copyright (c) 1998 Andrzej Bialecki <abial@freebsd.org>
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
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
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  *
27  * $FreeBSD: src/release/picobsd/tinyware/help/help.c,v 1.5 1999/08/28 01:33:54 peter Exp $
28  * $DragonFly: src/release/picobsd/tinyware/help/Attic/help.c,v 1.2 2003/06/17 04:27:20 dillon Exp $
29  */
30
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <ar.h>
35 #include <sys/ioctl.h>
36
37 int display(FILE *, const char *);
38
39 static int cnt, crt=-1;
40
41 int
42 main(int argc, char *argv[])
43 {
44         register int i, s;
45         FILE *fd;
46         struct ttysize ts;
47
48         if (!(fd=fopen("/help.a", "r"))) {
49                 (void)fputs("Couldn't open help archive.\n", stderr);
50                 exit(1);
51         }
52         cnt=0;
53         if (ioctl(fileno(stdout), TIOCGWINSZ, &ts)>=0) {
54                 crt=ts.ts_lines-1;
55         }
56         if (crt<3) crt=23;
57         s=display(fd, argc>1 ? argv[1] : "help");
58         if (s<0) s=0;
59         else for (i=2;i<argc;) {
60                 rewind(fd);
61                 s|=display(fd, argv[i++]);
62                 if (s<0) {
63                         s=0;
64                         break;
65                 }
66         }
67         (void)fclose(fd);
68         exit(s);
69 }
70
71 int
72 more(void)
73 {
74         char buf[8];
75
76         (void)fflush(stdout);
77         (void)fputs("\033[7mPress Enter to continue\033[m", stderr);
78         (void)fflush(stderr);
79         cnt=0;
80         if (fgets(buf, sizeof buf, stdin)) return 0;
81         (void)fputc('\n', stderr);
82         return 1;
83 }
84
85 int
86 display(FILE *fd, const char *fname)
87 {
88         register char *p;
89         register int c, n, o;
90         struct ar_hdr ar;
91         char aname[20];
92
93         if (!fgets(aname, sizeof aname, fd)) {
94                 return 1;
95         }
96         if (strncmp(aname, ARMAG, SARMAG)) return 1;
97         (void)snprintf(aname, sizeof(aname), "%s/", fname);
98         for (;;) {
99                 if (fread((void *)&ar, sizeof ar, 1, fd)!=1) return 1;
100                 if (strncmp(ar.ar_fmag, ARFMAG, 2)) return 1;
101                 n=0;
102                 p=ar.ar_size;
103                 do {
104                         if ((c=(int)(*p++-'0'))<0||c>9) break;
105                         n*=10; n+=c;
106                 } while (p<&ar.ar_size[sizeof ar.ar_size]);
107                 if (!strncmp(ar.ar_name, aname, strlen(aname))) break;
108                 if (fseek(fd, (long)n, SEEK_CUR)<0) return 1;
109                 if ((n&1)&&fgetc(fd)!='\n') return 1;
110         }
111         if (cnt>=crt&&more()) return -1;
112         (void)fputc('\n', stdout);
113         cnt++;
114         o=0; while (o<n&&(c=fgetc(fd))!=EOF) {
115         per:
116                 o++;
117                 (void)fputc(c, stdout);
118                 if (c!='\n') continue;
119                 if (++cnt<crt) continue;
120                 if (o>=n||(c=fgetc(fd))==EOF) break;
121                 if (more()) return -1;
122                 goto per;
123         }
124         if (cnt>=crt&&more()) return -1;
125         (void)fputc('\n', stdout);
126         cnt++;
127         if (!strcmp(fname, "help")) {
128                 rewind(fd);
129                 (void)fgets(aname, sizeof aname, fd);
130                 if (cnt>=crt&&more()) return -1;
131                 (void)fputs("The following help items are available:\n",
132                         stdout);
133                 cnt++;
134                 o=0;
135                 while (fread((void *)&ar, sizeof ar, 1, fd)==1) {
136                         if (strncmp(ar.ar_fmag, ARFMAG, 2)) break;
137                         if ((o%6)==0) {
138                                 (void)fputc('\n', stdout);
139                                 if (++cnt>=crt&&more()) return -1;
140                         }
141                         *(index(ar.ar_name,'/'))=' ';
142                         (void)printf("%.13s", ar.ar_name);
143                         ++o;
144                         n=0;
145                         p=ar.ar_size;
146                         do {
147                                 if ((c=(int)(*p++-'0'))<0||c>9) break;
148                                 n*=10; n+=c;
149                         } while (p<&ar.ar_size[sizeof ar.ar_size]);
150                         if (fseek(fd, (long)n, SEEK_CUR)<0) break;
151                         if ((n&1)&&fgetc(fd)!='\n') break;
152                 }
153                 if (cnt>=crt&&more()) return -1;
154                 (void)fputc('\n', stdout);
155                 cnt++;
156         }
157         return 0;
158 }