/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMScriptRunner.h

http://macfuse.googlecode.com/ · C++ Header · 134 lines · 28 code · 12 blank · 94 comment · 0 complexity · aca0eeae330768e6821eb72fd4843c87 MD5 · raw file

  1. //
  2. // GTMScriptRunner.h
  3. //
  4. // Copyright 2007-2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import <Foundation/Foundation.h>
  19. /// Encapsulates the interaction with an interpreter for running scripts.
  20. // This class manages the interaction with some command-line interpreter (e.g.,
  21. // a shell, perl, python) and allows you to run expressions through the
  22. // interpreter, and even full scripts that reside in files on disk. By default,
  23. // the "/bin/sh" interpreter is used, but others may be explicitly specified.
  24. // This can be a convenient way to run quick shell commands from Cocoa, or even
  25. // interact with other shell tools such as "bc", or even "gdb".
  26. //
  27. // It's important to note that by default commands and scripts will have their
  28. // environments erased before execution. You can control the environment they
  29. // get with the -setEnvironment: method.
  30. //
  31. // The best way to show what this class does is to show some examples.
  32. //
  33. // Examples:
  34. //
  35. // GTMScriptRunner *sr = [GTMScriptRunner runner];
  36. // NSString *output = [sr run:@"ls -l /dev/null"];
  37. // /* output == "crw-rw-rw- 1 root wheel 3, 2 Mar 22 10:35 /dev/null" */
  38. //
  39. // GTMScriptRunner *sr = [GTMScriptRunner runner];
  40. // NSString *output = [sr runScript:@"/path/to/my/script.sh"];
  41. // /* output == the standard output from the script*/
  42. //
  43. // GTMScriptRunner *sr = [GTMScriptRunner runnerWithPerl];
  44. // NSString *output = [sr run:@"print 'A'x4"];
  45. // /* output == "AAAA" */
  46. //
  47. // See the unit test file for more examples.
  48. //
  49. @interface GTMScriptRunner : NSObject {
  50. @private
  51. NSString *interpreter_;
  52. NSArray *interpreterArgs_;
  53. NSDictionary *environment_;
  54. BOOL trimsWhitespace_;
  55. }
  56. // Convenience methods for returning autoreleased GTMScriptRunner instances, that
  57. // are associated with the specified interpreter. The default interpreter
  58. // (returned from +runner is "/bin/sh").
  59. + (GTMScriptRunner *)runner;
  60. + (GTMScriptRunner *)runnerWithBash;
  61. + (GTMScriptRunner *)runnerWithPerl;
  62. + (GTMScriptRunner *)runnerWithPython;
  63. // Returns an autoreleased GTMScriptRunner instance associated with the specified
  64. // interpreter, and the given args. The specified args are the arguments that
  65. // should be applied to the interpreter itself, not scripts run through the
  66. // interpreter. For example, to start an interpreter using "perl -w", you could
  67. // do:
  68. // [GTMScriptRunner runnerWithInterpreter:@"/usr/bin/perl"
  69. // withArgs:[NSArray arrayWithObject:@"-w"]];
  70. //
  71. + (GTMScriptRunner *)runnerWithInterpreter:(NSString *)interp;
  72. + (GTMScriptRunner *)runnerWithInterpreter:(NSString *)interp
  73. withArgs:(NSArray *)args;
  74. // Returns a GTMScriptRunner associated with |interp|
  75. - (id)initWithInterpreter:(NSString *)interp;
  76. // Returns a GTMScriptRunner associated with |interp| and |args| applied to the
  77. // specified interpreter. This method is the designated initializer.
  78. - (id)initWithInterpreter:(NSString *)interp withArgs:(NSArray *)args;
  79. // Runs the specified command string by sending it through the interpreter's
  80. // standard input. The standard output is returned. The standard error is
  81. // discarded.
  82. - (NSString *)run:(NSString *)cmds;
  83. // Same as the previous method, except the standard error is returned in |err|
  84. // if specified.
  85. - (NSString *)run:(NSString *)cmds standardError:(NSString **)err;
  86. // Runs the file at |path| using the interpreter.
  87. - (NSString *)runScript:(NSString *)path;
  88. // Runs the file at |path|, passing it |args| as arguments.
  89. - (NSString *)runScript:(NSString *)path withArgs:(NSArray *)args;
  90. // Same as above, except the standard error is returned in |err| if specified.
  91. - (NSString *)runScript:(NSString *)path withArgs:(NSArray *)args
  92. standardError:(NSString **)err;
  93. // Returns the environment dictionary to use for the inferior process that will
  94. // run the interpreter. A return value of nil means that the interpreter's
  95. // environment should be erased.
  96. - (NSDictionary *)environment;
  97. // Sets the environment dictionary to use for the interpreter process. See
  98. // NSTask's -setEnvironment: documentation for details about the dictionary.
  99. // Basically, it's just a dict of key/value pairs corresponding to environment
  100. // keys and values. Setting a value of nil means that the environment should be
  101. // erased before running the interpreter.
  102. //
  103. // *** The default is nil. ***
  104. //
  105. // By default, all interpreters will run with a clean environment. If you want
  106. // the interpreter process to inherit your current environment you'll need to
  107. // do the following:
  108. //
  109. // GTMScriptRunner *sr = [GTMScriptRunner runner];
  110. // [sr setEnvironment:[[NSProcessInfo processInfo] environment]];
  111. //
  112. // SECURITY NOTE: That said, in general you should NOT do this because an
  113. // attacker can modify the environment that would then get sent to your scripts.
  114. // And if your binary is suid, then you ABSOLUTELY should not do this.
  115. //
  116. - (void)setEnvironment:(NSDictionary *)newEnv;
  117. // Sets (and returns) whether or not whitespace is automatically trimmed from
  118. // the ends of the returned strings. The default is YES, so trailing newlines
  119. // will be removed.
  120. - (BOOL)trimsWhitespace;
  121. - (void)setTrimsWhitespace:(BOOL)trim;
  122. @end