#include <stddef.h>
size_t
-__sprintf_internal(char* buffer, char* fmt, size_t max_len, va_list vargs);
+__ksprintf_internal(char* buffer, char* fmt, size_t max_len, va_list vargs);
size_t
-sprintf(char* buffer, char* fmt, ...);
+ksprintf(char* buffer, char* fmt, ...);
size_t
-snprintf(char* buffer, size_t n, char* fmt, ...);
+ksnprintf(char* buffer, size_t n, char* fmt, ...);
#endif /* __LUNAIX_STDIO_H */
--- /dev/null
+#ifndef __LUNAIX_USTDIO_H
+#define __LUNAIX_USTDIO_H
+
+#define stdout 0
+#define stdin 1
+
+void
+printf(const char* fmt, ...);
+
+#endif /* __LUNAIX_USTDIO_H */
{
uint32_t error_reg = apic_read_reg(APIC_ESR);
char buf[32];
- sprintf(buf, "APIC error, ESR=0x%x", error_reg);
+ ksprintf(buf, "APIC error, ESR=0x%x", error_reg);
console_flush();
__print_panic_msg(buf, param);
spin();
#include <lunaix/fctrl.h>
#include <lunaix/lunistd.h>
#include <lunaix/proc.h>
-#include <lunaix/syslog.h>
-
-LOG_MODULE("RDDIR")
void
_readdir_main()
{
int fd = open("/dev/./../dev/.", 0);
if (fd == -1) {
- kprintf(KERROR "fail to open (%d)\n", geterrno());
+ printf("fail to open (%d)\n", geterrno());
return;
}
char path[129];
int len = realpathat(fd, path, 128);
if (len < 0) {
- kprintf(KERROR "fail to read (%d)\n", geterrno());
+ printf("fail to read (%d)\n", geterrno());
} else {
path[len] = 0;
- kprintf("%s\n", path);
+ printf("%s\n", path);
}
struct dirent ent = { .d_offset = 0 };
while (readdir(fd, &ent) == 1) {
- kprintf(KINFO "%s\n", ent.d_name);
+ printf("%s\n", ent.d_name);
}
close(fd);
#include <lunaix/foptions.h>
#include <lunaix/input.h>
#include <lunaix/lunistd.h>
+#include <ulibc/stdio.h>
#define STDIN 1
#define STDOUT 0
int fd = open("/dev/input/i8042-kbd", 0);
if (fd < 0) {
- write(STDOUT, "fail to open", 13);
+ printf("fail to open (%d)", fd);
return;
}
struct input_evt_pkt event;
while (read(fd, &event, sizeof(event)) > 0) {
+ char* action;
if (event.pkt_type == PKT_PRESS) {
- write(STDOUT, "PRESSED: ", 10);
+ action = "pressed";
} else {
- write(STDOUT, "RELEASE: ", 10);
+ action = "release";
}
- char c = event.sys_code & 0xff;
- write(STDOUT, &c, 1);
- write(STDOUT, "\n", 2);
+
+ printf("%u: %s '%c', class=0x%x, scan=%d\n",
+ event.timestamp,
+ action,
+ event.sys_code & 0xff,
+ (event.sys_code & 0xff00) >> 8,
+ event.scan_code);
}
return;
}
\ No newline at end of file
#include <lunaix/foptions.h>
#include <lunaix/lunistd.h>
#include <lunaix/proc.h>
-#include <lunaix/syslog.h>
-
-LOG_MODULE("IOTEST")
-
-#define STDIN 1
-#define STDOUT 0
+#include <ulibc/stdio.h>
void
_iotest_main()
// 切换工作目录至 /dev
int errno = chdir("/dev");
if (errno) {
- write(STDOUT, "fail to chdir", 15);
+ write(stdout, "fail to chdir", 15);
return;
}
if (getcwd(read_out, sizeof(read_out))) {
- write(STDOUT, "current working dir: ", 22);
- write(STDOUT, read_out, 256);
- write(STDOUT, "\n", 2);
+ printf("current working dir: %s\n", read_out);
}
// sda 设备 - 硬盘
int fd = open("./sda", 0);
if (fd < 0) {
- kprintf(KERROR "fail to open (%d)\n", geterrno());
+ printf("fail to open (%d)\n", geterrno());
return;
}
lseek(fd, 4 * 4096, FSEEK_SET);
write(fd, test_sequence, sizeof(test_sequence));
- write(STDOUT, "input: ", 8);
- int size = read(STDIN, read_out, 256);
+ printf("input: ");
+ int size = read(stdin, read_out, 256);
+
+ printf("your said: %s\n", read_out);
- write(STDOUT, "your input: ", 13);
- write(STDOUT, read_out, size);
write(fd, read_out, size);
- write(STDOUT, "\n", 1);
// 读出我们写的内容
lseek(fd, 512, FSEEK_SET);
read(fd, read_out, sizeof(read_out));
// 将读出的内容直接写入tty设备
- write(STDOUT, read_out, sizeof(read_out));
- write(STDOUT, "\n", 1);
+ write(stdout, read_out, sizeof(read_out));
+ write(stdout, "\n", 1);
// 关闭文件,这同时会将页缓存中的数据下发到底层驱动
close(fd);
-
- kprint_hex(read_out, sizeof(read_out));
}
\ No newline at end of file
#include <lunaix/proc.h>
#include <lunaix/signal.h>
#include <lunaix/spike.h>
-#include <lunaix/syslog.h>
#include <lunaix/types.h>
-
-LOG_MODULE("SIGDEMO")
+#include <ulibc/stdio.h>
void __USER__
sigchild_handler(int signum)
{
- kprintf(KINFO "SIGCHLD received\n");
+ printf("SIGCHLD received\n");
}
void __USER__
sigsegv_handler(int signum)
{
pid_t pid = getpid();
- kprintf(KWARN "SIGSEGV received on process %d\n", pid);
+ printf("SIGSEGV received on process %d\n", pid);
_exit(signum);
}
sigalrm_handler(int signum)
{
pid_t pid = getpid();
- kprintf(KWARN "I, pid %d, have received an alarm!\n", pid);
+ printf("I, pid %d, have received an alarm!\n", pid);
}
void __USER__
int status;
pid_t p = 0;
- kprintf(KINFO "Child sleep 3s, parent pause.\n");
+ printf("Child sleep 3s, parent pause.\n");
if (!fork()) {
sleep(3);
_exit(0);
pause();
- kprintf("Parent resumed on SIGCHILD\n");
+ printf("Parent resumed on SIGCHILD\n");
for (int i = 0; i < 5; i++) {
pid_t pid = 0;
if (i == 3) {
i = *(int*)0xdeadc0de; // seg fault!
}
- kprintf(KINFO "%d\n", i);
+ printf("%d\n", i);
_exit(0);
}
- kprintf(KINFO "Forked %d\n", pid);
+ printf("Forked %d\n", pid);
}
while ((p = wait(&status)) >= 0) {
short code = WEXITSTATUS(status);
if (WIFSIGNALED(status)) {
- kprintf(KINFO "Process %d terminated by signal, exit_code: %d\n",
- p,
- code);
+ printf("Process %d terminated by signal, exit_code: %d\n", p, code);
} else if (WIFEXITED(status)) {
- kprintf(KINFO "Process %d exited with code %d\n", p, code);
+ printf("Process %d exited with code %d\n", p, code);
} else {
- kprintf(KWARN "Process %d aborted with code %d\n", p, code);
+ printf("Process %d aborted with code %d\n", p, code);
}
}
- kprintf("done\n");
-
- _exit(0);
+ printf("done\n");
}
\ No newline at end of file
}
size_t strlen =
- __sprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, args);
+ __ksprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, args);
dev->dev_id = devid++;
dev->name = HSTR(dev->name_val, strlen);
switch (log_level) {
case '0':
- snprintf(expanded_fmt,
- MAX_XFMT_SIZE,
- "[%s] (%s) %s",
- "INFO",
- component,
- fmt);
+ ksnprintf(expanded_fmt,
+ MAX_XFMT_SIZE,
+ "[%s] (%s) %s",
+ "INFO",
+ component,
+ fmt);
break;
case '1':
// tty_set_theme(VGA_COLOR_BROWN, current_theme >> 12);
- snprintf(expanded_fmt,
- MAX_XFMT_SIZE,
- "\033[6;0m[%s] (%s) %s\033[39;49m",
- "WARN",
- component,
- fmt);
+ ksnprintf(expanded_fmt,
+ MAX_XFMT_SIZE,
+ "\033[6;0m[%s] (%s) %s\033[39;49m",
+ "WARN",
+ component,
+ fmt);
break;
case '2':
// tty_set_theme(VGA_COLOR_LIGHT_RED, current_theme >> 12);
- snprintf(expanded_fmt,
- MAX_XFMT_SIZE,
- "\033[12;0m[%s] (%s) %s\033[39;49m",
- "EROR",
- component,
- fmt);
+ ksnprintf(expanded_fmt,
+ MAX_XFMT_SIZE,
+ "\033[12;0m[%s] (%s) %s\033[39;49m",
+ "EROR",
+ component,
+ fmt);
break;
case '3':
// tty_set_theme(VGA_COLOR_LIGHT_BLUE, current_theme >> 12);
- snprintf(expanded_fmt,
- MAX_XFMT_SIZE,
- "\033[9;0m[%s] (%s) %s\033[39;49m",
- "DEBG",
- component,
- fmt);
+ ksnprintf(expanded_fmt,
+ MAX_XFMT_SIZE,
+ "\033[9;0m[%s] (%s) %s\033[39;49m",
+ "DEBG",
+ component,
+ fmt);
break;
default:
- snprintf(expanded_fmt,
- MAX_XFMT_SIZE,
- "[%s] (%s) %s",
- "LOG",
- component,
- fmt);
+ ksnprintf(expanded_fmt,
+ MAX_XFMT_SIZE,
+ "[%s] (%s) %s",
+ "LOG",
+ component,
+ fmt);
break;
}
- __sprintf_internal(buf, expanded_fmt, MAX_KPRINTF_BUF_SIZE, args);
+ __ksprintf_internal(buf, expanded_fmt, MAX_KPRINTF_BUF_SIZE, args);
console_write_str(buf);
}
tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_RED);
tty_clear_line(24);
- __sprintf_internal(buf, fmt, MAX_KPRINTF_BUF_SIZE, args);
+ __ksprintf_internal(buf, fmt, MAX_KPRINTF_BUF_SIZE, args);
tty_put_str_at(buf, 0, 24);
va_end(args);
ch_cache[0] = '|';
ch_cache[1] = ' ';
while (size) {
- snprintf(buf, 64, " %.4p: ", ptr);
+ ksnprintf(buf, 64, " %.4p: ", ptr);
console_write_str(buf);
for (i = 0; i < 8 && size; i++, size--, ptr++) {
unsigned char c = *(data + ptr) & 0xff;
ch_cache[2 + i] = (32 <= c && c < 127) ? c : '.';
- snprintf(buf, 64, "%.2x ", c);
+ ksnprintf(buf, 64, "%.2x ", c);
console_write_str(buf);
}
ch_cache[2 + i] = '\0';
lxconsole_init()
{
memset(&lx_console, 0, sizeof(lx_console));
- fifo_init(&lx_console.output, VGA_BUFFER_VADDR + 0x1000, 8192, 0);
+ fifo_init(&lx_console.output, vzalloc(8192), 8192, 0);
fifo_init(&lx_console.input, valloc(4096), 4096, 0);
- // FIXME use valloc to allocate console buffer.
- // In doing this, the console buffer can only be accessed from kernel mode
- // any direct write to this buffer from user land should be purged!
-
- // 分配控制台缓存
- for (size_t i = 0; i < PG_ALIGN(lx_console.output.size); i += PG_SIZE) {
- uintptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
- vmm_set_mapping(PD_REFERENCED,
- (uintptr_t)lx_console.output.data + i,
- pa,
- PG_PREM_URW,
- 0);
- }
-
lx_console.flush_timer = NULL;
struct device* tty_dev = device_addseq(NULL, &lx_console, "tty");
#include <klibc/string.h>
+#include <ulibc/stdio.h>
+
LOG_MODULE("PROC0")
extern void
// 打开tty设备(控制台),作为标准输入输出。
// tty设备属于序列设备(Sequential Device),该类型设备的上层读写
// 无须经过Lunaix的缓存层,而是直接下发到底层驱动。(不受FO_DIRECT的影响)
- int stdout = open("/dev/tty", 0);
- int stdin = dup2(stdout, 1);
+ int fdstdout = open("/dev/tty", 0);
+ int fdstdin = dup2(stdout, 1);
pid_t p;
// if (!fork()) {
#else
_lxinit_main();
#endif
+ printf("==== test end ====\n");
_exit(0);
}
// 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
lock_reserved_memory();
- // we are using no kalloc!
- // assert_msg(kalloc_init(), "Fail to initialize heap");
-
rtc_init();
acpi_init(_k_init_mb_info);
apic_init();
pci_init();
block_init();
ahci_init();
- // ahci_list_device();
- // cake_stats();
syscall_install();
void
__assert_fail(const char* expr, const char* file, unsigned int line)
{
- sprintf(buffer, "%s (%s:%u)", expr, file, line);
+ ksprintf(buffer, "%s (%s:%u)", expr, file, line);
// Here we load the buffer's address into %edi ("D" constraint)
// This is a convention we made that the LUNAIX_SYS_PANIC syscall will
{
va_list args;
va_start(args, fmt);
- __sprintf_internal(buffer, fmt, 1024, args);
+ __ksprintf_internal(buffer, fmt, 1024, args);
va_end(args);
asm("int %0" ::"i"(LUNAIX_SYS_PANIC), "D"(buffer));
#define FLAG_CAPS (1 << 9)
size_t
-__sprintf_internal(char* buffer, char* fmt, size_t max_len, va_list vargs)
+__ksprintf_internal(char* buffer, char* fmt, size_t max_len, va_list vargs)
{
// This sprintf just a random implementation I found it on Internet . lol.
// Of course, with some modifications for porting to LunaixOS :)
}
size_t
-sprintf(char* buffer, char* fmt, ...)
+ksprintf(char* buffer, char* fmt, ...)
{
va_list args;
va_start(args, fmt);
- size_t len = __sprintf_internal(buffer, fmt, 0, args);
+ size_t len = __ksprintf_internal(buffer, fmt, 0, args);
va_end(args);
return len;
}
size_t
-snprintf(char* buffer, size_t n, char* fmt, ...)
+ksnprintf(char* buffer, size_t n, char* fmt, ...)
{
va_list args;
va_start(args, fmt);
- size_t len = __sprintf_internal(buffer, fmt, n, args);
+ size_t len = __ksprintf_internal(buffer, fmt, n, args);
va_end(args);
return len;
}
\ No newline at end of file
--- /dev/null
+#include <klibc/stdio.h>
+#include <lunaix/lunistd.h>
+#include <lunaix/spike.h>
+#include <ulibc/stdio.h>
+
+// This is VERY bad implementation as it mixes both kernel and user space
+// code together. It is here however, just for the convenience of our testing
+// program.
+// FIXME Eliminate this when we're able to load program.
+
+void __USER__
+printf(const char* fmt, ...)
+{
+ const char buf[512];
+ va_list args;
+ va_start(args, fmt);
+
+ size_t sz = __ksprintf_internal(buf, fmt, 512, args);
+
+ write(stdout, buf, sz);
+
+ va_end(args);
+}
\ No newline at end of file