Cosmetic: Fixed warn.
[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 #include "configuration.h"
40 #include "static-startup.h"
41 #ifdef ENABLE_BUNDLE
42 #include "bundle-util.h"
43 #endif
44
45 #ifdef HAVE_GNOME
46 #include "ui-gnome.h"
47 #endif
48
49
50 /* Config: */
51 #define LOCAL_PORT_DEFAULT 9201
52
53
54 static int optarg_text;
55 int optarg_verbose;
56 int optarg_port=LOCAL_PORT_DEFAULT;
57 static int optarg_start;
58 static int optarg_stop;
59 int optarg_no_fork;
60 static int optarg_startup_query;
61 static int optarg_startup_on;
62 static int optarg_startup_off;
63
64 void (*ui_interactive)(void);
65
66 static const struct poptOption popt_table[]={
67
68 #define UDPGATE_POPT(shortname,longname,argInfoP,argP,valP,descripP,argDescripP) \
69                 { \
70                         longName: (longname), \
71                         shortName: (shortname), \
72                         argInfo: (argInfoP)|(!(valP) ? 0 : POPT_ARG_VAL), \
73                         arg: (void *)argP, \
74                         val: (valP), \
75                         descrip: (descripP), \
76                         argDescrip: (argDescripP), \
77                 }
78
79 #ifdef HAVE_GNOME
80 #define OPT_TEXT_IF_GNOME N_("Disable Gnome UI; --text must be first argument")
81 #else /* HAVE_GNOME */
82 #define OPT_TEXT_IF_GNOME N_("(no Gnome UI compiled - stub only); --text must be first argument")
83 #endif /* HAVE_GNOME */
84                 UDPGATE_POPT(0  ,"text"         ,POPT_ARG_NONE                          ,&optarg_text         ,0,
85                                 OPT_TEXT_IF_GNOME,NULL),                                                                  
86 #undef OPT_TEXT_IF_GNOME                                                                          
87                 UDPGATE_POPT('v',"verbose"      ,POPT_ARG_NONE                          ,&optarg_verbose      ,0,
88                                 N_("Display additional debug information"),NULL),                                         
89                 UDPGATE_POPT('p',"port"         ,POPT_ARG_INT |POPT_ARGFLAG_SHOW_DEFAULT,&optarg_port         ,0,
90                                 N_("Listen on this UDP port"),NULL),                                                      
91                 UDPGATE_POPT('s',"start"        ,POPT_ARG_NONE                          ,&optarg_start        ,0,
92                                 N_("Start the daemon"),NULL),                                                             
93                 UDPGATE_POPT('S',"stop"         ,POPT_ARG_NONE                          ,&optarg_stop         ,0,
94                                 N_("Stop the daemon"),NULL),
95                 UDPGATE_POPT('1',"no-fork"      ,POPT_ARG_NONE                          ,&optarg_no_fork      ,0,
96                                 N_("Do not detach from the current process"),NULL),
97                 UDPGATE_POPT(0  ,"startup-query",POPT_ARG_NONE                          ,&optarg_startup_query,0,
98                                 N_("Query the current state of the system startup registrance"),NULL),
99                 UDPGATE_POPT(0  ,"startup-on"   ,POPT_ARG_NONE                          ,&optarg_startup_on   ,0,
100                                 N_("Register for the automatic system startup"),NULL),
101                 UDPGATE_POPT(0  ,"startup-off"  ,POPT_ARG_NONE                          ,&optarg_startup_off  ,0,
102                                 N_("Unregister from the automatic system startup"),NULL),
103
104 #undef UDPGATE_POPT
105                 POPT_TABLEEND
106                 };
107
108 static const struct poptOption popt_table_autohelp[]={
109                 { NULL,'\0',POPT_ARG_INCLUDE_TABLE,(struct poptOption *)&popt_table,0,PACKAGE },
110                 POPT_AUTOHELP
111                 POPT_TABLEEND
112                 };
113
114
115 #ifdef HAVE_GNOME
116 static jmp_buf gnome_init_atexit_jmpbuf;
117 static gboolean gnome_init_atexit_disable;
118
119 static void gnome_init_atexit_handler(void)
120 {
121         if (gnome_init_atexit_disable)
122                 return;
123
124         longjmp(gnome_init_atexit_jmpbuf,1);
125         g_assert_not_reached();
126         _exit(EXIT_FAILURE);
127 }
128
129 static gboolean gnome_init_g_log_handler_hit;
130 static void gnome_init_g_log_handler(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
131 {
132         gnome_init_g_log_handler_hit=TRUE;
133         g_log_default_handler(log_domain,log_level,message,user_data);
134 }
135 #endif /* HAVE_GNOME */
136
137 static gchar *localedir;
138
139 #ifdef ENABLE_BUNDLE
140 static gboolean locale_files_write(void)
141 {
142 gchar catalogs[]=G_STRINGIFY(CATALOGS);
143 gchar *catalog,*catalog_next;
144
145         for (catalog=catalogs;*catalog;catalog=catalog_next) {
146 gchar *cc,*cc_end;
147
148                 for (catalog_next=catalog;*catalog_next && *catalog_next!=' ';catalog_next++);
149                 if (*catalog_next)
150                         *catalog_next++='\0';
151                 cc=g_strdup(catalog);
152                 cc_end=cc+strlen(cc)-strlen(".gmo");
153                 if (cc_end>cc && !strcmp(cc_end,".gmo")) {
154                         *cc_end='\0';
155                         if (!bundle_util_file_write(
156                                         udpgate_printf_alloca("%s/%s/LC_MESSAGES/%s.mo",LOCALEDIR,cc,PACKAGE),  /* pathname */
157                                         catalog,        /* basename */
158                                         0644,   /* pathname_mode */
159                                         BUNDLE_UTIL_MKDIRS_MASK|BUNDLE_UTIL_TEMPORARY_MASK)) {  /* flags */
160 const gchar *tmpdir=g_get_tmp_dir();
161 const gchar *username=g_get_user_name();
162 gchar *trylocaledir=localedir;
163
164                                 if (!trylocaledir && tmpdir && username)
165                                         trylocaledir=g_strdup_printf("%s/.%s-%s/locale",tmpdir,username,PACKAGE);
166                                 if (trylocaledir && bundle_util_file_write(
167                                                 udpgate_printf_alloca("%s/%s/LC_MESSAGES/%s.mo",trylocaledir,cc,PACKAGE),       /* pathname */
168                                                 catalog,        /* basename */
169                                                 0644,   /* pathname_mode */
170                                                 BUNDLE_UTIL_MKDIRS_MASK|BUNDLE_UTIL_TEMPORARY_MASK))    /* flags */
171                                         localedir=trylocaledir;
172                                 }
173
174                         }
175                 g_free(cc);
176                 }
177         return FALSE;
178 }
179 #endif /* ENABLE_BUNDLE */
180
181 int main(int argc,char **argv)
182 {
183 poptContext context;
184 int errint;
185 gboolean is_interactive;
186 #ifdef HAVE_GNOME
187 gboolean no_gnome;
188 #endif /* HAVE_GNOME */
189 int exit_rc=EXIT_SUCCESS;
190
191 #ifdef ENABLE_BUNDLE
192         locale_files_write();
193 #endif /* ENABLE_BUNDLE */
194
195 #if 0
196         g_log_set_always_fatal(~(0
197                         |G_LOG_LEVEL_MESSAGE
198                         |G_LOG_LEVEL_INFO
199                         |G_LOG_LEVEL_DEBUG
200                         ));
201 #endif
202
203         /* Initialize the i18n stuff */
204         setlocale(LC_ALL,"");
205         if (!localedir)
206                 localedir=LOCALEDIR;
207         bindtextdomain(PACKAGE,localedir);
208         textdomain(PACKAGE);
209
210         /* Read it before the command-line parsing to get the default value
211          * of 'optarg_port' displayable by 'POPT_ARGFLAG_SHOW_DEFAULT'.
212          */
213         configuration_read();
214
215         if (argv[1] && !strcmp(argv[1],"--text"))
216                 optarg_text=1;
217
218 #ifdef HAVE_GNOME
219         no_gnome=(optarg_text || !getenv("DISPLAY") || !*getenv("DISPLAY"));
220
221         if (no_gnome)
222 #endif /* HAVE_GNOME */
223         {
224                 context=poptGetContext(
225                                 PACKAGE,        /* name */
226                                 argc,(/*en-const*/const char **)argv,   /* argc,argv */
227                                 popt_table_autohelp,    /* options */
228                                 POPT_CONTEXT_POSIXMEHARDER);    /* flags; && !POPT_CONTEXT_KEEP_FIRST */
229                 if (context==NULL) {
230                         g_assert_not_reached(); /* argument recognization args_error */
231                         return EXIT_FAILURE;
232                         }
233                 errint=poptReadDefaultConfig(context,
234                                 TRUE);  /* useEnv */
235                 if (errint!=0) {
236                         g_assert_not_reached(); /* argument recognization args_error */
237                         return EXIT_FAILURE;
238                         }
239                 errint=poptGetNextOpt(context);
240                 if (errint!=-1)
241                         g_error(_("Specified option not expected"));
242                 if (poptPeekArg(context))
243                         g_error(_("No arguments expected"));
244                 }
245 #ifdef HAVE_GNOME
246         else {
247 GnomeProgram *gnome_program;
248 guint handler_id;
249
250                 gnome_init_atexit_disable=FALSE;
251                 g_atexit(gnome_init_atexit_handler);
252                 gnome_init_g_log_handler_hit=FALSE;
253                 handler_id=g_log_set_handler(
254                                 "Gtk",  /* log_domain */
255                                 G_LOG_LEVEL_WARNING,    /* log_levels */
256                                 gnome_init_g_log_handler,       /* log_func */
257                                 NULL);  /* user_data */
258                 if (!setjmp(gnome_init_atexit_jmpbuf))
259                         gnome_program=gnome_program_init(PACKAGE,VERSION,LIBGNOMEUI_MODULE,argc,argv,
260                                         GNOME_PARAM_POPT_TABLE,popt_table,
261                                         GNOME_PARAM_POPT_FLAGS,(int)POPT_CONTEXT_POSIXMEHARDER,
262                                         NULL);
263                 else {
264                         no_gnome=TRUE;
265                         /* No message: (udpgate:3693): Gtk-WARNING **: cannot open display:
266                          * was reported, probably only '--help' message was shown.
267                          */
268                         if (!gnome_init_g_log_handler_hit)
269                                 exit(EXIT_SUCCESS);
270                         }
271                 gnome_init_atexit_disable=TRUE;
272                 g_log_remove_handler(
273                                 "Gtk",  /* log_domain */
274                                 handler_id);    /* handler_id */
275                 }
276 #endif /* HAVE_GNOME */
277
278         configuration_write();
279
280         is_interactive=(1
281                         && !optarg_start
282                         && !optarg_stop
283                         && !optarg_startup_query
284                         && !optarg_startup_on
285                         && !optarg_startup_off);
286
287         /* Initialize UI here to catch all GLog errors below. */
288         if (is_interactive
289 #ifdef HAVE_GNOME
290                         && (no_gnome || !ui_gnome_init())
291 #endif /* HAVE_GNOME */
292                         && !ui_line_init())
293                 g_error(_("No UI interface could be initialized"));
294
295         if (!is_interactive) {
296
297                 if (optarg_stop)
298                         if (!network_stop())
299                                 exit_rc=2;
300                 if (optarg_startup_query) {
301 gboolean is_on;
302
303                         if (static_startup_query(&is_on)) {
304                                 g_message((is_on
305                                                 ? _("System startup registrance is turned on.")
306                                                 : _("System startup registrance is turned off.")));
307                                 if (exit_rc<2)
308                                         exit_rc=(is_on ? 0 : 1);
309                                 }
310                         else
311                                 exit_rc=2;
312                         }
313                 if (optarg_startup_off) {
314                         if (!static_startup_off())
315                                 exit_rc=2;
316                         }
317                 if (optarg_startup_on) {
318                         if (!static_startup_on())
319                                 exit_rc=2;
320                         }
321                 if (optarg_start)
322                         if (!network_start(optarg_port))
323                                 exit_rc=2;
324
325                 network_detach();
326                 }
327         else
328                 (*ui_interactive)();
329
330         configuration_write();
331
332         exit(exit_rc);
333 }