xref: /reactos/base/system/diskpart/interpreter.c (revision 8e659192)
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 invokes 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 invoke.
66  */
67 BOOL
68 InterpretCmd(int argc, LPWSTR *argv)
69 {
70     PCOMMAND cmdptr;
71 
72     /* If no args provided */
73     if (argc < 1)
74         return TRUE;
75 
76     /* First, determine if the user wants to exit
77        or to use a comment */
78     if(wcsicmp(argv[0], L"exit") == 0)
79         return FALSE;
80 
81     if(wcsicmp(argv[0], L"rem") == 0)
82         return TRUE;
83 
84     /* Scan internal command table */
85     for (cmdptr = cmds; cmdptr->name; cmdptr++)
86     {
87         if (wcsicmp(argv[0], cmdptr->name) == 0)
88             return cmdptr->func(argc, argv);
89     }
90 
91     help_cmdlist();
92 
93     return TRUE;
94 }
95 
96 
97 /*
98  * InterpretScript(char *line):
99  * The main function used for when reading commands from scripts.
100  */
101 BOOL
102 InterpretScript(LPWSTR input_line)
103 {
104     LPWSTR args_vector[MAX_ARGS_COUNT];
105     INT args_count = 0;
106     BOOL bWhiteSpace = TRUE;
107     LPWSTR ptr;
108 
109     memset(args_vector, 0, sizeof(args_vector));
110 
111     ptr = input_line;
112     while (*ptr != 0)
113     {
114         if (iswspace(*ptr) || *ptr == L'\n')
115         {
116             *ptr = 0;
117             bWhiteSpace = TRUE;
118         }
119         else
120         {
121             if ((bWhiteSpace != FALSE) && (args_count < MAX_ARGS_COUNT))
122             {
123                 args_vector[args_count] = ptr;
124                 args_count++;
125             }
126 
127             bWhiteSpace = FALSE;
128         }
129 
130         ptr++;
131     }
132 
133     /* sends the string to find the command */
134     return InterpretCmd(args_count, args_vector);
135 }
136 
137 
138 /*
139  * InterpretMain():
140  * Contents for the main program loop as it reads each line, and then
141  * it sends the string to interpret_line, where it determines what
142  * command to use.
143  */
144 VOID
145 InterpretMain(VOID)
146 {
147     WCHAR input_line[MAX_STRING_SIZE];
148     LPWSTR args_vector[MAX_ARGS_COUNT];
149     INT args_count = 0;
150     BOOL bWhiteSpace = TRUE;
151     BOOL bRun = TRUE;
152     LPWSTR ptr;
153 
154     while (bRun != FALSE)
155     {
156         args_count = 0;
157         memset(args_vector, 0, sizeof(args_vector));
158 
159         /* Shown just before the input where the user places commands */
160         ConResPuts(StdOut, IDS_APP_PROMPT);
161 
162         /* Get input from the user. */
163         fgetws(input_line, MAX_STRING_SIZE, stdin);
164 
165         ptr = input_line;
166         while (*ptr != 0)
167         {
168             if (iswspace(*ptr) || *ptr == L'\n')
169             {
170                 *ptr = 0;
171                 bWhiteSpace = TRUE;
172             }
173             else
174             {
175                 if ((bWhiteSpace != FALSE) && (args_count < MAX_ARGS_COUNT))
176                 {
177                     args_vector[args_count] = ptr;
178                     args_count++;
179                 }
180                 bWhiteSpace = FALSE;
181             }
182             ptr++;
183         }
184 
185         /* Send the string to find the command */
186         bRun = InterpretCmd(args_count, args_vector);
187     }
188 }
189