]>
Commit | Line | Data |
---|---|---|
bc78a6d5 RC |
1 | /* |
2 | * Copyright (c) 2001, Jan Nieuwenhuizen. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by DJ Delorie <dj@cygnus.com> | |
13 | * Jan Nieuwenhuizen <janneke@gnu.org> | |
14 | * | |
15 | */ | |
16 | ||
17 | /* The purpose of this file is to provide functions for the invocation | |
18 | of install scripts. */ | |
19 | ||
20 | #if 0 | |
21 | static const char *cvsid = | |
22 | "\n%%% $Id$\n"; | |
23 | #endif | |
24 | ||
25 | #include "win32.h" | |
26 | #include <stdlib.h> | |
27 | #include <unistd.h> | |
28 | #include <stdio.h> | |
29 | #include "log.h" | |
30 | #include "concat.h" | |
3c054baf | 31 | #include "filemanip.h" |
bc78a6d5 RC |
32 | #include "mount.h" |
33 | #include "io_stream.h" | |
34 | ||
3c054baf | 35 | static String sh = String(); |
bc78a6d5 RC |
36 | static const char *cmd = 0; |
37 | static OSVERSIONINFO verinfo; | |
38 | ||
39 | static const char *shells[] = { | |
40 | "/bin/sh.exe", | |
41 | "/usr/bin/sh.exe", | |
42 | "/bin/bash.exe", | |
43 | "/usr/bin/bash.exe", | |
44 | 0 | |
45 | }; | |
46 | ||
47 | void | |
48 | init_run_script () | |
49 | { | |
50 | for (int i = 0; shells[i]; i++) | |
51 | { | |
3c054baf RC |
52 | sh = backslash (cygpath (shells[i],0).cstr_oneuse()); |
53 | if (_access (sh.cstr_oneuse(), 0) == 0) | |
bc78a6d5 | 54 | break; |
3c054baf | 55 | sh = String(); |
bc78a6d5 RC |
56 | } |
57 | ||
58 | char old_path[_MAX_PATH]; | |
59 | GetEnvironmentVariable ("PATH", old_path, sizeof (old_path)); | |
60 | SetEnvironmentVariable ("PATH", | |
61 | backslash (cygpath ("/bin;", | |
3c054baf RC |
62 | get_root_dir ().cstr_oneuse(), "/usr/bin;", |
63 | old_path, 0)).cstr_oneuse()); | |
bc78a6d5 | 64 | |
3c054baf | 65 | SetEnvironmentVariable ("CYGWINROOT", get_root_dir ().cstr_oneuse()); |
bc78a6d5 RC |
66 | |
67 | verinfo.dwOSVersionInfoSize = sizeof (verinfo); | |
68 | GetVersionEx (&verinfo); | |
69 | ||
70 | switch (verinfo.dwPlatformId) | |
71 | { | |
72 | case VER_PLATFORM_WIN32_NT: | |
73 | cmd = "cmd.exe"; | |
74 | break; | |
75 | case VER_PLATFORM_WIN32_WINDOWS: | |
76 | cmd = "command.com"; | |
77 | break; | |
78 | default: | |
79 | cmd = "command.com"; | |
80 | break; | |
81 | } | |
82 | } | |
83 | ||
84 | static void | |
85 | run (const char *sh, const char *args, const char *file) | |
86 | { | |
87 | BOOL b; | |
88 | char cmdline[_MAX_PATH]; | |
89 | STARTUPINFO si; | |
90 | PROCESS_INFORMATION pi; | |
91 | ||
92 | sprintf (cmdline, "%s %s %s", sh, args, file); | |
93 | memset (&pi, 0, sizeof (pi)); | |
94 | memset (&si, 0, sizeof (si)); | |
95 | si.cb = sizeof (si); | |
96 | si.lpTitle = (char *) "Cygwin Setup Post-Install Script"; | |
97 | si.dwFlags = STARTF_USEPOSITION; | |
98 | ||
99 | b = CreateProcess (0, cmdline, 0, 0, 0, | |
3c054baf | 100 | CREATE_NEW_CONSOLE, 0, get_root_dir ().cstr_oneuse(), &si, &pi); |
bc78a6d5 RC |
101 | |
102 | if (b) | |
103 | WaitForSingleObject (pi.hProcess, INFINITE); | |
104 | } | |
105 | ||
106 | void | |
3c054baf | 107 | run_script (String const &dir, String const &fname) |
bc78a6d5 | 108 | { |
3c054baf | 109 | char *ext = strrchr (fname.cstr_oneuse(), '.'); |
bc78a6d5 RC |
110 | if (!ext) |
111 | return; | |
112 | ||
3c054baf | 113 | if (sh.size() && strcmp (ext, ".sh") == 0) |
bc78a6d5 | 114 | { |
3c054baf RC |
115 | char *f2 = concat (dir.cstr_oneuse(), fname.cstr_oneuse(), 0); |
116 | log (LOG_TIMESTAMP, "running: %s -c %s", sh.cstr_oneuse(), f2); | |
117 | run (sh.cstr_oneuse(), "-c", f2); | |
5e0464a1 | 118 | delete[] f2; |
bc78a6d5 RC |
119 | } |
120 | else if (cmd && strcmp (ext, ".bat") == 0) | |
121 | { | |
3c054baf RC |
122 | char *f2 = backslash (cygpath (dir.cstr_oneuse(), fname.cstr_oneuse(),0)).cstr(); |
123 | log (LOG_TIMESTAMP, "running: %s /c %s", cmd, f2); | |
bc78a6d5 | 124 | run (cmd, "/c", f2); |
5e0464a1 | 125 | delete[] f2; |
bc78a6d5 RC |
126 | } |
127 | else | |
128 | return; | |
129 | ||
130 | /* if file exists then delete it otherwise just ignore no file error */ | |
3c054baf | 131 | io_stream::remove (String ("cygfile://") + dir+ fname+ ".done"); |
bc78a6d5 | 132 | |
3c054baf RC |
133 | io_stream::move (String ("cygfile://")+ dir+ fname, |
134 | String ("cygfile://")+ dir+ fname+ ".done"); | |
bc78a6d5 RC |
135 | } |
136 | ||
137 | void | |
3c054baf | 138 | try_run_script (String const &dir, String const &fname) |
bc78a6d5 | 139 | { |
3c054baf RC |
140 | if (io_stream::exists (String ("cygfile://")+ dir+ fname+ ".sh")) |
141 | run_script (dir.cstr_oneuse(), concat (fname.cstr_oneuse(), ".sh", 0)); | |
142 | if (io_stream::exists (String ("cygfile://")+ dir+ fname+ ".bat")) | |
143 | run_script (dir.cstr_oneuse(), concat (fname.cstr_oneuse(), ".bat", 0)); | |
bc78a6d5 RC |
144 | } |
145 |