4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
47 #define INVISMAXSIZE 36
50 static char *deflt
= NULL
, *prompt
= NULL
, *error
= NULL
, *help
= NULL
;
51 static int kpid
= BADPID
;
54 static char *label
, **invis
;
55 static int ninvis
= 0;
57 static int attr
= CKALPHA
;
62 "%s: ERROR: internal error occurred while attempting menu setup\n"
64 "\t-f file #file containing choices\n" \
65 "\t-l label #menu label\n" \
66 "\t-i invis [, ...] #invisible menu choices\n" \
67 "\t-m max #maximum choices user may select\n" \
68 "\t-n #do not sort choices alphabetically\n" \
69 "\t-o #don't prompt if only one choice\n" \
70 "\t-u #unnumbered choices\n"
72 static const char husage
[] = "Wh";
73 static const char eusage
[] = "We";
80 (void) fprintf(stderr
,
81 gettext("usage: %s [options] [choice [...]]\n"), prog
);
82 (void) fprintf(stderr
, gettext(OPTMESG
));
83 (void) fprintf(stderr
, gettext(MYOPTS
));
84 (void) fprintf(stderr
, gettext(STDOPTS
));
88 (void) fprintf(stderr
,
89 gettext("usage: %s [options] [choice [...]]\n"), prog
);
90 (void) fprintf(stderr
, gettext(OPTMESG
));
91 (void) fprintf(stderr
,
92 gettext("\t-W width\n\t-h help\n"));
96 (void) fprintf(stderr
,
97 gettext("usage: %s [options] [choice [...]]\n"), prog
);
98 (void) fprintf(stderr
, gettext(OPTMESG
));
99 (void) fprintf(stderr
,
100 gettext("\t-W width\n\t-e error\n"));
107 * Given argv[0], return a pointer to the basename of the program.
110 prog_name(char *arg0
)
114 /* first strip trailing '/' characters (exec() allows these!) */
115 str
= arg0
+ strlen(arg0
);
116 while (str
> arg0
&& *--str
== '/')
118 if ((str
= strrchr(arg0
, '/')) != NULL
)
124 main(int argc
, char **argv
)
130 char temp
[LSIZE
* MB_LEN_MAX
];
132 size_t invismaxsize
= INVISMAXSIZE
;
134 wchar_t wline
[LSIZE
], wtemp
[LSIZE
];
136 (void) setlocale(LC_ALL
, "");
138 #if !defined(TEXT_DOMAIN)
139 #define TEXT_DOMAIN "SYS_TEST"
141 (void) textdomain(TEXT_DOMAIN
);
143 prog
= prog_name(argv
[0]);
145 invis
= (char **)calloc(invismaxsize
, sizeof (char *));
147 (void) fprintf(stderr
,
148 gettext("Not enough memory\n"));
151 while ((c
= getopt(argc
, argv
, "m:oni:l:f:ud:p:e:h:k:s:QW:?")) != EOF
) {
152 /* check for invalid option */
153 if ((*prog
== 'e') && !strchr(eusage
, c
))
154 usage(); /* no valid options */
155 if ((*prog
== 'h') && !strchr(husage
, c
))
164 ckwidth
= atol(optarg
);
166 (void) fprintf(stderr
,
167 gettext("%s: ERROR: negative display width specified\n"),
175 if (max
> SHRT_MAX
|| max
< SHRT_MIN
) {
176 (void) fprintf(stderr
,
177 gettext("%s: ERROR: too large or too small max value specified\n"),
192 invis
[ninvis
++] = optarg
;
193 if (ninvis
== invismaxsize
) {
194 invismaxsize
+= INVISMAXSIZE
;
195 invis
= (char **)realloc(invis
,
196 invismaxsize
* sizeof (char *));
198 (void) fprintf(stderr
,
199 gettext("Not enough memory\n"));
202 (void) memset(invis
+ ninvis
, 0,
203 (invismaxsize
- ninvis
) *
213 if ((fp
= fopen(optarg
, "r")) == NULL
) {
214 (void) fprintf(stderr
,
215 gettext("%s: ERROR: can't open %s\n"),
246 signo
= atoi(optarg
);
260 mp
= allocmenu(label
, attr
);
263 while (fgetws(wline
, LSIZE
, fp
)) {
265 * Skip comment lines, those beginning with '#'.
266 * Note: AT&T forgot this & needs the next 2 lines.
271 if ((n
!= 0) && (wline
[n
- 1] == L
'\n'))
272 wline
[n
- 1] = L
'\0';
274 * if the line begins with a space character,
275 * this is a continuous line to the previous line.
277 if (iswspace(*wline
)) {
278 (void) wcscat(wtemp
, L
"\n");
279 (void) wcscat(wtemp
, wline
);
283 r
= wcstombs(temp
, wtemp
,
285 if (r
== (size_t)-1) {
286 (void) fprintf(stderr
,
287 gettext("Invalid character in the menu definition.\n"));
290 if (setitem(mp
, temp
)) {
291 (void) fprintf(stderr
,
292 gettext(INTERR
), prog
);
296 (void) wcscpy(wtemp
, wline
);
301 r
= wcstombs(temp
, wtemp
, n
* MB_LEN_MAX
);
302 if (r
== (size_t)-1) {
303 (void) fprintf(stderr
,
304 gettext("Invalid character in the menu definition.\n"));
307 if (setitem(mp
, temp
)) {
308 (void) fprintf(stderr
, gettext(INTERR
), prog
);
314 while (optind
< argc
) {
315 if (setitem(mp
, argv
[optind
++])) {
316 (void) fprintf(stderr
, gettext(INTERR
), prog
);
321 for (n
= 0; n
< ninvis
; ) {
322 if (setinvis(mp
, invis
[n
++])) {
323 (void) fprintf(stderr
, gettext(INTERR
), prog
);
330 ckitem_err(mp
, error
);
332 } else if (*prog
== 'h') {
334 ckitem_hlp(mp
, help
);
345 * if -o option is specified, mp->nchoices is 1, and if no invisible
346 * item is specified, ckitem() will consume two entries of item,
347 * even though 'max' is set to 1. So to take care of that problem, we
348 * allocate one extra element for item
350 item
= (char **)calloc(mmax
+1, sizeof (char *));
352 (void) fprintf(stderr
,
353 gettext("Not enough memory\n"));
356 n
= ckitem(mp
, item
, max
, deflt
, error
, help
, prompt
);
359 (void) kill(kpid
, signo
);
364 (void) puts(item
[i
++]);