shell.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <EXTERN.h> // from the Perl distribution
  2. #include <perl.h> // from the Perl distribution
  3. #ifdef WIN32
  4. // Perl win32 specific includes, found in perl\\lib\\CORE\\win32.h
  5. // Defines the windows specific convenience RunPerl() function,
  6. // which is not available on other operating systems.
  7. #include <win32.h>
  8. #include <wchar.h>
  9. #endif
  10. #include <cstdio>
  11. #include <cstdlib>
  12. #ifdef WIN32
  13. int main(int argc, char **argv, char **env)
  14. {
  15. // If the Slic3r is installed in a localized directory (containing non-iso
  16. // characters), spaces or semicolons, use short file names.
  17. char exe_path[MAX_PATH] = {0};
  18. char script_path[MAX_PATH];
  19. char gui_flag[6] = {"--gui"};
  20. #ifdef FORCE_GUI
  21. char** command_line = (char**)malloc(sizeof(char*) * ((++ argc) + 2));
  22. #else
  23. char** command_line = (char**)malloc(sizeof(char*) * ((++ argc) + 1));
  24. #endif
  25. {
  26. // Unicode path. This will not be used directly, but to test, whether
  27. // there are any non-ISO characters, in which case the path is converted to a
  28. // short path (using 8.3 directory names).
  29. wchar_t exe_path_w[MAX_PATH] = {0};
  30. char drive[_MAX_DRIVE];
  31. char dir[_MAX_DIR];
  32. char fname[_MAX_FNAME];
  33. char ext[_MAX_EXT];
  34. bool needs_short_paths = false;
  35. int len;
  36. int i;
  37. GetModuleFileNameA(NULL, exe_path, MAX_PATH-1);
  38. GetModuleFileNameW(NULL, exe_path_w, MAX_PATH-1);
  39. len = strlen(exe_path);
  40. if (len != wcslen(exe_path_w)) {
  41. needs_short_paths = true;
  42. } else {
  43. for (i = 0; i < len; ++ i)
  44. if ((wchar_t)exe_path[i] != exe_path_w[i] || exe_path[i] == ' ' ||
  45. exe_path[i] == ';') {
  46. needs_short_paths = true;
  47. break;
  48. }
  49. }
  50. if (needs_short_paths) {
  51. wchar_t exe_path_short[MAX_PATH] = {0};
  52. GetShortPathNameW(exe_path_w, exe_path_short, MAX_PATH);
  53. len = wcslen(exe_path_short);
  54. for (i = 0; i <= len; ++ i)
  55. exe_path[i] = (char)exe_path_short[i];
  56. }
  57. _splitpath(exe_path, drive, dir, fname, ext);
  58. _makepath(script_path, drive, dir, NULL, NULL);
  59. if (needs_short_paths)
  60. printf("Slic3r installed in a loclized path. Using an 8.3 path: \"%s\"\n",
  61. script_path);
  62. SetDllDirectoryA(script_path);
  63. _makepath(script_path, drive, dir, "slic3r", "pl");
  64. command_line[0] = exe_path;
  65. command_line[1] = script_path;
  66. memcpy(command_line + 2, argv + 1, sizeof(char*) * (argc - 2));
  67. #ifdef FORCE_GUI
  68. command_line[argc] = gui_flag;
  69. command_line[argc+1] = NULL;
  70. #else
  71. command_line[argc] = NULL;
  72. #endif
  73. // Unset the PERL5LIB and PERLLIB environment variables.
  74. SetEnvironmentVariable("PERL5LIB", NULL);
  75. SetEnvironmentVariable("PERLLIB", NULL);
  76. #if 0
  77. printf("Arguments: \r\n");
  78. for (size_t i = 0; i < argc + 1; ++ i)
  79. printf(" %d: %s\r\n", i, command_line[i]);
  80. #endif
  81. }
  82. #ifdef FORCE_GUI
  83. RunPerl(argc+1, command_line, NULL);
  84. #else
  85. RunPerl(argc, command_line, NULL);
  86. #endif
  87. free(command_line);
  88. }
  89. #else
  90. int main(int argc, char **argv, char **env)
  91. {
  92. PerlInterpreter *my_perl = perl_alloc();
  93. if (my_perl == NULL) {
  94. fprintf(stderr, "Cannot start perl interpreter. Exiting.\n");
  95. return -1;
  96. }
  97. perl_construct(my_perl);
  98. #ifdef FORCE_GUI
  99. char* command_line[] = { "slic3r", "slic3r.pl", "--gui" };
  100. #else
  101. char* command_line[] = { "slic3r", "slic3r.pl" };
  102. #endif
  103. perl_parse(my_perl, NULL, 3, command_line, (char **)NULL);
  104. perl_run(my_perl);
  105. perl_destruct(my_perl);
  106. perl_free(my_perl);
  107. }
  108. #endif