xref: /reactos/base/system/diskpart/interpreter.c (revision 321bcc05)
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, LPWSTR *argv);
13 BOOL rem_main(INT argc, LPWSTR *argv);
14 
15 
16 COMMAND cmds[] =
17 {
18     {L"active",      active_main,      IDS_HELP_CMD_ACTIVE,      IDS_HELP_CMD_DESC_ACTIVE},
19     {L"add",         add_main,         IDS_HELP_CMD_ADD,         IDS_HELP_CMD_DESC_ADD},
20     {L"assign",      assign_main,      IDS_HELP_CMD_ASSIGN,      IDS_HELP_CMD_DESC_ASSIGN},
21     {L"attach",      attach_main,      IDS_HELP_CMD_ATTACH,      IDS_HELP_CMD_DESC_ATTACH},
22     {L"attributes",  attributes_main,  IDS_HELP_CMD_ATTRIBUTES,  IDS_HELP_CMD_DESC_ATTRIBUTES},
23     {L"automount",   automount_main,   IDS_HELP_CMD_AUTOMOUNT,   IDS_HELP_CMD_DESC_AUTOMOUNT},
24     {L"break",       break_main,       IDS_HELP_CMD_BREAK,       IDS_HELP_CMD_DESC_BREAK},
25     {L"clean",       clean_main,       IDS_HELP_CMD_CLEAN,       IDS_HELP_CMD_DESC_CLEAN},
26     {L"compact",     compact_main,     IDS_HELP_CMD_COMPACT,     IDS_HELP_CMD_DESC_COMPACT},
27     {L"convert",     convert_main,     IDS_HELP_CMD_CONVERT,     IDS_HELP_CMD_DESC_CONVERT},
28     {L"create",      create_main,      IDS_HELP_CMD_CREATE,      IDS_HELP_CMD_DESC_CREATE},
29     {L"delete",      delete_main,      IDS_HELP_CMD_DELETE,      IDS_HELP_CMD_DESC_DELETE},
30     {L"detail",      detail_main,      IDS_HELP_CMD_DETAIL,      IDS_HELP_CMD_DESC_DETAIL},
31     {L"detach",      detach_main,      IDS_HELP_CMD_DETACH,      IDS_HELP_CMD_DESC_DETACH},
32     {L"exit",        NULL,             IDS_NONE,                 IDS_HELP_CMD_DESC_EXIT},
33     {L"expand",      expand_main,      IDS_HELP_CMD_EXPAND,      IDS_HELP_CMD_DESC_EXPAND},
34     {L"extend",      extend_main,      IDS_HELP_CMD_EXTEND,      IDS_HELP_CMD_DESC_EXTEND},
35     {L"filesystems", filesystems_main, IDS_HELP_CMD_FILESYSTEMS, IDS_HELP_CMD_DESC_FS},
36     {L"format",      format_main,      IDS_HELP_CMD_FORMAT,      IDS_HELP_CMD_DESC_FORMAT},
37     {L"gpt",         gpt_main,         IDS_HELP_CMD_GPT,         IDS_HELP_CMD_DESC_GPT},
38     {L"help",        help_main,        IDS_HELP_CMD_HELP,        IDS_HELP_CMD_DESC_HELP},
39     {L"import",      import_main,      IDS_HELP_CMD_IMPORT,      IDS_HELP_CMD_DESC_IMPORT},
40     {L"inactive",    inactive_main,    IDS_HELP_CMD_INACTIVE,    IDS_HELP_CMD_DESC_INACTIVE},
41     {L"list",        list_main,        IDS_HELP_CMD_LIST,        IDS_HELP_CMD_DESC_LIST},
42     {L"merge",       merge_main,       IDS_HELP_CMD_MERGE,       IDS_HELP_CMD_DESC_MERGE},
43     {L"offline",     offline_main,     IDS_HELP_CMD_OFFLINE,     IDS_HELP_CMD_DESC_OFFLINE},
44     {L"online",      online_main,      IDS_HELP_CMD_ONLINE,      IDS_HELP_CMD_DESC_ONLINE},
45     {L"recover",     recover_main,     IDS_HELP_CMD_RECOVER,     IDS_HELP_CMD_DESC_RECOVER},
46     {L"rem",         NULL,             IDS_NONE,                 IDS_HELP_CMD_DESC_REM},
47     {L"remove",      remove_main,      IDS_HELP_CMD_REMOVE,      IDS_HELP_CMD_DESC_REMOVE},
48     {L"repair",      repair_main,      IDS_HELP_CMD_REPAIR,      IDS_HELP_CMD_DESC_REPAIR},
49     {L"rescan",      rescan_main,      IDS_HELP_CMD_RESCAN,      IDS_HELP_CMD_DESC_RESCAN},
50     {L"retain",      retain_main,      IDS_HELP_CMD_RETAIN,      IDS_HELP_CMD_DESC_RETAIN},
51     {L"san",         san_main,         IDS_HELP_CMD_SAN,         IDS_HELP_CMD_DESC_SAN},
52     {L"select",      select_main,      IDS_HELP_CMD_SELECT,      IDS_HELP_CMD_DESC_SELECT},
53     {L"setid",       setid_main,       IDS_HELP_CMD_SETID,       IDS_HELP_CMD_DESC_SETID},
54     {L"shrink",      shrink_main,      IDS_HELP_CMD_SHRINK,      IDS_HELP_CMD_DESC_SHRINK},
55     {L"uniqueid",    uniqueid_main,    IDS_HELP_CMD_UNIQUEID,    IDS_HELP_CMD_DESC_UNIQUEID},
56     {NULL,           NULL,             IDS_NONE,                 IDS_NONE}
57 };
58 
59 
60 /* FUNCTIONS *****************************************************************/
61 
62 /*
63  * InterpretCmd(char *cmd_line, char *arg_line):
64  * compares the command name to a list of available commands, and
65  * determines which function to envoke.
66  */
67 BOOL
68 InterpretCmd(int argc, LPWSTR *argv)
69 {
70     PCOMMAND cmdptr;
71 
72     /* First, determine if the user wants to exit
73        or to use a comment */
74     if(wcsicmp(argv[0], L"exit") == 0)
75         return FALSE;
76 
77     if(wcsicmp(argv[0], L"rem") == 0)
78         return TRUE;
79 
80     /* Scan internal command table */
81     for (cmdptr = cmds; cmdptr->name; cmdptr++)
82     {
83         if (wcsicmp(argv[0], cmdptr->name) == 0)
84             return cmdptr->func(argc, argv);
85     }
86 
87     help_cmdlist();
88 
89     return TRUE;
90 }
91 
92 
93 /*
94  * InterpretScript(char *line):
95  * The main function used for when reading commands from scripts.
96  */
97 BOOL
98 InterpretScript(LPWSTR input_line)
99 {
100     LPWSTR args_vector[MAX_ARGS_COUNT];
101     INT args_count = 0;
102     BOOL bWhiteSpace = TRUE;
103     LPWSTR ptr;
104 
105     memset(args_vector, 0, sizeof(args_vector));
106 
107     ptr = input_line;
108     while (*ptr != 0)
109     {
110         if (iswspace(*ptr) || *ptr == L'\n')
111         {
112             *ptr = 0;
113             bWhiteSpace = TRUE;
114         }
115         else
116         {
117             if ((bWhiteSpace == TRUE) && (args_count < MAX_ARGS_COUNT))
118             {
119                 args_vector[args_count] = ptr;
120                 args_count++;
121             }
122 
123             bWhiteSpace = FALSE;
124         }
125 
126         ptr++;
127     }
128 
129     /* sends the string to find the command */
130     return InterpretCmd(args_count, args_vector);
131 }
132 
133 
134 /*
135  * InterpretMain():
136  * Contents for the main program loop as it reads each line, and then
137  * it sends the string to interpret_line, where it determines what
138  * command to use.
139  */
140 VOID
141 InterpretMain(VOID)
142 {
143     WCHAR input_line[MAX_STRING_SIZE];
144     LPWSTR args_vector[MAX_ARGS_COUNT];
145     INT args_count = 0;
146     BOOL bWhiteSpace = TRUE;
147     BOOL bRun = TRUE;
148     LPWSTR ptr;
149 
150     while (bRun == TRUE)
151     {
152         args_count = 0;
153         memset(args_vector, 0, sizeof(args_vector));
154 
155         /* shown just before the input where the user places commands */
156         PrintResourceString(IDS_APP_PROMPT);
157 
158         /* gets input from the user. */
159         fgetws(input_line, MAX_STRING_SIZE, stdin);
160 
161         ptr = input_line;
162         while (*ptr != 0)
163         {
164             if (iswspace(*ptr) || *ptr == L'\n')
165             {
166                 *ptr = 0;
167                 bWhiteSpace = TRUE;
168             }
169             else
170             {
171                 if ((bWhiteSpace == TRUE) && (args_count < MAX_ARGS_COUNT))
172                 {
173                     args_vector[args_count] = ptr;
174                     args_count++;
175                 }
176                 bWhiteSpace = FALSE;
177             }
178             ptr++;
179         }
180 
181         /* sends the string to find the command */
182         bRun = InterpretCmd(args_count, args_vector);
183     }
184 }
185