| 1 | /* |
| 2 | * This file is part of Jam - see jam.c for Copyright information. |
| 3 | */ |
| 4 | |
| 5 | #include "jam.h" |
| 6 | |
| 7 | #if defined(OS_AMIGA) |
| 8 | |
| 9 | #include "lists.h" |
| 10 | #include "execcmd.h" |
| 11 | #include "patchlevel.h" |
| 12 | |
| 13 | #include <string.h> |
| 14 | #include <stdlib.h> |
| 15 | |
| 16 | #include <proto/exec.h> |
| 17 | #include <proto/dos.h> |
| 18 | |
| 19 | #include <utility/hooks.h> |
| 20 | #include <dos/dos.h> |
| 21 | #include <dos/var.h> |
| 22 | |
| 23 | #define MAX_ENV_SIZE 1024 /* maximum number of environ entries */ |
| 24 | |
| 25 | |
| 26 | /* clib2 specific controls */ |
| 27 | int __minimum_os_lib_version = 52; |
| 28 | char * __minimum_os_lib_error = "Requires AmigaOS 4.0"; |
| 29 | BOOL __open_locale = FALSE; |
| 30 | BOOL __disable_dos_requesters = TRUE; |
| 31 | |
| 32 | |
| 33 | static const char* version __attribute__((used)) = |
| 34 | "$VER: bjam "VERSION"-1 (2.6.2007)"; /* dd.mm.yyyy */ |
| 35 | |
| 36 | |
| 37 | /* |
| 38 | * execamiga.c - execute a shell script |
| 39 | * |
| 40 | * Uses the abc-shell to execute all commands synchronously. |
| 41 | */ |
| 42 | void execcmd |
| 43 | ( |
| 44 | char *string, |
| 45 | void (*func)(void *closure, int status, timing_info*), |
| 46 | void *closure, |
| 47 | LIST *shell |
| 48 | ) |
| 49 | { |
| 50 | timing_info time = {0, 0}; |
| 51 | |
| 52 | static char tmpbuf[128]; |
| 53 | tmpbuf[0] = '\0'; |
| 54 | strlcpy(tmpbuf, "/t/bjamXXXXXX", sizeof(tmpbuf)); |
| 55 | |
| 56 | int fd = mkstemp(tmpbuf); |
| 57 | if ( fd == -1 ) |
| 58 | { |
| 59 | printf("can't open command file\n"); |
| 60 | (*func)(closure, EXEC_CMD_FAIL, &time); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | int len = strlen(string); |
| 65 | if ( write(fd, string, len) != len ) |
| 66 | { |
| 67 | printf("can't write command file\n"); |
| 68 | (*func)(closure, EXEC_CMD_FAIL, &time); |
| 69 | unlink(tmpbuf); |
| 70 | close(fd); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | static char cmdbuf[128]; |
| 75 | cmdbuf[0] = '\0'; |
| 76 | snprintf(cmdbuf, sizeof(cmdbuf), "/sdk/c/sh %s", tmpbuf); |
| 77 | |
| 78 | if ( DEBUG_EXECCMD ) |
| 79 | { |
| 80 | printf("string = '%s'\n", string); |
| 81 | printf("cmdbuf = '%s'\n", cmdbuf); |
| 82 | } |
| 83 | |
| 84 | int status = system(cmdbuf); |
| 85 | |
| 86 | unlink(tmpbuf); |
| 87 | close(fd); |
| 88 | |
| 89 | if ( status == 0 ) |
| 90 | { |
| 91 | (*func)(closure, EXEC_CMD_OK, &time); |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | (*func)(closure, EXEC_CMD_FAIL, &time); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | int execwait() |
| 101 | { |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /* |
| 107 | * environ - AmigaOS environment support |
| 108 | * |
| 109 | * The following code implements environ support for AmigaOS. |
| 110 | */ |
| 111 | void make_environ() __attribute__((constructor)); |
| 112 | void free_environ() __attribute__((destructor)); |
| 113 | |
| 114 | char **environ = NULL; |
| 115 | |
| 116 | |
| 117 | int is_ascii_string(char* str) |
| 118 | { |
| 119 | while ( *str != '\0' ) |
| 120 | { |
| 121 | if ( !isascii(*str) ) |
| 122 | { |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | ++str; |
| 127 | } |
| 128 | |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | uint32 copy_env(struct Hook *hook, APTR data, struct ScanVarsMsg *msg) |
| 134 | { |
| 135 | static uint32 env_size = 1; /* environ is null terminated */ |
| 136 | |
| 137 | if ( env_size == MAX_ENV_SIZE ) |
| 138 | { |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | if ( msg->sv_Name == 0 || |
| 143 | msg->sv_Var == 0 || |
| 144 | strlen(msg->sv_GDir) > 4 || /* excludes ENVARC: sub-directories */ |
| 145 | !is_ascii_string(msg->sv_Name) || |
| 146 | !is_ascii_string(msg->sv_Var) || |
| 147 | strstr(msg->sv_Name, ".prefs") != 0 || |
| 148 | strstr(msg->sv_Name, ".xml") != 0 || |
| 149 | strstr(msg->sv_Name, ".cfg") != 0 ) |
| 150 | |
| 151 | { |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | uint32 var_size = strlen(msg->sv_Name) + 1 + msg->sv_VarLen + 1; |
| 156 | char* var_buf = malloc(var_size); |
| 157 | if ( var_buf == 0 ) |
| 158 | { |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | snprintf(var_buf, var_size, "%s=%s", msg->sv_Name, msg->sv_Var); |
| 163 | |
| 164 | char **env = hook->h_Data; |
| 165 | env[env_size - 1] = var_buf; |
| 166 | ++env_size; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | void make_environ() |
| 173 | { |
| 174 | size_t environ_size = MAX_ENV_SIZE * sizeof(char*); |
| 175 | |
| 176 | environ = (char**)malloc(environ_size); |
| 177 | if ( environ == 0 ) |
| 178 | { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | memset(environ, 0, environ_size); |
| 183 | |
| 184 | struct Hook hook; |
| 185 | memset(&hook, 0, sizeof(struct Hook)); |
| 186 | hook.h_Entry = copy_env; |
| 187 | hook.h_Data = environ; |
| 188 | (void) IDOS->ScanVars(&hook, 0, 0); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | void free_environ() |
| 193 | { |
| 194 | char **i; |
| 195 | for ( i = environ; *i != 0; ++i ) |
| 196 | { |
| 197 | free(*i); |
| 198 | } |
| 199 | |
| 200 | free(environ); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | #endif |