Browse Source

Improve documentation in shell.c

The shell main loop function and text input function were dreadfully
devoid of comments.  This simply could not continue, so I added a
sprinkling of comments to each, as well as to important sections of the
file.  Further, I changed the special character values to look like
characters rather than numbers, which should aid in understanding.
Clara Hobbs 6 years ago
parent
commit
42873ebe9b
1 changed files with 32 additions and 6 deletions
  1. 32
    6
      src/shell.c

+ 32
- 6
src/shell.c View File

50
     .status = PDB_CONFIG_STATUS_VALID
50
     .status = PDB_CONFIG_STATUS_VALID
51
 };
51
 };
52
 
52
 
53
+/*
54
+ * Command functions
55
+ */
56
+
53
 static void cmd_license(BaseSequentialStream *chp, int argc, char *argv[])
57
 static void cmd_license(BaseSequentialStream *chp, int argc, char *argv[])
54
 {
58
 {
55
     (void) argv;
59
     (void) argv;
237
     chEvtSignal(pdb_led_thread, PDB_EVT_LED_IDENTIFY);
241
     chEvtSignal(pdb_led_thread, PDB_EVT_LED_IDENTIFY);
238
 }
242
 }
239
 
243
 
244
+/*
245
+ * List of shell commands
246
+ */
240
 static const struct pdb_shell_cmd commands[] = {
247
 static const struct pdb_shell_cmd commands[] = {
241
     {"license", cmd_license, "Show copyright and license information"},
248
     {"license", cmd_license, "Show copyright and license information"},
242
     {"erase", cmd_erase, "Erase all stored configuration"},
249
     {"erase", cmd_erase, "Erase all stored configuration"},
254
     {NULL, NULL, NULL}
261
     {NULL, NULL, NULL}
255
 };
262
 };
256
 
263
 
264
+/*
265
+ * The shell's configuration
266
+ */
257
 const struct pdb_shell_cfg shell_cfg = {
267
 const struct pdb_shell_cfg shell_cfg = {
258
     (BaseSequentialStream *)&SDU1,
268
     (BaseSequentialStream *)&SDU1,
259
     commands
269
     commands
302
 }
312
 }
303
 
313
 
304
 /*
314
 /*
305
- * PD Buddy configuration shell
306
- *
307
- * p: The configuration for the shell itself
315
+ * PD Buddy Sink configuration shell
308
  */
316
  */
309
 void pdb_shell(void)
317
 void pdb_shell(void)
310
 {
318
 {
315
     char *args[PDB_SHELL_MAX_ARGUMENTS + 1];
323
     char *args[PDB_SHELL_MAX_ARGUMENTS + 1];
316
 
324
 
317
     while (true) {
325
     while (true) {
326
+        /* Print the prompt */
318
         chprintf(chp, "PDBS) ");
327
         chprintf(chp, "PDBS) ");
328
+
329
+        /* Read a line of input */
319
         if (shellGetLine(chp, line, sizeof(line))) {
330
         if (shellGetLine(chp, line, sizeof(line))) {
331
+            /* If a command was not entered, prompt again */
320
             chprintf(chp, "\r\n");
332
             chprintf(chp, "\r\n");
321
             continue;
333
             continue;
322
         }
334
         }
335
+
336
+        /* Tokenize the line */
323
         lp = _strtok(line, " \t", &tokp);
337
         lp = _strtok(line, " \t", &tokp);
324
         cmd = lp;
338
         cmd = lp;
325
         n = 0;
339
         n = 0;
326
         while ((lp = _strtok(NULL, " \t", &tokp)) != NULL) {
340
         while ((lp = _strtok(NULL, " \t", &tokp)) != NULL) {
341
+            /* If we have too many tokens, abort */
327
             if (n >= PDB_SHELL_MAX_ARGUMENTS) {
342
             if (n >= PDB_SHELL_MAX_ARGUMENTS) {
328
                 chprintf(chp, "too many arguments\r\n");
343
                 chprintf(chp, "too many arguments\r\n");
329
                 cmd = NULL;
344
                 cmd = NULL;
332
             args[n++] = lp;
347
             args[n++] = lp;
333
         }
348
         }
334
         args[n] = NULL;
349
         args[n] = NULL;
350
+
351
+        /* If there's a command to run, run it */
335
         if (cmd != NULL) {
352
         if (cmd != NULL) {
353
+            /* Handle "help" in a special way */
336
             if (strcmp(cmd, "help") == 0) {
354
             if (strcmp(cmd, "help") == 0) {
337
                 if (n > 0) {
355
                 if (n > 0) {
338
                     chprintf(chp, "Usage: help\r\n");
356
                     chprintf(chp, "Usage: help\r\n");
344
                 if (scp != NULL)
362
                 if (scp != NULL)
345
                     list_commands(chp, scp);
363
                     list_commands(chp, scp);
346
             }
364
             }
365
+            /* Run a command, giving a generic error message if there is no
366
+             * such command */
347
             else if ((scp == NULL) || cmdexec(scp, chp, cmd, n, args)) {
367
             else if ((scp == NULL) || cmdexec(scp, chp, cmd, n, args)) {
348
                 chprintf(chp, "%s", cmd);
368
                 chprintf(chp, "%s", cmd);
349
                 chprintf(chp, " ?\r\n");
369
                 chprintf(chp, " ?\r\n");
372
     while (true) {
392
     while (true) {
373
         char c;
393
         char c;
374
 
394
 
395
+        /* Read a character */
375
         if (chSequentialStreamRead(chp, (uint8_t *)&c, 1) == 0)
396
         if (chSequentialStreamRead(chp, (uint8_t *)&c, 1) == 0)
376
             return true;
397
             return true;
377
-        if (c == 4) {
398
+        /* Abort if ^D is received */
399
+        if (c == '\x04') {
378
             chprintf(chp, "^D");
400
             chprintf(chp, "^D");
379
             return true;
401
             return true;
380
         }
402
         }
381
-        if ((c == 8) || (c == 127)) {
403
+        /* Delete a character if ASCII backspace or delete is received */
404
+        if ((c == '\b') || (c == '\x7F')) {
382
             if (p != line) {
405
             if (p != line) {
383
                 chSequentialStreamPut(chp, 0x08);
406
                 chSequentialStreamPut(chp, 0x08);
384
                 chSequentialStreamPut(chp, 0x20);
407
                 chSequentialStreamPut(chp, 0x20);
387
             }
410
             }
388
             continue;
411
             continue;
389
         }
412
         }
413
+        /* Finish reading input if Enter is pressed */
390
         if (c == '\r') {
414
         if (c == '\r') {
391
             chprintf(chp, "\r\n");
415
             chprintf(chp, "\r\n");
392
             *p = 0;
416
             *p = 0;
393
             return false;
417
             return false;
394
         }
418
         }
395
-        if (c < 0x20)
419
+        /* Ignore other non-printing characters */
420
+        if (c < ' ')
396
             continue;
421
             continue;
422
+        /* If there's room in the line buffer, append the new character */
397
         if (p < line + size - 1) {
423
         if (p < line + size - 1) {
398
             chSequentialStreamPut(chp, c);
424
             chSequentialStreamPut(chp, c);
399
             *p++ = (char)c;
425
             *p++ = (char)c;

Loading…
Cancel
Save