Re: [hackers] [dwm][PATCH] Do not call die() upon '-v' invocation

2016-10-29 Thread Klemens Nanni

On Sat, Oct 29, 2016 at 08:42:12PM +0200, Laslo Hunhold wrote:

On Fri, 28 Oct 2016 14:40:01 +0200
Klemens Nanni <k...@posteo.org> wrote:


Returning -1 upon a valid invocation like 'dwm -v' is just wrong.


I agree, but we should get rid of this EXIT_* stuff altogether.
My proposal:

if (argc == 2 && !strcmp("-v", argv[1])) {
fputs("dwm-"VERSION, stdout);
return 0;
} else if (argc != 1)
die("usage: dwm [-v]");

What do you guys think about it?


I'm used to seeing/printing version information on stderr as it's more
of a debug information than normal output.

EXIT_* aren't really needed imho either.



[hackers] [slock][PATCH] Improve option parsing routine

2016-10-28 Thread Klemens Nanni
This reduces the amount of strcmp() calls and comparisons in general to
a minimum.
---
 slock.c | 13 ++---
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/slock.c b/slock.c
index 283b04e..e9b050a 100644
--- a/slock.c
+++ b/slock.c
@@ -298,15 +298,14 @@ main(int argc, char *argv[]) {
Display *dpy;
int s, nlocks, nscreens;
 
-   if (argc > 1 && !strncmp("-", argv[1], 1)) {
-   if ((argc == 2 && !strcmp("-v", argv[1])) ||
-   (argc == 3 && !strcmp("-v", argv[1]) && !strcmp("--", 
argv[2]))) {
+   if (argv[1] && argv[1][0] == '-') {
+   if (argv[1][1] == 'v' && argv[1][2] == '\0' &&
+   (argc == 2 || (argc == 3 && !strcmp(argv[2], "--" {
fputs("slock-"VERSION"\n", stderr);
return 0;
-   } else if (!strcmp("--", argv[1])) {
-   --argc;
-   ++argv;
-   } else
+   } else if (argv[1][1] == '-' && argv[1][2] == '\0')
+   --argc, ++argv;
+   else
die("usage: slock [-v] [cmd [arg ...]]\n");
}
 
-- 
2.8.3




[hackers] [slock][PATCH] Remove arg.h, simplify option parsing

2016-10-28 Thread Klemens Nanni
arg.h is really ugly code and way to complex for tools like slock that
have such minimal synopsis.

Regardless of that, usage() does not have to be a function of it's own
if it's called just once.

This (hopefully) is the final commit implementing proper command-line
pargsing adhering to the POSIX Utility Syntax Guidelines[0] after
submitting two incomplete patches to the hackers@suckless mailinglist.

Thanks to quinq and emg for pointing out this out.

0: 
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02
---
 arg.h   | 65 -
 slock.c | 32 ++--
 2 files changed, 14 insertions(+), 83 deletions(-)
 delete mode 100644 arg.h

diff --git a/arg.h b/arg.h
deleted file mode 100644
index 0b23c53..000
--- a/arg.h
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copy me if you can.
- * by 20h
- */
-
-#ifndef ARG_H__
-#define ARG_H__
-
-extern char *argv0;
-
-/* use main(int argc, char *argv[]) */
-#define ARGBEGIN   for (argv0 = *argv, argv++, argc--;\
-   argv[0] && argv[0][0] == '-'\
-   && argv[0][1];\
-   argc--, argv++) {\
-   char argc_;\
-   char **argv_;\
-   int brk_;\
-   if (argv[0][1] == '-' && argv[0][2] == '\0') {\
-   argv++;\
-   argc--;\
-   break;\
-   }\
-   for (brk_ = 0, argv[0]++, argv_ = argv;\
-   argv[0][0] && !brk_;\
-   argv[0]++) {\
-   if (argv_ != argv)\
-   break;\
-   argc_ = argv[0][0];\
-   switch (argc_)
-
-/* Handles obsolete -NUM syntax */
-#define ARGNUM case '0':\
-   case '1':\
-   case '2':\
-   case '3':\
-   case '4':\
-   case '5':\
-   case '6':\
-   case '7':\
-   case '8':\
-   case '9'
-
-#define ARGEND }\
-   }
-
-#define ARGC() argc_
-
-#define ARGNUMF()  (brk_ = 1, estrtonum(argv[0], 0, INT_MAX))
-
-#define EARGF(x)   ((argv[0][1] == '\0' && argv[1] == NULL)?\
-   ((x), abort(), (char *)0) :\
-   (brk_ = 1, (argv[0][1] != '\0')?\
-   ([0][1]) :\
-   (argc--, argv++, argv[0])))
-
-#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\
-   (char *)0 :\
-   (brk_ = 1, (argv[0][1] != '\0')?\
-   ([0][1]) :\
-   (argc--, argv++, argv[0])))
-
-#define LNGARG()   [0][0]
-
-#endif
diff --git a/slock.c b/slock.c
index ad539dc..283b04e 100644
--- a/slock.c
+++ b/slock.c
@@ -19,11 +19,8 @@
 #include 
 #include 
 
-#include "arg.h"
 #include "util.h"
 
-char *argv0;
-
 enum {
INIT,
INPUT,
@@ -289,14 +286,8 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
return NULL;
 }
 
-static void
-usage(void)
-{
-   die("usage: slock [-v] [cmd [arg ...]]\n");
-}
-
 int
-main(int argc, char **argv) {
+main(int argc, char *argv[]) {
struct xrandr rr;
struct lock **locks;
struct passwd *pwd;
@@ -307,13 +298,17 @@ main(int argc, char **argv) {
Display *dpy;
int s, nlocks, nscreens;
 
-   ARGBEGIN {
-   case 'v':
-   fprintf(stderr, "slock-"VERSION"\n");
-   return 0;
-   default:
-   usage();
-   } ARGEND
+   if (argc > 1 && !strncmp("-", argv[1], 1)) {
+   if ((argc == 2 && !strcmp("-v", argv[1])) ||
+   (argc == 3 && !strcmp("-v", argv[1]) && !strcmp("--", 
argv[2]))) {
+   fputs("slock-"VERSION"\n", stderr);
+   return 0;
+   } else if (!strcmp("--", argv[1])) {
+   --argc;
+   ++argv;
+   } else
+   die("usage: slock [-v] [cmd [arg ...]]\n");
+   }
 
/* validate drop-user and -group */
errno = 0;
@@ -367,13 +362,14 @@ main(int argc, char **argv) {
return 1;
 
/* run post-lock 

[hackers] [slock][PATCH] Allow usage of -- as end-of-options specifier

2016-10-28 Thread Klemens Nanni
As pointed out in #suckless, sticking to the POSIX Utility Syntax
Guidelines[0] is a good idea.

0: 
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02
---
 slock.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/slock.c b/slock.c
index af5edb2..1d28e3a 100644
--- a/slock.c
+++ b/slock.c
@@ -302,6 +302,9 @@ main(int argc, char *argv[]) {
if (!strcmp("-v", argv[1])) {
fputs("slock-"VERSION"\n", stderr);
return 0;
+   } else if (!strcmp("--", argv[1])) {
+   --argc;
+   ++argv;
} else
die("usage: slock [-v] [cmd [arg ...]]\n");
}
-- 
2.8.3




[hackers] [slock][PATCH] Replace arg.h with if statements, simplify

2016-10-28 Thread Klemens Nanni
arg.h is pretty ugly code, but that is a matter of taste. Technically
speaking the argument parsing can be done much cleaner especially for
programs with such simple synopsis.

Regardless of that, usage() does not have to be a function of it's own
if it's called just once.

++argv is put on a seperate line since doing it within the execvp() call
would produce a compiler warning.
---
 arg.h   | 65 -
 slock.c | 28 ++--
 2 files changed, 10 insertions(+), 83 deletions(-)
 delete mode 100644 arg.h

diff --git a/arg.h b/arg.h
deleted file mode 100644
index 0b23c53..000
--- a/arg.h
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copy me if you can.
- * by 20h
- */
-
-#ifndef ARG_H__
-#define ARG_H__
-
-extern char *argv0;
-
-/* use main(int argc, char *argv[]) */
-#define ARGBEGIN   for (argv0 = *argv, argv++, argc--;\
-   argv[0] && argv[0][0] == '-'\
-   && argv[0][1];\
-   argc--, argv++) {\
-   char argc_;\
-   char **argv_;\
-   int brk_;\
-   if (argv[0][1] == '-' && argv[0][2] == '\0') {\
-   argv++;\
-   argc--;\
-   break;\
-   }\
-   for (brk_ = 0, argv[0]++, argv_ = argv;\
-   argv[0][0] && !brk_;\
-   argv[0]++) {\
-   if (argv_ != argv)\
-   break;\
-   argc_ = argv[0][0];\
-   switch (argc_)
-
-/* Handles obsolete -NUM syntax */
-#define ARGNUM case '0':\
-   case '1':\
-   case '2':\
-   case '3':\
-   case '4':\
-   case '5':\
-   case '6':\
-   case '7':\
-   case '8':\
-   case '9'
-
-#define ARGEND }\
-   }
-
-#define ARGC() argc_
-
-#define ARGNUMF()  (brk_ = 1, estrtonum(argv[0], 0, INT_MAX))
-
-#define EARGF(x)   ((argv[0][1] == '\0' && argv[1] == NULL)?\
-   ((x), abort(), (char *)0) :\
-   (brk_ = 1, (argv[0][1] != '\0')?\
-   ([0][1]) :\
-   (argc--, argv++, argv[0])))
-
-#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\
-   (char *)0 :\
-   (brk_ = 1, (argv[0][1] != '\0')?\
-   ([0][1]) :\
-   (argc--, argv++, argv[0])))
-
-#define LNGARG()   [0][0]
-
-#endif
diff --git a/slock.c b/slock.c
index ad539dc..af5edb2 100644
--- a/slock.c
+++ b/slock.c
@@ -19,11 +19,8 @@
 #include 
 #include 
 
-#include "arg.h"
 #include "util.h"
 
-char *argv0;
-
 enum {
INIT,
INPUT,
@@ -289,14 +286,8 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
return NULL;
 }
 
-static void
-usage(void)
-{
-   die("usage: slock [-v] [cmd [arg ...]]\n");
-}
-
 int
-main(int argc, char **argv) {
+main(int argc, char *argv[]) {
struct xrandr rr;
struct lock **locks;
struct passwd *pwd;
@@ -307,13 +298,13 @@ main(int argc, char **argv) {
Display *dpy;
int s, nlocks, nscreens;
 
-   ARGBEGIN {
-   case 'v':
-   fprintf(stderr, "slock-"VERSION"\n");
-   return 0;
-   default:
-   usage();
-   } ARGEND
+   if (argc > 1 && !strncmp("-", argv[1], 1)) {
+   if (!strcmp("-v", argv[1])) {
+   fputs("slock-"VERSION"\n", stderr);
+   return 0;
+   } else
+   die("usage: slock [-v] [cmd [arg ...]]\n");
+   }
 
/* validate drop-user and -group */
errno = 0;
@@ -367,13 +358,14 @@ main(int argc, char **argv) {
return 1;
 
/* run post-lock command */
-   if (argc > 0) {
+   if (argc > 1) {
switch (fork()) {
case -1:
die("slock: fork failed: %s\n", strerror(errno));
case 0:
if (close(ConnectionNumber(dpy)) < 0)
die("slock: close: %s\n", strerror(errno));
+   ++argv;
 

[hackers] [dwm][PATCH] Do not call die() upon '-v' invocation

2016-10-28 Thread Klemens Nanni
Returning -1 upon a valid invocation like 'dwm -v' is just wrong.
---
 dwm.c | 6 --
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/dwm.c b/dwm.c
index 421bf27..35828b4 100644
--- a/dwm.c
+++ b/dwm.c
@@ -2122,8 +2122,10 @@ zoom(const Arg *arg)
 int
 main(int argc, char *argv[])
 {
-   if (argc == 2 && !strcmp("-v", argv[1]))
-   die("dwm-"VERSION);
+   if (argc == 2 && !strcmp("-v", argv[1])) {
+   fputs("dwm-"VERSION, stderr);
+   return EXIT_SUCCESS;
+   }
else if (argc != 1)
die("usage: dwm [-v]");
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
-- 
2.8.3




[hackers] [st][PATCH] Add missing device path to '-l' example

2016-10-13 Thread Klemens Nanni
Also, it's ttyS0 not ttySO.
---
 st.1 | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/st.1 b/st.1
index 1e3f1d4..aedc174 100644
--- a/st.1
+++ b/st.1
@@ -96,18 +96,18 @@ use a tty
 .I line
 instead of a pseudo terminal.
 .I line
-should be a (pseudo-)serial device (e.g. /dev/ttySO on Linux for serial port
+should be a (pseudo-)serial device (e.g. /dev/ttyS0 on Linux for serial port
 0).
 When this flag is given
 remaining arguments are used as flags for
 .BR stty(1).
 By default st initializes the serial line to 8 bits, no parity, 1 stop bit
 and a 38400 baud rate. The speed is set by appending it as last argument
-(e.g. 'st -l 115200'). Arguments before the last one are
+(e.g. 'st -l /dev/ttyS0 115200'). Arguments before the last one are
 .BR stty(1)
 flags. If you want to set odd parity on 115200 baud use for example 'st -l
-parenb parodd 115200'. Set the number of bits by using for example 'st -l cs7
-115200'. See
+/dev/ttyS0 parenb parodd 115200'. Set the number of bits by using for
+example 'st -l /dev/ttyS0 cs7 115200'. See
 .BR stty(1)
 for more arguments and cases.
 .TP
-- 
2.8.3




Re: [hackers] [PATCH] [slock] Remove faulty example and add a section on security considerations

2016-09-28 Thread Klemens Nanni

On Wed, Sep 28, 2016 at 09:48:25PM +0200, FRIGN wrote:

Setting `DontVTSwitch' in xorg.conf(5) disables this feature
completely whereas chjj's fork (which mine is based on) blocks it in
slock only, which is imho a much saner approach since there are many
legitimate reasons to use multiple virtual terminals.


Can you point me to the piece of code that disables VT-switching in
your fork? I couldn't find it.

It's implicitly blocked by capturing the keys being pressed and
executing optional steps such as shutting down upon input.


Same story for `DontZap': I like quickly killing X with Ctrl+Alt+BS
while this should obviously be forbidden on a locked screen.


What you do is call
system("doas setxkbmap -option &");
which disables Ctrl+Alt+Backspace for the entire session. So you can
only kill your X server until you have locked your screen once. It
won't work afterwards, which sucks and is unpredictable.

Fair point, I shall fix this.


signature.asc
Description: PGP signature


Re: [hackers] [PATCH] [slock] Remove faulty example and add a section on security considerations

2016-09-28 Thread Klemens Nanni

On Wed, Sep 28, 2016 at 09:09:24PM +0200, FRIGN wrote:

I know this fork, and with the changes presented in this patch, slock
is just as secure as his version.
The difference is that he for instance implemented ways to upload
webcam images to imgur, send SMS's and auto-shutdown when the user
tries to switch VT's.

I removed media upload and SMS support since those features can easily
be added using a small wrapper script.


I think these changes are not necessary. If somebody tries to change
VT's, so be it! Especially because the shutdown sequence can open other
attack surfaces, which he also took care of mostly, by disallowing the
use of Sysrq in the shutdown sequence. In my opinion, with a strong
password and setting the configs as in the manpage, slock is damn
secure. It honestly took me a few days to analyze the "paranoid" slock
fork to find out that what I did was sufficient.

Setting `DontVTSwitch' in xorg.conf(5) disables this feature completely
whereas chjj's fork (which mine is based on) blocks it in slock only,
which is imho a much saner approach since there are many legitimate
reasons to use multiple virtual terminals.

Same story for `DontZap': I like quickly killing X with Ctrl+Alt+BS
while this should obviously be forbidden on a locked screen.

Best regards,
kl3


signature.asc
Description: PGP signature


Re: [hackers] [ubase][PATCH 1/5] mount: Use ternary operators

2016-07-11 Thread Klemens Nanni

The second hunk actually breaks it by setting either fsopts or dirname
but never both, so please ignore that one.


signature.asc
Description: PGP signature


[hackers] [ubase][PATCH 5/5] mount: Simplify exit logic

2016-07-11 Thread Klemens Nanni
---
 mount.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/mount.c b/mount.c
index d0b2fd8..529ee86 100644
--- a/mount.c
+++ b/mount.c
@@ -124,9 +124,8 @@ mounthelper(const char *fsname, const char *dir, const char 
*fstype)
eargv[i] = NULL;
 
execvp(eprog, (char * const *)eargv);
-   if (errno == ENOENT)
-   _exit(1);
-   weprintf("execvp:");
+   if (errno != ENOENT)
+   weprintf("execvp:");
_exit(1);
break;
default:
-- 
2.8.3




[hackers] [ubase][PATCH 3/5] mount: Typofix

2016-07-11 Thread Klemens Nanni
---
 mount.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mount.c b/mount.c
index 1f17789..1696219 100644
--- a/mount.c
+++ b/mount.c
@@ -54,7 +54,7 @@ findtype(const char *types, const char *t)
 }
 
 static void
-parseopts(const char *popts, unsigned long *flags, char *data, size_t datasiz)
+parseopts(const char *popts, unsigned long *flags, char *data, size_t datasize)
 {
unsigned int i, validopt;
size_t optlen, dlen = 0;
@@ -83,7 +83,7 @@ parseopts(const char *popts, unsigned long *flags, char 
*data, size_t datasiz)
 
if (!validopt && optlen > 0) {
/* unknown option, pass as data option to mount() */
-   if (dlen + optlen + 2 >= datasiz)
+   if (dlen + optlen + 2 >= datasize)
return; /* prevent overflow */
if (dlen)
data[dlen++] = ',';
-- 
2.8.3




[hackers] [ubase][PATCH 4/5] mount: Fix ambigious if statement

2016-07-11 Thread Klemens Nanni
---
 mount.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mount.c b/mount.c
index 1696219..d0b2fd8 100644
--- a/mount.c
+++ b/mount.c
@@ -253,9 +253,10 @@ main(int argc, char *argv[])
target = me->mnt_dir;
source = me->mnt_fsname;
}
-   if (!fsopts[0])
+   if (!fsopts[0]) {
estrlcat(fsopts, me->mnt_opts, 
sizeof(fsopts));
parseopts(fsopts, , data, 
sizeof(data));
+   }
if (!types)
types = me->mnt_type;
goto mountsingle;
-- 
2.8.3




Re: [hackers] [dmenu] [PATCHES] Improve code readability

2016-06-22 Thread Klemens Nanni

On Tue, Jun 21, 2016 at 10:19:57AM +0200, Hiltjo Posthuma wrote:

I don't think it is an improvement, so will disregard it.

Certainly not performance- or featurewise and the first patch may well
be thrown away as well, but how is turning this lengthy and empty-bodied
for loop into a much clearer while loop *not* an improvement in readability?

Just being curious.

Best regards,
kl3


signature.asc
Description: PGP signature


[hackers] [dmenu] [PATCHES] Improve code readability

2016-06-20 Thread Klemens Nanni

Hello,

those patches speak for themselves and apply cleanly on origin/HEAD.

Best regards,
kl3
From 9c23731e6eca581cbeed4a17ea734f857f816ef0 Mon Sep 17 00:00:00 2001
From: Klemens Nanni <k...@posteo.org>
Date: Tue, 24 May 2016 15:29:02 +0200
Subject: [PATCH 1/2] Use parantheses in ternary operators for better
 readability

---
 dmenu.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/dmenu.c b/dmenu.c
index e0c2f80..9d488d8 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -130,7 +130,7 @@ drawmenu(void)
x += promptw;
}
/* draw input field */
-   w = (lines > 0 || !matches) ? mw - x : inputw;
+   w = (lines > 0 || !matches) ? (mw - x) : inputw;
drw_setscheme(drw, [SchemeNorm]);
drw_text(drw, x, 0, w, bh, text, 0);
 
@@ -461,7 +461,7 @@ paste(void)
/* we have been given the current selection, now insert it into input */
XGetWindowProperty(dpy, win, utf8, 0, (sizeof text / 4) + 1, False,
   utf8, , , , , (unsigned char **));
-   insert(p, (q = strchr(p, '\n')) ? q - p : (ssize_t)strlen(p));
+   insert(p, (q = strchr(p, '\n')) ? (q - p) : (ssize_t)strlen(p));
XFree(p);
drawmenu();
 }
@@ -574,14 +574,14 @@ setup(void)
break;
 
x = info[i].x_org;
-   y = info[i].y_org + (topbar ? 0 : info[i].height - mh);
+   y = info[i].y_org + (topbar ? 0 : (info[i].height - mh));
mw = info[i].width;
XFree(info);
} else
 #endif
{
x = 0;
-   y = topbar ? 0 : sh - mh;
+   y = topbar ? 0 : (sh - mh);
mw = sw;
}
promptw = (prompt && *prompt) ? TEXTW(prompt) : 0;
-- 
2.8.3

From 4b3896ef24c913d3be391ae0740ac2b38b68e5c7 Mon Sep 17 00:00:00 2001
From: Klemens Nanni <k...@posteo.org>
Date: Tue, 24 May 2016 15:30:44 +0200
Subject: [PATCH 2/2] Use while not for loop

---
 dmenu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/dmenu.c b/dmenu.c
index 9d488d8..fe2501a 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -269,11 +269,11 @@ insert(const char *str, ssize_t n)
 static size_t
 nextrune(int inc)
 {
-   ssize_t n;
+   ssize_t n = cursor + inc;
 
/* return location of next utf8 rune in the given direction (+1 or -1) 
*/
-   for (n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += 
inc)
-   ;
+   while (n + inc >= 0 && (text[n] & 0xc0) == 0x80)
+   n += inc;
return n;
 }
 
-- 
2.8.3



signature.asc
Description: PGP signature


[hackers] [sites] Add new capscolor patch for slock

2016-01-30 Thread Klemens Nanni
I rebased Andrew Hills "capscolor" patch for slock onto the current HEAD
at 9dfe0ce, this patch adds it to the wiki.
commit ba95deb099021e893ed8b5f27d20dff4e5737454
Author: Klemens Nanni <k...@posteo.org>
Date:   Sat Jan 30 22:39:25 2016 +0100

[slock] 20160130-capscolor.diff

diff --git a/tools.suckless.org/slock/patches/capscolor.md b/tools.suckless.org/slock/patches/capscolor.md
index cd0fe80..189b174 100644
--- a/tools.suckless.org/slock/patches/capscolor.md
+++ b/tools.suckless.org/slock/patches/capscolor.md
@@ -8,12 +8,16 @@ Introduces an additional color to indicate the state of Caps Lock. Not
 compatible with the [failcolor](./failcolor) patch. Written against HEAD at
 a31b919, but should apply to 1.2.
 
+Version 20160130 is written against current HEAD at 9dfe0ce.
+
 Download
 
 
 * [slock-capscolor.diff](slock-capscolor.diff)
+* [slock-20160130-capscolor.diff](slock-20160130-capscolor.diff)
 
-Author
---
+Authors
+---
 
 * Andrew Hills <[ahi...@ednos.net](mailto:ahi...@ednos.net)>
+* Klemens Nanni <[k...@posteo.org](mailto:ahi...@ednos.net)> (20160130 version)
diff --git a/tools.suckless.org/slock/patches/slock-20160130-capscolor.diff b/tools.suckless.org/slock/patches/slock-20160130-capscolor.diff
new file mode 100644
index 000..06320b3
--- /dev/null
+++ b/tools.suckless.org/slock/patches/slock-20160130-capscolor.diff
@@ -0,0 +1,72 @@
+diff --git a/config.def.h b/config.def.h
+index fca0ae0..6673e54 100644
+--- a/config.def.h
 b/config.def.h
+@@ -2,5 +2,6 @@ static const char *colorname[NUMCOLS] = {
+ 	"black", /* after initialization */
+ 	"#005577",   /* during input */
+ 	"#CC",   /* failed/cleared the input */
++	"red",   /* CapsLock on */
+ };
+ static const Bool failonclear = True;
+diff --git a/slock.c b/slock.c
+index df2d3c6..8c1a791 100644
+--- a/slock.c
 b/slock.c
+@@ -17,6 +17,7 @@
+ #include 
+ #include 
+ #include 
++#include 
+ 
+ #if HAVE_BSD_AUTH
+ #include 
+@@ -27,6 +28,7 @@ enum {
+ 	INIT,
+ 	INPUT,
+ 	FAILED,
++	CAPS,
+ 	NUMCOLS
+ };
+ 
+@@ -127,15 +129,19 @@ readpw(Display *dpy, const char *pws)
+ #endif
+ {
+ 	char buf[32], passwd[256];
+-	int num, screen;
+-	unsigned int len, color;
++	int num, screen, caps;
++	unsigned int len, color, indicators;
+ 	KeySym ksym;
+ 	XEvent ev;
+ 	static int oldc = INIT;
+ 
+ 	len = 0;
++	caps = 0;
+ 	running = True;
+ 
++	if (!XkbGetIndicatorState(dpy, XkbUseCoreKbd, ))
++		caps = indicators & 1;
++
+ 	/* As "slock" stands for "Simple X display locker", the DPMS settings
+ 	 * had been removed and you can set it with "xset" or some other
+ 	 * utility. This way the user can easily set a customized DPMS
+@@ -177,6 +183,9 @@ readpw(Display *dpy, const char *pws)
+ if (len)
+ 	--len;
+ break;
++			case XK_Caps_Lock:
++caps = !caps;
++break;
+ 			default:
+ if (num && !iscntrl((int)buf[0]) && (len + num < sizeof(passwd))) {
+ 	memcpy(passwd + len, buf, num);
+@@ -184,7 +193,7 @@ readpw(Display *dpy, const char *pws)
+ }
+ break;
+ 			}
+-			color = len ? INPUT : (failure || failonclear ? FAILED : INIT);
++			color = len ? (caps ? CAPS : INPUT) : (failure || failonclear ? FAILED : INIT);
+ 			if (running && oldc != color) {
+ for (screen = 0; screen < nscreens; screen++) {
+ 	XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[color]);



signature.asc
Description: OpenPGP digital signature


[hackers] [dmenu] Typofix, patches rebased

2016-01-11 Thread Klemens Nanni
Hey,

cc596365a (unbolify dmenu) "breaks" some of the patches provided at
tools.suckless.org/dmenu/patches. Basically 's,bool,int' was all I had
to do besides some offset correction. Since this is the first time I'm
dealing with dmenu's source I do not guarantee anything, but it compiles
fine, though. Making them apply without errors ontop of latest git was
all I did, no changes in functionality or whatsoever.

Best Regards,
Klemens
>From 367f55b3fc31c23fdc0d534a04e3563938a58977 Mon Sep 17 00:00:00 2001
From: Klemens Nanni <k...@posteo.org>
Date: Mon, 11 Jan 2016 13:26:37 +0100
Subject: [PATCH 1/3] Typofix

---
 config.def.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/config.def.h b/config.def.h
index 8db1dda..dcffd38 100644
--- a/config.def.h
+++ b/config.def.h
@@ -6,7 +6,7 @@ static int topbar = 1;  /* -b  option; if 0, dmenu appears a
 static const char *fonts[] = {
 	"monospace:size=10"
 };
-static const char *prompt  = NULL;  /* -p  option; prompt to the elft of input field */
+static const char *prompt  = NULL;  /* -p  option; prompt to the left of input field */
 static const char *normbgcolor = "#22"; /* -nb option; normal background */
 static const char *normfgcolor = "#bb"; /* -nf option; normal foreground */
 static const char *selbgcolor  = "#005577"; /* -sb option; selected background   */
-- 
2.6.2




diff --git a/dmenu.c b/dmenu.c
index 4f22ffe..c2fc3ee 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -33,6 +33,7 @@ struct item {
 	char *text;
 	struct item *left, *right;
 	int out;
+	int distance;
 };
 
 static char text[BUFSIZ] = "";
@@ -254,6 +255,86 @@ match(void)
 	calcoffsets();
 }
 
+int
+compare_distance(const void *a, const void *b)
+{
+	struct item *da = *(struct item **) a;
+	struct item *db = *(struct item **) b;
+
+	if (!db)
+		return 1;
+	if (!da)
+		return -1;
+
+	return da->distance - db->distance;
+}
+
+void
+fuzzymatch(void)
+{
+	/* bang - we have so much memory */
+	struct item *it;
+	struct item **fuzzymatches = NULL;
+	char c;
+	int number_of_matches = 0, i, pidx, sidx, eidx;
+	int text_len = strlen(text), itext_len;
+
+	matches = matchend = NULL;
+
+	/* walk through all items */
+	for (it = items; it && it->text; it++) {
+		if (text_len) {
+			itext_len = strlen(it->text);
+			pidx = 0;
+			sidx = eidx = -1;
+			/* walk through item text */
+			for (i = 0; i < itext_len && (c = it->text[i]); i++) {
+/* fuzzy match pattern */
+if (text[pidx] == c) {
+	if(sidx == -1)
+		sidx = i;
+	pidx++;
+	if (pidx == text_len) {
+		eidx = i;
+		break;
+	}
+}
+			}
+			/* build list of matches */
+			if (eidx != -1) {
+/* compute distance */
+/* factor in 30% of sidx and distance between eidx and total
+ * text length .. let's see how it works */
+it->distance = eidx - sidx + (itext_len - eidx + sidx) / 3;
+appenditem(it, , );
+number_of_matches++;
+			}
+		} else {
+			appenditem(it, , );
+		}
+	}
+
+	if (number_of_matches) {
+		/* initialize array with matches */
+		if (!(fuzzymatches = realloc(fuzzymatches, number_of_matches * sizeof(struct item*
+			die("cannot realloc %u bytes:", number_of_matches * sizeof(struct item*));
+		for (i = 0, it = matches; it && i < number_of_matches; i++, it = it->right) {
+			fuzzymatches[i] = it;
+		}
+		/* sort matches according to distance */
+		qsort(fuzzymatches, number_of_matches, sizeof(struct item*), compare_distance);
+		/* rebuild list of matches */
+		matches = matchend = NULL;
+		for (i = 0, it = fuzzymatches[i];  i < number_of_matches && it && \
+it->text; i++, it = fuzzymatches[i]) {
+			appenditem(it, , );
+		}
+		free(fuzzymatches);
+	}
+	curr = sel = matches;
+	calcoffsets();
+}
+
 static void
 insert(const char *str, ssize_t n)
 {
@@ -264,7 +345,7 @@ insert(const char *str, ssize_t n)
 	if (n > 0)
 		memcpy([cursor], str, n);
 	cursor += n;
-	match();
+	fuzzymatch();
 }
 
 static size_t
@@ -309,7 +390,7 @@ keypress(XKeyEvent *ev)
 
 		case XK_k: /* delete right */
 			text[cursor] = '\0';
-			match();
+			fuzzymatch();
 			break;
 		case XK_u: /* delete left */
 			insert(NULL, 0 - cursor);
@@ -443,7 +524,7 @@ keypress(XKeyEvent *ev)
 		strncpy(text, sel->text, sizeof text - 1);
 		text[sizeof text - 1] = '\0';
 		cursor = strlen(text);
-		match();
+		fuzzymatch();
 		break;
 	}
 	drawmenu();
@@ -585,7 +666,7 @@ setup(void)
 	}
 	promptw = (prompt && *prompt) ? TEXTW(prompt) : 0;
 	inputw = MIN(inputw, mw/3);
-	match();
+	fuzzymatch();
 
 	/* create menu window */
 	swa.override_redirect = True;



diff --git a/dmenu.c b/dmenu.c
index e0c2f80..ef25442 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -487,6 +487,7 @@ readstdin(void)
 	}
 	if (items)
 		items[i].text = NULL;
+	if (i == 1 && items[0].tex

Re: [hackers] [dmenu] Typofix, patches rebased

2016-01-11 Thread Klemens Nanni
dmenu-git-20161101-instant.patch contains a typo at line 23,
's,next,text' fixes it.

Klemens Nanni:
> Hey, > > cc596365a (unbolify dmenu) "breaks" some of the patches provided at
> tools.suckless.org/dmenu/patches. Basically 's,bool,int' was all I had
> to do besides some offset correction. Since this is the first time I'm
> dealing with dmenu's source I do not guarantee anything, but it
compiles > fine, though. Making them apply without errors ontop of
latest git was > all I did, no changes in functionality or whatsoever. >
> Best Regards, > Klemens

-- 
Encrypt your messages using GNUPG if you can - nobody likes snoopers!
For more detailed information, look at the FSF's Email Self-Defense
Guide under https://emailselfdefense.fsf.org
My Key ID: 0x38ECD8BD | fingerprint: A5B8 05BF E06E 61AD 8399  0CDC 46B3
F9ED 38EC D8BD