2 * Copyright (c) 2000, Red Hat, Inc.
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.
9 * A copy of the GNU General Public License can be found at
12 * Written by DJ Delorie <dj@cygnus.com>
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. */
22 static const char *cvsid
=
46 #include "IniParseFindVisitor.h"
47 #include "IniParseFeedback.h"
48 //#include "filemanip.h"
50 #include "io_stream.h"
56 #include "IniDBBuilderPackage.h"
59 extern ThreeBarProgressPage Progress
;
61 unsigned int setup_timestamp
= 0;
64 extern int yyparse ();
65 /*extern int yydebug;*/
67 static char *error_buf
= 0;
68 static int error_count
= 0;
70 static const char *ini_filename
;
72 class GuiParseFeedback
: public IniParseFeedback
75 GuiParseFeedback () : lastpct (0)
77 Progress
.SetText2 ("");
78 Progress
.SetText3 ("");
79 Progress
.SetText4 ("Progress:");
81 virtual void progress(unsigned long const pos
, unsigned long const max
)
84 /* length not known or eof */
87 /* rounding down should mean this only ever fires once */
89 if (pos
* 100 / max
> lastpct
)
91 lastpct
= pos
* 100 / max
;
92 log (LOG_BABBLE
, String (lastpct
) + "% (" + String (pos
) + " of " + String (max
) + " bytes of ini file read");
94 Progress
.SetBar1(pos
, max
);
96 virtual void iniName (String
const &name
)
98 Progress
.SetText1 ((String ("Parsing ini file \"") + name
+ "\"").cstr_oneuse());
100 virtual void babble(String
const &message
)const
102 log (LOG_BABBLE
, message
);
104 virtual void warning (String
const &message
)const
106 MessageBox (0, message
.cstr_oneuse(), "Warning", 0);
108 virtual void error(String
const &message
)const
110 MessageBox (0, message
.cstr_oneuse(), "Error parsing", 0);
112 virtual ~ GuiParseFeedback ()
114 Progress
.SetText4("Package:");
117 unsigned char lastpct
;
121 do_local_ini (HWND owner
)
123 GuiParseFeedback myFeedback
;
124 IniDBBuilderPackage
findBuilder(myFeedback
);
125 IniParseFindVisitor
myVisitor (findBuilder
, local_dir
, myFeedback
);
126 Find (local_dir
).accept(myVisitor
);
127 setup_timestamp
= myVisitor
.timeStamp();
128 setup_version
= myVisitor
.version();
129 return myVisitor
.iniCount();
133 do_remote_ini (HWND owner
)
135 size_t ini_count
= 0;
136 GuiParseFeedback myFeedback
;
137 IniDBBuilderPackage
aBuilder(myFeedback
);
139 for (SiteList::const_iterator n
= site_list
.begin();
140 n
!= site_list
.end(); ++n
)
142 io_stream
*compressed_ini_file
=
143 get_url_to_membuf (n
->url
+ "/setup.bz2", owner
);
144 io_stream
*ini_file
= 0;
145 if (!compressed_ini_file
)
146 ini_file
= get_url_to_membuf (n
->url
+ "/setup.ini", owner
);
149 ini_file
= compress::decompress (compressed_ini_file
);
154 note (owner
, IDS_SETUPINI_MISSING
, n
->url
.cstr_oneuse());
158 if (compressed_ini_file
)
159 myFeedback
.iniName (n
->url
+ "/setup.bz2");
161 myFeedback
.iniName (n
->url
+ "/setup.ini");
163 aBuilder
.parse_mirror
= n
->url
;
164 ini_init (ini_file
, &aBuilder
, myFeedback
);
168 if (yyparse () || error_count
> 0)
169 MessageBox (0, error_buf
,
170 error_count
== 1 ? "Parse Error" : "Parse Errors", 0);
173 /* save known-good setup.ini locally */
174 String
const fp
= String ("file://") + local_dir
+ "/" +
175 rfc1738_escape_part (n
->url
) +
177 io_stream::mkpath_p (PATH_TO_FILE
, fp
);
178 io_stream
*inistream
= io_stream::open (fp
, "wb");
181 if (compressed_ini_file
)
184 compressed_ini_file
->seek (0, IO_SEEK_SET
);
185 ini_file
= compress::decompress (compressed_ini_file
);
188 ini_file
->seek (0, IO_SEEK_SET
);
189 if (io_stream::copy (ini_file
, inistream
))
190 io_stream::remove (fp
);
195 if (aBuilder
.timestamp
> setup_timestamp
)
197 setup_timestamp
= aBuilder
.timestamp
;
198 setup_version
= aBuilder
.version
;
201 delete compressed_ini_file
;
207 do_ini_thread (HINSTANCE h
, HWND owner
)
209 size_t ini_count
= 0;
210 if (source
== IDC_SOURCE_CWD
)
211 ini_count
= do_local_ini (owner
);
213 ini_count
= do_remote_ini (owner
);
217 next_dialog
= source
== IDC_SOURCE_CWD
? IDD_S_FROM_CWD
: IDD_SITE
;
221 if (get_root_dir ().cstr_oneuse())
223 io_stream::mkpath_p (PATH_TO_DIR
, "cygfile:///etc/setup");
225 unsigned int old_timestamp
= 0;
227 io_stream::open ("cygfile:///etc/setup/timestamp", "rt");
231 memset (temp
, '\0', 20);
232 if (ots
->read (temp
, 19))
233 sscanf (temp
, "%u", &old_timestamp
);
235 if (old_timestamp
&& setup_timestamp
236 && (old_timestamp
> setup_timestamp
))
238 int yn
= yesno (owner
, IDS_OLD_SETUPINI
);
240 LogSingleton::GetInstance().exit (1);
246 io_stream::open ("cygfile:///etc/setup/timestamp", "wt");
250 sprintf (temp
, "%u", setup_timestamp
);
251 nts
->write (temp
, strlen (temp
));
257 msg ("setup_version is %s, our_version is %s", setup_version
.size() ?
258 setup_version
.cstr_oneuse() : "(null)",
260 if (setup_version
.size())
262 String ini_version
= canonicalize_version (setup_version
);
263 String our_version
= canonicalize_version (version
);
264 // XXX useversion < operator
265 if (our_version
.compare (ini_version
) < 0)
266 note (owner
, IDS_OLD_SETUP_VERSION
, version
, setup_version
.cstr_oneuse());
269 next_dialog
= IDD_CHOOSE
;
273 do_ini_thread_reflector(void* p
)
276 context
= (HANDLE
*)p
;
278 do_ini_thread((HINSTANCE
)context
[0], (HWND
)context
[1]);
280 // Tell the progress page that we're done downloading
281 Progress
.PostMessage(WM_APP_SETUP_INI_DOWNLOAD_COMPLETE
, 0, next_dialog
);
286 static HANDLE context
[2];
289 do_ini (HINSTANCE h
, HWND owner
)
295 CreateThread (NULL
, 0, do_ini_thread_reflector
, context
, 0, &threadID
);
303 yyerror (String
const &s
)
305 char buf
[MAX_PATH
+ 1000];
307 sprintf (buf
, "%s line %d: ", ini_filename
, yylineno
- yybol ());
308 sprintf (buf
+ strlen (buf
), s
.cstr_oneuse());
309 OutputDebugString (buf
);
312 strcat (error_buf
, "\n");
313 len
= strlen (error_buf
) + strlen (buf
) + 5;
314 error_buf
= (char *) realloc (error_buf
, len
);
315 strcat (error_buf
, buf
);
319 len
= strlen (buf
) + 5;
320 error_buf
= (char *) malloc (len
);
321 strcpy (error_buf
, buf
);
324 /* TODO: is return 0 correct? */
328 extern "C" int fprintf (FILE * f
, const char *s
, ...);
330 static char stderrbuf
[1000];
333 fprintf (FILE * f
, const char *fmt
, ...)
338 va_start (args
, fmt
);
341 rv
= vsnprintf (buf
, 1000, fmt
, args
);
342 /* todo check here for overflows too */
343 strcat (stderrbuf
, buf
);
344 if (char *nl
= strchr (stderrbuf
, '\n'))
347 /*OutputDebugString (stderrbuf); */
348 MessageBox (0, buf
, "Cygwin Setup", 0);
355 rv
= vfprintf (f
, fmt
, args
);