aboutsummaryrefslogtreecommitdiff
path: root/st.c
diff options
context:
space:
mode:
authorChristoph Lohmann <20h@r-36.net>2014-06-01 17:08:16 +0200
committerChristoph Lohmann <20h@r-36.net>2014-06-01 17:08:16 +0200
commitc6fcb78b3a9a73b691875048848430c18870a0fc (patch)
treeed366826409ea9c653dfa0cb3e187a82970fb2d1 /st.c
parent8b4cfcea73a37ab6d67c06c69e9af3de304185e3 (diff)
downloadst-c6fcb78b3a9a73b691875048848430c18870a0fc.tar.gz
st-c6fcb78b3a9a73b691875048848430c18870a0fc.zip
Fixing color and refactor xsetcolorname.
By the recommendation of FRIGN I refactored xsetcolorname to remove the unnecessary r, g, b variables when allocating a new color. Colors are now freed and set to the new color. A die() should not happen here. Oth‐ erwise it is easy for applications to kill st. St should be resilent to malicious input. Second this patch standardises the naming of »color«. There is no »colour« here. Maybe in some parts of the world.
Diffstat (limited to 'st.c')
-rw-r--r--st.c88
1 files changed, 50 insertions, 38 deletions
diff --git a/st.c b/st.c
index 79a4e0a..11d01df 100644
--- a/st.c
+++ b/st.c
@@ -179,8 +179,8 @@ typedef unsigned long ulong;
179typedef unsigned short ushort; 179typedef unsigned short ushort;
180 180
181typedef XftDraw *Draw; 181typedef XftDraw *Draw;
182typedef XftColor Colour; 182typedef XftColor Color;
183typedef Colormap Colourmap; 183typedef Colormap Colormap;
184 184
185typedef struct { 185typedef struct {
186 char c[UTF_SIZ]; /* character code */ 186 char c[UTF_SIZ]; /* character code */
@@ -241,7 +241,7 @@ typedef struct {
241/* Purely graphic info */ 241/* Purely graphic info */
242typedef struct { 242typedef struct {
243 Display *dpy; 243 Display *dpy;
244 Colourmap cmap; 244 Colormap cmap;
245 Window win; 245 Window win;
246 Drawable buf; 246 Drawable buf;
247 Atom xembed, wmdeletewin, netwmname, netwmpid; 247 Atom xembed, wmdeletewin, netwmname, netwmpid;
@@ -340,7 +340,7 @@ typedef struct {
340 340
341/* Drawing Context */ 341/* Drawing Context */
342typedef struct { 342typedef struct {
343 Colour col[MAX(LEN(colorname), 256)]; 343 Color col[MAX(LEN(colorname), 256)];
344 Font font, bfont, ifont, ibfont; 344 Font font, bfont, ifont, ibfont;
345 GC gc; 345 GC gc;
346} DC; 346} DC;
@@ -1631,7 +1631,7 @@ tdefcolor(int *attr, int *npar, int l) {
1631 uint r, g, b; 1631 uint r, g, b;
1632 1632
1633 switch (attr[*npar + 1]) { 1633 switch (attr[*npar + 1]) {
1634 case 2: /* direct colour in RGB space */ 1634 case 2: /* direct color in RGB space */
1635 if (*npar + 4 >= l) { 1635 if (*npar + 4 >= l) {
1636 fprintf(stderr, 1636 fprintf(stderr,
1637 "erresc(38): Incorrect number of parameters (%d)\n", 1637 "erresc(38): Incorrect number of parameters (%d)\n",
@@ -1648,7 +1648,7 @@ tdefcolor(int *attr, int *npar, int l) {
1648 else 1648 else
1649 idx = TRUECOLOR(r, g, b); 1649 idx = TRUECOLOR(r, g, b);
1650 break; 1650 break;
1651 case 5: /* indexed colour */ 1651 case 5: /* indexed color */
1652 if (*npar + 2 >= l) { 1652 if (*npar + 2 >= l) {
1653 fprintf(stderr, 1653 fprintf(stderr,
1654 "erresc(38): Incorrect number of parameters (%d)\n", 1654 "erresc(38): Incorrect number of parameters (%d)\n",
@@ -1663,8 +1663,8 @@ tdefcolor(int *attr, int *npar, int l) {
1663 break; 1663 break;
1664 case 0: /* implemented defined (only foreground) */ 1664 case 0: /* implemented defined (only foreground) */
1665 case 1: /* transparent */ 1665 case 1: /* transparent */
1666 case 3: /* direct colour in CMY space */ 1666 case 3: /* direct color in CMY space */
1667 case 4: /* direct colour in CMYK space */ 1667 case 4: /* direct color in CMYK space */
1668 default: 1668 default:
1669 fprintf(stderr, 1669 fprintf(stderr,
1670 "erresc(38): gfx attr %d unknown\n", attr[*npar]); 1670 "erresc(38): gfx attr %d unknown\n", attr[*npar]);
@@ -2151,7 +2151,7 @@ strhandle(void) {
2151 /* FALLTHROUGH */ 2151 /* FALLTHROUGH */
2152 case 104: /* color reset, here p = NULL */ 2152 case 104: /* color reset, here p = NULL */
2153 j = (narg > 1) ? atoi(strescseq.args[1]) : -1; 2153 j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
2154 if (!xsetcolorname(j, p)) { 2154 if(xsetcolorname(j, p)) {
2155 fprintf(stderr, "erresc: invalid color %s\n", p); 2155 fprintf(stderr, "erresc: invalid color %s\n", p);
2156 } else { 2156 } else {
2157 /* 2157 /*
@@ -2720,14 +2720,14 @@ xloadcols(void) {
2720 int i; 2720 int i;
2721 XRenderColor color = { .alpha = 0xffff }; 2721 XRenderColor color = { .alpha = 0xffff };
2722 static bool loaded; 2722 static bool loaded;
2723 Colour *cp; 2723 Color *cp;
2724 2724
2725 if(loaded) { 2725 if(loaded) {
2726 for (cp = dc.col; cp < dc.col + LEN(dc.col); ++cp) 2726 for (cp = dc.col; cp < dc.col + LEN(dc.col); ++cp)
2727 XftColorFree(xw.dpy, xw.vis, xw.cmap, cp); 2727 XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
2728 } 2728 }
2729 2729
2730 /* load colours [0-15] and [256-LEN(colorname)] (config.h) */ 2730 /* load colors [0-15] and [256-LEN(colorname)] (config.h) */
2731 for(i = 0; i < LEN(colorname); i++) { 2731 for(i = 0; i < LEN(colorname); i++) {
2732 if(!colorname[i]) 2732 if(!colorname[i])
2733 continue; 2733 continue;
@@ -2736,7 +2736,7 @@ xloadcols(void) {
2736 } 2736 }
2737 } 2737 }
2738 2738
2739 /* load colours [16-231] ; same colours as xterm */ 2739 /* load colors [16-231] ; same colors as xterm */
2740 for(i = 16; i < 6*6*6+16; i++) { 2740 for(i = 16; i < 6*6*6+16; i++) {
2741 color.red = sixd_to_16bit( ((i-16)/36)%6 ); 2741 color.red = sixd_to_16bit( ((i-16)/36)%6 );
2742 color.green = sixd_to_16bit( ((i-16)/6) %6 ); 2742 color.green = sixd_to_16bit( ((i-16)/6) %6 );
@@ -2745,7 +2745,7 @@ xloadcols(void) {
2745 die("Could not allocate color %d\n", i); 2745 die("Could not allocate color %d\n", i);
2746 } 2746 }
2747 2747
2748 /* load colours [232-255] ; grayscale */ 2748 /* load colors [232-255] ; grayscale */
2749 for(; i < 256; i++) { 2749 for(; i < 256; i++) {
2750 color.red = color.green = color.blue = 0x0808 + 0x0a0a * (i-(6*6*6+16)); 2750 color.red = color.green = color.blue = 0x0808 + 0x0a0a * (i-(6*6*6+16));
2751 if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &dc.col[i])) 2751 if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &dc.col[i]))
@@ -2757,33 +2757,45 @@ xloadcols(void) {
2757int 2757int
2758xsetcolorname(int x, const char *name) { 2758xsetcolorname(int x, const char *name) {
2759 XRenderColor color = { .alpha = 0xffff }; 2759 XRenderColor color = { .alpha = 0xffff };
2760 Colour colour; 2760 Color ncolor;
2761
2761 if(!BETWEEN(x, 0, LEN(colorname))) 2762 if(!BETWEEN(x, 0, LEN(colorname)))
2762 return -1; 2763 return 1;
2764
2763 if(!name) { 2765 if(!name) {
2764 if(BETWEEN(x, 16, 16 + 215)) { 2766 if(BETWEEN(x, 16, 16 + 215)) { /* 256 color */
2765 int r = (x - 16) / 36, g = ((x - 16) % 36) / 6, b = (x - 16) % 6; 2767 color.red = sixd_to_16bit( ((x-16)/36)%6 );
2766 color.red = sixd_to_16bit(r); 2768 color.green = sixd_to_16bit( ((x-16)/6) %6 );
2767 color.green = sixd_to_16bit(g); 2769 color.blue = sixd_to_16bit( ((x-16)/1) %6 );
2768 color.blue = sixd_to_16bit(b); 2770 if(!XftColorAllocValue(xw.dpy, xw.vis,
2769 if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &colour)) 2771 xw.cmap, &color, &ncolor)) {
2770 return 0; /* something went wrong */ 2772 return 1;
2771 dc.col[x] = colour; 2773 }
2772 return 1; 2774
2773 } else if(BETWEEN(x, 16 + 216, 255)) { 2775 XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
2774 color.red = color.green = color.blue = 0x0808 + 0x0a0a * (x - (16 + 216)); 2776 dc.col[x] = ncolor;
2775 if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &colour)) 2777 return 0;
2776 return 0; /* something went wrong */ 2778 } else if(BETWEEN(x, 16 + 216, 255)) { /* greyscale */
2777 dc.col[x] = colour; 2779 color.red = color.green = color.blue = \
2778 return 1; 2780 0x0808 + 0x0a0a * (x - (16 + 216));
2779 } else { 2781 if(!XftColorAllocValue(xw.dpy, xw.vis,
2782 xw.cmap, &color, &ncolor)) {
2783 return 1;
2784 }
2785
2786 XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
2787 dc.col[x] = ncolor;
2788 return 0;
2789 } else { /* system colors */
2780 name = colorname[x]; 2790 name = colorname[x];
2781 } 2791 }
2782 } 2792 }
2783 if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, &colour)) 2793 if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, &ncolor))
2784 return 0; 2794 return 1;
2785 dc.col[x] = colour; 2795
2786 return 1; 2796 XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
2797 dc.col[x] = ncolor;
2798 return 0;
2787} 2799}
2788 2800
2789void 2801void
@@ -3099,7 +3111,7 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
3099 FcPattern *fcpattern, *fontpattern; 3111 FcPattern *fcpattern, *fontpattern;
3100 FcFontSet *fcsets[] = { NULL }; 3112 FcFontSet *fcsets[] = { NULL };
3101 FcCharSet *fccharset; 3113 FcCharSet *fccharset;
3102 Colour *fg, *bg, *temp, revfg, revbg, truefg, truebg; 3114 Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
3103 XRenderColor colfg, colbg; 3115 XRenderColor colfg, colbg;
3104 XRectangle r; 3116 XRectangle r;
3105 int oneatatime; 3117 int oneatatime;
@@ -3145,8 +3157,8 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
3145 3157
3146 if(base.mode & ATTR_BOLD) { 3158 if(base.mode & ATTR_BOLD) {
3147 /* 3159 /*
3148 * change basic system colours [0-7] 3160 * change basic system colors [0-7]
3149 * to bright system colours [8-15] 3161 * to bright system colors [8-15]
3150 */ 3162 */
3151 if(BETWEEN(base.fg, 0, 7)) 3163 if(BETWEEN(base.fg, 0, 7))
3152 fg = &dc.col[base.fg + 8]; 3164 fg = &dc.col[base.fg + 8];