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