aboutsummaryrefslogtreecommitdiff
path: root/src/os/shell.c
blob: cbedf72a9c43b8027e83a2de31ffc2ebc4208b28 (plain) (blame)
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
#include <stdbool.h>

#include "os/shell.h"
#include "types.h"
#include "vim.h"
#include "ascii.h"
#include "charset.h"


void shell_skip_word(char_u **cmd)
{
  char_u *p = *cmd;
  bool inquote = false;

  // Move `p` to the end of shell word by advancing the pointer it while it's
  // inside a quote or it's a non-whitespace character
  while (*p && (inquote || (*p != ' ' && *p != TAB))) {
    if (*p == '"')
      // Found a quote character, switch the `inquote` flag
      inquote = !inquote;
    ++p;
  }

  *cmd = p;
}

int shell_count_argc(char_u **ptr)
{
  int rv = 0;
  char_u *p = *ptr;

  while (true) {
    rv++;
    shell_skip_word(&p);
    if (*p == NUL)
      break;
    // Move to the next word
    p = skipwhite(p);
  }

  // Account for multiple args in p_shcf('shellcmdflag' option)
  p = p_shcf;
  while (true) {
    // Same as above, but doesn't need to take quotes into consideration
    p = skiptowhite(p);
    if (*p == NUL)
      break;
    rv++;
    p = skipwhite(p);
  }

  *ptr = p;

  return rv;
}

char ** shell_build_argv(char_u **ptr, int argc);