robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

bfredlMerge pull request #35206 from bfredl/nopreproc_cleanup38986188ba

master
3.8 KiB156 linesraw
1#include <stdbool.h>                // for true
2
3#include "nvim/channel.h"
4#include "nvim/errors.h"
5#include "nvim/eval.h"
6#include "nvim/eval/deprecated.h"
7#include "nvim/eval/funcs.h"
8#include "nvim/eval/typval.h"
9#include "nvim/eval/typval_defs.h"
10#include "nvim/ex_cmds.h"
11#include "nvim/gettext_defs.h"      // for _
12#include "nvim/globals.h"
13#include "nvim/macros_defs.h"       // for S_LEN
14#include "nvim/message.h"           // for semsg
15#include "nvim/types_defs.h"
16
17#include "eval/deprecated.c.generated.h"  // IWYU pragma: keep
18
19/// "rpcstart()" function (DEPRECATED)
20void f_rpcstart(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
21{
22  rettv->v_type = VAR_NUMBER;
23  rettv->vval.v_number = 0;
24
25  if (check_secure()) {
26    return;
27  }
28
29  if (argvars[0].v_type != VAR_STRING
30      || (argvars[1].v_type != VAR_LIST && argvars[1].v_type != VAR_UNKNOWN)) {
31    // Wrong argument types
32    emsg(_(e_invarg));
33    return;
34  }
35
36  list_T *args = NULL;
37  int argsl = 0;
38  if (argvars[1].v_type == VAR_LIST) {
39    args = argvars[1].vval.v_list;
40    argsl = tv_list_len(args);
41    // Assert that all list items are strings
42    int i = 0;
43    TV_LIST_ITER_CONST(args, arg, {
44      if (TV_LIST_ITEM_TV(arg)->v_type != VAR_STRING) {
45        semsg(_("E5010: List item %d of the second argument is not a string"),
46              i);
47        return;
48      }
49      i++;
50    });
51  }
52
53  if (argvars[0].vval.v_string == NULL || argvars[0].vval.v_string[0] == NUL) {
54    emsg(_(e_api_spawn_failed));
55    return;
56  }
57
58  // Allocate extra memory for the argument vector and the NULL pointer
59  int argvl = argsl + 2;
60  char **argv = xmalloc(sizeof(char *) * (size_t)argvl);
61
62  // Copy program name
63  argv[0] = xstrdup(argvars[0].vval.v_string);
64
65  int i = 1;
66  // Copy arguments to the vector
67  if (argsl > 0) {
68    TV_LIST_ITER_CONST(args, arg, {
69      argv[i++] = xstrdup(tv_get_string(TV_LIST_ITEM_TV(arg)));
70    });
71  }
72
73  // The last item of argv must be NULL
74  argv[i] = NULL;
75
76  Channel *chan = channel_job_start(argv, NULL, CALLBACK_READER_INIT,
77                                    CALLBACK_READER_INIT, CALLBACK_NONE,
78                                    false, true, false, false,
79                                    kChannelStdinPipe, NULL, 0, 0, NULL,
80                                    &rettv->vval.v_number);
81  if (chan) {
82    channel_create_event(chan, NULL);
83  }
84}
85
86/// "rpcstop()" function
87void f_rpcstop(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
88{
89  rettv->v_type = VAR_NUMBER;
90  rettv->vval.v_number = 0;
91
92  if (check_secure()) {
93    return;
94  }
95
96  if (argvars[0].v_type != VAR_NUMBER) {
97    // Wrong argument types
98    emsg(_(e_invarg));
99    return;
100  }
101
102  // if called with a job, stop it, else closes the channel
103  uint64_t id = (uint64_t)argvars[0].vval.v_number;
104  if (find_job(id, false)) {
105    f_jobstop(argvars, rettv, fptr);
106  } else {
107    const char *error;
108    rettv->vval.v_number =
109      channel_close((uint64_t)argvars[0].vval.v_number, kChannelPartRpc, &error);
110    if (!rettv->vval.v_number) {
111      emsg(error);
112    }
113  }
114}
115
116/// "last_buffer_nr()" function.
117void f_last_buffer_nr(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
118{
119  int n = 0;
120
121  FOR_ALL_BUFFERS(buf) {
122    if (n < buf->b_fnum) {
123      n = buf->b_fnum;
124    }
125  }
126
127  rettv->vval.v_number = n;
128}
129
130/// "termopen(cmd[, cwd])" function
131void f_termopen(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
132{
133  if (check_secure()) {
134    return;
135  }
136
137  bool must_free = false;
138
139  if (argvars[1].v_type == VAR_UNKNOWN) {
140    must_free = true;
141    argvars[1].v_type = VAR_DICT;
142    argvars[1].vval.v_dict = tv_dict_alloc();
143  }
144
145  if (argvars[1].v_type != VAR_DICT) {
146    // Wrong argument types
147    semsg(_(e_invarg2), "expected dictionary");
148    return;
149  }
150
151  tv_dict_add_bool(argvars[1].vval.v_dict, S_LEN("term"), true);
152  f_jobstart(argvars, rettv, fptr);
153  if (must_free) {
154    tv_dict_free(argvars[1].vval.v_dict);
155  }
156}