]> git.decadent.org.uk Git - ion3.git/blob - mod_statusbar/ion-statusd/ion-statusd.c
[svn-inject] Installing original source of ion3
[ion3.git] / mod_statusbar / ion-statusd / ion-statusd.c
1 /*
2  * ion/mod_statusbar/ion-statusd/ion-statusd.c
3  *
4  * Copyright (c) Tuomo Valkonen 2004-2006.
5  *
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.
10  */
11
12 #include <string.h>
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16
17 #include <libtu/util.h>
18 #include <libtu/optparser.h>
19 #include <libtu/errorlog.h>
20 #include <libtu/locale.h>
21 #include <libtu/misc.h>
22 #include <libextl/readconfig.h>
23 #include <libmainloop/select.h>
24 #include <libmainloop/signal.h>
25 #include <libmainloop/defer.h>
26
27 #ifndef CF_NO_LOCALE
28 #include <locale.h>
29 #endif
30
31 #include "../../version.h"
32
33
34 static OptParserOpt ion_opts[]={
35     /*{OPT_ID('d'), "display",  OPT_ARG, "host:dpy.scr", 
36      DUMMY_TR("X display to use")},*/
37     
38     {'c', "conffile", OPT_ARG, "config_file", 
39      DUMMY_TR("Configuration file")},
40     
41     {'s', "searchdir", OPT_ARG, "dir", 
42      DUMMY_TR("Add directory to search path")},
43
44     /*{OPT_ID('s'), "session",  OPT_ARG, "session_name", 
45      DUMMY_TR("Name of session (affects savefiles)")},*/
46     
47     {'h', "help", 0, NULL, 
48      DUMMY_TR("Show this help")},
49     
50     {'V', "version", 0, NULL,
51      DUMMY_TR("Show program version")},
52     
53     {OPT_ID('a'), "about", 0, NULL,
54      DUMMY_TR("Show about text")},
55
56     {'q', "quiet", 0, NULL, 
57      DUMMY_TR("Quiet mode")},
58
59     {'m', "meter", OPT_ARG, "meter_module",
60      DUMMY_TR("Load a meter module")},
61
62     END_OPTPARSEROPTS
63 };
64
65
66 static const char statusd_copy[]=
67     "Ion-statusd " ION_VERSION ", copyright (c) Tuomo Valkonen 2004-2005.";
68
69
70 static const char statusd_license[]=DUMMY_TR(
71     "This program is free software; you can redistribute it and/or\n"
72     "modify it under the terms of the GNU Lesser General Public\n"
73     "License as published by the Free Software Foundation; either\n"
74     "version 2.1 of the License, or (at your option) any later version.\n"
75     "\n"
76     "This program is distributed in the hope that it will be useful,\n"
77     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
78     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
79     "Lesser General Public License for more details.\n");
80
81
82 /* new_informs=TRUE because we should always print period when 
83  * initialisation is done. 
84  */
85 static bool new_informs=TRUE;
86 static ExtlTab configtab;
87
88 static void help()
89 {
90     int i;
91     printf(TR("Usage: %s [options]\n\n"), prog_execname());
92     for(i=0; ion_opts[i].descr!=NULL; i++)
93         ion_opts[i].descr=TR(ion_opts[i].descr);
94     optparser_printhelp(OPTP_MIDLONG, ion_opts);
95     printf("\n");
96 }
97
98
99 static void flush_informs()
100 {
101     if(new_informs){
102         printf(".\n");
103         fflush(stdout);
104         new_informs=FALSE;
105     }
106 }
107
108
109 static void mainloop()
110 {
111     sigset_t trapset;
112     
113     sigemptyset(&trapset);
114     sigaddset(&trapset, SIGALRM);
115     sigaddset(&trapset, SIGCHLD);
116     mainloop_trap_signals(&trapset);
117     
118     while(1){
119         int kill_sig=mainloop_check_signals();
120         if(kill_sig!=0 && kill_sig!=SIGUSR1){
121             if(kill_sig==SIGTERM)
122                 exit(EXIT_FAILURE);
123             else
124                 kill(getpid(), kill_sig);
125         }
126
127         mainloop_execute_deferred();
128
129         flush_informs();
130
131         mainloop_select();
132     }
133 }
134
135
136 extern bool statusd_register_exports();
137 extern void statusd_unregister_exports();
138
139
140 static void stdout_closed(int fd, void *data)
141 {
142     exit(EXIT_SUCCESS);
143 }
144
145
146 int main(int argc, char*argv[])
147 {
148     const char *mod=NULL;
149     char *mod2=NULL;
150     int loaded=0;
151     int opt;
152     bool quiet=FALSE;
153
154 #ifndef CF_NO_LOCALE    
155     if(setlocale(LC_ALL, "")==NULL)
156         warn("setlocale() call failed.");
157 #endif
158
159     configtab=extl_table_none();
160     
161     libtu_init(argv[0]);
162     extl_init();
163
164     if(!statusd_register_exports())
165         return EXIT_FAILURE;
166
167     extl_add_searchdir(EXTRABINDIR);
168     extl_add_searchdir(MODULEDIR);
169     extl_add_searchdir(ETCDIR);
170     extl_add_searchdir(SHAREDIR);
171     extl_add_searchdir(LCDIR);
172     extl_set_userdirs("ion3");
173
174     optparser_init(argc, argv, OPTP_MIDLONG, ion_opts);
175     
176     extl_read_config("ioncore_luaext", NULL, TRUE);
177     
178     while((opt=optparser_get_opt())){
179         switch(opt){
180         /*case OPT_ID('d'):
181             display=optparser_get_arg();
182             break;*/
183         case 's':
184             extl_add_searchdir(optparser_get_arg());
185             break;
186         /*case OPT_ID('s'):
187             extl_set_sessiondir(optparser_get_arg());
188             break;*/
189         case 'h':
190             help();
191             return EXIT_SUCCESS;
192         case 'V':
193             printf("%s\n", ION_VERSION);
194             return EXIT_SUCCESS;
195         case OPT_ID('a'):
196             printf("%s\n\n%s", statusd_copy, TR(statusd_license));
197             return EXIT_SUCCESS;
198         case 'c':
199             {
200                 ExtlTab t;
201                 const char *f=optparser_get_arg();
202                 if(extl_read_savefile(f, &t)){
203                     extl_unref_table(configtab);
204                     configtab=t;
205                 }else{
206                     warn(TR("Unable to load configuration file %s"), f);
207                 }
208             }
209             break;
210         case 'q':
211             quiet=TRUE;
212             break;
213         case 'm':
214             mod=optparser_get_arg();
215             if(strchr(mod, '/')==NULL && strchr(mod, '.')==NULL){
216                 mod2=scat("statusd_", mod);
217                 if(mod2==NULL)
218                     return EXIT_FAILURE;
219                 mod=mod2;
220             }
221             if(extl_read_config(mod, NULL, !quiet))
222                 loaded++;
223             if(mod2!=NULL)
224                 free(mod2);
225             break;
226         default:
227             warn(TR("Invalid command line."));
228             help();
229             return EXIT_FAILURE;
230         }
231     }
232     
233     if(loaded==0 && !quiet){
234         warn(TR("No meters loaded."));
235         return EXIT_FAILURE;
236     }
237     
238     mainloop();
239     
240     return EXIT_SUCCESS;
241 }
242
243
244 /*EXTL_DOC
245  * Inform that meter \var{name} has value \var{value}.
246  */
247 EXTL_EXPORT
248 void statusd_inform(const char *name, const char *value)
249 {
250     printf("%s: %s\n", name, value);
251     new_informs=TRUE;
252 }
253
254
255 /*EXTL_DOC
256  * Get configuration table for module \var{name}
257  */
258 EXTL_EXPORT
259 ExtlTab statusd_get_config(const char *name)
260 {
261     if(name==NULL){
262         return extl_ref_table(configtab);
263     }else{
264         ExtlTab t;
265         if(extl_table_gets_t(configtab, name, &t))
266             return t;
267         else
268             return extl_create_table();
269     }
270 }
271
272
273 /*EXTL_DOC
274  * Get last file modification time.
275  */
276 EXTL_EXPORT
277 double statusd_last_modified(const char *fname)
278 {
279     struct stat st;
280     
281     if(fname==NULL)
282         return (double)(-1);
283     
284     if(stat(fname, &st)!=0){
285         /*warn_err_obj(fname);*/
286         return (double)(-1);
287     }
288     
289     return (double)(st.st_mtime>st.st_ctime ? st.st_mtime : st.st_ctime);
290 }
291