Cosmetic: Removed some forgotten from captive-install-acquire(1).
[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
31 #ifdef HAVE_GNOME
32 #include <libgnome/gnome-program.h>
33 #include <libgnomeui/gnome-ui-init.h>
34 #endif
35
36 #include "main.h"       /* self */
37 #include "ui-line.h"
38 #include "network.h"
39
40 #ifdef HAVE_GNOME
41 #include "ui-gnome.h"
42 #endif
43
44
45 /* Config: */
46 #define LOCAL_PORT_DEFAULT 9201
47
48
49 static int optarg_text;
50 int optarg_verbose;
51 int optarg_port=LOCAL_PORT_DEFAULT;
52 static int optarg_start;
53 static int optarg_stop;
54 int optarg_no_fork;
55
56 void (*ui_interactive)(void);
57
58 static const struct poptOption popt_table[]={
59
60 #define UDPGATE_POPT(shortname,longname,argInfoP,argP,valP,descripP,argDescripP) \
61                 { \
62                         longName: (longname), \
63                         shortName: (shortname), \
64                         argInfo: (argInfoP)|(!(valP) ? 0 : POPT_ARG_VAL), \
65                         arg: (void *)argP, \
66                         val: (valP), \
67                         descrip: (descripP), \
68                         argDescrip: (argDescripP), \
69                 }
70
71 #ifdef HAVE_GNOME
72 #define OPT_TEXT_IF_GNOME N_("Disable Gnome UI; --text must be first argument")
73 #else /* HAVE_GNOME */
74 #define OPT_TEXT_IF_GNOME N_("(no Gnome UI compiled - stub only); --text must be first argument")
75 #endif /* HAVE_GNOME */
76                 UDPGATE_POPT(0  ,"text"   ,POPT_ARG_NONE                          ,&optarg_text   ,0,
77                                 OPT_TEXT_IF_GNOME,NULL),
78 #undef OPT_TEXT_IF_GNOME
79                 UDPGATE_POPT('v',"verbose",POPT_ARG_NONE                          ,&optarg_verbose,0,
80                                 N_("Display additional debug information"),NULL),
81                 UDPGATE_POPT('p',"port"   ,POPT_ARG_INT |POPT_ARGFLAG_SHOW_DEFAULT,&optarg_port   ,0,
82                                 N_("Listen on this UDP port"),NULL),
83                 UDPGATE_POPT('s',"start"  ,POPT_ARG_NONE                          ,&optarg_start  ,0,
84                                 N_("Start the daemon"),NULL),
85                 UDPGATE_POPT('S',"stop"   ,POPT_ARG_NONE                          ,&optarg_stop   ,0,
86                                 N_("Stop the daemon"),NULL),
87                 UDPGATE_POPT('1',"no-fork",POPT_ARG_NONE                          ,&optarg_no_fork,0,
88                                 N_("Do not detach from the current process"),NULL),
89
90 #undef UDPGATE_POPT
91                 POPT_TABLEEND
92                 };
93
94 static const struct poptOption popt_table_autohelp[]={
95                 { NULL,'\0',POPT_ARG_INCLUDE_TABLE,(struct poptOption *)&popt_table,0,PACKAGE },
96                 POPT_AUTOHELP
97                 POPT_TABLEEND
98                 };
99
100
101 #ifdef HAVE_GNOME
102 static jmp_buf gnome_init_atexit_jmpbuf;
103 static gboolean gnome_init_atexit_disable;
104
105 static void gnome_init_atexit_handler(void)
106 {
107         if (gnome_init_atexit_disable)
108                 return;
109
110         longjmp(gnome_init_atexit_jmpbuf,1);
111         g_assert_not_reached();
112         _exit(EXIT_FAILURE);
113 }
114
115 static gboolean gnome_init_g_log_handler_hit;
116 static void gnome_init_g_log_handler(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
117 {
118         gnome_init_g_log_handler_hit=TRUE;
119         g_log_default_handler(log_domain,log_level,message,user_data);
120 }
121 #endif /* HAVE_GNOME */
122
123 int main(int argc,char **argv)
124 {
125 poptContext context;
126 int errint;
127 gboolean is_interactive;
128 #ifdef HAVE_GNOME
129 gboolean no_gnome;
130 #endif /* HAVE_GNOME */
131
132 #if 0
133         g_log_set_always_fatal(~(0
134                         |G_LOG_LEVEL_MESSAGE
135                         |G_LOG_LEVEL_INFO
136                         |G_LOG_LEVEL_DEBUG
137                         ));
138 #endif
139
140         /* Initialize the i18n stuff */
141         setlocale(LC_ALL,"");
142         bindtextdomain(PACKAGE,LOCALEDIR);
143         textdomain(PACKAGE);
144
145         if (argv[1] && !strcmp(argv[1],"--text"))
146                 optarg_text=1;
147
148 #ifdef HAVE_GNOME
149         no_gnome=(optarg_text || !getenv("DISPLAY") || !*getenv("DISPLAY"));
150
151         if (no_gnome)
152 #endif /* HAVE_GNOME */
153         {
154                 context=poptGetContext(
155                                 PACKAGE,        /* name */
156                                 argc,(/*en-const*/const char **)argv,   /* argc,argv */
157                                 popt_table_autohelp,    /* options */
158                                 POPT_CONTEXT_POSIXMEHARDER);    /* flags; && !POPT_CONTEXT_KEEP_FIRST */
159                 if (context==NULL) {
160                         g_assert_not_reached(); /* argument recognization args_error */
161                         return EXIT_FAILURE;
162                         }
163                 errint=poptReadDefaultConfig(context,
164                                 TRUE);  /* useEnv */
165                 if (errint!=0) {
166                         g_assert_not_reached(); /* argument recognization args_error */
167                         return EXIT_FAILURE;
168                         }
169                 errint=poptGetNextOpt(context);
170                 if (errint!=-1)
171                         g_error(_("Specified option not expected"));
172                 if (poptPeekArg(context))
173                         g_error(_("No arguments expected"));
174                 }
175 #ifdef HAVE_GNOME
176         else {
177 GnomeProgram *gnome_program;
178 guint handler_id;
179
180                 gnome_init_atexit_disable=FALSE;
181                 g_atexit(gnome_init_atexit_handler);
182                 gnome_init_g_log_handler_hit=FALSE;
183                 handler_id=g_log_set_handler(
184                                 "Gtk",  /* log_domain */
185                                 G_LOG_LEVEL_WARNING,    /* log_levels */
186                                 gnome_init_g_log_handler,       /* log_func */
187                                 NULL);  /* user_data */
188                 if (!setjmp(gnome_init_atexit_jmpbuf))
189                         gnome_program=gnome_program_init(PACKAGE,VERSION,LIBGNOMEUI_MODULE,argc,argv,
190                                         GNOME_PARAM_POPT_TABLE,popt_table,
191                                         GNOME_PARAM_POPT_FLAGS,(int)POPT_CONTEXT_POSIXMEHARDER,
192                                         NULL);
193                 else {
194                         no_gnome=TRUE;
195                         /* No message: (udpgate:3693): Gtk-WARNING **: cannot open display:
196                          * was reported, probably only '--help' message was shown.
197                          */
198                         if (!gnome_init_g_log_handler_hit)
199                                 exit(EXIT_SUCCESS);
200                         }
201                 gnome_init_atexit_disable=TRUE;
202                 g_log_remove_handler(
203                                 "Gtk",  /* log_domain */
204                                 handler_id);    /* handler_id */
205                 }
206 #endif /* HAVE_GNOME */
207
208         is_interactive=(1
209                         && !optarg_start
210                         && !optarg_stop);
211
212         /* Initialize UI here to catch all GLog errors below. */
213         if (is_interactive
214 #ifdef HAVE_GNOME
215                         && (no_gnome || !ui_gnome_init())
216 #endif /* HAVE_GNOME */
217                         && !ui_line_init())
218                 g_error(_("No UI interface could be initialized"));
219
220         if (!is_interactive) {
221                 if (optarg_stop)
222                         network_stop();
223                 if (optarg_start)
224                         network_start(optarg_port);
225                 network_detach();
226                 }
227         else
228                 (*ui_interactive)();
229
230         return EXIT_SUCCESS;
231 }