aboutsummaryrefslogtreecommitdiff
path: root/src/shell.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/shell.c')
-rw-r--r--src/shell.c177
1 files changed, 0 insertions, 177 deletions
diff --git a/src/shell.c b/src/shell.c
deleted file mode 100644
index 4faa0a8..0000000
--- a/src/shell.c
+++ /dev/null
@@ -1,177 +0,0 @@
1#define SHELL_C
2
3#include "shell.h"
4
5static void cleanwhitespaces(char *line);
6static int parseline(char *line, char **v);
7
8bool
9checkfiles()
10{
11 /* TODO: add more checks (other files, use checksum...) */
12 /* How to check for pruning tables with new method? */
13 /* Solution: use list of steps */
14 /*
15 char fname[strlen(tabledir)+100];
16 int i;
17
18 for (i = 0; all_pd[i] != NULL; i++) {
19 strcpy(fname, tabledir);
20 strcat(fname, "/");
21 strcat(fname, all_pd[i]->filename);
22 if ((f = fopen(fname, "rb")) == NULL)
23 return false;
24 else
25 fclose(f);
26 }
27 */
28
29 return true;
30}
31
32static void
33cleanwhitespaces(char *line)
34{
35 char *i;
36
37 for (i = line; *i != 0; i++)
38 if (*i == '\t' || *i == '\n')
39 *i = ' ';
40}
41
42/* This function assumes that **v is large enough */
43static int
44parseline(char *line, char **v)
45{
46 char *t;
47 int n = 0;
48
49 cleanwhitespaces(line);
50
51 for (t = strtok(line, " "); t != NULL; t = strtok(NULL, " "))
52 strcpy(v[n++], t);
53
54 return n;
55}
56
57void
58exec_args(int c, char **v)
59{
60 int i;
61 char line[MAXLINELEN];
62 Command *cmd = NULL;
63 CommandArgs *args;
64 Alg *scramble;
65
66 for (i = 0; commands[i] != NULL; i++)
67 if (!strcmp(v[0], commands[i]->name))
68 cmd = commands[i];
69
70 if (cmd == NULL) {
71 fprintf(stderr, "%s: command not found\n", v[0]);
72 return;
73 }
74
75 args = cmd->parse_args(c-1, &v[1]);
76 if (!args->success) {
77 fprintf(stderr, "usage: %s\n", cmd->usage);
78 return;
79 }
80
81 if (args->scrstdin) {
82 while (true) {
83 if (fgets(line, MAXLINELEN, stdin) == NULL) {
84 clearerr(stdin);
85 break;
86 }
87
88 scramble = new_alg(line);
89
90 printf(">>> Line: %s", line);
91
92 if (scramble != NULL && scramble->len > 0) {
93 args->scramble = scramble;
94 cmd->exec(args);
95 free_alg(scramble);
96 args->scramble = NULL;
97 }
98 }
99 } else {
100 cmd->exec(args);
101 }
102 free_args(args);
103}
104
105void
106launch(bool batchmode)
107{
108 int i, shell_argc;
109 char line[MAXLINELEN], **shell_argv;
110
111 shell_argv = malloc(MAXNTOKENS * sizeof(char *));
112 for (i = 0; i < MAXNTOKENS; i++)
113 shell_argv[i] = malloc((MAXTOKENLEN+1) * sizeof(char));
114
115 if (!batchmode) {
116 fprintf(stderr, "Welcome to Nissy "VERSION".\n"
117 "Type \"commands\" for a list of commands.\n");
118 }
119
120 while (true) {
121 if (!batchmode) {
122 fprintf(stdout, "nissy-# ");
123 }
124
125 if (fgets(line, MAXLINELEN, stdin) == NULL)
126 break;
127
128 if (batchmode) {
129 printf(">>>\n"
130 ">>> Executing command: %s"
131 ">>>\n", line);
132 }
133
134 shell_argc = parseline(line, shell_argv);
135
136 if (shell_argc > 0)
137 exec_args(shell_argc, shell_argv);
138 }
139
140 for (i = 0; i < MAXNTOKENS; i++)
141 free(shell_argv[i]);
142 free(shell_argv);
143}
144
145#ifndef TEST
146int
147main(int argc, char *argv[])
148{
149 char *closing_cmd[1] = { "freemem" };
150
151 init_env();
152 init_trans();
153
154 if (!checkfiles()) {
155 fprintf(stderr,
156 "--- Warning ---\n"
157 "Some pruning tables are missing or unreadable\n"
158 "You can generate them with `nissy gen'.\n"
159 "---------------\n\n"
160 );
161 }
162
163 if (argc > 1) {
164 if (!strcmp(argv[1], "-b")) {
165 launch(true);
166 } else {
167 exec_args(argc-1, &argv[1]);
168 }
169 } else {
170 launch(false);
171 }
172
173 exec_args(1, closing_cmd);
174
175 return 0;
176}
177#endif

Generated with cgit - Back to sebastiano.tronto.net