]> cygwin.com Git - cygwin-apps/setup.git/blame - script.cc
2003-03-16 Max Bowsher <maxb@ukf.net>
[cygwin-apps/setup.git] / script.cc
CommitLineData
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
21static 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"
33
3c054baf 34static String sh = String();
bc78a6d5
RC
35static const char *cmd = 0;
36static OSVERSIONINFO verinfo;
37
38static 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
46void
47init_run_script ()
48{
49 for (int i = 0; shells[i]; i++)
50 {
1ac649ed 51 sh = backslash (cygpath (shells[i]));
3c054baf 52 if (_access (sh.cstr_oneuse(), 0) == 0)
bc78a6d5 53 break;
3c054baf 54 sh = String();
bc78a6d5
RC
55 }
56
57 char old_path[_MAX_PATH];
58 GetEnvironmentVariable ("PATH", old_path, sizeof (old_path));
1ac649ed
RC
59 SetEnvironmentVariable ("PATH", backslash (cygpath ("/bin") + ";" +
60 cygpath ("/usr/bin") + ";" +
61 old_path).cstr_oneuse());
3c054baf 62 SetEnvironmentVariable ("CYGWINROOT", get_root_dir ().cstr_oneuse());
bc78a6d5
RC
63
64 verinfo.dwOSVersionInfoSize = sizeof (verinfo);
65 GetVersionEx (&verinfo);
66
67 switch (verinfo.dwPlatformId)
68 {
69 case VER_PLATFORM_WIN32_NT:
70 cmd = "cmd.exe";
71 break;
72 case VER_PLATFORM_WIN32_WINDOWS:
73 cmd = "command.com";
74 break;
75 default:
76 cmd = "command.com";
77 break;
78 }
79}
80
81static void
82run (const char *sh, const char *args, const char *file)
83{
bc78a6d5
RC
84 char cmdline[_MAX_PATH];
85 STARTUPINFO si;
86 PROCESS_INFORMATION pi;
87
88 sprintf (cmdline, "%s %s %s", sh, args, file);
89 memset (&pi, 0, sizeof (pi));
90 memset (&si, 0, sizeof (si));
91 si.cb = sizeof (si);
92 si.lpTitle = (char *) "Cygwin Setup Post-Install Script";
93 si.dwFlags = STARTF_USEPOSITION;
94
92f9402a 95 BOOL createSucceeded = CreateProcess (0, cmdline, 0, 0, 0,
3c054baf 96 CREATE_NEW_CONSOLE, 0, get_root_dir ().cstr_oneuse(), &si, &pi);
bc78a6d5 97
92f9402a 98 if (createSucceeded)
bc78a6d5 99 WaitForSingleObject (pi.hProcess, INFINITE);
92f9402a
RC
100 CloseHandle(pi.hProcess);
101 CloseHandle(pi.hThread);
bc78a6d5
RC
102}
103
104void
3c054baf 105run_script (String const &dir, String const &fname)
bc78a6d5 106{
3c054baf 107 char *ext = strrchr (fname.cstr_oneuse(), '.');
bc78a6d5
RC
108 if (!ext)
109 return;
110
3c054baf 111 if (sh.size() && strcmp (ext, ".sh") == 0)
bc78a6d5 112 {
1ac649ed
RC
113 String f2 = dir + fname;
114 log (LOG_PLAIN, String ("running: ") + sh + " -c " + f2);
115 run (sh.cstr_oneuse(), "-c", f2.cstr_oneuse());
bc78a6d5
RC
116 }
117 else if (cmd && strcmp (ext, ".bat") == 0)
118 {
1ac649ed
RC
119 String f2 = backslash (cygpath (dir + fname));
120 log (LOG_PLAIN, String ("running: ") + cmd + " /c " + f2);
121 run (cmd, "/c", f2.cstr_oneuse());
bc78a6d5
RC
122 }
123 else
124 return;
125
126 /* if file exists then delete it otherwise just ignore no file error */
1ac649ed 127 io_stream::remove (String ("cygfile://") + dir + fname + ".done");
bc78a6d5 128
1ac649ed
RC
129 io_stream::move (String ("cygfile://") + dir + fname,
130 String ("cygfile://") + dir + fname+ ".done");
bc78a6d5
RC
131}
132
133void
3c054baf 134try_run_script (String const &dir, String const &fname)
bc78a6d5 135{
1ac649ed
RC
136 if (io_stream::exists (String ("cygfile://")+ dir + fname + ".sh"))
137 run_script (dir.cstr_oneuse(), (fname + ".sh").cstr_oneuse());
138 if (io_stream::exists (String ("cygfile://")+ dir + fname + ".bat"))
139 run_script (dir.cstr_oneuse(), (fname + ".bat").cstr_oneuse());
bc78a6d5
RC
140}
141
This page took 0.042942 seconds and 5 git commands to generate.