robin/laufey

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

Justin M. Keyesrefactor(os): libuv_proc_stdio #40506d303617132

master
4.5 KiB176 linesraw
1#pragma once
2
3#include <assert.h>
4#include <stdarg.h>
5#include <stdbool.h>
6#include <uv.h>
7
8#include "nvim/eval/typval_defs.h"
9#include "nvim/types_defs.h"
10
11enum { EVENT_HANDLER_MAX_ARGC = 10, };
12
13typedef void (*argv_callback)(void **argv);
14typedef struct {
15  argv_callback handler;
16  void *argv[EVENT_HANDLER_MAX_ARGC];
17} Event;
18
19#define event_create(cb, ...) ((Event){ .handler = cb, .argv = { __VA_ARGS__ } })
20
21typedef struct multiqueue MultiQueue;
22typedef void (*PutCallback)(MultiQueue *multiq, void *data);
23
24typedef struct signal_watcher SignalWatcher;
25typedef void (*signal_cb)(SignalWatcher *watcher, int signum, void *data);
26typedef void (*signal_close_cb)(SignalWatcher *watcher, void *data);
27
28struct signal_watcher {
29  uv_signal_t uv;
30  void *data;
31  signal_cb cb;
32  signal_close_cb close_cb;
33  MultiQueue *events;
34};
35
36typedef struct time_watcher TimeWatcher;
37typedef void (*time_cb)(TimeWatcher *watcher, void *data);
38
39struct time_watcher {
40  uv_timer_t uv;
41  void *data;
42  time_cb cb, close_cb;
43  MultiQueue *events;
44  bool blockable;
45};
46
47typedef struct wbuffer WBuffer;
48typedef void (*wbuffer_data_finalizer)(void *data);
49
50struct wbuffer {
51  size_t size, refcount;
52  char *data;
53  wbuffer_data_finalizer cb;
54};
55
56typedef struct stream Stream;
57typedef struct rstream RStream;
58/// Type of function called when the RStream buffer is filled with data
59///
60/// @param stream The Stream instance
61/// @param read_data data that was read
62/// @param count Number of bytes that was read.
63/// @param data User-defined data
64/// @param eof If the stream reached EOF.
65/// @return number of bytes which were consumed
66typedef size_t (*stream_read_cb)(RStream *stream, const char *read_data, size_t count, void *data,
67                                 bool eof);
68
69/// Type of function called when the Stream has information about a write
70/// request.
71///
72/// @param stream The Stream instance
73/// @param data User-defined data
74/// @param status 0 on success, anything else indicates failure
75typedef void (*stream_write_cb)(Stream *stream, void *data, int status);
76
77typedef void (*stream_close_cb)(Stream *stream, void *data);
78
79struct stream {
80  bool closed;
81  union {
82    uv_pipe_t pipe;
83    uv_tcp_t tcp;
84    uv_idle_t idle;
85#ifdef MSWIN
86    uv_tty_t tty;
87#endif
88  } uv;
89  uv_stream_t *uvstream;  ///< NULL when the stream is a file
90  uv_file fd;    ///< When the stream is a file, this is its file descriptor
91  int64_t fpos;  ///< When the stream is a file, this is the position in file
92  void *cb_data;
93  stream_close_cb before_close_cb, close_cb, internal_close_cb;
94  void *close_cb_data, *internal_data;
95  size_t pending_reqs;
96  MultiQueue *events;
97
98  // only used for writing:
99  stream_write_cb write_cb;
100  size_t curmem;
101  size_t maxmem;
102};
103
104struct rstream {
105  Stream s;
106  bool did_eof;
107  bool want_read;
108  bool pending_read;
109  bool paused_full;
110  char *buffer;  // ARENA_BLOCK_SIZE
111  char *read_pos;
112  char *write_pos;
113  uv_buf_t uvbuf;
114  stream_read_cb read_cb;
115  size_t num_bytes;
116};
117
118#define ADDRESS_MAX_SIZE 256
119
120typedef struct socket_watcher SocketWatcher;
121typedef void (*socket_cb)(SocketWatcher *watcher, int result, void *data);
122typedef void (*socket_close_cb)(SocketWatcher *watcher, void *data);
123
124struct socket_watcher {
125  // Pipe/socket path, or TCP address string
126  char addr[ADDRESS_MAX_SIZE];
127  // TCP server or unix socket (named pipe on Windows)
128  union {
129    struct {
130      uv_tcp_t handle;
131      struct addrinfo *addrinfo;
132    } tcp;
133    struct {
134      uv_pipe_t handle;
135    } pipe;
136  } uv;
137  uv_stream_t *stream;
138  void *data;
139  socket_cb cb;
140  socket_close_cb close_cb;
141  MultiQueue *events;
142};
143
144typedef enum {
145  kProcTypeUv,
146  kProcTypePty,
147} ProcType;
148
149/// OS process
150typedef struct proc Proc;
151typedef void (*proc_exit_cb)(Proc *proc, int status, void *data);
152typedef void (*proc_state_cb)(Proc *proc, bool suspended, void *data);
153typedef void (*internal_proc_cb)(Proc *proc);
154
155struct proc {
156  ProcType type;
157  Loop *loop;
158  void *data;
159  int pid, status, refcount;
160  uint8_t exit_signal;  // Signal used when killing (on Windows).
161  uint64_t stopped_time;  // proc_stop() timestamp
162  const char *cwd;
163  char **argv;
164  const char *exepath;
165  dict_T *env;
166  Stream in;
167  RStream out, err;
168  /// Exit handler. If set, user must call proc_free().
169  proc_exit_cb cb;
170  proc_state_cb state_cb;
171  internal_proc_cb internal_exit_cb, internal_close_cb;
172  bool closed, detach, overlapped, fwd_err;
173  bool stdio_noinherit;  ///< MSWIN: don't share parent console with child (prevent leaking to TUI).
174                         ///< No equivalent on Unix, where inherited stdio is a pipe, not terminal.
175  MultiQueue *events;
176};