Fixed checkstatic(1) conformance.
[udpgate.git] / src / main.c
1 /* $Id$
2  * UDP Gateway utility
3  * Copyright (C) 2004 Jan Kratochvil <project-udpgate@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 <glib/gutils.h>
27 #include <string.h>
28 #include <setjmp.h>
29 #include <unistd.h>
30 #include <libgnome/gnome-program.h>
31 #include <libgnomeui/gnome-ui-init.h>
32
33 #include "ui-gnome.h"
34
35
36 int optarg_verbose;
37 static int optarg_text;
38
39 static const struct poptOption popt_table[]={
40
41 #define UDPGATE_POPT(shortname,longname,argInfoP,argP,valP,descripP,argDescripP) \
42                 { \
43                         longName: (longname), \
44                         shortName: (shortname), \
45                         argInfo: (argInfoP)|(!(valP) ? 0 : POPT_ARG_VAL), \
46                         arg: (void *)argP, \
47                         val: (valP), \
48                         descrip: (descripP), \
49                         argDescrip: (argDescripP), \
50                 }
51
52                 UDPGATE_POPT(0  ,"text"            ,POPT_ARG_NONE  ,&optarg_text   ,0,
53                                 N_("Disable Gnome UI; --text must be first argument"),NULL),
54                 UDPGATE_POPT('v',"verbose"         ,POPT_ARG_NONE  ,&optarg_verbose,0,N_("Display additional debug information"),NULL),
55
56 #undef UDPGATE_POPT
57                 POPT_TABLEEND
58                 };
59
60 static const struct poptOption popt_table_autohelp[]={
61                 { NULL,'\0',POPT_ARG_INCLUDE_TABLE,(struct poptOption *)&popt_table,0,PACKAGE },
62                 POPT_AUTOHELP
63                 POPT_TABLEEND
64                 };
65
66
67 static jmp_buf gnome_init_atexit_jmpbuf;
68 static gboolean gnome_init_atexit_disable;
69
70 static void gnome_init_atexit_handler(void)
71 {
72         if (gnome_init_atexit_disable)
73                 return;
74
75         longjmp(gnome_init_atexit_jmpbuf,1);
76         g_assert_not_reached();
77         _exit(EXIT_FAILURE);
78 }
79
80 static gboolean gnome_init_g_log_handler_hit;
81 static void gnome_init_g_log_handler(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
82 {
83         gnome_init_g_log_handler_hit=TRUE;
84         g_log_default_handler(log_domain,log_level,message,user_data);
85 }
86
87 int main(int argc,char **argv)
88 {
89 poptContext context;
90 int errint;
91 gboolean is_interactive;
92 gboolean no_gnome;
93
94 #if 0
95         g_log_set_always_fatal(~(0
96                         |G_LOG_LEVEL_MESSAGE
97                         |G_LOG_LEVEL_INFO
98                         |G_LOG_LEVEL_DEBUG
99                         ));
100 #endif
101
102         /* Initialize the i18n stuff */
103         setlocale(LC_ALL,"");
104         bindtextdomain(PACKAGE,LOCALEDIR);
105         textdomain(PACKAGE);
106
107         if (argv[1] && !strcmp(argv[1],"--text"))
108                 optarg_text=1;
109
110         no_gnome=(optarg_text || !getenv("DISPLAY") || !*getenv("DISPLAY"));
111
112         if (no_gnome) {
113                 context=poptGetContext(
114                                 PACKAGE,        /* name */
115                                 argc,(/*en-const*/const char **)argv,   /* argc,argv */
116                                 popt_table_autohelp,    /* options */
117                                 POPT_CONTEXT_POSIXMEHARDER);    /* flags; && !POPT_CONTEXT_KEEP_FIRST */
118                 if (context==NULL) {
119                         g_assert_not_reached(); /* argument recognization args_error */
120                         return EXIT_FAILURE;
121                         }
122                 errint=poptReadDefaultConfig(context,
123                                 TRUE);  /* useEnv */
124                 if (errint!=0) {
125                         g_assert_not_reached(); /* argument recognization args_error */
126                         return EXIT_FAILURE;
127                         }
128                 errint=poptGetNextOpt(context);
129                 if (errint!=-1) {
130                         g_assert_not_reached(); /* some non-callbacked argument reached */
131                         return EXIT_FAILURE;
132                         }
133                 if (poptPeekArg(context)) {
134                         g_error(_("No arguments expected"));
135                         return EXIT_FAILURE;
136                         }
137                 }
138         else {
139 GnomeProgram *gnome_program;
140 guint handler_id;
141
142                 gnome_init_atexit_disable=FALSE;
143                 g_atexit(gnome_init_atexit_handler);
144                 gnome_init_g_log_handler_hit=FALSE;
145                 handler_id=g_log_set_handler(
146                                 "Gtk",  /* log_domain */
147                                 G_LOG_LEVEL_WARNING,    /* log_levels */
148                                 gnome_init_g_log_handler,       /* log_func */
149                                 NULL);  /* user_data */
150                 if (!setjmp(gnome_init_atexit_jmpbuf))
151                         gnome_program=gnome_program_init(PACKAGE,VERSION,LIBGNOMEUI_MODULE,argc,argv,
152                                         GNOME_PARAM_POPT_TABLE,popt_table,
153                                         GNOME_PARAM_POPT_FLAGS,(int)POPT_CONTEXT_POSIXMEHARDER,
154                                         NULL);
155                 else {
156                         no_gnome=TRUE;
157                         /* No message: (captive-install-acquire:3693): Gtk-WARNING **: cannot open display:
158                          * was reported, probably only '--help' message was shown.
159                          */
160                         if (!gnome_init_g_log_handler_hit)
161                                 exit(EXIT_SUCCESS);
162                         }
163                 gnome_init_atexit_disable=TRUE;
164                 g_log_remove_handler(
165                                 "Gtk",  /* log_domain */
166                                 handler_id);    /* handler_id */
167                 }
168
169         is_interactive=(1
170 /*                      && ! optarg_scan_path_list
171                         && ! optarg_scan_disks_quick
172                         && ! optarg_scan_disks 
173                         && ! optarg_microsoft_com */ );
174
175         /* Initialize UI here to catch all GLog errors below. */
176         if (is_interactive
177                         && (no_gnome || !ui_gnome_init())
178 /*                      && !ui_line_init()*/)
179                 g_error(_("No UI interface could be initialized"));
180
181         if (!is_interactive)
182                 /*scan_batch()*/;
183         else
184                 ui_gnome_interactive();
185
186         return EXIT_SUCCESS;
187 }