]> cygwin.com Git - cygwin-apps/setup.git/blob - script.cc
2002-02-19 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / script.cc
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 "filemanip.h"
32 #include "mount.h"
33 #include "io_stream.h"
34
35 static String sh = String();
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 {
52 sh = backslash (cygpath (shells[i],0).cstr_oneuse());
53 if (_access (sh.cstr_oneuse(), 0) == 0)
54 break;
55 sh = String();
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 ().cstr_oneuse(), "/usr/bin;",
63 old_path, 0)).cstr_oneuse());
64
65 SetEnvironmentVariable ("CYGWINROOT", get_root_dir ().cstr_oneuse());
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 ().cstr_oneuse(), &si, &pi);
101
102 if (b)
103 WaitForSingleObject (pi.hProcess, INFINITE);
104 }
105
106 void
107 run_script (String const &dir, String const &fname)
108 {
109 char *ext = strrchr (fname.cstr_oneuse(), '.');
110 if (!ext)
111 return;
112
113 if (sh.size() && strcmp (ext, ".sh") == 0)
114 {
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);
118 delete[] f2;
119 }
120 else if (cmd && strcmp (ext, ".bat") == 0)
121 {
122 char *f2 = backslash (cygpath (dir.cstr_oneuse(), fname.cstr_oneuse(),0)).cstr();
123 log (LOG_TIMESTAMP, "running: %s /c %s", cmd, f2);
124 run (cmd, "/c", f2);
125 delete[] f2;
126 }
127 else
128 return;
129
130 /* if file exists then delete it otherwise just ignore no file error */
131 io_stream::remove (String ("cygfile://") + dir+ fname+ ".done");
132
133 io_stream::move (String ("cygfile://")+ dir+ fname,
134 String ("cygfile://")+ dir+ fname+ ".done");
135 }
136
137 void
138 try_run_script (String const &dir, String const &fname)
139 {
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));
144 }
145
This page took 0.042811 seconds and 5 git commands to generate.