Deleted Added
sdiff udiff text old ( 119172 ) new ( 132493 )
full compact
1/*
2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
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:

--- 13 unchanged lines hidden (view full) ---

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 * Author: Hartmut Brandt <harti@freebsd.org>
28 */
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sbin/atm/atmconfig/main.c 119172 2003-08-20 08:25:36Z harti $");
31
32#include <sys/types.h>
33#include <sys/sysctl.h>
34#include <netdb.h>
35#include <stdarg.h>
36#include <ctype.h>
37#include <limits.h>
38#include <inttypes.h>
39#include "atmconfig.h"
40#include "private.h"
41
42/* verbosity level */
43int verbose;
44
45/* notitle option */
46static int notitle;

--- 17 unchanged lines hidden (view full) ---

64
65static int
66substr(const char *s1, const char *s2)
67{
68 return (strlen(s1) <= strlen(s2) && strncmp(s1, s2, strlen(s1)) == 0);
69}
70
71/*
72 * Function to print help. The help argument is in argv[0] here.
73 */
74static void
75help_func(int argc, char *argv[])
76{
77 FILE *hp;
78 const char *start, *end;
79 char *fname;
80 off_t match, last_match;
81 char line[LINE_MAX];
82 char key[100];
83 int level, i;
84
85 /*
86 * Find the help file
87 */
88 hp = NULL;
89 for (start = PATH_HELP; *start != '\0'; start = end + 1) {
90 for (end = start; *end != ':' && *end != '\0'; end++)
91 ;
92 if (start == end) {
93 if (asprintf(&fname, "%s", FILE_HELP) == -1)
94 err(1, NULL);
95 } else {
96 if (asprintf(&fname, "%.*s/%s", (int)(end - start),
97 start, FILE_HELP) == -1)
98 err(1, NULL);
99 }
100 if ((hp = fopen(fname, "r")) != NULL)
101 break;
102 free(fname);
103 }
104 if (hp == NULL)
105 errx(1, "help file not found");
106
107 if (argc == 0) {
108 if ((argv[0] = strdup("intro")) == NULL)
109 err(1, NULL);
110 argc = 1;
111 }
112
113 optind = 0;
114 match = -1;
115 last_match = -1;
116 for (;;) {
117 /* read next line */
118 if (fgets(line, sizeof(line), hp) == NULL) {
119 if (ferror(hp))
120 err(1, fname);
121 /* EOF */
122 clearerr(hp);
123 level = 999;
124 goto stop;
125 }
126 if (line[0] != '^')
127 continue;
128
129 if (sscanf(line + 1, "%d%99s", &level, key) != 2)
130 errx(1, "error in help file '%s'", line);
131
132 if (level < optind) {
133 stop:
134 /* next higher level entry - stop this level */
135 if (match == -1) {
136 /* not found */
137 goto not_found;
138 }
139 /* go back to the match */
140 if (fseeko(hp, match, SEEK_SET) == -1)
141 err(1, fname);
142 last_match = match;
143 match = -1;
144
145 /* go to next key */
146 if (++optind >= argc)
147 break;
148 }
149 if (level == optind) {
150 if (substr(argv[optind], key)) {
151 if (match != -1) {
152 printf("Ambiguous topic.");
153 goto list_topics;
154 }
155 if ((match = ftello(hp)) == -1)
156 err(1, fname);
157 }
158 }
159 }
160 if (last_match == -1) {
161 if (fseek(hp, 0L, SEEK_SET) == -1)
162 err(1, fname);
163 } else {
164 if (fseeko(hp, last_match, SEEK_SET) == -1)
165 err(1, fname);
166 }
167
168 for (;;) {
169 if (fgets(line, sizeof(line), hp) == NULL) {
170 if (ferror(hp))
171 err(1, fname);
172 break;
173 }
174 if (line[0] == '#')
175 continue;
176 if (line[0] == '^')
177 break;
178 printf("%s", line);
179 }
180
181 exit(0);
182
183 not_found:
184 printf("Topic not found.");
185
186 list_topics:
187 printf(" Use one of:\natmconfig");
188 for (i = 0; i < optind; i++)
189 printf(" %s", argv[i]);
190 printf(" [");
191 /* list all the keys at this level */
192 if (last_match == -1) {
193 if (fseek(hp, 0L, SEEK_SET) == -1)
194 err(1, fname);
195 } else {
196 if (fseeko(hp, last_match, SEEK_SET) == -1)
197 err(1, fname);
198 }
199
200 for (;;) {
201 /* read next line */
202 if (fgets(line, sizeof(line), hp) == NULL) {
203 if (ferror(hp))
204 err(1, fname);
205 break;
206 }
207 if (line[0] == '#' || line[0] != '^')
208 continue;
209
210 if (sscanf(line + 1, "%d%99s", &level, key) != 2)
211 errx(1, "error in help file '%s'", line);
212
213 if (level < optind)
214 break;
215 if (level == optind)
216 printf(" %s", key);
217 }
218 printf(" ]\n");
219 exit(1);
220}
221
222int
223main(int argc, char *argv[])
224{
225 int opt, i;
226 const struct cmdtab *match, *cc, *tab;

--- 294 unchanged lines hidden ---