Make WvStreams compile with gcc 4.4.
[wvstreams.git] / include / wvsystem.h
blob61e358eb258c5378ce2a19f7199d4fecbee233f2
1 /* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 */
5 #ifndef __WVSYSTEM_H
6 #define __WVSYSTEM_H
8 #include "wvsubproc.h"
10 /**
11 * WvSystem is a mostly-replacement for the libc system() function call,
12 * which people usually use because of its notational convenience, not
13 * because it calls the Unix shell. In fact, some people don't even realize
14 * it calls the shell, leading to security holes when people forget to
15 * quote user-provided parameters correctly.
17 * WvSystem() uses WvSubProc but makes sure it can be called in a single
18 * line of C++ code with a minimum of fluff. For example:
20 * WvSystem("rm", "-rf", filename, NULL);
21 * is like
22 * system(WvString("rm -rf %s", filename));
23 * except that you don't have weird security bugs if "filename" contains
24 * special characters like newline, space, quotation mark, etc.
26 * See WvSubProc and WvSubProcQueue for less concise, but more flexible ways
27 * to run subprograms.
29 class WvSystem : private WvSubProc
31 public:
32 /**
33 * Construct a WvSystem from a simple list of strings.
35 * For example:
36 * WvSystem("rm", "-rf", dirname);
38 * Note: this is unlike WvSubProc::prepare(cmd, ...) because you
39 * don't need to provide argv[0] yourself. "cmd" is automatically
40 * inserted as argv[0]. It also lets you pass WvString objects in
41 * without manually calling cstr(), because it doesn't use varargs.
42 * Unfortunately, that means it's limited to 20 arguments.
44 WvSystem(const char cmd[],
45 const char *a0 = NULL,
46 const char *a1 = NULL,
47 const char *a2 = NULL,
48 const char *a3 = NULL,
49 const char *a4 = NULL,
50 const char *a5 = NULL,
51 const char *a6 = NULL,
52 const char *a7 = NULL,
53 const char *a8 = NULL,
54 const char *a9 = NULL,
55 const char *a10 = NULL,
56 const char *a11 = NULL,
57 const char *a12 = NULL,
58 const char *a13 = NULL,
59 const char *a14 = NULL,
60 const char *a15 = NULL,
61 const char *a16 = NULL,
62 const char *a17 = NULL,
63 const char *a18 = NULL,
64 const char *a19 = NULL
67 // this function is inline so it can be a little bit less wasteful...
68 const char * const argv[] = {
69 cmd,
70 a0, a1, a2, a3, a4, a5, a6, a7, a8, a9,
71 a10, a11, a12, a13, a14, a15, a16, a17, a18, a19,
72 NULL
74 init(argv);
77 /**
78 * Construct a WvSystem from an argv array. This is exactly the same
79 * as WvSubProc's argv[] constructor, but the command name is always
80 * taken from argv[0] rather than provided separately.
82 * For example:
83 * const char *argv[] = { "rm", "-rf", dirname, NULL };
84 * WvSystem(argv);
86 WvSystem(const char * const *argv)
87 { init(argv); }
89 /**
90 * Destroy the WvSystem object. If you haven't yet called go(), the
91 * command is run before destruction.
93 virtual ~WvSystem();
95 /**
96 * Explicitly start the command running and wait for it to finish.
97 * This will happen automatically at object destruction time, but if you
98 * want to check the return code, you'll need to call go().
100 int go();
102 /** Redirect stdin from the given input file. */
103 WvSystem &infile(WvStringParm filename);
105 /** Redirect stdout to the given output file, which is overwritten. */
106 WvSystem &outfile(WvStringParm filename);
108 /** Redirect stderr to the given output file, which is overwritten. */
109 WvSystem &errfile(WvStringParm filename);
111 private:
112 bool started;
113 WvString fdfiles[3]; // stdin, stdout, stderr
115 void init(const char * const *argv);
116 virtual int fork(int *waitfd);
120 #endif // __WVSYSTEM_H