make(1): replace str_concat with str_concat2 and str_concat3

The new functions have a simpler interface, and str_concat3 is even more
general-purpose, since the middle string is no longer required to be
exactly of length 1.
This commit is contained in:
rillig 2020-08-10 19:53:19 +00:00
parent 21107e4e40
commit 644e5ad76b
7 changed files with 48 additions and 61 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: dir.c,v 1.86 2020/08/10 19:30:30 rillig Exp $ */
/* $NetBSD: dir.c,v 1.87 2020/08/10 19:53:19 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@ -70,14 +70,14 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: dir.c,v 1.86 2020/08/10 19:30:30 rillig Exp $";
static char rcsid[] = "$NetBSD: dir.c,v 1.87 2020/08/10 19:53:19 rillig Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)dir.c 8.2 (Berkeley) 1/2/94";
#else
__RCSID("$NetBSD: dir.c,v 1.86 2020/08/10 19:30:30 rillig Exp $");
__RCSID("$NetBSD: dir.c,v 1.87 2020/08/10 19:53:19 rillig Exp $");
#endif
#endif /* not lint */
#endif
@ -640,8 +640,7 @@ DirMatchFiles(const char *pattern, Path *p, Lst expansions)
{
(void)Lst_AtEnd(expansions,
(isDot ? bmake_strdup(entry->name) :
str_concat(p->name, entry->name,
STR_ADDSLASH)));
str_concat3(p->name, "/", entry->name)));
}
}
return 0;
@ -981,7 +980,7 @@ DirLookup(Path *p, const char *name MAKE_ATTR_UNUSED, const char *cp,
if (Hash_FindEntry(&p->files, cp) == NULL)
return NULL;
file = str_concat(p->name, cp, STR_ADDSLASH);
file = str_concat3(p->name, "/", cp);
if (DEBUG(DIR)) {
fprintf(debug_file, " returning %s\n", file);
}
@ -1012,7 +1011,7 @@ DirLookupSubdir(Path *p, const char *name)
char *file; /* the current filename to check */
if (p != dot) {
file = str_concat(p->name, name, STR_ADDSLASH);
file = str_concat3(p->name, "/", name);
} else {
/*
* Checking in dot -- DON'T put a leading ./ on the thing.
@ -1117,7 +1116,7 @@ DirFindDot(Boolean hasSlash MAKE_ATTR_UNUSED, const char *name, const char *cp)
}
hits += 1;
cur->hits += 1;
return str_concat(cur->name, cp, STR_ADDSLASH);
return str_concat3(cur->name, "/", cp);
}
return NULL;
@ -1735,8 +1734,8 @@ Dir_MakeFlags(const char *flag, Lst path)
if (Lst_Open(path) == SUCCESS) {
while ((ln = Lst_Next(path)) != NULL) {
p = (Path *)Lst_Datum(ln);
s2 = str_concat(flag, p->name, STR_ADDNONE);
str = str_concat(s1 = str, s2, STR_ADDSPACE);
s2 = str_concat2(flag, p->name);
str = str_concat3(s1 = str, " ", s2);
free(s1);
free(s2);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: job.c,v 1.205 2020/08/01 14:47:49 rillig Exp $ */
/* $NetBSD: job.c,v 1.206 2020/08/10 19:53:19 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@ -70,14 +70,14 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: job.c,v 1.205 2020/08/01 14:47:49 rillig Exp $";
static char rcsid[] = "$NetBSD: job.c,v 1.206 2020/08/10 19:53:19 rillig Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)job.c 8.2 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: job.c,v 1.205 2020/08/01 14:47:49 rillig Exp $");
__RCSID("$NetBSD: job.c,v 1.206 2020/08/10 19:53:19 rillig Exp $");
#endif
#endif /* not lint */
#endif
@ -2199,7 +2199,7 @@ Shell_Init(void)
shellName++;
} else
#endif
shellPath = str_concat(_PATH_DEFSHELLDIR, shellName, STR_ADDSLASH);
shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName);
}
if (commandShell->exit == NULL) {
commandShell->exit = "";

View File

@ -1,4 +1,4 @@
/* $NetBSD: main.c,v 1.302 2020/08/09 09:44:14 rillig Exp $ */
/* $NetBSD: main.c,v 1.303 2020/08/10 19:53:19 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -69,7 +69,7 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: main.c,v 1.302 2020/08/09 09:44:14 rillig Exp $";
static char rcsid[] = "$NetBSD: main.c,v 1.303 2020/08/10 19:53:19 rillig Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
@ -81,7 +81,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993\
#if 0
static char sccsid[] = "@(#)main.c 8.3 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: main.c,v 1.302 2020/08/09 09:44:14 rillig Exp $");
__RCSID("$NetBSD: main.c,v 1.303 2020/08/10 19:53:19 rillig Exp $");
#endif
#endif /* not lint */
#endif
@ -705,7 +705,7 @@ Main_ParseArgLine(const char *line)
if (!*line)
return;
buf = str_concat(argv0, line, STR_ADDSPACE);
buf = str_concat3(argv0, " ", line);
free(p1);
argv = brk_string(buf, &argc, TRUE, &args);

View File

@ -1,4 +1,4 @@
/* $NetBSD: nonints.h,v 1.92 2020/08/10 19:30:30 rillig Exp $ */
/* $NetBSD: nonints.h,v 1.93 2020/08/10 19:53:19 rillig Exp $ */
/*-
* Copyright (c) 1988, 1989, 1990, 1993
@ -134,14 +134,8 @@ void Parse_SetInput(const char *, int, int, char *(*)(void *, size_t *), void *)
Lst Parse_MainName(void);
/* str.c */
typedef enum {
STR_ADDNONE, /* just concat the two strings */
STR_ADDSPACE, /* add a space between the two strings */
STR_ADDSLASH /* add a slash between the two strings */
} StrConcatMode;
char *str_concat(const char *, const char *, StrConcatMode);
char *str_concat2(const char *, const char *);
char *str_concat3(const char *, const char *, const char *);
char **brk_string(const char *, int *, Boolean, char **);
char *Str_FindSubstring(const char *, const char *);
Boolean Str_Match(const char *, const char *);

View File

@ -1,4 +1,4 @@
/* $NetBSD: parse.c,v 1.250 2020/08/09 13:05:04 rillig Exp $ */
/* $NetBSD: parse.c,v 1.251 2020/08/10 19:53:19 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -69,14 +69,14 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: parse.c,v 1.250 2020/08/09 13:05:04 rillig Exp $";
static char rcsid[] = "$NetBSD: parse.c,v 1.251 2020/08/10 19:53:19 rillig Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)parse.c 8.3 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: parse.c,v 1.250 2020/08/09 13:05:04 rillig Exp $");
__RCSID("$NetBSD: parse.c,v 1.251 2020/08/10 19:53:19 rillig Exp $");
#endif
#endif /* not lint */
#endif
@ -2219,7 +2219,7 @@ Parse_include_file(char *file, Boolean isSystem, Boolean depinc, int silent)
break;
*prefEnd = '\0';
}
newName = str_concat(incdir, file + i, STR_ADDSLASH);
newName = str_concat3(incdir, "/", file + i);
fullname = Dir_FindFile(newName, parseIncPath);
if (fullname == NULL)
fullname = Dir_FindFile(newName, dirSearchPath);

View File

@ -1,4 +1,4 @@
/* $NetBSD: str.c,v 1.58 2020/08/10 19:30:30 rillig Exp $ */
/* $NetBSD: str.c,v 1.59 2020/08/10 19:53:19 rillig Exp $ */
/*-
* Copyright (c) 1988, 1989, 1990, 1993
@ -69,49 +69,43 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: str.c,v 1.58 2020/08/10 19:30:30 rillig Exp $";
static char rcsid[] = "$NetBSD: str.c,v 1.59 2020/08/10 19:53:19 rillig Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)str.c 5.8 (Berkeley) 6/1/90";
#else
__RCSID("$NetBSD: str.c,v 1.58 2020/08/10 19:30:30 rillig Exp $");
__RCSID("$NetBSD: str.c,v 1.59 2020/08/10 19:53:19 rillig Exp $");
#endif
#endif /* not lint */
#endif
#include "make.h"
/*-
* str_concat --
* concatenate the two strings, inserting a space or slash between them.
*
* returns --
* the resulting string in allocated space.
*/
/* Return the concatenation of s1 and s2, freshly allocated. */
char *
str_concat(const char *s1, const char *s2, StrConcatMode mode)
str_concat2(const char *s1, const char *s2)
{
size_t len1 = strlen(s1);
size_t len2 = strlen(s2);
char *result = bmake_malloc(len1 + 1 + len2 + 1);
/* copy first string into place */
char *result = bmake_malloc(len1 + len2 + 1);
memcpy(result, s1, len1);
/* add separator character */
if (mode == STR_ADDSPACE) {
result[len1] = ' ';
++len1;
} else if (mode == STR_ADDSLASH) {
result[len1] = '/';
++len1;
}
/* copy second string plus EOS into place */
memcpy(result + len1, s2, len2 + 1);
return result;
}
/* Return the concatenation of s1, s2 and s3, freshly allocated. */
char *
str_concat3(const char *s1, const char *s2, const char *s3)
{
size_t len1 = strlen(s1);
size_t len2 = strlen(s2);
size_t len3 = strlen(s3);
char *result = bmake_malloc(len1 + len2 + len3 + 1);
memcpy(result, s1, len1);
memcpy(result + len1, s2, len2);
memcpy(result + len1 + len2, s3, len3 + 1);
return result;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: suff.c,v 1.94 2020/08/10 19:30:30 rillig Exp $ */
/* $NetBSD: suff.c,v 1.95 2020/08/10 19:53:19 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -69,14 +69,14 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: suff.c,v 1.94 2020/08/10 19:30:30 rillig Exp $";
static char rcsid[] = "$NetBSD: suff.c,v 1.95 2020/08/10 19:53:19 rillig Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)suff.c 8.4 (Berkeley) 3/21/94";
#else
__RCSID("$NetBSD: suff.c,v 1.94 2020/08/10 19:30:30 rillig Exp $");
__RCSID("$NetBSD: suff.c,v 1.95 2020/08/10 19:53:19 rillig Exp $");
#endif
#endif /* not lint */
#endif
@ -1222,7 +1222,7 @@ SuffAddSrc(void *sp, void *lsp)
#endif
}
s2 = bmake_malloc(sizeof(Src));
s2->file = str_concat(targ->pref, s->name, STR_ADDNONE);
s2->file = str_concat2(targ->pref, s->name);
s2->pref = targ->pref;
s2->parent = targ;
s2->node = NULL;
@ -1821,7 +1821,7 @@ SuffApplyTransform(GNode *tGn, GNode *sGn, Suff *t, Suff *s)
/*
* Locate the transformation rule itself
*/
tname = str_concat(s->name, t->name, STR_ADDNONE);
tname = str_concat2(s->name, t->name);
ln = Lst_Find(transforms, tname, SuffGNHasNameP);
free(tname);