Re: [hackers] [sbase][PATCH] patch: improvments suggested by Silvan
Hi Mattias On Sun, Sep 24, 2017 at 8:20 PM, Mattias Andrée wrote: > On Sun, 24 Sep 2017 11:12:35 -0700 > Michael Forney wrote: > >> Hi Mattias, >> >> Instead of sending these patches on top of your original patch, can >> you send amended versions (v2, v3, etc)? You can use `git format-patch >> -v 2` to make this clear in the subject. >> >> I think that this would make it easier to review and keep track of your >> patch. >> >> Thanks! >> > > Hi Michael! > > I thought it would be easier to see the changes I make. > I think an amended version makes more sense when it's > time to merge. I would prefer an amended version of the patch as well. You could mention the changes between the versions under the '---' separator like it's done here (that way the change comments don't get picked up by 'git am'): https://lkml.org/lkml/2017/9/25/19
Re: [hackers] [PATCH][sbase] Add patch(1)
On Sun, Sep 24, 2017 at 8:57 PM, Mattias Andrée wrote: > On Sun, 24 Sep 2017 19:24:10 +0200 > Silvan Jegen wrote: > >> Heyho >> >> On Sun, Sep 24, 2017 at 06:28:57PM +0200, Mattias Andrée wrote: >> > On Sun, 24 Sep 2017 14:08:41 +0200 >> > Silvan Jegen wrote: >> > >> > > > + >> > > > + if (!new->len) >> > > > + for (i = 0; i < old->len; i++) >> > > > + if (old->lines[i].data[-2] != '-') >> > > >> > > I think according to the standard, refering to data[-2] invokes undefined >> > > behaviour, since at this time, data points to the beginning of the array. >> > >> > I'm not sure whether it is defined or undefined; I would think that it >> > defined, but that adding integers larger than INTPTR_MAX is undefined. >> > I will change to `*(data - 2)` as this is clearly defined. >> >> I was referring to >> https://stackoverflow.com/questions/3473675/are-negative-array-indexes-allowed-in-c/3473686#3473686 >> . `*(data -2) is equivalent to 'data[-2]' but since 'data' doesn't point >> to the second element of the array, I don't think this is valid. > > Hi! > > I think there has been some misunderstanding here, > and that we are in agreement that `a[-b]` in it self > is not invalid, but that question is whether the > deferenced address is valid. I understand why this > looks incorrect, `old->lines->data[0]` does not > actually point to the first character on a line > in a line but rather to the first character in the > line that is part of the content of the file that > hunk patches. For example if the patchfile contains > the line "- abc", `old->lines->data[0]` is `a`, not > `-`, because "- " part of the annotations in the > hunk. Ah, I missed that. In that case this negative index is ok. > This should probably be clarified, but you can see > that this happening just above this code. Pointing this out in a comment seems like a good idea to me. > I will look that your other comments later. Sure! Cheers, Silvan
Re: [hackers] [st] Refactoring (-61 lines)
On Sun, 24 Sep 2017 16:11:22 -0500 "Devin J. Pohly" wrote: Dear Devin, > Some refactoring on the st codebase, reducing it by 61 lines and > establishing cleaner separation between st.c and x.c. There are 28 > fewer extern variables, and and X-specific types are now > included only in x.c. > > These commits move code between st.c and x.c, adjusting the > linkage/storage modifiers or headers as needed. > > Notable change is patch 17/23, which migrates the config.h include > from st.c to x.c, since most of the configuration variables were only > used there. this is very impressive work! Word came up sometimes that the rendering and terminal emulation in st were in dire need of a better separation, also in the interest of starting to unfiddle the spaghetti code we have in there. These patches are a big step in that direction, nice work! With best regards Laslo -- Laslo Hunhold
[hackers] [st][PATCH 17/23] Move config.h include from st.c to x.c
--- config.def.h | 12 ++-- st.c | 18 -- st.h | 11 +++ x.c | 11 +++ 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/config.def.h b/config.def.h index 18cb31c..26c4e51 100644 --- a/config.def.h +++ b/config.def.h @@ -16,12 +16,12 @@ int borderpx = 2; * 4: value of shell in /etc/passwd * 5: value of shell in config.h */ -static char shell[] = "/bin/sh"; -static char *utmp = NULL; -static char stty_args[] = "stty raw pass8 nl -echo -iexten -cstopb 38400"; +char *shell = "/bin/sh"; +char *utmp = NULL; +char *stty_args = "stty raw pass8 nl -echo -iexten -cstopb 38400"; /* identification sequence returned in DA and DECID */ -static char vtiden[] = "\033[?6c"; +char *vtiden = "\033[?6c"; /* Kerning / character bounding-box multipliers */ float cwscale = 1.0; @@ -32,7 +32,7 @@ float chscale = 1.0; * * More advanced example: " `'\"()[]{}" */ -static char worddelimiters[] = " "; +char *worddelimiters = " "; /* selection timeouts (in milliseconds) */ unsigned int doubleclicktimeout = 300; @@ -80,7 +80,7 @@ char termname[] = "st-256color"; * * stty tabs */ -static unsigned int tabspaces = 8; +unsigned int tabspaces = 8; /* Terminal colors (16 first used in escape sequence) */ const char *colorname[] = { diff --git a/st.c b/st.c index 00214d3..ef2d0e4 100644 --- a/st.c +++ b/st.c @@ -107,16 +107,6 @@ typedef struct { int narg; /* nb of args */ } STREscape; -/* function definitions used in config.h */ -static void numlock(const Arg *); -static void printsel(const Arg *); -static void printscreen(const Arg *) ; -static void toggleprinter(const Arg *); -static void sendbreak(const Arg *); - -/* config.h for applying patches and the configuration. */ -#include "config.h" - static void execsh(void); static void stty(void); static void sigchld(int); @@ -200,14 +190,6 @@ static uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; static Rune utfmin[UTF_SIZ + 1] = { 0,0, 0x80, 0x800, 0x1}; static Rune utfmax[UTF_SIZ + 1] = {0x10, 0x7F, 0x7FF, 0x, 0x10}; -/* config.h array lengths */ -size_t colornamelen = LEN(colorname); -size_t mshortcutslen = LEN(mshortcuts); -size_t shortcutslen = LEN(shortcuts); -size_t mappedkeyslen = LEN(mappedkeys); -size_t keylen = LEN(key); -size_t selmaskslen = LEN(selmasks); - ssize_t xwrite(int fd, const char *s, size_t len) { diff --git a/st.h b/st.h index 5bd8a01..8cdacfe 100644 --- a/st.h +++ b/st.h @@ -201,6 +201,11 @@ void ttysend(char *, size_t); void ttywrite(const char *, size_t); void resettitle(void); +void numlock(const Arg *); +void printsel(const Arg *); +void printscreen(const Arg *); +void toggleprinter(const Arg *); +void sendbreak(const Arg *); void selclear(void); @@ -236,6 +241,11 @@ extern float cwscale; extern float chscale; extern unsigned int doubleclicktimeout; extern unsigned int tripleclicktimeout; +extern char *shell; +extern char *utmp; +extern char *stty_args; +extern char *vtiden; +extern char *worddelimiters; extern int allowaltscreen; extern unsigned int xfps; extern unsigned int actionfps; @@ -245,6 +255,7 @@ extern unsigned int blinktimeout; extern char termname[]; extern const char *colorname[]; extern size_t colornamelen; +extern unsigned int tabspaces; extern unsigned int defaultfg; extern unsigned int defaultbg; extern unsigned int defaultcs; diff --git a/x.c b/x.c index c8660e3..4bac345 100644 --- a/x.c +++ b/x.c @@ -28,6 +28,9 @@ static char *argv0; /* constants */ #define ISO14755CMD"dmenu -w %lu -p codepoint: > 8) @@ -189,6 +192,14 @@ static double defaultfontsize = 0; static int oldbutton = 3; /* button event on startup: 3 = release */ +/* config.h array lengths */ +size_t colornamelen = LEN(colorname); +size_t mshortcutslen = LEN(mshortcuts); +size_t shortcutslen = LEN(shortcuts); +size_t mappedkeyslen = LEN(mappedkeys); +size_t keylen = LEN(key); +size_t selmaskslen = LEN(selmasks); + int x2col(int x) { -- 2.14.1
[hackers] [st][PATCH 22/23] Push X-specific config.h function defs into x.c
--- win.h | 8 +--- x.c | 9 + 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/win.h b/win.h index 5c54c40..c834910 100644 --- a/win.h +++ b/win.h @@ -1,11 +1,8 @@ /* See LICENSE for license details. */ +/* Functions from x.c used by st.c */ void draw(void); -void clipcopy(const Arg *); -void clippaste(const Arg *); -void iso14755(const Arg *); -void selpaste(const Arg *); void xbell(void); void xclipcopy(void); void xloadcols(void); @@ -14,6 +11,3 @@ void xsetenv(void); void xsettitle(char *); void xsetpointermotion(int); void xsetsel(char *); -void zoom(const Arg *); -void zoomabs(const Arg *); -void zoomreset(const Arg *); diff --git a/x.c b/x.c index cc5a5a9..b0c890a 100644 --- a/x.c +++ b/x.c @@ -57,6 +57,15 @@ typedef struct { signed char crlf; /* crlf mode */ } Key; +/* function definitions used in config.h */ +static void clipcopy(const Arg *); +static void clippaste(const Arg *); +static void iso14755(const Arg *); +static void selpaste(const Arg *); +static void zoom(const Arg *); +static void zoomabs(const Arg *); +static void zoomreset(const Arg *); + /* config.h for applying patches and the configuration. */ #include "config.h" -- 2.14.1
[hackers] [st][PATCH 18/23] Remove now-unneeded config.h lengths
--- st.h | 4 x.c | 22 +++--- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/st.h b/st.h index 8cdacfe..c58f0ad 100644 --- a/st.h +++ b/st.h @@ -268,15 +268,11 @@ extern unsigned int mousefg; extern unsigned int mousebg; extern unsigned int defaultattr; extern MouseShortcut mshortcuts[]; -extern size_t mshortcutslen; extern Shortcut shortcuts[]; -extern size_t shortcutslen; extern KeySym mappedkeys[]; -extern size_t mappedkeyslen; extern Key key[]; extern size_t keylen; extern uint ignoremod; extern uint forceselmod; extern uint selmasks[]; -extern size_t selmaskslen; extern char ascii_printable[]; diff --git a/x.c b/x.c index 4bac345..ba2c162 100644 --- a/x.c +++ b/x.c @@ -192,14 +192,6 @@ static double defaultfontsize = 0; static int oldbutton = 3; /* button event on startup: 3 = release */ -/* config.h array lengths */ -size_t colornamelen = LEN(colorname); -size_t mshortcutslen = LEN(mshortcuts); -size_t shortcutslen = LEN(shortcuts); -size_t mappedkeyslen = LEN(mappedkeys); -size_t keylen = LEN(key); -size_t selmaskslen = LEN(selmasks); - int x2col(int x) { @@ -248,7 +240,7 @@ getbuttoninfo(XEvent *e) selnormalize(); sel.type = SEL_REGULAR; - for (type = 1; type < selmaskslen; ++type) { + for (type = 1; type < LEN(selmasks); ++type) { if (match(selmasks[type], state)) { sel.type = type; break; @@ -331,7 +323,7 @@ bpress(XEvent *e) return; } - for (ms = mshortcuts; ms < mshortcuts + mshortcutslen; ms++) { + for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) { if (e->xbutton.button == ms->b && match(ms->mask, e->xbutton.state)) { ttysend(ms->s, strlen(ms->s)); @@ -721,7 +713,7 @@ xloadcols(void) static int loaded; Color *cp; - dc.collen = MAX(colornamelen, 256); + dc.collen = MAX(LEN(colorname), 256); dc.col = xmalloc(dc.collen * sizeof(Color)); if (loaded) { @@ -1682,7 +1674,7 @@ kpress(XEvent *ev) len = XmbLookupString(xw.xic, e, buf, sizeof buf, &ksym, &status); /* 1. shortcuts */ - for (bp = shortcuts; bp < shortcuts + shortcutslen; bp++) { + for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) { if (ksym == bp->keysym && match(bp->mod, e->state)) { bp->func(&(bp->arg)); return; @@ -1758,16 +1750,16 @@ kmap(KeySym k, uint state) int i; /* Check for mapped keys out of X11 function keys. */ - for (i = 0; i < mappedkeyslen; i++) { + for (i = 0; i < LEN(mappedkeys); i++) { if (mappedkeys[i] == k) break; } - if (i == mappedkeyslen) { + if (i == LEN(mappedkeys)) { if ((k & 0x) < 0xFD00) return NULL; } - for (kp = key; kp < key + keylen; kp++) { + for (kp = key; kp < key + LEN(key); kp++) { if (kp->k != k) continue; -- 2.14.1
[hackers] [st][PATCH 20/23] remove X includes from st.c
All X interaction is now encapsulated in x.c. --- st.c | 4 1 file changed, 4 deletions(-) diff --git a/st.c b/st.c index ef2d0e4..3b60007 100644 --- a/st.c +++ b/st.c @@ -24,10 +24,6 @@ #include #include -/* X11 */ -#include -#include - #include "st.h" #include "win.h" -- 2.14.1
[hackers] [st][PATCH 16/23] Remove need for CurrentTime in st.c
Moving closer to removing that module's dependency on Xlib --- st.c | 2 +- win.h | 2 +- x.c | 19 +-- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/st.c b/st.c index 2bf9435..00214d3 100644 --- a/st.c +++ b/st.c @@ -1800,7 +1800,7 @@ strhandle(void) dec = base64dec(strescseq.args[2]); if (dec) { - xsetsel(dec, CurrentTime); + xsetsel(dec); xclipcopy(); } else { fprintf(stderr, "erresc: invalid base64\n"); diff --git a/win.h b/win.h index c4b5566..5e1f7fe 100644 --- a/win.h +++ b/win.h @@ -19,7 +19,7 @@ int xsetcolorname(int, const char *); void xsetenv(void); void xsettitle(char *); void xsetpointermotion(int); -void xsetsel(char *, Time); +void xsetsel(char *); void zoom(const Arg *); void zoomabs(const Arg *); void zoomreset(const Arg *); diff --git a/x.c b/x.c index 8abe238..c8660e3 100644 --- a/x.c +++ b/x.c @@ -115,9 +115,8 @@ static void propnotify(XEvent *); static void selnotify(XEvent *); static void selclear_(XEvent *); static void selrequest(XEvent *); - -static void selcopy(Time); static void selinit(void); +static void setsel(char *, Time); static int match(uint, uint); static char *kmap(KeySym, uint); @@ -360,12 +359,6 @@ bpress(XEvent *e) } } -void -selcopy(Time t) -{ - xsetsel(getsel(), t); -} - void propnotify(XEvent *e) { @@ -603,7 +596,7 @@ selrequest(XEvent *e) } void -xsetsel(char *str, Time t) +setsel(char *str, Time t) { free(sel.primary); sel.primary = str; @@ -613,6 +606,12 @@ xsetsel(char *str, Time t) selclear_(NULL); } +void +xsetsel(char *str) +{ + setsel(str, CurrentTime); +} + void brelease(XEvent *e) { @@ -626,7 +625,7 @@ brelease(XEvent *e) } else if (e->xbutton.button == Button1) { if (sel.mode == SEL_READY) { getbuttoninfo(e); - selcopy(e->xbutton.time); + setsel(getsel(), e->xbutton.time); } else selclear_(NULL); sel.mode = SEL_IDLE; -- 2.14.1
[hackers] [st][PATCH 23/23] no more extern for X-specific configs
--- config.def.h | 61 ++-- st.h | 29 + 2 files changed, 31 insertions(+), 59 deletions(-) diff --git a/config.def.h b/config.def.h index 26c4e51..1c181ab 100644 --- a/config.def.h +++ b/config.def.h @@ -5,8 +5,8 @@ * * font: see http://freedesktop.org/software/fontconfig/fontconfig-user.html */ -char font[] = "Liberation Mono:pixelsize=12:antialias=true:autohint=true"; -int borderpx = 2; +static char *font = "Liberation Mono:pixelsize=12:antialias=true:autohint=true"; +static int borderpx = 2; /* * What program is execed by st depends of these precedence rules: @@ -24,8 +24,8 @@ char *stty_args = "stty raw pass8 nl -echo -iexten -cstopb 38400"; char *vtiden = "\033[?6c"; /* Kerning / character bounding-box multipliers */ -float cwscale = 1.0; -float chscale = 1.0; +static float cwscale = 1.0; +static float chscale = 1.0; /* * word delimiter string @@ -35,35 +35,35 @@ float chscale = 1.0; char *worddelimiters = " "; /* selection timeouts (in milliseconds) */ -unsigned int doubleclicktimeout = 300; -unsigned int tripleclicktimeout = 600; +static unsigned int doubleclicktimeout = 300; +static unsigned int tripleclicktimeout = 600; /* alt screens */ int allowaltscreen = 1; /* frames per second st should at maximum draw to the screen */ -unsigned int xfps = 120; -unsigned int actionfps = 30; +static unsigned int xfps = 120; +static unsigned int actionfps = 30; /* * blinking timeout (set to 0 to disable blinking) for the terminal blinking * attribute. */ -unsigned int blinktimeout = 800; +static unsigned int blinktimeout = 800; /* * thickness of underline and bar cursors */ -unsigned int cursorthickness = 2; +static unsigned int cursorthickness = 2; /* * bell volume. It must be a value between -100 and 100. Use 0 for disabling * it */ -int bellvolume = 0; +static int bellvolume = 0; /* default TERM value */ -char termname[] = "st-256color"; +char *termname = "st-256color"; /* * spaces per tab @@ -83,7 +83,7 @@ char termname[] = "st-256color"; unsigned int tabspaces = 8; /* Terminal colors (16 first used in escape sequence) */ -const char *colorname[] = { +static const char *colorname[] = { /* 8 normal colors */ "black", "red3", @@ -118,8 +118,8 @@ const char *colorname[] = { */ unsigned int defaultfg = 7; unsigned int defaultbg = 0; -unsigned int defaultcs = 256; -unsigned int defaultrcs = 257; +static unsigned int defaultcs = 256; +static unsigned int defaultrcs = 257; /* * Default shape of cursor @@ -128,33 +128,33 @@ unsigned int defaultrcs = 257; * 6: Bar ("|") * 7: Snowman ("☃") */ -unsigned int cursorshape = 2; +static unsigned int cursorshape = 2; /* * Default columns and rows numbers */ -unsigned int cols = 80; -unsigned int rows = 24; +static unsigned int cols = 80; +static unsigned int rows = 24; /* * Default colour and shape of the mouse cursor */ -unsigned int mouseshape = XC_xterm; -unsigned int mousefg = 7; -unsigned int mousebg = 0; +static unsigned int mouseshape = XC_xterm; +static unsigned int mousefg = 7; +static unsigned int mousebg = 0; /* * Color used to display font attributes when fontconfig selected a font which * doesn't match the ones requested. */ -unsigned int defaultattr = 11; +static unsigned int defaultattr = 11; /* * Internal mouse shortcuts. * Beware that overloading Button1 will disable the selection. */ -MouseShortcut mshortcuts[] = { +static MouseShortcut mshortcuts[] = { /* button maskstring */ { Button4, XK_ANY_MOD, "\031" }, { Button5, XK_ANY_MOD, "\005" }, @@ -164,7 +164,7 @@ MouseShortcut mshortcuts[] = { #define MODKEY Mod1Mask #define TERMMOD (ControlMask|ShiftMask) -Shortcut shortcuts[] = { +static Shortcut shortcuts[] = { /* mask keysym functionargument */ { XK_ANY_MOD, XK_Break, sendbreak, {.i = 0} }, { ControlMask, XK_Print, toggleprinter, {.i = 0} }, @@ -209,26 +209,26 @@ Shortcut shortcuts[] = { * If you want keys other than the X11 function keys (0xFD00 - 0x) * to be mapped below, add them to this array. */ -KeySym mappedkeys[] = { -1 }; +static KeySym mappedkeys[] = { -1 }; /* * State bits to ignore when matching key or button events. By default, * numlock (Mod2Mask) and keyboard layout (XK_SWITCH_MOD) are ignored. */ -uint ignoremod = Mod2Mask|XK_SWITCH_MOD; +static uint ignoremod = Mod2Mask|XK_SWITCH_MOD; /* * Override mouse-select while mask is active (when MODE_MOUSE is set). * Note that if you want to use ShiftMask with selmasks, set this to an other * modifier, set to 0 to not use it. */ -uint forceselmod = ShiftMask; +static uint forceselmod = ShiftMask; /* * This is the huge key array which defines all
[hackers] [st][PATCH 21/23] drawregion and xloadcolor are internal to x.c
--- win.h | 1 - x.c | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/win.h b/win.h index a632604..5c54c40 100644 --- a/win.h +++ b/win.h @@ -1,7 +1,6 @@ /* See LICENSE for license details. */ void draw(void); -void drawregion(int, int, int, int); void clipcopy(const Arg *); void clippaste(const Arg *); diff --git a/x.c b/x.c index 858d044..cc5a5a9 100644 --- a/x.c +++ b/x.c @@ -131,9 +131,11 @@ static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int); static void xdrawglyph(Glyph, int, int); static void xclear(int, int, int, int); static void xdrawcursor(void); +static void drawregion(int, int, int, int); static int xgeommasktogravity(int); static int xloadfont(Font *, FcPattern *); static void xunloadfont(Font *); +static int xloadcolor(int, const char *, Color *); static void xhints(void); static void xinit(void); static void xresize(int, int); -- 2.14.1
[hackers] [st][PATCH 19/23] Migrate X-specific types into x.c
--- st.h | 36 win.h | 5 - x.c | 37 + 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/st.h b/st.h index c58f0ad..ad804ee 100644 --- a/st.h +++ b/st.h @@ -124,21 +124,6 @@ typedef struct { int *tabs; } Term; -/* Purely graphic info */ -typedef struct { - int tw, th; /* tty width and height */ - int w, h; /* window width and height */ - int ch; /* char height */ - int cw; /* char width */ - char state; /* focus, redraw, visible */ -} TermWindow; - -typedef struct { - uint b; - uint mask; - char *s; -} MouseShortcut; - typedef struct { int mode; int type; @@ -169,23 +154,6 @@ typedef union { const void *v; } Arg; -typedef struct { - uint mod; - KeySym keysym; - void (*func)(const Arg *); - const Arg arg; -} Shortcut; - -typedef struct { - KeySym k; - uint mask; - char *s; - /* three valued logic variables: 0 indifferent, 1 on, -1 off */ - signed char appkey;/* application keypad */ - signed char appcursor; /* application cursor */ - signed char crlf; /* crlf mode */ -} Key; - void die(const char *, ...); void redraw(void); @@ -267,10 +235,6 @@ extern unsigned int mouseshape; extern unsigned int mousefg; extern unsigned int mousebg; extern unsigned int defaultattr; -extern MouseShortcut mshortcuts[]; -extern Shortcut shortcuts[]; -extern KeySym mappedkeys[]; -extern Key key[]; extern size_t keylen; extern uint ignoremod; extern uint forceselmod; diff --git a/win.h b/win.h index 5e1f7fe..a632604 100644 --- a/win.h +++ b/win.h @@ -1,10 +1,5 @@ /* See LICENSE for license details. */ -/* X modifiers */ -#define XK_ANY_MODUINT_MAX -#define XK_NO_MOD 0 -#define XK_SWITCH_MOD (1<<13) - void draw(void); void drawregion(int, int, int, int); diff --git a/x.c b/x.c index ba2c162..858d044 100644 --- a/x.c +++ b/x.c @@ -21,6 +21,11 @@ static char *argv0; #include "st.h" #include "win.h" +/* X modifiers */ +#define XK_ANY_MODUINT_MAX +#define XK_NO_MOD 0 +#define XK_SWITCH_MOD (1<<13) + /* XEMBED messages */ #define XEMBED_FOCUS_IN 4 #define XEMBED_FOCUS_OUT 5 @@ -28,6 +33,30 @@ static char *argv0; /* constants */ #define ISO14755CMD"dmenu -w %lu -p codepoint:
[hackers] [st][PATCH 14/23] Move key mapping into x.c
--- config.def.h | 6 +++--- st.c | 58 ++ st.h | 17 +++-- x.c | 48 4 files changed, 68 insertions(+), 61 deletions(-) diff --git a/config.def.h b/config.def.h index dd94be2..18cb31c 100644 --- a/config.def.h +++ b/config.def.h @@ -209,13 +209,13 @@ Shortcut shortcuts[] = { * If you want keys other than the X11 function keys (0xFD00 - 0x) * to be mapped below, add them to this array. */ -static KeySym mappedkeys[] = { -1 }; +KeySym mappedkeys[] = { -1 }; /* * State bits to ignore when matching key or button events. By default, * numlock (Mod2Mask) and keyboard layout (XK_SWITCH_MOD) are ignored. */ -static uint ignoremod = Mod2Mask|XK_SWITCH_MOD; +uint ignoremod = Mod2Mask|XK_SWITCH_MOD; /* * Override mouse-select while mask is active (when MODE_MOUSE is set). @@ -228,7 +228,7 @@ uint forceselmod = ShiftMask; * This is the huge key array which defines all compatibility to the Linux * world. Please decide about changes wisely. */ -static Key key[] = { +Key key[] = { /* keysym maskstring appkey appcursor crlf */ { XK_KP_Home, ShiftMask, "\033[2J", 0, -1,0}, { XK_KP_Home, ShiftMask, "\033[1;2H", 0, +1,0}, diff --git a/st.c b/st.c index 9b92bc6..2f0f8ec 100644 --- a/st.c +++ b/st.c @@ -107,16 +107,6 @@ typedef struct { int narg; /* nb of args */ } STREscape; -typedef struct { - KeySym k; - uint mask; - char *s; - /* three valued logic variables: 0 indifferent, 1 on, -1 off */ - signed char appkey;/* application keypad */ - signed char appcursor; /* application cursor */ - signed char crlf; /* crlf mode */ -} Key; - /* function definitions used in config.h */ static void numlock(const Arg *); static void printsel(const Arg *); @@ -215,6 +205,8 @@ static Rune utfmax[UTF_SIZ + 1] = {0x10, 0x7F, 0x7FF, 0x, 0x10}; size_t colornamelen = LEN(colorname); size_t mshortcutslen = LEN(mshortcuts); size_t shortcutslen = LEN(shortcuts); +size_t mappedkeyslen = LEN(mappedkeys); +size_t keylen = LEN(key); size_t selmaskslen = LEN(selmasks); ssize_t @@ -2496,54 +2488,8 @@ redraw(void) draw(); } -int -match(uint mask, uint state) -{ - return mask == XK_ANY_MOD || mask == (state & ~ignoremod); -} - void numlock(const Arg *dummy) { term.numlock ^= 1; } - -char* -kmap(KeySym k, uint state) -{ - Key *kp; - int i; - - /* Check for mapped keys out of X11 function keys. */ - for (i = 0; i < LEN(mappedkeys); i++) { - if (mappedkeys[i] == k) - break; - } - if (i == LEN(mappedkeys)) { - if ((k & 0x) < 0xFD00) - return NULL; - } - - for (kp = key; kp < key + LEN(key); kp++) { - if (kp->k != k) - continue; - - if (!match(kp->mask, state)) - continue; - - if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0) - continue; - if (term.numlock && kp->appkey == 2) - continue; - - if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0) - continue; - - if (IS_SET(MODE_CRLF) ? kp->crlf < 0 : kp->crlf > 0) - continue; - - return kp->s; - } - - return NULL; -} diff --git a/st.h b/st.h index 4b21153..8d75687 100644 --- a/st.h +++ b/st.h @@ -179,6 +179,16 @@ typedef struct { const Arg arg; } Shortcut; +typedef struct { + KeySym k; + uint mask; + char *s; + /* three valued logic variables: 0 indifferent, 1 on, -1 off */ + signed char appkey;/* application keypad */ + signed char appcursor; /* application cursor */ + signed char crlf; /* crlf mode */ +} Key; + void die(const char *, ...); void redraw(void); @@ -187,7 +197,6 @@ void tnew(int, int, unsigned int); void tresize(int, int); void tsetdirt(int, int); void tsetdirtattr(int); -int match(uint, uint); void ttynew(void); size_t ttyread(void); void ttyresize(int, int); @@ -196,7 +205,6 @@ void ttywrite(const char *, size_t); void resettitle(void); -char *kmap(KeySym, uint); void selclear(void); void selnormalize(void); @@ -254,6 +262,11 @@ extern MouseShortcut mshortcuts[]; extern size_t mshortcutslen; extern Shortcut shortcuts[]; extern size_t shortcutslen; +extern KeySym mappedkeys[]; +extern size_t mappedkeyslen; +extern Key key[]; +extern size_t keylen; +extern uint ignoremod; extern uint forceselmod; extern uint selmasks[]; extern size_t selmaskslen; diff --git a/x.c b/x.c index c2faa9c..45278e0 100644 --- a/x.c +++ b/x.c
[hackers] [st][PATCH 01/23] move usage and argv0 into x.c
xinit is now internal to x.c --- st.c | 15 --- st.h | 2 -- win.h | 2 -- x.c | 21 +++-- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/st.c b/st.c index 7c7ddff..f1227ea 100644 --- a/st.c +++ b/st.c @@ -28,8 +28,6 @@ #include #include -char *argv0; - #define Glyph Glyph_ #define Font Font_ @@ -2691,16 +2689,3 @@ cresize(int width, int height) tresize(col, row); xresize(col, row); } - -void -usage(void) -{ - die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]" - " [-n name] [-o file]\n" - " [-T title] [-t title] [-w windowid]" - " [[-e] command [args ...]]\n" - " %s [-aiv] [-c class] [-f font] [-g geometry]" - " [-n name] [-o file]\n" - " [-T title] [-t title] [-w windowid] -l line" - " [stty_args ...]\n", argv0, argv0); -} diff --git a/st.h b/st.h index 44d4938..28a751d 100644 --- a/st.h +++ b/st.h @@ -214,8 +214,6 @@ size_t utf8encode(Rune, char *); void *xmalloc(size_t); char *xstrdup(char *); -void usage(void); - /* Globals */ extern TermWindow win; extern Term term; diff --git a/win.h b/win.h index 428111c..7b614eb 100644 --- a/win.h +++ b/win.h @@ -9,13 +9,11 @@ typedef XftGlyphFontSpec GlyphFontSpec; void draw(void); void drawregion(int, int, int, int); -void run(void); void xbell(int); void xclipcopy(void); void xclippaste(void); void xhints(void); -void xinit(void); void xloadcols(void); int xsetcolorname(int, const char *); void xloadfonts(char *, double); diff --git a/x.c b/x.c index 191e5dc..d68261c 100644 --- a/x.c +++ b/x.c @@ -15,11 +15,12 @@ #include #include -#include "arg.h" - #define Glyph Glyph_ #define Font Font_ +static char *argv0; + +#include "arg.h" #include "win.h" #include "st.h" @@ -89,6 +90,7 @@ static void xdrawcursor(void); static int xgeommasktogravity(int); static int xloadfont(Font *, FcPattern *); static void xunloadfont(Font *); +static void xinit(void); static void expose(XEvent *); static void visibility(XEvent *); @@ -108,6 +110,8 @@ static void selrequest(XEvent *); static void selcopy(Time); static void getbuttoninfo(XEvent *); static void mousereport(XEvent *); +static void usage(void); +static void run(void); static void (*handler[LASTEvent])(XEvent *) = { [KeyPress] = kpress, @@ -1701,6 +1705,19 @@ run(void) } } +void +usage(void) +{ + die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]" + " [-n name] [-o file]\n" + " [-T title] [-t title] [-w windowid]" + " [[-e] command [args ...]]\n" + " %s [-aiv] [-c class] [-f font] [-g geometry]" + " [-n name] [-o file]\n" + " [-T title] [-t title] [-w windowid] -l line" + " [stty_args ...]\n", argv0, argv0); +} + int main(int argc, char *argv[]) { -- 2.14.1
[hackers] [st][PATCH 12/23] un-export base64dec_getc
--- st.c | 1 + 1 file changed, 1 insertion(+) diff --git a/st.c b/st.c index 71d24ae..0093642 100644 --- a/st.c +++ b/st.c @@ -183,6 +183,7 @@ static char *utf8strchr(char *s, Rune u); static size_t utf8validate(Rune *, size_t); static char *base64dec(const char *); +static char base64dec_getc(const char **); static ssize_t xwrite(int, const char *, size_t); static void *xrealloc(void *, size_t); -- 2.14.1
[hackers] [st][PATCH 13/23] Move oldbutton var into x.c
--- st.c | 1 - st.h | 1 - x.c | 2 ++ 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/st.c b/st.c index 0093642..9b92bc6 100644 --- a/st.c +++ b/st.c @@ -201,7 +201,6 @@ char *opt_io= NULL; char *opt_line = NULL; char *opt_name = NULL; char *opt_title = NULL; -int oldbutton = 3; /* button event on startup: 3 = release */ static CSIEscape csiescseq; static STREscape strescseq; diff --git a/st.h b/st.h index 980c353..4b21153 100644 --- a/st.h +++ b/st.h @@ -222,7 +222,6 @@ extern char *opt_io; extern char *opt_line; extern char *opt_name; extern char *opt_title; -extern int oldbutton; /* config.h globals */ extern char font[]; diff --git a/x.c b/x.c index dc851d8..c2faa9c 100644 --- a/x.c +++ b/x.c @@ -184,6 +184,8 @@ static char *usedfont = NULL; static double usedfontsize = 0; static double defaultfontsize = 0; +static int oldbutton = 3; /* button event on startup: 3 = release */ + int x2col(int x) { -- 2.14.1
[hackers] [st][PATCH 15/23] Move fontspec buffer from Term to XWindow
Has to do with rendering, not terminal emulation, and keeps st.c dependent on Xlib --- st.c | 4 st.h | 4 +--- x.c | 10 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/st.c b/st.c index 2f0f8ec..2bf9435 100644 --- a/st.c +++ b/st.c @@ -176,7 +176,6 @@ static char *base64dec(const char *); static char base64dec_getc(const char **); static ssize_t xwrite(int, const char *, size_t); -static void *xrealloc(void *, size_t); /* Globals */ Term term; @@ -2424,9 +2423,6 @@ tresize(int col, int row) free(term.alt[i]); } - /* resize to new width */ - term.specbuf = xrealloc(term.specbuf, col * sizeof(GlyphFontSpec)); - /* resize to new height */ term.line = xrealloc(term.line, row * sizeof(Line)); term.alt = xrealloc(term.alt, row * sizeof(Line)); diff --git a/st.h b/st.h index 8d75687..5bd8a01 100644 --- a/st.h +++ b/st.h @@ -85,8 +85,6 @@ typedef unsigned int uint; typedef unsigned long ulong; typedef unsigned short ushort; -typedef XftGlyphFontSpec GlyphFontSpec; - typedef uint_least32_t Rune; #define Glyph Glyph_ @@ -113,7 +111,6 @@ typedef struct { Line *line; /* screen */ Line *alt;/* alternate screen */ int *dirty; /* dirtyness of lines */ - GlyphFontSpec *specbuf; /* font spec buffer used for rendering */ TCursor c;/* cursor */ int cursor; /* cursor style */ int top; /* topscroll limit */ @@ -215,6 +212,7 @@ size_t utf8decode(char *, Rune *, size_t); size_t utf8encode(Rune, char *); void *xmalloc(size_t); +void *xrealloc(void *, size_t); char *xstrdup(char *); /* Globals */ diff --git a/x.c b/x.c index 45278e0..8abe238 100644 --- a/x.c +++ b/x.c @@ -36,6 +36,7 @@ static char *argv0; typedef XftDraw *Draw; typedef XftColor Color; +typedef XftGlyphFontSpec GlyphFontSpec; /* Purely graphic info */ typedef struct { @@ -43,6 +44,7 @@ typedef struct { Colormap cmap; Window win; Drawable buf; + GlyphFontSpec *specbuf; /* font spec buffer used for rendering */ Atom xembed, wmdeletewin, netwmname, netwmpid; XIM xim; XIC xic; @@ -667,6 +669,9 @@ xresize(int col, int row) DefaultDepth(xw.dpy, xw.scr)); XftDrawChange(xw.draw, xw.buf); xclear(0, 0, win.w, win.h); + + /* resize to new width */ + xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec)); } ushort @@ -1027,6 +1032,9 @@ xinit(void) XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel); XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h); + /* font spec buffer */ + xw.specbuf = xmalloc(term.col * sizeof(GlyphFontSpec)); + /* Xft rendering context */ xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap); @@ -1528,7 +1536,7 @@ drawregion(int x1, int y1, int x2, int y2) term.dirty[y] = 0; - specs = term.specbuf; + specs = xw.specbuf; numspecs = xmakeglyphfontspecs(specs, &term.line[y][x1], x2 - x1, x1, y); i = ox = 0; -- 2.14.1
[hackers] [st][PATCH 10/23] Move cursor shape from TermWindow to Term
--- st.c | 7 --- st.h | 4 ++-- x.c | 5 ++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/st.c b/st.c index 341ad7d..be1666e 100644 --- a/st.c +++ b/st.c @@ -973,9 +973,10 @@ treset(void) } void -tnew(int col, int row) +tnew(int col, int row, unsigned int cursor) { - term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } }; + term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } }, + .cursor = cursor }; tresize(col, row); term.numlock = 1; @@ -1745,7 +1746,7 @@ csihandle(void) if (!BETWEEN(csiescseq.arg[0], 0, 6)) { goto unknown; } - win.cursor = csiescseq.arg[0]; + term.cursor = csiescseq.arg[0]; break; default: goto unknown; diff --git a/st.h b/st.h index 0cfffa5..ede19e3 100644 --- a/st.h +++ b/st.h @@ -115,6 +115,7 @@ typedef struct { int *dirty; /* dirtyness of lines */ GlyphFontSpec *specbuf; /* font spec buffer used for rendering */ TCursor c;/* cursor */ + int cursor; /* cursor style */ int top; /* topscroll limit */ int bot; /* bottom scroll limit */ int mode; /* terminal mode flags */ @@ -133,7 +134,6 @@ typedef struct { int ch; /* char height */ int cw; /* char width */ char state; /* focus, redraw, visible */ - int cursor; /* cursor style */ } TermWindow; typedef struct { @@ -183,7 +183,7 @@ void die(const char *, ...); void redraw(void); int tattrset(int); -void tnew(int, int); +void tnew(int, int, unsigned int); void tresize(int, int); void tsetdirt(int, int); void tsetdirtattr(int); diff --git a/x.c b/x.c index aa8ad35..bf5e531 100644 --- a/x.c +++ b/x.c @@ -1428,7 +1428,7 @@ xdrawcursor(void) /* draw the new one */ if (win.state & WIN_FOCUSED) { - switch (win.cursor) { + switch (term.cursor) { case 7: /* st extension: snowman */ utf8decode("☃", &g.u, UTF_SIZ); case 0: /* Blinking Block */ @@ -1849,7 +1849,6 @@ main(int argc, char *argv[]) { xw.l = xw.t = 0; xw.isfixed = False; - win.cursor = cursorshape; ARGBEGIN { case 'a': @@ -1904,7 +1903,7 @@ run: } setlocale(LC_CTYPE, ""); XSetLocaleModifiers(""); - tnew(MAX(cols, 1), MAX(rows, 1)); + tnew(MAX(cols, 1), MAX(rows, 1), cursorshape); xinit(); selinit(); run(); -- 2.14.1
[hackers] [st][PATCH 03/23] Move zoom functions into x.c
xhints is now internal to x.c --- st.c | 36 +--- st.h | 2 ++ win.h | 6 +++--- x.c | 34 +- 4 files changed, 39 insertions(+), 39 deletions(-) diff --git a/st.c b/st.c index 7ae864e..825197c 100644 --- a/st.c +++ b/st.c @@ -31,8 +31,8 @@ #define Glyph Glyph_ #define Font Font_ -#include "win.h" #include "st.h" +#include "win.h" #if defined(__linux) #include @@ -128,9 +128,6 @@ static void clipcopy(const Arg *); static void clippaste(const Arg *); static void numlock(const Arg *); static void selpaste(const Arg *); -static void zoom(const Arg *); -static void zoomabs(const Arg *); -static void zoomreset(const Arg *); static void printsel(const Arg *); static void printscreen(const Arg *) ; static void iso14755(const Arg *); @@ -2574,37 +2571,6 @@ tresize(int col, int row) term.c = c; } -void -zoom(const Arg *arg) -{ - Arg larg; - - larg.f = usedfontsize + arg->f; - zoomabs(&larg); -} - -void -zoomabs(const Arg *arg) -{ - xunloadfonts(); - xloadfonts(usedfont, arg->f); - cresize(0, 0); - ttyresize(); - redraw(); - xhints(); -} - -void -zoomreset(const Arg *arg) -{ - Arg larg; - - if (defaultfontsize > 0) { - larg.f = defaultfontsize; - zoomabs(&larg); - } -} - void resettitle(void) { diff --git a/st.h b/st.h index e4328fc..53d5009 100644 --- a/st.h +++ b/st.h @@ -90,6 +90,8 @@ typedef unsigned int uint; typedef unsigned long ulong; typedef unsigned short ushort; +typedef XftGlyphFontSpec GlyphFontSpec; + typedef uint_least32_t Rune; typedef struct { diff --git a/win.h b/win.h index e6e4448..2454b50 100644 --- a/win.h +++ b/win.h @@ -5,15 +5,12 @@ #define XK_NO_MOD 0 #define XK_SWITCH_MOD (1<<13) -typedef XftGlyphFontSpec GlyphFontSpec; - void draw(void); void drawregion(int, int, int, int); void xbell(void); void xclipcopy(void); void xclippaste(void); -void xhints(void); void xloadcols(void); int xsetcolorname(int, const char *); void xloadfonts(char *, double); @@ -25,3 +22,6 @@ void xresize(int, int); void xselpaste(void); unsigned long xwinid(void); void xsetsel(char *, Time); +void zoom(const Arg *); +void zoomabs(const Arg *); +void zoomreset(const Arg *); diff --git a/x.c b/x.c index b5cc58e..8aeb9c0 100644 --- a/x.c +++ b/x.c @@ -21,8 +21,8 @@ static char *argv0; #include "arg.h" -#include "win.h" #include "st.h" +#include "win.h" /* XEMBED messages */ #define XEMBED_FOCUS_IN 4 @@ -90,6 +90,7 @@ static void xdrawcursor(void); static int xgeommasktogravity(int); static int xloadfont(Font *, FcPattern *); static void xunloadfont(Font *); +static void xhints(void); static void xinit(void); static void xseturgency(int); @@ -414,6 +415,37 @@ xselpaste(void) xw.win, CurrentTime); } +void +zoom(const Arg *arg) +{ + Arg larg; + + larg.f = usedfontsize + arg->f; + zoomabs(&larg); +} + +void +zoomabs(const Arg *arg) +{ + xunloadfonts(); + xloadfonts(usedfont, arg->f); + cresize(0, 0); + ttyresize(); + redraw(); + xhints(); +} + +void +zoomreset(const Arg *arg) +{ + Arg larg; + + if (defaultfontsize > 0) { + larg.f = defaultfontsize; + zoomabs(&larg); + } +} + void xclipcopy(void) { -- 2.14.1
[hackers] [st][PATCH 08/23] Move window-manipulating functions into x.c
xresize is now internal to x.c --- st.c | 36 st.h | 9 + win.h | 1 - x.c | 45 + 4 files changed, 46 insertions(+), 45 deletions(-) diff --git a/st.c b/st.c index 559ce73..b23c87e 100644 --- a/st.c +++ b/st.c @@ -158,7 +158,6 @@ static void tnewline(int); static void tputtab(int); static void tputc(Rune); static void treset(void); -static void tresize(int, int); static void tscrollup(int, int); static void tscrolldown(int, int); static void tsetattr(int *, int); @@ -405,24 +404,6 @@ base64dec(const char *src) return result; } -int -x2col(int x) -{ - x -= borderpx; - x /= win.cw; - - return LIMIT(x, 0, term.col-1); -} - -int -y2row(int y) -{ - y -= borderpx; - y /= win.ch; - - return LIMIT(y, 0, term.row-1); -} - int tlinelen(int y) { @@ -2570,20 +2551,3 @@ kmap(KeySym k, uint state) return NULL; } - -void -cresize(int width, int height) -{ - int col, row; - - if (width != 0) - win.w = width; - if (height != 0) - win.h = height; - - col = (win.w - 2 * borderpx) / win.cw; - row = (win.h - 2 * borderpx) / win.ch; - - tresize(col, row); - xresize(col, row); -} diff --git a/st.h b/st.h index 1a8bc5c..2a8bb55 100644 --- a/st.h +++ b/st.h @@ -80,11 +80,6 @@ enum selection_snap { SNAP_LINE = 2 }; -enum window_state { - WIN_VISIBLE = 1, - WIN_FOCUSED = 2 -}; - typedef unsigned char uchar; typedef unsigned int uint; typedef unsigned long ulong; @@ -189,6 +184,7 @@ void redraw(void); int tattrset(int); void tnew(int, int); +void tresize(int, int); void tsetdirt(int, int); void tsetdirtattr(int); int match(uint, uint); @@ -201,14 +197,11 @@ void ttywrite(const char *, size_t); void resettitle(void); char *kmap(KeySym, uint); -void cresize(int, int); void selclear(void); void selnormalize(void); int selected(int, int); char *getsel(void); -int x2col(int); -int y2row(int); size_t utf8decode(char *, Rune *, size_t); size_t utf8encode(Rune, char *); diff --git a/win.h b/win.h index 1d5e664..c4b5566 100644 --- a/win.h +++ b/win.h @@ -19,7 +19,6 @@ int xsetcolorname(int, const char *); void xsetenv(void); void xsettitle(char *); void xsetpointermotion(int); -void xresize(int, int); void xsetsel(char *, Time); void zoom(const Arg *); void zoomabs(const Arg *); diff --git a/x.c b/x.c index 6f8d4bc..e7acf2e 100644 --- a/x.c +++ b/x.c @@ -94,6 +94,7 @@ static int xloadfont(Font *, FcPattern *); static void xunloadfont(Font *); static void xhints(void); static void xinit(void); +static void xresize(int, int); static void xseturgency(int); static void xloadfonts(char *, double); static void xunloadfonts(void); @@ -115,6 +116,10 @@ static void selrequest(XEvent *); static void selcopy(Time); static void selinit(void); + +static int x2col(int); +static int y2row(int); +static void cresize(int, int); static void getbuttoninfo(XEvent *); static void mousereport(XEvent *); static void usage(void); @@ -151,6 +156,11 @@ static DC dc; static XWindow xw; static XSelection xsel; +enum window_state { + WIN_VISIBLE = 1, + WIN_FOCUSED = 2 +}; + /* Font Ring Cache */ enum { FRC_NORMAL, @@ -169,6 +179,41 @@ typedef struct { static Fontcache frc[16]; static int frclen = 0; +int +x2col(int x) +{ + x -= borderpx; + x /= win.cw; + + return LIMIT(x, 0, term.col-1); +} + +int +y2row(int y) +{ + y -= borderpx; + y /= win.ch; + + return LIMIT(y, 0, term.row-1); +} + +void +cresize(int width, int height) +{ + int col, row; + + if (width != 0) + win.w = width; + if (height != 0) + win.h = height; + + col = (win.w - 2 * borderpx) / win.cw; + row = (win.h - 2 * borderpx) / win.ch; + + tresize(col, row); + xresize(col, row); +} + void getbuttoninfo(XEvent *e) { -- 2.14.1
[hackers] [st][PATCH 11/23] have ttyresize take new dimensions as arguments
This allows us to encapsulate TermWindow entirely inside x.c. --- st.c | 7 +++ st.h | 3 +-- x.c | 7 --- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/st.c b/st.c index be1666e..71d24ae 100644 --- a/st.c +++ b/st.c @@ -188,7 +188,6 @@ static ssize_t xwrite(int, const char *, size_t); static void *xrealloc(void *, size_t); /* Globals */ -TermWindow win; Term term; Selection sel; int cmdfd; @@ -870,14 +869,14 @@ ttysend(char *s, size_t n) } void -ttyresize(void) +ttyresize(int tw, int th) { struct winsize w; w.ws_row = term.row; w.ws_col = term.col; - w.ws_xpixel = win.tw; - w.ws_ypixel = win.th; + w.ws_xpixel = tw; + w.ws_ypixel = th; if (ioctl(cmdfd, TIOCSWINSZ, &w) < 0) fprintf(stderr, "Couldn't set window size: %s\n", strerror(errno)); } diff --git a/st.h b/st.h index ede19e3..980c353 100644 --- a/st.h +++ b/st.h @@ -190,7 +190,7 @@ void tsetdirtattr(int); int match(uint, uint); void ttynew(void); size_t ttyread(void); -void ttyresize(void); +void ttyresize(int, int); void ttysend(char *, size_t); void ttywrite(const char *, size_t); @@ -210,7 +210,6 @@ void *xmalloc(size_t); char *xstrdup(char *); /* Globals */ -extern TermWindow win; extern Term term; extern Selection sel; extern int cmdfd; diff --git a/x.c b/x.c index bf5e531..dc851d8 100644 --- a/x.c +++ b/x.c @@ -152,6 +152,7 @@ static void (*handler[LASTEvent])(XEvent *) = { }; /* Globals */ +static TermWindow win; static DC dc; static XWindow xw; static XSelection xsel; @@ -484,7 +485,7 @@ zoomabs(const Arg *arg) xunloadfonts(); xloadfonts(usedfont, arg->f); cresize(0, 0); - ttyresize(); + ttyresize(win.tw, win.th); redraw(); xhints(); } @@ -1719,7 +1720,7 @@ resize(XEvent *e) return; cresize(e->xconfigure.width, e->xconfigure.height); - ttyresize(); + ttyresize(win.tw, win.th); } void @@ -1750,7 +1751,7 @@ run(void) cresize(w, h); ttynew(); - ttyresize(); + ttyresize(win.tw, win.th); clock_gettime(CLOCK_MONOTONIC, &last); lastblink = last; -- 2.14.1
[hackers] [st][PATCH 09/23] Move font variables into x.c
--- st.c | 4 st.h | 4 x.c | 4 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/st.c b/st.c index b23c87e..341ad7d 100644 --- a/st.c +++ b/st.c @@ -207,10 +207,6 @@ static CSIEscape csiescseq; static STREscape strescseq; static int iofd = 1; -char *usedfont = NULL; -double usedfontsize = 0; -double defaultfontsize = 0; - static uchar utfbyte[UTF_SIZ + 1] = {0x80,0, 0xC0, 0xE0, 0xF0}; static uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; static Rune utfmin[UTF_SIZ + 1] = { 0,0, 0x80, 0x800, 0x1}; diff --git a/st.h b/st.h index 2a8bb55..0cfffa5 100644 --- a/st.h +++ b/st.h @@ -225,10 +225,6 @@ extern char *opt_name; extern char *opt_title; extern int oldbutton; -extern char *usedfont; -extern double usedfontsize; -extern double defaultfontsize; - /* config.h globals */ extern char font[]; extern int borderpx; diff --git a/x.c b/x.c index e7acf2e..aa8ad35 100644 --- a/x.c +++ b/x.c @@ -179,6 +179,10 @@ typedef struct { static Fontcache frc[16]; static int frclen = 0; +static char *usedfont = NULL; +static double usedfontsize = 0; +static double defaultfontsize = 0; + int x2col(int x) { -- 2.14.1
[hackers] [st][PATCH 05/23] Move ISO 14755 handling into x.c
--- st.c | 29 - win.h | 2 +- x.c | 28 +--- 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/st.c b/st.c index c070f46..306aa69 100644 --- a/st.c +++ b/st.c @@ -47,7 +47,6 @@ #define STR_ARG_SIZ ESC_ARG_SIZ /* macros */ -#define NUMMAXLEN(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1) #define DEFAULT(a, b) (a) = (a) ? (a) : (b) #define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == '\177') #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f)) @@ -55,8 +54,6 @@ #define ISDELIM(u) (utf8strchr(worddelimiters, u) != NULL) /* constants */ -#define ISO14755CMD"dmenu -w %lu -p codepoint: 7) - return; - if ((utf32 = strtoul(us, &e, 16)) == ULONG_MAX || - (*e != '\n' && *e != '\0')) - return; - - ttysend(uc, utf8encode(utf32, uc)); -} - void toggleprinter(const Arg *arg) { diff --git a/win.h b/win.h index 2454b50..bd2456b 100644 --- a/win.h +++ b/win.h @@ -8,6 +8,7 @@ void draw(void); void drawregion(int, int, int, int); +void iso14755(const Arg *); void xbell(void); void xclipcopy(void); void xclippaste(void); @@ -20,7 +21,6 @@ void xsetpointermotion(int); void xunloadfonts(void); void xresize(int, int); void xselpaste(void); -unsigned long xwinid(void); void xsetsel(char *, Time); void zoom(const Arg *); void zoomabs(const Arg *); diff --git a/x.c b/x.c index 529cb48..3099985 100644 --- a/x.c +++ b/x.c @@ -25,7 +25,11 @@ static char *argv0; #define XEMBED_FOCUS_IN 4 #define XEMBED_FOCUS_OUT 5 +/* constants */ +#define ISO14755CMD"dmenu -w %lu -p codepoint: > 8) #define TRUEGREEN(x) (((x) & 0xff00)) #define TRUEBLUE(x)(((x) & 0xff) << 8) @@ -1523,10 +1527,28 @@ xbell(void) XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL); } -unsigned long -xwinid(void) +void +iso14755(const Arg *arg) { - return xw.win; + char cmd[sizeof(ISO14755CMD) + NUMMAXLEN(xw.win)]; + FILE *p; + char *us, *e, codepoint[9], uc[UTF_SIZ]; + unsigned long utf32; + + snprintf(cmd, sizeof(cmd), ISO14755CMD, xw.win); + if (!(p = popen(cmd, "r"))) + return; + + us = fgets(codepoint, sizeof(codepoint), p); + pclose(p); + + if (!us || *us == '\0' || *us == '-' || strlen(us) > 7) + return; + if ((utf32 = strtoul(us, &e, 16)) == ULONG_MAX || + (*e != '\n' && *e != '\0')) + return; + + ttysend(uc, utf8encode(utf32, uc)); } void -- 2.14.1
[hackers] [st][PATCH 06/23] xloadfonts/xunloadfonts now local to x.c
--- win.h | 2 -- x.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/win.h b/win.h index bd2456b..bf7481f 100644 --- a/win.h +++ b/win.h @@ -14,11 +14,9 @@ void xclipcopy(void); void xclippaste(void); void xloadcols(void); int xsetcolorname(int, const char *); -void xloadfonts(char *, double); void xsetenv(void); void xsettitle(char *); void xsetpointermotion(int); -void xunloadfonts(void); void xresize(int, int); void xselpaste(void); void xsetsel(char *, Time); diff --git a/x.c b/x.c index 3099985..9049fa9 100644 --- a/x.c +++ b/x.c @@ -95,6 +95,8 @@ static void xunloadfont(Font *); static void xhints(void); static void xinit(void); static void xseturgency(int); +static void xloadfonts(char *, double); +static void xunloadfonts(void); static void expose(XEvent *); static void visibility(XEvent *); -- 2.14.1
[hackers] [st][PATCH 02/23] Move bell/urgent hints into x.c
xseturgency is now internal to x.c --- config.def.h | 2 +- st.c | 5 + st.h | 1 + win.h| 3 +-- x.c | 8 ++-- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/config.def.h b/config.def.h index 877afab..dd94be2 100644 --- a/config.def.h +++ b/config.def.h @@ -60,7 +60,7 @@ unsigned int cursorthickness = 2; * bell volume. It must be a value between -100 and 100. Use 0 for disabling * it */ -static int bellvolume = 0; +int bellvolume = 0; /* default TERM value */ char termname[] = "st-256color"; diff --git a/st.c b/st.c index f1227ea..7ae864e 100644 --- a/st.c +++ b/st.c @@ -2183,10 +2183,7 @@ tcontrolcode(uchar ascii) /* backwards compatibility to xterm */ strhandle(); } else { - if (!(win.state & WIN_FOCUSED)) - xseturgency(1); - if (bellvolume) - xbell(bellvolume); + xbell(); } break; case '\033': /* ESC */ diff --git a/st.h b/st.h index 28a751d..e4328fc 100644 --- a/st.h +++ b/st.h @@ -245,6 +245,7 @@ extern int allowaltscreen; extern unsigned int xfps; extern unsigned int actionfps; extern unsigned int cursorthickness; +extern int bellvolume; extern unsigned int blinktimeout; extern char termname[]; extern const char *colorname[]; diff --git a/win.h b/win.h index 7b614eb..e6e4448 100644 --- a/win.h +++ b/win.h @@ -10,7 +10,7 @@ typedef XftGlyphFontSpec GlyphFontSpec; void draw(void); void drawregion(int, int, int, int); -void xbell(int); +void xbell(void); void xclipcopy(void); void xclippaste(void); void xhints(void); @@ -20,7 +20,6 @@ void xloadfonts(char *, double); void xsetenv(void); void xsettitle(char *); void xsetpointermotion(int); -void xseturgency(int); void xunloadfonts(void); void xresize(int, int); void xselpaste(void); diff --git a/x.c b/x.c index d68261c..b5cc58e 100644 --- a/x.c +++ b/x.c @@ -91,6 +91,7 @@ static int xgeommasktogravity(int); static int xloadfont(Font *, FcPattern *); static void xunloadfont(Font *); static void xinit(void); +static void xseturgency(int); static void expose(XEvent *); static void visibility(XEvent *); @@ -1484,9 +1485,12 @@ xseturgency(int add) } void -xbell(int vol) +xbell(void) { - XkbBell(xw.dpy, xw.win, vol, (Atom)NULL); + if (!(win.state & WIN_FOCUSED)) + xseturgency(1); + if (bellvolume) + XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL); } unsigned long -- 2.14.1
[hackers] [st][PATCH 04/23] Move Glyph/Font workarounds closer to typedefs
--- st.c | 3 --- st.h | 1 + x.c | 4 +--- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/st.c b/st.c index 825197c..c070f46 100644 --- a/st.c +++ b/st.c @@ -28,9 +28,6 @@ #include #include -#define Glyph Glyph_ -#define Font Font_ - #include "st.h" #include "win.h" diff --git a/st.h b/st.h index 53d5009..ed7fa52 100644 --- a/st.h +++ b/st.h @@ -94,6 +94,7 @@ typedef XftGlyphFontSpec GlyphFontSpec; typedef uint_least32_t Rune; +#define Glyph Glyph_ typedef struct { Rune u; /* character code */ ushort mode; /* attribute flags */ diff --git a/x.c b/x.c index 8aeb9c0..529cb48 100644 --- a/x.c +++ b/x.c @@ -15,9 +15,6 @@ #include #include -#define Glyph Glyph_ -#define Font Font_ - static char *argv0; #include "arg.h" @@ -59,6 +56,7 @@ typedef struct { } XSelection; /* Font structure */ +#define Font Font_ typedef struct { int height; int width; -- 2.14.1
[hackers] [st][PATCH 07/23] Move selection functions into x.c
--- st.c | 35 +-- st.h | 1 - win.h | 5 +++-- x.c | 27 +++ 4 files changed, 27 insertions(+), 41 deletions(-) diff --git a/st.c b/st.c index 306aa69..559ce73 100644 --- a/st.c +++ b/st.c @@ -118,10 +118,7 @@ typedef struct { } Key; /* function definitions used in config.h */ -static void clipcopy(const Arg *); -static void clippaste(const Arg *); static void numlock(const Arg *); -static void selpaste(const Arg *); static void printsel(const Arg *); static void printscreen(const Arg *) ; static void toggleprinter(const Arg *); @@ -408,18 +405,6 @@ base64dec(const char *src) return result; } -void -selinit(void) -{ - clock_gettime(CLOCK_MONOTONIC, &sel.tclick1); - clock_gettime(CLOCK_MONOTONIC, &sel.tclick2); - sel.mode = SEL_IDLE; - sel.snap = 0; - sel.ob.x = -1; - sel.primary = NULL; - sel.clipboard = NULL; -} - int x2col(int x) { @@ -622,24 +607,6 @@ getsel(void) return str; } -void -selpaste(const Arg *dummy) -{ - xselpaste(); -} - -void -clipcopy(const Arg *dummy) -{ - xclipcopy(); -} - -void -clippaste(const Arg *dummy) -{ - xclippaste(); -} - void selclear(void) { @@ -1866,7 +1833,7 @@ strhandle(void) dec = base64dec(strescseq.args[2]); if (dec) { xsetsel(dec, CurrentTime); - clipcopy(NULL); + xclipcopy(); } else { fprintf(stderr, "erresc: invalid base64\n"); } diff --git a/st.h b/st.h index ed7fa52..1a8bc5c 100644 --- a/st.h +++ b/st.h @@ -204,7 +204,6 @@ char *kmap(KeySym, uint); void cresize(int, int); void selclear(void); -void selinit(void); void selnormalize(void); int selected(int, int); char *getsel(void); diff --git a/win.h b/win.h index bf7481f..1d5e664 100644 --- a/win.h +++ b/win.h @@ -8,17 +8,18 @@ void draw(void); void drawregion(int, int, int, int); +void clipcopy(const Arg *); +void clippaste(const Arg *); void iso14755(const Arg *); +void selpaste(const Arg *); void xbell(void); void xclipcopy(void); -void xclippaste(void); void xloadcols(void); int xsetcolorname(int, const char *); void xsetenv(void); void xsettitle(char *); void xsetpointermotion(int); void xresize(int, int); -void xselpaste(void); void xsetsel(char *, Time); void zoom(const Arg *); void zoomabs(const Arg *); diff --git a/x.c b/x.c index 9049fa9..6f8d4bc 100644 --- a/x.c +++ b/x.c @@ -114,6 +114,7 @@ static void selclear_(XEvent *); static void selrequest(XEvent *); static void selcopy(Time); +static void selinit(void); static void getbuttoninfo(XEvent *); static void mousereport(XEvent *); static void usage(void); @@ -413,7 +414,7 @@ selnotify(XEvent *e) } void -xselpaste(void) +selpaste(const Arg *dummy) { XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY, xw.win, CurrentTime); @@ -451,7 +452,7 @@ zoomreset(const Arg *arg) } void -xclipcopy(void) +clipcopy(const Arg *dummy) { Atom clipboard; @@ -466,7 +467,13 @@ xclipcopy(void) } void -xclippaste(void) +xclipcopy() +{ + clipcopy(NULL); +} + +void +clippaste(const Arg *dummy) { Atom clipboard; @@ -559,7 +566,7 @@ brelease(XEvent *e) } if (e->xbutton.button == Button2) { - xselpaste(); + selpaste(NULL); } else if (e->xbutton.button == Button1) { if (sel.mode == SEL_READY) { getbuttoninfo(e); @@ -1024,6 +1031,18 @@ xinit(void) xsel.xtarget = XA_STRING; } +void +selinit(void) +{ + clock_gettime(CLOCK_MONOTONIC, &sel.tclick1); + clock_gettime(CLOCK_MONOTONIC, &sel.tclick2); + sel.mode = SEL_IDLE; + sel.snap = 0; + sel.ob.x = -1; + sel.primary = NULL; + sel.clipboard = NULL; +} + int xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y) { -- 2.14.1
[hackers] [st] Refactoring (-61 lines)
Some refactoring on the st codebase, reducing it by 61 lines and establishing cleaner separation between st.c and x.c. There are 28 fewer extern variables, and and X-specific types are now included only in x.c. These commits move code between st.c and x.c, adjusting the linkage/storage modifiers or headers as needed. Notable change is patch 17/23, which migrates the config.h include from st.c to x.c, since most of the configuration variables were only used there.
Re: [hackers] [PATCH][sbase] Add patch(1)
On Sun, 24 Sep 2017 19:24:10 +0200 Silvan Jegen wrote: > Heyho > > On Sun, Sep 24, 2017 at 06:28:57PM +0200, Mattias Andrée wrote: > > On Sun, 24 Sep 2017 14:08:41 +0200 > > Silvan Jegen wrote: > > > > > > + > > > > + if (!new->len) > > > > + for (i = 0; i < old->len; i++) > > > > + if (old->lines[i].data[-2] != '-') > > > > > > I think according to the standard, refering to data[-2] invokes undefined > > > behaviour, since at this time, data points to the beginning of the array. > > > > > > > I'm not sure whether it is defined or undefined; I would think that it > > defined, but that adding integers larger than INTPTR_MAX is undefined. > > I will change to `*(data - 2)` as this is clearly defined. > > I was referring to > https://stackoverflow.com/questions/3473675/are-negative-array-indexes-allowed-in-c/3473686#3473686 > . `*(data -2) is equivalent to 'data[-2]' but since 'data' doesn't point > to the second element of the array, I don't think this is valid. Hi! I think there has been some misunderstanding here, and that we are in agreement that `a[-b]` in it self is not invalid, but that question is whether the deferenced address is valid. I understand why this looks incorrect, `old->lines->data[0]` does not actually point to the first character on a line in a line but rather to the first character in the line that is part of the content of the file that hunk patches. For example if the patchfile contains the line "- abc", `old->lines->data[0]` is `a`, not `-`, because "- " part of the annotations in the hunk. This should probably be clarified, but you can see that this happening just above this code. I will look that your other comments later. pgpWxkIdgIKQz.pgp Description: OpenPGP digital signature
Re: [hackers] [sbase][PATCH] patch: improvments suggested by Silvan
On Sun, 24 Sep 2017 11:12:35 -0700 Michael Forney wrote: > Hi Mattias, > > Instead of sending these patches on top of your original patch, can > you send amended versions (v2, v3, etc)? You can use `git format-patch > -v 2` to make this clear in the subject. > > I think that this would make it easier to review and keep track of your patch. > > Thanks! > Hi Michael! I thought it would be easier to see the changes I make. I think an amended version makes more sense when it's time to merge. pgpGklutUDIM8.pgp Description: OpenPGP digital signature
Re: [hackers] [sbase][PATCH] patch: improvments suggested by Silvan
Hi Mattias, Instead of sending these patches on top of your original patch, can you send amended versions (v2, v3, etc)? You can use `git format-patch -v 2` to make this clear in the subject. I think that this would make it easier to review and keep track of your patch. Thanks!
Re: [hackers] [PATCH][sbase] Add patch(1)
Heyho On Sun, Sep 24, 2017 at 06:28:57PM +0200, Mattias Andrée wrote: > On Sun, 24 Sep 2017 14:08:41 +0200 > Silvan Jegen wrote: > > > Heyho Mattias! > > > > I had a look at the patch. It's a lot of code (still only about 1/3 of > > GNU's patch size though) and it was rather hard for me to follow so more > > review should be done. Find my comments below. > > > > On Sun, Sep 03, 2017 at 07:13:20PM +0200, Mattias Andrée wrote: > > > +static void > > > +save_file_cpp(FILE *f, struct file_data *file) > > > +{ > > > + size_t i, j, n; > > > + char annot = ' '; > > > + > > > + for (i = 0; i <= file->n; i++) { > > > + if ((n = file->d[i].nold)) { > > > > In other places you iterate with "i < file->n" (see save_file below for > > example) so I think this may be an off-by-one error. > > There is an if-statement, that breaks the loop if `i == file->`, > after this clause, so this should be correct. I'll add blank lines I saw the break statement but the 'n = file->d[i].nold' will be executed before reaching that break statement resulting in a segfault when i == file->n. > around that if-statement to make it clearer. > > > > > > > > + fprintf(f, "%s\n", annot == '+' ? "#else" : ifndef); > > > + for (j = 0; j < n; j++) { > > > + fwriteline(f, file->d[i].old[j]); > > > + if (missinglf(file->d[i].old[j])) > > > + fprintf(f, "\n"); > > > + } > > > + annot = '-'; > > > + } > > > + if (i == file->n) > > > + break; > > > + if (annot == '-') > > > + fprintf(f, "%s\n", file->d[i].new ? "#else" : "#endif"); > > > + else if (annot == ' ' && file->d[i].new) > > > + fprintf(f, "%s\n", ifdef); > > > + else if (annot == '+' && !file->d[i].new) > > > + fprintf(f, "#endif\n"); > > > + fwriteline(f, file->d[i].line); > > > + if ((i + 1 < file->n || file->d[i].new) && > > > missinglf(file->d[i].line)) > > > + fprintf(f, "\n"); > > > + annot = file->d[i].new ? '+' : ' '; > > > + } > > > + if (annot != ' ') > > > + fprintf(f, "#endif\n"); > > > +} > > > + > > > +static void > > > +parse_hunk_copied(struct hunk *hunk, struct parsed_hunk *parsed) > > > +{ > > > + struct hunk_content *old = &parsed->old, *new = &parsed->new; > > > + size_t i = 0, a, b; > > > + char *p; > > > + > > > + free(hunk->head->data); > > > + > > > + old->lines = enmalloc(FAILURE, hunk->nlines * sizeof(*old->lines)); > > > + new->lines = enmalloc(FAILURE, hunk->nlines * sizeof(*new->lines)); > > > + parsed->annot = enmalloc(FAILURE, hunk->nlines + 1); > > > + > > > + p = hunk->lines[i++].data + 4; > > > + old->start = strtoul(p, &p, 10); > > > + old->len = 0; > > > + > > > + for (; hunk->lines[i].data[1] == ' '; i++) > > > + subline(old->lines + old->len++, hunk->lines + i, 2); > > > + > > > + p = hunk->lines[i++].data + 4; > > > + new->start = strtoul(p, &p, 10); > > > + new->len = 0; > > > + > > > + if (old->len) { > > > + for (; i < hunk->nlines; i++) > > > + subline(new->lines + new->len++, hunk->lines + i, 2); > > > + } else { > > > + for (; i < hunk->nlines; i++) { > > > + subline(new->lines + new->len++, hunk->lines + i, 2); > > > + if (hunk->lines[i].data[0] != '+') > > > + subline(old->lines + old->len++, hunk->lines + > > > i, 2); > > > + } > > > + } > > > > I think this if-else block can be rewritten like this. > > > > for (; i < hunk->nlines; i++) { > > subline(new->lines + new->len++, hunk->lines + i, 2); > > if (old->len == 0 && hunk->lines[i].data[0] != '+') > > subline(old->lines + old->len++, hunk->lines + i, 2); > > } > > I will use `!old->len` instead of `old->len == 0`. > > > > > > > > + > > > + if (!new->len) > > > + for (i = 0; i < old->len; i++) > > > + if (old->lines[i].data[-2] != '-') > > > > I think according to the standard, refering to data[-2] invokes undefined > > behaviour, since at this time, data points to the beginning of the array. > > I'm not sure whether it is defined or undefined; I would think that it > defined, but that adding integers larger than INTPTR_MAX is undefined. > I will change to `*(data - 2)` as this is clearly defined. I was referring to https://stackoverflow.com/questions/3473675/are-negative-array-indexes-allowed-in-c/3473686#3473686 . `*(data -2) is equivalent to 'data[-2]' but since 'data' doesn't point to the second element of the array, I don't think this is valid. > > > > > > > + new->lines[new->len++] = old->lines[i]; > > > + > > > +#define OLDLINE a < old->len && old->lines[a].data[-2] > > > +#define NEWLINE b < new->len && new->lines[b].data[-2] > > > + > > > + for (i = a
[hackers] [sbase][PATCH] patch: improvments suggested by Silvan
Signed-off-by: Mattias Andrée --- patch.c | 182 +--- 1 file changed, 95 insertions(+), 87 deletions(-) diff --git a/patch.c b/patch.c index c118dc9..caf34be 100644 --- a/patch.c +++ b/patch.c @@ -41,7 +41,7 @@ #define linecpy2mem(d, s) (memcpy(d, (s).data, (s).len + 1)) #define missinglf(l) ((l).len && (l).data[(l).len - 1] != '\n') #define fwriteline(f, l) (fwrite((l).data, 1, (l).len, f)) -#define enmemdup(f, s, n) ((n) ? memcpy(enmalloc(f, n), s, n) : 0) +#define enmemdup(f, s, n) ((n) ? memcpy(enmalloc(f, n), s, n) : NULL) enum { REJECTED = 1, FAILURE = 2 }; enum applicability { APPLICABLE, APPLIED, INAPPLICABLE }; @@ -104,10 +104,10 @@ struct patched_file { }; static enum format specified_format = GUESS; -static const char *patchfile = 0; -static char *rejectfile = 0; -static const char *outfile = 0; -static char *apply_patches_to = 0; +static const char *patchfile = NULL; +static char *rejectfile = NULL; +static const char *outfile = NULL; +static char *apply_patches_to = NULL; static size_t pflag = SIZE_MAX; static int bflag = 0; static int fflag = 0; @@ -115,16 +115,16 @@ static int lflag = 0; static int Rflag = 0; static int Nflag = 0; static int Uflag = 0; -static char *dflag = 0; +static char *dflag = NULL; static int rejected = 0; -static struct patched_file *prevpatch = 0; +static struct patched_file *prevpatch = NULL; static size_t prevpatchn = 0; -static struct patched_file *prevout = 0; +static struct patched_file *prevout = NULL; static size_t prevoutn = 0; static char stdin_dash[sizeof("-")]; static char stdout_dash[sizeof("-")]; -static char *ifdef = 0; -static char *ifndef = 0; +static char *ifdef = NULL; +static char *ifndef = NULL; static void usage(void) @@ -134,7 +134,7 @@ usage(void) } static void -load_lines(const char *path, struct file_data *out, int skip_lf, int orig) +load_lines(struct file_data *out, const char *path, int skip_lf, int orig) { FILE *f; struct linebuf b = EMPTY_LINEBUF; @@ -165,12 +165,12 @@ static char * ask(const char *instruction) { FILE *f; - char *answer = 0; + char *answer = NULL; size_t size = 0; ssize_t n; if (fflag) - return 0; + return NULL; if (!(f = fopen("/dev/tty", "r+"))) enprintf(FAILURE, "fopen /dev/tty:"); @@ -180,13 +180,13 @@ ask(const char *instruction) fflush(stdout); if ((n = getline(&answer, &size, f)) <= 0) { - answer = 0; + answer = NULL; } else { n -= (answer[n - 1] == '\n'); - answer[n] = 0; + answer[n] = '\0'; if (!*answer) { free(answer); - answer = 0; + answer = NULL; } } @@ -202,13 +202,13 @@ adjust_filename(const char *filename) const char *stripped = filename; char *rc; - if (p == filename || p[-1] == '/') - return 0; + if (p == filename || *(p - 1) == '/') + return NULL; for (; strips && (p = strchr(stripped, '/')); strips--) for (stripped = p; *stripped == '/'; stripped++); if (strips && pflag != SIZE_MAX) - return 0; + return NULL; if (dflag && *stripped != '/') enasprintf(FAILURE, &rc, "%s/%s", dflag, stripped); @@ -283,7 +283,7 @@ unquote(char *str) } } - str[w] = 0; + str[w] = '\0'; return 0; } @@ -302,7 +302,7 @@ parse_diff_line(char *str, char **old, char **new) char *s = strchr(str, '\0'); int ret = 0; - *new = 0; + *new = NULL; if (s == str) return -1; @@ -313,7 +313,7 @@ again: goto found; } else { while (--s != str && s - 1 != str) - if (s[-1] == ' ' && s[0] == '"') + if (*(s - 1) == ' ' && s[0] == '"') goto found; } @@ -348,19 +348,24 @@ ask_for_filename(struct patchset *patchset) { size_t i; char *answer; + int missing = 0; - for (i = 0; i < patchset->npatches; i++) - if (!patchset->patches[i].path) - goto found_unset; - return; + for (i = 0; i < patchset->npatches; i++) { + if (!patchset->patches[i].path) { + missing = 1; + break; + } + } + + if (!missing) + return; -found_unset: if (!(answer = ask("please enter name of file to patch"))) exit(FAILURE); - /* Two assumtions are made here. (1) if there are multiple -* patches to unnamed failes, they are all to the same file, -* (with
Re: [hackers] [PATCH][sbase] Add patch(1)
On Sun, 24 Sep 2017 14:08:41 +0200 Silvan Jegen wrote: > Heyho Mattias! > > I had a look at the patch. It's a lot of code (still only about 1/3 of > GNU's patch size though) and it was rather hard for me to follow so more > review should be done. Find my comments below. > > On Sun, Sep 03, 2017 at 07:13:20PM +0200, Mattias Andrée wrote: > > +static void > > +save_file_cpp(FILE *f, struct file_data *file) > > +{ > > + size_t i, j, n; > > + char annot = ' '; > > + > > + for (i = 0; i <= file->n; i++) { > > + if ((n = file->d[i].nold)) { > > In other places you iterate with "i < file->n" (see save_file below for > example) so I think this may be an off-by-one error. There is an if-statement, that breaks the loop if `i == file->`, after this clause, so this should be correct. I'll add blank lines around that if-statement to make it clearer. > > > > + fprintf(f, "%s\n", annot == '+' ? "#else" : ifndef); > > + for (j = 0; j < n; j++) { > > + fwriteline(f, file->d[i].old[j]); > > + if (missinglf(file->d[i].old[j])) > > + fprintf(f, "\n"); > > + } > > + annot = '-'; > > + } > > + if (i == file->n) > > + break; > > + if (annot == '-') > > + fprintf(f, "%s\n", file->d[i].new ? "#else" : "#endif"); > > + else if (annot == ' ' && file->d[i].new) > > + fprintf(f, "%s\n", ifdef); > > + else if (annot == '+' && !file->d[i].new) > > + fprintf(f, "#endif\n"); > > + fwriteline(f, file->d[i].line); > > + if ((i + 1 < file->n || file->d[i].new) && > > missinglf(file->d[i].line)) > > + fprintf(f, "\n"); > > + annot = file->d[i].new ? '+' : ' '; > > + } > > + if (annot != ' ') > > + fprintf(f, "#endif\n"); > > +} > > + > > +static void > > +parse_hunk_copied(struct hunk *hunk, struct parsed_hunk *parsed) > > +{ > > + struct hunk_content *old = &parsed->old, *new = &parsed->new; > > + size_t i = 0, a, b; > > + char *p; > > + > > + free(hunk->head->data); > > + > > + old->lines = enmalloc(FAILURE, hunk->nlines * sizeof(*old->lines)); > > + new->lines = enmalloc(FAILURE, hunk->nlines * sizeof(*new->lines)); > > + parsed->annot = enmalloc(FAILURE, hunk->nlines + 1); > > + > > + p = hunk->lines[i++].data + 4; > > + old->start = strtoul(p, &p, 10); > > + old->len = 0; > > + > > + for (; hunk->lines[i].data[1] == ' '; i++) > > + subline(old->lines + old->len++, hunk->lines + i, 2); > > + > > + p = hunk->lines[i++].data + 4; > > + new->start = strtoul(p, &p, 10); > > + new->len = 0; > > + > > + if (old->len) { > > + for (; i < hunk->nlines; i++) > > + subline(new->lines + new->len++, hunk->lines + i, 2); > > + } else { > > + for (; i < hunk->nlines; i++) { > > + subline(new->lines + new->len++, hunk->lines + i, 2); > > + if (hunk->lines[i].data[0] != '+') > > + subline(old->lines + old->len++, hunk->lines + > > i, 2); > > + } > > + } > > I think this if-else block can be rewritten like this. > > for (; i < hunk->nlines; i++) { > subline(new->lines + new->len++, hunk->lines + i, 2); > if (old->len == 0 && hunk->lines[i].data[0] != '+') > subline(old->lines + old->len++, hunk->lines + i, 2); > } I will use `!old->len` instead of `old->len == 0`. > > > > + > > + if (!new->len) > > + for (i = 0; i < old->len; i++) > > + if (old->lines[i].data[-2] != '-') > > I think according to the standard, refering to data[-2] invokes undefined > behaviour, since at this time, data points to the beginning of the array. I'm not sure whether it is defined or undefined; I would think that it defined, but that adding integers larger than INTPTR_MAX is undefined. I will change to `*(data - 2)` as this is clearly defined. > > > > + new->lines[new->len++] = old->lines[i]; > > + > > +#define OLDLINE a < old->len && old->lines[a].data[-2] > > +#define NEWLINE b < new->len && new->lines[b].data[-2] > > + > > + for (i = a = b = 0; a < old->len || b < new->len;) { > > + if (OLDLINE == '-') parsed->annot[i++] = '-', a++; > > + if (NEWLINE == '+') parsed->annot[i++] = '+', b++; > > + while (OLDLINE == ' ' && NEWLINE == ' ') > > + parsed->annot[i++] = ' ', a++, b++; > > + while (OLDLINE == '!') parsed->annot[i++] = '<', a++; > > + while (NEWLINE == '!') parsed->annot[i++] = '>', b++; > > + } > > + parsed->annot[i] = 0; > > +} > > + > > +static void > > +apply_contiguous_edit(struct file_data *f, size_t ln, size_t rm, size_t > > ad, struct li
[hackers] [slstatus] Move components into dedicated subdirectory || Laslo Hunhold
commit 7246dc4381c6c95454672a5c1aff65a02d6d3747 Author: Laslo Hunhold AuthorDate: Sun Sep 24 15:33:01 2017 +0200 Commit: Aaron Marcher CommitDate: Sun Sep 24 17:20:27 2017 +0200 Move components into dedicated subdirectory This brings us a lot more tidiness. diff --git a/Makefile b/Makefile index 1a90431..00c35c4 100644 --- a/Makefile +++ b/Makefile @@ -6,25 +6,25 @@ include config.mk REQ = util COM =\ - battery\ - cpu\ - datetime\ - disk\ - entropy\ - hostname\ - ip\ - kernel_release\ - keyboard_indicators\ - load_avg\ - num_files\ - ram\ - run_command\ - swap\ - temperature\ - uptime\ - user\ - volume\ - wifi + components/battery\ + components/cpu\ + components/datetime\ + components/disk\ + components/entropy\ + components/hostname\ + components/ip\ + components/kernel_release\ + components/keyboard_indicators\ + components/load_avg\ + components/num_files\ + components/ram\ + components/run_command\ + components/swap\ + components/temperature\ + components/uptime\ + components/user\ + components/volume\ + components/wifi all: slstatus @@ -39,14 +39,14 @@ config.h: $(CC) -o $@ $(LDFLAGS) $< $(COM:=.o) $(REQ:=.o) $(LDLIBS) .c.o: - $(CC) -c $(CPPFLAGS) $(CFLAGS) $< + $(CC) -o $@ -c $(CPPFLAGS) $(CFLAGS) $< clean: rm -f slstatus slstatus.o $(COM:=.o) $(REQ:=.o) dist: rm -rf "slstatus-$(VERSION)" - mkdir -p "slstatus-$(VERSION)" + mkdir -p "slstatus-$(VERSION)/components" cp -R LICENSE Makefile README config.mk config.def.h \ arg.h slstatus.c $(COM:=.c) $(REQ:=.c) $(REQ:=.h) \ slstatus.1 "slstatus-$(VERSION)" diff --git a/battery.c b/components/battery.c similarity index 98% rename from battery.c rename to components/battery.c index 0cea55c..f384aab 100644 --- a/battery.c +++ b/components/battery.c @@ -3,7 +3,7 @@ #include #include -#include "util.h" +#include "../util.h" const char * battery_perc(const char *bat) diff --git a/cpu.c b/components/cpu.c similarity index 98% rename from cpu.c rename to components/cpu.c index b4b7ef1..4a4a80b 100644 --- a/cpu.c +++ b/components/cpu.c @@ -2,7 +2,7 @@ #include #include -#include "util.h" +#include "../util.h" const char * cpu_freq(void) diff --git a/datetime.c b/components/datetime.c similarity index 91% rename from datetime.c rename to components/datetime.c index 98510e3..0816923 100644 --- a/datetime.c +++ b/components/datetime.c @@ -1,7 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include -#include "util.h" +#include "../util.h" const char * datetime(const char *fmt) diff --git a/disk.c b/components/disk.c similarity index 98% rename from disk.c rename to components/disk.c index 51cdaaa..90a8e0b 100644 --- a/disk.c +++ b/components/disk.c @@ -3,7 +3,7 @@ #include #include -#include "util.h" +#include "../util.h" const char * disk_free(const char *mnt) diff --git a/entropy.c b/components/entropy.c similarity index 91% rename from entropy.c rename to components/entropy.c index 211022a..0d3564e 100644 --- a/entropy.c +++ b/components/entropy.c @@ -1,7 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include -#include "util.h" +#include "../util.h" const char * entropy(void) diff --git a/hostname.c b/components/hostname.c similarity index 91% rename from hostname.c rename to components/hostname.c index 0ad1f3b..aed77a6 100644 --- a/hostname.c +++ b/components/hostname.c @@ -2,7 +2,7 @@ #include #include -#include "util.h" +#include "../util.h" const char * hostname(void) diff --git a/ip.c b/components/ip.c similarity index 98% rename from ip.c rename to components/ip.c index a042c79..f98b2ed 100644 --- a/ip.c +++ b/components/ip.c @@ -5,7 +5,7 @@ #include #include -#include "util.h" +#include "../util.h" const char * ipv4(const char *iface) diff --git a/kernel_release.c b/components/kernel_release.c similarity index 92% rename from kernel_release.c rename to components/kernel_release.c index abe0acd..f539b6a 100644 --- a/kernel_release.c +++ b/components/kernel_release.c @@ -2,7 +2,7 @@ #include #include -#include "util.h" +#include "../util.h" const char * kernel_release(void) diff --git a/keyboard_indicators.c b/components/keyboard_indicators.c similarity index 95% rename from keyboard_indicators.c rename to components/keyboard_indicators.c index 13e8648..b7713b6 100644 --- a/keyboard_indicators.c +++ b/components/keyboard_indicators.c @@ -2,7 +2,7 @@ #include #include -#include "util.h" +#include "../util.h" const char * keyboard_indicators(void) diff --git a/load_avg.c b/components/load_avg.c similarity index 93% rename from load_avg.c rename to components/load_avg.c index ad22ae4..d6f6b
[hackers] [surf][PATCH] add support for WebGL
--- config.def.h | 1 + surf.c | 5 + 2 files changed, 6 insertions(+) diff --git a/config.def.h b/config.def.h index 2e735bf..ec99e27 100644 --- a/config.def.h +++ b/config.def.h @@ -46,6 +46,7 @@ static Parameter defconfig[ParameterLast] = { [SpellLanguages] = { { .v = ((char *[]){ "en_US", NULL }) }, }, [StrictTLS] = { { .i = 1 }, }, [Style] = { { .i = 1 }, }, + [WebGl] = { { .i = 1 }, }, [ZoomLevel] = { { .f = 1.0 }, }, }; diff --git a/surf.c b/surf.c index 0f8b9c9..65b2aeb 100644 --- a/surf.c +++ b/surf.c @@ -79,6 +79,7 @@ typedef enum { SpellLanguages, StrictTLS, Style, + WebGl, ZoomLevel, ParameterLast } ParamName; @@ -820,6 +821,9 @@ setparameter(Client *c, int refresh, ParamName p, const Arg *a) setstyle(c, getstyle(geturi(c))); refresh = 0; break; + case WebGl: + webkit_settings_set_enable_webgl(s, a->i); + break; case ZoomLevel: webkit_web_view_set_zoom_level(c->view, a->f); return; /* do not update */ @@ -1079,6 +1083,7 @@ newview(Client *c, WebKitWebView *rv) "enable-accelerated-2d-canvas", curconfig[AcceleratedCanvas].val.i, "enable-site-specific-quirks", curconfig[SiteQuirks].val.i, "enable-smooth-scrolling", curconfig[SmoothScrolling].val.i, + "enable-webgl", curconfig[WebGl].val.i, "media-playback-requires-user-gesture", curconfig[MediaManualPlay].val.i, NULL); /* For more interesting settings, have a look at -- 2.11.0
Re: [hackers] [PATCH][sbase] Add patch(1)
Heyho Mattias! I had a look at the patch. It's a lot of code (still only about 1/3 of GNU's patch size though) and it was rather hard for me to follow so more review should be done. Find my comments below. On Sun, Sep 03, 2017 at 07:13:20PM +0200, Mattias Andrée wrote: > Signed-off-by: Mattias Andrée > --- > Makefile |2 + > README |1 + > TODO |1 - > libutil/asprintf.c | 74 +++ > libutil/getlines.c | 17 +- > patch.1| 250 +++ > patch.c| 1835 > > text.h |4 +- > util.h |5 + > 9 files changed, 2182 insertions(+), 7 deletions(-) > create mode 100644 libutil/asprintf.c > create mode 100644 patch.1 > create mode 100644 patch.c > > diff --git a/Makefile b/Makefile > index 1c39fef..014db74 100644 > --- a/Makefile > +++ b/Makefile > @@ -45,6 +45,7 @@ LIBUTFSRC =\ > > LIBUTIL = libutil.a > LIBUTILSRC =\ > + libutil/asprintf.c\ > libutil/concat.c\ > libutil/cp.c\ > libutil/crypt.c\ > @@ -132,6 +133,7 @@ BIN =\ > nohup\ > od\ > paste\ > + patch\ > pathchk\ > printenv\ > printf\ > diff --git a/README b/README > index da2e500..6c94f2f 100644 > --- a/README > +++ b/README > @@ -59,6 +59,7 @@ The following tools are implemented: > 0#*|o nl . > 0=*|o nohup . > 0=*|o od . > +0=patch . > 0#* o pathchk . > #*|o paste . > 0=*|x printenv. > diff --git a/TODO b/TODO > index 5edb8a3..fe2344e 100644 > --- a/TODO > +++ b/TODO > @@ -8,7 +8,6 @@ awk > bc > diff > ed manpage > -patch > stty > > If you are looking for some work to do on sbase, another option is to > diff --git a/libutil/asprintf.c b/libutil/asprintf.c > new file mode 100644 > index 000..929ed09 > --- /dev/null > +++ b/libutil/asprintf.c > @@ -0,0 +1,74 @@ > +/* See LICENSE file for copyright and license details. */ > +#include > +#include > +#include > + > +#include "../util.h" > + > +static int xenvasprintf(int, char **, const char *, va_list); > + > +int > +asprintf(char **strp, const char *fmt, ...) > +{ > + va_list ap; > + int ret; > + > + va_start(ap, fmt); > + ret = xenvasprintf(-1, strp, fmt, ap); > + va_end(ap); > + > + return ret; > +} > + > +int > +easprintf(char **strp, const char *fmt, ...) > +{ > + va_list ap; > + int ret; > + > + va_start(ap, fmt); > + ret = xenvasprintf(1, strp, fmt, ap); > + va_end(ap); > + > + return ret; > +} > + > +int > +enasprintf(int status, char **strp, const char *fmt, ...) > +{ > + va_list ap; > + int ret; > + > + va_start(ap, fmt); > + ret = xenvasprintf(status, strp, fmt, ap); > + va_end(ap); > + > + return ret; > +} > + > +int > +xenvasprintf(int status, char **strp, const char *fmt, va_list ap) > +{ > + int ret; > + va_list ap2; > + > + va_copy(ap2, ap); > + ret = vsnprintf(0, 0, fmt, ap2); > + va_end(ap2); > + if (ret < 0) { > + if (status >= 0) > + enprintf(status, "vsnprintf:"); > + *strp = 0; > + return -1; > + } > + > + *strp = malloc(ret + 1); > + if (!*strp) { > + if (status >= 0) > + enprintf(status, "malloc:"); > + return -1; > + } > + > + vsprintf(*strp, fmt, ap); > + return ret; > +} > diff --git a/libutil/getlines.c b/libutil/getlines.c > index b912769..9af7684 100644 > --- a/libutil/getlines.c > +++ b/libutil/getlines.c > @@ -7,7 +7,7 @@ > #include "../util.h" > > void > -getlines(FILE *fp, struct linebuf *b) > +ngetlines(int status, FILE *fp, struct linebuf *b) > { > char *line = NULL; > size_t size = 0, linelen = 0; > @@ -16,17 +16,24 @@ getlines(FILE *fp, struct linebuf *b) > while ((len = getline(&line, &size, fp)) > 0) { > if (++b->nlines > b->capacity) { > b->capacity += 512; > - b->lines = erealloc(b->lines, b->capacity * > sizeof(*b->lines)); > + b->lines = enrealloc(status, b->lines, b->capacity * > sizeof(*b->lines)); > } > linelen = len; > - b->lines[b->nlines - 1].data = memcpy(emalloc(linelen + 1), > line, linelen + 1); > + b->lines[b->nlines - 1].data = memcpy(enmalloc(status, linelen > + 1), line, linelen + 1); > b->lines[b->nlines - 1].len = linelen; > } > free(line); > - if (b->lines && b->nlines && linelen && b->lines[b->nlines - > 1].data[linelen - 1] != '\n') { > - b->lines[b->nlines - 1].data = erealloc(b->lines[b->nlines - > 1].data, linelen + 2); > + b->nolf = b->lines && b->nlines && linelen && b->lines[b->nlines - > 1].data[linelen - 1] != '\n'; > + if (b->nolf) { > + b->lines[b->nlines - 1].data = enrealloc(status,