#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)subr.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: subr.c,v 1.26 2023/08/26 15:18:27 rillig Exp $");
#endif
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
void
arrayify(int *e_length, Eptr **e_array, Eptr header)
{
Eptr errorp;
Eptr *array;
int listlength;
int listindex;
for (errorp = header, listlength = 0;
errorp != NULL; errorp = errorp->error_next, listlength++)
continue;
array = Calloc(listlength+1, sizeof (Eptr));
for (listindex = 0, errorp = header;
listindex < listlength;
listindex++, errorp = errorp->error_next) {
array[listindex] = errorp;
errorp->error_position = listindex;
}
array[listindex] = NULL;
*e_length = listlength;
*e_array = array;
}
void *
Calloc(size_t nelements, size_t size)
{
void *back;
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;
}
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;
}
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;
}
char
lastchar(const char *string)
{
size_t length;
if (string == NULL)
return '\0';
length = strlen(string);
if (length >= 1)
return string[length-1];
else
return '\0';
}
char
firstchar(const char *string)
{
if (string != NULL)
return string[0];
else
return '\0';
}
char
next_lastchar(const char *string)
{
size_t length;
if (string == NULL)
return '\0';
length = strlen(string);
if (length >= 2)
return string[length - 2];
else
return '\0';
}
void
clob_last(char *string, char newstuff)
{
if (string != NULL && string[0] != '\0')
string[strlen(string) - 1] = newstuff;
}
bool
persperdexplode(char *string, char **r_perd, char **r_pers)
{
char *cp;
size_t length = string != NULL ? strlen(string) : 0;
if (length >= 4 && string[length - 1] == ')') {
for (cp = &string[length - 2];
isdigit((unsigned char)*cp) && *cp != '(';
--cp)
continue;
if (*cp == '(') {
string[length - 1] = '\0';
*r_perd = strdup(cp+1);
string[length - 1] = ')';
*cp = '\0';
*r_pers = strdup(string);
*cp = '(';
return true;
}
}
return false;
}
#if 0
static bool
qpersperdexplode(char *string, char **r_perd, char **r_pers)
{
char *cp;
int length = 0;
if (string)
length = strlen(string);
if (length >= 4 && string[length - 1] == ')') {
for (cp = &string[length - 2];
isdigit((unsigned char)*cp) && *cp != '(';
--cp)
continue;
if (*cp == '(' && *(cp - 1) == '"') {
string[length - 1] = '\0';
*r_perd = strdup(cp+1);
string[length - 1] = ')';
*(cp - 1) = '\0';
*r_pers = strdup(string + 1);
*(cp - 1) = '"';
return true;
}
}
return false;
}
#endif
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[] = {
{ "unknown", cincomment, coutcomment },
{ "cpp", cincomment, coutcomment },
{ "cc", cincomment, coutcomment },
{ "as", ASINCOMMENT, newline },
{ "ld", cincomment, coutcomment },
{ "lint", cincomment, coutcomment },
{ "f77", fincomment, foutcomment },
{ "pi", piincomment, pioutcomment },
{ "pc", piincomment, pioutcomment },
{ "franz", lispincomment, newline },
{ "lisp", lispincomment, newline },
{ "vaxima", lispincomment, newline },
{ "ratfor", fincomment, foutcomment },
{ "lex", cincomment, coutcomment },
{ "yacc", cincomment, coutcomment },
{ "apl", ".lm", newline },
{ "make", ASINCOMMENT, newline },
{ "ri", riincomment, rioutcomment },
{ "troff", troffincomment, troffoutcomment },
{ "mod2", mod2incomment, mod2outcomment },
{ NULL, NULL, NULL }
};
void
printerrors(bool look_at_subclass, int errorc, Eptr errorv[])
{
int i;
Eptr errorp;
for (errorp = errorv[i = 0]; i < errorc; errorp = errorv[++i]) {
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");
}
}
void
wordvprint(FILE *fyle, int wordc, char **wordv)
{
const char *sep = "";
for (int i = 0; i < wordc; i++)
if (wordv[i] != NULL) {
fprintf(fyle, "%s%s",sep,wordv[i]);
sep = " ";
}
}
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;
}
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;
}
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;
}
const char *
plural(int n)
{
return n > 1 ? "s" : "";
}
const char *
verbform(int n)
{
return n > 1 ? "" : "s";
}