]>
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" | |
3c054baf | 30 | #include "filemanip.h" |
bc78a6d5 RC |
31 | #include "mount.h" |
32 | #include "io_stream.h" | |
ad646f43 | 33 | #include "script.h" |
bc78a6d5 | 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 | { | |
1ac649ed | 52 | sh = backslash (cygpath (shells[i])); |
3c054baf | 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)); | |
1ac649ed RC |
60 | SetEnvironmentVariable ("PATH", backslash (cygpath ("/bin") + ";" + |
61 | cygpath ("/usr/bin") + ";" + | |
62 | old_path).cstr_oneuse()); | |
3c054baf | 63 | SetEnvironmentVariable ("CYGWINROOT", get_root_dir ().cstr_oneuse()); |
bc78a6d5 RC |
64 | |
65 | verinfo.dwOSVersionInfoSize = sizeof (verinfo); | |
66 | GetVersionEx (&verinfo); | |
67 | ||
68 | switch (verinfo.dwPlatformId) | |
69 | { | |
70 | case VER_PLATFORM_WIN32_NT: | |
71 | cmd = "cmd.exe"; | |
72 | break; | |
73 | case VER_PLATFORM_WIN32_WINDOWS: | |
74 | cmd = "command.com"; | |
75 | break; | |
76 | default: | |
77 | cmd = "command.com"; | |
78 | break; | |
79 | } | |
80 | } | |
81 | ||
82 | static void | |
83 | run (const char *sh, const char *args, const char *file) | |
84 | { | |
bc78a6d5 RC |
85 | char cmdline[_MAX_PATH]; |
86 | STARTUPINFO si; | |
87 | PROCESS_INFORMATION pi; | |
88 | ||
89 | sprintf (cmdline, "%s %s %s", sh, args, file); | |
90 | memset (&pi, 0, sizeof (pi)); | |
91 | memset (&si, 0, sizeof (si)); | |
92 | si.cb = sizeof (si); | |
93 | si.lpTitle = (char *) "Cygwin Setup Post-Install Script"; | |
94 | si.dwFlags = STARTF_USEPOSITION; | |
95 | ||
92f9402a | 96 | BOOL createSucceeded = CreateProcess (0, cmdline, 0, 0, 0, |
3c054baf | 97 | CREATE_NEW_CONSOLE, 0, get_root_dir ().cstr_oneuse(), &si, &pi); |
bc78a6d5 | 98 | |
92f9402a | 99 | if (createSucceeded) |
bc78a6d5 | 100 | WaitForSingleObject (pi.hProcess, INFINITE); |
92f9402a RC |
101 | CloseHandle(pi.hProcess); |
102 | CloseHandle(pi.hThread); | |
bc78a6d5 RC |
103 | } |
104 | ||
105 | void | |
3c054baf | 106 | run_script (String const &dir, String const &fname) |
bc78a6d5 | 107 | { |
3c054baf | 108 | char *ext = strrchr (fname.cstr_oneuse(), '.'); |
bc78a6d5 RC |
109 | if (!ext) |
110 | return; | |
111 | ||
3c054baf | 112 | if (sh.size() && strcmp (ext, ".sh") == 0) |
bc78a6d5 | 113 | { |
1ac649ed RC |
114 | String f2 = dir + fname; |
115 | log (LOG_PLAIN, String ("running: ") + sh + " -c " + f2); | |
116 | run (sh.cstr_oneuse(), "-c", f2.cstr_oneuse()); | |
bc78a6d5 RC |
117 | } |
118 | else if (cmd && strcmp (ext, ".bat") == 0) | |
119 | { | |
1ac649ed RC |
120 | String f2 = backslash (cygpath (dir + fname)); |
121 | log (LOG_PLAIN, String ("running: ") + cmd + " /c " + f2); | |
122 | run (cmd, "/c", f2.cstr_oneuse()); | |
bc78a6d5 RC |
123 | } |
124 | else | |
125 | return; | |
126 | ||
127 | /* if file exists then delete it otherwise just ignore no file error */ | |
1ac649ed | 128 | io_stream::remove (String ("cygfile://") + dir + fname + ".done"); |
bc78a6d5 | 129 | |
1ac649ed RC |
130 | io_stream::move (String ("cygfile://") + dir + fname, |
131 | String ("cygfile://") + dir + fname+ ".done"); | |
bc78a6d5 RC |
132 | } |
133 | ||
134 | void | |
3c054baf | 135 | try_run_script (String const &dir, String const &fname) |
bc78a6d5 | 136 | { |
1ac649ed RC |
137 | if (io_stream::exists (String ("cygfile://")+ dir + fname + ".sh")) |
138 | run_script (dir.cstr_oneuse(), (fname + ".sh").cstr_oneuse()); | |
139 | if (io_stream::exists (String ("cygfile://")+ dir + fname + ".bat")) | |
140 | run_script (dir.cstr_oneuse(), (fname + ".bat").cstr_oneuse()); | |
bc78a6d5 RC |
141 | } |
142 | ||
ad646f43 RC |
143 | bool |
144 | Script::isAScript (String const &file) | |
145 | { | |
146 | /* file may be /etc/postinstall or etc/postinstall */ | |
147 | if (file.casecompare ("/etc/postinstall/", 17) && file.casecompare ("etc/postinstall/", 16)) | |
148 | return false; | |
149 | if (file.cstr_oneuse()[file.size() - 1] == '/') | |
150 | return false; | |
151 | return true; | |
152 | } | |
153 | ||
154 | Script::Script (String const &fileName) : scriptName (fileName) | |
155 | { | |
156 | ||
157 | } | |
158 | ||
159 | String | |
160 | Script::baseName()const | |
161 | { | |
162 | String result = scriptName; | |
163 | while (result.find ('/')) | |
164 | result = result.substr(result.find ('/')); | |
165 | return result; | |
166 | } |