Skip to content

Commit

Permalink
stdio: add swappable contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
stsp committed Oct 13, 2024
1 parent cb93c80 commit 22c4a5e
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 5 deletions.
29 changes: 28 additions & 1 deletion src/libc/ansi/stdio/stdaux.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,36 @@
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#include <stdio.h>
#include <libc/file.h>
#include <libc/djctx.h>

FILE __dj_stdaux = {
FILE __dj_stdaux;

static const FILE __dj_stdaux_init = {
0, 0, 0, 0,
_IORW | _IONBF,
3
};

struct ste_state {
FILE __dj_stdaux;
};

static struct ste_state *ste;

static const struct ste_state sinit =
{
.__dj_stdaux = __dj_stdaux_init,
};

static void ste_pre(void)
{
#define _SV(x) ste->x = x
_SV(__dj_stdaux);
}
static void ste_post(void)
{
#define _RS(x) x = ste->x
_RS(__dj_stdaux);
}
DJ64_DEFINE_SWAPPABLE_CONTEXT2(ste_state, ste, sinit,
ste_pre(), ste_post());
29 changes: 28 additions & 1 deletion src/libc/ansi/stdio/stderr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,36 @@
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#include <stdio.h>
#include <libc/file.h>
#include <libc/djctx.h>

FILE __dj_stderr = {
FILE __dj_stderr;

static const FILE __dj_stderr_init = {
0, 0, 0, 0,
_IOWRT | _IONBF,
2
};

struct ste_state {
FILE __dj_stderr;
};

static struct ste_state *ste;

static const struct ste_state sinit =
{
.__dj_stderr = __dj_stderr_init,
};

static void ste_pre(void)
{
#define _SV(x) ste->x = x
_SV(__dj_stderr);
}
static void ste_post(void)
{
#define _RS(x) x = ste->x
_RS(__dj_stderr);
}
DJ64_DEFINE_SWAPPABLE_CONTEXT2(ste_state, ste, sinit,
ste_pre(), ste_post());
29 changes: 28 additions & 1 deletion src/libc/ansi/stdio/stdin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,36 @@
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#include <stdio.h>
#include <libc/file.h>
#include <libc/djctx.h>

FILE __dj_stdin = {
FILE __dj_stdin;

static const FILE __dj_stdin_init = {
0, 0, 0, 0,
_IOREAD | _IOLBF,
0
};

struct ste_state {
FILE __dj_stdin;
};

static struct ste_state *ste;

static const struct ste_state sinit =
{
.__dj_stdin = __dj_stdin_init,
};

static void ste_pre(void)
{
#define _SV(x) ste->x = x
_SV(__dj_stdin);
}
static void ste_post(void)
{
#define _RS(x) x = ste->x
_RS(__dj_stdin);
}
DJ64_DEFINE_SWAPPABLE_CONTEXT2(ste_state, ste, sinit,
ste_pre(), ste_post());
29 changes: 28 additions & 1 deletion src/libc/ansi/stdio/stdout.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,36 @@
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#include <stdio.h>
#include <libc/file.h>
#include <libc/djctx.h>

FILE __dj_stdout = {
FILE __dj_stdout;

static const FILE __dj_stdout_init = {
0, 0, 0, 0,
_IOWRT | _IOFBF,
1
};

struct ste_state {
FILE __dj_stdout;
};

static struct ste_state *ste;

static const struct ste_state sinit =
{
.__dj_stdout = __dj_stdout_init,
};

static void ste_pre(void)
{
#define _SV(x) ste->x = x
_SV(__dj_stdout);
}
static void ste_post(void)
{
#define _RS(x) x = ste->x
_RS(__dj_stdout);
}
DJ64_DEFINE_SWAPPABLE_CONTEXT2(ste_state, ste, sinit,
ste_pre(), ste_post());
29 changes: 28 additions & 1 deletion src/libc/ansi/stdio/stdprn.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,36 @@
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#include <stdio.h>
#include <libc/file.h>
#include <libc/djctx.h>

FILE __dj_stdprn = {
FILE __dj_stdprn;

static const FILE __dj_stdprn_init = {
0, 0, 0, 0,
_IOWRT | _IONBF,
4
};

struct ste_state {
FILE __dj_stdprn;
};

static struct ste_state *ste;

static const struct ste_state sinit =
{
.__dj_stdprn = __dj_stdprn_init,
};

static void ste_pre(void)
{
#define _SV(x) ste->x = x
_SV(__dj_stdprn);
}
static void ste_post(void)
{
#define _RS(x) x = ste->x
_RS(__dj_stdprn);
}
DJ64_DEFINE_SWAPPABLE_CONTEXT2(ste_state, ste, sinit,
ste_pre(), ste_post());

0 comments on commit 22c4a5e

Please sign in to comment.