| 1 | #include "StdAfx.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <map>
|
|---|
| 4 | #include <list>
|
|---|
| 5 |
|
|---|
| 6 | #include <boost/bind.hpp>
|
|---|
| 7 | #include <boost/assign.hpp>
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | #include <parsers/where_parser.hpp>
|
|---|
| 11 | #include <parsers/filter/where_filter.hpp>
|
|---|
| 12 | #include <parsers/filter/where_filter_impl.hpp>
|
|---|
| 13 |
|
|---|
| 14 | #include <parsers/where_parser.hpp>
|
|---|
| 15 | #include <simple_timer.hpp>
|
|---|
| 16 | #include "filter.hpp"
|
|---|
| 17 |
|
|---|
| 18 | #include <parsers/filter/where_filter_impl.hpp>
|
|---|
| 19 |
|
|---|
| 20 | using namespace boost::assign;
|
|---|
| 21 | using namespace parsers::where;
|
|---|
| 22 |
|
|---|
| 23 | tasksched_filter::filter_obj_handler::filter_obj_handler() {
|
|---|
| 24 | insert(types)
|
|---|
| 25 | (_T("title"), (type_string))
|
|---|
| 26 | // (_T("account"), (type_string))
|
|---|
| 27 | // (_T("application"), (type_string))
|
|---|
| 28 | // (_T("comment"), (type_string))
|
|---|
| 29 | // (_T("creator"), (type_string))
|
|---|
| 30 | // (_T("parameters"), (type_string))
|
|---|
| 31 | // (_T("working_directory"), (type_string))
|
|---|
| 32 | // (_T("error_retry_count"), (type_int))
|
|---|
| 33 | // (_T("error_retry_interval"), (type_int))
|
|---|
| 34 | // (_T("idle_wait"), (type_int))
|
|---|
| 35 | (_T("exit_code"), (type_int))
|
|---|
| 36 | // (_T("flags"), (type_int))
|
|---|
| 37 | // (_T("max_run_time"), (type_int))
|
|---|
| 38 | // (_T("priority"), (type_int))
|
|---|
| 39 | (_T("status"), (type_custom_hresult))
|
|---|
| 40 | (_T("most_recent_run_time"), (type_date));
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | bool tasksched_filter::filter_obj_handler::has_variable(std::wstring key) {
|
|---|
| 44 | return types.find(key) != types.end();
|
|---|
| 45 | }
|
|---|
| 46 | parsers::where::value_type tasksched_filter::filter_obj_handler::get_type(std::wstring key) {
|
|---|
| 47 | types_type::const_iterator cit = types.find(key);
|
|---|
| 48 | if (cit == types.end())
|
|---|
| 49 | return parsers::where::type_invalid;
|
|---|
| 50 | return cit->second;
|
|---|
| 51 | }
|
|---|
| 52 | bool tasksched_filter::filter_obj_handler::can_convert(parsers::where::value_type from, parsers::where::value_type to) {
|
|---|
| 53 | if ((from == parsers::where::type_string)&&(to == type_custom_hresult))
|
|---|
| 54 | return true;
|
|---|
| 55 | if ((from == parsers::where::type_int)&&(to == type_custom_hresult))
|
|---|
| 56 | return true;
|
|---|
| 57 | return false;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | tasksched_filter::filter_obj_handler::handler::bound_string_type tasksched_filter::filter_obj_handler::bind_string(std::wstring key) {
|
|---|
| 61 | handler::bound_string_type ret;
|
|---|
| 62 | if (key == _T("title"))
|
|---|
| 63 | ret = &object_type::get_title;
|
|---|
| 64 | // else if (key == _T("account"))
|
|---|
| 65 | // ret = &object_type::get_account_name;
|
|---|
| 66 | // else if (key == _T("application"))
|
|---|
| 67 | // ret = &object_type::get_application_name;
|
|---|
| 68 | // else if (key == _T("comment"))
|
|---|
| 69 | // ret = &object_type::get_comment;
|
|---|
| 70 | // else if (key == _T("creator"))
|
|---|
| 71 | // ret = &object_type::get_creator;
|
|---|
| 72 | // else if (key == _T("parameters"))
|
|---|
| 73 | // ret = &object_type::get_parameters;
|
|---|
| 74 | // else if (key == _T("working_directory"))
|
|---|
| 75 | // ret = &object_type::get_working_directory;
|
|---|
| 76 | // else
|
|---|
| 77 | NSC_DEBUG_MSG_STD(_T("Failed to bind (string): ") + key);
|
|---|
| 78 | return ret;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | tasksched_filter::filter_obj_handler::handler::bound_int_type tasksched_filter::filter_obj_handler::bind_int(std::wstring key) {
|
|---|
| 83 | handler::bound_int_type ret;
|
|---|
| 84 | // if (key == _T("error_retry_count"))
|
|---|
| 85 | // ret = &object_type::get_error_retry_count;
|
|---|
| 86 | // else if (key == _T("error_retry_interval"))
|
|---|
| 87 | // ret = &object_type::get_error_retry_interval;
|
|---|
| 88 | // // else if (key == _T("idle_wait"))
|
|---|
| 89 | // // ret = &object_type::get_idle_wait;
|
|---|
| 90 | if (key == _T("exit_code"))
|
|---|
| 91 | ret = &object_type::get_exit_code;
|
|---|
| 92 | // else if (key == _T("flags"))
|
|---|
| 93 | // ret = &object_type::get_flags;
|
|---|
| 94 | // else if (key == _T("max_run_time"))
|
|---|
| 95 | // ret = &object_type::get_max_run_time;
|
|---|
| 96 | // else if (key == _T("priority"))
|
|---|
| 97 | // ret = &object_type::get_priority;
|
|---|
| 98 | else if (key == _T("status"))
|
|---|
| 99 | ret = &object_type::get_status;
|
|---|
| 100 | else if (key == _T("most_recent_run_time"))
|
|---|
| 101 | ret = &object_type::get_most_recent_run_time;
|
|---|
| 102 | else
|
|---|
| 103 | NSC_DEBUG_MSG_STD(_T("Failed to bind (int): ") + key);
|
|---|
| 104 | return ret;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | bool tasksched_filter::filter_obj_handler::has_function(parsers::where::value_type to, std::wstring name, ast_expr_type subject) {
|
|---|
| 108 | if (to == type_custom_hresult)
|
|---|
| 109 | return true;
|
|---|
| 110 | return false;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | long tasksched_filter::filter_obj::convert_status(std::wstring status) {
|
|---|
| 114 | if (status == _T("ready"))
|
|---|
| 115 | return 3;
|
|---|
| 116 | if (status == _T("running"))
|
|---|
| 117 | return 4;
|
|---|
| 118 | if (status == _T("unknown"))
|
|---|
| 119 | return 0;
|
|---|
| 120 | if (status == _T("disabled"))
|
|---|
| 121 | return 1;
|
|---|
| 122 | if (status == _T("queued"))
|
|---|
| 123 | return 2;
|
|---|
| 124 | return strEx::stoi(status);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | std::wstring tasksched_filter::filter_obj::convert_status(long status) {
|
|---|
| 128 | std::wstring ret;
|
|---|
| 129 | if (status == 3)
|
|---|
| 130 | return _T("ready");
|
|---|
| 131 | if (status == 4)
|
|---|
| 132 | return _T("running");
|
|---|
| 133 | if (status == 0)
|
|---|
| 134 | return _T("unknown");
|
|---|
| 135 | if (status == 1)
|
|---|
| 136 | return _T("disabled");
|
|---|
| 137 | if (status == 2)
|
|---|
| 138 | return _T("queued");
|
|---|
| 139 | return strEx::itos(status);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | tasksched_filter::filter_obj_handler::handler::bound_function_type tasksched_filter::filter_obj_handler::bind_function(parsers::where::value_type to, std::wstring name, ast_expr_type subject) {
|
|---|
| 143 | handler::bound_function_type ret;
|
|---|
| 144 | if (to == type_custom_hresult)
|
|---|
| 145 | ret = &object_type::fun_convert_status;
|
|---|
| 146 | else
|
|---|
| 147 | NSC_DEBUG_MSG_STD(_T("Failed to bind (function): ") + name);
|
|---|
| 148 | return ret;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 156 |
|
|---|
| 157 | #define DEFINE_GET_EX(type, variable, helper, func) type tasksched_filter::filter_obj::get_ ## variable() { return helper.fetch(this, &IRegisteredTask::func, variable); }
|
|---|
| 158 |
|
|---|
| 159 | #define DEFINE_GET_STRING(variable, helper, func) DEFINE_GET_EX(std::wstring, variable, helper, func)
|
|---|
| 160 | #define DEFINE_GET_DWORD(variable, helper, func) DEFINE_GET_EX(unsigned long, variable, helper, func)
|
|---|
| 161 | #define DEFINE_GET_WORD(variable, helper, func) DEFINE_GET_EX(unsigned short, variable, helper, func)
|
|---|
| 162 | #define DEFINE_GET_DATE(variable, helper, func) DEFINE_GET_EX(tasksched_filter::filter_obj::task_sched_date, variable, helper, func)
|
|---|
| 163 | #define DEFINE_GET_HRESULT(variable, helper, func) DEFINE_GET_EX(long, variable, helper, func)
|
|---|
| 164 |
|
|---|
| 165 | DEFINE_GET_STRING(title, string_fetcher, get_Name);
|
|---|
| 166 | DEFINE_GET_HRESULT(exit_code, hresult_fetcher, get_LastTaskResult);
|
|---|
| 167 | DEFINE_GET_WORD(status, state_fetcher, get_State);
|
|---|
| 168 | DEFINE_GET_DATE(most_recent_run_time, date_fetcher, get_LastRunTime);
|
|---|
| 169 |
|
|---|
| 170 | tasksched_filter::filter_obj::ast_expr_type tasksched_filter::filter_obj::fun_convert_status(parsers::where::value_type target_type, ast_expr_type const& subject) {
|
|---|
| 171 | return ast_expr_type(parsers::where::int_value(convert_status(subject.get_string(*this))));
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 | std::wstring tasksched_filter::filter_obj::render(std::wstring format) {
|
|---|
| 176 | strEx::replace(format, _T("%title%"), get_title());
|
|---|
| 177 | // strEx::replace(format, _T("%account%"), get_account_name());
|
|---|
| 178 | // strEx::replace(format, _T("%application%"), get_application_name());
|
|---|
| 179 | // strEx::replace(format, _T("%comment%"), get_comment());
|
|---|
| 180 | // strEx::replace(format, _T("%creator%"), get_creator());
|
|---|
| 181 | // strEx::replace(format, _T("%parameters%"), get_parameters());
|
|---|
| 182 | // strEx::replace(format, _T("%working_directory%"), get_working_directory());
|
|---|
| 183 | //
|
|---|
| 184 | strEx::replace(format, _T("%exit_code%"), strEx::itos(get_exit_code()));
|
|---|
| 185 | // strEx::replace(format, _T("%error_retry_count%"), strEx::itos(get_error_retry_count()));
|
|---|
| 186 | // strEx::replace(format, _T("%error_retry_interval%"), strEx::itos(get_error_retry_interval()));
|
|---|
| 187 | // strEx::replace(format, _T("%flags%"), strEx::itos(get_flags()));
|
|---|
| 188 | // //strEx::replace(format, _T("%idle_wait%"), strEx::itos(get_idle_wait()));
|
|---|
| 189 | // strEx::replace(format, _T("%max_run_time%"), strEx::itos(get_max_run_time()));
|
|---|
| 190 | // strEx::replace(format, _T("%priority%"), strEx::itos(get_priority()));
|
|---|
| 191 | strEx::replace(format, _T("%status%"), convert_status(get_status()));
|
|---|
| 192 | //
|
|---|
| 193 | // // strEx::replace(format, _T("%next_run%"), strEx::format_date(get_next_run()));
|
|---|
| 194 | if (get_most_recent_run_time()) {
|
|---|
| 195 | task_sched_date date = get_most_recent_run_time();
|
|---|
| 196 | unsigned long long t = date;
|
|---|
| 197 | if (t == 0 || date.never_) {
|
|---|
| 198 | strEx::replace(format, _T("%most_recent_run_time%"), _T("never"));
|
|---|
| 199 | strEx::replace(format, _T("%most_recent_run_time-raw%"), _T("never"));
|
|---|
| 200 | } else {
|
|---|
| 201 | strEx::replace(format, _T("%most_recent_run_time%"), strEx::format_date(t));
|
|---|
| 202 | strEx::replace(format, _T("%most_recent_run_time-raw%"), strEx::itos(t));
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | strEx::replace(format, _T("\n"), _T(""));
|
|---|
| 207 | return format;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 | struct where_mode_filter : public tasksched_filter::filter_engine_type {
|
|---|
| 214 | tasksched_filter::filter_argument data;
|
|---|
| 215 | parsers::where::parser<tasksched_filter::filter_obj_handler> ast_parser;
|
|---|
| 216 | tasksched_filter::filter_obj_handler object_handler;
|
|---|
| 217 |
|
|---|
| 218 | where_mode_filter(tasksched_filter::filter_argument data) : data(data) {}
|
|---|
| 219 | bool boot() { return true; }
|
|---|
| 220 |
|
|---|
| 221 | bool validate(std::wstring &message) {
|
|---|
| 222 | if (data->debug)
|
|---|
| 223 | data->error->report_debug(_T("Parsing: ") + data->filter);
|
|---|
| 224 |
|
|---|
| 225 | if (!ast_parser.parse(data->filter)) {
|
|---|
| 226 | data->error->report_error(_T("Parsing failed of '") + data->filter + _T("' at: ") + ast_parser.rest);
|
|---|
| 227 | message = _T("Parsing failed: ") + ast_parser.rest;
|
|---|
| 228 | return false;
|
|---|
| 229 | }
|
|---|
| 230 | if (data->debug)
|
|---|
| 231 | data->error->report_debug(_T("Parsing succeeded: ") + ast_parser.result_as_tree());
|
|---|
| 232 |
|
|---|
| 233 | if (!ast_parser.derive_types(object_handler) || object_handler.has_error()) {
|
|---|
| 234 | message = _T("Invalid types: ") + object_handler.get_error();
|
|---|
| 235 | return false;
|
|---|
| 236 | }
|
|---|
| 237 | if (data->debug)
|
|---|
| 238 | data->error->report_debug(_T("Type resolution succeeded: ") + ast_parser.result_as_tree());
|
|---|
| 239 |
|
|---|
| 240 | if (!ast_parser.bind(object_handler) || object_handler.has_error()) {
|
|---|
| 241 | message = _T("Variable and function binding failed: ") + object_handler.get_error();
|
|---|
| 242 | return false;
|
|---|
| 243 | }
|
|---|
| 244 | if (data->debug)
|
|---|
| 245 | data->error->report_debug(_T("Binding succeeded: ") + ast_parser.result_as_tree());
|
|---|
| 246 |
|
|---|
| 247 | if (!ast_parser.static_eval(object_handler) || object_handler.has_error()) {
|
|---|
| 248 | message = _T("Static evaluation failed: ") + object_handler.get_error();
|
|---|
| 249 | return false;
|
|---|
| 250 | }
|
|---|
| 251 | if (data->debug)
|
|---|
| 252 | data->error->report_debug(_T("Static evaluation succeeded: ") + ast_parser.result_as_tree());
|
|---|
| 253 |
|
|---|
| 254 | return true;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | bool match(tasksched_filter::flyweight_type &record) {
|
|---|
| 258 | tasksched_filter::filter_obj obj(record);
|
|---|
| 259 | bool ret = ast_parser.evaluate(obj);
|
|---|
| 260 | if (obj.has_error()) {
|
|---|
| 261 | data->error->report_error(_T("Error: ") + obj.get_error());
|
|---|
| 262 | }
|
|---|
| 263 | return ret;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | std::wstring get_name() {
|
|---|
| 267 | return _T("where");
|
|---|
| 268 | }
|
|---|
| 269 | std::wstring get_subject() { return data->filter; }
|
|---|
| 270 | };
|
|---|
| 271 |
|
|---|
| 272 | tasksched_filter::filter_engine tasksched_filter::factories::create_engine(tasksched_filter::filter_argument arg) {
|
|---|
| 273 | return filter_engine(new where_mode_filter(arg));
|
|---|
| 274 | }
|
|---|
| 275 | tasksched_filter::filter_argument tasksched_filter::factories::create_argument(std::wstring syntax) {
|
|---|
| 276 | return filter_argument(new tasksched_filter::filter_argument_type(tasksched_filter::filter_argument_type::error_type(new where_filter::nsc_error_handler()), syntax));
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | tasksched_filter::filter_result tasksched_filter::factories::create_result(tasksched_filter::filter_argument arg) {
|
|---|
| 280 | return filter_result(new where_filter::simple_count_result<flyweight_type>(arg));
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|