]> Sergey Matveev's repositories - bfs.git/blob - src/exec.h
Skip mtab
[bfs.git] / src / exec.h
1 // Copyright © Tavian Barnes <tavianator@tavianator.com>
2 // SPDX-License-Identifier: 0BSD
3
4 /**
5  * Implementation of -exec/-execdir/-ok/-okdir.
6  */
7
8 #ifndef BFS_EXEC_H
9 #define BFS_EXEC_H
10
11 #include <stddef.h>
12
13 struct BFTW;
14 struct bfs_ctx;
15
16 /**
17  * Flags for the -exec actions.
18  */
19 enum bfs_exec_flags {
20         /** Prompt the user before executing (-ok, -okdir). */
21         BFS_EXEC_CONFIRM = 1 << 0,
22         /** Run the command in the file's parent directory (-execdir, -okdir). */
23         BFS_EXEC_CHDIR   = 1 << 1,
24         /** Pass multiple files at once to the command (-exec ... {} +). */
25         BFS_EXEC_MULTI   = 1 << 2,
26 };
27
28 /**
29  * Buffer for a command line to be executed.
30  */
31 struct bfs_exec {
32         /** Flags for this exec buffer. */
33         enum bfs_exec_flags flags;
34
35         /** The bfs context. */
36         const struct bfs_ctx *ctx;
37         /** Command line template. */
38         char **tmpl_argv;
39         /** Command line template size. */
40         size_t tmpl_argc;
41
42         /** The built command line. */
43         char **argv;
44         /** Number of command line arguments. */
45         size_t argc;
46         /** Capacity of argv. */
47         size_t argv_cap;
48
49         /** Current size of all arguments. */
50         size_t arg_size;
51         /** Maximum arg_size before E2BIG. */
52         size_t arg_max;
53         /** Lower bound for arg_max. */
54         size_t arg_min;
55
56         /** A file descriptor for the working directory, for BFS_EXEC_CHDIR. */
57         int wd_fd;
58         /** The path to the working directory, for BFS_EXEC_CHDIR. */
59         char *wd_path;
60         /** Length of the working directory path. */
61         size_t wd_len;
62
63         /** The ultimate return value for bfs_exec_finish(). */
64         int ret;
65 };
66
67 /**
68  * Parse an exec action.
69  *
70  * @param argv
71  *         The (bfs) command line argument to parse.
72  * @param flags
73  *         Any flags for this exec action.
74  * @param ctx
75  *         The bfs context.
76  * @return
77  *         The parsed exec action, or NULL on failure.
78  */
79 struct bfs_exec *bfs_exec_parse(const struct bfs_ctx *ctx, char **argv, enum bfs_exec_flags flags);
80
81 /**
82  * Execute the command for a file.
83  *
84  * @param execbuf
85  *         The parsed exec action.
86  * @param ftwbuf
87  *         The bftw() data for the current file.
88  * @return 0 if the command succeeded, -1 if it failed.  If the command could
89  *         be executed, -1 is returned, and errno will be non-zero.  For
90  *         BFS_EXEC_MULTI, errors will not be reported until bfs_exec_finish().
91  */
92 int bfs_exec(struct bfs_exec *execbuf, const struct BFTW *ftwbuf);
93
94 /**
95  * Finish executing any commands.
96  *
97  * @param execbuf
98  *         The parsed exec action.
99  * @return 0 on success, -1 if any errors were encountered.
100  */
101 int bfs_exec_finish(struct bfs_exec *execbuf);
102
103 /**
104  * Free a parsed exec action.
105  */
106 void bfs_exec_free(struct bfs_exec *execbuf);
107
108 #endif // BFS_EXEC_H