ckunix.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*++
  2. Copyright (c) 2016 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. ckunix.c
  9. Abstract:
  10. This module implements support for the Chalk application on POSIX like OSes.
  11. Author:
  12. Evan Green 15-Aug-2016
  13. Environment:
  14. POSIX
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <assert.h>
  20. #include <libgen.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <minoca/lib/types.h>
  26. #include <minoca/lib/chalk.h>
  27. #include <minoca/lib/chalk/app.h>
  28. //
  29. // ---------------------------------------------------------------- Definitions
  30. //
  31. #define CK_APP_PREFIX "/usr"
  32. #define CK_APP_LIBDIR CK_APP_PREFIX "/lib"
  33. //
  34. // ------------------------------------------------------ Data Type Definitions
  35. //
  36. //
  37. // ----------------------------------------------- Internal Function Prototypes
  38. //
  39. VOID
  40. ChalkAddSearchPath (
  41. PVOID Vm,
  42. PSTR Directory,
  43. PSTR ChalkDirectory
  44. );
  45. //
  46. // -------------------------------------------------------------------- Globals
  47. //
  48. //
  49. // ------------------------------------------------------------------ Functions
  50. //
  51. VOID
  52. ChalkSetupModulePath (
  53. PVOID Vm,
  54. PSTR Script
  55. )
  56. /*++
  57. Routine Description:
  58. This routine adds the default library search paths. This routine assumes
  59. there are at least two slots on the stack and that the module path list has
  60. already been pushed.
  61. Arguments:
  62. Vm - Supplies a pointer to the virtual machine.
  63. Script - Supplies a pointer to the script path.
  64. Return Value:
  65. None.
  66. --*/
  67. {
  68. PSTR Copy;
  69. PSTR CurrentDirectory;
  70. PSTR DirName;
  71. PSTR ExecName;
  72. PSTR Home;
  73. PSTR Next;
  74. PSTR Path;
  75. PSTR ScriptBase;
  76. Copy = NULL;
  77. if (Script != NULL) {
  78. Copy = strdup(Script);
  79. if (Copy == NULL) {
  80. return;
  81. }
  82. }
  83. //
  84. // If a script was supplied, add the directory of the script.
  85. //
  86. if (Script != NULL) {
  87. ScriptBase = dirname(Copy);
  88. if (ScriptBase != NULL) {
  89. ChalkAddSearchPath(Vm, ScriptBase, NULL);
  90. }
  91. //
  92. // In interactive mode, add the current working directory.
  93. //
  94. } else {
  95. CurrentDirectory = getcwd(NULL, 0);
  96. if (CurrentDirectory != NULL) {
  97. ChalkAddSearchPath(Vm, CurrentDirectory, NULL);
  98. free(CurrentDirectory);
  99. }
  100. }
  101. if (Copy != NULL) {
  102. free(Copy);
  103. Copy = NULL;
  104. }
  105. //
  106. // Add the special environment variable.
  107. //
  108. Path = getenv("CK_LIBRARY_PATH");
  109. if (Path != NULL) {
  110. Copy = strdup(Path);
  111. if (Copy == NULL) {
  112. return;
  113. }
  114. Path = Copy;
  115. while (Path != NULL) {
  116. Next = strchr(Path, ':');
  117. if (Next != NULL) {
  118. *Next = '\0';
  119. Next += 1;
  120. }
  121. ChalkAddSearchPath(Vm, Path, NULL);
  122. Path = Next;
  123. }
  124. free(Copy);
  125. } else {
  126. //
  127. // Add the sysroot-like path relative to the executable.
  128. //
  129. if ((CkAppExecName != NULL) && (*CkAppExecName != '\0')) {
  130. ExecName = strdup(CkAppExecName);
  131. if (ExecName != NULL) {
  132. DirName = dirname(ExecName);
  133. if (DirName != NULL) {
  134. ChalkAddSearchPath(Vm, DirName, "../lib/chalk");
  135. }
  136. free(ExecName);
  137. }
  138. }
  139. //
  140. // Add the current user's home directory.
  141. //
  142. Home = getenv("HOME");
  143. if (Home != NULL) {
  144. ChalkAddSearchPath(Vm, Home, ".chalk");
  145. }
  146. //
  147. // Add the system path.
  148. //
  149. ChalkAddSearchPath(Vm, CK_APP_LIBDIR, "chalk");
  150. }
  151. return;
  152. }
  153. //
  154. // --------------------------------------------------------- Internal Functions
  155. //