]> cygwin.com Git - cygwin-apps/setup.git/blame - ini.cc
2002-02-19 Robert Collins <rbtcollins@hotmail.com>
[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
RC
49#include "threebar.h"
50extern ThreeBarProgressPage Progress;
51
23c9e63c 52unsigned int setup_timestamp = 0;
13d27274 53char *setup_version = 0;
23c9e63c 54
0af2d779 55extern int yyparse ();
b11b49f3
DD
56/*extern int yydebug;*/
57
58static char *error_buf = 0;
59static int error_count = 0;
23c9e63c 60
de6a1a64 61static int local_ini;
23c9e63c 62
de6a1a64
RC
63static void
64find_routine (char *path, unsigned int fsize)
65{
858f100d 66 if (!strstr (path, "setup.ini") )
de6a1a64 67 return;
1ac649ed
RC
68 io_stream *ini_file = io_stream::open (String ("file://") + local_dir + "/" +
69 path, "rb");
23c9e63c 70 if (!ini_file)
b5b282c4 71 {
1ac649ed
RC
72 note (NULL, IDS_SETUPINI_MISSING, (String ("file://") + local_dir + "/" +
73 path).cstr_oneuse());
de6a1a64 74 return;
b5b282c4 75 }
3c054baf
RC
76 else
77 log (LOG_BABBLE, String ("Found ini file - file://") + local_dir + "/" + path);
23c9e63c 78
de6a1a64 79 /* FIXME: only use most recent copy */
13d27274
DD
80 setup_timestamp = 0;
81 setup_version = 0;
82
3c054baf 83 ini_init (ini_file, String ("file://") + local_dir + "/" + path);
23c9e63c 84
b24c88b3 85 /*yydebug = 1; */
b11b49f3
DD
86
87 if (yyparse () || error_count > 0)
341988b9 88 MessageBox (0, error_buf, error_count == 1 ? "Parse Error" : "Parse Errors", 0);
dd3f7f9b 89 else
de6a1a64
RC
90 local_ini++;
91}
92
93static int
ab57ceaa 94do_local_ini (HWND owner)
de6a1a64
RC
95{
96 local_ini = 0;
97 find (local_dir, find_routine);
98 return local_ini;
99}
100
101static int
ab57ceaa 102do_remote_ini (HWND owner)
de6a1a64
RC
103{
104 size_t ini_count = 0;
ab57ceaa 105
de6a1a64 106 for (size_t n = 1; n <= site_list.number (); n++)
dd3f7f9b 107 {
de6a1a64 108 io_stream *ini_file =
3c054baf 109 get_url_to_membuf (site_list[n]->url + "/setup.ini", owner);
de6a1a64
RC
110
111 if (!ini_file)
112 {
3c054baf 113 note (owner, IDS_SETUPINI_MISSING, site_list[n]->url.cstr_oneuse());
de6a1a64
RC
114 continue;
115 }
116
117 /* FIXME: only use most recent copy */
118 setup_timestamp = 0;
119 setup_version = 0;
120
121 ini_init (ini_file, site_list[n]->url);
122
123 /*yydebug = 1; */
124
125 if (yyparse () || error_count > 0)
126 MessageBox (0, error_buf,
127 error_count == 1 ? "Parse Error" : "Parse Errors", 0);
128 else
129 {
130 /* save known-good setup.ini locally */
3c054baf
RC
131 String const fp = String ("file://") + local_dir + "/" +
132 rfc1738_escape_part (site_list[n]->url) +
133 "/setup.ini";
de6a1a64
RC
134 io_stream::mkpath_p (PATH_TO_FILE, fp);
135 io_stream *inistream = io_stream::open (fp, "wb");
136 if (inistream && !ini_file->seek (0, IO_SEEK_SET))
137 {
138 if (io_stream::copy (ini_file, inistream))
3c054baf 139 io_stream::remove (fp.cstr_oneuse());
de6a1a64
RC
140 delete ini_file;
141 delete inistream;
142 }
5e0464a1 143 ++ini_count;
dd3f7f9b
DD
144 }
145 }
de6a1a64
RC
146 return ini_count;
147}
148
ab57ceaa
RC
149static void
150do_ini_thread (HINSTANCE h, HWND owner)
de6a1a64
RC
151{
152 size_t ini_count = 0;
153 if (source == IDC_SOURCE_CWD)
ab57ceaa 154 ini_count = do_local_ini (owner);
de6a1a64 155 else
ab57ceaa 156 ini_count = do_remote_ini (owner);
de6a1a64
RC
157
158 if (ini_count == 0)
159 {
160 next_dialog = source == IDC_SOURCE_CWD ? IDD_S_FROM_CWD : IDD_SITE;
161 return;
162 }
23c9e63c 163
3c054baf 164 if (get_root_dir ().cstr_oneuse())
23c9e63c 165 {
b24c88b3 166 io_stream::mkpath_p (PATH_TO_DIR, "cygfile:///etc/setup");
04d6e06b
DD
167
168 unsigned int old_timestamp = 0;
b24c88b3
RC
169 io_stream *ots =
170 io_stream::open ("cygfile:///etc/setup/timestamp", "rt");
04d6e06b 171 if (ots)
23c9e63c 172 {
b24c88b3
RC
173 char temp[20];
174 memset (temp, '\0', 20);
175 if (ots->read (temp, 19))
176 sscanf (temp, "%u", &old_timestamp);
177 delete ots;
04d6e06b
DD
178 if (old_timestamp && setup_timestamp
179 && (old_timestamp > setup_timestamp))
180 {
ab57ceaa 181 int yn = yesno (owner, IDS_OLD_SETUPINI);
04d6e06b 182 if (yn == IDNO)
89b1a15b 183 exit_setup (1);
04d6e06b 184 }
23c9e63c 185 }
04d6e06b 186 if (setup_timestamp)
23c9e63c 187 {
b24c88b3
RC
188 io_stream *nts =
189 io_stream::open ("cygfile:///etc/setup/timestamp", "wt");
04d6e06b
DD
190 if (nts)
191 {
b24c88b3
RC
192 char temp[20];
193 sprintf (temp, "%u", setup_timestamp);
194 nts->write (temp, strlen (temp));
195 delete nts;
04d6e06b 196 }
23c9e63c
DD
197 }
198 }
199
b24c88b3
RC
200 msg ("setup_version is %s, our_version is %s", setup_version ? : "(null)",
201 version);
13d27274
DD
202 if (setup_version)
203 {
3c054baf
RC
204 String ini_version = canonicalize_version (setup_version);
205 String our_version = canonicalize_version (version);
206 // XXX useversion < operator
207 if (our_version.compare (ini_version) < 0)
ab57ceaa 208 note (owner, IDS_OLD_SETUP_VERSION, version, setup_version);
13d27274
DD
209 }
210
ab57ceaa 211 next_dialog = IDD_CHOOSER;
23c9e63c
DD
212}
213
ab57ceaa
RC
214static void
215do_ini_thread_reflector(void* p)
216{
217 HANDLE *context;
218 context = (HANDLE*)p;
219
220 do_ini_thread((HINSTANCE)context[0], (HWND)context[1]);
221
222 // Tell the progress page that we're done downloading
223 Progress.PostMessage(WM_APP_SETUP_INI_DOWNLOAD_COMPLETE, 0, next_dialog);
224
225 _endthread();
226}
227
228static HANDLE context[2];
229
230void
231do_ini (HINSTANCE h, HWND owner)
232{
233 context[0] = h;
234 context[1] = owner;
235
236 _beginthread(do_ini_thread_reflector, 0, context);
237}
238
239
b11b49f3
DD
240extern int yylineno;
241
b24c88b3
RC
242extern "C" int
243yyerror (char *s, ...)
23c9e63c 244{
b11b49f3
DD
245 char buf[1000];
246 int len;
247 sprintf (buf, "setup.ini line %d: ", yylineno);
248 va_list args;
249 va_start (args, s);
250 vsprintf (buf + strlen (buf), s, args);
251 OutputDebugString (buf);
252 if (error_buf)
253 {
254 strcat (error_buf, "\n");
255 len = strlen (error_buf) + strlen (buf) + 5;
256 error_buf = (char *) realloc (error_buf, len);
257 strcat (error_buf, buf);
258 }
259 else
260 {
261 len = strlen (buf) + 5;
262 error_buf = (char *) malloc (len);
263 strcpy (error_buf, buf);
264 }
265 error_count++;
b24c88b3
RC
266 /* TODO: is return 0 correct? */
267 return 0;
23c9e63c
DD
268}
269
b24c88b3 270extern "C" int fprintf (FILE * f, const char *s, ...);
23c9e63c 271
b11b49f3
DD
272static char stderrbuf[1000];
273
23c9e63c 274int
b24c88b3 275fprintf (FILE * f, const char *fmt, ...)
23c9e63c
DD
276{
277 char buf[1000];
278 int rv;
279 va_list args;
280 va_start (args, fmt);
281 if (f == stderr)
282 {
283 rv = vsprintf (buf, fmt, args);
b11b49f3
DD
284 strcat (stderrbuf, buf);
285 if (char *nl = strchr (stderrbuf, '\n'))
286 {
287 *nl = 0;
b24c88b3 288 /*OutputDebugString (stderrbuf); */
b11b49f3
DD
289 MessageBox (0, buf, "Cygwin Setup", 0);
290 stderrbuf[0] = 0;
291 }
42bf5b92 292
23c9e63c
DD
293 }
294 else
295 {
296 rv = vfprintf (f, fmt, args);
297 }
298 return rv;
299}
This page took 0.062965 seconds and 5 git commands to generate.