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
=
31 #include "filemanip.h"
33 #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
],0).cstr_oneuse());
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",
61 backslash (cygpath ("/bin;",
62 get_root_dir ().cstr_oneuse(), "/usr/bin;",
63 old_path
, 0)).cstr_oneuse());
65 SetEnvironmentVariable ("CYGWINROOT", get_root_dir ().cstr_oneuse());
67 verinfo
.dwOSVersionInfoSize
= sizeof (verinfo
);
68 GetVersionEx (&verinfo
);
70 switch (verinfo
.dwPlatformId
)
72 case VER_PLATFORM_WIN32_NT
:
75 case VER_PLATFORM_WIN32_WINDOWS
:
85 run (const char *sh
, const char *args
, const char *file
)
88 char cmdline
[_MAX_PATH
];
90 PROCESS_INFORMATION pi
;
92 sprintf (cmdline
, "%s %s %s", sh
, args
, file
);
93 memset (&pi
, 0, sizeof (pi
));
94 memset (&si
, 0, sizeof (si
));
96 si
.lpTitle
= (char *) "Cygwin Setup Post-Install Script";
97 si
.dwFlags
= STARTF_USEPOSITION
;
99 b
= CreateProcess (0, cmdline
, 0, 0, 0,
100 CREATE_NEW_CONSOLE
, 0, get_root_dir ().cstr_oneuse(), &si
, &pi
);
103 WaitForSingleObject (pi
.hProcess
, INFINITE
);
107 run_script (String
const &dir
, String
const &fname
)
109 char *ext
= strrchr (fname
.cstr_oneuse(), '.');
113 if (sh
.size() && strcmp (ext
, ".sh") == 0)
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
);
120 else if (cmd
&& strcmp (ext
, ".bat") == 0)
122 char *f2
= backslash (cygpath (dir
.cstr_oneuse(), fname
.cstr_oneuse(),0)).cstr();
123 log (LOG_TIMESTAMP
, "running: %s /c %s", cmd
, f2
);
130 /* if file exists then delete it otherwise just ignore no file error */
131 io_stream::remove (String ("cygfile://") + dir
+ fname
+ ".done");
133 io_stream::move (String ("cygfile://")+ dir
+ fname
,
134 String ("cygfile://")+ dir
+ fname
+ ".done");
138 try_run_script (String
const &dir
, String
const &fname
)
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));