16aeeca8eSWarner Losh /*- 26aeeca8eSWarner Losh * Copyright (c) 2002-2003 M. Warner Losh. 36aeeca8eSWarner Losh * All rights reserved. 46aeeca8eSWarner Losh * 56aeeca8eSWarner Losh * Redistribution and use in source and binary forms, with or without 66aeeca8eSWarner Losh * modification, are permitted provided that the following conditions 76aeeca8eSWarner Losh * are met: 86aeeca8eSWarner Losh * 1. Redistributions of source code must retain the above copyright 96aeeca8eSWarner Losh * notice, this list of conditions and the following disclaimer. 106aeeca8eSWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 116aeeca8eSWarner Losh * notice, this list of conditions and the following disclaimer in the 126aeeca8eSWarner Losh * documentation and/or other materials provided with the distribution. 136aeeca8eSWarner Losh * 146aeeca8eSWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 156aeeca8eSWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 166aeeca8eSWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 176aeeca8eSWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 186aeeca8eSWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 196aeeca8eSWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 206aeeca8eSWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 216aeeca8eSWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 226aeeca8eSWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 236aeeca8eSWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 246aeeca8eSWarner Losh * SUCH DAMAGE. 256aeeca8eSWarner Losh * 266aeeca8eSWarner Losh * $FreeBSD$ 276aeeca8eSWarner Losh */ 286aeeca8eSWarner Losh 296aeeca8eSWarner Losh #ifndef DEVD_HH 306aeeca8eSWarner Losh #define DEVD_HH 316aeeca8eSWarner Losh 326aeeca8eSWarner Losh class config; 336aeeca8eSWarner Losh 346aeeca8eSWarner Losh /** 356aeeca8eSWarner Losh * var_list is a collection of variables. These collections of variables 366aeeca8eSWarner Losh * are stacked up and popped down for each event that we have to process. 376aeeca8eSWarner Losh * We have multiple levels so that we can push variables that are unique 386aeeca8eSWarner Losh * to the event in question, in addition to having global variables. This 396aeeca8eSWarner Losh * allows for future flexibility. 406aeeca8eSWarner Losh */ 416aeeca8eSWarner Losh class var_list 426aeeca8eSWarner Losh { 436aeeca8eSWarner Losh public: 446aeeca8eSWarner Losh var_list() {} 456aeeca8eSWarner Losh virtual ~var_list() {} 466aeeca8eSWarner Losh /** Set a variable in this var list. 476aeeca8eSWarner Losh */ 486aeeca8eSWarner Losh void set_variable(const std::string &var, const std::string &val); 496aeeca8eSWarner Losh /** Get the variable out of this, and no other, var_list. If 506aeeca8eSWarner Losh * no variable of %var is set, then %bogus will be returned. 516aeeca8eSWarner Losh */ 526aeeca8eSWarner Losh const std::string &get_variable(const std::string &var) const; 536aeeca8eSWarner Losh /** Is there a variable of %var set in thi stable? 546aeeca8eSWarner Losh */ 556aeeca8eSWarner Losh bool is_set(const std::string &var) const; 566aeeca8eSWarner Losh /** A completely bogus string. 576aeeca8eSWarner Losh */ 586aeeca8eSWarner Losh static const std::string bogus; 596aeeca8eSWarner Losh static const std::string nothing; 606aeeca8eSWarner Losh private: 616aeeca8eSWarner Losh std::map<std::string, std::string> _vars; 626aeeca8eSWarner Losh }; 636aeeca8eSWarner Losh 646aeeca8eSWarner Losh /** 656aeeca8eSWarner Losh * eps is short for event_proc_single. It is a single entry in an 666aeeca8eSWarner Losh * event_proc. Each keyword needs its own subclass from eps. 676aeeca8eSWarner Losh */ 68b884d5e8SDimitry Andric struct eps 696aeeca8eSWarner Losh { 706aeeca8eSWarner Losh public: 716aeeca8eSWarner Losh eps() {} 726aeeca8eSWarner Losh virtual ~eps() {} 736aeeca8eSWarner Losh /** Does this eps match the current config? 746aeeca8eSWarner Losh */ 756aeeca8eSWarner Losh virtual bool do_match(config &) = 0; 766aeeca8eSWarner Losh /** Perform some action for this eps. 776aeeca8eSWarner Losh */ 786aeeca8eSWarner Losh virtual bool do_action(config &) = 0; 796aeeca8eSWarner Losh }; 806aeeca8eSWarner Losh 816aeeca8eSWarner Losh /** 826aeeca8eSWarner Losh * match is the subclass used to match an individual variable. Its 836aeeca8eSWarner Losh * actions are nops. 846aeeca8eSWarner Losh */ 856aeeca8eSWarner Losh class match : public eps 866aeeca8eSWarner Losh { 876aeeca8eSWarner Losh public: 886aeeca8eSWarner Losh match(config &, const char *var, const char *re); 896aeeca8eSWarner Losh virtual ~match(); 906aeeca8eSWarner Losh virtual bool do_match(config &); 916aeeca8eSWarner Losh virtual bool do_action(config &) { return true; } 926aeeca8eSWarner Losh private: 93*5dfc0f6cSIan Lepore bool _inv; 946aeeca8eSWarner Losh std::string _var; 956aeeca8eSWarner Losh std::string _re; 966aeeca8eSWarner Losh regex_t _regex; 976aeeca8eSWarner Losh }; 986aeeca8eSWarner Losh 996aeeca8eSWarner Losh /** 100cd70782bSWarner Losh * media is the subclass used to match an individual variable. Its 101cd70782bSWarner Losh * actions are nops. 102cd70782bSWarner Losh */ 103cd70782bSWarner Losh class media : public eps 104cd70782bSWarner Losh { 105cd70782bSWarner Losh public: 106cd70782bSWarner Losh media(config &, const char *var, const char *type); 107cd70782bSWarner Losh virtual ~media(); 108cd70782bSWarner Losh virtual bool do_match(config &); 109cd70782bSWarner Losh virtual bool do_action(config &) { return true; } 110cd70782bSWarner Losh private: 111cd70782bSWarner Losh std::string _var; 112cd70782bSWarner Losh int _type; 113cd70782bSWarner Losh }; 114cd70782bSWarner Losh 115cd70782bSWarner Losh /** 1166aeeca8eSWarner Losh * action is used to fork a process. It matches everything. 1176aeeca8eSWarner Losh */ 1186aeeca8eSWarner Losh class action : public eps 1196aeeca8eSWarner Losh { 1206aeeca8eSWarner Losh public: 1216aeeca8eSWarner Losh action(const char *cmd); 1226aeeca8eSWarner Losh virtual ~action(); 1236aeeca8eSWarner Losh virtual bool do_match(config &) { return true; } 1246aeeca8eSWarner Losh virtual bool do_action(config &); 1256aeeca8eSWarner Losh private: 1266aeeca8eSWarner Losh std::string _cmd; 1276aeeca8eSWarner Losh }; 1286aeeca8eSWarner Losh 129b884d5e8SDimitry Andric struct event_proc 1306aeeca8eSWarner Losh { 1316aeeca8eSWarner Losh public: 1326aeeca8eSWarner Losh event_proc(); 1336aeeca8eSWarner Losh virtual ~event_proc(); 1346aeeca8eSWarner Losh int get_priority() const { return (_prio); } 1356aeeca8eSWarner Losh void set_priority(int prio) { _prio = prio; } 1366aeeca8eSWarner Losh void add(eps *); 137ef370346SEitan Adler bool matches(config &) const; 138ef370346SEitan Adler bool run(config &) const; 1396aeeca8eSWarner Losh private: 1406aeeca8eSWarner Losh int _prio; 1416aeeca8eSWarner Losh std::vector<eps *> _epsvec; 1426aeeca8eSWarner Losh }; 1436aeeca8eSWarner Losh 1446aeeca8eSWarner Losh class config 1456aeeca8eSWarner Losh { 1466aeeca8eSWarner Losh public: 147c3fa0037SEitan Adler config() : _pidfile("") { push_var_table(); } 1486aeeca8eSWarner Losh virtual ~config() { reset(); } 1496aeeca8eSWarner Losh void add_attach(int, event_proc *); 1506aeeca8eSWarner Losh void add_detach(int, event_proc *); 1516aeeca8eSWarner Losh void add_directory(const char *); 1526aeeca8eSWarner Losh void add_nomatch(int, event_proc *); 153842ccec5SWarner Losh void add_notify(int, event_proc *); 1546aeeca8eSWarner Losh void set_pidfile(const char *); 1556aeeca8eSWarner Losh void reset(); 1566aeeca8eSWarner Losh void parse(); 157b8f92ce4SWarner Losh void close_pidfile(); 1581a0cc6b1SPawel Jakub Dawidek void open_pidfile(); 1591a0cc6b1SPawel Jakub Dawidek void write_pidfile(); 1601a0cc6b1SPawel Jakub Dawidek void remove_pidfile(); 1616aeeca8eSWarner Losh void push_var_table(); 1626aeeca8eSWarner Losh void pop_var_table(); 1636aeeca8eSWarner Losh void set_variable(const char *var, const char *val); 1646aeeca8eSWarner Losh const std::string &get_variable(const std::string &var); 165*5dfc0f6cSIan Lepore const std::string expand_string(const char * var, 166*5dfc0f6cSIan Lepore const char * prepend = NULL, const char * append = NULL); 1676aeeca8eSWarner Losh char *set_vars(char *); 1686aeeca8eSWarner Losh void find_and_execute(char); 1696aeeca8eSWarner Losh protected: 1706aeeca8eSWarner Losh void sort_vector(std::vector<event_proc *> &); 1716aeeca8eSWarner Losh void parse_one_file(const char *fn); 1726aeeca8eSWarner Losh void parse_files_in_dir(const char *dirname); 1736aeeca8eSWarner Losh void expand_one(const char *&src, std::string &dst); 174ef370346SEitan Adler bool is_id_char(char) const; 1756aeeca8eSWarner Losh bool chop_var(char *&buffer, char *&lhs, char *&rhs); 1766aeeca8eSWarner Losh private: 1776aeeca8eSWarner Losh std::vector<std::string> _dir_list; 1786aeeca8eSWarner Losh std::string _pidfile; 1796aeeca8eSWarner Losh std::vector<var_list *> _var_list_table; 1806aeeca8eSWarner Losh std::vector<event_proc *> _attach_list; 1816aeeca8eSWarner Losh std::vector<event_proc *> _detach_list; 1826aeeca8eSWarner Losh std::vector<event_proc *> _nomatch_list; 183842ccec5SWarner Losh std::vector<event_proc *> _notify_list; 1846aeeca8eSWarner Losh }; 1856aeeca8eSWarner Losh 1866aeeca8eSWarner Losh #endif /* DEVD_HH */ 187