Code

get_author_initials: various fixes
[tig.git] / io.h
1 /* Copyright (c) 2006-2010 Jonas Fonseca <fonseca@diku.dk>
2  *
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.
7  *
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.
12  */
14 #ifndef TIG_IO_H
15 #define TIG_IO_H
17 /*
18  * Argument array helpers.
19  */
21 bool argv_from_string(const char *argv[SIZEOF_ARG], int *argc, char *cmd);
22 bool argv_from_env(const char **argv, const char *name);
23 void argv_free(const char *argv[]);
24 size_t argv_size(const char **argv);
25 bool argv_append(const char ***argv, const char *arg);
26 bool argv_append_array(const char ***dst_argv, const char *src_argv[]);
27 bool argv_copy(const char ***dst, const char *src[]);
29 /*
30  * Executing external commands.
31  */
33 enum io_type {
34         IO_FD,                  /* File descriptor based IO. */
35         IO_BG,                  /* Execute command in the background. */
36         IO_FG,                  /* Execute command with same std{in,out,err}. */
37         IO_RD,                  /* Read only fork+exec IO. */
38         IO_WR,                  /* Write only fork+exec IO. */
39         IO_AP,                  /* Append fork+exec output to file. */
40 };
42 struct io {
43         int pipe;               /* Pipe end for reading or writing. */
44         pid_t pid;              /* PID of spawned process. */
45         int error;              /* Error status. */
46         char *buf;              /* Read buffer. */
47         size_t bufalloc;        /* Allocated buffer size. */
48         size_t bufsize;         /* Buffer content size. */
49         char *bufpos;           /* Current buffer position. */
50         unsigned int eof:1;     /* Has end of file been reached. */
51 };
53 typedef int (*io_read_fn)(char *, size_t, char *, size_t, void *data);
55 bool io_open(struct io *io, const char *fmt, ...);
56 bool io_kill(struct io *io);
57 bool io_done(struct io *io);
58 bool io_run(struct io *io, enum io_type type, const char *dir, const char *argv[], ...);
59 bool io_run_bg(const char **argv);
60 bool io_run_fg(const char **argv, const char *dir);
61 bool io_run_append(const char **argv, int fd);
62 bool io_eof(struct io *io);
63 int io_error(struct io *io);
64 char * io_strerror(struct io *io);
65 bool io_can_read(struct io *io, bool can_block);
66 ssize_t io_read(struct io *io, void *buf, size_t bufsize);
67 char * io_get(struct io *io, int c, bool can_read);
68 bool io_write(struct io *io, const void *buf, size_t bufsize);
69 bool io_read_buf(struct io *io, char buf[], size_t bufsize);
70 bool io_run_buf(const char **argv, char buf[], size_t bufsize);
71 int io_load(struct io *io, const char *separators,
72             io_read_fn read_property, void *data);
73 int io_run_load(const char **argv, const char *separators,
74                 io_read_fn read_property, void *data);
76 #endif