+#define _XOPEN_SOURCE 700
+#include <liba.h>
+
#include <stdc.h>
#include "dbc.h"
-#include "utf.h"
-#include "io.h"
-#include <ctype.h>
+//#include "utf.h"
+//#include <ctype.h>
#include <unistd.h>
-#include <fcntl.h>
-#include <sys/stat.h>
+//#include <fcntl.h>
+//#include <sys/stat.h>
#include <dirent.h>
#include <vec.h>
long gapbuf_save(GapBuf* buf, char* path)
{
require(buf != NULL);
- long fd;
+ File fd;
long nwrite = 0;
- if (path && (fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) >= 0)
+// if (path && (fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) >= 0)
+ if (path && (fd = File_OpenForWrite(path)) >= 0)
{
- nwrite = writefd(fd, buf->bufstart, (buf->gapstart - buf->bufstart));
+ nwrite = File_Write(fd, buf->bufstart, (buf->gapstart - buf->bufstart));
if (nwrite >= 0)
{
- nwrite = writefd(fd, buf->gapend, (buf->bufend - buf->gapend));
+ nwrite = File_Write(fd, buf->gapend, (buf->bufend - buf->gapend));
}
- close(fd);
+ File_Close(fd);
}
else
{
require(buf != NULL);
require(path != NULL);
/* load the contents from the file */
- int fd;
+ File fd;
struct stat sb = {0};
- if (((fd = open(path, O_RDONLY, 0)) >= 0) && (fstat(fd, &sb) >= 0) && (sb.st_size > 0))
+ if (((fd = File_OpenForRead(path)) >= 0) && (fstat((int)fd, &sb) >= 0) && (sb.st_size > 0))
{
/* allocate the buffer in advance */
free(buf->bufstart);
else
{
buf->gapstart += sb.st_size;
- (void)readfd(fd, buf->bufstart, sb.st_size);
+ (void)File_Read(fd, buf->bufstart, sb.st_size);
}
}
if (fd > 0)
Helper functions for reading and writing file descriptors as well as outputting telemetry data.
*/
void telem_send(char* fmt, ...);
-long writefd(int fd, char* data, long towrite);
-long readfd(int fd, char* data, long toread);
-char* readfile(char* path);
+
+//long writefd(int fd, char* data, long towrite);
+//long readfd(int fd, char* data, long toread);
+//char* readfile(char* path);
+
char* abspath(char* path);
+#define _XOPEN_SOURCE 700
+#include <liba.h>
+
#include <stdc.h>
-#include "utf.h"
#include "io.h"
#include <unistd.h>
#include <sys/wait.h>
struct PipeData* pipedata = job->data;
char* chunk = pipedata->data + pipedata->nwrite;
errno = 0;
- long nwrite = write(job->fd, chunk, pipedata->ndata);
+ long nwrite = File_Write(job->fd, chunk, pipedata->ndata);
if (nwrite >= 0)
{
pipedata->ndata -= nwrite;
#include <stdc.h>
#include "utf.h"
#include "tide.h"
-//#include "x11.h"
static void leap_process_keysym(LeapState_T* state, unsigned long keysym)
{
+++ /dev/null
-#include <stdc.h>
-#include "io.h"
-
-long readfd(int fd, char* data, long toread)
-{
- long nread = 0;
- while (toread && ((nread = read(fd, data, toread)) >= 0))
- {
- data += nread;
- toread -= nread;
- }
- return nread;
-}
+++ /dev/null
-#include <stdc.h>
-#include "io.h"
-#include <fcntl.h>
-#include <sys/stat.h>
-
-char* readfile(char* path)
-{
- int fd;
- char* data = NULL;
- struct stat sb = {0};
- if (((fd = open(path, O_RDONLY, 0)) >= 0) && (fstat(fd, &sb) >= 0) && (sb.st_size > 0))
- {
- data = malloc(sb.st_size+1);
- (void)readfd(fd, data, sb.st_size);
- data[sb.st_size] = '\0';
- }
- if (fd > 0)
- {
- close(fd);
- }
- return data;
-}
+#include <liba.h>
+
#include <stdc.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
-#include "io.h"
+
static int TelemFd = -1;
static char TelemBuf[16384];
str = add_pid(str, &nleft);
str = add_message(str, &nleft, fmt, args);
va_end(args);
- if (writefd(fd, TelemBuf, str - TelemBuf) < 0)
+ if (File_Write(fd, TelemBuf, str - TelemBuf) < 0)
{
close_telem();
}
+++ /dev/null
-#include <stdc.h>
-#include "io.h"
-
-long writefd(int fd, char* data, long towrite)
-{
- long nwrite = 0;
- while (towrite && ((nwrite = write(fd, data, towrite)) > 0))
- {
- data += nwrite;
- towrite -= nwrite;
- }
- return nwrite;
-}
char buf[32];
KeySym key = 0;
XLookupString(&(e->xkey), buf, sizeof(buf), &key, 0);
- printf("%s\n", buf);
-
return key;
}
+#define _XOPEN_SOURCE 700
+#include <liba.h>
#include <stdc.h>
-#include "utf.h"
#include "io.h"
#include <sys/socket.h>
#ifdef __linux__
if ((tio.c_lflag & ICANON) == ICANON)
{
char b = byte;
- write(Pty_Fd, &b, 1);
+ File_Write(Pty_Fd, &b, 1);
if (byte == '\n')
{
*((char*)(EditView->buffer.contents.gapstart-1)) = ' ';
}
else
{
- read(Pty_Fd, &b, 1);
+ File_Read(Pty_Fd, &b, 1);
}
}
else if (byte == '\n' && (EditView->buffer.selection.end == EditView->buffer.point.end))
/* write the data and read back to discard the echoed chars */
// printf("write: '%s'\n", str);
- writefd(Pty_Fd, str, slen);
+ File_Write(Pty_Fd, str, slen);
State = READ_ECHO;
IgnoreCount = slen+1;
InputBuf[(InputPos = 0)] = '\0';
static void xpty_read(Job* job)
{
- long nread = read(job->fd, ReadBuf, sizeof(ReadBuf)-1);
+ long nread = File_Read(job->fd, ReadBuf, sizeof(ReadBuf)-1);
if (nread <= 0)
{
job->readfn = NULL;
void xpty_rawsend(char* str)
{
- (void)write(Pty_Fd, str, strlen(str));
+ (void)File_Write(Pty_Fd, str, strlen(str));
}
return fd_count;
}
-Int ProcessMessages(void* arg)
+int ProcessMessages(void* arg)
{
(void)arg;
for (;;)
#include <errno.h>
typedef unsigned char Byte;
-typedef int Int;
-typedef unsigned int Uint;
+typedef long Int;
+typedef unsigned long Uint;
typedef double Real;
typedef Int Rune;
typedef _Bool Bool;
Int UTF8_Encode(Byte str[UTF_MAX], Rune rune);
Bool UTF8_Decode(Rune* rune, Int* length, Int byte);
+/*
+ File I/O
+*/
+typedef Int File;
+File File_OpenForRead(char* path);
+File File_OpenForWrite(char* path);
+File File_OpenForReadWrite(char* path);
+File File_OpenForAppend(char* path);
+Int File_Close(File f);
+//FileStats File_Statistics(File);
+Int File_Read(File f, char* buf, Uint bufsz);
+Int File_Write(File f, char* buf, Uint bufsz);
+char* File_ReadAll(char* path);
+
+
/*
Message Bus Interface
*/
typedef thrd_t Thread_T;
-Int Thread_Create(Thread_T* thread, Int (*func)(void*), void *arg);
+Int Thread_Create(Thread_T* thread, int (*func)(void*), void *arg);
Thread_T Thread_Current(void);
void Thread_Yield(void);
_Noreturn void Thread_Exit(Int res);
--- /dev/null
+#define _XOPEN_SOURCE 700
+#include <liba.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+
+File File_OpenForRead(char* path)
+{
+ return (File)open(path, O_RDONLY);
+}
+
+File File_OpenForWrite(char* path)
+{
+ return (File)open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+}
+
+File File_OpenForReadWrite(char* path)
+{
+ return (File)open(path, O_RDWR|O_CREAT|O_TRUNC, 0644);
+}
+
+File File_OpenForAppend(char* path)
+{
+ return (File)open(path, O_WRONLY|O_APPEND);
+}
+
+Int File_Close(File f)
+{
+ return close((int)f);
+}
+
+//FileStats File_Statistics(File)
+//{
+//}
+
+Int File_Read(File f, char* data, Uint toread)
+{
+ long nread = 0;
+ while (toread && ((nread = read((int)f, data, toread)) >= 0))
+ {
+ data += nread;
+ toread -= nread;
+ }
+ return nread;
+}
+
+Int File_Write(File f, char* data, Uint towrite)
+{
+ long nwrite = 0;
+ while (towrite && ((nwrite = write(f, data, towrite)) > 0))
+ {
+ data += nwrite;
+ towrite -= nwrite;
+ }
+ return nwrite;
+
+}
+
+char* File_ReadAll(char* path)
+{
+ char* data = NULL;
+ struct stat sb = {0};
+ File f = File_OpenForRead(path);
+ if ((f >= 0) && (fstat(f, &sb) >= 0) && (sb.st_size > 0))
+ {
+ data = malloc(sb.st_size+1);
+ (void)File_Read(f, data, sb.st_size);
+ data[sb.st_size] = '\0';
+ }
+ if (f > 0)
+ {
+ File_Close(f);
+ }
+ return data;
+}
+
#include <liba.h>
#include <threads.h>
-Int Thread_Create(Thread_T* thread, Int (*func)(void*), void *arg)
+Int Thread_Create(Thread_T* thread, int (*func)(void*), void *arg)
{
- return thrd_create(thread, func, arg);
+ return thrd_create(thread, (int (*)(void*))func, arg);
}
Thread_T Thread_Current(void)
Int Thread_Join(Thread_T thread)
{
- Int res = 0;
+ int res = 0;
Int success = thrd_join(thread, &res);
return (success == thrd_success ? res : success);
}