]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/parser.cc
better error reporting. epist now reports the line number and token a parser error...
[mikachu/openbox.git] / util / epist / parser.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // parser.cc for Epistrophy - a key handler for NETWM/EWMH window managers.
3 // Copyright (c) 2002 - 2002 Ben Jansens <ben at orodu.net>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22
23 #ifdef    HAVE_CONFIG_H
24 #  include "../../config.h"
25 #endif // HAVE_CONFIG_H
26
27 extern "C" {
28 #include <stdio.h>
29 #include <string.h>
30 }
31
32 #include "parser.hh"
33 #include <string>
34 #include <iostream>
35
36 using std::string;
37 using std::cout;
38
39 parser::parser(keytree *kt, Config *conf)
40   : _kt(kt), _config(conf), _mask(0), _action(Action::noaction),
41     _key(""), _arg(""), _add(true)
42 {
43 }
44
45 parser::~parser()
46 {
47   // nothing to see here. move along.
48 }
49
50 void parser::parse(string rc_file)
51 {
52   extern int yyparse(void *);
53   extern FILE *yyin;
54
55   yyin = fopen(rc_file.c_str(), "r");
56
57   yyparse(this);
58
59   fclose(yyin);
60   _kt->reset();
61   _kt->initialize();
62 }
63
64 void parser::setAction(string act)
65 {
66   struct {
67     const char* str;
68     Action::ActionType act;
69   }
70   actions[] = {
71     { "noaction", Action::noaction },
72     { "execute", Action::execute },
73     { "iconify", Action::iconify },
74     { "raise", Action::raise },
75     { "lower", Action::lower },
76     { "close", Action::close },
77     { "toggleShade", Action::toggleShade },
78     { "toggleOmnipresent", Action::toggleOmnipresent },
79     { "movewindowup", Action::moveWindowUp },
80     { "movewindowdown", Action::moveWindowDown },
81     { "movewindowleft", Action::moveWindowLeft },
82     { "movewindowright", Action::moveWindowRight },
83     { "resizewindowwidth", Action::resizeWindowWidth },
84     { "resizewindowheight", Action::resizeWindowHeight },
85     { "togglemaximizefull", Action::toggleMaximizeFull },
86     { "togglemaximizevertical", Action::toggleMaximizeVertical },
87     { "togglemaximizehorizontal", Action::toggleMaximizeHorizontal },
88     { "sendtoworkspace", Action::sendToWorkspace },
89     { "nextwindow", Action::nextWindow },
90     { "prevwindow", Action::prevWindow },
91     { "nextwindowonallworkspaces", Action::nextWindowOnAllWorkspaces },
92     { "prevwindowonallworkspaces", Action::prevWindowOnAllWorkspaces },
93     { "nextwindowonallscreens", Action::nextWindowOnAllScreens },
94     { "prevwindowonallscreens", Action::prevWindowOnAllScreens },
95     { "nextwindowofclass", Action::nextWindowOfClass },
96     { "prevwindowofclass", Action::prevWindowOfClass },
97     { "nextwindowofclassonallworkspaces", Action::nextWindowOfClassOnAllWorkspaces },
98     { "prevwindowofclassonallworkspaces", Action::prevWindowOfClassOnAllWorkspaces },
99     { "changeworkspace", Action::changeWorkspace },
100     { "nextworkspace", Action::nextWorkspace },
101     { "prevworkspace", Action::prevWorkspace },
102     { "nextworkspacerow", Action::upWorkspace },
103     { "prevworkspacerow", Action::downWorkspace },
104     { "prevworkspacecolumn", Action::leftWorkspace },
105     { "nextworkspacecolumn", Action::rightWorkspace },
106     { "nextscreen", Action::nextScreen },
107     { "prevscreen", Action::prevScreen },
108     { "showrootmenu", Action::showRootMenu },
109     { "showworkspacemenu", Action::showWorkspaceMenu },
110     { "toggledecorations", Action::toggleDecorations },
111     { "togglegrabs", Action::toggleGrabs },
112     { "stringchain", Action::stringChain },
113     { "keychain", Action::keyChain },
114     { "numberchain", Action::numberChain },
115     { "cancelchain", Action::cancelChain },
116     { "", Action::noaction }
117   };
118
119   bool found = false;
120
121   for (int i = 0; actions[i].str != ""; ++i) {
122     if ( strcasecmp(actions[i].str, act.c_str()) == 0 ) {
123       _action = actions[i].act;
124       found = true;
125       break;
126     }
127   }
128
129   if (!found) {
130     cout << "ERROR: Invalid action (" << act << "). Binding ignored.\n";
131     _add = false;
132   }
133 }
134
135 void parser::addModifier(string mod)
136 {
137   struct {
138     const char *str;
139     unsigned int mask;
140   }
141   modifiers[] = {
142     { "mod1", Mod1Mask },
143     { "mod2", Mod2Mask },
144     { "mod3", Mod3Mask },
145     { "mod4", Mod4Mask },
146     { "mod5", Mod5Mask },
147     { "control", ControlMask },
148     { "shift", ShiftMask },
149     { "", 0 }
150   };
151
152   bool found = false;
153
154   for (int i = 0; modifiers[i].str != ""; ++i) {
155     if ( strcasecmp(modifiers[i].str, mod.c_str()) == 0 ) {
156       _mask |= modifiers[i].mask;
157       found = true;
158       break;
159     }
160   }
161
162   if (!found) {
163     cout << "ERROR: Invalid modifier (" << mod << "). Binding ignored.\n";
164     _add = false;
165   }
166 }
167
168 void parser::endAction()
169 {
170   if (_add)
171     _kt->addAction(_action, _mask, _key, _arg);
172   reset();
173
174   _add = true;
175 }
176
177 void parser::startChain()
178 {
179   _kt->advanceOnNewNode();
180   setChainBinding();
181   reset();
182 }
183
184 void parser::endChain()
185 {
186   _kt->retract();
187   reset();
188 }
189
190 void parser::setChainBinding()
191 {
192   if (_mask != 0 && _key != "") {
193     if (!_add) {
194       cout << "Error: Bad modifier detected on chain's root key.\n";
195       _add = true;
196     }
197     _kt->setCurrentNodeProps(Action::noaction, _mask, _key, "");
198     reset();
199   }
200 }
201
202 void parser::reset()
203 {
204   _mask = 0;
205   _action = Action::noaction;
206   _key = "";
207   _arg = "";
208 }