2 * Copyright (c) 2001, Jan Nieuwenhuizen.
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.
9 * A copy of the GNU General Public License can be found at
12 * Written by DJ Delorie <dj@cygnus.com>
13 * Jan Nieuwenhuizen <janneke@gnu.org>
17 /* The purpose of this file is to provide functions for the invocation
18 of install scripts. */
21 static const char *cvsid
=
30 #include "filemanip.h"
32 #include "io_stream.h"
35 static String sh
= String();
36 static const char *cmd
= 0;
37 static OSVERSIONINFO verinfo
;
39 static const char *shells
[] = {
50 for (int i
= 0; shells
[i
]; i
++)
52 sh
= backslash (cygpath (shells
[i
]));
53 if (_access (sh
.cstr_oneuse(), 0) == 0)
58 char old_path
[_MAX_PATH
];
59 GetEnvironmentVariable ("PATH", old_path
, sizeof (old_path
));
60 SetEnvironmentVariable ("PATH", backslash (cygpath ("/bin") + ";" +
61 cygpath ("/usr/bin") + ";" +
62 old_path
).cstr_oneuse());
63 SetEnvironmentVariable ("CYGWINROOT", get_root_dir ().cstr_oneuse());
65 verinfo
.dwOSVersionInfoSize
= sizeof (verinfo
);
66 GetVersionEx (&verinfo
);
68 switch (verinfo
.dwPlatformId
)
70 case VER_PLATFORM_WIN32_NT
:
73 case VER_PLATFORM_WIN32_WINDOWS
:
83 run (const char *sh
, const char *args
, const char *file
)
85 char cmdline
[_MAX_PATH
];
87 PROCESS_INFORMATION pi
;
89 sprintf (cmdline
, "%s %s %s", sh
, args
, file
);
90 memset (&pi
, 0, sizeof (pi
));
91 memset (&si
, 0, sizeof (si
));
93 si
.lpTitle
= (char *) "Cygwin Setup Post-Install Script";
94 si
.dwFlags
= STARTF_USEPOSITION
;
96 BOOL createSucceeded
= CreateProcess (0, cmdline
, 0, 0, 0,
97 CREATE_NEW_CONSOLE
, 0, get_root_dir ().cstr_oneuse(), &si
, &pi
);
100 WaitForSingleObject (pi
.hProcess
, INFINITE
);
101 CloseHandle(pi
.hProcess
);
102 CloseHandle(pi
.hThread
);
106 run_script (String
const &dir
, String
const &fname
)
108 char *ext
= strrchr (fname
.cstr_oneuse(), '.');
112 if (sh
.size() && strcmp (ext
, ".sh") == 0)
114 String f2
= dir
+ fname
;
115 log (LOG_PLAIN
, String ("running: ") + sh
+ " -c " + f2
);
116 run (sh
.cstr_oneuse(), "-c", f2
.cstr_oneuse());
118 else if (cmd
&& strcmp (ext
, ".bat") == 0)
120 String f2
= backslash (cygpath (dir
+ fname
));
121 log (LOG_PLAIN
, String ("running: ") + cmd
+ " /c " + f2
);
122 run (cmd
, "/c", f2
.cstr_oneuse());
127 /* if file exists then delete it otherwise just ignore no file error */
128 io_stream::remove (String ("cygfile://") + dir
+ fname
+ ".done");
130 io_stream::move (String ("cygfile://") + dir
+ fname
,
131 String ("cygfile://") + dir
+ fname
+ ".done");
135 try_run_script (String
const &dir
, String
const &fname
)
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());
144 Script::isAScript (String
const &file
)
146 /* file may be /etc/postinstall or etc/postinstall */
147 if (file
.casecompare ("/etc/postinstall/", 17) && file
.casecompare ("etc/postinstall/", 16))
149 if (file
.cstr_oneuse()[file
.size() - 1] == '/')
154 Script::Script (String
const &fileName
) : scriptName (fileName
)
160 Script::baseName()const
162 String result
= scriptName
;
163 while (result
.find ('/'))
164 result
= result
.substr(result
.find ('/'));