code-golfing: cleanup osc color related code
* adds missing function prototype * move xgetcolor() prototype to win.h (that's where all the other x.c func prototype seems to be declared at) * check for snprintf error/truncation * reduces code duplication for osc 10/11/12 * unify osc_color_response() and osc4_color_response() into a single function the latter two was suggested by Quentin Rameau in his patch review on the hackers list.
This commit is contained in:
parent
f95e3c9ba4
commit
8594517e89
3 changed files with 57 additions and 3 deletions
56
st.c
56
st.c
|
@ -168,6 +168,7 @@ static void csidump(void);
|
|||
static void csihandle(void);
|
||||
static void csiparse(void);
|
||||
static void csireset(void);
|
||||
static void osc_color_response(int, int, int);
|
||||
static int eschandle(uchar);
|
||||
static void strdump(void);
|
||||
static void strhandle(void);
|
||||
|
@ -276,7 +277,7 @@ xrealloc(void *p, size_t len)
|
|||
}
|
||||
|
||||
char *
|
||||
xstrdup(char *s)
|
||||
xstrdup(const char *s)
|
||||
{
|
||||
if ((s = strdup(s)) == NULL)
|
||||
die("strdup: %s\n", strerror(errno));
|
||||
|
@ -1900,11 +1901,41 @@ csireset(void)
|
|||
memset(&csiescseq, 0, sizeof(csiescseq));
|
||||
}
|
||||
|
||||
void
|
||||
osc_color_response(int num, int index, int is_osc4)
|
||||
{
|
||||
int n;
|
||||
char buf[32];
|
||||
unsigned char r, g, b;
|
||||
|
||||
if (xgetcolor(is_osc4 ? num : index, &r, &g, &b)) {
|
||||
fprintf(stderr, "erresc: failed to fetch %s color %d\n",
|
||||
is_osc4 ? "osc4" : "osc",
|
||||
is_osc4 ? num : index);
|
||||
return;
|
||||
}
|
||||
|
||||
n = snprintf(buf, sizeof buf, "\033]%s%d;rgb:%02x%02x/%02x%02x/%02x%02x\007",
|
||||
is_osc4 ? "4;" : "", num, r, r, g, g, b, b);
|
||||
if (n < 0 || n >= sizeof(buf)) {
|
||||
fprintf(stderr, "error: %s while printing %s response\n",
|
||||
n < 0 ? "snprintf failed" : "truncation occurred",
|
||||
is_osc4 ? "osc4" : "osc");
|
||||
} else {
|
||||
ttywrite(buf, n, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
strhandle(void)
|
||||
{
|
||||
char *p = NULL, *dec;
|
||||
int j, narg, par;
|
||||
const struct { int idx; char *str; } osc_table[] = {
|
||||
{ defaultfg, "foreground" },
|
||||
{ defaultbg, "background" },
|
||||
{ defaultcs, "cursor" }
|
||||
};
|
||||
|
||||
term.esc &= ~(ESC_STR_END|ESC_STR);
|
||||
strparse();
|
||||
|
@ -1938,6 +1969,24 @@ strhandle(void)
|
|||
}
|
||||
}
|
||||
return;
|
||||
case 10:
|
||||
case 11:
|
||||
case 12:
|
||||
if (narg < 2)
|
||||
break;
|
||||
p = strescseq.args[1];
|
||||
if ((j = par - 10) < 0 || j >= LEN(osc_table))
|
||||
break; /* shouldn't be possible */
|
||||
|
||||
if (!strcmp(p, "?")) {
|
||||
osc_color_response(par, osc_table[j].idx, 0);
|
||||
} else if (xsetcolorname(osc_table[j].idx, p)) {
|
||||
fprintf(stderr, "erresc: invalid %s color: %s\n",
|
||||
osc_table[j].str, p);
|
||||
} else {
|
||||
tfulldirt();
|
||||
}
|
||||
return;
|
||||
case 4: /* color set */
|
||||
if (narg < 3)
|
||||
break;
|
||||
|
@ -1945,7 +1994,10 @@ strhandle(void)
|
|||
/* FALLTHROUGH */
|
||||
case 104: /* color reset, here p = NULL */
|
||||
j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
|
||||
if (xsetcolorname(j, p)) {
|
||||
|
||||
if (p && !strcmp(p, "?")) {
|
||||
osc_color_response(j, 0, 1);
|
||||
} else if (xsetcolorname(j, p)) {
|
||||
if (par == 104 && narg <= 1)
|
||||
return; /* color reset without parameter */
|
||||
fprintf(stderr, "erresc: invalid color j=%d, p=%s\n",
|
||||
|
|
3
st.h
3
st.h
|
@ -119,7 +119,7 @@ size_t utf8encode(Rune, char *);
|
|||
|
||||
void *xmalloc(size_t);
|
||||
void *xrealloc(void *, size_t);
|
||||
char *xstrdup(char *);
|
||||
char *xstrdup(const char *);
|
||||
|
||||
/* config.h globals */
|
||||
extern char *utmp;
|
||||
|
@ -133,5 +133,6 @@ extern char *termname;
|
|||
extern unsigned int tabspaces;
|
||||
extern unsigned int defaultfg;
|
||||
extern unsigned int defaultbg;
|
||||
extern unsigned int defaultcs;
|
||||
extern MouseKey mkeys[];
|
||||
extern float alpha;
|
||||
|
|
1
win.h
1
win.h
|
@ -30,6 +30,7 @@ void xdrawline(Line, int, int, int);
|
|||
void xfinishdraw(void);
|
||||
void xloadcols(void);
|
||||
int xsetcolorname(int, const char *);
|
||||
int xgetcolor(int, unsigned char *, unsigned char *, unsigned char *);
|
||||
void xseticontitle(char *);
|
||||
void xsettitle(char *);
|
||||
int xsetcursor(int);
|
||||
|
|
Loading…
Add table
Reference in a new issue