aboutsummaryrefslogtreecommitdiff
path: root/old/2021-11-10-beforeremovingchecker/shell.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano.tronto@gmail.com>2021-11-11 21:37:34 +0100
committerSebastiano Tronto <sebastiano.tronto@gmail.com>2021-11-11 21:37:34 +0100
commit3568412f8f230774d0d11d7ed1c897424f95d3ef (patch)
tree77223792d8c925a9b1fc32b3f4341e943b5f8209 /old/2021-11-10-beforeremovingchecker/shell.c
parent67e1b5e6e6a2c917a2fe58a37a1382c982b1e5c5 (diff)
downloadnissy-3568412f8f230774d0d11d7ed1c897424f95d3ef.tar.gz
nissy-3568412f8f230774d0d11d7ed1c897424f95d3ef.zip
Rewritten from scratch. Welocme nissy 2.0!
Diffstat (limited to '')
-rw-r--r--old/2021-11-10-beforeremovingchecker/shell.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/old/2021-11-10-beforeremovingchecker/shell.c b/old/2021-11-10-beforeremovingchecker/shell.c
new file mode 100644
index 0000000..e591faa
--- /dev/null
+++ b/old/2021-11-10-beforeremovingchecker/shell.c
@@ -0,0 +1,99 @@
1#include "shell.h"
2
3static void cleanwhitespaces(char *line);
4static int parseline(char *line, char **v);
5
6static void
7cleanwhitespaces(char *line)
8{
9 char *i;
10
11 for (i = line; *i != 0; i++)
12 if (*i == '\t' || *i == '\n')
13 *i = ' ';
14}
15
16/* This function assumes that **v is large enough */
17static int
18parseline(char *line, char **v)
19{
20 char *t;
21 int n = 0;
22
23 cleanwhitespaces(line);
24
25 for (t = strtok(line, " "); t != NULL; t = strtok(NULL, " "))
26 strcpy(v[n++], t);
27
28 return n;
29}
30
31void
32exec_args(int c, char **v)
33{
34 int i;
35 Command *cmd = NULL;
36 CommandArgs *args;
37
38 for (i = 0; i < NCOMMANDS; i++)
39 if (commands[i] != NULL && !strcmp(v[0], commands[i]->name))
40 cmd = commands[i];
41
42 if (cmd == NULL) {
43 fprintf(stderr, "%s: command not found\n", v[0]);
44 return;
45 }
46
47 args = cmd->parse_args(c-1, &v[1]);
48 if (!args->success) {
49 fprintf(stderr, "usage: %s\n", cmd->usage);
50 return;
51 }
52
53 cmd->exec(args);
54 free_args(args);
55}
56
57void
58launch()
59{
60 int i, shell_argc;
61 char line[MAXLINELEN], **shell_argv;
62
63 shell_argv = malloc(MAXNTOKENS * sizeof(char *));
64 for (i = 0; i < MAXNTOKENS; i++)
65 shell_argv[i] = malloc((MAXTOKENLEN+1) * sizeof(char));
66
67 fprintf(stderr, "Welcome to Nissy 2.0 (demo version).\n");
68 fprintf(stderr, "Limited commands available. ");
69 fprintf(stderr, "Type 'help' for a list.\n");
70
71 while (true) {
72 fprintf(stderr, "nissy-# ");
73 if (fgets(line, MAXLINELEN, stdin) == NULL)
74 break;
75 shell_argc = parseline(line, shell_argv);
76 exec_args(shell_argc, shell_argv);
77 }
78
79 for (i = 0; i < MAXNTOKENS; i++)
80 free(shell_argv[i]);
81 free(shell_argv);
82}
83
84/* We will have our main() here, for now */
85int
86main(int argc, char *argv[])
87{
88 init_moves();
89 init_trans();
90 init_coord();
91 init_symcoord();
92
93 if (argc > 1)
94 exec_args(argc-1, &argv[1]);
95 else
96 launch();
97
98 return 0;
99}

Generated with cgit - Back to sebastiano.tronto.net