From 6bce15fd41d37c023643ca2f7e9f3150494d9356 Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Sun, 9 Oct 2011 17:47:24 +0000 Subject: [PATCH] Add $ESC(code), which allows changing text color and attributes in banners. git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@615 db0b04b0-f4d1-0310-9a6d-de3e77497b0e --- dgamelaunch.c | 50 +++++++++++++++++++++++++++++++++++++++ examples/dgamelaunch.conf | 2 ++ 2 files changed, 52 insertions(+) diff --git a/dgamelaunch.c b/dgamelaunch.c index 463b958..fca1b08 100644 --- a/dgamelaunch.c +++ b/dgamelaunch.c @@ -369,6 +369,55 @@ idle_alarm_reset(void) /* ************************************************************* */ +char * +bannerfuncmangle(char *buf, char *fromstr, char *(*strmangler)(char *)) +{ + static char bufnew[81]; + char *loc; + char *b = buf; + char *bnpos = bufnew; + memset(bufnew, 0, 80); + + if (strstr(b, fromstr)) { + while ((loc = strstr(b, fromstr)) != NULL) { + char *fn = loc + strlen(fromstr); + if (*fn == '(') { + char *fn_end = strchr(fn, ')'); + if (fn_end) { + char funcparam[81]; + char *funcout; + fn++; + memset(funcparam, 0, 80); + memcpy(funcparam, fn, (fn_end - fn)); + funcout = (*strmangler)(funcparam); + memcpy(bnpos, b, (loc - b)); + bnpos += strlen(bnpos); + b = fn_end; + b++; + memcpy(bnpos, funcout, strlen(funcout)); + bnpos += strlen(funcout); + } + } + } + } + strcpy(bnpos, b); + return bufnew; +} + +char * +bannermangler_func_esc(char *str) +{ + static char buf[11]; + int len = strlen(str); + if (len > 7) len = 7; + buf[0] = '\x1b'; + buf[1] = '['; + strncpy((buf + 2), str, len); + buf[len+2] = 'm'; + buf[len+3] = '\0'; + return buf; +} + char * bannerstrmangle(char *buf, char *fromstr, char *tostr) @@ -527,6 +576,7 @@ loadbanner (char *fname, struct dg_banner *ban) } else { strncpy(bufnew, bannerstrmangle(bufnew, "$USERNAME", "[Anonymous]"), 80); } + strncpy(bufnew, bannerfuncmangle(bufnew, "$ESC", bannermangler_func_esc), 80); banner_addline(ban, bufnew); } diff --git a/examples/dgamelaunch.conf b/examples/dgamelaunch.conf index a5f92c0..c1a1079 100644 --- a/examples/dgamelaunch.conf +++ b/examples/dgamelaunch.conf @@ -58,6 +58,8 @@ bannervars = [ "$MOTDTIME" = "2011.10.08", # - $VERSION replaced with "dgamelaunch v" + dgl version number. # - $USERNAME replaced with logged-in user's name, or with "[Anonymous]" # - $INCLUDE(filename) the named file will be inserted here. +# - $ESC(code) Allows to use ECMA-48 SGR escape sequences, setting +# text color and attributes. will output "ESC [ code m" banner = "/dgl-banner" # The following two options are fairly insecure. They will force us to -- 2.47.3