int shell_exec(const ss_ &command, const ExecOptions &opts) { log_d(MODULE, "shell_exec(\"%s\")", cs(command)); int f = fork(); if(f == 0){ execl("/bin/sh", "sh", "-c", command.c_str(), (const char*)nullptr); } int exit_status; while(wait(&exit_status) > 0); return exit_status; }
void add(const ss_ &path, std::function<void(const ss_ &path)> cb) { for(auto &pair : m_watch){ sp_<WatchThing> &watch = pair.second; if(watch->path == path){ log_d(MODULE, "Adding callback to path \"%s\" (inotify fd=%i)", cs(path), pair.first); watch->cbs.push_back(cb); return; } } int r = inotify_add_watch(m_fd, path.c_str(), IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE | IN_MOVED_FROM | IN_DELETE | IN_MODIFY | IN_ATTRIB); if(r == -1){ throw Exception(ss_()+"inotify_add_watch() failed: "+ strerror(errno)+" (path="+path+")"); } log_d(MODULE, "Watching path \"%s\" (inotify fd=%i)", cs(path), m_fd); m_watch[r] = sp_<WatchThing>(new WatchThing(path, {cb})); }
static sv_<ss_> split_string_to_clean_ui_pieces(const ss_ &s, size_t piece_len) { sv_<ss_> result; size_t start_from = 0; for(;;){ if(start_from >= s.size()) break; size_t start_from_instead = start_from; for(size_t i=start_from; i<s.size(); i++){ if((s[i] < 'a' || s[i] > 'z') && (s[i] < 'A' || s[i] > 'Z') && (s[i] < '0' || s[i] > '9')){ start_from_instead = i + 1; continue; } break; } if(start_from_instead >= s.size()){ result.push_back(s.substr(start_from)); break; } if(start_from_instead >= s.size() - piece_len){ result.push_back(s.substr(start_from_instead)); break; } size_t end_at = start_from_instead + piece_len; if(s.size() > end_at + 2 && end_at >= 3){ if(is_spacing(s[end_at-2]) && !is_spacing(s[end_at-1]) && !is_spacing(s[end_at]) && (is_spacing(s[end_at+1]) || is_spacing(s[end_at+2]))){ end_at = end_at - 1; } else if(is_spacing(s[end_at-3]) && !is_spacing(s[end_at-2]) && !is_spacing(s[end_at-1]) && !is_spacing(s[end_at]) && (is_spacing(s[end_at+1]) || is_spacing(s[end_at+2]))){ end_at = end_at - 2; } } result.push_back(s.substr(start_from_instead, end_at - start_from_instead)); start_from = end_at; } return result; }
static ss_ truncate(const ss_ &s, size_t len) { if(s.size() < len) return s; return s.substr(0, len); }
static void arduino_serial_write(const ss_ &data) { arduino_serial_write(data.c_str(), data.size()); }
Message(const ss_ &text="", const ss_ &short_text=""): text(text), short_text(short_text.empty() ? text : short_text) {}