1N/A/*
1N/A * CDDL HEADER START
1N/A *
1N/A * The contents of this file are subject to the terms of the
1N/A * Common Development and Distribution License, Version 1.0 only
1N/A * (the "License"). You may not use this file except in compliance
1N/A * with the License.
1N/A *
1N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1N/A * or http://www.opensolaris.org/os/licensing.
1N/A * See the License for the specific language governing permissions
1N/A * and limitations under the License.
1N/A *
1N/A * When distributing Covered Code, include this CDDL HEADER in each
1N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1N/A * If applicable, add the following below this CDDL HEADER, with the
1N/A * fields enclosed by brackets "[]" replaced with your own identifying
1N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1N/A *
1N/A * CDDL HEADER END
1N/A */
1N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
1N/A/* All Rights Reserved */
1N/A
1N/A
1N/A/*
1N/A * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A */
1N/A
1N/A#include <stdio.h>
1N/A#include <string.h>
1N/A#include <limits.h>
1N/A#include <sys/types.h>
1N/A#include "libadm.h"
1N/A
1N/A/*
1N/A * This file is the only one anywhere to need these functions,
1N/A * so we declare them here, not in libadm.h
1N/A */
1N/Aextern char *__compile(char *, char *, const char *, int);
1N/Aextern int __step(const char *, const char *);
1N/A
1N/A#define ESIZE 1024
1N/A
1N/A#define ERRMSG0 "Input is required."
1N/A#define ERRMSG1 "Please enter a string containing no more than %d characters."
1N/A#define ERRMSG2 \
1N/A "Pattern matching has failed."
1N/A#define ERRMSG3 \
1N/A "Please enter a string which contains no embedded, \
1N/A leading or trailing spaces or tabs."
1N/A
1N/A#define HLPMSG0 "Please enter a string"
1N/A#define HLPMSG1 "Please enter a string containing no more than %d characters"
1N/A#define HLPMSG2 "matches one of the following patterns:"
1N/A#define HLPMSG3 "matches the following pattern:"
1N/A#define HLPMSG4 "contains no embedded, leading or trailing spaces or tabs."
1N/A
1N/Astatic char *errstr;
1N/A
1N/Astatic char *
1N/Asethlp(char *msg, char *regexp[], int length)
1N/A{
1N/A int i;
1N/A
1N/A if (length)
1N/A (void) sprintf(msg, HLPMSG1, length);
1N/A else
1N/A (void) strcpy(msg, HLPMSG0);
1N/A
1N/A (void) strcat(msg, length ? " and " : " which ");
1N/A
1N/A if (regexp && regexp[0]) {
1N/A (void) strcat(msg, regexp[1] ? HLPMSG2 : HLPMSG3);
1N/A for (i = 0; regexp[i]; i++) {
1N/A (void) strcat(msg, "\\n\\t");
1N/A (void) strcat(msg, regexp[i]);
1N/A }
1N/A } else
1N/A (void) strcat(msg, HLPMSG4);
1N/A return (msg);
1N/A}
1N/A
1N/Aint
1N/Ackstr_val(char *regexp[], int length, char *input)
1N/A{
1N/A char expbuf[ESIZE];
1N/A int i, valid;
1N/A
1N/A valid = 1;
1N/A if (length && (strlen(input) > (size_t)length)) {
1N/A errstr = ERRMSG1;
1N/A return (1);
1N/A }
1N/A if (regexp && regexp[0]) {
1N/A valid = 0;
1N/A for (i = 0; !valid && regexp[i]; ++i) {
1N/A if (!__compile(regexp[i], expbuf, &expbuf[ESIZE], '\0'))
1N/A return (2);
1N/A valid = __step(input, expbuf);
1N/A }
1N/A if (!valid)
1N/A errstr = ERRMSG2;
1N/A } else if (strpbrk(input, " \t")) {
1N/A errstr = ERRMSG3;
1N/A valid = 0;
1N/A }
1N/A return (valid == 0);
1N/A}
1N/A
1N/Avoid
1N/Ackstr_err(char *regexp[], int length, char *error, char *input)
1N/A{
1N/A char *defhlp;
1N/A char temp[1024];
1N/A
1N/A if (input) {
1N/A if (ckstr_val(regexp, length, input)) {
1N/A /* LINTED E_SEC_PRINTF_VAR_FMT */
1N/A (void) snprintf(temp, sizeof (temp), errstr, length);
1N/A puterror(stdout, temp, error);
1N/A return;
1N/A }
1N/A }
1N/A
1N/A defhlp = sethlp(temp, regexp, length);
1N/A puterror(stdout, defhlp, error);
1N/A}
1N/A
1N/Avoid
1N/Ackstr_hlp(char *regexp[], int length, char *help)
1N/A{
1N/A char *defhlp;
1N/A char hlpbuf[1024];
1N/A
1N/A defhlp = sethlp(hlpbuf, regexp, length);
1N/A puthelp(stdout, defhlp, help);
1N/A}
1N/A
1N/Aint
1N/Ackstr(char *strval, char *regexp[], int length, char *defstr, char *error,
1N/A char *help, char *prompt)
1N/A{
1N/A int n;
1N/A char *defhlp;
1N/A char input[MAX_INPUT],
1N/A hlpbuf[1024],
1N/A errbuf[1024];
1N/A
1N/A defhlp = NULL;
1N/A if (!prompt)
1N/A prompt = "Enter an appropriate value";
1N/A
1N/Astart:
1N/A putprmpt(stderr, prompt, NULL, defstr);
1N/A if (getinput(input))
1N/A return (1);
1N/A
1N/A n = (int)strlen(input);
1N/A if (n == 0) {
1N/A if (defstr) {
1N/A (void) strcpy(strval, defstr);
1N/A return (0);
1N/A }
1N/A puterror(stderr, ERRMSG0, error);
1N/A goto start;
1N/A }
1N/A if (strcmp(input, "?") == 0) {
1N/A if (defhlp == NULL)
1N/A defhlp = sethlp(hlpbuf, regexp, length);
1N/A puthelp(stderr, defhlp, help);
1N/A goto start;
1N/A }
1N/A if (ckquit && (strcmp(input, "q") == 0)) {
1N/A (void) strcpy(strval, input);
1N/A return (3);
1N/A }
1N/A if (ckstr_val(regexp, length, input)) {
1N/A /* LINTED E_SEC_PRINTF_VAR_FMT */
1N/A (void) snprintf(errbuf, sizeof (errbuf), errstr, length);
1N/A puterror(stderr, errbuf, error);
1N/A goto start;
1N/A }
1N/A (void) strcpy(strval, input);
1N/A return (0);
1N/A}
1N/A