e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami/*
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * CDDL HEADER START
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * The contents of this file are subject to the terms of the
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * Common Development and Distribution License (the "License").
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * You may not use this file except in compliance with the License.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * or http://www.opensolaris.org/os/licensing.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * See the License for the specific language governing permissions
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * and limitations under the License.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * When distributing Covered Code, include this CDDL HEADER in each
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * If applicable, add the following below this CDDL HEADER, with the
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * fields enclosed by brackets "[]" replaced with your own identifying
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * information: Portions Copyright [yyyy] [name of copyright owner]
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * CDDL HEADER END
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami/*
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * Use is subject to license terms.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami/*
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * General purpse string manipulation routines
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami#include <stdio.h>
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami#include <_conv.h>
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami/*
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * Implementation of isspace() that does not require <ctype.h>
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * or <sys/ctype.h>, appropriate for simple non-localized use.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahramiint
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahramiconv_strproc_isspace(int c)
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami{
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami return ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n'));
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami}
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami/*
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * Remove leading and trailing whitespace from the given string.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * entry:
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * str - String to be trimmed
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * exit:
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * The pointer to the trimmed string is returned.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * note:
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * Leading whitespace is trimmed by advancing the given str pointer,
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * and not by making a copy or allocing more memory. Hence, the caller
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * should retain a copy of the original str pointer if they need to
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * free the original memory or otherwise access it.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * Trailing whitespace is trimmed by inserting a NULL termination
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * in the position at which the first trailing whitespce character
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * lies. This routine can therefore modify the memory used by
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * the input string.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahramichar *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahramiconv_strproc_trim(char *str)
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami{
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami char *tail;
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami /* Skip leading whitespace */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami while (conv_strproc_isspace(*str))
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami str++;
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami /* Back up over trailing whitespace */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami tail = str + strlen(str);
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami while ((tail > str) && conv_strproc_isspace(*(tail - 1)))
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami tail--;
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *tail = '\0';
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami return (str);
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami}
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami/*
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * Given a debug token of the form:
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * token=value
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * extract and return a pointer to the value.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * entry:
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * str - String to process
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * token_len = Length of the token, not counting the '=' character,
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * or any whitespace between the token and the '='.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * to_upper - True to convert the returned value to upper case.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * value - Address of pointer to receive the value string.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * exit:
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * On success, *value is updated to point at the value string,
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * and True (1) is returned. On failure, False (0) is returned.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * note:
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * If CONV_SPEXV_F_UCASE is specified, this routine modifies
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * the memory pointed at by str.
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami */
e23c41c9edb2294649cde3d370ae755701f3f140Ali BahramiBoolean
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahramiconv_strproc_extract_value(char *str, size_t token_len, int flags,
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami const char **value)
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami{
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami int trim = (flags & CONV_SPEXV_F_NOTRIM) == 0;
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami /* Skip the token */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami str += token_len;
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami /*
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami * If TRIM, skip whitespace between token and '='
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami if (trim)
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami while (conv_strproc_isspace(*str))
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami str++;
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami /* If there's not a '=' here, this isn't the token we thought it was */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami if (*str != '=')
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami return (FALSE);
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami str++; /* skip the '=' */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami /* if TRIM, skip whitespace following the '=' */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami if (trim)
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami while (conv_strproc_isspace(*str))
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami str++;
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami /* Null value and it's not OK? Make it an error. */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami if (((flags & CONV_SPEXV_F_NULLOK) == 0) && (*str == '\0'))
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami return (FALSE);
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *value = str;
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami /* Convert to uppercase on request */
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami if (flags & CONV_SPEXV_F_UCASE)
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami for (; *str; str++)
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami if ((*str >= 'a') && (*str <= 'z'))
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami *str = *str - ('a' - 'A');
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami return (TRUE);
e23c41c9edb2294649cde3d370ae755701f3f140Ali Bahrami}