4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * Ion is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
17 #include <sys/types.h>
22 #include <libtu/util.h>
23 #include <libtu/optparser.h>
24 #include <libtu/errorlog.h>
25 #include <libextl/readconfig.h>
26 #include <libmainloop/exec.h>
28 #include <ioncore/common.h>
29 #include <ioncore/global.h>
30 #include <ioncore/ioncore.h>
31 #include <ioncore/exec.h>
32 #include <ioncore/event.h>
33 #include "../version.h"
36 /* Options. Getopt is not used because getopt_long is quite gnu-specific
37 * and they don't know of '-display foo' -style args anyway.
38 * Instead, I've reinvented the wheel in libtu :(.
40 static OptParserOpt ion_opts[]={
41 {OPT_ID('d'), "display", OPT_ARG, "host:dpy.scr",
42 DUMMY_TR("X display to use")},
44 {'c', "conffile", OPT_ARG, "config_file",
45 DUMMY_TR("Configuration file")},
47 {'s', "searchdir", OPT_ARG, "dir",
48 DUMMY_TR("Add directory to search path")},
50 {OPT_ID('o'), "oneroot", 0, NULL,
51 DUMMY_TR("Manage default screen only")},
53 {OPT_ID('s'), "session", OPT_ARG, "session_name",
54 DUMMY_TR("Name of session (affects savefiles)")},
56 {OPT_ID('S'), "smclientid", OPT_ARG, "client_id",
57 DUMMY_TR("Session manager client ID")},
59 {OPT_ID('N'), "noerrorlog", 0, NULL,
60 DUMMY_TR("Do not create startup error log and display it "
63 {'h', "help", 0, NULL,
64 DUMMY_TR("Show this help")},
66 {'V', "version", 0, NULL,
67 DUMMY_TR("Show program version")},
69 {OPT_ID('a'), "about", 0, NULL,
70 DUMMY_TR("Show about text")},
76 void check_new_user_help()
78 const char *userdir=extl_userdir();
80 char *tmp=NULL, *cmd=NULL;
85 warn(TR("Could not get user configuration file directory."));
89 libtu_asprintf(&oldbeard, "%s/.welcome_msg_displayed", userdir);
94 if(access(oldbeard, F_OK)==0){
99 libtu_asprintf(&tmp, TR("%s/welcome.txt"), SHAREDIR);
102 if(access(tmp, F_OK)==0)
103 libtu_asprintf(&cmd, "%s %s", CF_XMESSAGE, tmp);
105 libtu_asprintf(&cmd, "%s %s/welcome.txt", CF_XMESSAGE, SHAREDIR);
110 ret=ioncore_exec(cmd);
115 /* This should actually be done when less or xmessage returns,
116 * but that would mean yet another script...
118 mkdir(userdir, 0700);
119 if(open(oldbeard, O_CREAT|O_RDWR, 0600)<0)
120 warn_err_obj(oldbeard);
132 printf(TR("Usage: %s [options]\n\n"), libtu_progname());
133 for(i=0; ion_opts[i].descr!=NULL; i++)
134 ion_opts[i].descr=TR(ion_opts[i].descr);
135 optparser_printhelp(OPTP_MIDLONG, ion_opts);
140 int main(int argc, char*argv[])
142 const char *cfgfile="cfg_ion";
143 const char *display=NULL;
150 bool may_continue=FALSE;
151 bool noerrorlog=FALSE;
155 if(!ioncore_init("ion3", argc, argv, LOCALEDIR))
158 extl_add_searchdir(EXTRABINDIR); /* ion-completefile */
159 extl_add_searchdir(MODULEDIR);
160 extl_add_searchdir(ETCDIR);
161 extl_add_searchdir(SHAREDIR);
162 extl_add_searchdir(LCDIR);
163 extl_set_userdirs("ion3");
165 optparser_init(argc, argv, OPTP_MIDLONG, ion_opts);
167 while((opt=optparser_get_opt())){
170 display=optparser_get_arg();
173 cfgfile=optparser_get_arg();
176 extl_add_searchdir(optparser_get_arg());
179 ioncore_g.sm_client_id=optparser_get_arg();
182 stflags|=IONCORE_STARTUP_ONEROOT;
185 extl_set_sessiondir(optparser_get_arg());
194 printf("%s\n", ION_VERSION);
197 printf("%s\n", ioncore_aboutmsg());
200 warn(TR("Invalid command line."));
207 /* We may have to pass the file to xmessage so just using tmpfile()
210 libtu_asprintf(&efnam, "%s/ion-%d-startup-errorlog", P_tmpdir,
215 ef=fopen(efnam, "wt");
221 cloexec_braindamage_fix(fileno(ef));
222 fprintf(ef, TR("Ion startup error log:\n"));
223 errorlog_begin_file(&el, ef);
228 if(ioncore_startup(display, cfgfile, stflags))
233 warn(TR("Refusing to start due to encountered errors."));
235 check_new_user_help();
239 if(errorlog_end(&el) && ioncore_g.dpy!=NULL){
243 ioncore_setup_display(DefaultScreen(ioncore_g.dpy));
245 XCloseDisplay(ioncore_g.dpy);
247 close(ioncore_g.conn);
248 libtu_asprintf(&cmd, CF_XMESSAGE " %s", efnam);
251 }else if(system(cmd)==-1){
257 if(!may_continue && pid>0)
258 waitpid(pid, NULL, 0);
272 /* The code should never return here */