Fatality can be only enabled from '#if 0' again.
[captive.git] / src / install / acquire / main.c
1 /* $Id$
2  * Drivers acquiring installation utility
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include <popt.h>
24 #include <locale.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <mntent.h>
28 #include <glib/ghash.h>
29 #include <glib/gstrfuncs.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <libgnomevfs/gnome-vfs-init.h>
34 #include "ui-line.h"
35 #include "microsoftcom.h"
36 #include <libgnomevfs/gnome-vfs-uri.h>
37 #include "diskscan.h"
38 #include "moduriload.h"
39 #include <libgnome/gnome-program.h>
40 #include <libgnomeui/gnome-ui-init.h>
41 #include <setjmp.h>
42 #include "ui-gnome.h"
43
44 #ifdef ENABLE_STANDALONE
45 #include "standalone-libntfs.h"
46 #endif
47
48 #include <captive/macros.h>
49 #include <captive/client.h>
50 #include <captive/captivemodid.h>
51
52
53 CaptiveCaptivemodidObject *captivemodid;
54
55 int optarg_verbose;
56 int optarg_dry;
57 static int optarg_microsoft_com;
58 static int optarg_scan_disks;
59 static int optarg_scan_disks_quick;
60 static int optarg_text;
61 static GList *optarg_scan_path_list;    /* of (char *) */
62
63 static void acquire_popt_callback
64                 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data);
65
66 static const struct poptOption popt_table[]={
67                 { argInfo:POPT_ARG_CALLBACK,arg:(void *)acquire_popt_callback },
68
69 #define BUG_ACQUIRE_POPT(shortname,longname,argInfoP,argP,valP,descripP,argDescripP) \
70                 { \
71                         longName: (longname), \
72                         shortName: (shortname), \
73                         argInfo: (argInfoP)|(!(valP) ? 0 : POPT_ARG_VAL), \
74                         arg: (void *)argP, \
75                         val: (valP), \
76                         descrip: (descripP), \
77                         argDescrip: (argDescripP), \
78                 }
79
80                 BUG_ACQUIRE_POPT(0  ,"text"            ,POPT_ARG_NONE  ,&optarg_text   ,0,
81                                 N_("Disable Gnome UI; --text must be first argument"),NULL),
82                 BUG_ACQUIRE_POPT('v',"verbose"         ,POPT_ARG_NONE  ,&optarg_verbose,0,N_("Display additional debug information"),NULL),
83                 BUG_ACQUIRE_POPT('n',"dry"             ,POPT_ARG_NONE  ,&optarg_dry    ,0,N_("No modifications, no files written"),NULL),
84                 BUG_ACQUIRE_POPT(0  ,"scan-disks"      ,POPT_ARG_NONE  ,&optarg_scan_disks,0,N_("Scan all files on local disks"),NULL),
85                 BUG_ACQUIRE_POPT(0  ,"scan-disks-quick",POPT_ARG_NONE  ,&optarg_scan_disks_quick,0,
86                                 N_("Scan MS-Windows directories on local disks"),NULL),
87                 BUG_ACQUIRE_POPT(0  ,"scan-path" ,POPT_ARG_STRING,NULL              ,0,
88                                 N_("Scan specified disk path or web URL"),N_("path/URI")),
89                 BUG_ACQUIRE_POPT(0  ,"microsoft-com"   ,POPT_ARG_NONE  ,&optarg_microsoft_com,0,
90                                 N_("Download from microsoft.com; Legal: You may need to have valid Microsoft Windows XP license."),NULL),
91
92 #undef BUG_ACQUIRE_POPT
93                 POPT_TABLEEND
94                 };
95
96 static const struct poptOption popt_table_autohelp[]={
97                 { NULL,'\0',POPT_ARG_INCLUDE_TABLE,(struct poptOption *)&popt_table,0,PACKAGE },
98                 POPT_AUTOHELP
99                 POPT_TABLEEND
100                 };
101
102 /* poptCallbackType captive_popt_callback */
103 static void acquire_popt_callback
104                 (poptContext con,enum poptCallbackReason reason,const struct poptOption *opt,const char *arg,const void *data)
105 {
106         g_return_if_fail(reason==POPT_CALLBACK_REASON_OPTION);
107
108         if (opt->longName && !strcmp(opt->longName,"scan-path")) {
109                 optarg_scan_path_list=g_list_append(optarg_scan_path_list,gnome_vfs_uri_new(arg));
110                 }
111 }
112
113
114 void (*ui_interactive)(void);
115
116 static gboolean ui_progress_dummy(GnomeVFSURI *uri)
117 {
118         /* 'uri' may be NULL */
119
120         return FALSE;   /* not aborted */
121 }
122
123 gboolean (*ui_progress)(GnomeVFSURI *uri)=ui_progress_dummy;
124 void (*ui_progress_bar)(gint done,gint length);
125
126 static GList *mod_uri_list_local;
127 static void mod_uri_list_local_init(void)
128 {
129         if (mod_uri_list_local)
130                 return;
131         mod_uri_list_local=mod_uri_list();
132 }
133
134 void scan_disks_quick(void)
135 {
136 GList *mod_uri_list_quick_local,*mod_uri_l;
137
138         mod_uri_list_local_init();
139         mod_uri_list_quick_local=NULL;
140         for (mod_uri_l=mod_uri_list_local;mod_uri_l;mod_uri_l=mod_uri_l->next) {
141                 mod_uri_list_quick_local=g_list_prepend(mod_uri_list_quick_local,
142                                 gnome_vfs_uri_append_path(mod_uri_l->data,"windows/system32"));
143                 }
144         mod_uri_list_quick_local=g_list_reverse(mod_uri_list_quick_local);
145         g_list_foreach(mod_uri_list_quick_local,(GFunc)mod_uri_load,NULL);
146         gnome_vfs_uri_list_free(mod_uri_list_quick_local);
147 }
148
149 void scan_disks(void)
150 {
151         mod_uri_list_local_init();
152         g_list_foreach(mod_uri_list_local,(GFunc)mod_uri_load,NULL);
153 }
154
155 void microsoft_com(void)
156 {
157         g_list_foreach(mod_uri_microsoftcom_list(),(GFunc)mod_uri_load_base_reporting,NULL);
158 }
159
160 static void scan_batch(void)
161 {
162         g_list_foreach(optarg_scan_path_list,(GFunc)mod_uri_load_base_reporting,NULL);
163
164         if (optarg_scan_disks_quick)
165                 scan_disks_quick();
166         if (optarg_scan_disks)
167                 scan_disks();
168         if (optarg_microsoft_com)
169                 microsoft_com();
170 }
171
172 static jmp_buf gnome_init_atexit_jmpbuf;
173 static gboolean gnome_init_atexit_disable;
174
175 static void gnome_init_atexit_handler(void)
176 {
177         if (gnome_init_atexit_disable)
178                 return;
179
180         longjmp(gnome_init_atexit_jmpbuf,1);
181         g_assert_not_reached();
182         _exit(EXIT_FAILURE);
183 }
184
185 gboolean gnome_init_g_log_handler_hit;
186 static void gnome_init_g_log_handler(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
187 {
188         gnome_init_g_log_handler_hit=TRUE;
189         g_log_default_handler(log_domain,log_level,message,user_data);
190 }
191
192 int main(int argc,char **argv)
193 {
194 poptContext context;
195 int errint;
196 gboolean is_interactive;
197 gboolean no_gnome;
198
199 #if 0
200         g_log_set_always_fatal(~(0
201                         |G_LOG_LEVEL_MESSAGE
202                         |G_LOG_LEVEL_INFO
203                         |G_LOG_LEVEL_DEBUG
204                         ));
205 #endif
206
207         captive_standalone_init();
208
209         if (!gnome_vfs_init())
210                 g_error(_("GnomeVFS failed to initialize"));
211
212 #ifdef ENABLE_STANDALONE
213         standalone_libntfs_init();
214 #endif
215
216         if (argv[1] && !strcmp(argv[1],"--text"))
217                 optarg_text=1;
218
219         no_gnome=(optarg_text || !getenv("DISPLAY") || !*getenv("DISPLAY"));
220
221         if (no_gnome) {
222                 context=poptGetContext(
223                                 PACKAGE,        /* name */
224                                 argc,(/*en-const*/const char **)argv,   /* argc,argv */
225                                 popt_table_autohelp,    /* options */
226                                 POPT_CONTEXT_POSIXMEHARDER);    /* flags; && !POPT_CONTEXT_KEEP_FIRST */
227                 if (context==NULL) {
228                         g_assert_not_reached(); /* argument recognization args_error */
229                         return EXIT_FAILURE;
230                         }
231                 errint=poptReadDefaultConfig(context,
232                                 TRUE);  /* useEnv */
233                 if (errint!=0) {
234                         g_assert_not_reached(); /* argument recognization args_error */
235                         return EXIT_FAILURE;
236                         }
237                 errint=poptGetNextOpt(context);
238                 if (errint!=-1) {
239                         g_assert_not_reached(); /* some non-callbacked argument reached */
240                         return EXIT_FAILURE;
241                         }
242                 if (poptPeekArg(context)) {
243                         g_error(_("No arguments expected"));
244                         return EXIT_FAILURE;
245                         }
246                 }
247         else {
248 GnomeProgram *gnome_program;
249 guint handler_id;
250
251                 captive_standalone_gnome_init();
252                 gnome_init_atexit_disable=FALSE;
253                 g_atexit(gnome_init_atexit_handler);
254                 gnome_init_g_log_handler_hit=FALSE;
255                 handler_id=g_log_set_handler(
256                                 "Gtk",  /* log_domain */
257                                 G_LOG_LEVEL_WARNING,    /* log_levels */
258                                 gnome_init_g_log_handler,       /* log_func */
259                                 NULL);  /* user_data */
260                 if (!setjmp(gnome_init_atexit_jmpbuf))
261                         gnome_program=gnome_program_init(PACKAGE,VERSION,LIBGNOMEUI_MODULE,argc,argv,
262                                         GNOME_PARAM_POPT_TABLE,popt_table,
263                                         GNOME_PARAM_POPT_FLAGS,(int)POPT_CONTEXT_POSIXMEHARDER,
264                                         NULL);
265                 else {
266                         no_gnome=TRUE;
267                         /* No message: (captive-install-acquire:3693): Gtk-WARNING **: cannot open display:
268                          * was reported, probably only '--help' message was shown.
269                          */
270                         if (!gnome_init_g_log_handler_hit)
271                                 exit(EXIT_SUCCESS);
272                         }
273                 gnome_init_atexit_disable=TRUE;
274                 g_log_remove_handler(
275                                 "Gtk",  /* log_domain */
276                                 handler_id);    /* handler_id */
277                 }
278
279         is_interactive=(1
280                         && ! optarg_scan_path_list
281                         && ! optarg_scan_disks_quick
282                         && ! optarg_scan_disks 
283                         && ! optarg_microsoft_com);
284
285         /* Initialize UI here to catch all GLog errors below. */
286         if (is_interactive
287                         && (no_gnome || !ui_gnome_init())
288                         && !ui_line_init())
289                 g_error(_("No UI interface could be initialized"));
290
291         captivemodid=captive_captivemodid_load_default(TRUE);
292
293         mod_uri_load_base_reporting(gnome_vfs_uri_new("file://" G_STRINGIFY(VARLIBCAPTIVEDIR)));
294
295         if (!is_interactive)
296                 scan_batch();
297         else
298                 (*ui_interactive)();
299
300         gnome_vfs_shutdown();
301
302         return EXIT_SUCCESS;
303 }