p3dPythonMain.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Filename: p3dPythonMain.cxx
  2. // Created by: drose (29Aug09)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #include "run_p3dpython.h"
  15. #include <iostream>
  16. #include <sstream>
  17. #include <vector>
  18. #include <assert.h>
  19. #include <string.h> // strrchr
  20. using namespace std;
  21. #if defined(_WIN32) && defined(NON_CONSOLE)
  22. // On Windows, we may need to build p3dpythonw.exe, a non-console
  23. // version of this program.
  24. // We'll wrap the main() function with our own startup WinMain().
  25. #define main local_main
  26. int main(int argc, char *argv[]);
  27. // Returns a newly-allocated string representing the quoted argument
  28. // beginning at p. Advances p to the first character following the
  29. // close quote.
  30. static char *
  31. parse_quoted_arg(char *&p) {
  32. char quote = *p;
  33. ++p;
  34. string result;
  35. while (*p != '\0' && *p != quote) {
  36. // TODO: handle escape characters? Not sure if we need to.
  37. result += *p;
  38. ++p;
  39. }
  40. if (*p == quote) {
  41. ++p;
  42. }
  43. return strdup(result.c_str());
  44. }
  45. // Returns a newly-allocated string representing the unquoted argument
  46. // beginning at p. Advances p to the first whitespace following the
  47. // argument.
  48. static char *
  49. parse_unquoted_arg(char *&p) {
  50. string result;
  51. while (*p != '\0' && !isspace(*p)) {
  52. result += *p;
  53. ++p;
  54. }
  55. return strdup(result.c_str());
  56. }
  57. int WINAPI
  58. WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
  59. char *command_line = GetCommandLine();
  60. vector<char *> argv;
  61. char *p = command_line;
  62. while (*p != '\0') {
  63. if (*p == '"') {
  64. char *arg = parse_quoted_arg(p);
  65. argv.push_back(arg);
  66. } else {
  67. char *arg = parse_unquoted_arg(p);
  68. argv.push_back(arg);
  69. }
  70. // Skip whitespace.
  71. while (*p != '\0' && isspace(*p)) {
  72. ++p;
  73. }
  74. }
  75. assert(!argv.empty());
  76. return main(argv.size(), &argv[0]);
  77. }
  78. #endif // NON_CONSOLE
  79. ////////////////////////////////////////////////////////////////////
  80. // Function: main
  81. // Description: This is a trivial main() function that invokes
  82. // P3DPythonRun. It's used to build p3dpython.exe,
  83. // which is the preferred way to run Python in a child
  84. // process, as a separate executable.
  85. ////////////////////////////////////////////////////////////////////
  86. int
  87. main(int argc, char *argv[]) {
  88. const char *program_name = argv[0];
  89. const char *archive_file = NULL;
  90. const char *input_handle_str = NULL;
  91. const char *output_handle_str = NULL;
  92. const char *interactive_console_str = NULL;
  93. if (argc > 1) {
  94. archive_file = argv[1];
  95. }
  96. if (argc > 2) {
  97. input_handle_str = argv[2];
  98. }
  99. if (argc > 3) {
  100. output_handle_str = argv[3];
  101. }
  102. if (argc > 4) {
  103. interactive_console_str = argv[4];
  104. }
  105. if (archive_file == NULL || *archive_file == '\0') {
  106. cerr << "No archive filename specified on command line.\n";
  107. return 1;
  108. }
  109. FHandle input_handle = invalid_fhandle;
  110. if (input_handle_str != NULL && *input_handle_str) {
  111. stringstream stream(input_handle_str);
  112. stream >> input_handle;
  113. if (!stream) {
  114. input_handle = invalid_fhandle;
  115. }
  116. }
  117. FHandle output_handle = invalid_fhandle;
  118. if (output_handle_str != NULL && *output_handle_str) {
  119. stringstream stream(output_handle_str);
  120. stream >> output_handle;
  121. if (!stream) {
  122. output_handle = invalid_fhandle;
  123. }
  124. }
  125. bool interactive_console = false;
  126. if (interactive_console_str != NULL && *interactive_console_str) {
  127. stringstream stream(interactive_console_str);
  128. int flag;
  129. stream >> flag;
  130. if (!stream.fail()) {
  131. interactive_console = (flag != 0);
  132. }
  133. }
  134. int status = run_p3dpython(program_name, archive_file, input_handle,
  135. output_handle, NULL, interactive_console);
  136. if (status != 0) {
  137. cerr << "Failure on startup.\n";
  138. }
  139. return status;
  140. }