diff --git a/INSTALL.org b/INSTALL.org index 62d069e..20663b6 100644 --- a/INSTALL.org +++ b/INSTALL.org @@ -11,22 +11,16 @@ ** Copy Library Code to Your Project This is the recommended way to use this library. Just copy [[file:src/ctlseqs.h][ctlseqs.h]] and - [[file:src/ctlseqs.c][ctlseqs.c]] (and optionally the C++ wrapper [[file:src/ctlseqs.hh][ctlseqs.hh]]) to your project and - build it alongside with other code. + [[file:src/ctlseqs.c][ctlseqs.c]] to your project and build it alongside with other code. - No precautions need to be taken, as ctlseqs has no third-party dependencies - and does not require any fancy compiler flags or CPP macros. An ISO C99 and - POSIX.1-2008 compliant C implementation would suffice. + Requires an ISO C99 and POSIX.1-2008 compliant C implementation. ** Build and Install From Source Alternatively, You may want a systemwide installation of the library and link it to your project. Some scripts are provided to help you with that. - GNU Autoconf, Automake, Libtool and Autoconf Archive is required. - - Generate and execute configure script. There are no custom configuration - options available for this library, just builtin ones. + Requires GNU Autoconf, Automake, Libtool and Autoconf Archive. #+BEGIN_SRC shell autoreconf --install @@ -35,7 +29,7 @@ Compile the library source code, as well as [[file:examples][examples]] and test suites. Optionally, you can run the test suite (requires DejaGnu), install the - binary and man pages to your prefix. + binary and man pages. #+BEGIN_SRC shell make && make check && make install diff --git a/configure.ac b/configure.ac index ae4d0f0..7a8be14 100644 --- a/configure.ac +++ b/configure.ac @@ -22,7 +22,6 @@ AC_PROG_CPP AC_PROG_INSTALL AC_PROG_LN_S AC_PROG_MAKE_SET -AC_PROG_RANLIB # Checks for compiler builtins and attributes. m4_ifdef([AX_GCC_BUILTIN], [ diff --git a/man/ctlseqs.7 b/man/ctlseqs.7 index bb761d2..f8db1a1 100644 --- a/man/ctlseqs.7 +++ b/man/ctlseqs.7 @@ -6,9 +6,6 @@ ctlseqs - helper library for terminal control sequences .SH SYNOPSIS .nf .B #include -.PP -.B #define __cplusplus 201103L -.BR "#include " " /* C++ Wrapper */" .fi . .SH DESCRIPTION diff --git a/src/Makefile.am b/src/Makefile.am index 7c3e18d..5c20640 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,4 +18,4 @@ lib_LTLIBRARIES = libctlseqs.la libctlseqs_la_SOURCES = ctlseqs.c -include_HEADERS = ctlseqs.h ctlseqs.hh +include_HEADERS = ctlseqs.h diff --git a/src/ctlseqs.c b/src/ctlseqs.c index 36946a0..e2f198e 100644 --- a/src/ctlseqs.c +++ b/src/ctlseqs.c @@ -5,18 +5,18 @@ * * This file is part of the ctlseqs library. * - * This library is free software: you can redistribute it and/or modify + * Ctlseqs is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * Ctlseqs is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this library. If not, see . + * along with ctlseqs. If not, see . */ #ifdef HAVE_CONFIG_H @@ -346,7 +346,9 @@ ctlseqs_match(struct ctlseqs_reader *reader, struct ctlseqs_matcher const *match } } reader->state = state; - reader->buf_start += idx; + if (!reader->retain_partial || retval != CTLSEQS_PARTIAL) { + reader->buf_start += idx; + } if (reader->buf_start == reader->buf_end) { reader->buf_start = 0; reader->buf_end = 0; diff --git a/src/ctlseqs.h b/src/ctlseqs.h index 8a7f8e8..cd54d8d 100644 --- a/src/ctlseqs.h +++ b/src/ctlseqs.h @@ -5,18 +5,18 @@ * * This file is part of the ctlseqs library. * - * This library is free software: you can redistribute it and/or modify + * Ctlseqs is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * Ctlseqs is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this library. If not, see . + * along with ctlseqs. If not, see . */ #ifndef CTLSEQS_H_ diff --git a/src/ctlseqs.hh b/src/ctlseqs.hh deleted file mode 100644 index 80b15d4..0000000 --- a/src/ctlseqs.hh +++ /dev/null @@ -1,172 +0,0 @@ -/** - * ctlseqs.hh - C++ wrapper for the ctlseqs library - * - * Copyright (C) 2020 CismonX - * - * This file is part of the ctlseqs library. - * - * This library is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this library. If not, see . - */ - -#ifndef CTLSEQS_HH_ -#define CTLSEQS_HH_ - -#include "ctlseqs.h" - -#include -#include -#include -#include - -namespace ctlseqs { - - // Control sequence matcher - class matcher final { - - ctlseqs_matcher *const matcher_; - - friend class reader; - - public: - - matcher(matcher const &matcher) = delete; - - matcher() : matcher_(ctlseqs_matcher_init()) { - if (matcher_ == nullptr) { - throw std::bad_alloc(); - } - } - - void patterns(std::vector const &patterns) { - ctlseqs_matcher_opts opts { patterns.data(), patterns.size() }; - switch (ctlseqs_matcher_setopt(matcher_, &opts)) { - case CTLSEQS_NOMEM: - throw std::bad_alloc(); - } - } - - ~matcher() { - ctlseqs_matcher_free(matcher_); - } - }; - - // Buffer for parsed sequence - template - class buffer final { - - ctlseqs_value buffer_[N]; - - public: - - // Get string value - char const *str(std::size_t idx) const { - return buffer_[idx].str; - } - - // Get integer value by index - unsigned long num(std::size_t idx) const { - return buffer_[idx].num; - } - }; - - // Control sequence reader - class reader final { - - ctlseqs_reader *const reader_; - - ctlseqs_value *buffer_; - size_t maxlen_; - int fd_; - bool no_poll_; - bool retain_partial_; - bool options_modified_; - - ssize_t setopt_and_read(matcher const &matcher, int timeout) { - if (options_modified_) { - unsigned flags = retain_partial_ * CTLSEQS_READER_RETAIN_PARTIAL | no_poll_ * CTLSEQS_READER_NO_POLL; - ctlseqs_reader_opts opts { buffer_, maxlen_, fd_, flags }; - switch (ctlseqs_reader_setopt(reader_, &opts)) { - case CTLSEQS_NOMEM: - throw std::bad_alloc(); - } - options_modified_ = false; - } - ssize_t retval = ctlseqs_read(reader_, matcher.matcher_, timeout); - switch (retval) { - case CTLSEQS_ERROR: - throw std::runtime_error("read failed"); - } - return retval; - } - - public: - - reader(reader const &reader) = delete; - - reader() : reader_(ctlseqs_reader_init()) { - if (reader_ == nullptr) { - throw std::bad_alloc(); - } - } - - // Set buffer for parsed message - template - void buffer(buffer const &buffer) { - buffer_ = reinterpret_cast(&buffer); - options_modified_ = true; - } - - // Set file descriptor to read from - void fd(int fd) { - fd_ = fd; - options_modified_ = true; - } - - // Set maximum read size - void maxlen(std::size_t maxlen) { - maxlen_ = maxlen; - options_modified_ = true; - } - - // Do not poll() before read() - void no_poll(bool enabled = true) { - no_poll_ = enabled; - options_modified_ = true; - } - - // Retain data after a partial match - void retain_partial(bool enabled = true) { - retain_partial_ = enabled; - options_modified_ = true; - } - - // Read without timemout - ssize_t read(matcher const &matcher) { - return setopt_and_read(matcher, -1); - } - - // Read with timeout - template - ssize_t read(matcher const &matcher, std::chrono::duration timeout) { - int timeout_millis = std::chrono::duration_cast(timeout).count(); - return setopt_and_read(matcher.matcher_, timeout_millis); - } - - ~reader() { - ctlseqs_reader_free(reader_); - } - }; -} - -#endif // !CTLSEQS_HH_