Parsing commandline arguments
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
CFLAGS := -ffreestanding -Wall -Wextra -g -fcommon -nostdinc \
|
||||
-isystem $(ROOT)/std/include -isystem $(ROOT)/ulib -isystem $(ROOT)/share
|
||||
-isystem $(ROOT)/std/include -isystem $(ROOT)/ulib -isystem $(ROOT)/share \
|
||||
-isystem $(ROOT)/ulib/std
|
||||
|
||||
CURRENT_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
|
||||
|
||||
|
@ -1,60 +1,42 @@
|
||||
#include <stdint.h>
|
||||
#include <string/string.h>
|
||||
#include <system/system.h>
|
||||
#include <sysdefs/ioctl.h>
|
||||
#include <sysdefs/ipcpipe.h>
|
||||
#include <sysdefs/processctl.h>
|
||||
#include <uprintf.h>
|
||||
#include <ansiq/all.h>
|
||||
#include <string/char.h>
|
||||
|
||||
void *string_memset2(void *p, int c, size_t n) {
|
||||
char *cp = p;
|
||||
for (size_t i = 0; i < n; i++) cp[i] = c;
|
||||
return p;
|
||||
}
|
||||
|
||||
void main(void) {
|
||||
uprintf(ANSIQ_CUR_SET(0, 0));
|
||||
uprintf(ANSIQ_SCR_CLR_ALL);
|
||||
|
||||
int32_t ioh = ioctl(IOCTL_NOHANDLE,
|
||||
IOCTL_OPENF,
|
||||
(uint64_t)"temp:/hello.txt",
|
||||
IOCTL_F_WRITE | IOCTL_F_READ | IOCTL_F_MAKE,
|
||||
0
|
||||
);
|
||||
|
||||
char *text = "Hello from FS";
|
||||
ioctl(ioh, IOCTL_WRITE, (uint64_t)text, string_len(text), 0);
|
||||
|
||||
char buf[0x100] = {0};
|
||||
ioctl(ioh, IOCTL_READ, (uint64_t)buf, string_len(text), 0);
|
||||
|
||||
uprintf("file contents: %s\n", buf);
|
||||
|
||||
ioctl(ioh, IOCTL_CLOSEF, 0, 0, 0);
|
||||
|
||||
uprintf("Hello world using uprintf\n");
|
||||
|
||||
const char *tbargs[] = { "-i" };
|
||||
int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb", (uint64_t)tbargs, 1);
|
||||
char *tbargs[] = { "-m", "interactive" };
|
||||
int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb", (uint64_t)&tbargs, 2);
|
||||
uint64_t selfpid = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0);
|
||||
ipcpipe(tb, IPCPIPE_OUT, IPCPIPE_REPLACE, (uint8_t *)selfpid, IPCPIPE_OUT);
|
||||
processctl(tb, PCTL_RUN, 0, 0, 0);
|
||||
while(processctl(tb, PCTL_POLLSTATE, 0, 0, 0) != 2);
|
||||
|
||||
if (ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_MAKE, NULL, 0) < 0) {
|
||||
uprintf("failed to create 10th pipe\n");
|
||||
}
|
||||
ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_MAKE, NULL, 0);
|
||||
ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_ADD_BCAST, NULL, 1);
|
||||
|
||||
int32_t r = ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_ADD_BCAST, NULL, 1);
|
||||
uprintf("%d\n", r);
|
||||
|
||||
while(1) {
|
||||
while(processctl(tb, PCTL_POLLSTATE, 0, 0, 0) != 2) {
|
||||
int32_t kbchr;
|
||||
int32_t read = ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_READ, (uint8_t *)&kbchr, sizeof(kbchr));
|
||||
if (read > 0) {
|
||||
kbchr &= 0xFF;
|
||||
if ((kbchr >= 0x20 && kbchr <= 0x7F) || kbchr == 0xA) {
|
||||
uprintf("%c", kbchr & 0xFF);
|
||||
uint8_t c = kbchr & 0xff;
|
||||
if (string_chr_isascii(c)) {
|
||||
if (c != 0) {
|
||||
ipcpipe(tb, IPCPIPE_IN, IPCPIPE_WRITE, &c, 1);
|
||||
}
|
||||
if (string_chr_isprint(c)) {
|
||||
uprintf("%c", c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(;;);
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,76 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <uprintf.h>
|
||||
#include <dlmalloc/malloc.h>
|
||||
#include <args.h>
|
||||
#include <args/args.h>
|
||||
#include <string/string.h>
|
||||
#include <string/char.h>
|
||||
#include <log.h>
|
||||
#include <ansiq/all.h>
|
||||
#include <system/system.h>
|
||||
#include <sysdefs/ipcpipe.h>
|
||||
|
||||
void main(void) {
|
||||
uprintf("Hello from tb!\n");
|
||||
static struct {
|
||||
char *modestr;
|
||||
enum { MODE_INTERACTIVE = 1, MODE_RUNFILE = 2 } mode;
|
||||
} CONFIG;
|
||||
|
||||
for (size_t i = 0; i < argslen(); i++) {
|
||||
uprintf("i = %d\n", i);
|
||||
uprintf("arg: %s\n", args()[i]);
|
||||
#define LINEBUF_MAX 1024
|
||||
|
||||
static Arg ARGS[] = {
|
||||
ARG("-m", ARG_STRING, &CONFIG.modestr),
|
||||
ARG_END(),
|
||||
};
|
||||
|
||||
void set_config(void) {
|
||||
int32_t ret;
|
||||
if ((ret = parse_args(args(), argslen(), ARGS)) < 0) {
|
||||
uprintf("Could not parse args: %d\n", ret);
|
||||
}
|
||||
|
||||
if (CONFIG.modestr != NULL) {
|
||||
if (string_strcmp(CONFIG.modestr, "interactive") == 0) {
|
||||
CONFIG.mode = MODE_INTERACTIVE;
|
||||
} else if (string_strcmp(CONFIG.modestr, "runfile") == 0) {
|
||||
CONFIG.mode = MODE_RUNFILE;
|
||||
} else {
|
||||
LOG(LOG_ERR, "Unknown mode %s\n", CONFIG.modestr);
|
||||
}
|
||||
} else {
|
||||
CONFIG.mode = MODE_RUNFILE;
|
||||
}
|
||||
uprintf("CONFIG.mode = %d\n", CONFIG.mode);
|
||||
}
|
||||
|
||||
void process_cmd(char *cmdtext) {
|
||||
|
||||
}
|
||||
|
||||
void do_mode_interactive(void) {
|
||||
char linebuf[LINEBUF_MAX];
|
||||
size_t cursor;
|
||||
for(;;) {
|
||||
uprintf("tb# ");
|
||||
|
||||
cursor = 0;
|
||||
string_memset(linebuf, 0, LINEBUF_MAX);
|
||||
char c = 0;
|
||||
while (c != '\n') {
|
||||
int32_t rd = ipcpipe(IPCPIPE_SELFPID, IPCPIPE_IN, IPCPIPE_READ, (uint8_t *)&c, 1);
|
||||
if (rd > 0 && cursor < LINEBUF_MAX) {
|
||||
linebuf[cursor++] = c;
|
||||
}
|
||||
}
|
||||
linebuf[cursor - 1] = '\0';
|
||||
uprintf("\n");
|
||||
|
||||
process_cmd(linebuf);
|
||||
}
|
||||
}
|
||||
|
||||
void main(void) {
|
||||
set_config();
|
||||
|
||||
if (CONFIG.mode == MODE_INTERACTIVE) {
|
||||
do_mode_interactive();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user