Add the Diskpart utility by Lee Schroeder (milawynsrealm).
[reactos.git] / reactos / base / system / diskpart / interpreter.c
1 /*
2 * PROJECT: ReactOS DiskPart
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/diskpart/interpreter.c
5 * PURPOSE: Reads the user input and then envokes the selected
6 * command by the user.
7 * PROGRAMMERS: Lee Schroeder
8 */
9
10 #include "diskpart.h"
11
12 BOOL exit_main(INT argc, WCHAR **argv);
13 BOOL rem_main(INT argc, WCHAR **argv);
14
15
16 COMMAND cmds[] =
17 {
18 {L"active", active_main, help_active},
19 {L"add", add_main, help_add},
20 {L"assign", assign_main, help_assign},
21 {L"attributes", attributes_main, help_attributes},
22 {L"automount", automount_main, help_automount},
23 {L"break", break_main, help_break},
24 {L"clean", clean_main, help_clean},
25 {L"compact", compact_main, help_compact},
26 {L"convert", convert_main, help_convert},
27 {L"create", create_main, help_create},
28 {L"delete", delete_main, help_delete},
29 {L"detail", detail_main, help_detail},
30 {L"detach", detach_main, help_detach},
31 {L"exit", exit_main, NULL},
32 {L"expand", expand_main, help_expand},
33 {L"extend", extend_main, help_extend},
34 {L"filesystems", filesystems_main, help_filesystems},
35 {L"format", format_main, help_format},
36 {L"gpt", gpt_main, help_gpt},
37 {L"help", help_main, help_help},
38 {L"list", list_main, help_list},
39 {L"import", import_main, help_import},
40 {L"inactive", inactive_main, help_inactive},
41 {L"merge", merge_main, help_merge},
42 {L"offline", offline_main, help_offline},
43 {L"online", online_main, help_online},
44 {L"recover", recover_main, help_recover},
45 {L"rem", rem_main, NULL},
46 {L"remove", remove_main, help_remove},
47 {L"repair", repair_main, help_repair},
48 {L"rescan", rescan_main, help_rescan},
49 {L"retain", retain_main, help_retain},
50 {L"san", san_main, help_san},
51 {L"select", select_main, help_select},
52 {L"setid", setid_main, help_setid},
53 {L"shrink", shrink_main, help_shrink},
54 {L"uniqueid", uniqueid_main, help_uniqueid},
55 {NULL, NULL, NULL}
56 };
57
58
59 /* FUNCTIONS *****************************************************************/
60
61 BOOL
62 exit_main(INT argc, WCHAR **argv)
63 {
64 return FALSE;
65 }
66
67
68 BOOL
69 rem_main(INT argc, WCHAR **argv)
70 {
71 return TRUE;
72 }
73
74
75 /*
76 * interpret_cmd(char *cmd_line, char *arg_line):
77 * compares the command name to a list of available commands, and
78 * determines which function to envoke.
79 */
80 BOOL
81 interpret_cmd(int argc, WCHAR **argv)
82 {
83 PCOMMAND cmdptr;
84
85 /* Scan internal command table */
86 for (cmdptr = cmds; cmdptr->name; cmdptr++)
87 {
88 if (wcsicmp(argv[0], cmdptr->name) == 0)
89 {
90 return cmdptr->func(argc, argv);
91 }
92 }
93
94 help_cmdlist();
95
96 return TRUE;
97 }
98
99
100 /*
101 * interpret_script(char *line):
102 * The main function used for when reading commands from scripts.
103 */
104 BOOL
105 interpret_script(WCHAR *input_line)
106 {
107 WCHAR *args_vector[MAX_ARGS_COUNT];
108 INT args_count = 0;
109 BOOL bWhiteSpace = TRUE;
110 WCHAR *ptr;
111
112 memset(args_vector, 0, sizeof(args_vector));
113
114 ptr = input_line;
115 while (*ptr != 0)
116 {
117 if (iswspace(*ptr) || *ptr == L'\n')
118 {
119 *ptr = 0;
120 bWhiteSpace = TRUE;
121 }
122 else
123 {
124 if ((bWhiteSpace == TRUE) && (args_count < MAX_ARGS_COUNT))
125 {
126 args_vector[args_count] = ptr;
127 args_count++;
128 }
129
130 bWhiteSpace = FALSE;
131 }
132
133 ptr++;
134 }
135
136 /* sends the string to find the command */
137 return interpret_cmd(args_count, args_vector);
138 }
139
140
141 /*
142 * interpret_main():
143 * Contents for the main program loop as it reads each line, and then
144 * it sends the string to interpret_line, where it determines what
145 * command to use.
146 */
147 BOOL
148 interpret_main(VOID)
149 {
150 WCHAR input_line[MAX_STRING_SIZE];
151 WCHAR *args_vector[MAX_ARGS_COUNT];
152 INT args_count = 0;
153 BOOL bWhiteSpace = TRUE;
154 WCHAR *ptr;
155
156 memset(args_vector, 0, sizeof(args_vector));
157
158 /* shown just before the input where the user places commands */
159 PrintResourceString(IDS_APP_PROMPT);
160
161 /* gets input from the user. */
162 fgetws(input_line, MAX_STRING_SIZE, stdin);
163
164 ptr = input_line;
165 while (*ptr != 0)
166 {
167 if (iswspace(*ptr) || *ptr == L'\n')
168 {
169 *ptr = 0;
170 bWhiteSpace = TRUE;
171 }
172 else
173 {
174 if ((bWhiteSpace == TRUE) && (args_count < MAX_ARGS_COUNT))
175 {
176 args_vector[args_count] = ptr;
177 args_count++;
178 }
179
180 bWhiteSpace = FALSE;
181 }
182
183 ptr++;
184 }
185
186 /* sends the string to find the command */
187 return interpret_cmd(args_count, args_vector);
188 }