sbs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
  3. * All rights reserved.
  4. * This component and the accompanying materials are made available
  5. * under the terms of the License "Eclipse Public License v1.0"
  6. * which accompanies this distribution, and is available
  7. * at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. *
  9. * Initial Contributors:
  10. * Nokia Corporation - initial contribution.
  11. *
  12. * Contributors:
  13. *
  14. * Description:
  15. *
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. #include "env.h"
  22. #include "log.h"
  23. #include "buffer.h"
  24. #include "talon_process.h"
  25. #include "file.h"
  26. #ifdef HAS_MSVCRT
  27. /* Make all output handling binary */
  28. unsigned int _CRT_fmode = _O_BINARY;
  29. #endif
  30. #define OUTPUT_UNWANTED 0
  31. #define OUTPUT_WANTED 1
  32. #define OUTPUT_UNBUFFERED 2
  33. #define OUTPUT_BUFFERED 3
  34. typedef struct env_vars
  35. {
  36. char name[1000];
  37. char value[1000];
  38. } env_var;
  39. env_var sbs_env[] =
  40. {
  41. {"HOSTPLATFORM", "win 32"},
  42. {"HOSTPLATFORM_DIR", "win32"},
  43. {"HOSTPLATFORM32_DIR", "win32"},
  44. {"SBS_HOME", "C:\\build_c\\hg\\raptor"},
  45. {"__PYTHON__", "C:\\build_c\\Apps\\Python270\\python.exe"},
  46. {"PYTHONPATH", "C:\\build_c\\hg\\raptor\\raptor"},
  47. {"__MINGW__", "C:\\build_c\\hg\\win32-support\\mingw"},
  48. {"__MOUNTOPTIONS__", "-u"},
  49. {"__UMOUNTOPTIONS__", "-u"},
  50. {"CYGWIN", "nontsec nosmbntsec"},
  51. {"__CYGWIN__", "C:\\build_c\\hg\\win32-support\\cygwin"},
  52. };
  53. typedef struct command_struct
  54. {
  55. char *name;
  56. char *commandline;
  57. } command;
  58. command commands[] =
  59. {
  60. {"umount.exe", "-u /tmp"},
  61. {"mount.exe", "-u \"C:\\build_c\\hg\\win32-support\\cygwin\\tmp\" /tmp"},
  62. {"umount.exe", "-u /"},
  63. {"mount.exe", "-u \"C:\\build_c\\hg\\win32-support\\cygwin\" /"},
  64. {"mount.exe", ""},
  65. {(char *)0, (char *)0}
  66. };
  67. int do_process(char executable[], char *args[], int timeout, int want_output)
  68. {
  69. proc *p;
  70. p = process_run(executable, args, timeout);
  71. int retval = 0;
  72. if (p)
  73. {
  74. unsigned int iterator = 0;
  75. byteblock *bb;
  76. while ((bb = buffer_getbytes(p->output, &iterator)))
  77. {
  78. if(want_output)
  79. write(STDOUT_FILENO, &bb->byte0, bb->fill);
  80. }
  81. retval = p->returncode;
  82. process_free(&p);
  83. return retval;
  84. } else {
  85. fprintf(stderr, "error: %s", "failed to run process\n");
  86. return 1;
  87. }
  88. }
  89. char *get_sbs_home(void)
  90. {
  91. char *sbs_home = talon_getenv("SBS_HOME");
  92. if(sbs_home != NULL)
  93. DEBUG(("SBS_HOME found in environment: %s\n", sbs_home));
  94. else
  95. {
  96. sbs_home = get_parent_dir(get_parent_dir(get_exe_location()));
  97. DEBUG(("SBS_HOME calculated as: %s\n", sbs_home));
  98. }
  99. return sbs_home;
  100. }
  101. char *set_python(char *sbs_home)
  102. {
  103. char *sbs_python = talon_getenv("SBS_PYTHON");
  104. if(sbs_python != NULL)
  105. {
  106. DEBUG(("SBS_PYTHON = %s\n", sbs_python));
  107. return sbs_python;
  108. }
  109. char local_python[] = "win32\\python27\\python.exe";
  110. int size = strlen(sbs_home) + strlen(local_python) + 2;
  111. char *__local_python__ = malloc(size);
  112. snprintf(__local_python__, size, "%s\\%s", sbs_home, local_python);
  113. int exists = is_file(__local_python__);
  114. if(exists == 1) // Check that local python exists
  115. {
  116. DEBUG(("Found local Python %s\n", __local_python__));
  117. talon_setenv("SBS_PYTHON", __local_python__);
  118. talon_setenv("PYTHONPATH", "");
  119. talon_setenv("PYTHONHOME", "");
  120. }
  121. else
  122. {
  123. DEBUG(("Using python.exe from PATH as no local Python exists.\n"));
  124. // __local_python__ is already at least the length of
  125. // the string "python.exe" due to the malloc above, so
  126. // it can be reused to return "python.exe"
  127. snprintf(__local_python__, 11, "python.exe");
  128. }
  129. return __local_python__;
  130. }
  131. int main(int argc, char *argv[])
  132. {
  133. int i;
  134. int retval = 0;
  135. char * talon_debug = talon_getenv("TALON_DEBUG");
  136. if(talon_debug != NULL)
  137. {
  138. loglevel = 1;
  139. free(talon_debug);
  140. }
  141. char *sbs_home = get_sbs_home();
  142. if(sbs_home == NULL)
  143. {
  144. printf("Error determining SBS_HOME. Cannot continue.");
  145. retval = -1;
  146. }
  147. char *python = set_python(sbs_home);
  148. DEBUG(("Using python = %s\n", python));
  149. /* sizeof(sbs_env)/sizeof(env_var) is the size of the sbs_env array */
  150. for(i = 0; i < sizeof(sbs_env)/sizeof(env_var); i++)
  151. {
  152. talon_setenv(sbs_env[i].name, sbs_env[i].value);
  153. }
  154. int path_prepend_ok = talon_pathprepend("C:\\build_c\\hg\\win32-support\\mingw\\bin;"
  155. "C:\\build_c\\hg\\win32-support\\cygwin\\bin;"
  156. "C:\\build_c\\hg\\raptor\\win32\\bin");
  157. if(path_prepend_ok != 0)
  158. {
  159. printf("Failed to prepend to PATH. Exiting.\n");
  160. return -1;
  161. }
  162. i = 0;
  163. char **args = malloc((argc+10)*sizeof(char *));
  164. while((commands[i].name != (char *)0) && (commands[i].commandline != (char *)0))
  165. {
  166. DEBUG(("command:\n%s\ncommandline:\n%s\n", commands[i].name, commands[i].commandline));
  167. args[0] = NULL;
  168. args[1] = commands[i].commandline;
  169. args[2] = NULL;
  170. retval = do_process(commands[i].name, args, 4000, OUTPUT_WANTED);
  171. i++;
  172. };
  173. args[0] = NULL;
  174. args[1] = "C:\\build_c\\hg\\raptor\\python\\raptor_start.py";
  175. for(i = 1; i < argc; i++)
  176. {
  177. args[i+1] = argv[i];
  178. }
  179. args[argc+1] = NULL;
  180. retval = do_process(python, args, 3600000, OUTPUT_WANTED);
  181. free(python);
  182. free(args);
  183. free(sbs_home);
  184. return retval;
  185. }