Consistency fixes
[notion.git] / notion / notion.c
blobf744dc98083b6b2907394364f3157301727450f9
1 /*
2 * notion/notion/notion.c
4 * Copyright (c) the Notion team 2013.
5 * Copyright (c) Tuomo Valkonen 1999-2009.
7 * See the included file LICENSE for details.
8 */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
20 #include <libtu/util.h>
21 #include <libtu/optparser.h>
22 #include <libtu/errorlog.h>
23 #include <libtu/prefix.h>
24 #include <libextl/readconfig.h>
25 #include <libmainloop/exec.h>
27 #include <ioncore/common.h>
28 #include <ioncore/global.h>
29 #include <ioncore/ioncore.h>
30 #include <ioncore/exec.h>
31 #include <ioncore/event.h>
32 #include "../version.h"
34 #ifndef P_tmpdir
35 #define P_tmpdir "/tmp"
36 #endif
38 /* Options. Getopt is not used because getopt_long is quite gnu-specific
39 * and they don't know of '-display foo' -style args anyway.
40 * Instead, I've reinvented the wheel in libtu :(.
42 static OptParserOpt ion_opts[]={
43 {OPT_ID('d'), "display", OPT_ARG, "host:dpy.scr",
44 DUMMY_TR("X display to use")},
46 {'c', "conffile", OPT_ARG, "config_file",
47 DUMMY_TR("Configuration file")},
49 {'s', "searchdir", OPT_ARG, "dir",
50 DUMMY_TR("Add directory to search path")},
52 {OPT_ID('o'), "oneroot", 0, NULL,
53 DUMMY_TR("Manage default screen only")},
55 {OPT_ID('s'), "session", OPT_ARG, "session_name",
56 DUMMY_TR("Name of session (affects savefiles)")},
58 {OPT_ID('S'), "smclientid", OPT_ARG, "client_id",
59 DUMMY_TR("Session manager client ID")},
61 {OPT_ID('N'), "noerrorlog", 0, NULL,
62 DUMMY_TR("Do not create startup error log and display it "
63 "with xmessage.")},
65 {'h', "help", 0, NULL,
66 DUMMY_TR("Show this help")},
68 {'V', "version", 0, NULL,
69 DUMMY_TR("Show program version")},
71 {OPT_ID('a'), "about", 0, NULL,
72 DUMMY_TR("Show about text")},
74 END_OPTPARSEROPTS
78 void check_new_user_help()
80 const char *userdir=extl_userdir();
81 char *oldbeard=NULL;
82 char *tmp=NULL, *cmd=NULL;
83 bool ret;
85 if(userdir==NULL){
86 warn(TR("Could not get user configuration file directory."));
87 return;
90 libtu_asprintf(&oldbeard, "%s/.welcome_msg_displayed", userdir);
92 if(oldbeard==NULL)
93 return;
95 if(access(oldbeard, F_OK)==0){
96 free(oldbeard);
97 return;
100 libtu_asprintf(&tmp, TR("%s/welcome.txt"), SHAREDIR);
102 if(tmp!=NULL){
103 if(access(tmp, F_OK)==0)
104 libtu_asprintf(&cmd, "%s %s", CF_XMESSAGE, tmp);
105 else
106 libtu_asprintf(&cmd, "%s %s/welcome.txt", CF_XMESSAGE, SHAREDIR);
108 free(tmp);
110 if(cmd!=NULL){
111 ret=ioncore_exec(cmd);
113 free(cmd);
115 if(ret){
116 /* This should actually be done when less or xmessage returns,
117 * but that would mean yet another script...
119 mkdir(userdir, 0700);
120 if(open(oldbeard, O_CREAT|O_RDWR, 0600)<0)
121 warn_err_obj(oldbeard);
126 free(oldbeard);
130 static void help()
132 int i;
133 printf(TR("Usage: %s [options]\n\n"), libtu_progname());
134 for(i=0; ion_opts[i].descr!=NULL; i++)
135 ion_opts[i].descr=TR(ion_opts[i].descr);
136 optparser_printhelp(OPTP_MIDLONG, ion_opts);
137 printf("\n");
141 int main(int argc, char*argv[])
143 const char *cfgfile="cfg_notion";
144 const char *display=NULL;
145 char *cmd=NULL;
146 int stflags=0;
147 int opt;
148 ErrorLog el;
149 FILE *ef=NULL;
150 char *efnam=NULL;
151 bool may_continue=FALSE;
152 bool noerrorlog=FALSE;
153 char *localedir;
155 libtu_init(argv[0]);
157 #ifdef CF_RELOCATABLE_BIN_LOCATION
158 prefix_set(argv[0], CF_RELOCATABLE_BIN_LOCATION);
159 #endif
161 localedir=prefix_add(LOCALEDIR);
163 if(!ioncore_init(CF_EXECUTABLE, argc, argv, localedir))
164 return EXIT_FAILURE;
166 if(localedir!=NULL)
167 free(localedir);
169 prefix_wrap_simple(extl_add_searchdir, EXTRABINDIR); /* ion-completefile */
170 prefix_wrap_simple(extl_add_searchdir, MODULEDIR);
171 prefix_wrap_simple(extl_add_searchdir, ETCDIR);
172 prefix_wrap_simple(extl_add_searchdir, SHAREDIR);
173 prefix_wrap_simple(extl_add_searchdir, LCDIR);
174 extl_set_userdirs(CF_EXECUTABLE);
176 optparser_init(argc, argv, OPTP_MIDLONG, ion_opts);
178 while((opt=optparser_get_opt())){
179 switch(opt){
180 case OPT_ID('d'):
181 display=optparser_get_arg();
182 break;
183 case 'c':
184 cfgfile=optparser_get_arg();
185 break;
186 case 's':
187 extl_add_searchdir(optparser_get_arg());
188 break;
189 case OPT_ID('S'):
190 ioncore_g.sm_client_id=optparser_get_arg();
191 break;
192 case OPT_ID('o'):
193 stflags|=IONCORE_STARTUP_ONEROOT;
194 break;
195 case OPT_ID('s'):
196 extl_set_sessiondir(optparser_get_arg());
197 break;
198 case OPT_ID('N'):
199 noerrorlog=TRUE;
200 break;
201 case 'h':
202 help();
203 return EXIT_SUCCESS;
204 case 'V':
205 printf("%s\n", ION_VERSION);
206 return EXIT_SUCCESS;
207 case OPT_ID('a'):
208 printf("%s\n", ioncore_aboutmsg());
209 return EXIT_SUCCESS;
210 default:
211 warn(TR("Invalid command line."));
212 help();
213 return EXIT_FAILURE;
217 if(!noerrorlog){
218 /* We may have to pass the file to xmessage so just using tmpfile()
219 * isn't sufficient.
221 libtu_asprintf(&efnam, "%s/ion-%d-startup-errorlog", P_tmpdir,
222 getpid());
223 if(efnam==NULL){
224 warn_err();
225 }else{
226 ef=fopen(efnam, "wt");
227 if(ef==NULL){
228 warn_err_obj(efnam);
229 free(efnam);
230 efnam=NULL;
231 }else{
232 cloexec_braindamage_fix(fileno(ef));
233 fprintf(ef, TR("Notion startup error log:\n"));
234 errorlog_begin_file(&el, ef);
239 if(ioncore_startup(display, cfgfile, stflags))
240 may_continue=TRUE;
242 if(!may_continue)
243 warn(TR("Refusing to start due to encountered errors."));
244 else
245 check_new_user_help();
247 if(ef!=NULL){
248 pid_t pid=-1;
249 if(errorlog_end(&el) && ioncore_g.dpy!=NULL){
250 fclose(ef);
251 pid=fork();
252 if(pid==0){
253 ioncore_setup_display(DefaultScreen(ioncore_g.dpy));
254 if(!may_continue)
255 XCloseDisplay(ioncore_g.dpy);
256 else
257 close(ioncore_g.conn);
258 libtu_asprintf(&cmd, CF_XMESSAGE " %s", efnam);
259 if(cmd==NULL){
260 warn_err();
261 }else if(system(cmd)==-1){
262 warn_err_obj(cmd);
264 unlink(efnam);
265 exit(EXIT_SUCCESS);
267 if(!may_continue && pid>0)
268 waitpid(pid, NULL, 0);
269 }else{
270 fclose(ef);
272 if(pid<0)
273 unlink(efnam);
274 free(efnam);
277 if(!may_continue)
278 return EXIT_FAILURE;
280 ioncore_mainloop();
282 /* The code should never return here */
283 return EXIT_SUCCESS;