]>
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" | |
31 | #include "mount.h" | |
32 | #include "io_stream.h" | |
33 | ||
34 | static char *sh = 0; | |
35 | static const char *cmd = 0; | |
36 | static OSVERSIONINFO verinfo; | |
37 | ||
38 | static const char *shells[] = { | |
39 | "/bin/sh.exe", | |
40 | "/usr/bin/sh.exe", | |
41 | "/bin/bash.exe", | |
42 | "/usr/bin/bash.exe", | |
43 | 0 | |
44 | }; | |
45 | ||
46 | void | |
47 | init_run_script () | |
48 | { | |
49 | for (int i = 0; shells[i]; i++) | |
50 | { | |
51 | sh = backslash (cygpath (shells[i], 0)); | |
52 | if (_access (sh, 0) == 0) | |
53 | break; | |
54 | free (sh); | |
55 | sh = 0; | |
56 | } | |
57 | ||
58 | char old_path[_MAX_PATH]; | |
59 | GetEnvironmentVariable ("PATH", old_path, sizeof (old_path)); | |
60 | SetEnvironmentVariable ("PATH", | |
61 | backslash (cygpath ("/bin;", | |
62 | get_root_dir (), "/usr/bin;", | |
63 | old_path, 0))); | |
64 | ||
65 | SetEnvironmentVariable ("CYGWINROOT", get_root_dir ()); | |
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, | |
100 | CREATE_NEW_CONSOLE, 0, get_root_dir (), &si, &pi); | |
101 | ||
102 | if (b) | |
103 | WaitForSingleObject (pi.hProcess, INFINITE); | |
104 | } | |
105 | ||
106 | void | |
107 | run_script (char const *dir, char const *fname) | |
108 | { | |
109 | char *ext = strrchr (fname, '.'); | |
110 | if (!ext) | |
111 | return; | |
112 | ||
113 | if (sh && strcmp (ext, ".sh") == 0) | |
114 | { | |
115 | char *f2 = concat (dir, fname, 0); | |
116 | log (0, "running: %s -c %s", sh, f2); | |
117 | run (sh, "-c", f2); | |
118 | free (f2); | |
119 | } | |
120 | else if (cmd && strcmp (ext, ".bat") == 0) | |
121 | { | |
122 | char *f2 = backslash (cygpath (dir, fname, 0)); | |
123 | log (0, "running: %s /c %s", cmd, f2); | |
124 | run (cmd, "/c", f2); | |
125 | free (f2); | |
126 | } | |
127 | else | |
128 | return; | |
129 | ||
130 | /* if file exists then delete it otherwise just ignore no file error */ | |
131 | io_stream::remove (concat ("cygfile://", dir, fname, ".done", 0)); | |
132 | ||
133 | io_stream::move (concat ("cygfile://", dir, fname, 0), | |
134 | concat ("cygfile://", dir, fname, ".done", 0)); | |
135 | } | |
136 | ||
137 | void | |
138 | try_run_script (char const *dir, char const *fname) | |
139 | { | |
140 | if (io_stream::exists (concat ("cygfile://", dir, fname, ".sh", 0))) | |
141 | run_script (dir, concat (fname, ".sh", 0)); | |
142 | if (io_stream::exists (concat ("cygfile://", dir, fname, ".bat", 0))) | |
143 | run_script (dir, concat (fname, ".bat", 0)); | |
144 | } | |
145 |