-/* forward declarations */
-void appenditem(Item *i, Item **list, Item **last);
-void calcoffsets(void);
-char *cistrstr(const char *s, const char *sub);
-void cleanup(void);
-void drawmenu(void);
-void drawtext(const char *text, unsigned long col[ColLast]);
-void *emalloc(unsigned int size);
-void eprint(const char *errstr, ...);
-char *estrdup(const char *str);
-unsigned long getcolor(const char *colstr);
-Bool grabkeyboard(void);
-void initfont(const char *fontstr);
-void kpress(XKeyEvent * e);
-void match(char *pattern);
-void readstdin(void);
-void run(void);
-void setup(Bool topbar);
-unsigned int textnw(const char *text, unsigned int len);
-unsigned int textw(const char *text);
-
-#include "config.h"
-
-/* variables */
-char *font = FONT;
-char *maxname = NULL;
-char *normbg = NORMBGCOLOR;
-char *normfg = NORMFGCOLOR;
-char *prompt = NULL;
-char *selbg = SELBGCOLOR;
-char *selfg = SELFGCOLOR;
-char text[4096];
-int screen;
-int ret = 0;
-unsigned int cmdw = 0;
-unsigned int mw, mh;
-unsigned int promptw = 0;
-unsigned int numlockmask = 0;
-Bool running = True;
-Display *dpy;
-DC dc = {0};
-Item *allitems = NULL; /* first of all items */
-Item *item = NULL; /* first of pattern matching items */
-Item *sel = NULL;
-Item *next = NULL;
-Item *prev = NULL;
-Item *curr = NULL;
-Window root, win;
-int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
-char *(*fstrstr)(const char *, const char *) = strstr;
+static void appenditem(Item *item, Item **list, Item **last);
+static void calcoffsets(void);
+static char *cistrstr(const char *s, const char *sub);
+static void drawmenu(void);
+static void grabkeyboard(void);
+static void insert(const char *str, ssize_t n);
+static void keypress(XKeyEvent *ev);
+static void match(void);
+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, promptw;
+static size_t cursor = 0;
+static const char *font = NULL;
+static const char *prompt = NULL;
+static const char *normbgcolor = "#cccccc";
+static const char *normfgcolor = "#000000";
+static const char *selbgcolor = "#0066ff";
+static const char *selfgcolor = "#ffffff";
+static unsigned int lines = 0;
+static unsigned long normcol[ColLast];
+static unsigned long selcol[ColLast];
+static Atom utf8;
+static Bool topbar = True;
+static DC *dc;
+static Item *items = NULL;
+static Item *matches, *matchend;
+static Item *prev, *curr, *next, *sel;
+static Window win;
+static XIC xic;
+
+static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
+static char *(*fstrstr)(const char *, const char *) = strstr;
+
+int
+main(int argc, char *argv[]) {
+ Bool fast = False;
+ int i;
+
+ for(i = 1; i < argc; i++)
+ /* these options take no arguments */
+ if(!strcmp(argv[i], "-v")) { /* prints version information */
+ puts("dmenu-"VERSION", © 2006-2011 dmenu engineers, see LICENSE for details");
+ exit(EXIT_SUCCESS);
+ }
+ else if(!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */
+ topbar = False;
+ else if(!strcmp(argv[i], "-f")) /* grabs keyboard before reading stdin */
+ fast = True;
+ else if(!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
+ fstrncmp = strncasecmp;
+ fstrstr = cistrstr;
+ }
+ else if(i+1 == argc)
+ usage();
+ /* these options take one argument */
+ else if(!strcmp(argv[i], "-l")) /* number of lines in vertical list */
+ lines = atoi(argv[++i]);
+ else if(!strcmp(argv[i], "-p")) /* adds prompt to left of input field */
+ prompt = argv[++i];
+ else if(!strcmp(argv[i], "-fn")) /* font or font set */
+ font = argv[++i];
+ else if(!strcmp(argv[i], "-nb")) /* normal background color */
+ normbgcolor = argv[++i];
+ else if(!strcmp(argv[i], "-nf")) /* normal foreground color */
+ normfgcolor = argv[++i];
+ else if(!strcmp(argv[i], "-sb")) /* selected background color */
+ selbgcolor = argv[++i];
+ else if(!strcmp(argv[i], "-sf")) /* selected foreground color */
+ selfgcolor = argv[++i];
+ else
+ usage();
+
+ dc = initdc();
+ initfont(dc, font);
+
+ if(fast) {
+ grabkeyboard();
+ readstdin();
+ }
+ else {
+ readstdin();
+ grabkeyboard();
+ }
+ setup();
+ run();
+
+ return EXIT_FAILURE; /* unreachable */
+}