mirror of
https://github.com/idris-lang/Idris2.git
synced 2024-11-24 06:52:19 +03:00
Add missing C support
This commit is contained in:
parent
0db7e883d2
commit
80450bbc8e
@ -3,5 +3,6 @@ Self-hosted Idris 2
|
||||
|
||||
See `INSTALL.md` for installation instructions.
|
||||
|
||||
This repository might move or be renamed or anything at no notice. It will
|
||||
eventually turn into the real Idris 2 repository, one way or another.
|
||||
There are no guarantees: This repository might move or be renamed or deleted or
|
||||
anything at no notice. It will eventually turn into the real Idris 2
|
||||
repository, one way or another.
|
||||
|
48
support/c/Makefile
Normal file
48
support/c/Makefile
Normal file
@ -0,0 +1,48 @@
|
||||
include $(IDRIS2_CURDIR)/config.mk
|
||||
|
||||
TARGET = libidris2_support
|
||||
|
||||
LIBTARGET = $(TARGET).a
|
||||
DYLIBTARGET = $(TARGET)$(SHLIB_SUFFIX)
|
||||
|
||||
CFLAGS += -O2
|
||||
|
||||
SRCS = $(wildcard *.c)
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
DEPS = $(OBJS:.o=.d)
|
||||
|
||||
|
||||
all: build
|
||||
|
||||
.PHONY: build
|
||||
|
||||
build: $(LIBTARGET) $(DYLIBTARGET)
|
||||
|
||||
$(LIBTARGET): $(OBJS)
|
||||
$(AR) rc $@ $^
|
||||
$(RANLIB) $@
|
||||
|
||||
$(DYLIBTARGET): $(OBJS)
|
||||
$(CC) -shared $(LDFLAGS) -o $@ $^
|
||||
|
||||
|
||||
-include $(DEPS)
|
||||
|
||||
%.d: %.c
|
||||
@$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@
|
||||
|
||||
|
||||
.PHONY: clean
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(DYLIBTARGET) $(LIBTARGET)
|
||||
|
||||
cleandep: clean
|
||||
rm -f $(DEPS)
|
||||
|
||||
|
||||
.PHONY: install
|
||||
|
||||
install: build
|
||||
mkdir -p ${PREFIX}/idris2-${IDRIS2_VERSION}/lib
|
||||
install $(LIBTARGET) $(DYLIBTARGET) ${PREFIX}/idris2-${IDRIS2_VERSION}/lib
|
82
support/c/getline.c
Normal file
82
support/c/getline.c
Normal file
@ -0,0 +1,82 @@
|
||||
/* $NetBSD: fgetln.c,v 1.9 2008/04/29 06:53:03 martin Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2011 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Christos Zoulas.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
ssize_t
|
||||
getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
|
||||
{
|
||||
char *ptr, *eptr;
|
||||
|
||||
if (*buf == NULL || *bufsiz == 0) {
|
||||
*bufsiz = BUFSIZ;
|
||||
if ((*buf = malloc(*bufsiz)) == NULL)
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (ptr = *buf, eptr = *buf + *bufsiz;;) {
|
||||
int c = fgetc(fp);
|
||||
if (c == -1) {
|
||||
if (feof(fp)) {
|
||||
*ptr = '\0';
|
||||
return ptr - *buf;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
*ptr++ = c;
|
||||
if (c == delimiter) {
|
||||
*ptr = '\0';
|
||||
return ptr - *buf;
|
||||
}
|
||||
if (ptr + 2 >= eptr) {
|
||||
char *nbuf;
|
||||
size_t nbufsiz = *bufsiz * 2;
|
||||
ssize_t d = ptr - *buf;
|
||||
if ((nbuf = realloc(*buf, nbufsiz)) == NULL)
|
||||
return -1;
|
||||
*buf = nbuf;
|
||||
*bufsiz = nbufsiz;
|
||||
eptr = nbuf + nbufsiz;
|
||||
ptr = nbuf + d;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t
|
||||
getline(char **buf, size_t *bufsiz, FILE *fp)
|
||||
{
|
||||
return getdelim(buf, bufsiz, '\n', fp);
|
||||
}
|
7
support/c/getline.h
Normal file
7
support/c/getline.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef GETLINE_H
|
||||
#define GETLINE_H
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
ssize_t getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp);
|
||||
ssize_t getline(char **buf, size_t *bufsiz, FILE *fp);
|
||||
#endif // GETLINE_H
|
187
support/c/idris_buffer.c
Normal file
187
support/c/idris_buffer.c
Normal file
@ -0,0 +1,187 @@
|
||||
#include "idris_buffer.h"
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
int size;
|
||||
uint8_t data[0];
|
||||
} Buffer;
|
||||
|
||||
void* idris2_newBuffer(int bytes) {
|
||||
size_t size = sizeof(Buffer) + bytes*sizeof(uint8_t);
|
||||
|
||||
Buffer* buf = malloc(size);
|
||||
if (buf == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buf->size = bytes;
|
||||
memset(buf->data, 0, bytes);
|
||||
|
||||
return (void*)buf;
|
||||
}
|
||||
|
||||
void idris2_freeBuffer(void* buf) {
|
||||
free(buf);
|
||||
}
|
||||
|
||||
void idris2_copyBuffer(void* from, int start, int len,
|
||||
void* to, int loc) {
|
||||
Buffer* bfrom = from;
|
||||
Buffer* bto = to;
|
||||
|
||||
if (loc >= 0 && loc+len <= bto->size) {
|
||||
memcpy(bto->data + loc, bfrom->data + start, len);
|
||||
}
|
||||
}
|
||||
|
||||
int idris2_getBufferSize(void* buffer) {
|
||||
return ((Buffer*)buffer)->size;
|
||||
}
|
||||
|
||||
void idris2_setBufferByte(void* buffer, int loc, int byte) {
|
||||
Buffer* b = buffer;
|
||||
if (loc >= 0 && loc < b->size) {
|
||||
b->data[loc] = byte;
|
||||
}
|
||||
}
|
||||
|
||||
void idris2_setBufferInt(void* buffer, int loc, int val) {
|
||||
Buffer* b = buffer;
|
||||
if (loc >= 0 && loc+3 < b->size) {
|
||||
b->data[loc] = val & 0xff;
|
||||
b->data[loc+1] = (val >> 8) & 0xff;
|
||||
b->data[loc+2] = (val >> 16) & 0xff;
|
||||
b->data[loc+3] = (val >> 24) & 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
void idris2_setBufferDouble(void* buffer, int loc, double val) {
|
||||
Buffer* b = buffer;
|
||||
// I am not proud of this
|
||||
if (loc >= 0 && loc + sizeof(double) <= b->size) {
|
||||
unsigned char* c = (unsigned char*)(& val);
|
||||
int i;
|
||||
for (i = 0; i < sizeof(double); ++i) {
|
||||
b->data[loc+i] = c[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void idris2_setBufferString(void* buffer, int loc, char* str) {
|
||||
Buffer* b = buffer;
|
||||
int len = strlen(str);
|
||||
|
||||
if (loc >= 0 && loc+len <= b->size) {
|
||||
memcpy((b->data)+loc, str, len);
|
||||
}
|
||||
}
|
||||
|
||||
int idris2_getBufferByte(void* buffer, int loc) {
|
||||
Buffer* b = buffer;
|
||||
if (loc >= 0 && loc < b->size) {
|
||||
return b->data[loc];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int idris2_getBufferInt(void* buffer, int loc) {
|
||||
Buffer* b = buffer;
|
||||
if (loc >= 0 && loc+3 < b->size) {
|
||||
return b->data[loc] +
|
||||
(b->data[loc+1] << 8) +
|
||||
(b->data[loc+2] << 16) +
|
||||
(b->data[loc+3] << 24);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
double idris2_getBufferDouble(void* buffer, int loc) {
|
||||
Buffer* b = buffer;
|
||||
double d;
|
||||
// I am even less proud of this
|
||||
unsigned char *c = (unsigned char*)(& d);
|
||||
if (loc >= 0 && loc + sizeof(double) <= b->size) {
|
||||
int i;
|
||||
for (i = 0; i < sizeof(double); ++i) {
|
||||
c[i] = b->data[loc+i];
|
||||
}
|
||||
return d;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
char* idris2_getBufferString(void* buffer, int loc, int len) {
|
||||
Buffer* b = buffer;
|
||||
char * s = (char*)(b->data + loc);
|
||||
char * rs = malloc(len + 1);
|
||||
strncpy(rs, s, len);
|
||||
rs[len] = '\0';
|
||||
return rs;
|
||||
}
|
||||
|
||||
void* idris2_readBufferFromFile(char* fn) {
|
||||
FILE* f = fopen(fn, "r");
|
||||
if (f == NULL) { return NULL; }
|
||||
|
||||
int fd = fileno(f);
|
||||
int len;
|
||||
|
||||
struct stat fbuf;
|
||||
if (fstat(fd, &fbuf) == 0) {
|
||||
len = (int)(fbuf.st_size);
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t size = sizeof(Buffer) + len*sizeof(uint8_t);
|
||||
Buffer* buf = malloc(size);
|
||||
buf->size = len;
|
||||
|
||||
fread((buf->data), sizeof(uint8_t), (size_t)len, f);
|
||||
fclose(f);
|
||||
return buf;
|
||||
}
|
||||
|
||||
int idris2_writeBufferToFile(char* fn, void* buffer, int max) {
|
||||
Buffer* b = buffer;
|
||||
FILE* f = fopen(fn, "w");
|
||||
if (f == NULL) { return 0; }
|
||||
|
||||
fwrite((b->data), sizeof(uint8_t), max, f);
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// To be added when the file API has moved to the C support libs
|
||||
/*
|
||||
int idris2_readBuffer(FILE* h, void* buffer, int loc, int max) {
|
||||
Buffer* b = buffer;
|
||||
size_t len;
|
||||
|
||||
if (loc >= 0 && loc < b->size) {
|
||||
if (loc + max > b->size) {
|
||||
max = b->size - loc;
|
||||
}
|
||||
len = fread((b->data)+loc, sizeof(uint8_t), (size_t)max, h);
|
||||
return len;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void idris2_writeBuffer(FILE* h, void* buffer, int loc, int len) {
|
||||
Buffer* b = buffer;
|
||||
|
||||
if (loc >= 0 && loc < b->size) {
|
||||
if (loc + len > b->size) {
|
||||
len = b->size - loc;
|
||||
}
|
||||
fwrite((b->data)+loc, sizeof(uint8_t), len, h);
|
||||
}
|
||||
}
|
||||
*/
|
33
support/c/idris_buffer.h
Normal file
33
support/c/idris_buffer.h
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef __IDRIS_BUFFER_H
|
||||
#define __IDRIS_BUFFER_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void* idris2_newBuffer(int bytes);
|
||||
void idris2_freeBuffer(void* buf);
|
||||
|
||||
int idris2_getBufferSize(void* buffer);
|
||||
|
||||
void idris2_setBufferByte(void* buffer, int loc, int byte);
|
||||
void idris2_setBufferInt(void* buffer, int loc, int val);
|
||||
void idris2_setBufferDouble(void* buffer, int loc, double val);
|
||||
void idris2_setBufferString(void* buffer, int loc, char* str);
|
||||
|
||||
void idris2_copyBuffer(void* from, int start, int len,
|
||||
void* to, int loc);
|
||||
|
||||
void* idris2_readBufferFromFile(char* fn);
|
||||
int idris2_writeBufferToFile(char* fn, void* buffer, int max);
|
||||
|
||||
// To be added when the file API has moved to the C support libs
|
||||
// int idris2_readBuffer(FILE* h, void* buffer, int loc, int max);
|
||||
// void idris2_writeBuffer(FILE* h, void* buffer, int loc, int len);
|
||||
|
||||
int idris2_getBufferByte(void* buffer, int loc);
|
||||
int idris2_getBufferInt(void* buffer, int loc);
|
||||
double idris2_getBufferDouble(void* buffer, int loc);
|
||||
char* idris2_getBufferString(void* buffer, int loc, int len);
|
||||
|
||||
#endif
|
64
support/c/idris_directory.c
Normal file
64
support/c/idris_directory.c
Normal file
@ -0,0 +1,64 @@
|
||||
#include "idris_directory.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
char* idris2_currentDirectory() {
|
||||
char* cwd = malloc(1024); // probably ought to deal with the unlikely event of this being too small
|
||||
getcwd(cwd, 1024);
|
||||
return cwd; // freed by RTS
|
||||
}
|
||||
|
||||
int idris2_changeDir(char* dir) {
|
||||
return chdir(dir);
|
||||
}
|
||||
|
||||
int idris2_createDir(char* dir) {
|
||||
#ifdef _WIN32
|
||||
return mkdir(dir);
|
||||
#else
|
||||
return mkdir(dir, S_IRWXU | S_IRGRP | S_IROTH);
|
||||
#endif
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
DIR* dirptr;
|
||||
int error;
|
||||
} DirInfo;
|
||||
|
||||
void* idris2_dirOpen(char* dir) {
|
||||
DIR *d = opendir(dir);
|
||||
if (d == NULL) {
|
||||
return NULL;
|
||||
} else {
|
||||
DirInfo* di = malloc(sizeof(DirInfo));
|
||||
di->dirptr = d;
|
||||
di->error = 0;
|
||||
|
||||
return (void*)di;
|
||||
}
|
||||
}
|
||||
|
||||
void idris2_dirClose(void* d) {
|
||||
DirInfo* di = (DirInfo*)d;
|
||||
|
||||
closedir(di->dirptr);
|
||||
free(di);
|
||||
}
|
||||
|
||||
char* idris2_nextDirEntry(void* d) {
|
||||
DirInfo* di = (DirInfo*)d;
|
||||
struct dirent* de = readdir(di->dirptr);
|
||||
|
||||
if (de == NULL) {
|
||||
di->error = -1;
|
||||
return NULL;
|
||||
} else {
|
||||
return de->d_name;
|
||||
}
|
||||
}
|
||||
|
||||
|
11
support/c/idris_directory.h
Normal file
11
support/c/idris_directory.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef __IDRIS_DIRECTORY_H
|
||||
#define __IDRIS_DIRECTORY_H
|
||||
|
||||
char* idris2_currentDirectory();
|
||||
int idris2_changeDir(char* dir);
|
||||
int idris2_createDir(char* dir);
|
||||
void* idris2_dirOpen(char* dir);
|
||||
void idris2_dirClose(void* d);
|
||||
char* idris2_nextDirEntry(void* d);
|
||||
|
||||
#endif
|
164
support/c/idris_file.c
Normal file
164
support/c/idris_file.c
Normal file
@ -0,0 +1,164 @@
|
||||
#include "getline.h"
|
||||
#include "idris_file.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "windows/win_utils.h"
|
||||
#else
|
||||
#include <sys/select.h>
|
||||
#endif
|
||||
|
||||
FILE* idris2_openFile(char* name, char* mode) {
|
||||
#ifdef _WIN32
|
||||
FILE *f = win32_u8fopen(name, mode);
|
||||
#else
|
||||
FILE *f = fopen(name, mode);
|
||||
#endif
|
||||
return (void *)f;
|
||||
}
|
||||
|
||||
void idris2_closeFile(FILE* f) {
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
int idris2_fileError(FILE* f) {
|
||||
return ferror(f);
|
||||
}
|
||||
|
||||
int idris2_fileErrno() {
|
||||
switch(errno) {
|
||||
case ENOENT:
|
||||
return 2;
|
||||
case EACCES:
|
||||
return 3;
|
||||
case EEXIST:
|
||||
return 4;
|
||||
default:
|
||||
return (errno + 5);
|
||||
}
|
||||
}
|
||||
|
||||
int idris2_fileRemove(const char *filename) {
|
||||
return remove(filename);
|
||||
}
|
||||
|
||||
int idris2_fileSize(FILE* f) {
|
||||
int fd = fileno(f);
|
||||
|
||||
struct stat buf;
|
||||
if (fstat(fd, &buf) == 0) {
|
||||
return (int)(buf.st_size);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int idris2_fpoll(FILE* f)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return win_fpoll(f);
|
||||
#else
|
||||
fd_set x;
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 1;
|
||||
timeout.tv_usec = 0;
|
||||
int fd = fileno(f);
|
||||
|
||||
FD_ZERO(&x);
|
||||
FD_SET(fd, &x);
|
||||
|
||||
int r = select(fd+1, &x, 0, 0, &timeout);
|
||||
return r;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
char* idris2_readLine(FILE* f) {
|
||||
char *buffer = NULL;
|
||||
size_t n = 0;
|
||||
ssize_t len;
|
||||
len = getline(&buffer, &n, f);
|
||||
if (len < 0 && buffer != NULL) {
|
||||
buffer[0] = '\0'; // Copy Idris 1 behaviour - empty string if nothing read
|
||||
}
|
||||
return buffer; // freed by RTS if not NULL
|
||||
}
|
||||
|
||||
char* idris_readChars(int num, FILE* f) {
|
||||
char *buffer = malloc((num+1)*sizeof(char));
|
||||
size_t len;
|
||||
len = fread(buffer, sizeof(char), (size_t)num, f);
|
||||
buffer[len] = '\0';
|
||||
|
||||
if (len <= 0) {
|
||||
return NULL;
|
||||
} else {
|
||||
return buffer; // freed by RTS
|
||||
}
|
||||
}
|
||||
|
||||
int idris2_writeLine(FILE* f, char* str) {
|
||||
if (fputs(str, f) == EOF) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int idris2_eof(FILE* f) {
|
||||
return feof(f);
|
||||
}
|
||||
|
||||
int idris2_fileAccessTime(FILE* f) {
|
||||
int fd = fileno(f);
|
||||
|
||||
struct stat buf;
|
||||
if (fstat(fd, &buf) == 0) {
|
||||
return buf.st_atime;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int idris2_fileModifiedTime(FILE* f) {
|
||||
int fd = fileno(f);
|
||||
|
||||
struct stat buf;
|
||||
if (fstat(fd, &buf) == 0) {
|
||||
return buf.st_mtime;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int idris2_fileStatusTime(FILE* f) {
|
||||
int fd = fileno(f);
|
||||
|
||||
struct stat buf;
|
||||
if (fstat(fd, &buf) == 0) {
|
||||
return buf.st_ctime;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
FILE* idris2_stdin() {
|
||||
return stdin;
|
||||
}
|
||||
|
||||
FILE* idris2_stdout() {
|
||||
return stdout;
|
||||
}
|
||||
|
||||
FILE* idris2_stderr() {
|
||||
return stderr;
|
||||
}
|
||||
|
||||
|
34
support/c/idris_file.h
Normal file
34
support/c/idris_file.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef __IDRIS_FILE_H
|
||||
#define __IDRIS_FILE_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
FILE* idris2_openFile(char* name, char* mode);
|
||||
void idris2_closeFile(FILE* f);
|
||||
|
||||
int idris2_fileError(FILE* f);
|
||||
|
||||
// Turn errno into an integer understandable by System.File
|
||||
int idris2_fileErrno();
|
||||
|
||||
int idris2_fileRemove(const char *filename);
|
||||
int idris2_fileSize(FILE* h);
|
||||
|
||||
int idris2_fpoll(FILE* f);
|
||||
|
||||
// Treat as a Ptr String (might be NULL)
|
||||
char* idris2_readLine(FILE* f);
|
||||
char* idris_readChars(int num, FILE* f);
|
||||
|
||||
int idris2_writeLine(FILE* f, char* str);
|
||||
|
||||
int idris2_eof(FILE* f);
|
||||
int idris2_fileAccessTime(FILE* f);
|
||||
int idris2_fileModifiedTime(FILE* f);
|
||||
int idris2_fileStatusTime(FILE* f);
|
||||
|
||||
FILE* idris2_stdin();
|
||||
FILE* idris2_stdout();
|
||||
FILE* idris2_stderr();
|
||||
|
||||
#endif
|
61
support/c/idris_support.c
Normal file
61
support/c/idris_support.c
Normal file
@ -0,0 +1,61 @@
|
||||
#include "idris_support.h"
|
||||
#include "idris_file.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
extern char** environ;
|
||||
|
||||
int idris2_isNull(void* ptr) {
|
||||
return (ptr == NULL);
|
||||
}
|
||||
|
||||
char* idris2_getString(void *p) {
|
||||
return (char*)p;
|
||||
}
|
||||
|
||||
int idris2_getErrno() {
|
||||
return errno;
|
||||
}
|
||||
|
||||
char* idris2_getStr() {
|
||||
char *inp = idris2_readLine(stdin);
|
||||
// Remove trailing newline; easier to do this than in PrimIO which
|
||||
// doesn't have the relevant functions available yet
|
||||
for(char *c = inp; *c != '\0'; ++c) {
|
||||
if (*c == '\n' || *c == '\r') {
|
||||
*c = '\0';
|
||||
}
|
||||
}
|
||||
return inp;
|
||||
}
|
||||
|
||||
void idris2_putStr(char* f) {
|
||||
idris2_writeLine(stdout, f);
|
||||
}
|
||||
|
||||
void idris2_sleep(int sec) {
|
||||
struct timespec t;
|
||||
t.tv_sec = sec;
|
||||
t.tv_nsec = 0;
|
||||
|
||||
nanosleep(&t, NULL);
|
||||
}
|
||||
|
||||
void idris2_usleep(int usec) {
|
||||
struct timespec t;
|
||||
t.tv_sec = usec / 1000000;
|
||||
t.tv_nsec = (usec % 1000000) * 1000;
|
||||
|
||||
nanosleep(&t, NULL);
|
||||
}
|
||||
|
||||
int idris2_time() {
|
||||
return time(NULL); // RTS needs to have 32 bit integers at least
|
||||
}
|
||||
|
||||
char* idris2_getEnvPair(int i) {
|
||||
return *(environ + i);
|
||||
}
|
20
support/c/idris_support.h
Normal file
20
support/c/idris_support.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef __IDRIS_SUPPORT_H
|
||||
#define __IDRIS_SUPPORT_H
|
||||
|
||||
// Return non-zero if the pointer is null
|
||||
int idris2_isNull(void*);
|
||||
// Convert a Ptr String intro a String, assuming the string has been checked
|
||||
// to be non-null
|
||||
char* idris2_getString(void *p);
|
||||
int idris2_getErrno();
|
||||
|
||||
char* idris2_getStr();
|
||||
void idris2_putStr(char* f);
|
||||
|
||||
void idris2_sleep(int sec);
|
||||
void idris2_usleep(int usec);
|
||||
int idris2_time();
|
||||
|
||||
char* idris2_getEnvPair(int i);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user