1999-06-23 13:19:30 +02:00
|
|
|
/*
|
2019-02-15 21:24:31 +01:00
|
|
|
* boxes - Command line filter to draw/remove ASCII boxes around text
|
2023-03-26 21:32:08 +02:00
|
|
|
* Copyright (c) 1999-2023 Thomas Jensen and the boxes contributors
|
1999-07-20 20:56:15 +02:00
|
|
|
*
|
2022-09-18 14:56:30 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
|
|
|
|
* License, version 3, as published by the Free Software Foundation.
|
|
|
|
* This program 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 program.
|
|
|
|
* If not, see <https://www.gnu.org/licenses/>.
|
2021-01-27 17:13:17 +01:00
|
|
|
*
|
2022-09-18 14:56:30 +02:00
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
1999-06-23 13:19:30 +02:00
|
|
|
*/
|
|
|
|
|
2019-02-20 11:04:09 +01:00
|
|
|
/*
|
2021-01-30 21:10:05 +01:00
|
|
|
* Tool functions for error reporting and some string handling and other needful things
|
2019-02-20 11:04:09 +01:00
|
|
|
*/
|
2019-02-15 21:24:31 +01:00
|
|
|
|
1999-06-23 13:19:30 +02:00
|
|
|
#ifndef TOOLS_H
|
|
|
|
#define TOOLS_H
|
|
|
|
|
2021-01-30 21:10:05 +01:00
|
|
|
#include <unitypes.h>
|
|
|
|
|
2021-02-05 11:01:38 +01:00
|
|
|
#include "boxes.h"
|
|
|
|
|
1999-06-23 13:19:30 +02:00
|
|
|
|
2021-02-22 20:59:42 +01:00
|
|
|
|
2021-02-06 18:17:00 +01:00
|
|
|
#define BMAX(a, b) ({ /* return the larger value */ \
|
|
|
|
__typeof__ (a) _a = (a); \
|
|
|
|
__typeof__ (b) _b = (b); \
|
|
|
|
_a > _b ? _a : _b; \
|
|
|
|
})
|
1999-06-23 13:19:30 +02:00
|
|
|
|
|
|
|
#define BFREE(p) { /* free memory and clear pointer */ \
|
2022-09-17 16:02:42 +02:00
|
|
|
if (p) { \
|
|
|
|
free((void *) (p)); \
|
|
|
|
(p) = NULL; \
|
|
|
|
} \
|
1999-06-23 13:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-27 17:13:17 +01:00
|
|
|
int empty_line(const line_t *line);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-01-28 21:37:23 +01:00
|
|
|
size_t expand_tabs_into(const uint32_t *input_buffer, const int tabstop, uint32_t **text,
|
|
|
|
size_t **tabpos, size_t *tabpos_len);
|
2021-01-27 17:13:17 +01:00
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-01-27 17:13:17 +01:00
|
|
|
void btrim(char *text, size_t *len);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-01-28 21:37:23 +01:00
|
|
|
void btrim32(uint32_t *text, size_t *len);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2022-09-17 16:02:42 +02:00
|
|
|
char *my_strnrstr(const char *s1, const char *s2, const size_t s2_len, int skip);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2022-09-17 16:02:42 +02:00
|
|
|
/**
|
|
|
|
* Calculates the length (in bytes) of the segment at the end of `s` which consists entirely of bytes in `accept`.
|
|
|
|
* This is like `strspn()`, but from the end of the string.
|
2023-05-14 22:17:10 +02:00
|
|
|
* @param s the string to search
|
|
|
|
* @param accept acceptable characters that form the trailing segment
|
2022-09-17 16:02:42 +02:00
|
|
|
* @return the number of bytes found as described above
|
|
|
|
*/
|
|
|
|
size_t my_strrspn(const char *s, const char *accept);
|
2021-01-27 17:13:17 +01:00
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-01-27 17:13:17 +01:00
|
|
|
int strisyes(const char *s);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-01-27 17:13:17 +01:00
|
|
|
int strisno(const char *s);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-02-22 20:59:42 +01:00
|
|
|
/**
|
|
|
|
* Concatenate variable number of strings into one. This would normally be achieved via snprintf(), but that's not
|
|
|
|
* available on all platforms where boxes is compiled.
|
2023-05-14 22:17:10 +02:00
|
|
|
* @param count number of strings given in the following
|
2021-02-22 20:59:42 +01:00
|
|
|
* @param <...> the strings
|
2023-05-14 22:17:10 +02:00
|
|
|
* @return a new string, for which new memory was allocated
|
2021-02-22 20:59:42 +01:00
|
|
|
*/
|
|
|
|
char *concat_strings_alloc(size_t count, ...);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-01-27 17:13:17 +01:00
|
|
|
void concat_strings(char *dst, int max_len, int count, ...);
|
1999-06-23 13:19:30 +02:00
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2023-05-14 22:17:10 +02:00
|
|
|
/**
|
|
|
|
* Repeat the string `s` `count` times.
|
|
|
|
* @param s the string to repeat
|
|
|
|
* @param count the number of times the string should be repeated
|
|
|
|
* @return a new string, for which new memory was allocated, or NULL if `s` was NULL or out of memory
|
|
|
|
*/
|
|
|
|
char *repeat(char *s, size_t count);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if tab expansion mode is "keep", and if so, calculates a new indentation string based on the one given.
|
|
|
|
* The new string contains tabs in their original positions.
|
|
|
|
*
|
|
|
|
* @param lineno index of the input line we are referring to
|
|
|
|
* @param indentspc previously calculated "space-only" indentation string (may be NULL). This is only used when
|
|
|
|
* opt.tabexp != 'k', in which case it will be used as the function result.
|
|
|
|
* @param indentspc_len desired result length, measured in spaces only
|
|
|
|
* @return if successful and opt.tabexp == 'k': new string;
|
|
|
|
* on error (invalid input or out of memory): NULL
|
|
|
|
*/
|
|
|
|
uint32_t *tabbify_indent(const size_t lineno, uint32_t *indentspc, const size_t indentspc_len);
|
1999-08-31 17:38:42 +02:00
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-01-30 21:10:05 +01:00
|
|
|
char *nspaces(const size_t n);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-02-06 18:17:00 +01:00
|
|
|
void print_input_lines(const char *heading);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-04-15 20:50:02 +02:00
|
|
|
void analyze_line_ascii(input_t *input_ptr, line_t *line);
|
2021-02-06 18:17:00 +01:00
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2022-09-17 16:02:42 +02:00
|
|
|
size_t count_invisible_chars(const uint32_t *s, size_t *num_esc, char **ascii, size_t **posmap);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2022-09-17 16:02:42 +02:00
|
|
|
/**
|
|
|
|
* Determine whether the given sequence of characters is a CSI (also called "escape sequence") that resets all
|
|
|
|
* modifications, typically `ESC[0m`.
|
|
|
|
* @param csi a pointer into a zero-terminated UTF-32 string
|
|
|
|
* @returns 1 if true, 0 if false
|
|
|
|
*/
|
|
|
|
int is_csi_reset(const uint32_t *csi);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-03-04 22:13:04 +01:00
|
|
|
int array_contains(char **array, const size_t array_len, const char *s);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-04-02 15:45:51 +02:00
|
|
|
/**
|
|
|
|
* Determine if the given `array` contains the given string (case-insensitive!).
|
|
|
|
* @param array an array of strings to search
|
|
|
|
* @param s the string to search
|
|
|
|
* @returns != 0 if found, 0 if not found
|
|
|
|
*/
|
|
|
|
int array_contains0(char **array, const char *s);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
|
|
|
int array_contains_bxs(bxstr_t **array, const size_t array_len, bxstr_t *s);
|
|
|
|
|
|
|
|
|
2021-04-02 15:45:51 +02:00
|
|
|
/**
|
|
|
|
* Count the number of elements in a zero-terminated array.
|
|
|
|
* @param array a zero-terminated array of strings (can be NULL)
|
|
|
|
* @returns the number of elements, excluding the zero terminator
|
|
|
|
*/
|
|
|
|
size_t array_count0(char **array);
|
2006-07-22 21:11:07 +02:00
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-04-02 22:12:21 +02:00
|
|
|
/**
|
|
|
|
* Trim leading and trailing whitespace from a string and return the result in a new string, for which memory is
|
|
|
|
* allocated. `s` and `e` may point into some other string, which will not be modified. `e` should be greater than `s`.
|
|
|
|
* @param s pointer to the first character of the string
|
|
|
|
* @param e pointer to the last character of the string (NOT the character AFTER the last)
|
|
|
|
* @returns a new string, which may be the empty string if the input was invalid or consisted only of whitespace
|
|
|
|
*/
|
|
|
|
char *trimdup(char *s, char *e);
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
|
2021-04-02 22:12:21 +02:00
|
|
|
/**
|
|
|
|
* Determine if the given string is a valid tag. Valid tags are lower-case alphanumeric strings which may have
|
|
|
|
* intermixed, single hyphens. A valid tag starts with a letter. Hyphens may also not appear as last character.
|
|
|
|
* @param tag a potential tag
|
|
|
|
* @return flag indicating validity
|
|
|
|
*/
|
|
|
|
int tag_is_valid(char *tag);
|
|
|
|
|
|
|
|
|
2021-04-07 21:21:44 +02:00
|
|
|
/**
|
|
|
|
* Duplicate at most `n` bytes from the given string `s`. Memory for the new string is obtained with `malloc()`, and
|
|
|
|
* can be freed with `free()`. A terminating null byte is added. We include this implementation because the libc's
|
|
|
|
* `strndup()` is not consistently available across all platforms.
|
|
|
|
* @param s a string
|
|
|
|
* @param n maximum number of characters to copy (excluding the null byte)
|
|
|
|
* @returns a new string, or `NULL` if either `s` was also `NULL`, or out of memory
|
|
|
|
*/
|
|
|
|
char *bx_strndup(const char *s, size_t n);
|
|
|
|
|
|
|
|
|
2021-11-01 14:19:52 +01:00
|
|
|
/**
|
|
|
|
* Just calls `fprintf()` internally, but this function can be mocked in unit tests. So, it's a "mockable `fprintf()`".
|
|
|
|
* @param stream Where to print, for example `stderr`
|
|
|
|
* @param format the format string, followed by the arguments of the format string
|
|
|
|
*/
|
|
|
|
void bx_fprintf(FILE *stream, const char *format, ...);
|
|
|
|
|
|
|
|
|
2023-04-19 21:06:00 +02:00
|
|
|
/**
|
|
|
|
* Determine if the given string is an "ASCII ID", which means:
|
|
|
|
* - It consists only of the letters `abcdefghijklmnopqrstuvwxyz-0123456789`. If not in strict mode, upper case A-Z
|
|
|
|
* and underscores are also allowed.
|
|
|
|
* - The first letter is a-z (lower case). If not in strict mode, upper case A-Z is also allowed.
|
|
|
|
* - The last character may not be a hyphen or underscore.
|
|
|
|
* - No two hyphens or underscores, or mixture thereof, may appear in sequence.
|
|
|
|
* - The entire string must not be the word `none`.
|
|
|
|
* - Minimum length is 1 character.
|
|
|
|
* @param s the string to check
|
|
|
|
* @param strict flag indicating whether "strict checks" should be applied (1) or not (0)
|
|
|
|
* @return flag (1 or 0)
|
|
|
|
*/
|
|
|
|
int is_ascii_id(bxstr_t *s, int strict);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the file pointed to by `pathname` and associate a stream with it. Supports non-ASCII pathnames and encapsulates
|
|
|
|
* the logic for different operating systems.
|
|
|
|
* @param pathname the pathname of the file to open
|
|
|
|
* @param mode a mode sequence like for standard `fopen()`
|
|
|
|
* @return the file stream
|
|
|
|
*/
|
|
|
|
FILE *bx_fopens(bxstr_t *pathname, char *mode);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the file pointed to by `pathname` and associate a stream with it. Supports non-ASCII pathnames and encapsulates
|
|
|
|
* the logic for different operating systems.
|
|
|
|
* @param pathname the pathname of the file to open, as a UTF-8 or ASCII (single byte) byte sequence
|
|
|
|
* @param mode a mode sequence like for standard `fopen()`
|
|
|
|
* @return the file stream
|
|
|
|
*/
|
|
|
|
FILE *bx_fopen(char *pathname, char *mode);
|
|
|
|
|
|
|
|
|
1999-06-23 13:19:30 +02:00
|
|
|
#endif
|
|
|
|
|
2023-05-14 22:17:10 +02:00
|
|
|
/* vim: set cindent sw=4: */
|