Misc update.

This commit is contained in:
CismonX 2020-11-27 09:23:12 +08:00
parent de6e3577a8
commit 5029af1fa8
Signed by: cismonx
GPG Key ID: 3094873E29A482FB
7 changed files with 14 additions and 194 deletions

View File

@ -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

View File

@ -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], [

View File

@ -6,9 +6,6 @@ ctlseqs - helper library for terminal control sequences
.SH SYNOPSIS
.nf
.B #include <ctlseqs.h>
.PP
.B #define __cplusplus 201103L
.BR "#include <ctlseqs.hh>" " /* C++ Wrapper */"
.fi
.
.SH DESCRIPTION

View File

@ -18,4 +18,4 @@
lib_LTLIBRARIES = libctlseqs.la
libctlseqs_la_SOURCES = ctlseqs.c
include_HEADERS = ctlseqs.h ctlseqs.hh
include_HEADERS = ctlseqs.h

View File

@ -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 <https://www.gnu.org/licenses/>.
* along with ctlseqs. If not, see <https://www.gnu.org/licenses/>.
*/
#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;

View File

@ -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 <https://www.gnu.org/licenses/>.
* along with ctlseqs. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CTLSEQS_H_

View File

@ -1,172 +0,0 @@
/**
* ctlseqs.hh - C++ wrapper for the ctlseqs library
*
* Copyright (C) 2020 CismonX <admin@cismon.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#ifndef CTLSEQS_HH_
#define CTLSEQS_HH_
#include "ctlseqs.h"
#include <chrono>
#include <new>
#include <stdexcept>
#include <vector>
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<char const *> 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 <std::size_t N>
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 <std::size_t N>
void buffer(buffer<N> const &buffer) {
buffer_ = reinterpret_cast<ctlseqs_value *>(&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 <typename R, typename P>
ssize_t read(matcher const &matcher, std::chrono::duration<R, P> timeout) {
int timeout_millis = std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count();
return setopt_and_read(matcher.matcher_, timeout_millis);
}
~reader() {
ctlseqs_reader_free(reader_);
}
};
}
#endif // !CTLSEQS_HH_