Simplifying parsing and data structure building
[xmenu] / xmenu.c
diff --git a/xmenu.c b/xmenu.c
index 890f590..5fefae8 100644 (file)
--- a/xmenu.c
+++ b/xmenu.c
@@ -28,7 +28,6 @@ struct DC {
        XftColor border;
        XftColor separator;
 
        XftColor border;
        XftColor separator;
 
-       Drawable d;
        GC gc;
        XftFont *font;
 };
        GC gc;
        XftFont *font;
 };
@@ -74,6 +73,7 @@ static void setupdc(void);
 static void calcgeom(void);
 static struct Item *allocitem(const char *label, const char *output);
 static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
 static void calcgeom(void);
 static struct Item *allocitem(const char *label, const char *output);
 static struct Menu *allocmenu(struct Menu *parent, struct Item *list, unsigned level);
+static struct Menu * buildmenutree(unsigned level, const char *label, const char *output);
 static struct Menu *parsestdin(void);
 static void calcmenu(struct Menu *menu);
 static void grabpointer(void);
 static struct Menu *parsestdin(void);
 static void calcmenu(struct Menu *menu);
 static void grabpointer(void);
@@ -87,7 +87,7 @@ static void drawmenu(struct Menu *currmenu);
 static struct Item *itemcycle(struct Menu *currmenu, int direction);
 static void run(struct Menu *currmenu);
 static void freemenu(struct Menu *menu);
 static struct Item *itemcycle(struct Menu *currmenu, int direction);
 static void run(struct Menu *currmenu);
 static void freemenu(struct Menu *menu);
-static void cleanup(struct Menu *rootmenu);
+static void cleanup(void);
 static void usage(void);
 
 /* global variables (X stuff and geometries) */
 static void usage(void);
 
 /* global variables (X stuff and geometries) */
@@ -146,7 +146,10 @@ main(int argc, char *argv[])
        /* run event loop */
        run(rootmenu);
 
        /* run event loop */
        run(rootmenu);
 
-       cleanup(rootmenu);
+       /* freeing stuff */
+       freemenu(rootmenu);
+       cleanup();
+
        return 0;
 }
 
        return 0;
 }
 
@@ -249,14 +252,18 @@ allocitem(const char *label, const char *output)
 
        if ((item = malloc(sizeof *item)) == NULL)
                err(1, "malloc");
 
        if ((item = malloc(sizeof *item)) == NULL)
                err(1, "malloc");
-       if (*label == '\0') {
+       if (label == NULL) {
                item->label = NULL;
                item->output = NULL;
        } else {
                if ((item->label = strdup(label)) == NULL)
                        err(1, "strdup");
                item->label = NULL;
                item->output = NULL;
        } else {
                if ((item->label = strdup(label)) == NULL)
                        err(1, "strdup");
-               if ((item->output = strdup(output)) == NULL)
-                       err(1, "strdup");
+               if (label == output) {
+                       item->output = item->label;
+               } else {
+                       if ((item->output = strdup(output)) == NULL)
+                               err(1, "strdup");
+               }
        }
        item->y = 0;
        item->h = item->label ? geom.itemh : geom.separator;
        }
        item->y = 0;
        item->h = item->label ? geom.itemh : geom.separator;
@@ -304,96 +311,92 @@ allocmenu(struct Menu *parent, struct Item *list, unsigned level)
        return menu;
 }
 
        return menu;
 }
 
-/* create menus and items from the stdin */
+/* build the menu tree */
 static struct Menu *
 static struct Menu *
-parsestdin(void)
+buildmenutree(unsigned level, const char *label, const char *output)
 {
 {
-       char *s, buf[BUFSIZ];
-       char *label, *output;
-       unsigned level = 0;
+       static struct Menu *prevmenu = NULL;    /* menu the previous item was added to */
+       static struct Menu *rootmenu = NULL;    /* menu to be returned */
+       struct Item *curritem = NULL;           /* item currently being read */
+       struct Item *item;                      /* dummy item for loops */
+       struct Menu *menu;                      /* dummy menu for loops */
        unsigned i;
        unsigned i;
-       struct Item *curritem = NULL;   /* item currently being read */
-       struct Menu *prevmenu = NULL;   /* menu the previous item was added to */
-       struct Item *item;              /* dummy item for loops */
-       struct Menu *menu;              /* dummy menu for loops */
-       struct Menu *rootmenu;          /* menu to be returned */
-
-       rootmenu = NULL;
-
-       while (fgets(buf, BUFSIZ, stdin) != NULL) {
-               level = 0;
-               s = buf;
-
-               while (*s == '\t') {
-                       level++;
-                       s++;
-               }
-
-               label = output = s;
-
-               while (*s != '\0' && *s != '\t' && *s != '\n')
-                       s++;
-
-               while (*s == '\t')
-                       *s++ = '\0';
-
-               if (*s != '\0' && *s != '\n')
-                       output = s;
-
-               while (*s != '\0' && *s != '\n')
-                       s++;
-
-               if (*s == '\n')
-                       *s = '\0';
-
-               curritem = allocitem(label, output);
-
-               if (prevmenu == NULL) {                 /* there is no menu yet */
-                        menu = allocmenu(NULL, curritem, level);
-                        rootmenu = menu;
-                        prevmenu = menu;
-                        curritem->prev = NULL;
-                        curritem->next = NULL;
-               } else if (level < prevmenu->level) {   /* item is continuation of a parent menu*/
-                       for (menu = prevmenu, i = level;
-                             menu != NULL && i < prevmenu->level;
-                             menu = menu->parent, i++)
-                               ;
 
 
-                       if (menu == NULL)
-                               errx(1, "reached NULL menu");
-
-                       for (item = menu->list; item->next != NULL; item = item->next)
-                               ;
-
-                       item->next = curritem;
+       /* create the item */
+       curritem = allocitem(label, output);
+
+       /* put the item in the menu tree */
+       if (prevmenu == NULL) {                 /* there is no menu yet */
+                menu = allocmenu(NULL, curritem, level);
+                rootmenu = menu;
+                prevmenu = menu;
+                curritem->prev = NULL;
+       } else if (level < prevmenu->level) {   /* item is continuation of a parent menu */
+               /* go up the menu tree until find the menu this item continues */
+               for (menu = prevmenu, i = level;
+                         menu != NULL && i != prevmenu->level;
+                         menu = menu->parent, i++)
+                       ;
+               if (menu == NULL)
+                       errx(1, "reached NULL menu");
 
 
-                       curritem->prev = item;
-                       curritem->next = NULL;
+               /* find last item in the new menu */
+               for (item = menu->list; item->next != NULL; item = item->next)
+                       ;
 
 
-                       prevmenu = menu;
-               } else if (level == prevmenu->level) {  /* item is a continuation of current menu */
-                       for (item = prevmenu->list; item->next != NULL; item = item->next)
-                               ;
-                       item->next = curritem;
+               prevmenu = menu;
+               item->next = curritem;
+               curritem->prev = item;
+       } else if (level == prevmenu->level) {  /* item is a continuation of current menu */
+               /* find last item in the previous menu */
+               for (item = prevmenu->list; item->next != NULL; item = item->next)
+                       ;
 
 
-                       curritem->prev = item;
-                       curritem->next = NULL;
+               item->next = curritem;
+               curritem->prev = item;
+       } else if (level > prevmenu->level) {   /* item begins a new menu */
+               menu = allocmenu(prevmenu, curritem, level);
 
 
-               } else if (level > prevmenu->level) {   /* item begins a new menu */
-                       menu = allocmenu(prevmenu, curritem, level);
+               /* find last item in the previous menu */
+               for (item = prevmenu->list; item->next != NULL; item = item->next)
+                       ;
 
 
-                       for (item = prevmenu->list; item->next != NULL; item = item->next)
-                               ;
+               prevmenu = menu;
+               menu->caller = item;
+               item->submenu = menu;
+               curritem->prev = NULL;
+       }
 
 
-                       item->submenu = menu;
-                       menu->caller = item;
+       return rootmenu;
+}
 
 
-                       curritem->prev = NULL;
-                       curritem->next = NULL;
+/* create menus and items from the stdin */
+static struct Menu *
+parsestdin(void)
+{
+       struct Menu *rootmenu;
+       char *s, buf[BUFSIZ];
+       char *label, *output;
+       unsigned level = 0;
 
 
-                       prevmenu = menu;
+       while (fgets(buf, BUFSIZ, stdin) != NULL) {
+               /* get the indentation level */
+               level = strspn(buf, "\t");
+
+               /* get the label */
+               s = level + buf;
+               label = strtok(s, "\t\n");
+
+               /* get the output */
+               output = strtok(NULL, "\n");
+               if (output == NULL) {
+                       output = label;
+               } else {
+                       while (*output == '\t')
+                               output++;
                }
                }
+
+               rootmenu = buildmenutree(level, label, output);
        }
 
        return rootmenu;
        }
 
        return rootmenu;
@@ -598,14 +601,12 @@ mapmenu(struct Menu *currmenu)
 static void
 drawseparator(struct Menu *menu, struct Item *item)
 {
 static void
 drawseparator(struct Menu *menu, struct Item *item)
 {
-       int linex, liney, linew;
+       int y;
 
 
-       linex = dc.font->height;
-       liney = item->y + item->h/2;
-       linew = menu->w - dc.font->height;
+       y = item->y + item->h/2;
 
        XSetForeground(dpy, dc.gc, dc.separator.pixel);
 
        XSetForeground(dpy, dc.gc, dc.separator.pixel);
-       XDrawLine(dpy, menu->pixmap, dc.gc, linex, liney, linew, liney);
+       XDrawLine(dpy, menu->pixmap, dc.gc, 0, y, menu->w, y);
 }
 
 /* draw regular item */
 }
 
 /* draw regular item */
@@ -821,6 +822,9 @@ freemenu(struct Menu *menu)
                        freemenu(item->submenu);
                tmp = item;
                item = item->next;
                        freemenu(item->submenu);
                tmp = item;
                item = item->next;
+               if (tmp->label != tmp->output)
+                       free(tmp->label);
+               free(tmp->output);
                free(tmp);
        }
 
                free(tmp);
        }
 
@@ -832,13 +836,11 @@ freemenu(struct Menu *menu)
 
 /* cleanup and exit */
 static void
 
 /* cleanup and exit */
 static void
-cleanup(struct Menu *rootmenu)
+cleanup(void)
 {
        XUngrabPointer(dpy, CurrentTime);
        XUngrabKeyboard(dpy, CurrentTime);
 
 {
        XUngrabPointer(dpy, CurrentTime);
        XUngrabKeyboard(dpy, CurrentTime);
 
-       freemenu(rootmenu);
-
        XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
        XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
        XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);
        XftColorFree(dpy, visual, colormap, &dc.normal[ColorBG]);
        XftColorFree(dpy, visual, colormap, &dc.normal[ColorFG]);
        XftColorFree(dpy, visual, colormap, &dc.selected[ColorBG]);