xref: /freebsd/sbin/devd/devd.hh (revision 6aeeca8edf430749fd06906be1d363d23e086e64)
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  */
686aeeca8eSWarner Losh class 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:
936aeeca8eSWarner Losh 	std::string _var;
946aeeca8eSWarner Losh 	std::string _re;
956aeeca8eSWarner Losh 	regex_t _regex;
966aeeca8eSWarner Losh };
976aeeca8eSWarner Losh 
986aeeca8eSWarner Losh /**
996aeeca8eSWarner Losh  * action is used to fork a process.  It matches everything.
1006aeeca8eSWarner Losh  */
1016aeeca8eSWarner Losh class action : public eps
1026aeeca8eSWarner Losh {
1036aeeca8eSWarner Losh public:
1046aeeca8eSWarner Losh 	action(const char *cmd);
1056aeeca8eSWarner Losh 	virtual ~action();
1066aeeca8eSWarner Losh 	virtual bool do_match(config &) { return true; }
1076aeeca8eSWarner Losh 	virtual bool do_action(config &);
1086aeeca8eSWarner Losh private:
1096aeeca8eSWarner Losh 	std::string _cmd;
1106aeeca8eSWarner Losh };
1116aeeca8eSWarner Losh 
1126aeeca8eSWarner Losh class event_proc
1136aeeca8eSWarner Losh {
1146aeeca8eSWarner Losh public:
1156aeeca8eSWarner Losh 	event_proc();
1166aeeca8eSWarner Losh 	virtual ~event_proc();
1176aeeca8eSWarner Losh 	int get_priority() const { return (_prio); }
1186aeeca8eSWarner Losh 	void set_priority(int prio) { _prio = prio; }
1196aeeca8eSWarner Losh 	void add(eps *);
1206aeeca8eSWarner Losh 	bool matches(config &);
1216aeeca8eSWarner Losh 	bool run(config &);
1226aeeca8eSWarner Losh private:
1236aeeca8eSWarner Losh 	int _prio;
1246aeeca8eSWarner Losh 	std::vector<eps *> _epsvec;
1256aeeca8eSWarner Losh };
1266aeeca8eSWarner Losh 
1276aeeca8eSWarner Losh class config
1286aeeca8eSWarner Losh {
1296aeeca8eSWarner Losh public:
1306aeeca8eSWarner Losh 	config() : _pidfile("") { push_var_table(); }
1316aeeca8eSWarner Losh 	virtual ~config() { reset(); }
1326aeeca8eSWarner Losh 	void add_attach(int, event_proc *);
1336aeeca8eSWarner Losh 	void add_detach(int, event_proc *);
1346aeeca8eSWarner Losh 	void add_directory(const char *);
1356aeeca8eSWarner Losh 	void add_nomatch(int, event_proc *);
1366aeeca8eSWarner Losh 	void set_pidfile(const char *);
1376aeeca8eSWarner Losh 	void reset();
1386aeeca8eSWarner Losh 	void parse();
1396aeeca8eSWarner Losh 	void drop_pidfile();
1406aeeca8eSWarner Losh 	void push_var_table();
1416aeeca8eSWarner Losh 	void pop_var_table();
1426aeeca8eSWarner Losh 	void set_variable(const char *var, const char *val);
1436aeeca8eSWarner Losh 	const std::string &get_variable(const std::string &var);
1446aeeca8eSWarner Losh 	const std::string expand_string(const std::string &var);
1456aeeca8eSWarner Losh 	char *set_vars(char *);
1466aeeca8eSWarner Losh 	void find_and_execute(char);
1476aeeca8eSWarner Losh protected:
1486aeeca8eSWarner Losh 	void sort_vector(std::vector<event_proc *> &);
1496aeeca8eSWarner Losh 	void parse_one_file(const char *fn);
1506aeeca8eSWarner Losh 	void parse_files_in_dir(const char *dirname);
1516aeeca8eSWarner Losh 	void expand_one(const char *&src, std::string &dst);
1526aeeca8eSWarner Losh 	bool is_id_char(char);
1536aeeca8eSWarner Losh 	bool chop_var(char *&buffer, char *&lhs, char *&rhs);
1546aeeca8eSWarner Losh private:
1556aeeca8eSWarner Losh 	std::vector<std::string> _dir_list;
1566aeeca8eSWarner Losh 	std::string _pidfile;
1576aeeca8eSWarner Losh 	std::vector<var_list *> _var_list_table;
1586aeeca8eSWarner Losh 	std::vector<event_proc *> _attach_list;
1596aeeca8eSWarner Losh 	std::vector<event_proc *> _detach_list;
1606aeeca8eSWarner Losh 	std::vector<event_proc *> _nomatch_list;
1616aeeca8eSWarner Losh };
1626aeeca8eSWarner Losh 
1636aeeca8eSWarner Losh #endif /* DEVD_HH */
164