better comment parsing
[dana/openbox.git] / plugins / mouse / mouserc_parse.l
1 %{
2 #include "mouse.h"
3 #include <glib.h>
4 #ifdef HAVE_STDIO_H
5 #  include <stdio.h>
6 #endif
7
8 static int lineno;
9 static char *path;
10 static gboolean error;
11
12 static char *context;
13 static char *event;
14 static char *button;
15 static char *action;
16
17 static void endofline();
18 static int mparsewrap();
19 static void gotfield();
20 static void addbinding();
21 %}
22
23 field [A-Za-z0-9][-A-Za-z0-9]*
24 white [ \t]*
25
26 %%
27
28 ^{white}\#.*\n lineno++;
29 {field} gotfield();
30 \n endofline();
31 [ \t]
32 . error = TRUE;
33
34 %%
35
36 static void gotfield()
37 {
38     if (context == NULL)
39         context = g_strdup(mparsetext);
40     else if (event == NULL)
41         event = g_strdup(mparsetext);
42     else if (button == NULL)
43         button = g_strdup(mparsetext);
44     else if (action == NULL)
45         action = g_strdup(mparsetext);
46     else
47         error = TRUE;
48 }
49
50 static void endofline()
51 {
52     if (!error && context && event && button && action)
53         addbinding();
54     else if (error || context || event || button || action)
55         g_warning("Parser error in '%s' on line %d", path, lineno);
56
57     error = FALSE;
58     g_free(context); g_free(event); g_free(button); g_free(action);
59     context = event = button = action = NULL;
60
61     ++lineno;
62 }
63
64 static void addbinding()
65 {
66     Action *a;
67     MouseAction mact;
68
69     if (!g_ascii_strcasecmp(event, "press"))
70         mact = MouseAction_Press;
71     else if (!g_ascii_strcasecmp(event, "release"))
72         mact = MouseAction_Release;
73     else if (!g_ascii_strcasecmp(event, "click"))
74         mact = MouseAction_Click;
75     else if (!g_ascii_strcasecmp(event, "doubleclick"))
76         mact = MouseAction_DClick;
77     else if (!g_ascii_strcasecmp(event, "drag"))
78         mact = MouseAction_Motion;
79     else {
80         g_warning("Invalid event '%s' in '%s' on line %d", event, path,
81                   lineno);
82         return;
83     }
84
85     if (!g_ascii_strcasecmp(action, "focus")) {
86         a = action_new(action_focus);
87     } else if (!g_ascii_strcasecmp(action, "unfocus")) {
88         a = action_new(action_unfocus);
89     } else if (!g_ascii_strcasecmp(action, "iconify")) {
90         a = action_new(action_iconify);
91     } else if (!g_ascii_strcasecmp(action, "raise")) {
92         a = action_new(action_raise);
93     } else if (!g_ascii_strcasecmp(action, "lower")) {
94         a = action_new(action_lower);
95     } else if (!g_ascii_strcasecmp(action, "focusraise")) {
96         a = action_new(action_focusraise);
97     } else if (!g_ascii_strcasecmp(action, "close")) {
98         a = action_new(action_close);
99     } else if (!g_ascii_strcasecmp(action, "kill")) {
100         a = action_new(action_kill);
101     } else if (!g_ascii_strcasecmp(action, "shade")) {
102         a = action_new(action_shade);
103     } else if (!g_ascii_strcasecmp(action, "unshade")) {
104         a = action_new(action_unshade);
105     } else if (!g_ascii_strcasecmp(action, "toggleshade")) {
106         a = action_new(action_toggle_shade);
107     } else if (!g_ascii_strcasecmp(action, "toggleomnipresent")) {
108         a = action_new(action_toggle_omnipresent);
109     } else if (!g_ascii_strcasecmp(action, "maximizefull")) {
110         a = action_new(action_maximize_full);
111     } else if (!g_ascii_strcasecmp(action, "unmaximizefull")) {
112         a = action_new(action_unmaximize_full);
113     } else if (!g_ascii_strcasecmp(action, "togglemaximizefull")) {
114         a = action_new(action_toggle_maximize_full);
115     } else if (!g_ascii_strcasecmp(action, "maximizehorz")) {
116         a = action_new(action_maximize_horz);
117     } else if (!g_ascii_strcasecmp(action, "unmaximizehorz")) {
118         a = action_new(action_unmaximize_horz);
119     } else if (!g_ascii_strcasecmp(action, "togglemaximizehorz")) {
120         a = action_new(action_toggle_maximize_horz);
121     } else if (!g_ascii_strcasecmp(action, "maximizevert")) {
122         a = action_new(action_maximize_vert);
123     } else if (!g_ascii_strcasecmp(action, "unmaximizevert")) {
124         a = action_new(action_unmaximize_vert);
125     } else if (!g_ascii_strcasecmp(action, "togglemaximizevert")) {
126         a = action_new(action_toggle_maximize_vert);
127     } else if (!g_ascii_strcasecmp(action, "sendtonextdesktop")) {
128         a = action_new(action_send_to_next_desktop);
129         a->data.sendtonextprev.wrap = FALSE;
130         a->data.sendtonextprev.follow = TRUE;
131     } else if (!g_ascii_strcasecmp(action, "sendtonextdesktopwrap")) {
132         a = action_new(action_send_to_next_desktop);
133         a->data.sendtonextprev.wrap = TRUE;
134         a->data.sendtonextprev.follow = TRUE;
135     } else if (!g_ascii_strcasecmp(action, "sendtopreviousdesktop")) {
136         a = action_new(action_send_to_previous_desktop);
137         a->data.sendtonextprev.wrap = FALSE;
138         a->data.sendtonextprev.follow = TRUE;
139     } else if (!g_ascii_strcasecmp(action, "sendtopreviousdesktopwrap")) {
140         a = action_new(action_send_to_previous_desktop);
141         a->data.sendtonextprev.wrap = TRUE;
142         a->data.sendtonextprev.follow = TRUE;
143     } else if (!g_ascii_strcasecmp(action, "nextdesktop")) {
144         a = action_new(action_next_desktop);
145         a->data.nextprevdesktop.wrap = FALSE;
146     } else if (!g_ascii_strcasecmp(action, "nextdesktopwrap")) {
147         a = action_new(action_next_desktop);
148         a->data.nextprevdesktop.wrap = TRUE;
149     } else if (!g_ascii_strcasecmp(action, "previousdesktop")) {
150         a = action_new(action_previous_desktop);
151         a->data.nextprevdesktop.wrap = FALSE;
152     } else if (!g_ascii_strcasecmp(action, "previousdesktopwrap")) {
153         a = action_new(action_previous_desktop);
154         a->data.nextprevdesktop.wrap = TRUE;
155     } else if (!g_ascii_strcasecmp(action, "nextdesktopcolumn")) {
156         a = action_new(action_next_desktop_column);
157         a->data.nextprevdesktop.wrap = FALSE;
158     } else if (!g_ascii_strcasecmp(action, "nextdesktopcolumnwrap")) {
159         a = action_new(action_next_desktop_column);
160         a->data.nextprevdesktop.wrap = TRUE;
161     } else if (!g_ascii_strcasecmp(action, "previousdesktopcolumn")) {
162         a = action_new(action_previous_desktop_column);
163         a->data.nextprevdesktop.wrap = FALSE;
164     } else if (!g_ascii_strcasecmp(action, "previousdesktopcolumnwrap")) {
165         a = action_new(action_previous_desktop_column);
166         a->data.nextprevdesktop.wrap = TRUE;
167     } else if (!g_ascii_strcasecmp(action, "nextdesktoprow")) {
168         a = action_new(action_next_desktop_row);
169         a->data.nextprevdesktop.wrap = FALSE;
170     } else if (!g_ascii_strcasecmp(action, "nextdesktoprowwrap")) {
171         a = action_new(action_next_desktop_row);
172         a->data.nextprevdesktop.wrap = TRUE;
173     } else if (!g_ascii_strcasecmp(action, "previousdesktoprow")) {
174         a = action_new(action_previous_desktop_row);
175         a->data.nextprevdesktop.wrap = FALSE;
176     } else if (!g_ascii_strcasecmp(action, "previousdesktoprowwrap")) {
177         a = action_new(action_previous_desktop_row);
178         a->data.nextprevdesktop.wrap = TRUE;
179     } else if (!g_ascii_strcasecmp(action, "move") &&
180                mact == MouseAction_Motion) {
181         a = action_new(action_move);
182     } else if (!g_ascii_strcasecmp(action, "resize") &&
183                mact == MouseAction_Motion) {
184         a = action_new(action_resize);
185     } else {
186         g_warning("Invalid action '%s' in '%s' on line %d", action, path,
187                   lineno);
188         return;
189     }
190
191     if (!mbind(button, context, mact, a)) {
192         action_free(a);
193         g_warning("Unable to add binding '%s %s %s %s'",
194                   context, event, button, action);
195     }
196 }
197
198
199 static int mparsewrap()
200 {
201     g_free(context); g_free(event); g_free(button); g_free(action);
202     return 1;
203 }
204
205 void mouserc_parse()
206 {
207     path = g_build_filename(g_get_home_dir(), ".openbox", "mouserc", NULL);
208     if ((mparsein = fopen(path, "r")) == NULL) {
209         g_free(path);
210         path = g_build_filename(RCDIR, "mouserc", NULL);
211         if ((mparsein = fopen(path, "r")) == NULL) {
212             g_warning("No mouserc file found!");
213             g_free(path);
214             return;
215         }
216     }
217
218     lineno = 1;
219     error = FALSE;
220     context = event = button = action = NULL;
221
222     mparselex();
223
224     g_free(path);
225 }