diff options
| author | NRK <nrk@disroot.org> | 2022-01-07 23:21:04 +0600 |
|---|---|---|
| committer | Hiltjo Posthuma <hiltjo@codemadness.org> | 2022-04-19 11:43:37 +0200 |
| commit | 8629d9a1da72cc18568a8f146307b0e939b77ebf (patch) | |
| tree | e7237647a29f5888b916567f9c5290fda436a087 | |
| parent | base64_digits: reduce scope, implicit zero, +1 size (diff) | |
| download | st-8629d9a1da72cc18568a8f146307b0e939b77ebf.tar.gz st-8629d9a1da72cc18568a8f146307b0e939b77ebf.zip | |
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.
| -rw-r--r-- | st.c | 90 | ||||
| -rw-r--r-- | st.h | 2 | ||||
| -rw-r--r-- | win.h | 1 |
3 files changed, 33 insertions, 60 deletions
| @@ -161,6 +161,7 @@ static void csidump(void); | |||
| 161 | static void csihandle(void); | 161 | static void csihandle(void); |
| 162 | static void csiparse(void); | 162 | static void csiparse(void); |
| 163 | static void csireset(void); | 163 | static void csireset(void); |
| 164 | static void osc_color_response(int, int, int); | ||
| 164 | static int eschandle(uchar); | 165 | static int eschandle(uchar); |
| 165 | static void strdump(void); | 166 | static void strdump(void); |
| 166 | static void strhandle(void); | 167 | static void strhandle(void); |
| @@ -1835,39 +1836,28 @@ csireset(void) | |||
| 1835 | } | 1836 | } |
| 1836 | 1837 | ||
| 1837 | void | 1838 | void |
| 1838 | osc4_color_response(int num) | 1839 | osc_color_response(int num, int index, int is_osc4) |
| 1839 | { | 1840 | { |
| 1840 | int n; | 1841 | int n; |
| 1841 | char buf[32]; | 1842 | char buf[32]; |
| 1842 | unsigned char r, g, b; | 1843 | unsigned char r, g, b; |
| 1843 | 1844 | ||
| 1844 | if (xgetcolor(num, &r, &g, &b)) { | 1845 | if (xgetcolor(is_osc4 ? num : index, &r, &g, &b)) { |
| 1845 | fprintf(stderr, "erresc: failed to fetch osc4 color %d\n", num); | 1846 | fprintf(stderr, "erresc: failed to fetch %s color %d\n", |
| 1847 | is_osc4 ? "osc4" : "osc", | ||
| 1848 | is_osc4 ? num : index); | ||
| 1846 | return; | 1849 | return; |
| 1847 | } | 1850 | } |
| 1848 | 1851 | ||
| 1849 | n = snprintf(buf, sizeof buf, "\033]4;%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", | 1852 | n = snprintf(buf, sizeof buf, "\033]%s%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", |
| 1850 | num, r, r, g, g, b, b); | 1853 | is_osc4 ? "4;" : "", num, r, r, g, g, b, b); |
| 1851 | 1854 | if (n < 0 || n >= sizeof(buf)) { | |
| 1852 | ttywrite(buf, n, 1); | 1855 | fprintf(stderr, "error: %s while printing %s response\n", |
| 1853 | } | 1856 | n < 0 ? "snprintf failed" : "truncation occurred", |
| 1854 | 1857 | is_osc4 ? "osc4" : "osc"); | |
| 1855 | void | 1858 | } else { |
| 1856 | osc_color_response(int index, int num) | 1859 | ttywrite(buf, n, 1); |
| 1857 | { | ||
| 1858 | int n; | ||
| 1859 | char buf[32]; | ||
| 1860 | unsigned char r, g, b; | ||
| 1861 | |||
| 1862 | if (xgetcolor(index, &r, &g, &b)) { | ||
| 1863 | fprintf(stderr, "erresc: failed to fetch osc color %d\n", index); | ||
| 1864 | return; | ||
| 1865 | } | 1860 | } |
| 1866 | |||
| 1867 | n = snprintf(buf, sizeof buf, "\033]%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", | ||
| 1868 | num, r, r, g, g, b, b); | ||
| 1869 | |||
| 1870 | ttywrite(buf, n, 1); | ||
| 1871 | } | 1861 | } |
| 1872 | 1862 | ||
| 1873 | void | 1863 | void |
| @@ -1875,6 +1865,11 @@ strhandle(void) | |||
| 1875 | { | 1865 | { |
| 1876 | char *p = NULL, *dec; | 1866 | char *p = NULL, *dec; |
| 1877 | int j, narg, par; | 1867 | int j, narg, par; |
| 1868 | const struct { int idx; char *str; } osc_table[] = { | ||
| 1869 | { defaultfg, "foreground" }, | ||
| 1870 | { defaultbg, "background" }, | ||
| 1871 | { defaultcs, "cursor" } | ||
| 1872 | }; | ||
| 1878 | 1873 | ||
| 1879 | term.esc &= ~(ESC_STR_END|ESC_STR); | 1874 | term.esc &= ~(ESC_STR_END|ESC_STR); |
| 1880 | strparse(); | 1875 | strparse(); |
| @@ -1909,43 +1904,22 @@ strhandle(void) | |||
| 1909 | } | 1904 | } |
| 1910 | return; | 1905 | return; |
| 1911 | case 10: | 1906 | case 10: |
| 1912 | if (narg < 2) | ||
| 1913 | break; | ||
| 1914 | |||
| 1915 | p = strescseq.args[1]; | ||
| 1916 | |||
| 1917 | if (!strcmp(p, "?")) | ||
| 1918 | osc_color_response(defaultfg, 10); | ||
| 1919 | else if (xsetcolorname(defaultfg, p)) | ||
| 1920 | fprintf(stderr, "erresc: invalid foreground color: %s\n", p); | ||
| 1921 | else | ||
| 1922 | tfulldirt(); | ||
| 1923 | return; | ||
| 1924 | case 11: | 1907 | case 11: |
| 1925 | if (narg < 2) | ||
| 1926 | break; | ||
| 1927 | |||
| 1928 | p = strescseq.args[1]; | ||
| 1929 | |||
| 1930 | if (!strcmp(p, "?")) | ||
| 1931 | osc_color_response(defaultbg, 11); | ||
| 1932 | else if (xsetcolorname(defaultbg, p)) | ||
| 1933 | fprintf(stderr, "erresc: invalid background color: %s\n", p); | ||
| 1934 | else | ||
| 1935 | tfulldirt(); | ||
| 1936 | return; | ||
| 1937 | case 12: | 1908 | case 12: |
| 1938 | if (narg < 2) | 1909 | if (narg < 2) |
| 1939 | break; | 1910 | break; |
| 1940 | |||
| 1941 | p = strescseq.args[1]; | 1911 | p = strescseq.args[1]; |
| 1942 | 1912 | if ((j = par - 10) < 0 || j >= LEN(osc_table)) | |
| 1943 | if (!strcmp(p, "?")) | 1913 | break; /* shouldn't be possible */ |
| 1944 | osc_color_response(defaultcs, 12); | 1914 | |
| 1945 | else if (xsetcolorname(defaultcs, p)) | 1915 | if (!strcmp(p, "?")) { |
| 1946 | fprintf(stderr, "erresc: invalid cursor color: %s\n", p); | 1916 | osc_color_response(par, osc_table[j].idx, 0); |
| 1947 | else | 1917 | } else if (xsetcolorname(osc_table[j].idx, p)) { |
| 1918 | fprintf(stderr, "erresc: invalid %s color: %s\n", | ||
| 1919 | osc_table[j].str, p); | ||
| 1920 | } else { | ||
| 1948 | tfulldirt(); | 1921 | tfulldirt(); |
| 1922 | } | ||
| 1949 | return; | 1923 | return; |
| 1950 | case 4: /* color set */ | 1924 | case 4: /* color set */ |
| 1951 | if (narg < 3) | 1925 | if (narg < 3) |
| @@ -1955,9 +1929,9 @@ strhandle(void) | |||
| 1955 | case 104: /* color reset */ | 1929 | case 104: /* color reset */ |
| 1956 | j = (narg > 1) ? atoi(strescseq.args[1]) : -1; | 1930 | j = (narg > 1) ? atoi(strescseq.args[1]) : -1; |
| 1957 | 1931 | ||
| 1958 | if (p && !strcmp(p, "?")) | 1932 | if (p && !strcmp(p, "?")) { |
| 1959 | osc4_color_response(j); | 1933 | osc_color_response(j, 0, 1); |
| 1960 | else if (xsetcolorname(j, p)) { | 1934 | } else if (xsetcolorname(j, p)) { |
| 1961 | if (par == 104 && narg <= 1) | 1935 | if (par == 104 && narg <= 1) |
| 1962 | return; /* color reset without parameter */ | 1936 | return; /* color reset without parameter */ |
| 1963 | fprintf(stderr, "erresc: invalid color j=%d, p=%s\n", | 1937 | fprintf(stderr, "erresc: invalid color j=%d, p=%s\n", |
| @@ -111,8 +111,6 @@ void *xmalloc(size_t); | |||
| 111 | void *xrealloc(void *, size_t); | 111 | void *xrealloc(void *, size_t); |
| 112 | char *xstrdup(const char *); | 112 | char *xstrdup(const char *); |
| 113 | 113 | ||
| 114 | int xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b); | ||
| 115 | |||
| 116 | /* config.h globals */ | 114 | /* config.h globals */ |
| 117 | extern char *utmp; | 115 | extern char *utmp; |
| 118 | extern char *scroll; | 116 | extern char *scroll; |
| @@ -30,6 +30,7 @@ void xdrawline(Line, int, int, int); | |||
| 30 | void xfinishdraw(void); | 30 | void xfinishdraw(void); |
| 31 | void xloadcols(void); | 31 | void xloadcols(void); |
| 32 | int xsetcolorname(int, const char *); | 32 | int xsetcolorname(int, const char *); |
| 33 | int xgetcolor(int, unsigned char *, unsigned char *, unsigned char *); | ||
| 33 | void xseticontitle(char *); | 34 | void xseticontitle(char *); |
| 34 | void xsettitle(char *); | 35 | void xsettitle(char *); |
| 35 | int xsetcursor(int); | 36 | int xsetcursor(int); |
