paring
210b3039
2 file(s) · +13 −24
| 36 | 36 | static void readstdin(void); |
|
| 37 | 37 | static void run(void); |
|
| 38 | 38 | static void setup(void); |
|
| 39 | - | static void usage(void); |
|
| 40 | 39 | ||
| 41 | - | static char text[BUFSIZ]; |
|
| 40 | + | static char text[BUFSIZ] = ""; |
|
| 42 | 41 | static int bh, mw, mh; |
|
| 43 | 42 | static int inputw = 0; |
|
| 44 | 43 | static int lines = 0; |
|
| 79 | 78 | else if(!strcmp(argv[i], "-i")) |
|
| 80 | 79 | fstrncmp = strncasecmp; |
|
| 81 | 80 | else if(i == argc-1) |
|
| 82 | - | usage(); |
|
| 81 | + | goto usage; |
|
| 83 | 82 | /* double flags */ |
|
| 84 | 83 | else if(!strcmp(argv[i], "-l")) |
|
| 85 | 84 | lines = atoi(argv[++i]); |
|
| 98 | 97 | else if(!strcmp(argv[i], "-sf")) |
|
| 99 | 98 | selfgcolor = argv[++i]; |
|
| 100 | 99 | else |
|
| 101 | - | usage(); |
|
| 100 | + | goto usage; |
|
| 102 | 101 | ||
| 103 | 102 | dc = initdc(); |
|
| 104 | 103 | initfont(dc, font); |
|
| 105 | 104 | readstdin(); |
|
| 106 | 105 | setup(); |
|
| 107 | 106 | run(); |
|
| 107 | + | return EXIT_FAILURE; |
|
| 108 | 108 | ||
| 109 | - | return EXIT_FAILURE; /* should not reach */ |
|
| 109 | + | usage: |
|
| 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 | ||
| 112 | 115 | void |
|
| 223 | 226 | ||
| 224 | 227 | len = strlen(text); |
|
| 225 | 228 | XLookupString(ev, buf, sizeof buf, &ksym, NULL); |
|
| 226 | - | if(ev->state & ControlMask) { |
|
| 229 | + | if(ev->state & ControlMask) |
|
| 227 | 230 | switch(tolower(ksym)) { |
|
| 228 | 231 | default: |
|
| 229 | 232 | return; |
|
| 277 | 280 | XConvertSelection(dc->dpy, XA_PRIMARY, utf8, utf8, win, CurrentTime); |
|
| 278 | 281 | return; |
|
| 279 | 282 | } |
|
| 280 | - | } |
|
| 281 | 283 | switch(ksym) { |
|
| 282 | 284 | default: |
|
| 283 | 285 | if(!iscntrl(*buf)) |
|
| 341 | 343 | case XK_Return: |
|
| 342 | 344 | case XK_KP_Enter: |
|
| 343 | 345 | fputs((sel && !(ev->state & ShiftMask)) ? sel->text : text, stdout); |
|
| 344 | - | fflush(stdout); |
|
| 345 | 346 | exit(EXIT_SUCCESS); |
|
| 346 | 347 | case XK_Right: |
|
| 347 | 348 | if(cursor < len) { |
|
| 403 | 404 | else |
|
| 404 | 405 | matches = lsubstr; |
|
| 405 | 406 | } |
|
| 406 | - | curr = prev = next = sel = matches; |
|
| 407 | + | curr = sel = matches; |
|
| 407 | 408 | calcoffsets(); |
|
| 408 | 409 | } |
|
| 409 | 410 | ||
| 438 | 439 | for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) { |
|
| 439 | 440 | if((p = strchr(buf, '\n'))) |
|
| 440 | 441 | *p = '\0'; |
|
| 441 | - | if(!(item = malloc(sizeof *item))) |
|
| 442 | + | if(!(item = calloc(1, sizeof *item))) |
|
| 442 | 443 | eprintf("cannot malloc %u bytes\n", sizeof *item); |
|
| 443 | 444 | if(!(item->text = strdup(buf))) |
|
| 444 | 445 | eprintf("cannot strdup %u bytes\n", strlen(buf)+1); |
|
| 445 | - | item->next = item->left = item->right = NULL; |
|
| 446 | 446 | inputw = MAX(inputw, textw(dc, item->text)); |
|
| 447 | 447 | } |
|
| 448 | 448 | } |
|
| 530 | 530 | inputw = MIN(inputw, mw/3); |
|
| 531 | 531 | promptw = prompt ? textw(dc, prompt) : 0; |
|
| 532 | 532 | XMapRaised(dc->dpy, win); |
|
| 533 | - | text[0] = '\0'; |
|
| 534 | 533 | match(); |
|
| 535 | 534 | } |
|
| 536 | - | ||
| 537 | - | void |
|
| 538 | - | usage(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 | - | } |
|
| 100 | 100 | ||
| 101 | 101 | if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) |
|
| 102 | 102 | weprintf("no locale support\n"); |
|
| 103 | - | if(!(dc = malloc(sizeof *dc))) |
|
| 103 | + | if(!(dc = calloc(1, sizeof *dc))) |
|
| 104 | 104 | eprintf("cannot malloc %u bytes\n", sizeof *dc); |
|
| 105 | 105 | if(!(dc->dpy = XOpenDisplay(NULL))) |
|
| 106 | 106 | eprintf("cannot open display\n"); |
|
| 107 | 107 | ||
| 108 | 108 | dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL); |
|
| 109 | 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 | 110 | return dc; |
|
| 114 | 111 | } |
|
| 115 | 112 | ||
| 187 | 184 | weprintf(const char *fmt, ...) { |
|
| 188 | 185 | va_list ap; |
|
| 189 | 186 | ||
| 190 | - | fprintf(stderr, "%s: warning: ", progname); |
|
| 187 | + | fprintf(stderr, "%s: ", progname); |
|
| 191 | 188 | va_start(ap, fmt); |
|
| 192 | 189 | vfprintf(stderr, fmt, ap); |
|
| 193 | 190 | va_end(ap); |
|