Changeset View
Changeset View
Standalone View
Standalone View
lib/libedit/chartype.h
/* $NetBSD: chartype.h,v 1.25 2016/03/07 00:05:20 christos Exp $ */ | /* $NetBSD: chartype.h,v 1.35 2017/05/22 19:16:25 christos Exp $ */ | ||||
/*- | /*- | ||||
* Copyright (c) 2009 The NetBSD Foundation, Inc. | * Copyright (c) 2009 The NetBSD Foundation, Inc. | ||||
* All rights reserved. | * All rights reserved. | ||||
* | * | ||||
* Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | ||||
* modification, are permitted provided that the following conditions | * modification, are permitted provided that the following conditions | ||||
* are met: | * are met: | ||||
Show All 16 Lines | |||||
* POSSIBILITY OF SUCH DAMAGE. | * POSSIBILITY OF SUCH DAMAGE. | ||||
* | * | ||||
* $FreeBSD$ | * $FreeBSD$ | ||||
*/ | */ | ||||
#ifndef _h_chartype_f | #ifndef _h_chartype_f | ||||
#define _h_chartype_f | #define _h_chartype_f | ||||
#ifdef WIDECHAR | |||||
/* Ideally we should also test the value of the define to see if it | /* Ideally we should also test the value of the define to see if it | ||||
* supports non-BMP code points without requiring UTF-16, but nothing | * supports non-BMP code points without requiring UTF-16, but nothing | ||||
* seems to actually advertise this properly, despite Unicode 3.1 having | * seems to actually advertise this properly, despite Unicode 3.1 having | ||||
* been around since 2001... */ | * been around since 2001... */ | ||||
#if !defined(__NetBSD__) && !defined(__sun) && !(defined(__APPLE__) && defined(__MACH__)) && !defined(__OpenBSD__) && !defined(__FreeBSD__) | #if !defined(__NetBSD__) && \ | ||||
!defined(__sun) && \ | |||||
!(defined(__APPLE__) && defined(__MACH__)) && \ | |||||
!defined(__OpenBSD__) && \ | |||||
!defined(__FreeBSD__) && \ | |||||
!defined(__DragonFly__) | |||||
#ifndef __STDC_ISO_10646__ | #ifndef __STDC_ISO_10646__ | ||||
/* In many places it is assumed that the first 127 code points are ASCII | /* In many places it is assumed that the first 127 code points are ASCII | ||||
* compatible, so ensure wchar_t indeed does ISO 10646 and not some other | * compatible, so ensure wchar_t indeed does ISO 10646 and not some other | ||||
* funky encoding that could break us in weird and wonderful ways. */ | * funky encoding that could break us in weird and wonderful ways. */ | ||||
#error wchar_t must store ISO 10646 characters | #error wchar_t must store ISO 10646 characters | ||||
#endif | #endif | ||||
#endif | #endif | ||||
/* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it... | /* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it... | ||||
* ref: ISO/IEC DTR 19769 | * ref: ISO/IEC DTR 19769 | ||||
*/ | */ | ||||
#if WCHAR_MAX < INT32_MAX | #if WCHAR_MAX < INT32_MAX | ||||
#warning Build environment does not support non-BMP characters | #warning Build environment does not support non-BMP characters | ||||
#endif | #endif | ||||
#define ct_wctob wctob | |||||
#define ct_wctomb wctomb | |||||
#define ct_wcrtomb wcrtomb | |||||
#define ct_wctomb_reset wctomb(0,0) | |||||
#define ct_wcstombs wcstombs | |||||
#define ct_mbstowcs mbstowcs | |||||
#define Char wchar_t | |||||
#define FUN(prefix,rest) prefix ## _w ## rest | |||||
#define FUNW(type) type ## _w | |||||
#define TYPE(type) type ## W | |||||
#define FSTR "%ls" | |||||
#define FSTARSTR "%.*ls" | |||||
#define STR(x) L ## x | |||||
#define UC(c) c | |||||
#define Isalpha(x) iswalpha(x) | |||||
#define Isalnum(x) iswalnum(x) | |||||
#define Isgraph(x) iswgraph(x) | |||||
#define Isspace(x) iswspace(x) | |||||
#define Isdigit(x) iswdigit(x) | |||||
#define Iscntrl(x) iswcntrl(x) | |||||
#define Isprint(x) iswprint(x) | |||||
#define Isupper(x) iswupper(x) | |||||
#define Islower(x) iswlower(x) | |||||
#define Toupper(x) towupper(x) | |||||
#define Tolower(x) towlower(x) | |||||
#define IsASCII(x) (x < 0x100) | |||||
#define Strlen(x) wcslen(x) | |||||
#define Strchr(s,c) wcschr(s,c) | |||||
#define Strrchr(s,c) wcsrchr(s,c) | |||||
#define Strstr(s,v) wcsstr(s,v) | |||||
#define Strdup(x) wcsdup(x) | |||||
#define Strcpy(d,s) wcscpy(d,s) | |||||
#define Strncpy(d,s,n) wcsncpy(d,s,n) | |||||
#define Strncat(d,s,n) wcsncat(d,s,n) | |||||
#define Strcmp(s,v) wcscmp(s,v) | |||||
#define Strncmp(s,v,n) wcsncmp(s,v,n) | |||||
#define Strcspn(s,r) wcscspn(s,r) | |||||
#define Strtol(p,e,b) wcstol(p,e,b) | |||||
static inline int | |||||
Width(wchar_t c) | |||||
{ | |||||
int w = wcwidth(c); | |||||
return w < 0 ? 0 : w; | |||||
} | |||||
#else /* NARROW */ | |||||
#define ct_wctob(w) ((int)(w)) | |||||
#define ct_wctomb error | |||||
#define ct_wcrtomb error | |||||
#define ct_wctomb_reset | |||||
#define ct_wcstombs(a, b, c) (strncpy(a, b, c), strlen(a)) | |||||
#define ct_mbstowcs(a, b, c) (strncpy(a, b, c), strlen(a)) | |||||
#define Char char | |||||
#define FUN(prefix,rest) prefix ## _ ## rest | |||||
#define FUNW(type) type | |||||
#define TYPE(type) type | |||||
#define FSTR "%s" | |||||
#define FSTARSTR "%.*s" | |||||
#define STR(x) x | |||||
#define UC(c) (unsigned char)(c) | |||||
#define Isalpha(x) isalpha((unsigned char)x) | |||||
#define Isalnum(x) isalnum((unsigned char)x) | |||||
#define Isgraph(x) isgraph((unsigned char)x) | |||||
#define Isspace(x) isspace((unsigned char)x) | |||||
#define Isdigit(x) isdigit((unsigned char)x) | |||||
#define Iscntrl(x) iscntrl((unsigned char)x) | |||||
#define Isprint(x) isprint((unsigned char)x) | |||||
#define Isupper(x) isupper((unsigned char)x) | |||||
#define Islower(x) islower((unsigned char)x) | |||||
#define Toupper(x) toupper((unsigned char)x) | |||||
#define Tolower(x) tolower((unsigned char)x) | |||||
#define IsASCII(x) isascii((unsigned char)x) | |||||
#define Strlen(x) strlen(x) | |||||
#define Strchr(s,c) strchr(s,c) | |||||
#define Strrchr(s,c) strrchr(s,c) | |||||
#define Strstr(s,v) strstr(s,v) | |||||
#define Strdup(x) strdup(x) | |||||
#define Strcpy(d,s) strcpy(d,s) | |||||
#define Strncpy(d,s,n) strncpy(d,s,n) | |||||
#define Strncat(d,s,n) strncat(d,s,n) | |||||
#define Strcmp(s,v) strcmp(s,v) | |||||
#define Strncmp(s,v,n) strncmp(s,v,n) | |||||
#define Strcspn(s,r) strcspn(s,r) | |||||
#define Strtol(p,e,b) strtol(p,e,b) | |||||
#define Width(c) 1 | |||||
#endif | |||||
#ifdef WIDECHAR | |||||
/* | /* | ||||
* Conversion buffer | * Conversion buffer | ||||
*/ | */ | ||||
typedef struct ct_buffer_t { | typedef struct ct_buffer_t { | ||||
char *cbuff; | char *cbuff; | ||||
size_t csize; | size_t csize; | ||||
Char *wbuff; | wchar_t *wbuff; | ||||
size_t wsize; | size_t wsize; | ||||
} ct_buffer_t; | } ct_buffer_t; | ||||
#define ct_encode_string __ct_encode_string | |||||
/* Encode a wide-character string and return the UTF-8 encoded result. */ | /* Encode a wide-character string and return the UTF-8 encoded result. */ | ||||
public char *ct_encode_string(const Char *, ct_buffer_t *); | char *ct_encode_string(const wchar_t *, ct_buffer_t *); | ||||
#define ct_decode_string __ct_decode_string | |||||
/* Decode a (multi)?byte string and return the wide-character string result. */ | /* Decode a (multi)?byte string and return the wide-character string result. */ | ||||
public Char *ct_decode_string(const char *, ct_buffer_t *); | wchar_t *ct_decode_string(const char *, ct_buffer_t *); | ||||
/* Decode a (multi)?byte argv string array. | /* Decode a (multi)?byte argv string array. | ||||
* The pointer returned must be free()d when done. */ | * The pointer returned must be free()d when done. */ | ||||
protected Char **ct_decode_argv(int, const char *[], ct_buffer_t *); | libedit_private wchar_t **ct_decode_argv(int, const char *[], ct_buffer_t *); | ||||
/* Resizes the conversion buffer(s) if needed. */ | /* Encode a character into the destination buffer, provided there is sufficient | ||||
protected int ct_conv_cbuff_resize(ct_buffer_t *, size_t); | |||||
protected int ct_conv_wbuff_resize(ct_buffer_t *, size_t); | |||||
protected ssize_t ct_encode_char(char *, size_t, Char); | |||||
protected size_t ct_enc_width(Char); | |||||
#define ct_free_argv(s) el_free(s) | |||||
#else | |||||
#define ct_encode_string(s, b) (s) | |||||
#define ct_decode_string(s, b) (s) | |||||
#define ct_decode_argv(l, s, b) (s) | |||||
#define ct_conv_cbuff_resize(b, s) ((s) == (0)) | |||||
#define ct_conv_wbuff_resize(b, s) ((s) == (0)) | |||||
#define ct_encode_char(d, l, s) (*d = s, 1) | |||||
#define ct_free_argv(s) | |||||
#endif | |||||
#ifndef NARROWCHAR | |||||
/* Encode a characted into the destination buffer, provided there is sufficient | |||||
* buffer space available. Returns the number of bytes used up (zero if the | * buffer space available. Returns the number of bytes used up (zero if the | ||||
* character cannot be encoded, -1 if there was not enough space available). */ | * character cannot be encoded, -1 if there was not enough space available). */ | ||||
libedit_private ssize_t ct_encode_char(char *, size_t, wchar_t); | |||||
libedit_private size_t ct_enc_width(wchar_t); | |||||
/* The maximum buffer size to hold the most unwieldy visual representation, | /* The maximum buffer size to hold the most unwieldy visual representation, | ||||
* in this case \U+nnnnn. */ | * in this case \U+nnnnn. */ | ||||
#define VISUAL_WIDTH_MAX ((size_t)8) | #define VISUAL_WIDTH_MAX ((size_t)8) | ||||
/* The terminal is thought of in terms of X columns by Y lines. In the cases | /* The terminal is thought of in terms of X columns by Y lines. In the cases | ||||
* where a wide character takes up more than one column, the adjacent | * where a wide character takes up more than one column, the adjacent | ||||
* occupied column entries will contain this faux character. */ | * occupied column entries will contain this faux character. */ | ||||
#define MB_FILL_CHAR ((Char)-1) | #define MB_FILL_CHAR ((wchar_t)-1) | ||||
/* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn | /* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn | ||||
* style visual expansions. */ | * style visual expansions. */ | ||||
protected int ct_visual_width(Char); | libedit_private int ct_visual_width(wchar_t); | ||||
/* Turn the given character into the appropriate visual format, matching | /* Turn the given character into the appropriate visual format, matching | ||||
* the width given by ct_visual_width(). Returns the number of characters used | * the width given by ct_visual_width(). Returns the number of characters used | ||||
* up, or -1 if insufficient space. Buffer length is in count of Char's. */ | * up, or -1 if insufficient space. Buffer length is in count of wchar_t's. */ | ||||
protected ssize_t ct_visual_char(Char *, size_t, Char); | libedit_private ssize_t ct_visual_char(wchar_t *, size_t, wchar_t); | ||||
/* Convert the given string into visual format, using the ct_visual_char() | /* Convert the given string into visual format, using the ct_visual_char() | ||||
* function. Uses a static buffer, so not threadsafe. */ | * function. Uses a static buffer, so not threadsafe. */ | ||||
protected const Char *ct_visual_string(const Char *); | libedit_private const wchar_t *ct_visual_string(const wchar_t *, ct_buffer_t *); | ||||
/* printable character, use ct_visual_width() to find out display width */ | /* printable character, use ct_visual_width() to find out display width */ | ||||
#define CHTYPE_PRINT ( 0) | #define CHTYPE_PRINT ( 0) | ||||
/* control character found inside the ASCII portion of the charset */ | /* control character found inside the ASCII portion of the charset */ | ||||
#define CHTYPE_ASCIICTL (-1) | #define CHTYPE_ASCIICTL (-1) | ||||
/* a \t */ | /* a \t */ | ||||
#define CHTYPE_TAB (-2) | #define CHTYPE_TAB (-2) | ||||
/* a \n */ | /* a \n */ | ||||
#define CHTYPE_NL (-3) | #define CHTYPE_NL (-3) | ||||
/* non-printable character */ | /* non-printable character */ | ||||
#define CHTYPE_NONPRINT (-4) | #define CHTYPE_NONPRINT (-4) | ||||
/* classification of character c, as one of the above defines */ | /* classification of character c, as one of the above defines */ | ||||
protected int ct_chr_class(Char c); | libedit_private int ct_chr_class(wchar_t c); | ||||
#endif | |||||
size_t ct_mbrtowc(wchar_t *, const char *, size_t); | |||||
#endif /* _chartype_f */ | #endif /* _chartype_f */ |