Skip to content

Commit

Permalink
port of wchar support from 64tass project [#11]
Browse files Browse the repository at this point in the history
  • Loading branch information
stsp committed Oct 19, 2024
1 parent ecf2c14 commit c4291b7
Show file tree
Hide file tree
Showing 6 changed files with 1,151 additions and 50 deletions.
55 changes: 55 additions & 0 deletions include/_wchar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Copyright (C) 2012 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 2003 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */
#ifndef __dj_include_wchar_h_
#define __dj_include_wchar_h_

/* Bare bones header to satisfy SGI STL's basic_string<> */

#ifdef __cplusplus
extern "C" {
#endif

#ifndef __dj_ENFORCE_ANSI_FREESTANDING

#include <sys/djtypes.h>
#include <stddef.h>

#ifndef _WINT_T
__DJ_wint_t
#define _WINT_T
#endif

#ifndef WEOF
#define WEOF ((wint_t)(-1))
#endif

typedef struct
{
int shift_state;
} mbstate_t;

#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) \
|| !defined(__STRICT_ANSI__) || defined(__cplusplus)

#endif /* (__STDC_VERSION__ >= 199901L) || !__STRICT_ANSI__ */

#ifndef __STRICT_ANSI__

#ifndef _POSIX_SOURCE

#endif /* !_POSIX_SOURCE */
#endif /* !__STRICT_ANSI__ */
#endif /* !__dj_ENFORCE_ANSI_FREESTANDING */

#ifndef __dj_ENFORCE_FUNCTION_CALLS
#endif /* !__dj_ENFORCE_FUNCTION_CALLS */

#ifdef __cplusplus
}
#endif

#endif /* !__dj_include_wchar_h_ */

90 changes: 41 additions & 49 deletions include/wchar.h
Original file line number Diff line number Diff line change
@@ -1,55 +1,47 @@
/* Copyright (C) 2012 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 2003 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */
#ifndef __dj_include_wchar_h_
#define __dj_include_wchar_h_

/* Bare bones header to satisfy SGI STL's basic_string<> */

#ifdef __cplusplus
extern "C" {
#endif

#ifndef __dj_ENFORCE_ANSI_FREESTANDING

#include <sys/djtypes.h>
#include <stddef.h>

#ifndef _WINT_T
__DJ_wint_t
#define _WINT_T
#endif

#ifndef WEOF
#define WEOF ((wint_t)(-1))
#endif

typedef struct
{
/*
$Id: wchar.h 2966 2023-01-08 21:28:13Z soci $
This program 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 2 of the License, or
(at your option) any later version.
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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef WCHAR_H
#define WCHAR_H
#include "inttypes.h"

#if defined __DJGPP__ || (defined __WATCOMC__ && !defined _WIN32)
#include <_wchar.h>
extern size_t wcrtomb(char *, wchar_t, mbstate_t *);
extern size_t mbrtowc(wchar_t *, const char *, size_t, mbstate_t *);
typedef uint32_t unichar_t;
#elif defined __GNUC__ || defined _MSC_VER || defined __WATCOMC__
#include <wchar.h>
#elif __STDC_VERSION__ >= 199901L && !defined __VBCC__
#include <wchar.h>
#else
typedef uint32_t wint_t;

typedef struct {
int shift_state;
} mbstate_t;

#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) \
|| !defined(__STRICT_ANSI__) || defined(__cplusplus)

#endif /* (__STDC_VERSION__ >= 199901L) || !__STRICT_ANSI__ */

#ifndef __STRICT_ANSI__

#ifndef _POSIX_SOURCE

#endif /* !_POSIX_SOURCE */
#endif /* !__STRICT_ANSI__ */
#endif /* !__dj_ENFORCE_ANSI_FREESTANDING */

#ifndef __dj_ENFORCE_FUNCTION_CALLS
#endif /* !__dj_ENFORCE_FUNCTION_CALLS */

#ifdef __cplusplus
}
extern size_t wcrtomb(char *, wchar_t, mbstate_t *);
extern size_t mbrtowc(wchar_t *, const char *, size_t, mbstate_t *);
#endif

#endif /* !__dj_include_wchar_h_ */
extern int wcwidth_v13(unichar_t);
extern int isprint_v13(unichar_t);

#endif
117 changes: 117 additions & 0 deletions src/libc/posix/wchar/attributes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
$Id: attributes.h 2673 2021-05-16 05:10:23Z soci $
This program 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 2 of the License, or
(at your option) any later version.
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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef ATTRIBUTES_H
#define ATTRIBUTES_H

#ifdef __has_attribute
#elif defined __GNUC__
# define __has_attribute(a) __has_attribute ## a
# define __has_attribute__unused__ (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
# define __has_attribute__warn_unused_result__ (__GNUC__ >= 4)
# define __has_attribute__malloc__ (__GNUC__ >= 3)
# define __has_attribute__noreturn__ (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95))
# define __has_attribute__regparm__ (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95))
# define __has_attribute__noinline__ (__GNUC__ >= 3)
# define __has_attribute__fallthrough__ (__GNUC__ >= 7)
#else
# define __has_attribute(a) (0)
#endif

#if __has_attribute(__unused__)
# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#else
# define UNUSED(x) x
#endif

#if __has_attribute(__warn_unused_result__)
# define MUST_CHECK __attribute__((__warn_unused_result__))
#else
# define MUST_CHECK
#endif

#if __has_attribute(__malloc__)
# if __has_attribute(__warn_unused_result__)
# define MALLOC __attribute__((__malloc__,__warn_unused_result__))
# else
# define MALLOC __attribute__((__malloc__))
# endif
#elif __has_attribute(__warn_unused_result__)
# define MALLOC __attribute__((__warn_unused_result__))
#else
# define MALLOC
#endif

#if __has_attribute(__noreturn__)
# define NO_RETURN __attribute__((__noreturn__))
#else
# define NO_RETURN
#endif

#if __has_attribute(__regparm__) && defined __i386__
# define FAST_CALL __attribute__((__regparm__(3)))
#else
# define FAST_CALL
#endif

#if __has_attribute(__noinline__)
# define NO_INLINE __attribute__((__noinline__))
#else
# define NO_INLINE
#endif

#if defined __cplusplus || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
#elif defined __GNUC__ || (defined _MSC_VER && _MSC_VER >= 900)
# define inline __inline
#else
# define inline
#endif

#if __has_attribute(__fallthrough__) && (!defined __clang__ || __clang_major__ >= 5)
# define FALL_THROUGH __attribute__((__fallthrough__))
#else
# define FALL_THROUGH do {} while (false)
#endif

#ifdef __has_builtin
#elif defined __GNUC__
# define __has_builtin(a) __has_builtin ## a
# define __has_builtin__builtin_add_overflow (__GNUC__ > 5)
# define __has_builtin__builtin_mul_overflow (__GNUC__ > 5)
# define __has_builtin__builtin_expect (__GNUC__ > 2)
#else
# define __has_builtin(a) (0)
#endif

#if __has_builtin(__builtin_add_overflow)
# define add_overflow(a, b, c) __builtin_add_overflow(a, b, c)
# define inc_overflow(a, b) __builtin_add_overflow(*a, b, a)
#else
# define add_overflow(a, b, c) ((*(c) = (a) + (b)) < (b))
# define inc_overflow(a, b) ((*(a) = *(a) + (b)) < (b))
#endif

#if __has_builtin(__builtin_expect)
# define likely(a) (__builtin_expect(!!(a), 1))
# define unlikely(a) (__builtin_expect(!!(a), 0))
#else
# define likely(a) (a)
# define unlikely(a) (a)
#endif

#endif
6 changes: 6 additions & 0 deletions src/libc/posix/wchar/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details
TOP=../..

SRC += wchar.c

include $(TOP)/../makefile.inc
Loading

0 comments on commit c4291b7

Please sign in to comment.