/*
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
back = calloc(nelements, size);
if (back == NULL)
errx(1, "Ran out of memory.");
return back;
}
char *
Strdup(const char *s)
{
char *ret;
ret = strdup(s);
if (ret == NULL) {
errx(1, "Ran out of memory.");
}
return ret;
}
/*
* find the position of a given character in a string
* (one based)
*/
int
position(const char *string, char ch)
{
if (string != NULL)
for (int i = 1; *string != '\0'; string++, i++)
if (*string == ch)
return i;
return -1;
}
/*
* clobber the first occurrence of ch in string by the new character
*/
char *
substitute(char *string, char chold, char chnew)
{
char *cp = string;
if (cp != NULL)
while (*cp != '\0') {
if (*cp == chold) {
*cp = chnew;
break;
}
cp++;
}
return string;
}
/*
* parse a string that is the result of a format %s(%d)
* return TRUE if this is of the proper format
*/
bool
persperdexplode(char *string, char **r_perd, char **r_pers)
{
char *cp;
size_t length = string != NULL ? strlen(string) : 0;
#if 0 /* unused */
/*
* parse a quoted string that is the result of a format \"%s\"(%d)
* return TRUE if this is of the proper format
*/
static bool
qpersperdexplode(char *string, char **r_perd, char **r_pers)
{
char *cp;
int length = 0;
for (int i = 0; i < wordc; i++)
if (wordv[i] != NULL) {
fprintf(fyle, "%s%s",sep,wordv[i]);
sep = " ";
}
}
/*
* Given a string, parse it into a number of words, and build
* a wordc wordv combination pointing into it.
*/
void
wordvbuild(char *string, int *r_wordc, char ***r_wordv)
{
char *cp;
char **wordv;
int wordcount;
int wordindex;
for (wordcount = 0, cp = string; *cp != '\0'; wordcount++) {
while (isspace((unsigned char)*cp))
cp++;
if (*cp == '\0')
break;
while (*cp != '\0' && !isspace((unsigned char)*cp))
cp++;
}
wordv = Calloc(wordcount + 1, sizeof (char *));
for (cp=string, wordindex=0; wordcount > 0; wordindex++, --wordcount) {
while (isspace((unsigned char)*cp))
cp++;
if (*cp == '\0')
break;
wordv[wordindex] = cp;
while (*cp != '\0' && !isspace((unsigned char)*cp))
cp++;
*cp++ = '\0';
}
if (wordcount != 0)
errx(6, "Initial miscount of the number of words in a line");
wordv[wordindex] = NULL;
#ifdef FULLDEBUG
for (wordcount = 0; wordcount < wordindex; wordcount++)
printf("Word %d = \"%s\"\n", wordcount, wordv[wordcount]);
printf("\n");
#endif
*r_wordc = wordindex;
*r_wordv = wordv;
}
/*
* Compare two 0 based wordvectors
*/
bool
wordv_eq(char **wordv1, int wordc, char **wordv2)
{
for (int i = 0; i < wordc; i++)
if (wordv1[i] == NULL || wordv2[i] == NULL
|| strcmp(wordv1[i], wordv2[i]) != 0)
return false;
return true;
}
/*
* splice a 0 based word vector onto the tail of a
* new wordv, allowing the first emptyhead slots to be empty
*/
char **
wordvsplice(int emptyhead, int wordc, char **wordv)
{
char **nwordv;
int nwordc = emptyhead + wordc;
int i;
nwordv = Calloc(nwordc, sizeof (char *));
for (i = 0; i < emptyhead; i++)
nwordv[i] = NULL;
for (i = emptyhead; i < nwordc; i++)
nwordv[i] = wordv[i-emptyhead];
return nwordv;
}