+/* --- @env_setting_p@ --- *
+ *
+ * Arguments: @const char *var@ = environment variable name
+ *
+ * Returns: Nonzero if the variable is set to a non-empty value,
+ * otherwise zero.
+ *
+ * Use: This is the recommended way to check the `NO_COLOR',
+ * `CLICOLOR' and `CLICOLOR_FORCE' variables.
+ */
+
+static int env_setting_p(const char *var)
+ { const char *p; p = getenv(var); return (p && *p); }
+
+/* --- @ttycolour_enablep@ --- *
+ *
+ * Arguments: @unsigned f@ = flags
+ *
+ * Returns: Nonzero if colours should be applied to output, otherwise
+ * zero.
+ *
+ * Use: This function determines whether it's generally a good idea
+ * to produce output in pretty colours. Set @TCEF_TTY@ if the
+ * output stream is -- or should be considered to be --
+ * interactive (e.g., according to @isatty@); set @TCEF_DFLT@ if
+ * the application prefers to produce coloured output if
+ * possible.
+ *
+ * The detailed behaviour is as follows. (Since the purpose of
+ * this function is to abide by common conventions and to be
+ * convenient for users, these details may change in future.)
+ *
+ * * If the `NO_COLOR' environment variable is non-empty, then
+ * colour is disabled (%%\url{https://no-color.org/}%%).
+ *
+ * * If the `TERM' variable is set to `dumb', then colour is
+ * disabled (Emacs).
+ *
+ * * If the `FORCE_COLOR' environment variable is non-empty,
+ * then colour is enabled, unless the value is 0, in which
+ * case colour is disabled (apparently from the Node
+ * community, %%\url{%%https://force-color.org/}%% and
+ * %%\url{https://nodejs.org/api/tty.html#writestreamgetcolordepthenv}%%).
+ *
+ * * If the `CLICOLOR_FORCE' environment variable is
+ * non-empty, then colour is enabled (apparently from
+ * Mac OS, (%%\url{http://bixense.com/clicolors/}%%).
+ *
+ * * If the @TCEF_TTY@ flag is clear, then colour is disabled.
+ *
+ * * If the @TCEF_DFLT@ flag is set, then colour is enabled.
+ *
+ * * If the `CLICOLOR' environment variable is non-empty, then
+ * colour is enabled (again, apparently from Mac OS,
+ * (%%\url{http://bixense.com/clicolors/}%%).
+ *
+ * * Otherwise, colour is disabled.
+ */
+
+int ttycolour_enablep(unsigned f)
+{
+ const char *t;
+
+ if (env_setting_p("NO_COLOR")) return (0);
+ else if (t = getenv("TERM"), !t || STRCMP(t, ==, "dumb")) return (0);
+ else if (t = getenv("FORCE_COLOR"), t && *t) return (*t == '0' ? 0 : 1);
+ else if (env_setting_p("CLICOLOR_FORCE")) return (1);
+ else if (!(f&TCEF_TTY)) return (0);
+ else if ((f&TCEF_DFLT) || env_setting_p("CLICOLOR")) return (1);
+ else return (0);
+}
+
+
+
+int ttycolour_config(struct tty_attr *attr, const char *user, unsigned f,
+ struct tty *tty, const struct ttycolour_style *tab)