mescc: m4 support: WIP

This commit is contained in:
Jan Nieuwenhuizen 2018-05-27 10:28:30 +02:00
parent 58a0b97991
commit 559699969c
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
15 changed files with 433 additions and 34 deletions

View File

@ -28,7 +28,21 @@
#include_next <errno.h>
#else // ! (__GNUC__ && POSIX)
int errno;
#define ENOENT 2 /* No such file or directory */
#define EBADF 9 /* Bad file number */
#define ENOMEM 12 /* Out of memory */
#define EEXIST 17 /* File exists */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define EMFILE 24 /* Too many open files */
#define EPIPE 32 /* Broken pipe */
#define ERANGE 34 /* Math result not representable */
#define ENAMETOOLONG 36 /* File name too long */
#define ENOSYS 38 /* Invalid system call number */
#define ELOOP 40 /* Too many symbolic links encountered */
#endif // ! (__GNUC__ && POSIX)
#endif // __MES_ERRNO_H

View File

@ -32,12 +32,20 @@
#define O_WRONLY 1
#define O_RDWR 2
#define O_CREAT 64
#define O_EXCL 128
#define O_TRUNC 512
#define S_IRWXU 00700
#define S_IXUSR 00100
#define S_IWUSR 00200
#define S_IRUSR 00400
#define F_DUPFD 0
#define F_GETFD 1
#define F_SETFD 2
#define F_GETFL 3
#define F_SETFL 4
int open (char const *s, int flags, ...);
#endif // ! (__GNUC__ && POSIX)

View File

@ -24,6 +24,11 @@
#undef __MES_FLOAT_H
#include_next <float.h>
#else // ! (__GNUC__ && POSIX)
#define MIN_EXP -1021
#define DBL_MIN_EXP -1021
#define LDBL_MIN_EXP -1021
#endif // ! (__GNUC__ && POSIX)
#endif // __MES_FLOAT_H

View File

@ -28,8 +28,12 @@
#include_next <limits.h>
#else // ! (__GNUC__ && POSIX)
#define CHAR_BIT 8
#define UCHAR_MAX 255
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define MB_CUR_MAX 1
#endif // ! (__GNUC__ && POSIX)
#endif // __MES_LIMITS_H

View File

@ -28,8 +28,16 @@ typedef int sigset_t;
typedef int stack_t;
#ifndef __MES_PID_T
#define __MES_PID_T
typedef int pid_t;
#endif
#ifndef __MES_UID_T
#define __MES_UID_T
typedef int uid_t;
#endif
typedef int clock_t;
typedef int sigval_t;
@ -116,14 +124,20 @@ typedef struct siginfo_t {
// typedef void __signalfn_t(int);
// typedef __signalfn_t *__sighandler_t;
// typedef __signalfn_t *__sighandler_t;
typedef void __sighandler_t(int);
struct sigaction {
void (*sa_sigaction) (int, siginfo_t *, void *);
//__sighandler_t sa_handler;
void (*sa_handler) (int);
unsigned long sa_flags;
sigset_t sa_mask;
};
#define SIG_DFL ((__sighandler_t)0) /* default signal handling */
#define SIG_IGN ((__sighandler_t)1) /* ignore signal */
#define SIG_ERR ((__sighandler_t)-1) /* error return from signal */
#ifdef __i386__
@ -206,4 +220,3 @@ int sigemptyset (sigset_t *set);
#endif //! (__GNUC__ && POSIX)
#endif // __MES_SIGNAL_H

View File

@ -26,6 +26,15 @@
#else // ! (__GNUC__ && POSIX)
#include <stdint.h>
#include <unistd.h>
#ifndef offsetof
#if __MESC__
#define offsetof(type, field) (&((type *)0)->field)
#else // !__MESC__
#define offsetof(type, field) ((size_t)&((type *)0)->field)
#endif // !__MESC__
#endif // offsetof
#endif // ! (__GNUC__ && POSIX)
#endif // __MES_STDDEF_H

View File

@ -87,8 +87,8 @@ int fdungetc (int c, int fd);
int fdputs (char const* s, int fd);
#endif // __MES_GNUC__
#define fputs fdputs
#define fputc fdputc
//#define fputs fdputs
//#define fputc fdputc
typedef int FILE;
@ -98,16 +98,27 @@ typedef int FILE;
typedef unsigned long size_t;
#endif
int getc (FILE *stream);
int fputc (int c, FILE* stream);
int fdputs (char const* s, int fd);
int fputs (char const* s, FILE *stream);
int putc (int c, FILE* stream);
//int putc (int c, int fd);
int eputc (int c);
int eputs (char const* s);
int fclose (FILE *stream);
FILE *fdopen (int fd, char const *mode);
int fflush (FILE *stream);
FILE *fopen (char const *file_name, char const *mode);
int ferror (FILE *stream);
int fprintf (FILE *stream, char const *format, ...);
int fdputc (int c, int fd);
int fflush (FILE *stream);
int ferror (FILE *stream);
FILE *fopen (char const *file_name, char const *mode);
int fpurge (FILE *stream);
//void __fpurge (FILE *stream);
int fprintf (FILE *stream, char const *format, ...);
size_t fread (void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t __freadahead (FILE *fp);
//size_t freadahead (FILE *fp);
int fseek (FILE *stream, long offset, int whence);
long ftell (FILE *stream);
size_t fwrite (void const *ptr, size_t size, size_t nmemb, FILE *stream);

View File

@ -18,7 +18,7 @@
* along with Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __MES_SYS_STAT_H
#define __MES_SYS_STAT_H 1
#define __MES_SYS_STAT_H 1lei
#if __GNUC__ && POSIX
#undef __MES_SYS_STAT_H
@ -33,23 +33,30 @@ typedef int mode_t;
int chmod (char const *file_name, mode_t mode);
struct stat {
int st_dev;
int st_ino;
int st_mode;
int st_nlink;
int st_uid;
int st_gid;
int st_rdev;
int st_size;
int st_blksize;
int st_blocks;
int st_atime;
int st_mtime;
int st_ctime;
struct stat
{
unsigned long st_dev; /* Device. */
unsigned long st_ino; /* File serial number. */
unsigned int st_mode; /* File mode. */
unsigned int st_nlink; /* Link count. */
unsigned int st_uid; /* User ID of the file's owner. */
unsigned int st_gid; /* Group ID of the file's group. */
unsigned long st_rdev; /* Device number, if device. */
unsigned long __pad1;
long st_size; /* Size of file, in bytes. */
int st_blksize; /* Optimal block size for I/O. */
int __pad2;
long st_blocks; /* Number 512-byte blocks allocated. */
long st_atime; /* Time of last access. */
unsigned long st_atime_nsec;
long st_mtime; /* Time of last modification. */
unsigned long st_mtime_nsec;
long st_ctime; /* Time of last status change. */
unsigned long st_ctime_nsec;
unsigned int __unused4;
unsigned int __unused5;
};
#endif // !(__GNUC__ && POSIX)
#endif // __MES_SYS_STAT_H

View File

@ -32,6 +32,16 @@
typedef unsigned long size_t;
#endif
#ifndef __MES_PID_T
#define __MES_PID_T
typedef int pid_t;
#endif
#ifndef __MES_UID_T
#define __MES_UID_T
typedef int uid_t;
#endif
#endif // ! (__GNUC__ && POSIX)
#endif // __MES_SYS_TYPES_H

View File

@ -41,7 +41,24 @@ struct tm {
struct tm *localtime (time_t const *timep);
time_t time (time_t *tloc);
#ifndef __MES_STRUCT_TIMESPEC
#define __MES_STRUCT_TIMESPEC
#ifndef __kernel_long_t
typedef long __kernel_long_t;
typedef unsigned long __kernel_ulong_t;
#endif
typedef __kernel_long_t __kernel_time_t;
struct timespec
{
__kernel_time_t tv_sec;
long tv_nsec;
};
#endif // __MES_STRUCT_TIMESPEC
#endif // ! (__GNUC__ && POSIX)
#endif // __MES_TIME_H

View File

@ -150,4 +150,7 @@ __fixsfdi (double a1)
eputs ("__fixsfdi stub\n");
return 0;
}
#include <m4.c>
#endif // !POSIX

View File

@ -87,10 +87,15 @@ fprintf (FILE *stream, char const *format, ...)
}
size_t
fread (void *ptr, size_t size, size_t nmemb, FILE *stream)
fread (void *data, size_t size, size_t count, FILE *stream)
{
eputs ("fread stub\n");
return 0;
if (! size || !count)
return 0;
int bytes = read ((int)stream, data, size * count);
if (bytes > 0)
return bytes/size;
return bytes;
}
int
@ -107,13 +112,6 @@ ftell (FILE *stream)
return 0;
}
size_t
fwrite (void const *ptr, size_t size, size_t nmemb, FILE *stream)
{
int fd = (int)stream;
return write (fd, ptr, size * nmemb);
}
int
gettimeofday (struct timeval *tv, struct timezone *tz)
{

View File

@ -175,6 +175,24 @@ eputc (int c)
return fdputc (c, STDERR);
}
int
fputc (int c, FILE* stream)
{
return fdputc (c, (int)stream);
}
int
fputs (char const* s, FILE* stream)
{
return fdputs (s, (int)stream);
}
int
putc (int c, FILE* stream)
{
return fdputc (c, (int)stream);
}
FILE*
fopen (char const* file_name, char const* mode)
{
@ -331,6 +349,18 @@ strncmp (char const* a, char const* b, size_t length)
return *a - *b;
}
size_t
fwrite (void const *data, size_t size, size_t count, FILE *stream)
{
if (! size || !count)
return 0;
int bytes = write ((int)stream, data, size * count);
if (bytes > 0)
//return bytes/size;
return count;
return bytes;
}
char *
getenv (char const* s)
{

View File

@ -27,6 +27,8 @@
#define SYS_lseek "0x13"
#define SYS_access "0x21"
#define SYS_brk "0x2d"
#define SYS_stat "0x6a"
#define SYS_fstat "0x6c"
#define SYS_fsync "0x76"
#define SYS_getcwd "0xb7"
@ -84,6 +86,50 @@ lseek (int fd, off_t offset, int whence)
#endif
}
int
stat (char const *file_name, struct stat *statbuf)
{
#if !__TINYC__
int r;
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov $"SYS_stat",%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (file_name), "" (statbuf)
: "eax", "ebx", "ecx"
);
return r;
#endif
}
int
fstat (int fd, struct stat *statbuf)
{
#if !__TINYC__
int r;
asm (
"mov %1,%%ebx\n\t"
"mov %2,%%ecx\n\t"
"mov $"SYS_fstat",%%eax\n\t"
//"mov $"SYS_oldfstat",%%eax\n\t"
"int $0x80\n\t"
"mov %%eax,%0\n\t"
: "=r" (r)
: "" (fd), "" (statbuf)
: "eax", "ebx", "ecx"
);
return r;
#endif
}
char *
getcwd (char *buf, size_t size)
{

224
lib/m4.c Normal file
View File

@ -0,0 +1,224 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of Mes.
*
* Mes 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.
*
* Mes 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 Mes. If not, see <http://www.gnu.org/licenses/>.
*/
int
abort (int x)
{
eputs ("abort stub\n");
return 0;
}
int
atof (int x)
{
eputs ("atof stub\n");
return 0;
}
int
atol (int x)
{
eputs ("atol stub\n");
return 0;
}
int
bcmp (int x)
{
eputs ("bcmp stub\n");
return 0;
}
void
bcopy (void const *src, void *dest, size_t n)
{
return memmove (dest, src, n);
}
int
bzero (int x)
{
eputs ("bzero stub\n");
return 0;
}
int
fileno (FILE *stream)
{
return (int)stream;
}
// void
// __fpurge (FILE *stream)
// {
// eputs ("__fpurge stub\n");
// return 0;
// }
int
fpurge (FILE *stream)
{
eputs ("fpurge stub\n");
return 0;
}
// size_t
// __freadahead (FILE *fp)
// {
// eputs ("__freadahead stub\n");
// return 0;
// }
size_t
freadahead (FILE *fp)
{
eputs ("freadahead stub\n");
return 0;
}
int
getc (FILE *stream)
{
return fgetc ((int)stream);
}
int
index (char const *s, int c)
{
return strchr (s, c);
}
int
isalnum (int c)
{
return isdigit (c) || isalpha (c);
}
int
isalpha (int c)
{
return islower (c) || isupper (c);
}
int
iscntrl (int c)
{
return c >= 0 && c < 32;
}
int
islower (int c)
{
return c >= 'a' && c <= 'z';
}
int
isprint (int c)
{
return c >= 32 && c < 127;
}
int
ispunct (int c)
{
return isprint (c) && !isspace (c) && !isalnum (c);
}
int
isupper (int c)
{
return c >= 'A' && c <= 'Z';
}
int
mktemp (int x)
{
eputs ("mktemp stub\n");
return 0;
}
int
pclose (int x)
{
eputs ("pclose stub\n");
return 0;
}
int
popen (int x)
{
eputs ("popen stub\n");
return 0;
}
int
rindex (char const *s, int c)
{
return strrchr (s, c);
}
int
rewind (int x)
{
eputs ("rewind stub\n");
return 0;
}
int
setbuf (int x)
{
eputs ("setbuf stub\n");
return 0;
}
int
signal (int x)
{
eputs ("signal stub\n");
return 0;
}
int
sys_errlist (int x)
{
eputs ("sys_errlist stub\n");
return 0;
}
int
sys_nerr (int x)
{
eputs ("sys_nerr stub\n");
return 0;
}
int
system (int x)
{
eputs ("system stub\n");
return 0;
}
int
tolower (int x)
{
eputs ("tolower stub\n");
return 0;
}