Updated LICENSE and CHANGELOG.
[idzebra-moved-to-github.git] / isam / issh.c
1 /*
2  * Excersizer-application for the isam subsystem. Don't play with it.
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9
10 #include <isam.h>
11
12 static ISAM is = 0;
13
14 int c_open(char *p);
15 int c_close(char *p);
16 int c_write(char *p);
17 int c_merge(char *p);
18 int c_rewind(char *p);
19 int c_read(char *p);
20 int c_exit(char *p);
21 int c_help(char *p);
22 int c_list(char *p);
23 int c_pos(char *p);
24
25 struct
26 {
27     char *word;
28     int (*fun)(char *p);
29 } cmdl[] =
30 {
31     {"open", c_open},
32     {"close", c_close},
33     {"write", c_write},
34     {"merge", c_merge},
35     {"rewind", c_rewind},
36     {"read", c_read},
37     {"exit", c_exit},
38     {"quit", c_exit},
39     {"help", c_help},
40     {"list", 0},
41     {"pos", c_pos},
42
43     {0,0}
44 };
45
46
47 int c_pos(char *p)
48 {
49 }
50
51 int c_open(char *p)
52 {
53     if (!*p)
54     {
55         printf("Usage: open name\n");
56         return -1;
57     }
58     if (!(is = is_open(p, 1)))
59         return -1;
60     return 0;
61 }
62
63 int c_close(char *p)
64 {
65     if (!is)
66     {
67         printf("Open first.\n");
68         return -1;
69     }
70     is_close(is);
71     return 0;
72 }
73
74 int c_write(char *p) {}
75
76 int c_merge(char *p)
77 {
78     char line[100], buf[1024], ch;
79     int pos = 0, num = 0;
80     int op, key;
81     int val;
82
83     if (!is)
84     {
85         printf("Open first.\n");
86         return -1;
87     }
88     if (!sscanf(p, "%d", &val))
89     {
90         printf("Usage: merge <address>\n");
91         return -1;
92     }
93     printf("Enter pairs of <operation> <key>. Terminate with '.'\n");
94     while (gets(line) && *line != '.')
95     {
96         if (sscanf(line, "%d %d", &op, &key) < 2)
97         {
98             printf("Error in input\n");
99             return -1;
100         }
101         ch = op;
102         *(buf + pos++) = ch;
103         memcpy(buf + pos, &key, sizeof(key));
104         pos += sizeof(key);
105         num++;
106     }
107     printf("is_merge(-->%d) returns: %d\n", val, is_merge(is, val, num , buf));
108     return 0;
109 }
110
111 int c_rewind(char *p)
112 {
113     if (!is)
114     {
115         printf("Open first.\n");
116         return -1;
117     }
118 }
119
120 int c_read(char *p) {}
121
122 int c_exit(char *p)
123 {
124     exit(0);
125 }
126
127 int c_help(char *p)
128 {
129     int i;
130
131     printf("ISSH: ISAM debugging shell.\n");
132     printf("Commands:\n");
133     for (i = 0; cmdl[i].word; i++)
134         printf("    %s %s\n", cmdl[i].word, cmdl[i].fun ? "": "UNDEFINED");
135     return 0;
136 }
137
138 int main()
139 {
140     char line[1024];
141     static char word[1024] = "help", arg[1024] = "";
142     int i;
143
144     log_init(LOG_ALL, "issh", 0);
145
146     common_resource = res_open("testres");
147     assert(common_resource);
148
149     for (;;)
150     {
151         printf("ISSH> ");
152         fflush(stdout);
153         if (!gets(line))
154             return 0;
155         *arg = '\0';
156         if (*line && sscanf(line, "%s %[^;]", word, arg) < 1)
157             abort();
158         for (i = 0; cmdl[i].word; i++)
159             if (!strncmp(cmdl[i].word, word, strlen(word)))
160             {
161                 printf("%s\n", (*cmdl[i].fun)(arg) == 0 ? "OK" : "FAILED");
162                 break;
163             }
164         if (!cmdl[i].word)
165             (*c_help)("");
166     }
167 }