Skip to content

Commit 8ef9c08

Browse files
committed
[Breaking] Add context pointer to character conversion
This allows passing general read and write lambdas that require a pointer to some context. The context should usually fold away, since character conversion is always_inline.
1 parent aacd8d6 commit 8ef9c08

File tree

14 files changed

+87
-74
lines changed

14 files changed

+87
-74
lines changed

mos-platform/atari8-common/getchar.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#include <atari.h>
22
#include <stdio.h>
33

4-
__attribute__((always_inline, weak)) int __to_ascii(int (*read)(void)) {
5-
int c = read();
4+
__attribute__((always_inline, weak)) int __to_ascii(void *ctx,
5+
int (*read)(void *ctx)) {
6+
int c = read(ctx);
67
switch (c) {
7-
case 0x1e:
8-
return '\b';
9-
case 0x7f:
10-
return '\t';
11-
case 0x9b:
12-
return '\n';
13-
case 0xfd:
14-
return '\a';
15-
default:
16-
return c;
8+
case 0x1e:
9+
return '\b';
10+
case 0x7f:
11+
return '\t';
12+
case 0x9b:
13+
return '\n';
14+
case 0xfd:
15+
return '\a';
16+
default:
17+
return c;
1718
}
1819
}
1920

mos-platform/atari8-common/putchar.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#include <stdio.h>
22

3-
__attribute__((always_inline, weak)) void __from_ascii(char c,
4-
void (*write)(char c)) {
3+
__attribute__((always_inline, weak)) void
4+
__from_ascii(char c, void *ctx, void (*write)(char c, void *ctx)) {
55
if (__builtin_expect(c == '\n', 0))
6-
write(0x9b);
6+
write(0x9b, ctx);
77
else if (__builtin_expect(c == '\t', 0))
8-
write(0x7f);
8+
write(0x7f, ctx);
99
else if (__builtin_expect(c == '\a', 0))
10-
write(0xfd);
10+
write(0xfd, ctx);
1111
else if (__builtin_expect(c == '\b', 0))
12-
write(0x1e);
12+
write(0x1e, ctx);
1313
else
14-
write(c);
14+
write(c, ctx);
1515
}
1616

1717
// Send character output via HATABS/IOCB0 which the OS opens to "E:"

mos-platform/commodore/char-conv.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@ asm(".section .init.250,\"axR\",@progbits\n"
1111
" lda #0x0e\n"
1212
" jsr __CHROUT\n");
1313

14-
__attribute__((always_inline, weak)) void __from_ascii(char c,
15-
void (*write)(char c)) {
14+
__attribute__((always_inline, weak)) void
15+
__from_ascii(char c, void *ctx, void (*write)(char c, void *ctx)) {
1616
if (__builtin_expect(c == '\n', 0))
17-
write('\r');
17+
write('\r', ctx);
1818
else if (__builtin_expect(c == '\b', 0))
19-
write('\x9d'); // CURSOR LEFT
19+
write('\x9d', ctx); // CURSOR LEFT
2020
else if ('a' <= c && c <= 'z')
21-
write(c & ~0x20);
21+
write(c & ~0x20, ctx);
2222
else if ('A' <= c && c <= 'Z')
23-
write(c | 0x80);
23+
write(c | 0x80, ctx);
2424
else
25-
write(c);
25+
write(c, ctx);
2626
}
2727

28-
__attribute__((always_inline, weak)) int __to_ascii(int (*read)(void)) {
29-
int c = read();
28+
__attribute__((always_inline, weak)) int __to_ascii(void *ctx,
29+
int (*read)(void *ctx)) {
30+
int c = read(ctx);
3031
if (__builtin_expect(c == '\r', 0))
3132
return '\n';
3233
else if (__builtin_expect(c == '\x9d', 0)) // CURSOR LEFT
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#include <stdio.h>
22

3-
static char buf[36];
4-
static char* bptr;
5-
63
extern "C" const char *_translate_filename(const char *filename) {
7-
bptr = buf;
4+
static char buf[36];
5+
char *bptr = buf;
86
for (const char *s = filename; *s; ++s)
9-
__from_ascii(*s, [](char c) { *bptr++ = c; });
7+
__from_ascii(*s, &bptr, [](char c, void *ctx) {
8+
char *&bptr = *(char **)ctx;
9+
*bptr++ = c;
10+
});
1011
*bptr = '\0';
1112
return buf;
1213
}

mos-platform/common/c/char-conv.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
__attribute__((always_inline, weak)) void __from_ascii(char c,
2-
void (*write)(char c)) {
3-
write(c);
1+
__attribute__((always_inline, weak)) void
2+
__from_ascii(char c, void *ctx, void (*write)(char c, void *ctx)) {
3+
write(c, ctx);
44
}
55

6-
__attribute__((always_inline, weak)) int __to_ascii(int (*read)(void)) {
7-
return read();
6+
__attribute__((always_inline, weak)) int __to_ascii(void *ctx,
7+
int (*read)(void *ctx)) {
8+
return read(ctx);
89
}

mos-platform/common/c/stdio-minimal.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ __attribute__((weak)) int fputs(const char *__restrict__ s,
3535

3636
__attribute__((weak)) int getc(FILE *stream) { return fgetc(stream); }
3737

38-
__attribute__((weak)) int getchar(void) { return __to_ascii(__getchar); }
38+
static int getchar_wrapper(void *ctx) { return __getchar(); }
39+
__attribute__((weak)) int getchar(void) {
40+
return __to_ascii(NULL, getchar_wrapper);
41+
}
3942

4043
__attribute__((weak)) int putc(int c, FILE *stream) { return fputc(c, stream); }
4144

45+
static void putchar_wrapper(char c, void *ctx) { __putchar(c); }
4246
__attribute__((weak)) int putchar(int c) {
43-
__from_ascii(c, __putchar);
47+
__from_ascii(c, NULL, putchar_wrapper);
4448
return c;
4549
}
4650

mos-platform/common/c/ungetc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int ungetc(int c, FILE *stream) {
1313
return c;
1414
}
1515

16-
static int ungetc_getchar(void) {
16+
static int ungetc_getchar(void* ctx) {
1717
if (full) {
1818
full = false;
1919
return ungetc_buffer;
@@ -22,4 +22,4 @@ static int ungetc_getchar(void) {
2222
}
2323
}
2424

25-
int getchar(void) { return __to_ascii(ungetc_getchar); }
25+
int getchar(void) { return __to_ascii(NULL, ungetc_getchar); }

mos-platform/common/include/stdio.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ typedef struct _FILE FILE;
2424
#define FOPEN_MAX 8
2525

2626
/* See setvbuf(), third argument */
27-
#define _IOFBF (1u<<0)
28-
#define _IOLBF (1u<<1)
29-
#define _IONBF (1u<<2)
27+
#define _IOFBF (1u << 0)
28+
#define _IOLBF (1u << 1)
29+
#define _IONBF (1u << 2)
3030

3131
typedef uint64_t fpos_t;
3232

@@ -115,11 +115,13 @@ void perror(const char *s);
115115

116116
// Write a sequence of characters in the target's character set that
117117
// correspond to the given ASCII character.
118-
__attribute__((always_inline)) void __from_ascii(char c, void (*write)(char c));
118+
__attribute__((always_inline)) void
119+
__from_ascii(char c, void *ctx, void (*write)(char c, void *ctx));
119120

120121
// Read a sequence of characters in the target's character set and return the
121122
// corrsponding ASCII character.
122-
__attribute__((always_inline)) int __to_ascii(int (*read)(void));
123+
__attribute__((always_inline)) int __to_ascii(void *ctx,
124+
int (*read)(void *ctx));
123125

124126
// Put a character in the target's character set out to the target's
125127
// equivalent of file descriptor 1 (stdout).

mos-platform/cpm65/putchar.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
#include <cpm.h>
1010
#include <stdio.h>
1111

12-
__attribute__((always_inline, weak)) void __from_ascii(char c,
13-
void (*emit)(char c)) {
12+
__attribute__((always_inline, weak)) void
13+
__from_ascii(char c, void *ctx, void (*emit)(char c, void *ctx)) {
1414
if (__builtin_expect(c == '\n', 0))
15-
emit('\r');
16-
emit(c);
15+
emit('\r', ctx);
16+
emit(c, ctx);
1717
}
1818

1919
void __putchar(char c) { cpm_conout(c); }

mos-platform/cx16/char-conv.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ asm(".section .init.250,\"axR\",@progbits\n"
99
" lda #0x0f\n"
1010
" jsr __CHROUT\n");
1111

12-
__attribute__((always_inline, weak)) void __from_ascii(char c,
13-
void (*write)(char c)) {
12+
__attribute__((always_inline, weak)) void
13+
__from_ascii(char c, void *ctx, void (*write)(char c, void *ctx)) {
1414
if (__builtin_expect(c == '\n', 0))
15-
write('\r');
15+
write('\r', ctx);
1616
else if (__builtin_expect(c == '\b', 0))
17-
write('\x9d'); // CURSOR LEFT
17+
write('\x9d', ctx); // CURSOR LEFT
1818
else
19-
write(c);
19+
write(c, ctx);
2020
}
2121

22-
__attribute__((always_inline, weak)) int __to_ascii(int (*read)(void)) {
23-
int c = read();
22+
__attribute__((always_inline, weak)) int __to_ascii(void *ctx,
23+
int (*read)(void *ctx)) {
24+
int c = read(ctx);
2425
switch (c) {
2526
case '\r':
2627
return '\n';

0 commit comments

Comments
 (0)