NetBSD/usr.bin/error/subr.c

412 lines
9.7 KiB
C
Raw Normal View History

/* $NetBSD: subr.c,v 1.19 2009/08/13 06:59:37 dholland Exp $ */
1995-09-02 09:57:23 +04:00
1993-04-09 16:58:06 +04:00
/*
1995-09-02 09:57:23 +04:00
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
1993-04-09 16:58:06 +04:00
*
* 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
1993-04-09 16:58:06 +04:00
* 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.
*/
1997-10-18 18:44:21 +04:00
#include <sys/cdefs.h>
1993-04-09 16:58:06 +04:00
#ifndef lint
1995-09-02 09:57:23 +04:00
#if 0
static char sccsid[] = "@(#)subr.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: subr.c,v 1.19 2009/08/13 06:59:37 dholland Exp $");
1993-04-09 16:58:06 +04:00
#endif /* not lint */
#include <ctype.h>
1997-10-18 18:44:21 +04:00
#include <err.h>
#include <stdio.h>
1993-04-09 16:58:06 +04:00
#include <stdlib.h>
#include <string.h>
#include "error.h"
2009-08-13 06:10:50 +04:00
1993-04-09 16:58:06 +04:00
/*
2009-08-13 07:07:49 +04:00
* Arrayify a list of rules
1993-04-09 16:58:06 +04:00
*/
1997-10-18 18:44:21 +04:00
void
arrayify(int *e_length, Eptr **e_array, Eptr header)
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
Eptr errorp;
Eptr *array;
int listlength;
int listindex;
1993-04-09 16:58:06 +04:00
for (errorp = header, listlength = 0;
errorp; errorp = errorp->error_next, listlength++)
continue;
array = Calloc(listlength+1, sizeof (Eptr));
2009-08-13 07:07:49 +04:00
for (listindex = 0, errorp = header;
1993-04-09 16:58:06 +04:00
listindex < listlength;
2009-08-13 07:07:49 +04:00
listindex++, errorp = errorp->error_next) {
1993-04-09 16:58:06 +04:00
array[listindex] = errorp;
errorp->error_position = listindex;
}
array[listindex] = NULL;
1993-04-09 16:58:06 +04:00
*e_length = listlength;
*e_array = array;
}
void *
Calloc(size_t nelements, size_t size)
1993-04-09 16:58:06 +04:00
{
void *back;
2009-08-13 07:07:49 +04:00
back = calloc(nelements, size);
if (back == NULL)
1997-10-18 18:44:21 +04:00
errx(1, "Ran out of memory.");
2009-08-13 07:07:49 +04:00
return (back);
1993-04-09 16:58:06 +04:00
}
char *
Strdup(const char *s)
{
char *ret;
ret = strdup(s);
if (ret == NULL) {
errx(1, "Ran out of memory.");
}
return ret;
}
1993-04-09 16:58:06 +04:00
/*
2009-08-13 07:07:49 +04:00
* find the position of a given character in a string
* (one based)
1993-04-09 16:58:06 +04:00
*/
1997-10-18 18:44:21 +04:00
int
2009-08-13 07:50:02 +04:00
position(const char *string, char ch)
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
int i;
1993-04-09 16:58:06 +04:00
if (string)
2009-08-13 07:07:49 +04:00
for (i=1; *string; string++, i++) {
if (*string == ch)
return (i);
}
return (-1);
1993-04-09 16:58:06 +04:00
}
1997-10-18 18:44:21 +04:00
1993-04-09 16:58:06 +04:00
/*
2009-08-13 07:07:49 +04:00
* clobber the first occurance of ch in string by the new character
1993-04-09 16:58:06 +04:00
*/
1997-10-18 18:44:21 +04:00
char *
substitute(char *string, char chold, char chnew)
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
char *cp = string;
1993-04-09 16:58:06 +04:00
if (cp)
2009-08-13 07:07:49 +04:00
while (*cp) {
if (*cp == chold) {
*cp = chnew;
break;
}
cp++;
1993-04-09 16:58:06 +04:00
}
2009-08-13 07:07:49 +04:00
return (string);
1993-04-09 16:58:06 +04:00
}
1997-10-18 18:44:21 +04:00
char
2009-08-13 07:50:02 +04:00
lastchar(const char *string)
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
int length;
if (string == NULL)
return ('\0');
1993-04-09 16:58:06 +04:00
length = strlen(string);
if (length >= 1)
2009-08-13 07:07:49 +04:00
return (string[length-1]);
1993-04-09 16:58:06 +04:00
else
2009-08-13 07:07:49 +04:00
return ('\0');
1993-04-09 16:58:06 +04:00
}
1997-10-18 18:44:21 +04:00
char
2009-08-13 07:50:02 +04:00
firstchar(const char *string)
1993-04-09 16:58:06 +04:00
{
if (string)
2009-08-13 07:07:49 +04:00
return (string[0]);
1993-04-09 16:58:06 +04:00
else
2009-08-13 07:07:49 +04:00
return ('\0');
1993-04-09 16:58:06 +04:00
}
1997-10-18 18:44:21 +04:00
char
2009-08-13 07:50:02 +04:00
next_lastchar(const char *string)
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
int length;
if (string == NULL)
return ('\0');
1993-04-09 16:58:06 +04:00
length = strlen(string);
if (length >= 2)
2009-08-13 07:07:49 +04:00
return (string[length - 2]);
1993-04-09 16:58:06 +04:00
else
2009-08-13 07:07:49 +04:00
return ('\0');
1993-04-09 16:58:06 +04:00
}
1997-10-18 18:44:21 +04:00
void
clob_last(char *string, char newstuff)
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
int length = 0;
1993-04-09 16:58:06 +04:00
if (string)
length = strlen(string);
if (length >= 1)
string[length - 1] = newstuff;
}
/*
2009-08-13 07:07:49 +04:00
* parse a string that is the result of a format %s(%d)
* return TRUE if this is of the proper format
1993-04-09 16:58:06 +04:00
*/
bool
persperdexplode(char *string, char **r_perd, char **r_pers)
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
char *cp;
int length = 0;
1993-04-09 16:58:06 +04:00
if (string)
length = strlen(string);
if (length >= 4 && string[length - 1] == ')') {
1993-04-09 16:58:06 +04:00
for (cp = &string[length - 2];
isdigit((unsigned char)*cp) && *cp != '(';
1993-04-09 16:58:06 +04:00
--cp)
continue;
2009-08-13 07:07:49 +04:00
if (*cp == '(') {
1993-04-09 16:58:06 +04:00
string[length - 1] = '\0'; /* clobber the ) */
*r_perd = strdup(cp+1);
1993-04-09 16:58:06 +04:00
string[length - 1] = ')';
*cp = '\0'; /* clobber the ( */
*r_pers = strdup(string);
1993-04-09 16:58:06 +04:00
*cp = '(';
return true;
2009-08-13 07:07:49 +04:00
}
1993-04-09 16:58:06 +04:00
}
return false;
1993-04-09 16:58:06 +04:00
}
1997-10-18 18:44:21 +04:00
2009-08-13 06:10:50 +04:00
#if 0 /* unused */
1993-04-09 16:58:06 +04:00
/*
2009-08-13 07:07:49 +04:00
* parse a quoted string that is the result of a format \"%s\"(%d)
* return TRUE if this is of the proper format
1993-04-09 16:58:06 +04:00
*/
2009-08-13 06:10:50 +04:00
static boolean
qpersperdexplode(char *string, char **r_perd, char **r_pers)
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
char *cp;
int length = 0;
1993-04-09 16:58:06 +04:00
if (string)
length = strlen(string);
if (length >= 4 && string[length - 1] == ')') {
1993-04-09 16:58:06 +04:00
for (cp = &string[length - 2];
isdigit((unsigned char)*cp) && *cp != '(';
1993-04-09 16:58:06 +04:00
--cp)
continue;
2009-08-13 07:07:49 +04:00
if (*cp == '(' && *(cp - 1) == '"') {
1993-04-09 16:58:06 +04:00
string[length - 1] = '\0';
*r_perd = strdup(cp+1);
1993-04-09 16:58:06 +04:00
string[length - 1] = ')';
*(cp - 1) = '\0'; /* clobber the " */
*r_pers = strdup(string + 1);
1993-04-09 16:58:06 +04:00
*(cp - 1) = '"';
return true;
2009-08-13 07:07:49 +04:00
}
1993-04-09 16:58:06 +04:00
}
return false;
1993-04-09 16:58:06 +04:00
}
2009-08-13 06:10:50 +04:00
#endif /* 0 - unused */
1993-04-09 16:58:06 +04:00
2009-08-13 07:07:49 +04:00
static char cincomment[] = CINCOMMENT;
static char coutcomment[] = COUTCOMMENT;
static char fincomment[] = FINCOMMENT;
static char foutcomment[] = FOUTCOMMENT;
static char newline[] = NEWLINE;
static char piincomment[] = PIINCOMMENT;
static char pioutcomment[] = PIOUTCOMMENT;
static char lispincomment[] = LISPINCOMMENT;
static char riincomment[] = RIINCOMMENT;
static char rioutcomment[] = RIOUTCOMMENT;
static char troffincomment[] = TROFFINCOMMENT;
static char troffoutcomment[] = TROFFOUTCOMMENT;
static char mod2incomment[] = MOD2INCOMMENT;
static char mod2outcomment[] = MOD2OUTCOMMENT;
struct lang_desc lang_table[] = {
{ /*INUNKNOWN 0*/ "unknown", cincomment, coutcomment },
{ /*INCPP 1*/ "cpp", cincomment, coutcomment },
{ /*INCC 2*/ "cc", cincomment, coutcomment },
{ /*INAS 3*/ "as", ASINCOMMENT, newline },
{ /*INLD 4*/ "ld", cincomment, coutcomment },
{ /*INLINT 5*/ "lint", cincomment, coutcomment },
{ /*INF77 6*/ "f77", fincomment, foutcomment },
{ /*INPI 7*/ "pi", piincomment, pioutcomment },
{ /*INPC 8*/ "pc", piincomment, pioutcomment },
{ /*INFRANZ 9*/ "franz", lispincomment, newline },
{ /*INLISP 10*/ "lisp", lispincomment, newline },
{ /*INVAXIMA 11*/ "vaxima", lispincomment, newline },
{ /*INRATFOR 12*/ "ratfor", fincomment, foutcomment },
{ /*INLEX 13*/ "lex", cincomment, coutcomment },
{ /*INYACC 14*/ "yacc", cincomment, coutcomment },
{ /*INAPL 15*/ "apl", ".lm", newline },
{ /*INMAKE 16*/ "make", ASINCOMMENT, newline },
{ /*INRI 17*/ "ri", riincomment, rioutcomment },
{ /*INTROFF 18*/ "troff", troffincomment, troffoutcomment },
{ /*INMOD2 19*/ "mod2", mod2incomment, mod2outcomment },
{ 0, 0, 0 }
1993-04-09 16:58:06 +04:00
};
1997-10-18 18:44:21 +04:00
void
printerrors(bool look_at_subclass, int errorc, Eptr errorv[])
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
int i;
Eptr errorp;
1993-04-09 16:58:06 +04:00
2009-08-13 07:07:49 +04:00
for (errorp = errorv[i = 0]; i < errorc; errorp = errorv[++i]) {
1993-04-09 16:58:06 +04:00
if (errorp->error_e_class == C_IGNORE)
continue;
if (look_at_subclass && errorp->error_s_class == C_DUPL)
continue;
printf("Error %d, (%s error) [%s], text = \"",
i,
class_table[errorp->error_e_class],
lang_table[errorp->error_language].lang_name);
wordvprint(stdout,errorp->error_lgtext,errorp->error_text);
printf("\"\n");
}
}
1997-10-18 18:44:21 +04:00
void
wordvprint(FILE *fyle, int wordc, char **wordv)
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
int i;
2009-08-13 07:50:02 +04:00
const char *sep = "";
1993-04-09 16:58:06 +04:00
2009-08-13 07:07:49 +04:00
for (i = 0; i < wordc; i++)
1993-04-09 16:58:06 +04:00
if (wordv[i]) {
fprintf(fyle, "%s%s",sep,wordv[i]);
sep = " ";
}
}
/*
2009-08-13 07:07:49 +04:00
* Given a string, parse it into a number of words, and build
* a wordc wordv combination pointing into it.
1993-04-09 16:58:06 +04:00
*/
1997-10-18 18:44:21 +04:00
void
wordvbuild(char *string, int *r_wordc, char ***r_wordv)
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
char *cp;
char **wordv;
int wordcount;
int wordindex;
1993-04-09 16:58:06 +04:00
2009-08-13 07:07:49 +04:00
for (wordcount = 0, cp = string; *cp; wordcount++) {
while (*cp && isspace((unsigned char)*cp))
1993-04-09 16:58:06 +04:00
cp++;
if (*cp == '\0')
1993-04-09 16:58:06 +04:00
break;
1999-05-15 22:46:27 +04:00
while (*cp && !isspace((unsigned char)*cp))
1993-04-09 16:58:06 +04:00
cp++;
}
wordv = Calloc(wordcount + 1, sizeof (char *));
2009-08-13 07:07:49 +04:00
for (cp=string, wordindex=0; wordcount; wordindex++, --wordcount) {
1998-11-07 02:06:38 +03:00
while (*cp && isspace((unsigned char)*cp))
1993-04-09 16:58:06 +04:00
cp++;
if (*cp == '\0')
1993-04-09 16:58:06 +04:00
break;
wordv[wordindex] = cp;
2009-08-13 07:07:49 +04:00
while (*cp && !isspace((unsigned char)*cp))
1993-04-09 16:58:06 +04:00
cp++;
*cp++ = '\0';
}
if (wordcount != 0)
1997-10-18 18:44:21 +04:00
errx(6, "Initial miscount of the number of words in a line");
wordv[wordindex] = NULL;
1993-04-09 16:58:06 +04:00
#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;
}
1997-10-18 18:44:21 +04:00
1993-04-09 16:58:06 +04:00
/*
2009-08-13 07:07:49 +04:00
* Compare two 0 based wordvectors
1993-04-09 16:58:06 +04:00
*/
1997-10-18 18:44:21 +04:00
int
wordvcmp(char **wordv1, int wordc, char **wordv2)
1993-04-09 16:58:06 +04:00
{
1997-10-18 18:44:21 +04:00
int i;
2009-08-13 07:07:49 +04:00
int back;
1997-10-18 18:44:21 +04:00
2009-08-13 07:07:49 +04:00
for (i = 0; i < wordc; i++) {
if (wordv1[i] == NULL || wordv2[i] == NULL)
2009-08-13 07:07:49 +04:00
return (-1);
if ((back = strcmp(wordv1[i], wordv2[i])) != 0)
2009-08-13 07:07:49 +04:00
return (back);
1993-04-09 16:58:06 +04:00
}
2009-08-13 07:07:49 +04:00
return (0); /* they are equal */
1993-04-09 16:58:06 +04:00
}
2009-08-13 07:07:49 +04:00
1993-04-09 16:58:06 +04:00
/*
2009-08-13 07:07:49 +04:00
* splice a 0 basedword vector onto the tail of a
* new wordv, allowing the first emptyhead slots to be empty
1993-04-09 16:58:06 +04:00
*/
2009-08-13 07:07:49 +04:00
char **
wordvsplice(int emptyhead, int wordc, char **wordv)
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
char **nwordv;
int nwordc = emptyhead + wordc;
int i;
1993-04-09 16:58:06 +04:00
nwordv = Calloc(nwordc, sizeof (char *));
1993-04-09 16:58:06 +04:00
for (i = 0; i < emptyhead; i++)
nwordv[i] = NULL;
2009-08-13 07:07:49 +04:00
for (i = emptyhead; i < nwordc; i++) {
1993-04-09 16:58:06 +04:00
nwordv[i] = wordv[i-emptyhead];
}
2009-08-13 07:07:49 +04:00
return (nwordv);
1993-04-09 16:58:06 +04:00
}
1997-10-18 18:44:21 +04:00
1993-04-09 16:58:06 +04:00
/*
* plural and verb forms
1993-04-09 16:58:06 +04:00
*/
2009-08-13 07:50:02 +04:00
static const char *S = "s";
static const char *N = "";
1997-10-18 18:44:21 +04:00
2009-08-13 07:50:02 +04:00
const char *
plural(int n)
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
return (n > 1 ? S : N);
1993-04-09 16:58:06 +04:00
}
1997-10-18 18:44:21 +04:00
2009-08-13 07:50:02 +04:00
const char *
verbform(int n)
1993-04-09 16:58:06 +04:00
{
2009-08-13 07:07:49 +04:00
return (n > 1 ? N : S);
1993-04-09 16:58:06 +04:00
}