4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * See the included file LICENSE for details.
14 #include <sys/types.h>
19 #include <libtu/util.h>
20 #include <libtu/optparser.h>
21 #include <libtu/errorlog.h>
22 #include <libtu/prefix.h>
23 #include <libextl/readconfig.h>
24 #include <libmainloop/exec.h>
26 #include <ioncore/common.h>
27 #include <ioncore/global.h>
28 #include <ioncore/ioncore.h>
29 #include <ioncore/exec.h>
30 #include <ioncore/event.h>
31 #include "../version.h"
34 /* Options. Getopt is not used because getopt_long is quite gnu-specific
35 * and they don't know of '-display foo' -style args anyway.
36 * Instead, I've reinvented the wheel in libtu :(.
38 static OptParserOpt ion_opts[]={
39 {OPT_ID('d'), "display", OPT_ARG, "host:dpy.scr",
40 DUMMY_TR("X display to use")},
42 {'c', "conffile", OPT_ARG, "config_file",
43 DUMMY_TR("Configuration file")},
45 {'s', "searchdir", OPT_ARG, "dir",
46 DUMMY_TR("Add directory to search path")},
48 {OPT_ID('o'), "oneroot", 0, NULL,
49 DUMMY_TR("Manage default screen only")},
51 {OPT_ID('s'), "session", OPT_ARG, "session_name",
52 DUMMY_TR("Name of session (affects savefiles)")},
54 {OPT_ID('S'), "smclientid", OPT_ARG, "client_id",
55 DUMMY_TR("Session manager client ID")},
57 {OPT_ID('N'), "noerrorlog", 0, NULL,
58 DUMMY_TR("Do not create startup error log and display it "
61 {'h', "help", 0, NULL,
62 DUMMY_TR("Show this help")},
64 {'V', "version", 0, NULL,
65 DUMMY_TR("Show program version")},
67 {OPT_ID('a'), "about", 0, NULL,
68 DUMMY_TR("Show about text")},
74 void check_new_user_help()
76 const char *userdir=extl_userdir();
78 char *tmp=NULL, *cmd=NULL;
83 warn(TR("Could not get user configuration file directory."));
87 libtu_asprintf(&oldbeard, "%s/.welcome_msg_displayed", userdir);
92 if(access(oldbeard, F_OK)==0){
97 libtu_asprintf(&tmp, TR("%s/welcome.txt"), SHAREDIR);
100 if(access(tmp, F_OK)==0)
101 libtu_asprintf(&cmd, "%s %s", CF_XMESSAGE, tmp);
103 libtu_asprintf(&cmd, "%s %s/welcome.txt", CF_XMESSAGE, SHAREDIR);
108 ret=ioncore_exec(cmd);
113 /* This should actually be done when less or xmessage returns,
114 * but that would mean yet another script...
116 mkdir(userdir, 0700);
117 if(open(oldbeard, O_CREAT|O_RDWR, 0600)<0)
118 warn_err_obj(oldbeard);
130 printf(TR("Usage: %s [options]\n\n"), libtu_progname());
131 for(i=0; ion_opts[i].descr!=NULL; i++)
132 ion_opts[i].descr=TR(ion_opts[i].descr);
133 optparser_printhelp(OPTP_MIDLONG, ion_opts);
138 int main(int argc, char*argv[])
140 const char *cfgfile="cfg_ion";
141 const char *display=NULL;
148 bool may_continue=FALSE;
149 bool noerrorlog=FALSE;
154 #ifdef CF_RELOCATABLE
155 prefix_set(argv[0], ION3_LOCATION);
158 localedir=prefix_add(LOCALEDIR);
160 if(!ioncore_init("ion3", argc, argv, localedir))
166 prefix_wrap_simple(extl_add_searchdir, EXTRABINDIR); /* ion-completefile */
167 prefix_wrap_simple(extl_add_searchdir, MODULEDIR);
168 prefix_wrap_simple(extl_add_searchdir, ETCDIR);
169 prefix_wrap_simple(extl_add_searchdir, SHAREDIR);
170 prefix_wrap_simple(extl_add_searchdir, LCDIR);
171 extl_set_userdirs("ion3");
173 optparser_init(argc, argv, OPTP_MIDLONG, ion_opts);
175 while((opt=optparser_get_opt())){
178 display=optparser_get_arg();
181 cfgfile=optparser_get_arg();
184 extl_add_searchdir(optparser_get_arg());
187 ioncore_g.sm_client_id=optparser_get_arg();
190 stflags|=IONCORE_STARTUP_ONEROOT;
193 extl_set_sessiondir(optparser_get_arg());
202 printf("%s\n", ION_VERSION);
205 printf("%s\n", ioncore_aboutmsg());
208 warn(TR("Invalid command line."));
215 /* We may have to pass the file to xmessage so just using tmpfile()
218 libtu_asprintf(&efnam, "%s/ion-%d-startup-errorlog", P_tmpdir,
223 ef=fopen(efnam, "wt");
229 cloexec_braindamage_fix(fileno(ef));
230 fprintf(ef, TR("Ion startup error log:\n"));
231 errorlog_begin_file(&el, ef);
236 if(ioncore_startup(display, cfgfile, stflags))
241 warn(TR("Refusing to start due to encountered errors."));
243 check_new_user_help();
247 if(errorlog_end(&el) && ioncore_g.dpy!=NULL){
251 ioncore_setup_display(DefaultScreen(ioncore_g.dpy));
253 XCloseDisplay(ioncore_g.dpy);
255 close(ioncore_g.conn);
256 libtu_asprintf(&cmd, CF_XMESSAGE " %s", efnam);
259 }else if(system(cmd)==-1){
265 if(!may_continue && pid>0)
266 waitpid(pid, NULL, 0);
280 /* The code should never return here */