Subversion Repositories SvarDOS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/* This file provides functions for parsing commands and their arguments
2
 
3
   Warning: parsecmd() will modify the cmdline string, so it won't be
4
   readable anymore in any other way other than via ptrtable[].
5
   This function returns the number of arguments that have been parsed,
6
   or -1 on parsing error.
7
 
8
   Copyright (C) 2012-2016 Mateusz Viste */
9
 
10
#include "version.h"
11
 
12
int parsecmd(char *cmdline, char **ptrtable, int maxargs) {
13
  int x = 0, argc = 0, state = 0;
14
  for (;;) {
15
    switch (cmdline[x]) { /* detect delimiter and non-delimiter chars */
16
      case 0x0: /* detect end of string */
17
        return(argc); /* return number of arguments */
18
      case ' ': /* space */
19
      case 0x9: /* tab */
20
      case 0xA: /* LF */
21
      case 0xD: /* CR */
22
        if (state != 0) { /* if awaiting for argument end */
23
          cmdline[x] = 0; /* terminate the substring */
24
          state = 0; /* switch to 'waiting for argument end' state */
25
        }
26
        break;
27
      default: /* anything that is not a delimiter */
28
        if (state == 0) { /* if awaiting for argument start */
29
          if (argc == maxargs) return(-1); /* look out for arg overflow */
30
          ptrtable[argc] = &cmdline[x]; /* save the address of the substring */
31
          argc += 1; /* increment the arguments count */
32
          state = 1; /* switch to 'waiting for argument' state */
33
        }
34
    }
35
    x += 1; /* move to the next character of cmdline */
36
  }
37
}