libsysbase
Dave Murphy
wintermute2k4@ntlworld.com
Tue May 23 19:10:00 GMT 2006
Hi,
I've done some work on adding a support library to libgloss designed to
support pseudo device drivers based on the current libnosys. I've used
this on a few embedded platforms to implement printf and filesystem
support within newlib. I'd like to present my patch here for peer review
and guidance in contributing this to newlib if it's acceptable.
My psuedo devices are implemented as a table of function pointers which
include entries for stdin, stdout and stderr. I've allowed for a total
of 16 devices.
typedef struct {
const char *name;
int structSize;
int (*open_r)(struct _reent *r, void *fileStruct, const char
*path,int flags,int mode);
int (*close_r)(struct _reent *r,int fd);
int (*write_r)(struct _reent *r,int fd,const char *ptr,int len);
int (*read_r)(struct _reent *r,int fd,char *ptr,int len);
int (*seek_r)(struct _reent *r,int fd,int pos,int dir);
int (*fstat_r)(struct _reent *r,int fd,struct stat *st);
int (*stat_r)(struct _reent *r,const char *file,struct stat *st);
int (*link_r)(struct _reent *r,char *existing, char *new);
int (*unlink_r)(struct _reent *r,char *name);
int (*chdir_r)(struct _reent *r,char *name);
} devoptab_t;
The name field is the name of the device - the support framework checks
for this name followed by a colon at the start of a filename passed to
fopen.
Currently I make printf and associated functions work by creating a
devopt_tab entry with just a write function and insert this in the table
for stdin & stderr, setting these streams as unbuffered.
#include <sys/iosupport.h>
int con_write(struct _reent *r,int fd,const char *ptr,int len) {
...
code to output string
...
return len;
}
const devoptab_t dotab_stdout = {
"con",
0,
NULL,
NULL,
con_write,
NULL,
NULL,
NULL
};
devoptab_list[STD_OUT] = &dotab_stdout;
devoptab_list[STD_ERR] = &dotab_stdout;
setvbuf(stderr, NULL , _IONBF, 0);
setvbuf(stdout, NULL , _IONBF, 0);
I should probably add a function to set these entries in iosupport.c
rather than setting the table entries directly.
For filesystem devices I've provided AddDevice which takes a pointer to
a devoptab_t structure as shown above and inserts it into a free space
in the device table, returning a device number or -1 on failure.
SetDefaultDevice sets the device number to be used when no device
descriptor is found at the start of the filename.
I'm still planning on adding chdir and getcwd to this and providing some
method of parsing directories. I've considered adding functions similar
to TOS ( fsfirst and fsnext ) if anyone is familiar with the old Atari
machines. I also noticed opendir, readdir etc in the posix folder but
these are dependent on dirent.h which is stubbed in the default newlib
install. How would I go about installing a suitable header to use for
these functions in my library?
The sbrk function I've implemented here is similar to the one found in
libc/sys/arm but extended a little to allow for situations where the
heap and the stack are not in the same memory area as is the case on a
couple of the platforms I target. I usually set the extent of the heap
in my crt0 files using the fake_heap_start and fake_heap_end variables
I've added.
If this is suitable for contribution to newlib it might be worth adding
a configure switch along the lines of --enable-libsysbase. How would I
go about doing that?
I'm not sure if newlib/libc/include/sys is a good place to put the
iosupport.h header I've added. Is there a better place for that to go?
I should also write some comprehensive documentation for this addition.
What format should that be in and where should it be placed?
thanks
Dave
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: libsysbase.patch
URL: <http://sourceware.org/pipermail/newlib/attachments/20060523/72c17794/attachment.ksh>
More information about the Newlib
mailing list