ulib uprintf to pipe not termdev, ulib Add stringbuffer and linearlist, tb Capture subshell output
This commit is contained in:
@ -21,7 +21,7 @@ void clearbss(void) {
|
||||
}
|
||||
}
|
||||
|
||||
#define MAX_ARGS 15
|
||||
#define MAX_ARGS 25
|
||||
static char *_args[MAX_ARGS];
|
||||
|
||||
size_t _argslen;
|
||||
@ -34,14 +34,10 @@ size_t argslen(void) {
|
||||
return _argslen;
|
||||
}
|
||||
|
||||
Dev_t termdev;
|
||||
|
||||
// ulib initialization goes here
|
||||
void _premain(void) {
|
||||
clearbss();
|
||||
|
||||
devctl(&termdev, DEVCTL_GET_HANDLE, (uint8_t *)DEV_TERMDEV, 0, 0);
|
||||
|
||||
for (size_t i = 0; i < ARRLEN(_args); i++) {
|
||||
_args[i] = umalloc(PROC_ARG_MAX);
|
||||
}
|
||||
|
30
ulib/linearlist.h
Normal file
30
ulib/linearlist.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef ULIB_LINEARLIST_H_
|
||||
#define ULIB_LINEARLIST_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <umalloc/umalloc.h>
|
||||
|
||||
#define LINLIST_APPEND(lst, d) \
|
||||
do { \
|
||||
if ((lst)->data == NULL) { \
|
||||
(lst)->capacity = 1; \
|
||||
(lst)->data = umalloc(sizeof(*(lst)->data) * (lst)->capacity); \
|
||||
} else { \
|
||||
if ((lst)->count == (lst)->capacity) { \
|
||||
(lst)->capacity *= 2; \
|
||||
(lst)->data = urealloc((lst)->data, sizeof(*(lst)->data) * (lst)->capacity); \
|
||||
} \
|
||||
} \
|
||||
(lst)->data[(lst)->count++] = (d); \
|
||||
} while(0)
|
||||
|
||||
#define LINLIST_FREE(lst) \
|
||||
do { \
|
||||
if ((lst)->data != NULL) { \
|
||||
ufree((lst)->data); \
|
||||
(lst)->data = NULL; \
|
||||
} \
|
||||
(lst)->count = 0; \
|
||||
} while(0)
|
||||
|
||||
#endif // ULIB_LINEARLIST_H_
|
@ -1,11 +1,8 @@
|
||||
#include <stdint.h>
|
||||
#include <system/system.h>
|
||||
#include <sysdefs/ipcpipe.h>
|
||||
#include <sysdefs/devctl.h>
|
||||
#include <printf/printf.h>
|
||||
|
||||
extern Dev_t termdev;
|
||||
|
||||
void putchar_(char c) {
|
||||
devctl(&termdev, 0x00, (uint8_t *)&c, 1, 0);
|
||||
ipcpipe(-1, IPCPIPE_OUT, IPCPIPE_WRITE, (uint8_t *)&c, 1);
|
||||
}
|
||||
|
26
ulib/string/stringbuffer.c
Normal file
26
ulib/string/stringbuffer.c
Normal file
@ -0,0 +1,26 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <string/stringbuffer.h>
|
||||
#include <umalloc/umalloc.h>
|
||||
#include <string/string.h>
|
||||
#include <linearlist.h>
|
||||
|
||||
void stringbuffer_init(StringBuffer *sb) {
|
||||
string_memset(sb, 0, sizeof(*sb));
|
||||
}
|
||||
|
||||
void stringbuffer_free(StringBuffer *sb) {
|
||||
LINLIST_FREE(sb);
|
||||
}
|
||||
|
||||
void stringbuffer_appendchar(StringBuffer *sb, char c) {
|
||||
LINLIST_APPEND(sb, c);
|
||||
}
|
||||
|
||||
void stringbuffer_appendcstr(StringBuffer *sb, char *cstr) {
|
||||
char *s = cstr;
|
||||
while (*s) {
|
||||
stringbuffer_appendchar(sb, *s);
|
||||
s++;
|
||||
}
|
||||
}
|
15
ulib/string/stringbuffer.h
Normal file
15
ulib/string/stringbuffer.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef ULIB_STRING_STRINGBUFFER_H_
|
||||
#define ULIB_STRING_STRINGBUFFER_H_
|
||||
|
||||
typedef struct {
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
char *data;
|
||||
} StringBuffer;
|
||||
|
||||
void stringbuffer_init(StringBuffer *sb);
|
||||
void stringbuffer_free(StringBuffer *sb);
|
||||
void stringbuffer_appendchar(StringBuffer *sb, char c);
|
||||
void stringbuffer_appendcstr(StringBuffer *sb, char *cstr);
|
||||
|
||||
#endif // ULIB_STRING_STRINGBUFFER_H_
|
@ -4,6 +4,7 @@
|
||||
#include <ansiq/all.h>
|
||||
#include <args/args.h>
|
||||
#include <string/string.h>
|
||||
#include <string/stringbuffer.h>
|
||||
#include <string/conv.h>
|
||||
#include <string/char.h>
|
||||
#include <sync/spinlock.h>
|
||||
|
@ -118,3 +118,33 @@ void ufree(void *ptr_) {
|
||||
}
|
||||
}
|
||||
|
||||
void *urealloc(void *ptr, size_t newsize) {
|
||||
void *new;
|
||||
|
||||
UmBlock *block = (UmBlock *)(ptr - sizeof(UmBlock));
|
||||
if (block->magic != BLOCK_MAGIC || block->data != ptr) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!ptr) {
|
||||
new = umalloc(newsize);
|
||||
if (!new) {
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
if (block->size < newsize) {
|
||||
new = umalloc(newsize);
|
||||
if (!new) {
|
||||
goto err;
|
||||
}
|
||||
string_memcpy(new, ptr, block->size);
|
||||
ufree(ptr);
|
||||
} else {
|
||||
new = ptr;
|
||||
}
|
||||
}
|
||||
return new;
|
||||
|
||||
err:
|
||||
return NULL;
|
||||
}
|
||||
|
@ -6,5 +6,6 @@
|
||||
|
||||
void *umalloc(size_t size);
|
||||
void ufree(void *ptr_);
|
||||
void *urealloc(void *ptr, size_t newsize);
|
||||
|
||||
#endif // ULIB_UMALLOC_UMALLOC_H_
|
||||
|
Reference in New Issue
Block a user