3 * Copyright (C) 2004 Jan Kratochvil <project-udpgate@jankratochvil.net>
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
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.
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
22 #include <glib/gmessages.h>
26 #include <glib/gutils.h>
32 #include <libgnome/gnome-program.h>
33 #include <libgnomeui/gnome-ui-init.h>
36 #include "main.h" /* self */
46 #define LOCAL_PORT_DEFAULT 9201
49 static int optarg_text;
51 int optarg_port=LOCAL_PORT_DEFAULT;
52 static int optarg_start;
53 static int optarg_stop;
56 void (*ui_interactive)(void);
58 static const struct poptOption popt_table[]={
60 #define UDPGATE_POPT(shortname,longname,argInfoP,argP,valP,descripP,argDescripP) \
62 longName: (longname), \
63 shortName: (shortname), \
64 argInfo: (argInfoP)|(!(valP) ? 0 : POPT_ARG_VAL), \
67 descrip: (descripP), \
68 argDescrip: (argDescripP), \
71 UDPGATE_POPT(0 ,"text" ,POPT_ARG_NONE ,&optarg_text ,0,
73 N_("Disable Gnome UI; --text must be first argument")
74 #else /* HAVE_GNOME */
75 N_("(no Gnome UI compiled - stub only); --text must be first argument")
76 #endif /* HAVE_GNOME */
78 UDPGATE_POPT('v',"verbose",POPT_ARG_NONE ,&optarg_verbose,0,
79 N_("Display additional debug information"),NULL),
80 UDPGATE_POPT('p',"port" ,POPT_ARG_INT |POPT_ARGFLAG_SHOW_DEFAULT,&optarg_port ,0,
81 N_("Listen on this UDP port"),NULL),
82 UDPGATE_POPT('s',"start" ,POPT_ARG_NONE ,&optarg_start ,0,
83 N_("Start the daemon"),NULL),
84 UDPGATE_POPT('S',"stop" ,POPT_ARG_NONE ,&optarg_stop ,0,
85 N_("Stop the daemon"),NULL),
86 UDPGATE_POPT('1',"no-fork",POPT_ARG_NONE ,&optarg_no_fork,0,
87 N_("Do not detach from the current process"),NULL),
93 static const struct poptOption popt_table_autohelp[]={
94 { NULL,'\0',POPT_ARG_INCLUDE_TABLE,(struct poptOption *)&popt_table,0,PACKAGE },
101 static jmp_buf gnome_init_atexit_jmpbuf;
102 static gboolean gnome_init_atexit_disable;
104 static void gnome_init_atexit_handler(void)
106 if (gnome_init_atexit_disable)
109 longjmp(gnome_init_atexit_jmpbuf,1);
110 g_assert_not_reached();
114 static gboolean gnome_init_g_log_handler_hit;
115 static void gnome_init_g_log_handler(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
117 gnome_init_g_log_handler_hit=TRUE;
118 g_log_default_handler(log_domain,log_level,message,user_data);
120 #endif /* HAVE_GNOME */
122 int main(int argc,char **argv)
126 gboolean is_interactive;
129 #endif /* HAVE_GNOME */
132 g_log_set_always_fatal(~(0
139 /* Initialize the i18n stuff */
140 setlocale(LC_ALL,"");
141 bindtextdomain(PACKAGE,LOCALEDIR);
144 if (argv[1] && !strcmp(argv[1],"--text"))
148 no_gnome=(optarg_text || !getenv("DISPLAY") || !*getenv("DISPLAY"));
151 #endif /* HAVE_GNOME */
153 context=poptGetContext(
155 argc,(/*en-const*/const char **)argv, /* argc,argv */
156 popt_table_autohelp, /* options */
157 POPT_CONTEXT_POSIXMEHARDER); /* flags; && !POPT_CONTEXT_KEEP_FIRST */
159 g_assert_not_reached(); /* argument recognization args_error */
162 errint=poptReadDefaultConfig(context,
165 g_assert_not_reached(); /* argument recognization args_error */
168 errint=poptGetNextOpt(context);
170 g_error(_("Specified option not expected"));
171 if (poptPeekArg(context))
172 g_error(_("No arguments expected"));
176 GnomeProgram *gnome_program;
179 gnome_init_atexit_disable=FALSE;
180 g_atexit(gnome_init_atexit_handler);
181 gnome_init_g_log_handler_hit=FALSE;
182 handler_id=g_log_set_handler(
183 "Gtk", /* log_domain */
184 G_LOG_LEVEL_WARNING, /* log_levels */
185 gnome_init_g_log_handler, /* log_func */
186 NULL); /* user_data */
187 if (!setjmp(gnome_init_atexit_jmpbuf))
188 gnome_program=gnome_program_init(PACKAGE,VERSION,LIBGNOMEUI_MODULE,argc,argv,
189 GNOME_PARAM_POPT_TABLE,popt_table,
190 GNOME_PARAM_POPT_FLAGS,(int)POPT_CONTEXT_POSIXMEHARDER,
194 /* No message: (captive-install-acquire:3693): Gtk-WARNING **: cannot open display:
195 * was reported, probably only '--help' message was shown.
197 if (!gnome_init_g_log_handler_hit)
200 gnome_init_atexit_disable=TRUE;
201 g_log_remove_handler(
202 "Gtk", /* log_domain */
203 handler_id); /* handler_id */
205 #endif /* HAVE_GNOME */
211 /* Initialize UI here to catch all GLog errors below. */
214 && (no_gnome || !ui_gnome_init())
215 #endif /* HAVE_GNOME */
217 g_error(_("No UI interface could be initialized"));
219 if (!is_interactive) {
223 network_start(optarg_port);