-
Notifications
You must be signed in to change notification settings - Fork 1
Filter unsupported OSC output from host-shell #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+361
−30
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: 2020-2026 Dimitris Panokostas | ||
| * SPDX-License-Identifier: GPL-3.0-or-later | ||
| */ | ||
|
|
||
| #ifndef HOST_TERMINAL_FILTER_H | ||
| #define HOST_TERMINAL_FILTER_H | ||
|
|
||
| #define HOST_TERMINAL_TEXT 0 | ||
| #define HOST_TERMINAL_ESCAPE 1 | ||
| #define HOST_TERMINAL_OSC 2 | ||
| #define HOST_TERMINAL_OSC_ESCAPE 3 | ||
|
|
||
| struct host_terminal_filter | ||
| { | ||
| int state; | ||
| int utf8_remaining; | ||
| }; | ||
|
|
||
| static inline int host_terminal_filter_utf8_continuations(unsigned char value) | ||
| { | ||
| if (value >= 0xC2 && value <= 0xDF) { | ||
| return 1; | ||
| } | ||
| if (value >= 0xE0 && value <= 0xEF) { | ||
| return 2; | ||
| } | ||
| if (value >= 0xF0 && value <= 0xF4) { | ||
| return 3; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| static inline int host_terminal_filter_put(unsigned char *output, int output_size, | ||
| int *output_len, unsigned char value) | ||
| { | ||
| if (*output_len >= output_size) { | ||
| return 0; | ||
| } | ||
|
|
||
| output[(*output_len)++] = value; | ||
| return 1; | ||
| } | ||
|
|
||
| /* | ||
| * Convert host ANSI CSI sequences to the Amiga console's single-byte CSI and | ||
| * discard OSC sequences, which the Amiga console does not support. The state | ||
| * is retained so escape sequences may span HostShell_Read() calls. | ||
| * | ||
| * The output can be at most input_len + 1 bytes when a pending ESC from the | ||
| * previous call turns out to be literal. | ||
| */ | ||
| static inline int host_terminal_filter_process(struct host_terminal_filter *filter, | ||
| const unsigned char *input, int input_len, | ||
| unsigned char *output, int output_size) | ||
| { | ||
| int output_len = 0; | ||
|
|
||
| if (filter == NULL || input == NULL || input_len < 0 || | ||
| output == NULL || output_size < 0) { | ||
| return -1; | ||
| } | ||
|
|
||
| for (int i = 0; i < input_len; i++) { | ||
| unsigned char c = input[i]; | ||
|
|
||
| switch (filter->state) { | ||
| case HOST_TERMINAL_TEXT: | ||
| if (filter->utf8_remaining > 0 && c >= 0x80 && c <= 0xBF) { | ||
| filter->utf8_remaining--; | ||
| if (!host_terminal_filter_put(output, output_size, &output_len, c)) { | ||
| return -1; | ||
| } | ||
| } else { | ||
| filter->utf8_remaining = 0; | ||
| if (c == 0x1B) { | ||
| filter->state = HOST_TERMINAL_ESCAPE; | ||
| } else if (c == 0x9D) { | ||
| filter->state = HOST_TERMINAL_OSC; | ||
| } else { | ||
| filter->utf8_remaining = host_terminal_filter_utf8_continuations(c); | ||
| if (!host_terminal_filter_put(output, output_size, &output_len, c)) { | ||
| return -1; | ||
| } | ||
| } | ||
| } | ||
| break; | ||
|
|
||
| case HOST_TERMINAL_ESCAPE: | ||
| if (c == '[') { | ||
| if (!host_terminal_filter_put(output, output_size, &output_len, 0x9B)) { | ||
| return -1; | ||
| } | ||
| filter->state = HOST_TERMINAL_TEXT; | ||
| } else if (c == ']') { | ||
| filter->state = HOST_TERMINAL_OSC; | ||
| filter->utf8_remaining = 0; | ||
| } else { | ||
| if (!host_terminal_filter_put(output, output_size, &output_len, 0x1B) || | ||
| !host_terminal_filter_put(output, output_size, &output_len, c)) { | ||
| return -1; | ||
| } | ||
| filter->state = HOST_TERMINAL_TEXT; | ||
| filter->utf8_remaining = host_terminal_filter_utf8_continuations(c); | ||
| } | ||
| break; | ||
|
|
||
| case HOST_TERMINAL_OSC: | ||
| if (filter->utf8_remaining > 0 && c >= 0x80 && c <= 0xBF) { | ||
| filter->utf8_remaining--; | ||
| } else if (c == 0x07 || c == 0x9C) { | ||
| filter->state = HOST_TERMINAL_TEXT; | ||
| filter->utf8_remaining = 0; | ||
| } else if (c == 0x1B) { | ||
| filter->state = HOST_TERMINAL_OSC_ESCAPE; | ||
| filter->utf8_remaining = 0; | ||
| } else { | ||
| filter->utf8_remaining = host_terminal_filter_utf8_continuations(c); | ||
| } | ||
| break; | ||
|
|
||
| case HOST_TERMINAL_OSC_ESCAPE: | ||
| if (c == '\\' || c == 0x07 || c == 0x9C) { | ||
| filter->state = HOST_TERMINAL_TEXT; | ||
| filter->utf8_remaining = 0; | ||
| } else if (c != 0x1B) { | ||
| filter->state = HOST_TERMINAL_OSC; | ||
| filter->utf8_remaining = host_terminal_filter_utf8_continuations(c); | ||
| } | ||
| break; | ||
|
|
||
| default: | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| return output_len; | ||
| } | ||
|
|
||
| static inline int host_terminal_filter_finish(struct host_terminal_filter *filter, | ||
| unsigned char *output, int output_size) | ||
| { | ||
| int output_len = 0; | ||
|
|
||
| if (filter == NULL || output == NULL || output_size < 0) { | ||
| return -1; | ||
| } | ||
|
|
||
| if (filter->state == HOST_TERMINAL_ESCAPE && | ||
| !host_terminal_filter_put(output, output_size, &output_len, 0x1B)) { | ||
| return -1; | ||
| } | ||
|
|
||
| filter->state = HOST_TERMINAL_TEXT; | ||
| filter->utf8_remaining = 0; | ||
| return output_len; | ||
| } | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.