X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=dmenu.c;h=da6270583fa31765817228279ad7aea3e65ae71e;hb=bb3e33def3e5156594ac59b75dc52798accbebcd;hp=cee73b25254c944eb4e3f8d714290c8bad2268e9;hpb=11ea52d1709423e0f4e6702aaee2dff2a3b0107e;p=dmenu.git diff --git a/dmenu.c b/dmenu.c index cee73b2..da62705 100644 --- a/dmenu.c +++ b/dmenu.c @@ -31,18 +31,17 @@ static void grabkeyboard(void); static void insert(const char *str, ssize_t n); static void keypress(XKeyEvent *ev); static void match(Bool sub); -static size_t nextrune(int incr); +static size_t nextrune(int inc); static void paste(void); static void readstdin(void); static void run(void); static void setup(void); +static void usage(void); static char text[BUFSIZ] = ""; static int bh, mw, mh; -static int inputw; +static int inputw, promptw; static int lines = 0; -static int monitor = -1; -static int promptw; static size_t cursor = 0; static const char *font = NULL; static const char *prompt = NULL; @@ -70,7 +69,7 @@ main(int argc, char *argv[]) { for(i = 1; i < argc; i++) /* single flags */ if(!strcmp(argv[i], "-v")) { - fputs("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details\n", stdout); + puts("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details"); exit(EXIT_SUCCESS); } else if(!strcmp(argv[i], "-b")) @@ -80,12 +79,10 @@ main(int argc, char *argv[]) { else if(!strcmp(argv[i], "-i")) fstrncmp = strncasecmp; else if(i+1 == argc) - goto usage; + usage(); /* double flags */ else if(!strcmp(argv[i], "-l")) lines = atoi(argv[++i]); - else if(!strcmp(argv[i], "-m")) - monitor = atoi(argv[++i]); else if(!strcmp(argv[i], "-p")) prompt = argv[++i]; else if(!strcmp(argv[i], "-fn")) @@ -99,7 +96,7 @@ main(int argc, char *argv[]) { else if(!strcmp(argv[i], "-sf")) selfgcolor = argv[++i]; else - goto usage; + usage(); dc = initdc(); initfont(dc, font); @@ -114,12 +111,8 @@ main(int argc, char *argv[]) { } setup(); run(); - return EXIT_FAILURE; -usage: - fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-m monitor] [-p prompt] [-fn font]\n" - " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr); - return EXIT_FAILURE; + return EXIT_FAILURE; /* should not reach */ } void @@ -135,7 +128,7 @@ appenditem(Item *item, Item **list, Item **last) { void calcoffsets(void) { - unsigned int i, n; + int i, n; if(lines > 0) n = lines * bh; @@ -388,11 +381,11 @@ void match(Bool sub) { size_t len = strlen(text); Item *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend; - Item *item, *next; + Item *item, *lnext; lexact = lprefix = lsubstr = exactend = prefixend = substrend = NULL; - for(item = sub ? matches : items; item && item->text; item = next) { - next = sub ? item->right : item + 1; + for(item = sub ? matches : items; item && item->text; item = lnext) { + lnext = sub ? item->right : item + 1; if(!fstrncmp(text, item->text, len + 1)) appenditem(item, &lexact, &exactend); else if(!fstrncmp(text, item->text, len)) @@ -426,10 +419,10 @@ match(Bool sub) { } size_t -nextrune(int incr) { - size_t n, len = strlen(text); +nextrune(int inc) { + ssize_t n; - for(n = cursor + incr; n >= 0 && n < len && (text[n] & 0xc0) == 0x80; n += incr); + for(n = cursor + inc; n + inc >= 0 && (text[n] & 0xc0) == 0x80; n += inc); return n; } @@ -442,7 +435,7 @@ paste(void) { XGetWindowProperty(dc->dpy, win, utf8, 0, (sizeof text / 4) + 1, False, utf8, &da, &di, &dl, &dl, (unsigned char **)&p); - insert(p, (q = strchr(p, '\n')) ? q-p : strlen(p)); + insert(p, (q = strchr(p, '\n')) ? q-p : (ssize_t)strlen(p)); XFree(p); drawmenu(); } @@ -452,7 +445,7 @@ readstdin(void) { char buf[sizeof text], *p, *maxstr = NULL; size_t i, max = 0, size = 0; - for(i = 0; fgets(buf, sizeof buf, stdin); items[++i].text = NULL) { + for(i = 0; fgets(buf, sizeof buf, stdin); i++) { if(i+1 >= size / sizeof *items) if(!(items = realloc(items, (size += BUFSIZ)))) eprintf("cannot realloc %u bytes:", size); @@ -463,6 +456,8 @@ readstdin(void) { if(strlen(items[i].text) > max) max = strlen(maxstr = items[i].text); } + if(items) + items[i].text = NULL; inputw = maxstr ? textw(dc, maxstr) : 0; } @@ -519,8 +514,7 @@ setup(void) { XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du); for(i = 0; i < n-1; i++) - if((monitor == info[i].screen_number) - || (monitor < 0 && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))) + if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)) break; x = info[i].x_org; y = info[i].y_org + (topbar ? 0 : info[i].height - mh); @@ -534,8 +528,8 @@ setup(void) { y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - mh; mw = DisplayWidth(dc->dpy, screen); } - inputw = MIN(inputw, mw/3); promptw = prompt ? textw(dc, prompt) : 0; + inputw = MIN(inputw, mw/3); match(False); /* menu window */ @@ -551,3 +545,10 @@ setup(void) { resizedc(dc, mw, mh); drawmenu(); } + +void +usage(void) { + fputs("usage: dmenu [-b] [-f] [-i] [-l lines] [-p prompt] [-fn font]\n" + " [-nb color] [-nf color] [-sb color] [-sf color] [-v]\n", stderr); + exit(EXIT_FAILURE); +}