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