1
0

utilites/include

This commit is contained in:
dtb 2022-05-14 20:53:48 -04:00
parent 6d620a5a23
commit ce8ac30706
4 changed files with 165 additions and 0 deletions

64
include/ascii.h Normal file
View File

@ -0,0 +1,64 @@
#ifndef _ASCII_H
# define _ASCII_H
# define ASCII_MAX_VALUE 0x7f
# define ASCII_ALPHA_UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# define ASCII_ALPHA_LOWER "abcdefghijklmnopqrstuvwxyz"
# define ASCII_BINARY_DIGITS_UPPER "01"
# define ASCII_BINARY_DIGITS_LOWER ASCII_BINARY_DIGITS_UPPER
# define ASCII_OCTAL_DIGITS_UPPER ASCII_BINARY_DIGITS_UPPER "234567"
# define ASCII_OCTAL_DIGITS_LOWER ASCII_OCTAL_DIGITS_UPPER
# define ASCII_DECIMAL_DIGITS_UPPER ASCII_OCTAL_DIGITS_UPPER "89"
# define ASCII_DECIMAL_DIGITS_LOWER ASCII_DECIMAL_DIGITS_UPPER
# define ASCII_HEXADECIMAL_DIGITS_UPPER \
ASCII_DECIMAL_DIGITS_UPPER "ABCDEF"
# define ASCII_HEXADECIMAL_DIGITS_LOWER \
ASCII_DECIMAL_DIGITS_UPPER "abcdef"
# define ASCII_CHAR_NAMES { \
/* 0 */ "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", \
/* 8 */ "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI", \
/* 16 */ "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", \
/* 24 */ "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US", \
/* 32 */ "SP", "EXCLAMATION_MARK" \
};
enum{
/* 0 */ ASCII_NUL, ASCII_SOH, ASCII_STX, ASCII_ETX,
/* 4 */ ASCII_EOT, ASCII_ENQ, ASCII_ACK, ASCII_BEL,
/* 8 */ ASCII_BS, ASCII_HT, ASCII_LF, ASCII_VT,
/* 12 */ ASCII_FF, ASCII_CR, ASCII_SO, ASCII_SI,
/* 16 */ ASCII_DLE, ASCII_DC1, ASCII_DC2, ASCII_DC3,
/* 20 */ ASCII_DC4, ASCII_NAK, ASCII_SYN, ASCII_ETB,
/* 24 */ ASCII_CAN, ASCII_EM, ASCII_SUB, ASCII_ESC,
/* 28 */ ASCII_FS, ASCII_GS, ASCII_RS, ASCII_US,
/* 32 */ ASCII_SP, ASCII_EXCLAMATION_MARK, ASCII_QUOTATION_MARK, ASCII_NUMBER_SIGN,
/* 36 */ ASCII_CURRENCY_SIGN, ASCII_PERCENT_SIGN, ASCII_AMPERSAND, ASCII_APOSTROPHE,
/* 40 */ ASCII_LEFT_PARENTHESIS, ASCII_RIGHT_PARENTHESIS, ASCII_ASTERISK, ASCII_PLUS_SIGN,
/* 44 */ ASCII_COMMA, ASCII_HYPHEN, ASCII_FULL_STOP, ASCII_SOLIDUS,
/* 48 */ ASCII_0, ASCII_1, ASCII_2, ASCII_3,
/* 52 */ ASCII_4, ASCII_5, ASCII_6, ASCII_7,
/* 56 */ ASCII_8, ASCII_9, ASCII_COLON, ASCII_SEMICOLON,
/* 60 */ ASCII_LESS_THAN_SIGN, ASCII_EQUALS_SIGN, ASCII_GREATER_THAN_SIGN, ASCII_QUESTION_MARK,
/* 64 */ ASCII_COMMERCIAL_AT, ASCII_A, ASCII_B, ASCII_C,
/* 68 */ ASCII_D, ASCII_E, ASCII_F, ASCII_G,
/* 72 */ ASCII_H, ASCII_I, ASCII_J, ASCII_K,
/* 76 */ ASCII_L, ASCII_M, ASCII_N, ASCII_O,
/* 80 */ ASCII_P, ASCII_Q, ASCII_R, ASCII_S,
/* 84 */ ASCII_T, ASCII_U, ASCII_V, ASCII_W,
/* 88 */ ASCII_X, ASCII_Y, ASCII_Z, ASCII_LEFT_SQUARE_BRACKET,
/* 92 */ ASCII_REVERSED_SOLIDUS, ASCII_RIGHT_SQUARE_BRACKET, ASCII_CIRCUMFLEX_ACCENT, ASCII_LOW_LINE,
/* 96 */ ASCII_GRAVE_ACCENT, ASCII_a, ASCII_b, ASCII_c,
/* 100 */ ASCII_d, ASCII_e, ASCII_f, ASCII_g,
/* 104 */ ASCII_h, ASCII_i, ASCII_j, ASCII_k,
/* 108 */ ASCII_l, ASCII_m, ASCII_n, ASCII_o,
/* 112 */ ASCII_p, ASCII_q, ASCII_r, ASCII_s,
/* 116 */ ASCII_t, ASCII_u, ASCII_v, ASCII_w,
/* 120 */ ASCII_x, ASCII_y, ASCII_z, ASCII_LEFT_CURLY_BRACKET,
/* 124 */ ASCII_VERTICAL_LINE, ASCII_RIGHT_CURLY_BRACKET, ASCII_OVERLINE, ASCII_DELETE
};
#endif /* ifndef _ASCII_H */

13
include/stdbool.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef _STDBOOL_H
# define _STDBOOL_H
# define bool _Bool
# define false 0
# define true 1
# define __bool_true_false_are_defined 1
/* "dirtbag boolean"
* - ideally the compiler would keep all the booleans in [x] number of unsigned
* [type]s so it could squeeze eight booleans out of one byte.
* alas, the simplest thing tends to be best, and it's nice to not waste 20
* cycles (figure made up) on applying a bitmask to an int to read a bool */
# define _Bool unsigned char
#endif /* ifndef _STDBOOL_H */

79
include/sysexits.h Normal file
View File

@ -0,0 +1,79 @@
#ifndef _SYSEXITS_H
# define _SYSEXITS_H
/* This is a blind implementation of FreeBSD's sysexits(3) as described by the
* manpage. https://www.freebsd.org/cgi/man.cgi?query=sysexits */
/* Status descriptions copied from the manpage. */
/* These should not be relied upon. Also, good diagnostic output trumps
* this file. */
enum{
EX_OK = 0,
/* The command was used incorrectly, e.g., with the wrong number of
* arguments, a bad flag, a bad syntax in a parameter, or whatever. */
EX_USAGE = 64,
/* The input data was incorrect in some way. This should only be used
* for user's data and not system files. */
EX_DATAERR = 65,
/* An input file (not a system file) did not exist or was not readable.
* This could also include errors like "No message" to a mailer (if it
* cared to catch it). */
EX_NOINPUT = 66,
/* The user specified did not exist. This might be used for mail
* addresses or remote logins. */
EX_NOUSER = 67,
/* The host specified did not exist. This is used in mail addresses or
* network requests. */
EX_NOHOST = 68,
/* A service is unavailable. This can occur if a support program or
* file does not exist. This can also be used as a catchall message
* when something you wanted to do does not work, but you do not know
* why. */
EX_UNAVAILABLE = 69,
/* An internal software error has been detected. This should be limited
* to non-operating system related errors as possible. */
EX_SOFTWARE = 70,
/* An operating system error has been detected. This is intended to be
* used for such things as "cannot fork", "cannot create pipe", or the
* like. It includes things like getuid returning a user that does not
* exist in the passwd file. */
EX_OSERR = 71,
/* Some system file (e.g., /etc/passwd, /var/run/utx.active, etc.) does
* not exist, cannot be opened, or has some sort of error (e.g., syntax
* error). */
EX_OSFILE = 72,
/* A (user specified) output file cannot be created. */
EX_CANTCREAT = 73,
/* An error occurred while doing I/O on some file. */
EX_IOERR = 74,
/* Temporary failure, indicating something that is not really an error.
* In sendmail, this means that a mailer (e.g.) could not create a
* connection, and the request should be reattempted later. */
EX_TEMPFAIL = 75,
/* The remote system returned something that was "not possible" during a
* protocol exchange. */
EX_PROTOCOL = 76,
/* You did not have sufficient permission to perform the operation.
* This is not intended for file system problems, which should use
* EX_NOINPUT or EX_CANTCREAT, but rather for higher level permissions.
*/
EX_NOPERM = 77,
/* Something was found in an unconfigured or misconfigured state. */
EX_CONFIG = 78
};
#endif /* ifndef _SYSEXITS_H */

9
include/usefulmacros.h Normal file
View File

@ -0,0 +1,9 @@
/* #include <stddef.h>
* - Needed for NULL in NOARGVZERO
* #include <sysexits.h>
* - Needed for EX_OSERR in NOARGVZERO
* #include <unistd.h>
* - Needed for write(2) in NOARGVZERO */
#define ARRAYLEN(a) sizeof((a)) / sizeof(*(a))
#define NOARGVZERO(a) if(*(a) == NULL) { write(2, "argv[0] is NULL; OS error, exiting...\n", 38); return EX_OSERR; }