1
0
Fork 0

removed -t, now using isatty() instead of select() to prevent execution from an interactive shell

This commit is contained in:
Anselm R. Garbe 2007-02-19 21:18:36 +01:00
parent 665488a6ee
commit 66b2e8379f
2 changed files with 15 additions and 31 deletions

View file

@ -10,7 +10,6 @@ dmenu \- dynamic menu
.RB [ \-p " <prompt>"] .RB [ \-p " <prompt>"]
.RB [ \-sb " <color>"] .RB [ \-sb " <color>"]
.RB [ \-sf " <color>"] .RB [ \-sf " <color>"]
.RB [ \-t " <seconds>"]
.RB [ \-v ] .RB [ \-v ]
.SH DESCRIPTION .SH DESCRIPTION
.SS Overview .SS Overview
@ -41,9 +40,6 @@ defines the selected background color (#RGB, #RRGGBB, and color names are suppor
.B \-sf <color> .B \-sf <color>
defines the selected foreground color (#RGB, #RRGGBB, and color names are supported). defines the selected foreground color (#RGB, #RRGGBB, and color names are supported).
.TP .TP
.B \-t <seconds>
defines the seconds to wait for standard input, before exiting (default is 3).
.TP
.B \-v .B \-v
prints version information to standard output, then exits. prints version information to standard output, then exits.
.SH USAGE .SH USAGE

42
main.c
View file

@ -10,8 +10,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <X11/Xutil.h> #include <X11/Xutil.h>
#include <X11/keysym.h> #include <X11/keysym.h>
@ -457,6 +455,12 @@ readstdin(void) {
return maxname; return maxname;
} }
static void
usage(void) {
eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
" [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n");
}
/* extern */ /* extern */
int screen; int screen;
@ -472,16 +476,16 @@ main(int argc, char *argv[]) {
char *normfg = NORMFGCOLOR; char *normfg = NORMFGCOLOR;
char *selbg = SELBGCOLOR; char *selbg = SELBGCOLOR;
char *selfg = SELFGCOLOR; char *selfg = SELFGCOLOR;
fd_set rd;
int i, j; int i, j;
struct timeval timeout;
Item *itm; Item *itm;
XEvent ev; XEvent ev;
XModifierKeymap *modmap; XModifierKeymap *modmap;
XSetWindowAttributes wa; XSetWindowAttributes wa;
timeout.tv_usec = 0; if(isatty(STDIN_FILENO)) {
timeout.tv_sec = 3; fputs("error: dmenu can't run in an interactive shell\n", stdout);
usage();
}
/* command line args */ /* command line args */
for(i = 1; i < argc; i++) for(i = 1; i < argc; i++)
if(!strncmp(argv[i], "-b", 3)) { if(!strncmp(argv[i], "-b", 3)) {
@ -505,41 +509,26 @@ main(int argc, char *argv[]) {
else if(!strncmp(argv[i], "-sf", 4)) { else if(!strncmp(argv[i], "-sf", 4)) {
if(++i < argc) selfg = argv[i]; if(++i < argc) selfg = argv[i];
} }
else if(!strncmp(argv[i], "-t", 3)) { else if(!strncmp(argv[i], "-v", 3))
if(++i < argc) timeout.tv_sec = atoi(argv[i]); eprint("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
}
else if(!strncmp(argv[i], "-v", 3)) {
fputs("dmenu-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
exit(EXIT_SUCCESS);
}
else else
eprint("usage: dmenu [-b] [-fn <font>] [-nb <color>] [-nf <color>] [-p <prompt>]\n" usage();
" [-sb <color>] [-sf <color>] [-t <seconds>] [-v]\n", stdout);
setlocale(LC_CTYPE, ""); setlocale(LC_CTYPE, "");
dpy = XOpenDisplay(0); dpy = XOpenDisplay(0);
if(!dpy) if(!dpy)
eprint("dmenu: cannot open display\n"); eprint("dmenu: cannot open display\n");
screen = DefaultScreen(dpy); screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen); root = RootWindow(dpy, screen);
/* Note, the select() construction allows to grab all keypresses as
* early as possible, to not loose them. But if there is no standard
* input supplied, we will make sure to exit after MAX_WAIT_STDIN
* seconds. This is convenience behavior for rapid typers.
*/
while(XGrabKeyboard(dpy, root, True, GrabModeAsync, while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
GrabModeAsync, CurrentTime) != GrabSuccess) GrabModeAsync, CurrentTime) != GrabSuccess)
usleep(1000); usleep(1000);
FD_ZERO(&rd);
FD_SET(STDIN_FILENO, &rd);
if(select(ConnectionNumber(dpy) + 1, &rd, NULL, NULL, &timeout) < 1)
goto UninitializedEnd;
maxname = readstdin(); maxname = readstdin();
/* init modifier map */ /* init modifier map */
modmap = XGetModifierMapping(dpy); modmap = XGetModifierMapping(dpy);
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
for (j = 0; j < modmap->max_keypermod; j++) { for (j = 0; j < modmap->max_keypermod; j++) {
if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock)) if(modmap->modifiermap[i * modmap->max_keypermod + j]
== XKeysymToKeycode(dpy, XK_Num_Lock))
numlockmask = (1 << i); numlockmask = (1 << i);
} }
} }
@ -607,7 +596,6 @@ main(int argc, char *argv[]) {
XFreePixmap(dpy, dc.drawable); XFreePixmap(dpy, dc.drawable);
XFreeGC(dpy, dc.gc); XFreeGC(dpy, dc.gc);
XDestroyWindow(dpy, win); XDestroyWindow(dpy, win);
UninitializedEnd:
XUngrabKeyboard(dpy, CurrentTime); XUngrabKeyboard(dpy, CurrentTime);
XCloseDisplay(dpy); XCloseDisplay(dpy);
return ret; return ret;