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 <libextl/readconfig.h>
23 #include <libmainloop/exec.h>
25 #include <ioncore/common.h>
26 #include <ioncore/global.h>
27 #include <ioncore/ioncore.h>
28 #include <ioncore/exec.h>
29 #include <ioncore/event.h>
30 #include "../version.h"
33 /* Options. Getopt is not used because getopt_long is quite gnu-specific
34 * and they don't know of '-display foo' -style args anyway.
35 * Instead, I've reinvented the wheel in libtu :(.
37 static OptParserOpt ion_opts[]={
38 {OPT_ID('d'), "display", OPT_ARG, "host:dpy.scr",
39 DUMMY_TR("X display to use")},
41 {'c', "conffile", OPT_ARG, "config_file",
42 DUMMY_TR("Configuration file")},
44 {'s', "searchdir", OPT_ARG, "dir",
45 DUMMY_TR("Add directory to search path")},
47 {OPT_ID('o'), "oneroot", 0, NULL,
48 DUMMY_TR("Manage default screen only")},
50 {OPT_ID('s'), "session", OPT_ARG, "session_name",
51 DUMMY_TR("Name of session (affects savefiles)")},
53 {OPT_ID('S'), "smclientid", OPT_ARG, "client_id",
54 DUMMY_TR("Session manager client ID")},
56 {OPT_ID('N'), "noerrorlog", 0, NULL,
57 DUMMY_TR("Do not create startup error log and display it "
60 {'h', "help", 0, NULL,
61 DUMMY_TR("Show this help")},
63 {'V', "version", 0, NULL,
64 DUMMY_TR("Show program version")},
66 {OPT_ID('a'), "about", 0, NULL,
67 DUMMY_TR("Show about text")},
73 void check_new_user_help()
75 const char *userdir=extl_userdir();
77 char *tmp=NULL, *cmd=NULL;
82 warn(TR("Could not get user configuration file directory."));
86 libtu_asprintf(&oldbeard, "%s/.welcome_msg_displayed", userdir);
91 if(access(oldbeard, F_OK)==0){
96 libtu_asprintf(&tmp, TR("%s/welcome.txt"), SHAREDIR);
99 if(access(tmp, F_OK)==0)
100 libtu_asprintf(&cmd, "%s %s", CF_XMESSAGE, tmp);
102 libtu_asprintf(&cmd, "%s %s/welcome.txt", CF_XMESSAGE, SHAREDIR);
107 ret=ioncore_exec(cmd);
112 /* This should actually be done when less or xmessage returns,
113 * but that would mean yet another script...
115 mkdir(userdir, 0700);
116 if(open(oldbeard, O_CREAT|O_RDWR, 0600)<0)
117 warn_err_obj(oldbeard);
129 printf(TR("Usage: %s [options]\n\n"), libtu_progname());
130 for(i=0; ion_opts[i].descr!=NULL; i++)
131 ion_opts[i].descr=TR(ion_opts[i].descr);
132 optparser_printhelp(OPTP_MIDLONG, ion_opts);
137 int main(int argc, char*argv[])
139 const char *cfgfile="cfg_ion";
140 const char *display=NULL;
147 bool may_continue=FALSE;
148 bool noerrorlog=FALSE;
152 if(!ioncore_init("ion3", argc, argv, LOCALEDIR))
155 extl_add_searchdir(EXTRABINDIR); /* ion-completefile */
156 extl_add_searchdir(MODULEDIR);
157 extl_add_searchdir(ETCDIR);
158 extl_add_searchdir(SHAREDIR);
159 extl_add_searchdir(LCDIR);
160 extl_set_userdirs("ion3");
162 optparser_init(argc, argv, OPTP_MIDLONG, ion_opts);
164 while((opt=optparser_get_opt())){
167 display=optparser_get_arg();
170 cfgfile=optparser_get_arg();
173 extl_add_searchdir(optparser_get_arg());
176 ioncore_g.sm_client_id=optparser_get_arg();
179 stflags|=IONCORE_STARTUP_ONEROOT;
182 extl_set_sessiondir(optparser_get_arg());
191 printf("%s\n", ION_VERSION);
194 printf("%s\n", ioncore_aboutmsg());
197 warn(TR("Invalid command line."));
204 /* We may have to pass the file to xmessage so just using tmpfile()
207 libtu_asprintf(&efnam, "%s/ion-%d-startup-errorlog", P_tmpdir,
212 ef=fopen(efnam, "wt");
218 cloexec_braindamage_fix(fileno(ef));
219 fprintf(ef, TR("Ion startup error log:\n"));
220 errorlog_begin_file(&el, ef);
225 if(ioncore_startup(display, cfgfile, stflags))
230 warn(TR("Refusing to start due to encountered errors."));
232 check_new_user_help();
236 if(errorlog_end(&el) && ioncore_g.dpy!=NULL){
240 ioncore_setup_display(DefaultScreen(ioncore_g.dpy));
242 XCloseDisplay(ioncore_g.dpy);
244 close(ioncore_g.conn);
245 libtu_asprintf(&cmd, CF_XMESSAGE " %s", efnam);
248 }else if(system(cmd)==-1){
254 if(!may_continue && pid>0)
255 waitpid(pid, NULL, 0);
269 /* The code should never return here */