Merge pull request #457 from vivien/text-variable
[tig.git] / include / tig / io.h
blob091518ce9e4d7ea009f3a5b75669904848c40d07
1 /* Copyright (c) 2006-2015 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #ifndef TIG_IO_H
15 #define TIG_IO_H
17 #include "tig/tig.h"
19 struct buffer {
20 char *data;
21 size_t size;
25 * Encoding conversion.
28 #define ENCODING_UTF8 "UTF-8"
30 struct encoding;
32 struct encoding *encoding_open(const char *fromcode);
33 bool encoding_convert(struct encoding *encoding, struct buffer *buf);
34 const char *encoding_iconv(iconv_t iconv_out, const char *string, size_t length);
35 struct encoding *get_path_encoding(const char *path, struct encoding *default_encoding);
37 extern char encoding_arg[];
38 extern struct encoding *default_encoding;
41 * Executing external commands.
44 enum io_flags {
45 IO_RD_FORWARD_STDIN = 1 << 0, /* Forward stdin from parent process to child. */
46 IO_RD_WITH_STDERR = 1 << 1, /* Redirect stderr to stdin. */
49 enum io_type {
50 IO_FD, /* File descriptor based IO. */
51 IO_BG, /* Execute command in the background. */
52 IO_FG, /* Execute command with same std{in,out,err}. */
53 IO_RD, /* Read only fork+exec IO. */
54 IO_WR, /* Write only fork+exec IO. */
55 IO_AP, /* Append fork+exec output to file. */
58 struct io {
59 int pipe; /* Pipe end for reading or writing. */
60 pid_t pid; /* PID of spawned process. */
61 int error; /* Error status. */
62 char *buf; /* Read buffer. */
63 size_t bufalloc; /* Allocated buffer size. */
64 size_t bufsize; /* Buffer content size. */
65 char *bufpos; /* Current buffer position. */
66 unsigned int eof:1; /* Has end of file been reached. */
67 unsigned int span:1; /* Support commands spanning multiple lines. */
68 int status:8; /* Status exit code. */
71 typedef enum status_code (*io_read_fn)(char *, size_t, char *, size_t, void *data);
73 bool io_open(struct io *io, const char *fmt, ...) PRINTF_LIKE(2, 3);
74 bool io_from_string(struct io *io, const char *str);
75 bool io_kill(struct io *io);
76 bool io_done(struct io *io);
77 bool io_exec(struct io *io, enum io_type type, const char *dir, char * const env[], const char *argv[], int custom);
78 bool io_run(struct io *io, enum io_type type, const char *dir, char * const env[], const char *argv[]);
79 bool io_run_bg(const char **argv, const char *dir);
80 bool io_run_fg(const char **argv, const char *dir);
81 bool io_run_append(const char **argv, int fd);
82 bool io_eof(struct io *io);
83 int io_error(struct io *io);
84 char * io_strerror(struct io *io);
85 bool io_can_read(struct io *io, bool can_block);
86 ssize_t io_read(struct io *io, void *buf, size_t bufsize);
87 bool io_get(struct io *io, struct buffer *buf, int c, bool can_read);
88 bool io_write(struct io *io, const void *buf, size_t bufsize);
89 bool io_printf(struct io *io, const char *fmt, ...) PRINTF_LIKE(2, 3);
90 bool io_read_buf(struct io *io, char buf[], size_t bufsize);
91 bool io_run_buf(const char **argv, char buf[], size_t bufsize);
92 enum status_code io_load(struct io *io, const char *separators,
93 io_read_fn read_property, void *data);
94 enum status_code io_load_span(struct io *io, const char *separators,
95 size_t *lineno, io_read_fn read_property, void *data);
96 enum status_code io_run_load(const char **argv, const char *separators,
97 io_read_fn read_property, void *data);
98 char *io_memchr(struct buffer *buf, char *data, int c);
100 const char *get_temp_dir(void);
102 bool PRINTF_LIKE(2, 3) io_fprintf(FILE *file, const char *fmt, ...);
103 bool io_trace(const char *fmt, ...);
105 #endif
106 /* vim: set ts=8 sw=8 noexpandtab: */