]> cygwin.com Git - cygwin-apps/setup.git/blame - ini.cc
2002-04-02 Gary R. Van Sickle <g.r.vansickle@worldnet.att.net>
[cygwin-apps/setup.git] / ini.cc
CommitLineData
23c9e63c
DD
1/*
2 * Copyright (c) 2000, Red Hat, Inc.
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 *
14 */
15
16/* The purpose of this file is to get and parse the setup.ini file
17 from the mirror site. A few support routines for the bison and
18 flex parsers are provided also. We check to see if this setup.ini
19 is older than the one we used last time, and if so, warn the user. */
20
b24c88b3
RC
21#if 0
22static const char *cvsid =
23 "\n%%% $Id$\n";
24#endif
8507f105 25
23c9e63c
DD
26#include "win32.h"
27
28#include <stdio.h>
b11b49f3 29#include <stdlib.h>
23c9e63c 30#include <stdarg.h>
ab57ceaa 31#include <process.h>
23c9e63c
DD
32
33#include "ini.h"
34#include "resource.h"
3c054baf 35#include "String++.h"
23c9e63c
DD
36#include "state.h"
37#include "geturl.h"
38#include "dialog.h"
39#include "msg.h"
89b1a15b 40#include "log.h"
13d27274 41#include "version.h"
a351e48c 42#include "mount.h"
de6a1a64
RC
43#include "site.h"
44#include "rfc1738.h"
45#include "find.h"
23c9e63c 46
b24c88b3
RC
47#include "io_stream.h"
48
ab57ceaa 49#include "threebar.h"
58ee6135
RC
50
51#include "rfc1738.h"
b92028a4 52
076654e7 53#include "IniDBBuilderPackage.h"
3272d625 54#include "compress.h"
b92028a4 55
ab57ceaa
RC
56extern ThreeBarProgressPage Progress;
57
23c9e63c 58unsigned int setup_timestamp = 0;
b92028a4 59String setup_version;
23c9e63c 60
0af2d779 61extern int yyparse ();
b11b49f3
DD
62/*extern int yydebug;*/
63
64static char *error_buf = 0;
65static int error_count = 0;
23c9e63c 66
de6a1a64 67static int local_ini;
23c9e63c 68
076654e7
RC
69static IniDBBuilder *findBuilder;
70
de6a1a64
RC
71static void
72find_routine (char *path, unsigned int fsize)
73{
858f100d 74 if (!strstr (path, "setup.ini") )
de6a1a64 75 return;
1ac649ed
RC
76 io_stream *ini_file = io_stream::open (String ("file://") + local_dir + "/" +
77 path, "rb");
23c9e63c 78 if (!ini_file)
b5b282c4 79 {
1ac649ed
RC
80 note (NULL, IDS_SETUPINI_MISSING, (String ("file://") + local_dir + "/" +
81 path).cstr_oneuse());
de6a1a64 82 return;
b5b282c4 83 }
3c054baf
RC
84 else
85 log (LOG_BABBLE, String ("Found ini file - file://") + local_dir + "/" + path);
23c9e63c 86
58ee6135
RC
87 /* Attempt to unescape the string */
88 path[strlen(path) -10] = '\0';
89 String mirror = rfc1738_unescape_part (path);
076654e7
RC
90 findBuilder->parse_mirror = mirror;
91 ini_init (ini_file, findBuilder);
23c9e63c 92
b24c88b3 93 /*yydebug = 1; */
b11b49f3
DD
94
95 if (yyparse () || error_count > 0)
341988b9 96 MessageBox (0, error_buf, error_count == 1 ? "Parse Error" : "Parse Errors", 0);
dd3f7f9b 97 else
de6a1a64 98 local_ini++;
076654e7 99 if (findBuilder->timestamp > setup_timestamp)
b92028a4 100 {
076654e7
RC
101 setup_timestamp = findBuilder->timestamp;
102 setup_version = findBuilder->version;
b92028a4 103 }
de6a1a64
RC
104}
105
106static int
ab57ceaa 107do_local_ini (HWND owner)
de6a1a64
RC
108{
109 local_ini = 0;
076654e7 110 findBuilder = new IniDBBuilderPackage;
de6a1a64 111 find (local_dir, find_routine);
076654e7
RC
112 delete findBuilder;
113 findBuilder = 0;
de6a1a64
RC
114 return local_ini;
115}
116
117static int
ab57ceaa 118do_remote_ini (HWND owner)
de6a1a64
RC
119{
120 size_t ini_count = 0;
076654e7 121 IniDBBuilderPackage *aBuilder = new IniDBBuilderPackage;
ab57ceaa 122
de6a1a64 123 for (size_t n = 1; n <= site_list.number (); n++)
dd3f7f9b 124 {
3272d625
RC
125 io_stream *compressed_ini_file =
126 get_url_to_membuf (site_list[n]->url + "/setup.bz2", owner);
127 io_stream *ini_file = 0;
128 if (!compressed_ini_file)
129 ini_file = get_url_to_membuf (site_list[n]->url + "/setup.ini", owner);
130 else
131 {
132 ini_file = compress::decompress (compressed_ini_file);
133 }
de6a1a64
RC
134
135 if (!ini_file)
136 {
3c054baf 137 note (owner, IDS_SETUPINI_MISSING, site_list[n]->url.cstr_oneuse());
de6a1a64
RC
138 continue;
139 }
140
076654e7
RC
141 aBuilder->parse_mirror = site_list[n]->url;
142 ini_init (ini_file, aBuilder);
de6a1a64
RC
143
144 /*yydebug = 1; */
145
146 if (yyparse () || error_count > 0)
147 MessageBox (0, error_buf,
148 error_count == 1 ? "Parse Error" : "Parse Errors", 0);
149 else
150 {
151 /* save known-good setup.ini locally */
3c054baf
RC
152 String const fp = String ("file://") + local_dir + "/" +
153 rfc1738_escape_part (site_list[n]->url) +
154 "/setup.ini";
de6a1a64
RC
155 io_stream::mkpath_p (PATH_TO_FILE, fp);
156 io_stream *inistream = io_stream::open (fp, "wb");
157 if (inistream && !ini_file->seek (0, IO_SEEK_SET))
158 {
159 if (io_stream::copy (ini_file, inistream))
3c054baf 160 io_stream::remove (fp.cstr_oneuse());
de6a1a64
RC
161 delete inistream;
162 }
5e0464a1 163 ++ini_count;
dd3f7f9b 164 }
076654e7 165 if (aBuilder->timestamp > setup_timestamp)
b92028a4 166 {
076654e7
RC
167 setup_timestamp = aBuilder->timestamp;
168 setup_version = aBuilder->version;
b92028a4 169 }
3272d625
RC
170 delete ini_file;
171 delete compressed_ini_file;
dd3f7f9b 172 }
076654e7 173 delete aBuilder;
de6a1a64
RC
174 return ini_count;
175}
176
ab57ceaa
RC
177static void
178do_ini_thread (HINSTANCE h, HWND owner)
de6a1a64
RC
179{
180 size_t ini_count = 0;
181 if (source == IDC_SOURCE_CWD)
ab57ceaa 182 ini_count = do_local_ini (owner);
de6a1a64 183 else
ab57ceaa 184 ini_count = do_remote_ini (owner);
de6a1a64
RC
185
186 if (ini_count == 0)
187 {
188 next_dialog = source == IDC_SOURCE_CWD ? IDD_S_FROM_CWD : IDD_SITE;
189 return;
190 }
23c9e63c 191
3c054baf 192 if (get_root_dir ().cstr_oneuse())
23c9e63c 193 {
b24c88b3 194 io_stream::mkpath_p (PATH_TO_DIR, "cygfile:///etc/setup");
04d6e06b
DD
195
196 unsigned int old_timestamp = 0;
b24c88b3
RC
197 io_stream *ots =
198 io_stream::open ("cygfile:///etc/setup/timestamp", "rt");
04d6e06b 199 if (ots)
23c9e63c 200 {
b24c88b3
RC
201 char temp[20];
202 memset (temp, '\0', 20);
203 if (ots->read (temp, 19))
204 sscanf (temp, "%u", &old_timestamp);
205 delete ots;
04d6e06b
DD
206 if (old_timestamp && setup_timestamp
207 && (old_timestamp > setup_timestamp))
208 {
ab57ceaa 209 int yn = yesno (owner, IDS_OLD_SETUPINI);
04d6e06b 210 if (yn == IDNO)
9f4a0c62 211 LogSingleton::GetInstance().exit (1);
04d6e06b 212 }
23c9e63c 213 }
04d6e06b 214 if (setup_timestamp)
23c9e63c 215 {
b24c88b3
RC
216 io_stream *nts =
217 io_stream::open ("cygfile:///etc/setup/timestamp", "wt");
04d6e06b
DD
218 if (nts)
219 {
b24c88b3
RC
220 char temp[20];
221 sprintf (temp, "%u", setup_timestamp);
222 nts->write (temp, strlen (temp));
223 delete nts;
04d6e06b 224 }
23c9e63c
DD
225 }
226 }
227
b92028a4
RC
228 msg ("setup_version is %s, our_version is %s", setup_version.size() ?
229 setup_version.cstr_oneuse() : "(null)",
b24c88b3 230 version);
b92028a4 231 if (setup_version.size())
13d27274 232 {
3c054baf
RC
233 String ini_version = canonicalize_version (setup_version);
234 String our_version = canonicalize_version (version);
235 // XXX useversion < operator
236 if (our_version.compare (ini_version) < 0)
b92028a4 237 note (owner, IDS_OLD_SETUP_VERSION, version, setup_version.cstr_oneuse());
13d27274
DD
238 }
239
f6a81f69 240 next_dialog = IDD_CHOOSE;
23c9e63c
DD
241}
242
45e01f23 243static DWORD WINAPI
ab57ceaa
RC
244do_ini_thread_reflector(void* p)
245{
246 HANDLE *context;
247 context = (HANDLE*)p;
248
249 do_ini_thread((HINSTANCE)context[0], (HWND)context[1]);
250
251 // Tell the progress page that we're done downloading
252 Progress.PostMessage(WM_APP_SETUP_INI_DOWNLOAD_COMPLETE, 0, next_dialog);
253
45e01f23 254 ExitThread(0);
ab57ceaa
RC
255}
256
257static HANDLE context[2];
258
259void
260do_ini (HINSTANCE h, HWND owner)
261{
45e01f23
RC
262 context[0] = h;
263 context[1] = owner;
264
265 DWORD threadID;
266 CreateThread (NULL, 0, do_ini_thread_reflector, context, 0, &threadID);
ab57ceaa
RC
267}
268
269
b11b49f3
DD
270extern int yylineno;
271
6391823e
RC
272extern int
273yyerror (String const &s)
23c9e63c 274{
b11b49f3
DD
275 char buf[1000];
276 int len;
277 sprintf (buf, "setup.ini line %d: ", yylineno);
6391823e 278 sprintf (buf + strlen (buf), s.cstr_oneuse());
b11b49f3
DD
279 OutputDebugString (buf);
280 if (error_buf)
281 {
282 strcat (error_buf, "\n");
283 len = strlen (error_buf) + strlen (buf) + 5;
284 error_buf = (char *) realloc (error_buf, len);
285 strcat (error_buf, buf);
286 }
287 else
288 {
289 len = strlen (buf) + 5;
290 error_buf = (char *) malloc (len);
291 strcpy (error_buf, buf);
292 }
293 error_count++;
b24c88b3
RC
294 /* TODO: is return 0 correct? */
295 return 0;
23c9e63c
DD
296}
297
b24c88b3 298extern "C" int fprintf (FILE * f, const char *s, ...);
23c9e63c 299
b11b49f3
DD
300static char stderrbuf[1000];
301
23c9e63c 302int
b24c88b3 303fprintf (FILE * f, const char *fmt, ...)
23c9e63c
DD
304{
305 char buf[1000];
306 int rv;
307 va_list args;
308 va_start (args, fmt);
309 if (f == stderr)
310 {
311 rv = vsprintf (buf, fmt, args);
b11b49f3
DD
312 strcat (stderrbuf, buf);
313 if (char *nl = strchr (stderrbuf, '\n'))
314 {
315 *nl = 0;
b24c88b3 316 /*OutputDebugString (stderrbuf); */
b11b49f3
DD
317 MessageBox (0, buf, "Cygwin Setup", 0);
318 stderrbuf[0] = 0;
319 }
42bf5b92 320
23c9e63c
DD
321 }
322 else
323 {
324 rv = vfprintf (f, fmt, args);
325 }
326 return rv;
327}
This page took 0.070043 seconds and 5 git commands to generate.