-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.h
93 lines (80 loc) · 3.3 KB
/
io.h
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Copyright (c) 2006-2012 Jonas Fonseca <fonseca@diku.dk>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef TIG_IO_H
#define TIG_IO_H
#include "tig.h"
/*
* Argument array helpers.
*/
bool argv_to_string(const char *argv[SIZEOF_ARG], char *buf, size_t buflen, const char *sep);
bool argv_from_string_no_quotes(const char *argv[SIZEOF_ARG], int *argc, char *cmd);
bool argv_from_string(const char *argv[SIZEOF_ARG], int *argc, char *cmd);
bool argv_from_env(const char **argv, const char *name);
void argv_free(const char *argv[]);
size_t argv_size(const char **argv);
bool argv_append(const char ***argv, const char *arg);
bool argv_append_array(const char ***dst_argv, const char *src_argv[]);
bool argv_copy(const char ***dst, const char *src[]);
bool argv_remove_quotes(const char *argv[]);
/*
* Encoding conversion.
*/
struct encoding;
struct encoding *encoding_open(const char *fromcode);
char *encoding_convert(struct encoding *encoding, char *line);
/*
* Executing external commands.
*/
enum io_type {
IO_FD, /* File descriptor based IO. */
IO_BG, /* Execute command in the background. */
IO_FG, /* Execute command with same std{in,out,err}. */
IO_RD, /* Read only fork+exec IO. */
IO_RD_STDIN, /* Read only fork+exec IO with stdin. */
IO_WR, /* Write only fork+exec IO. */
IO_AP, /* Append fork+exec output to file. */
};
struct io {
int pipe; /* Pipe end for reading or writing. */
pid_t pid; /* PID of spawned process. */
int error; /* Error status. */
char *buf; /* Read buffer. */
size_t bufalloc; /* Allocated buffer size. */
size_t bufsize; /* Buffer content size. */
char *bufpos; /* Current buffer position. */
unsigned int eof:1; /* Has end of file been reached. */
int status:8; /* Status exit code. */
};
typedef int (*io_read_fn)(char *, size_t, char *, size_t, void *data);
bool io_open(struct io *io, const char *fmt, ...) PRINTF_LIKE(2, 3);
bool io_kill(struct io *io);
bool io_done(struct io *io);
bool io_run(struct io *io, enum io_type type, const char *dir, const char *argv[], ...);
bool io_run_bg(const char **argv);
bool io_run_fg(const char **argv, const char *dir);
bool io_run_append(const char **argv, int fd);
bool io_eof(struct io *io);
int io_error(struct io *io);
char * io_strerror(struct io *io);
bool io_can_read(struct io *io, bool can_block);
ssize_t io_read(struct io *io, void *buf, size_t bufsize);
char * io_get(struct io *io, int c, bool can_read);
bool io_write(struct io *io, const void *buf, size_t bufsize);
bool io_printf(struct io *io, const char *fmt, ...) PRINTF_LIKE(2, 3);
bool io_read_buf(struct io *io, char buf[], size_t bufsize);
bool io_run_buf(const char **argv, char buf[], size_t bufsize);
int io_load(struct io *io, const char *separators,
io_read_fn read_property, void *data);
int io_run_load(const char **argv, const char *separators,
io_read_fn read_property, void *data);
#endif