aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dmenu.c30
-rw-r--r--draw.c7
2 files changed, 13 insertions, 24 deletions
diff --git a/dmenu.c b/dmenu.c
index 6ed025d..d8ef88f 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -36,9 +36,8 @@ static void paste(void);
36static void readstdin(void); 36static void readstdin(void);
37static void run(void); 37static void run(void);
38static void setup(void); 38static void setup(void);
39static void usage(void);
40 39
41static char text[BUFSIZ]; 40static char text[BUFSIZ] = "";
42static int bh, mw, mh; 41static int bh, mw, mh;
43static int inputw = 0; 42static int inputw = 0;
44static int lines = 0; 43static int lines = 0;
@@ -79,7 +78,7 @@ main(int argc, char *argv[]) {
79 else if(!strcmp(argv[i], "-i")) 78 else if(!strcmp(argv[i], "-i"))
80 fstrncmp = strncasecmp; 79 fstrncmp = strncasecmp;
81 else if(i == argc-1) 80 else if(i == argc-1)
82 usage(); 81 goto usage;
83 /* double flags */ 82 /* double flags */
84 else if(!strcmp(argv[i], "-l")) 83 else if(!strcmp(argv[i], "-l"))
85 lines = atoi(argv[++i]); 84 lines = atoi(argv[++i]);
@@ -98,15 +97,19 @@ main(int argc, char *argv[]) {
98 else if(!strcmp(argv[i], "-sf")) 97 else if(!strcmp(argv[i], "-sf"))
99 selfgcolor = argv[++i]; 98 selfgcolor = argv[++i];
100 else 99 else
101 usage(); 100 goto usage;
102 101
103 dc = initdc(); 102 dc = initdc();
104 initfont(dc, font); 103 initfont(dc, font);
105 readstdin(); 104 readstdin();
106 setup(); 105 setup();
107 run(); 106 run();
107 return EXIT_FAILURE;
108 108
109 return EXIT_FAILURE; /* should not reach */ 109usage:
110 fputs("usage: dmenu [-b] [-i] [-l lines] [-m monitor] [-p prompt] [-fn font]\n"
111 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
112 return EXIT_FAILURE;
110} 113}
111 114
112void 115void
@@ -223,7 +226,7 @@ keypress(XKeyEvent *ev) {
223 226
224 len = strlen(text); 227 len = strlen(text);
225 XLookupString(ev, buf, sizeof buf, &ksym, NULL); 228 XLookupString(ev, buf, sizeof buf, &ksym, NULL);
226 if(ev->state & ControlMask) { 229 if(ev->state & ControlMask)
227 switch(tolower(ksym)) { 230 switch(tolower(ksym)) {
228 default: 231 default:
229 return; 232 return;
@@ -277,7 +280,6 @@ keypress(XKeyEvent *ev) {
277 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime); 280 XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime);
278 return; 281 return;
279 } 282 }
280 }
281 switch(ksym) { 283 switch(ksym) {
282 default: 284 default:
283 if(!iscntrl(*buf)) 285 if(!iscntrl(*buf))
@@ -341,7 +343,6 @@ keypress(XKeyEvent *ev) {
341 case XK_Return: 343 case XK_Return:
342 case XK_KP_Enter: 344 case XK_KP_Enter:
343 fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout); 345 fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout);
344 fflush(stdout);
345 exit(EXIT_SUCCESS); 346 exit(EXIT_SUCCESS);
346 case XK_Right: 347 case XK_Right:
347 if(cursor < len) { 348 if(cursor < len) {
@@ -403,7 +404,7 @@ match(void) {
403 else 404 else
404 matches = lsubstr; 405 matches = lsubstr;
405 } 406 }
406 curr = prev = next = sel = matches; 407 curr = sel = matches;
407 calcoffsets(); 408 calcoffsets();
408} 409}
409 410
@@ -438,11 +439,10 @@ readstdin(void) {
438 for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) { 439 for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) {
439 if((p = strchr(buf, '\n'))) 440 if((p = strchr(buf, '\n')))
440 *p = '\0'; 441 *p = '\0';
441 if(!(item = malloc(sizeof *item))) 442 if(!(item = calloc(1, sizeof *item)))
442 eprintf("cannot malloc %u bytes\n", sizeof *item); 443 eprintf("cannot malloc %u bytes\n", sizeof *item);
443 if(!(item->text = strdup(buf))) 444 if(!(item->text = strdup(buf)))
444 eprintf("cannot strdup %u bytes\n", strlen(buf)+1); 445 eprintf("cannot strdup %u bytes\n", strlen(buf)+1);
445 item->next = item->left = item->right = NULL;
446 inputw = MAX(inputw, textw(dc, item->text)); 446 inputw = MAX(inputw, textw(dc, item->text));
447 } 447 }
448} 448}
@@ -530,13 +530,5 @@ setup(void) {
530 inputw = MIN(inputw, mw/3); 530 inputw = MIN(inputw, mw/3);
531 promptw = prompt ? textw(dc, prompt) : 0; 531 promptw = prompt ? textw(dc, prompt) : 0;
532 XMapRaised(dc->dpy, win); 532 XMapRaised(dc->dpy, win);
533 text[0] = '\0';
534 match(); 533 match();
535} 534}
536
537void
538usage(void) {
539 fputs("usage: dmenu [-b] [-i] [-l lines] [-m monitor] [-p prompt] [-fn font]\n"
540 " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr);
541 exit(EXIT_FAILURE);
542}
diff --git a/draw.c b/draw.c
index 28c658c..80a5074 100644
--- a/draw.c
+++ b/draw.c
@@ -100,16 +100,13 @@ initdc(void) {
100 100
101 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) 101 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
102 weprintf("no locale support\n"); 102 weprintf("no locale support\n");
103 if(!(dc = malloc(sizeof *dc))) 103 if(!(dc = calloc(1, sizeof *dc)))
104 eprintf("cannot malloc %u bytes\n", sizeof *dc); 104 eprintf("cannot malloc %u bytes\n", sizeof *dc);
105 if(!(dc->dpy = XOpenDisplay(NULL))) 105 if(!(dc->dpy = XOpenDisplay(NULL)))
106 eprintf("cannot open display\n"); 106 eprintf("cannot open display\n");
107 107
108 dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL); 108 dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL);
109 XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter); 109 XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
110 dc->font.xfont = NULL;
111 dc->font.set = NULL;
112 dc->canvas = None;
113 return dc; 110 return dc;
114} 111}
115 112
@@ -187,7 +184,7 @@ void
187weprintf(const char *fmt, ...) { 184weprintf(const char *fmt, ...) {
188 va_list ap; 185 va_list ap;
189 186
190 fprintf(stderr, "%s: warning: ", progname); 187 fprintf(stderr, "%s: ", progname);
191 va_start(ap, fmt); 188 va_start(ap, fmt);
192 vfprintf(stderr, fmt, ap); 189 vfprintf(stderr, fmt, ap);
193 va_end(ap); 190 va_end(ap);