From a178d532020aa159b27fa0fa5e9bb7490c5c35e6 Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Thu, 6 Dec 2007 10:48:00 +0000 Subject: [PATCH] Move RC_ to EINFO_ for all env vars related to einfo. --- sh/functions.sh | 14 ++--- src/_usage.h | 6 +- src/einfo.h | 2 +- src/fstabinfo.c | 2 +- src/libeinfo.c | 118 ++++++++++++++++++++++++---------------- src/librc-misc.c | 2 +- src/mountinfo.c | 2 +- src/rc-misc.c | 40 +++++++++++--- src/rc-misc.h | 1 + src/rc-status.c | 4 +- src/rc-update.c | 2 +- src/rc.c | 6 +- src/runscript.c | 4 +- src/start-stop-daemon.c | 4 +- 14 files changed, 129 insertions(+), 78 deletions(-) diff --git a/sh/functions.sh b/sh/functions.sh index fd67cb2b..00828f54 100644 --- a/sh/functions.sh +++ b/sh/functions.sh @@ -26,14 +26,14 @@ RC_GOT_FUNCTIONS="yes" eindent() { - RC_EINDENT=$((${RC_EINDENT:-0} + 2)) - [ "${RC_EINDENT}" -gt 40 ] && RC_EINDENT=40 - export RC_EINDENT + EINFO_INDENT=$((${EINFO_INDENT:-0} + 2)) + [ "${EINFO_INDENT}" -gt 40 ] && EINFO_INDENT=40 + export EINFO_INDENT } eoutdent() { - RC_EINDENT=$((${RC_EINDENT:-0} - 2)) - [ "${RC_EINDENT}" -lt 0 ] && RC_EINDENT=0 + EINFO_INDENT=$((${EINFO_INDENT:-0} - 2)) + [ "${EINFO_INDENT}" -lt 0 ] && EINFO_INDENT=0 return 0 } @@ -166,12 +166,12 @@ unset _sanitize_path for arg; do case "${arg}" in --nocolor|--nocolour|-C) - export RC_NOCOLOR="yes" + export EINFO_COLOR="NO" ;; esac done -if [ -z "${GOOD}" ] && ! yesno "${RC_NOCOLOR}"; then +if [ -z "${GOOD}" ] && yesno ${EINFO_COLOR:-YES}; then eval $(eval_ecolors) fi diff --git a/src/_usage.h b/src/_usage.h index d5529b69..6ee391ae 100644 --- a/src/_usage.h +++ b/src/_usage.h @@ -40,10 +40,10 @@ "Run verbosely", \ "Run quietly" -#define case_RC_COMMON_getopt_case_C setenv ("RC_NOCOLOR", "yes", 1); +#define case_RC_COMMON_getopt_case_C setenv ("EINFO_COLOR", "NO", 1); #define case_RC_COMMON_getopt_case_h usage (EXIT_SUCCESS); -#define case_RC_COMMON_getopt_case_v setenv ("RC_VERBOSE", "yes", 1); -#define case_RC_COMMON_getopt_case_q setenv ("RC_QUIET", "yes", 1); +#define case_RC_COMMON_getopt_case_v setenv ("EINFO_VERBOSE", "YES", 1); +#define case_RC_COMMON_getopt_case_q setenv ("EINFO_QUIET", "YES", 1); #define case_RC_COMMON_getopt_default usage (EXIT_FAILURE); #define case_RC_COMMON_GETOPT \ diff --git a/src/einfo.h b/src/einfo.h index feed9fff..990ae6fd 100644 --- a/src/einfo.h +++ b/src/einfo.h @@ -71,7 +71,7 @@ void elog (int __level, const char *__fmt, ...) __EEND_PRINTF; * - eerror - red * * The n suffix denotes that no new line should be printed. - * The v suffix means only print if RC_VERBOSE is yes. + * The v suffix means only print if EINFO_VERBOSE is yes. */ /*@{*/ int einfon (const char *__fmt, ...) __EINFO_PRINTF; diff --git a/src/fstabinfo.c b/src/fstabinfo.c index ffb42a1f..9888fff7 100644 --- a/src/fstabinfo.c +++ b/src/fstabinfo.c @@ -205,7 +205,7 @@ int fstabinfo (int argc, char **argv) } /* No point in outputting if quiet */ - if (rc_yesno (getenv ("RC_QUIET"))) + if (rc_yesno (getenv ("EINFO_QUIET"))) continue; switch (output) { diff --git a/src/libeinfo.c b/src/libeinfo.c index fdbf18bf..bff93302 100644 --- a/src/libeinfo.c +++ b/src/libeinfo.c @@ -86,12 +86,10 @@ hidden_proto(ewendv) #define OK "ok" #define NOT_OK "!!" -#define CHECK_VERBOSE if (! is_env ("RC_VERBOSE", "yes")) return 0 - - /* Number of spaces for an indent */ +/* Number of spaces for an indent */ #define INDENT_WIDTH 2 - /* How wide can the indent go? */ +/* How wide can the indent go? */ #define INDENT_MAX 40 /* Default colours */ @@ -154,18 +152,50 @@ static bool term_is_cons25 = false; /* A pointer to a string to prefix to einfo/ewarn/eerror messages */ static const char *_eprefix = NULL; -static bool is_env (const char *var, const char *val) +static bool yesno (const char *value) { - char *v; - - if (! var) + if (! value) { + errno = ENOENT; return (false); + } - v = getenv (var); - if (! v) - return (val ? false : true); + if (strcasecmp (value, "yes") == 0 || + strcasecmp (value, "y") == 0 || + strcasecmp (value, "true") == 0 || + strcasecmp (value, "on") == 0 || + strcasecmp (value, "1") == 0) + return (true); - return (strcasecmp (v, val) ? false : true); + if (strcasecmp (value, "no") != 0 && + strcasecmp (value, "n") != 0 && + strcasecmp (value, "false") != 0 && + strcasecmp (value, "off") != 0 && + strcasecmp (value, "0") != 0) + errno = EINVAL; + + return (false); +} + +static bool noyes (const char *value) { + int serrno = errno; + bool retval; + + errno = 0; + retval = yesno (value); + if (errno == 0) { + retval = ! retval; + errno = serrno; + } + + return (retval); +} + +static bool is_quiet() { + return (yesno (getenv ("EINFO_QUIET"))); +} + +static bool is_verbose() { + return (yesno (getenv ("EINFO_VERBOSE"))); } static bool colour_terminal (FILE *f) @@ -176,7 +206,7 @@ static bool colour_terminal (FILE *f) if (f && ! isatty (fileno (f))) return (false); - if (is_env ("RC_NOCOLOR", "yes")) + if (noyes (getenv ("EINFO_COLOR"))) return (false); if (in_colour == 0) @@ -234,7 +264,7 @@ hidden_def(eprefix) static void elogv (int level, const char *fmt, va_list ap) { - char *e = getenv ("RC_ELOG"); + char *e = getenv ("EINFO_LOG"); va_list apc; if (fmt && e) { @@ -259,7 +289,7 @@ hidden_def(elog) static int _eindent (FILE *stream) { - char *env = getenv ("RC_EINDENT"); + char *env = getenv ("EINFO_INDENT"); int amount = 0; char indent[INDENT_MAX]; @@ -377,7 +407,7 @@ int einfon (const char *fmt, ...) int retval; va_list ap; - if (! fmt || is_env ("RC_QUIET", "yes")) + if (! fmt || is_quiet ()) return (0); va_start (ap, fmt); @@ -393,7 +423,7 @@ int ewarnn (const char *fmt, ...) int retval; va_list ap; - if (! fmt || is_env ("RC_QUIET", "yes")) + if (! fmt || is_quiet ()) return (0); va_start (ap, fmt); @@ -422,7 +452,7 @@ int einfo (const char *fmt, ...) int retval; va_list ap; - if (! fmt || is_env ("RC_QUIET", "yes")) + if (! fmt || is_quiet()) return (0); va_start (ap, fmt); @@ -439,7 +469,7 @@ int ewarn (const char *fmt, ...) int retval; va_list ap; - if (! fmt || is_env ("RC_QUIET", "yes")) + if (! fmt || is_quiet ()) return (0); va_start (ap, fmt); @@ -457,7 +487,7 @@ void ewarnx (const char *fmt, ...) int retval; va_list ap; - if (fmt && ! is_env ("RC_QUIET", "yes")) { + if (fmt && ! is_quiet ()) { va_start (ap, fmt); elogv (LOG_WARNING, fmt, ap); retval = _ewarnvn (fmt, ap); @@ -507,7 +537,7 @@ int ebegin (const char *fmt, ...) int retval; va_list ap; - if (! fmt || is_env ("RC_QUIET", "yes")) + if (! fmt || is_quiet ()) return (0); va_start (ap, fmt); @@ -584,7 +614,7 @@ int eend (int retval, const char *fmt, ...) { va_list ap; - if (is_env ("RC_QUIET", "yes")) + if (is_quiet ()) return (retval); va_start (ap, fmt); @@ -599,7 +629,7 @@ int ewend (int retval, const char *fmt, ...) { va_list ap; - if (is_env ("RC_QUIET", "yes")) + if (is_quiet ()) return (retval); va_start (ap, fmt); @@ -618,7 +648,7 @@ hidden_def(ebracket) void eindent (void) { - char *env = getenv ("RC_EINDENT"); + char *env = getenv ("EINFO_INDENT"); int amount = 0; char num[10]; @@ -634,13 +664,13 @@ void eindent (void) amount = INDENT_MAX; snprintf (num, 10, "%08d", amount); - setenv ("RC_EINDENT", num, 1); + setenv ("EINFO_INDENT", num, 1); } hidden_def(eindent) void eoutdent (void) { - char *env = getenv ("RC_EINDENT"); + char *env = getenv ("EINFO_INDENT"); int amount = 0; char num[10]; @@ -655,10 +685,10 @@ void eoutdent (void) amount -= INDENT_WIDTH; if (amount <= 0) - unsetenv ("RC_EINDENT"); + unsetenv ("EINFO_EINDENT"); else { snprintf (num, 10, "%08d", amount); - setenv ("RC_EINDENT", num, 1); + setenv ("EINFO_EINDENT", num, 1); } } hidden_def(eoutdent) @@ -668,9 +698,7 @@ int einfovn (const char *fmt, ...) int retval; va_list ap; - CHECK_VERBOSE; - - if (! fmt) + if (! fmt || ! is_verbose ()) return (0); va_start (ap, fmt); @@ -686,9 +714,7 @@ int ewarnvn (const char *fmt, ...) int retval; va_list ap; - CHECK_VERBOSE; - - if (! fmt) + if (! fmt || ! is_verbose ()) return (0); va_start (ap, fmt); @@ -704,9 +730,7 @@ int einfov (const char *fmt, ...) int retval; va_list ap; - CHECK_VERBOSE; - - if (! fmt) + if (! fmt || ! is_verbose ()) return (0); va_start (ap, fmt); @@ -723,9 +747,7 @@ int ewarnv (const char *fmt, ...) int retval; va_list ap; - CHECK_VERBOSE; - - if (! fmt) + if (! fmt || ! is_verbose ()) return (0); va_start (ap, fmt); @@ -742,9 +764,7 @@ int ebeginv (const char *fmt, ...) int retval; va_list ap; - CHECK_VERBOSE; - - if (! fmt) + if (! fmt || ! is_verbose ()) return (0); va_start (ap, fmt); @@ -761,8 +781,9 @@ hidden_def(ebeginv) int eendv (int retval, const char *fmt, ...) { va_list ap; - - CHECK_VERBOSE; + + if (! is_verbose ()) + return (0); va_start (ap, fmt); _do_eend ("eendv", retval, fmt, ap); @@ -776,7 +797,8 @@ int ewendv (int retval, const char *fmt, ...) { va_list ap; - CHECK_VERBOSE; + if (! is_verbose ()) + return (0); va_start (ap, fmt); _do_eend ("ewendv", retval, fmt, ap); @@ -788,14 +810,14 @@ hidden_def(ewendv) void eindentv (void) { - if (is_env ("RC_VERBOSE", "yes")) + if (is_verbose ()) eindent (); } hidden_def(eindentv) void eoutdentv (void) { - if (is_env ("RC_VERBOSE", "yes")) + if (is_verbose ()) eoutdent (); } hidden_def(eoutdentv) diff --git a/src/librc-misc.c b/src/librc-misc.c index a8c9f33d..4df05ecf 100644 --- a/src/librc-misc.c +++ b/src/librc-misc.c @@ -35,7 +35,7 @@ bool rc_yesno (const char *value) { if (! value) { - errno = EINVAL; + errno = ENOENT; return (false); } diff --git a/src/mountinfo.c b/src/mountinfo.c index 4fed2b1a..09003d4a 100644 --- a/src/mountinfo.c +++ b/src/mountinfo.c @@ -443,7 +443,7 @@ int mountinfo (int argc, char **argv) rc_strlist_reverse (nodes); result = EXIT_FAILURE; - quiet = rc_yesno (getenv ("RC_QUIET")); + quiet = rc_yesno (getenv ("EINFO_QUIET")); STRLIST_FOREACH (nodes, n, i) { if (point_regex && regexec (point_regex, n, 0, NULL, 0) != 0) continue; diff --git a/src/rc-misc.c b/src/rc-misc.c index dfedbdc9..7a0a5ef9 100644 --- a/src/rc-misc.c +++ b/src/rc-misc.c @@ -63,7 +63,7 @@ static void _free_rc_conf (void) rc_strlist_free (rc_conf); } -bool rc_conf_yesno (const char *setting) +char *rc_conf_value (const char *setting) { if (! rc_conf) { char *line; @@ -88,10 +88,14 @@ bool rc_conf_yesno (const char *setting) p++; } } - } - return (rc_yesno (rc_config_value (rc_conf, setting))); + return (rc_config_value (rc_conf, setting)); +} + +bool rc_conf_yesno (const char *setting) +{ + return (rc_yesno (rc_conf_value (setting))); } char **env_filter (void) @@ -181,10 +185,10 @@ char **env_filter (void) However, we do need a path, so use a default. */ if (! got_path) { env_len = strlen ("PATH=") + strlen (PATH_PREFIX) + 2; - p = xmalloc (sizeof (char) * env_len); - snprintf (p, env_len, "PATH=%s", PATH_PREFIX); - rc_strlist_add (&env, p); - free (p); + e = xmalloc (sizeof (char) * env_len); + snprintf (e, env_len, "PATH=%s", PATH_PREFIX); + rc_strlist_add (&env, e); + free (e); } rc_strlist_free (whitelist); @@ -242,6 +246,7 @@ char **env_config (void) FILE *fp; char buffer[PATH_MAX]; char *runlevel = rc_runlevel_get (); + char *p; /* One char less to drop the trailing / */ i = strlen ("RC_LIBDIR=") + strlen (RC_LIBDIR) + 1; @@ -323,6 +328,27 @@ char **env_config (void) free (line); } + /* Be quiet or verbose as necessary */ + if ((p = rc_conf_value ("rc_quiet"))) { + i = strlen ("EINFO_QUIET=") + strlen (p) + 1; + line = xmalloc (sizeof (char) * i); + snprintf (line, i, "EINFO_QUIET=%s", p); + rc_strlist_add (&env, line); + free (line); + } + if ((p = rc_conf_value ("rc_verbose"))) { + i = strlen ("EINFO_VERBOSE=") + strlen (p) + 1; + line = xmalloc (sizeof (char) * i); + snprintf (line, i, "EINFO_VERBOSE=%s", p); + rc_strlist_add (&env, line); + free (line); + } + + errno = 0; + if ((! rc_conf_yesno ("rc_color") && errno == 0) || + rc_conf_yesno ("rc_nocolor")) + rc_strlist_add (&env, "EINFO_COLOR=no"); + free (runlevel); return (env); } diff --git a/src/rc-misc.h b/src/rc-misc.h index 040cc116..bb3b33dc 100644 --- a/src/rc-misc.h +++ b/src/rc-misc.h @@ -111,6 +111,7 @@ static inline bool exists (const char *pathname) return (stat (pathname, &buf) == 0); } +char *rc_conf_value (const char *var); bool rc_conf_yesno (const char *var); char **env_filter (void); char **env_config (void); diff --git a/src/rc-status.c b/src/rc-status.c index 3958b71a..50d05edc 100644 --- a/src/rc-status.c +++ b/src/rc-status.c @@ -82,7 +82,9 @@ static void print_service (char *service) } else snprintf (status, sizeof (status), " stopped "); - if (isatty (fileno (stdout)) && ! rc_yesno (getenv ("RC_NOCOLOR"))) + errno = 0; + if ((rc_yesno (getenv ("EINFO_COLOR")) || errno == ENOENT) && + isatty (fileno (stdout))) printf ("\n"); ebracket (cols, color, status); } diff --git a/src/rc-update.c b/src/rc-update.c index 02a650fe..04f6cdff 100644 --- a/src/rc-update.c +++ b/src/rc-update.c @@ -185,7 +185,7 @@ int rc_update (int argc, char **argv) } } - verbose = rc_yesno (getenv ("RC_VERBOSE")); + verbose = rc_yesno (getenv ("EINFO_VERBOSE")); if ((action & DOSHOW && action != DOSHOW) || (action & DOADD && action != DOADD) || diff --git a/src/rc.c b/src/rc.c index 61c82cf6..c20a760c 100644 --- a/src/rc.c +++ b/src/rc.c @@ -216,8 +216,8 @@ static int do_e (int argc, char **argv) if (argc < 3) eerrorx ("%s: not enough arguments", applet); - unsetenv ("RC_ELOG"); - setenv ("RC_ELOG", argv[1], 1); + unsetenv ("EINFO_LOG"); + setenv ("EINFO_LOG", argv[1], 1); argc -= 2; argv += 2; @@ -939,7 +939,7 @@ int main (int argc, char **argv) eerrorx ("%s: root access required", applet); /* Enable logging */ - setenv ("RC_ELOG", "rc", 1); + setenv ("EINFO_LOG", "rc", 1); /* Export our PID */ snprintf (pidstr, sizeof (pidstr), "%d", getpid ()); diff --git a/src/runscript.c b/src/runscript.c index f81db232..8b60c17c 100644 --- a/src/runscript.c +++ b/src/runscript.c @@ -1098,8 +1098,8 @@ int runscript (int argc, char **argv) if ((softlevel = xstrdup (getenv ("RC_SOFTLEVEL"))) == NULL) { /* Ensure our environment is pure Also, add our configuration to it */ - tmplist = env_config (); env = env_filter (); + tmplist = env_config (); rc_strlist_join (&env, tmplist); rc_strlist_free (tmplist); tmplist = NULL; @@ -1135,7 +1135,7 @@ int runscript (int argc, char **argv) softlevel = rc_runlevel_get (); } - setenv ("RC_ELOG", service, 1); + setenv ("EINFO_LOG", service, 1); setenv ("SVCNAME", applet, 1); /* Set an env var so that we always know our pid regardless of any diff --git a/src/start-stop-daemon.c b/src/start-stop-daemon.c index 989a6e20..1f59d986 100644 --- a/src/start-stop-daemon.c +++ b/src/start-stop-daemon.c @@ -708,8 +708,8 @@ int start_stop_daemon (int argc, char **argv) case_RC_COMMON_GETOPT } - quiet = rc_yesno (getenv ("RC_QUIET")); - verbose = rc_yesno (getenv ("RC_VERBOSE")); + quiet = rc_yesno (getenv ("EINFO_QUIET")); + verbose = rc_yesno (getenv ("EINFO_VERBOSE")); /* Allow start-stop-daemon --signal HUP --exec /usr/sbin/dnsmasq * instead of forcing --stop --oknodo as well */