summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-06-24 03:08:29 -0400
committerIan Moffett <ian@osmora.org>2025-06-24 03:08:29 -0400
commit21d6805767f5b277e00409bf1c3b61c6e125c5f0 (patch)
tree69a2103fe37592bd4a4ff88e5b9cb9f94dd43470
parente61f8826a78ce9558416a48275729b6bdcabfb7c (diff)
usr.bin: osh: Add comments
Introduce comments for the OSH interpreter. Comments are denoted by '@' and are ignored by OSH. Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r--usr.bin/osh/osh.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/usr.bin/osh/osh.c b/usr.bin/osh/osh.c
index 47ea695..04705f7 100644
--- a/usr.bin/osh/osh.c
+++ b/usr.bin/osh/osh.c
@@ -39,6 +39,7 @@
#include <stdio.h>
#define is_ascii(C) ((C) >= 0 && (C) <= 128)
+#define COMMENT '@'
#define WELCOME \
":::::::::::::::::::::::::::::::::::::::\n" \
":: OSMORA GATEWAY ~ Every key echos ::\n" \
@@ -157,6 +158,11 @@ parse_args(char *input, char *argv[], int max_args)
{
int argc = 0;
+ /* ignore comments */
+ if (*input == '@') {
+ return 0;
+ }
+
while (*input != '\0') {
/* skip leading spaces */
while (*input == ' ') {
@@ -168,11 +174,21 @@ parse_args(char *input, char *argv[], int max_args)
break;
}
+ /* comment? */
+ if (*input == COMMENT) {
+ break;
+ }
+
if (argc < max_args) {
argv[argc++] = input; /* mark start of the argument */
}
/* move forward until next space or end */
while (*input != '\0' && *input != ' ') {
+ /* ignore comments */
+ if (*input == COMMENT) {
+ return 0;
+ }
+
input++;
}
@@ -296,6 +312,17 @@ command_match(const char *input, int argc, char *argv[], bool wait)
}
}
+static void
+script_skip_comment(int fd)
+{
+ char c;
+
+ while (c != '\n') {
+ if (read(fd, &c, 1) <= 0)
+ break;
+ }
+}
+
static int
open_script(const char *pathname)
{
@@ -310,6 +337,12 @@ open_script(const char *pathname)
}
while (read(fd, &c, 1) > 0) {
+ /* Skip comments */
+ if (c == COMMENT) {
+ script_skip_comment(fd);
+ continue;
+ }
+
/* Skip blank newlines */
if (c == '\n' && buf_i == 0) {
continue;