summaryrefslogtreecommitdiffabout
path: root/microkde/kdecore/kshell.h
Unidiff
Diffstat (limited to 'microkde/kdecore/kshell.h') (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kdecore/kshell.h143
1 files changed, 143 insertions, 0 deletions
diff --git a/microkde/kdecore/kshell.h b/microkde/kdecore/kshell.h
new file mode 100644
index 0000000..35d8217
--- a/dev/null
+++ b/microkde/kdecore/kshell.h
@@ -0,0 +1,143 @@
1/*
2 This file is part of the KDE libraries
3
4 Copyright (c) 2003 Oswald Buddenhagen <ossi@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20*/
21#ifndef _KSHELL_H
22#define _KSHELL_H
23
24#include <qstring.h>
25#include <qstringlist.h>
26
27/**
28 * Provides some basic POSIX shell and bash functionality.
29 * @see KStringHandler
30 */
31namespace KShell {
32
33 /**
34 * Flags for @ref splitArgs().
35 */
36 enum Options {
37 NoOptions = 0,
38
39 /**
40 * Perform tilde expansion.
41 */
42 TildeExpand = 1,
43
44 /**
45 * Bail out if a non-quoting and not quoted shell meta character is encoutered.
46 * Meta characters are the command separators @p semicolon and @p ampersand,
47 * the redirection symbols @p less-than, @p greater-than and the @p pipe @p symbol,
48 * the grouping symbols opening and closing @p parens and @p braces, the command
49 * substitution symbol @p backquote, the generic substitution symbol @p dollar
50 * (if not followed by an apostrophe), the wildcards @p asterisk and
51 * @p question @p mark, and the comment symbol @p hash @p mark. Additionally,
52 * a variable assignment in the first word is recognized.
53 */
54 AbortOnMeta = 2
55 };
56
57 /**
58 * Status codes from @ref splitArgs()
59 */
60 enum Errors {
61 /**
62 * Success.
63 */
64 NoError = 0,
65
66 /**
67 * Indicates a parsing error, like an unterminated quoted string.
68 */
69 BadQuoting,
70
71 /**
72 * The AbortOnMeta flag was set and a shell meta character
73 * was encoutered.
74 */
75 FoundMeta
76 };
77
78 /**
79 * Splits @p cmd according to POSIX shell word splitting and quoting rules.
80 * Can optionally perform tilde expansion and/or abort if it finds shell
81 * meta characters it cannot process.
82 *
83 * @param cmd the command to split
84 * @param flags operation flags, see @ref Options
85 * @param err if not NULL, a status code will be stored at the pointer
86 * target, see @ref Errors
87 * @return a list of unquoted words or an empty list if an error occured
88 */
89 QStringList splitArgs( const QString &cmd, int flags = 0, int *err = 0 );
90
91 /**
92 * Quotes and joins @p args together according to POSIX shell rules.
93 *
94 * @param args a list of strings to quote and join
95 * @return a command suitable for shell execution
96 */
97 QString joinArgs( const QStringList &args );
98
99 /**
100 * Same as above, but $'' is used instead of '' for the quoting.
101 * The output is suitable for @ref splitArgs(), bash, zsh and possibly
102 * other bourne-compatible shells, but not for plain sh. The advantage
103 * is, that control characters (ASCII less than 32) are escaped into
104 * human-readable strings.
105 *
106 * @param args a list of strings to quote and join
107 * @return a command suitable for shell execution
108 */
109 QString joinArgsDQ( const QStringList &args );
110
111 /**
112 * Quotes and joins @p argv together according to POSIX shell rules.
113 *
114 * @param argv an array of c strings to quote and join.
115 * The strings are expected to be in local-8-bit encoding.
116 * @param argc maximal number of strings in @p argv. if not supplied,
117 * @p argv must be null-terminated.
118 * @return a command suitable for shell execution
119 */
120 QString joinArgs( const char * const *argv, int argc = -1 );
121
122 /**
123 * Performs tilde expansion on @p path. Interprets "~/path" and
124 * "~user/path".
125 *
126 * @param path the path to tilde-expand
127 * @return the expanded path
128 */
129 QString tildeExpand( const QString &path );
130
131 /**
132 * Obtain a @p user's home directory.
133 *
134 * @param user The name of the user whose home dir should be obtained.
135 * An empty string denotes the current user.
136 * @return The user's home directory.
137 */
138 QString homeDir( const QString &user );
139
140}
141
142
143#endif /* _KSHELL_H */