Move utilitarian routines to util.c, which isn't the same as the old

compatability-geared util.c.  These are things like message printers
and the PrintAddr function for traversing lists.  Other general-purpose
utilities inside make(1) can go here, in time.
This commit is contained in:
jmallett 2002-10-10 19:27:48 +00:00
parent 2b25357133
commit acfa6aea13
4 changed files with 288 additions and 234 deletions

View File

@ -5,7 +5,7 @@
PROG= make
CFLAGS+=-I${.CURDIR}
SRCS= arch.c buf.c compat.c cond.c dir.c for.c hash.c job.c main.c \
make.c parse.c str.c suff.c targ.c var.c
make.c parse.c str.c suff.c targ.c util.c var.c
SRCS+= lstAppend.c lstAtEnd.c lstAtFront.c lstClose.c lstConcat.c \
lstDatum.c lstDeQueue.c lstDestroy.c lstDupl.c lstEnQueue.c \
lstFind.c lstFindFrom.c lstFirst.c lstForEach.c lstForEachFrom.c \

View File

@ -58,21 +58,6 @@ __FBSDID("$FreeBSD$");
* treats them as if they were given when first
* invoked. Used by the parse module to implement
* the .MFLAGS target.
*
* Error Print a tagged error message. The global
* MAKE variable must have been defined. This
* takes a format string and two optional
* arguments for it.
*
* Fatal Print an error message and exit. Also takes
* a format string and two arguments.
*
* Punt Aborts all jobs and exits with a message. Also
* takes a format string and two arguments.
*
* Finish Finish things up by printing the number of
* errors which occured, as passed to it, and
* exiting.
*/
#include <sys/types.h>
@ -135,7 +120,7 @@ Boolean beVerbose; /* -v flag */
Boolean oldVars; /* variable substitution style */
Boolean checkEnvFirst; /* -e flag */
Lst envFirstVars; /* (-E) vars to override from env */
static Boolean jobsRunning; /* TRUE if the jobs might be running */
Boolean jobsRunning; /* TRUE if the jobs might be running */
static void MainParseArgs(int, char **);
char * chdir_verify_path(char *, char *);
@ -1138,215 +1123,6 @@ Cmd_Exec(char *cmd, char **error)
*res = '\0';
return res;
}
/*-
* Debug --
* Print a debugging message given its format.
*
* Results:
* None.
*
* Side Effects:
* The message is printed.
*/
/* VARARGS */
void
Debug(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fflush(stderr);
}
/*-
* Error --
* Print an error message given its format.
*
* Results:
* None.
*
* Side Effects:
* The message is printed.
*/
/* VARARGS */
void
Error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
(void)fflush(stderr);
}
/*-
* Fatal --
* Produce a Fatal error message. If jobs are running, waits for them
* to finish.
*
* Results:
* None
*
* Side Effects:
* The program exits
*/
/* VARARGS */
void
Fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (jobsRunning)
Job_Wait();
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
(void)fflush(stderr);
if (DEBUG(GRAPH2))
Targ_PrintGraph(2);
exit(2); /* Not 1 so -q can distinguish error */
}
/*
* Punt --
* Major exception once jobs are being created. Kills all jobs, prints
* a message and exits.
*
* Results:
* None
*
* Side Effects:
* All children are killed indiscriminately and the program Lib_Exits
*/
/* VARARGS */
void
Punt(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(void)fprintf(stderr, "make: ");
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
(void)fflush(stderr);
DieHorribly();
}
/*-
* DieHorribly --
* Exit without giving a message.
*
* Results:
* None
*
* Side Effects:
* A big one...
*/
void
DieHorribly(void)
{
if (jobsRunning)
Job_AbortAll();
if (DEBUG(GRAPH2))
Targ_PrintGraph(2);
exit(2); /* Not 1, so -q can distinguish error */
}
/*
* Finish --
* Called when aborting due to errors in child shell to signal
* abnormal exit, with the number of errors encountered in Make_Make.
*
* Results:
* None
*
* Side Effects:
* The program exits
*/
void
Finish(int errors)
{
Fatal("%d error%s", errors, errors == 1 ? "" : "s");
}
/*
* emalloc --
* malloc, but die on error.
*/
void *
emalloc(size_t len)
{
void *p;
if ((p = malloc(len)) == NULL)
enomem();
return(p);
}
/*
* estrdup --
* strdup, but die on error.
*/
char *
estrdup(const char *str)
{
char *p;
if ((p = strdup(str)) == NULL)
enomem();
return(p);
}
/*
* erealloc --
* realloc, but die on error.
*/
void *
erealloc(void *ptr, size_t size)
{
if ((ptr = realloc(ptr, size)) == NULL)
enomem();
return(ptr);
}
/*
* enomem --
* die when out of memory.
*/
void
enomem(void)
{
err(2, NULL);
}
/*
* enunlink --
* Remove a file carefully, avoiding directories.
*/
int
eunlink(const char *file)
{
struct stat st;
if (lstat(file, &st) == -1)
return -1;
if (S_ISDIR(st.st_mode)) {
errno = EISDIR;
return -1;
}
return unlink(file);
}
/*
* usage --
@ -1361,11 +1137,3 @@ usage(void)
" [variable=value] [target ...]");
exit(2);
}
int
PrintAddr(void *a, void *b __unused)
{
printf("%p ", a);
return 0;
}

View File

@ -276,6 +276,7 @@ extern Lst dirSearchPath; /* The list of directories to search when
extern Lst parseIncPath; /* The list of directories to search when
* looking for includes */
extern Boolean jobsRunning; /* True if jobs are running */
extern Boolean compatMake; /* True if we are make compatible */
extern Boolean ignoreErrors; /* True if should ignore all errors */
extern Boolean beSilent; /* True if should print no commands */

285
usr.bin/make/util.c Normal file
View File

@ -0,0 +1,285 @@
/*
* Copyright (c) 2002 Juli Mallett. All rights reserved.
* Copyright (c) 1988, 1989, 1990, 1993
* The Regents of the University of California. All rights reserved.
* Copyright (c) 1989 by Berkeley Softworks
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Adam de Boor.
*
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*
* @(#)main.c 8.3 (Berkeley) 3/19/94
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*-
* util.c --
* General utilitarian routines for make(1).
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <err.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sysexits.h>
#include <stdarg.h>
#include <unistd.h>
#include "make.h"
#include "hash.h"
#include "dir.h"
#include "job.h"
#include "pathnames.h"
/*-
* Debug --
* Print a debugging message given its format.
*
* Results:
* None.
*
* Side Effects:
* The message is printed.
*/
/* VARARGS */
void
Debug(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fflush(stderr);
}
/*-
* Error --
* Print an error message given its format.
*
* Results:
* None.
*
* Side Effects:
* The message is printed.
*/
/* VARARGS */
void
Error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
(void)fflush(stderr);
}
/*-
* Fatal --
* Produce a Fatal error message. If jobs are running, waits for them
* to finish.
*
* Results:
* None
*
* Side Effects:
* The program exits
*/
/* VARARGS */
void
Fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (jobsRunning)
Job_Wait();
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
(void)fflush(stderr);
if (DEBUG(GRAPH2))
Targ_PrintGraph(2);
exit(2); /* Not 1 so -q can distinguish error */
}
/*
* Punt --
* Major exception once jobs are being created. Kills all jobs, prints
* a message and exits.
*
* Results:
* None
*
* Side Effects:
* All children are killed indiscriminately and the program Lib_Exits
*/
/* VARARGS */
void
Punt(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(void)fprintf(stderr, "make: ");
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
(void)fflush(stderr);
DieHorribly();
}
/*-
* DieHorribly --
* Exit without giving a message.
*
* Results:
* None
*
* Side Effects:
* A big one...
*/
void
DieHorribly(void)
{
if (jobsRunning)
Job_AbortAll();
if (DEBUG(GRAPH2))
Targ_PrintGraph(2);
exit(2); /* Not 1, so -q can distinguish error */
}
/*
* Finish --
* Called when aborting due to errors in child shell to signal
* abnormal exit, with the number of errors encountered in Make_Make.
*
* Results:
* None
*
* Side Effects:
* The program exits
*/
void
Finish(int errors)
{
Fatal("%d error%s", errors, errors == 1 ? "" : "s");
}
/*
* emalloc --
* malloc, but die on error.
*/
void *
emalloc(size_t len)
{
void *p;
if ((p = malloc(len)) == NULL)
enomem();
return(p);
}
/*
* estrdup --
* strdup, but die on error.
*/
char *
estrdup(const char *str)
{
char *p;
if ((p = strdup(str)) == NULL)
enomem();
return(p);
}
/*
* erealloc --
* realloc, but die on error.
*/
void *
erealloc(void *ptr, size_t size)
{
if ((ptr = realloc(ptr, size)) == NULL)
enomem();
return(ptr);
}
/*
* enomem --
* die when out of memory.
*/
void
enomem(void)
{
err(2, NULL);
}
/*
* enunlink --
* Remove a file carefully, avoiding directories.
*/
int
eunlink(const char *file)
{
struct stat st;
if (lstat(file, &st) == -1)
return -1;
if (S_ISDIR(st.st_mode)) {
errno = EISDIR;
return -1;
}
return unlink(file);
}
/*
* Printaddr --
* Print the address of a node, used as an interative function.
*/
int
PrintAddr(void *a, void *b __unused)
{
printf("%p ", a);
return 0;
}