Skip to content

File wsh_shell_cmd.c

File List > src > wsh_shell_cmd.c

Go to the documentation of this file

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include "wsh_shell_cmd.h"

static WSH_SHELL_RET_STATE_t WshShellCmd_ValidateCmd(const WshShellCmd_t* pcCmd);

static WSH_SHELL_RET_STATE_t WshShellCmd_ValidateOptions(const WshShellCmd_t* pcCmd) {
    WSH_SHELL_RET_STATE_t retState = WSH_SHELL_RET_STATE_SUCCESS;

    if (!pcCmd->Options)
        return retState;

    const WshShellOption_t* pcOptOuter = pcCmd->Options;
    for (; pcOptOuter->Type != WSH_SHELL_OPTION_END; pcOptOuter++) {
        const WshShellOption_t* pcOptInner = pcOptOuter + 1;

        for (; pcOptInner->Type != WSH_SHELL_OPTION_END; pcOptInner++) {
            if (pcOptOuter->ShortName && pcOptInner->ShortName && pcOptOuter->ShortName[0] &&
                pcOptInner->ShortName[0] && strcmp(pcOptOuter->ShortName, pcOptInner->ShortName) == 0) {
                WSH_SHELL_PRINT_ERR("Duplicate short option name detected: %s %s\r\n", pcCmd->Name,
                                    pcOptInner->ShortName);
                WSH_SHELL_ASSERT(0);
                retState = WSH_SHELL_RET_STATE_ERROR;
            }

            if (pcOptOuter->LongName && pcOptInner->LongName && pcOptOuter->LongName[0] && pcOptInner->LongName[0] &&
                strcmp(pcOptOuter->LongName, pcOptInner->LongName) == 0) {
                WSH_SHELL_PRINT_ERR("Duplicate long option name detected: %s %s\r\n", pcCmd->Name,
                                    pcOptInner->LongName);
                WSH_SHELL_ASSERT(0);
                retState = WSH_SHELL_RET_STATE_ERROR;
            }
        }
    }

    return retState;
}

#if WSH_SHELL_SUBCOMMANDS
static WSH_SHELL_RET_STATE_t WshShellCmd_ValidateSubCmds(const WshShellCmd_t* pcCmd) {
    WSH_SHELL_RET_STATE_t retState = WSH_SHELL_RET_STATE_SUCCESS;

    if (pcCmd->SubCmdNum == 0 || !pcCmd->SubCmds)
        return retState;

    for (WshShell_Size_t i = 0; i < pcCmd->SubCmdNum; i++) {
        const WshShellCmd_t* pcSubI = pcCmd->SubCmds[i];
        if (!pcSubI) {
            WSH_SHELL_PRINT_ERR("Null subcommand pointer in %s [%d]\r\n", pcCmd->Name, i);
            WSH_SHELL_ASSERT(0);
            retState = WSH_SHELL_RET_STATE_ERROR;
            continue;
        }

        for (WshShell_Size_t j = i + 1; j < pcCmd->SubCmdNum; j++) {
            const WshShellCmd_t* pcSubJ = pcCmd->SubCmds[j];
            if (pcSubJ && pcSubI->Name && pcSubJ->Name && strcmp(pcSubI->Name, pcSubJ->Name) == 0) {
                WSH_SHELL_PRINT_ERR("Duplicate subcommand name detected: %s %s\r\n", pcCmd->Name, pcSubJ->Name);
                WSH_SHELL_ASSERT(0);
                retState = WSH_SHELL_RET_STATE_ERROR;
            }
        }

        WSH_SHELL_RET_STATE_t subRet = WshShellCmd_ValidateCmd(pcSubI);
        if (subRet != WSH_SHELL_RET_STATE_SUCCESS)
            retState = subRet;
    }

    return retState;
}
#endif /* WSH_SHELL_SUBCOMMANDS */

static WSH_SHELL_RET_STATE_t WshShellCmd_ValidateCmd(const WshShellCmd_t* pcCmd) {
    WSH_SHELL_RET_STATE_t retState = WshShellCmd_ValidateOptions(pcCmd);
#if WSH_SHELL_SUBCOMMANDS
    WSH_SHELL_RET_STATE_t subRet = WshShellCmd_ValidateSubCmds(pcCmd);
    if (subRet != WSH_SHELL_RET_STATE_SUCCESS)
        retState = subRet;
#endif
    return retState;
}

WSH_SHELL_RET_STATE_t WshShellCmd_Attach(WshShellCmd_Table_t* pShellCommands, const WshShellCmd_t* pcCmdTable[],
                                         WshShell_Size_t cmdNum) {
    WSH_SHELL_ASSERT(pShellCommands && pcCmdTable && cmdNum > 0);
    if (!pShellCommands || !pcCmdTable || cmdNum == 0)
        return WSH_SHELL_RET_STATE_ERR_PARAM;

    if (pShellCommands->List != NULL)
        return WSH_SHELL_RET_STATE_ERR_BUSY;  // Already inited

    WSH_SHELL_RET_STATE_t retState = WSH_SHELL_RET_STATE_SUCCESS;

    for (WshShell_Size_t cmd = 0; cmd < cmdNum; cmd++) {
        const WshShellCmd_t* pcCmd = pcCmdTable[cmd];
        if (pcCmd == NULL) {
            WSH_SHELL_ASSERT(0);
            continue;
        }

        WSH_SHELL_RET_STATE_t vRet = WshShellCmd_ValidateCmd(pcCmd);
        if (vRet != WSH_SHELL_RET_STATE_SUCCESS)
            retState = vRet;
    }

    pShellCommands->List = pcCmdTable;
    pShellCommands->Num  = cmdNum;

    return retState;
}

void WshShellCmd_DeAttach(WshShellCmd_Table_t* pShellCommands) {
    WSH_SHELL_ASSERT(pShellCommands);

    if (pShellCommands)
        *pShellCommands = (WshShellCmd_Table_t){0};
}

WshShell_Size_t WshShellCmd_GetCmdNum(WshShellCmd_Table_t* pShellCommands) {
    WSH_SHELL_ASSERT(pShellCommands);

    if (!pShellCommands || !pShellCommands->List)
        return 0;

    return pShellCommands->Num;
}

const WshShellCmd_t* WshShellCmd_GetCmdByIndex(WshShellCmd_Table_t* pShellCommands, WshShell_Size_t idx) {
    WSH_SHELL_ASSERT(pShellCommands);
    if (!pShellCommands || !pShellCommands->List)
        return NULL;

    WSH_SHELL_ASSERT(idx < pShellCommands->Num);

    return idx < pShellCommands->Num ? pShellCommands->List[idx] : NULL;
}

const WshShellCmd_t* WshShellCmd_SearchCmd(WshShellCmd_Table_t* pShellCommands, const WshShell_Char_t* pcCmdName) {
    WSH_SHELL_ASSERT(pcCmdName);
    if (!pcCmdName)
        return NULL;

    for (WshShell_Size_t cmd = 0; cmd < WshShellCmd_GetCmdNum(pShellCommands); cmd++) {
        const WshShellCmd_t* pcCmd = WshShellCmd_GetCmdByIndex(pShellCommands, cmd);
        if (WSH_SHELL_STRNCMP(pcCmd->Name, pcCmdName, WSH_SHELL_CMD_NAME_LEN) == 0)
            return pcCmd;
    }

    return NULL;
}

#if WSH_SHELL_SUBCOMMANDS
WshShell_Size_t WshShellCmd_GetSubCmdNum(const WshShellCmd_t* pcCmd) {
    if (!pcCmd || !pcCmd->SubCmds)
        return 0;

    return pcCmd->SubCmdNum;
}

const WshShellCmd_t* WshShellCmd_GetSubCmdByIndex(const WshShellCmd_t* pcCmd, WshShell_Size_t idx) {
    if (!pcCmd || !pcCmd->SubCmds || idx >= pcCmd->SubCmdNum)
        return NULL;

    return pcCmd->SubCmds[idx];
}

const WshShellCmd_t* WshShellCmd_SearchSubCmd(const WshShellCmd_t* pcCmd, const WshShell_Char_t* pcSubName) {
    if (!pcCmd || !pcSubName || !pcCmd->SubCmds)
        return NULL;

    for (WshShell_Size_t i = 0; i < pcCmd->SubCmdNum; i++) {
        const WshShellCmd_t* pcSub = pcCmd->SubCmds[i];
        if (!pcSub || !pcSub->Name)
            continue;

        if (WSH_SHELL_STRNCMP(pcSub->Name, pcSubName, WSH_SHELL_CMD_NAME_LEN) == 0)
            return pcSub;
    }

    return NULL;
}
#endif /* WSH_SHELL_SUBCOMMANDS */

const WshShellOption_t* WshShellCmd_FindOptByName(const WshShellCmd_t* pcCmd, const WshShell_Char_t* pcName) {
    if (!pcCmd || !pcName || !pcCmd->Options)
        return NULL;

    const WshShell_Size_t strLen = WSH_SHELL_STRLEN(pcName);

    const WshShellOption_t* pcOpt = pcCmd->Options;
    for (; pcOpt->Type != WSH_SHELL_OPTION_END; pcOpt++) {
        if (pcOpt->ShortName && strLen <= WSH_SHELL_OPTION_SHORT_NAME_LEN &&
            WSH_SHELL_STRNCMP(pcOpt->ShortName, pcName, strLen + 1) == 0)
            return pcOpt;
        if (pcOpt->LongName && WSH_SHELL_STRNCMP(pcOpt->LongName, pcName, WSH_SHELL_OPTION_LONG_NAME_LEN) == 0)
            return pcOpt;
    }
    return NULL;
}

static const WshShellOption_t* WshShellCmd_FindOpt(const WshShellCmd_t* pcCmd, const WshShell_Char_t* pcStr,
                                                   WshShell_Size_t strLen) {
    WSH_SHELL_ASSERT(pcCmd && pcStr);
    if (!pcCmd || !pcStr)
        return NULL;

    const WshShellOption_t* pcWaitsInputOpt = NULL;

    const WshShellOption_t* pcOpt = pcCmd->Options;
    for (; pcOpt->Type != WSH_SHELL_OPTION_END; pcOpt++) {
        switch (pcOpt->Type) {
            case WSH_SHELL_OPTION_NO:
                continue;

            case WSH_SHELL_OPTION_WAITS_INPUT:
                pcWaitsInputOpt = pcOpt;
                continue;

            default: {
                WshShell_Bool_t isShort        = (strLen <= WSH_SHELL_OPTION_SHORT_NAME_LEN);
                const WshShell_Char_t* pRefStr = isShort ? pcOpt->ShortName : pcOpt->LongName;
                WshShell_Size_t cmpLen         = isShort ? strLen + 1 : WSH_SHELL_OPTION_LONG_NAME_LEN;

                if (pRefStr && WSH_SHELL_STRNCMP(pRefStr, pcStr, cmpLen) == 0)
                    return pcOpt;
            }
        }
    }

    return pcWaitsInputOpt;
}
WshShellOption_Ctx_t WshShellCmd_ParseOpt(const WshShellCmd_t* pcCmd, WshShell_Size_t argc,
                                          const WshShell_Char_t* pArgv[], WshShell_Size_t rights,
                                          WshShell_Size_t* pTokenPos) {
    WSH_SHELL_ASSERT(pcCmd && pArgv && argc > 0 && pTokenPos);

    WshShellOption_Ctx_t optCtx = {0};
    if (!pcCmd || !pArgv || argc == 0 || !pTokenPos || *pTokenPos >= argc)
        return optCtx;

    /* Always skip command token */
    if (*pTokenPos == 0)
        (*pTokenPos)++;

    WshShell_Size_t startTokenPos = *pTokenPos;

    /* Try to find option in argv */
    while (*pTokenPos < argc) {
        const WshShell_Char_t* pcStr  = pArgv[*pTokenPos];
        WshShell_Size_t strLen        = WSH_SHELL_STRLEN(pcStr);
        const WshShellOption_t* pcOpt = WshShellCmd_FindOpt(pcCmd, pcStr, strLen);

        if (!pcOpt) {
            WSH_SHELL_PRINT_WARN("Unknown option: %s\r\n", pcStr);
            /* Signal parse error to the caller via ParseError flag.
             * Advance past the bad token so the OPTION_NO fallback
             * (tokenPos == startTokenPos check) is not triggered. */
            optCtx.ParseError = true;
            (*pTokenPos)++;
            break;
        }

        /* Verify enough tokens follow for all required arguments */
        if (pcOpt->ArgNum > 0 && *pTokenPos + pcOpt->ArgNum >= argc) {
            WSH_SHELL_PRINT_WARN("Option %s requires %d argument(s)\r\n", pcStr, pcOpt->ArgNum);
            optCtx.ParseError = true;
            (*pTokenPos)++;
            break;
        }

        optCtx.Option   = pcOpt;
        optCtx.TokenPos = *pTokenPos;

        if (pcOpt->Type == WSH_SHELL_OPTION_WAITS_INPUT) {
            *pTokenPos = argc;
        } else {
            *pTokenPos += pcOpt->ArgNum + 1;
        }

        break;
    }

    /* If no option found and no tokens were skipped — use OPTION_NO (empty call) */
    if (!optCtx.Option && *pTokenPos == startTokenPos) {
        const WshShellOption_t* pcOpt = pcCmd->Options;
        for (; pcOpt->Type != WSH_SHELL_OPTION_END; pcOpt++) {
            if (pcOpt->Type == WSH_SHELL_OPTION_NO) {
                optCtx.Option   = pcOpt;
                optCtx.TokenPos = *pTokenPos;
                break;
            }
        }
    }

    /* Access rights check (single point) */
    if (optCtx.Option) {
        if (((optCtx.Option->Access & rights) == 0) && !(rights & WSH_SHELL_OPT_ACCESS_ADMIN)) {

            WshShell_Char_t optRightsRow[5];
            WshShell_Char_t usrRightsRow[5];
            WshShellStr_AccessBitsToStr(optCtx.Option->Access, optRightsRow);
            WshShellStr_AccessBitsToStr(rights, usrRightsRow);

            WSH_SHELL_PRINT_ERR("No access rights for option (%s), user (%s)\r\n", optRightsRow, usrRightsRow);

            optCtx.Option = NULL;
        }
    }

    return optCtx;
}

WSH_SHELL_RET_STATE_t WshShellCmd_GetOptValue(WshShellOption_Ctx_t* pOptCtx, WshShell_Size_t argc,
                                              const WshShell_Char_t* pArgv[], WshShell_Size_t valueSize, void* pValue) {
    WSH_SHELL_ASSERT(pOptCtx && pOptCtx->Option && pArgv && pValue && valueSize);
    if (!pOptCtx || !pOptCtx->Option || !pArgv || !pValue || valueSize == 0)
        return WSH_SHELL_RET_STATE_ERR_PARAM;

    if (argc < 2 || pOptCtx->TokenPos == 0)
        return WSH_SHELL_RET_STATE_ERR_EMPTY;

    WshShell_Size_t valIdx = pOptCtx->TokenPos + 1;
    if (valIdx >= argc)
        return WSH_SHELL_RET_STATE_ERR_EMPTY;

    switch (pOptCtx->Option->Type) {
        case WSH_SHELL_OPTION_STR:
            WSH_SHELL_STRNCPY((WshShell_Char_t*)pValue, pArgv[valIdx], valueSize);
            break;

        case WSH_SHELL_OPTION_INT:
            /* base 0 → auto-detect: 0x prefix → hex, 0 prefix → octal, else decimal */
            *((WshShell_Size_t*)pValue) = WSH_SHELL_STRTOL(pArgv[valIdx], NULL, 0);
            break;

        case WSH_SHELL_OPTION_FLOAT:
            *((float*)pValue) = WSH_SHELL_STRTOF(pArgv[valIdx], NULL);
            break;

        case WSH_SHELL_OPTION_ENUM:
            if (pOptCtx->Option->Enum) {
                const WshShellOptionEnum_t* pEnum = pOptCtx->Option->Enum;
                WshShell_Bool_t found             = false;
                for (WshShell_Size_t i = 0; i < pEnum->Count; i++) {
                    if (WSH_SHELL_STRNCMP(pArgv[valIdx], pEnum->Values[i], WSH_SHELL_ENUM_VALUE_MAX_LEN) == 0) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    WSH_SHELL_PRINT_WARN("Invalid value \"%s\" for %s\r\n", pArgv[valIdx], pOptCtx->Option->LongName);
                    return WSH_SHELL_RET_STATE_ERR_PARAM;
                }
            }
            WSH_SHELL_STRNCPY((WshShell_Char_t*)pValue, pArgv[valIdx], valueSize);
            break;

        case WSH_SHELL_OPTION_NO:
        case WSH_SHELL_OPTION_HELP:
        case WSH_SHELL_OPTION_WO_PARAM:
        case WSH_SHELL_OPTION_MULTI_ARG:
        case WSH_SHELL_OPTION_WAITS_INPUT:
        case WSH_SHELL_OPTION_END:
        case WSH_SHELL_OPTION_ENUM_SIZE:
        default:
            return WSH_SHELL_RET_STATE_ERR_EMPTY;
    }

    return WSH_SHELL_RET_STATE_SUCCESS;
}

void WshShellCmd_PrintOptionsOverview(const WshShellCmd_t* pcCmd) {
    WSH_SHELL_ASSERT(pcCmd);
    if (!pcCmd)
        return;

    WSH_SHELL_PRINT_SYS("%s\r\n", pcCmd->Descr);

#if WSH_SHELL_CMD_PRINT_OPT_OVERVIEW

    const WshShell_Size_t shortNameMaxLen = WSH_SHELL_OPTION_SHORT_NAME_LEN + 5;
    const WshShell_Size_t longNameMaxLen  = WSH_SHELL_OPTION_LONG_NAME_LEN;
    const WshShell_Size_t typeMaxLen      = 10;
    const WshShell_Size_t accessMaxLen    = 6;

    if (pcCmd->Options) {
        WSH_SHELL_PRINT_SYS("\r\nOptions overview:\r\n");

        WshShell_Char_t headTemplate[64];
        WSH_SHELL_SNPRINTF(headTemplate, sizeof(headTemplate),
                           WSH_SHELL_COLOR_SYS "  %%-%ds %%-%ds %%-%ds %%-%ds %%s\r\n" WSH_SHELL_ESC_RESET_STYLE,
                           shortNameMaxLen, longNameMaxLen, typeMaxLen, accessMaxLen);

        WSH_SHELL_PRINT(headTemplate, "Short", "Long", "Type", "Access", "Descr");

        WshShell_Char_t rowTemplate[64];
        WSH_SHELL_SNPRINTF(rowTemplate, sizeof(rowTemplate), "  %%-%ds %%-%ds %%-%ds %%-%ds %%s\r\n", shortNameMaxLen,
                           longNameMaxLen, typeMaxLen, accessMaxLen);

        const WshShellOption_t* pcOpt = pcCmd->Options;
        for (; pcOpt->Type != WSH_SHELL_OPTION_END; pcOpt++) {
            if (pcOpt->Type == WSH_SHELL_OPTION_WAITS_INPUT)
                continue;

            WshShell_Char_t accessRow[8];
            WshShellStr_AccessBitsToStr(pcOpt->Access, accessRow);
            WSH_SHELL_PRINT(rowTemplate, pcOpt->ShortName, pcOpt->LongName, WshShell_OptTypeStr_Get(pcOpt->Type),
                            accessRow, pcOpt->Descr);
        }
    }

#if WSH_SHELL_SUBCOMMANDS
    if (pcCmd->SubCmdNum > 0 && pcCmd->SubCmds != NULL) {
        const WshShell_Size_t subNameMaxLen = WSH_SHELL_CMD_NAME_LEN;

        WshShell_Char_t subHeadTemplate[64];
        WSH_SHELL_SNPRINTF(subHeadTemplate, sizeof(subHeadTemplate),
                           WSH_SHELL_COLOR_SYS "\r\nSubcommands:\r\n  %%-%ds %%s\r\n" WSH_SHELL_ESC_RESET_STYLE,
                           subNameMaxLen);
        WSH_SHELL_PRINT(subHeadTemplate, "Name", "Descr");

        WshShell_Char_t subRowTemplate[32];
        WSH_SHELL_SNPRINTF(subRowTemplate, sizeof(subRowTemplate), "  %%-%ds %%s\r\n", subNameMaxLen);

        for (WshShell_Size_t i = 0; i < pcCmd->SubCmdNum; i++) {
            const WshShellCmd_t* pcSub = pcCmd->SubCmds[i];
            if (!pcSub || !pcSub->Name)
                continue;
            WSH_SHELL_PRINT(subRowTemplate, pcSub->Name, pcSub->Descr ? pcSub->Descr : "");
        }
    }
#endif /* WSH_SHELL_SUBCOMMANDS */

#else /* WSH_SHELL_CMD_PRINT_OPT_OVERVIEW */

    return;

#endif /* WSH_SHELL_CMD_PRINT_OPT_OVERVIEW */
}