move conky.1
[monky] / src / conky.c
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * This program is licensed under BSD license, read COPYING
5  *
6  *  $Id$
7  */
8
9 #include "conky.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <time.h>
15 #include <locale.h>
16 #include <signal.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <limits.h>
22 #if HAVE_DIRENT_H
23 #include <dirent.h>
24 #endif
25 #include <sys/time.h>
26 #ifdef X11
27 #include <X11/Xutil.h>
28 #endif /* X11 */
29 #include <sys/types.h>
30 #include <sys/stat.h>
31
32 #define CONFIG_FILE "$HOME/.conkyrc"
33 #define MAIL_FILE "$MAIL"
34 #define MAX_IF_BLOCK_DEPTH 5
35
36 #ifdef X11
37
38 /* alignments */
39 enum alignment {
40         TOP_LEFT = 1,
41         TOP_RIGHT,
42         BOTTOM_LEFT,
43         BOTTOM_RIGHT,
44         NONE
45 };
46
47
48 /* for fonts */
49 struct font_list {
50
51         char name[TEXT_BUFFER_SIZE];
52         int num;
53         XFontStruct *font;
54
55 #ifdef XFT
56         XftFont *xftfont;
57         int font_alpha;
58 #endif  
59
60 };
61 static int selected_font = 0;
62 static int font_count = -1;
63 struct font_list *fonts = NULL;
64
65 #ifdef XFT
66
67 #define font_height() use_xft ? (fonts[selected_font].xftfont->ascent + fonts[selected_font].xftfont->descent) : \
68 (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
69 #define font_ascent() use_xft ? fonts[selected_font].xftfont->ascent : fonts[selected_font].font->max_bounds.ascent
70 #define font_descent() use_xft ? fonts[selected_font].xftfont->descent : fonts[selected_font].font->max_bounds.descent
71
72 #else
73
74 #define font_height() (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
75 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
76 #define font_descent() fonts[selected_font].font->max_bounds.descent
77
78 #endif
79
80 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
81
82
83 static void set_font();
84
85
86 int addfont(const char *data_in)
87 {
88         if (font_count > MAX_FONTS) {
89                 CRIT_ERR("you don't need that many fonts, sorry.");
90         }
91         font_count++;
92         if (font_count == 0) {
93                 if (fonts != NULL) {
94                         free(fonts);
95                 }
96                 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
97                         CRIT_ERR("malloc");
98                 }
99         }
100         fonts = realloc(fonts, (sizeof(struct font_list) * (font_count+1)));
101         if (fonts == NULL) {
102                 CRIT_ERR("realloc in addfont");
103         }
104         if (strlen(data_in) < TEXT_BUFFER_SIZE) { // must account for null terminator
105                 strncpy(fonts[font_count].name, data_in, TEXT_BUFFER_SIZE);
106 #ifdef XFT
107                 fonts[font_count].font_alpha = 0xffff;
108 #endif
109         } else {
110                 CRIT_ERR("Oops...looks like something overflowed in addfont().");
111         }
112         return font_count;
113 }
114
115 void set_first_font(const char *data_in)
116 {
117         if (font_count < 0) {
118                 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
119                         CRIT_ERR("malloc");
120                 }
121                 font_count++;
122         }
123         if (strlen(data_in) > 1) {
124                 strncpy(fonts[0].name, data_in, TEXT_BUFFER_SIZE-1);
125 #ifdef XFT
126                 fonts[0].font_alpha = 0xffff;
127 #endif
128         }
129 }
130
131 void free_fonts()
132 {
133         int i;
134         for (i=0;i<=font_count;i++) {
135 #ifdef XFT
136                 if (use_xft) {
137                         XftFontClose(display, fonts[i].xftfont);
138                 } else
139 #endif
140                 {
141                         XFreeFont(display, fonts[i].font);
142                 }
143 }
144         free(fonts);
145         fonts = NULL;
146         font_count = -1;
147         selected_font = 0;
148         set_first_font("6x10");
149 }
150
151
152 static void load_fonts()
153 {
154         int i;
155         for (i=0;i<=font_count;i++) {
156 #ifdef XFT
157         /* load Xft font */
158         if (use_xft) {
159         /*if (fonts[i].xftfont != NULL && selected_font == 0) {
160                         XftFontClose(display, fonts[i].xftfont);
161         }*/
162                 if ((fonts[i].xftfont =
163                         XftFontOpenName(display, screen, fonts[i].name)) != NULL)
164                         continue;
165                 
166                 ERR("can't load Xft font '%s'", fonts[i].name);
167                 if ((fonts[i].xftfont =
168                         XftFontOpenName(display, screen,
169                                         "courier-12")) != NULL)
170                         continue;
171                 
172                 ERR("can't load Xft font '%s'", "courier-12");
173                 
174                 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
175                         CRIT_ERR("can't load font '%s'", "fixed");
176                 }
177                 use_xft = 0;
178                 
179                 continue;
180         }
181 #endif
182         /* load normal font */
183 /*      if (fonts[i].font != NULL)
184                 XFreeFont(display, fonts[i].font);*/
185         
186         if ((fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
187                 ERR("can't load font '%s'", fonts[i].name);
188                 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
189                         CRIT_ERR("can't load font '%s'", "fixed");
190                 }
191         }
192         }
193 }
194
195 #endif /* X11 */
196
197 /* default config file */
198 static char *current_config;
199
200 /* set to 1 if you want all text to be in uppercase */
201 static unsigned int stuff_in_upper_case;
202
203 /* Update interval */
204 static double update_interval;
205
206 /* Run how many times? */
207 static unsigned long total_run_times;
208
209 /* fork? */
210 static int fork_to_background;
211
212 static int cpu_avg_samples, net_avg_samples;
213
214 #ifdef X11
215
216 /* Always on bottom */
217 static int on_bottom;
218
219 /* Position on the screen */
220 static int text_alignment;
221 static int gap_x, gap_y;
222
223 /* border */
224 static int draw_borders;
225 static int stippled_borders;
226
227 static int draw_shades, draw_outline;
228
229 static int border_margin, border_width;
230
231 static long default_fg_color, default_bg_color, default_out_color;
232
233 /* create own window or draw stuff to root? */
234 static int own_window = 0;
235
236 #ifdef OWN_WINDOW
237 /* fixed size/pos is set if wm/user changes them */
238 static int fixed_size = 0, fixed_pos = 0;
239 #endif
240
241 static int minimum_width, minimum_height;
242
243 /* UTF-8 */
244 int utf8_mode = 0;
245
246 #endif /* X11 */
247
248 /* no buffers in used memory? */
249 int no_buffers;
250
251 /* pad percentages to decimals? */
252 static int pad_percents = 0;
253
254 /* Text that is shown */
255 static char original_text[] =
256     "$nodename - $sysname $kernel on $machine\n"
257     "$hr\n"
258     "${color grey}Uptime:$color $uptime\n"
259     "${color grey}Frequency (in MHz):$color $freq\n"
260     "${color grey}Frequency (in Ghz):$color $freq_g\n"
261     "${color grey}RAM Usage:$color $mem/$memmax - $memperc% ${membar 4}\n"
262     "${color grey}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar 4}\n"
263     "${color grey}CPU Usage:$color $cpu% ${cpubar 4}\n"
264     "${color grey}Processes:$color $processes  ${color grey}Running:$color $running_processes\n"
265     "$hr\n"
266     "${color grey}File systems:\n"
267     " / $color${fs_free /}/${fs_size /} ${fs_bar 6 /}\n"
268     "${color grey}Networking:\n"
269     " Up:$color ${upspeed eth0} k/s${color grey} - Down:$color ${downspeed eth0} k/s\n"
270     "${color grey}Temperatures:\n"
271     " CPU:$color ${i2c temp 1}°C${color grey} - MB:$color ${i2c temp 2}°C\n"
272     "$hr\n"
273 #ifdef SETI
274     "${color grey}SETI@Home Statistics:\n"
275     "${color grey}Seti Unit Number:$color $seti_credit\n"
276     "${color grey}Seti Progress:$color $seti_prog% $seti_progbar\n"
277 #endif
278 #ifdef MPD
279     "${color grey}MPD: $mpd_status $mpd_artist - $mpd_title from $mpd_album at $mpd_vol\n"
280     "Bitrate: $mpd_bitrate\n" "Progress: $mpd_bar\n"
281 #endif
282     "${color grey}Name          PID     CPU%    MEM%\n"
283     " ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}\n"
284     " ${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}\n"
285     " ${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}\n"
286     " ${color lightgrey} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}\n"
287     "${tail /var/log/Xorg.0.log 3}";
288
289 static char *text = original_text;
290
291 static int total_updates;
292
293 /* if-blocks */
294 static int blockdepth = 0;
295 static int if_jumped = 0;
296 static int blockstart[MAX_IF_BLOCK_DEPTH];
297
298 int check_mount(char *s)
299 {
300         int ret = 0;
301         FILE *mtab = fopen("/etc/mtab", "r");
302         if (mtab) {
303                 char buf1[256], buf2[128];
304                 while (fgets(buf1, 256, mtab)) {
305                         sscanf(buf1, "%*s %128s", buf2);
306                         if (!strcmp(s, buf2)) {
307                                 ret = 1;
308                                 break;
309                         }
310                 }
311                 fclose(mtab);
312         } else {
313                 ERR("Could not open mtab");
314         }
315         return ret;
316 }
317
318
319 #ifdef X11
320 static inline int calc_text_width(const char *s, unsigned int l)
321 {
322 #ifdef XFT
323         if (use_xft) {
324                 XGlyphInfo gi;
325                 if (utf8_mode) {
326                         XftTextExtentsUtf8(display, fonts[selected_font].xftfont, s, l, &gi);
327                 } else {
328                         XftTextExtents8(display, fonts[selected_font].xftfont, s, l, &gi);
329                 }
330                 return gi.xOff;
331         } else
332 #endif
333         {
334                 return XTextWidth(fonts[selected_font].font, s, l);
335         }
336 }
337 #endif /* X11 */
338
339 /* formatted text to render on screen, generated in generate_text(),
340  * drawn in draw_stuff() */
341
342 static char text_buffer[TEXT_BUFFER_SIZE * 4];
343
344 /* special stuff in text_buffer */
345
346 #define SPECIAL_CHAR '\x01'
347
348 enum {
349         HORIZONTAL_LINE,
350         STIPPLED_HR,
351         BAR,
352         FG,
353         BG,
354         OUTLINE,
355         ALIGNR,
356         ALIGNC,
357         GRAPH,
358         OFFSET,
359         VOFFSET,
360         FONT,
361 };
362
363 static struct special_t {
364         int type;
365         short height;
366         short width;
367         long arg;
368         double *graph;
369         double graph_scale;
370         int graph_width;
371         int scaled;
372         short font_added;
373         unsigned long first_colour; // for graph gradient
374         unsigned long last_colour;
375 } specials[128];
376
377 static int special_count;
378 #ifdef X11
379 static int special_index;       /* used when drawing */
380 #endif /* X11 */
381
382 #define MAX_GRAPH_DEPTH 256     /* why 256? who knows. */
383
384 static struct special_t *new_special(char *buf, int t)
385 {
386         if (special_count >= 128)
387                 CRIT_ERR("too many special things in text");
388
389         buf[0] = SPECIAL_CHAR;
390         buf[1] = '\0';
391         if (t == GRAPH && specials[special_count].graph == NULL) {
392                 if (specials[special_count].width > 0
393                     && specials[special_count].width < MAX_GRAPH_DEPTH)
394                         specials[special_count].graph_width = specials[special_count].width - 3;        // subtract 3 for the box
395                 else
396                         specials[special_count].graph_width =
397                             MAX_GRAPH_DEPTH;
398                 specials[special_count].graph =
399                     calloc(specials[special_count].graph_width,
400                            sizeof(double));
401                 specials[special_count].graph_scale = 100;
402         }
403         specials[special_count].type = t;
404         return &specials[special_count++];
405 }
406
407 typedef struct tailstring_list {
408         char data[TEXT_BUFFER_SIZE];
409         struct tailstring_list *next;
410         struct tailstring_list *first;
411 } tailstring;
412
413 void addtail(tailstring ** head, char *data_in)
414 {
415         tailstring *tmp;
416         if ((tmp = malloc(sizeof(*tmp))) == NULL) {
417                 CRIT_ERR("malloc");
418         }
419         if (*head == NULL) {
420                 tmp->first = tmp;
421         } else {
422                 tmp->first = (*head)->first;
423         }
424         strncpy(tmp->data, data_in, TEXT_BUFFER_SIZE);
425         tmp->next = *head;
426         *head = tmp;
427 }
428
429 void freetail(tailstring * head)
430 {
431         tailstring *tmp;
432         while (head != NULL) {
433                 tmp = head->next;
434                 free(head);
435                 head = tmp;
436         }
437 }
438
439 void freelasttail(tailstring * head)
440 {
441         tailstring * tmp = head;
442         while(tmp != NULL) {
443                 if (tmp->next == head->first) {
444                         tmp->next = NULL;
445                         break;
446                 }
447                 tmp = tmp->next;
448         }
449         free(head->first);
450         while(head != NULL && tmp != NULL) {
451                 head->first = tmp;
452                 head = head->next;
453         }
454 }
455
456 static void new_bar(char *buf, int w, int h, int usage)
457 {
458         struct special_t *s = new_special(buf, BAR);
459         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
460         s->width = w;
461         s->height = h;
462 }
463
464 static const char *scan_bar(const char *args, int *w, int *h)
465 {
466         *w = 0;                 /* zero width means all space that is available */
467         *h = 6;
468         /* bar's argument is either height or height,width */
469         if (args) {
470                 int n = 0;
471                 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1)
472                         sscanf(args, "%d %n", h, &n);
473                 args += n;
474         }
475
476         return args;
477 }
478
479 static char *scan_font(const char *args)
480 {
481         if (args && sizeof(args) < 127) {
482                 return strdup(args);
483         }
484         else {
485                 ERR("font scan failed, lets hope it doesn't mess stuff up");
486         }
487         return NULL;
488 }
489
490 #ifdef X11
491 static void new_font(char *buf, char * args) {
492         struct special_t *s = new_special(buf, FONT);
493         if (!s->font_added || strcmp(args, fonts[s->font_added].name)) {
494                 int tmp = selected_font;
495                 selected_font = s->font_added = addfont(args);
496                 load_fonts();
497                 set_font();
498                 selected_font = tmp;
499         }
500 }
501 #endif
502
503 inline void graph_append(struct special_t *graph, double f)
504 {
505         int i;
506         if (graph->scaled) {
507                 graph->graph_scale = 0;
508         }
509         graph->graph[graph->graph_width - 1] = f; /* add new data */
510         for (i = 0; i < graph->graph_width - 1; i++) { /* shift all the data by 1 */
511                 graph->graph[i] = graph->graph[i + 1];
512                 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
513                         graph->graph_scale = graph->graph[i]; /* check if we need to update the scale */
514                 }
515         }
516 }
517
518 static void new_graph(char *buf, int w, int h, unsigned int first_colour, unsigned int second_colour, double i, int scaled)
519 {
520         struct special_t *s = new_special(buf, GRAPH);
521         s->width = w;
522         s->height = h;
523         s->first_colour = first_colour;
524         s->last_colour = second_colour;
525         s->scaled = scaled;
526         if (s->width) {
527                 s->graph_width = s->width - 3;  // subtract 3 for rectangle around
528         }
529         if (scaled) {
530                 s->graph_scale = 1;
531         } else {
532                 s->graph_scale = 100;
533         }
534         graph_append(s, i);
535 }
536
537 static const char *scan_graph(const char *args, int *w, int *h, unsigned int *first_colour, unsigned int *last_colour)
538 {
539         *w = 0;                 /* zero width means all space that is available */
540         *h = 25;
541         *first_colour = 0;
542         *last_colour = 0;
543         /* graph's argument is either height or height,width */
544         if (args) {
545                 if (sscanf(args, "%*s %d,%d %x %x", h, w, first_colour, last_colour) < 4) {
546                         if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) < 4) {
547                                 *w = 0;
548                                 *h = 25;                        
549                                 if (sscanf(args, "%*s %x %x", first_colour, last_colour) < 3) {
550                                 *w = 0;
551                                 *h = 25;
552                                 if (sscanf(args, "%x %x", first_colour, last_colour) < 2) {
553                                         *first_colour = 0;
554                                         *last_colour = 0;
555                                         if (sscanf(args, "%d,%d", h, w) < 2) {
556                                                 *first_colour = 0;
557                                                 *last_colour = 0;
558                                                 sscanf(args, "%*s %d,%d", h, w);
559                                         }
560                                 }
561                         }
562                         }
563                 }
564         }
565
566         return args;
567 }
568
569
570 static inline void new_hr(char *buf, int a)
571 {
572         new_special(buf, HORIZONTAL_LINE)->height = a;
573 }
574
575 static inline void new_stippled_hr(char *buf, int a, int b)
576 {
577         struct special_t *s = new_special(buf, STIPPLED_HR);
578         s->height = b;
579         s->arg = a;
580 }
581
582 static inline void new_fg(char *buf, long c)
583 {
584         new_special(buf, FG)->arg = c;
585 }
586
587 static inline void new_bg(char *buf, long c)
588 {
589         new_special(buf, BG)->arg = c;
590 }
591
592 static inline void new_outline(char *buf, long c)
593 {
594         new_special(buf, OUTLINE)->arg = c;
595 }
596
597 static inline void new_offset(char *buf, long c)
598 {
599         new_special(buf, OFFSET)->arg = c;
600 }
601
602 static inline void new_voffset(char *buf, long c)
603 {
604         new_special(buf, VOFFSET)->arg = c;
605 }
606
607 static inline void new_alignr(char *buf, long c)
608 {
609         new_special(buf, ALIGNR)->arg = c;
610 }
611
612 static inline void new_alignc(char *buf, long c)
613 {
614         new_special(buf, ALIGNC)->arg = c;
615 }
616
617 /* quite boring functions */
618
619 static inline void for_each_line(char *b, void (*f) (char *))
620 {
621         char *ps, *pe;
622
623         for (ps = b, pe = b; *pe; pe++) {
624                 if (*pe == '\n') {
625                         *pe = '\0';
626                         f(ps);
627                         *pe = '\n';
628                         ps = pe + 1;
629                 }
630         }
631
632         if (ps < pe)
633                 f(ps);
634 }
635
636 static void convert_escapes(char *buf)
637 {
638         char *p = buf, *s = buf;
639
640         while (*s) {
641                 if (*s == '\\') {
642                         s++;
643                         if (*s == 'n')
644                                 *p++ = '\n';
645                         else if (*s == '\\')
646                                 *p++ = '\\';
647                         s++;
648                 } else
649                         *p++ = *s++;
650         }
651         *p = '\0';
652 }
653
654 /* converts from bytes to human readable format (k, M, G, T) */
655 static void human_readable(long long a, char *buf, int size)
656 {
657         //Strange conditional due to possible overflows
658         if(a / 1024 / 1024 / 1024.0 > 1024.0){
659                 snprintf(buf, size, "%.2fT", (a / 1024 / 1024) / 1024 / 1024.0);
660         }
661         else if (a >= 1024 * 1024 * 1024) {
662                 snprintf(buf, size, "%.2fG", (a / 1024 / 1024) / 1024.0);
663         }
664         else if (a >= 1024 * 1024) {
665                 double m = (a / 1024) / 1024.0;
666                 if (m >= 100.0)
667                         snprintf(buf, size, "%.0fM", m);
668                 else
669                         snprintf(buf, size, "%.1fM", m);
670         } else if (a >= 1024)
671                 snprintf(buf, size, "%Ldk", a / (long long) 1024);
672         else
673                 snprintf(buf, size, "%Ld", a);
674 }
675
676 /* text handling */
677
678 enum text_object_type {
679         OBJ_acpiacadapter,
680         OBJ_adt746xcpu,
681         OBJ_adt746xfan,
682         OBJ_acpifan,
683         OBJ_addr,
684         OBJ_linkstatus,
685         OBJ_acpitemp,
686         OBJ_acpitempf,
687         OBJ_battery,
688         OBJ_buffers,
689         OBJ_cached,
690         OBJ_color,
691         OBJ_font,
692         OBJ_cpu,
693         OBJ_cpubar,
694         OBJ_cpugraph,
695         OBJ_downspeed,
696         OBJ_downspeedf,
697         OBJ_downspeedgraph,
698         OBJ_else,
699         OBJ_endif,
700         OBJ_exec,
701         OBJ_execi,
702         OBJ_execbar,
703         OBJ_execgraph,
704         OBJ_freq,
705         OBJ_freq_g,
706         OBJ_fs_bar,
707         OBJ_fs_bar_free,
708         OBJ_fs_free,
709         OBJ_fs_free_perc,
710         OBJ_fs_size,
711         OBJ_fs_used,
712         OBJ_fs_used_perc,
713         OBJ_hr,
714         OBJ_offset,
715         OBJ_voffset,
716         OBJ_alignr,
717         OBJ_alignc,
718         OBJ_i2c,
719         OBJ_if_existing,
720         OBJ_if_mounted,
721         OBJ_if_running,
722         OBJ_top,
723         OBJ_top_mem,
724         OBJ_tail,
725         OBJ_kernel,
726         OBJ_loadavg,
727         OBJ_machine,
728         OBJ_mails,
729         OBJ_mem,
730         OBJ_membar,
731         OBJ_memgraph,
732         OBJ_memmax,
733         OBJ_memperc,
734         OBJ_mixer,
735         OBJ_mixerl,
736         OBJ_mixerr,
737         OBJ_mixerbar,
738         OBJ_mixerlbar,
739         OBJ_mixerrbar,
740         OBJ_new_mails,
741         OBJ_nodename,
742         OBJ_pre_exec,
743 #ifdef MLDONKEY
744         OBJ_ml_upload_counter,
745         OBJ_ml_download_counter,
746         OBJ_ml_nshared_files,
747         OBJ_ml_shared_counter,
748         OBJ_ml_tcp_upload_rate,
749         OBJ_ml_tcp_download_rate,
750         OBJ_ml_udp_upload_rate,
751         OBJ_ml_udp_download_rate,
752         OBJ_ml_ndownloaded_files,
753         OBJ_ml_ndownloading_files,
754 #endif
755         OBJ_processes,
756         OBJ_running_processes,
757         OBJ_shadecolor,
758         OBJ_outlinecolor,
759         OBJ_stippled_hr,
760         OBJ_swap,
761         OBJ_swapbar,
762         OBJ_swapmax,
763         OBJ_swapperc,
764         OBJ_sysname,
765         OBJ_temp1,              /* i2c is used instead in these */
766         OBJ_temp2,
767         OBJ_text,
768         OBJ_time,
769         OBJ_utime,
770         OBJ_totaldown,
771         OBJ_totalup,
772         OBJ_updates,
773         OBJ_upspeed,
774         OBJ_upspeedf,
775         OBJ_upspeedgraph,
776         OBJ_uptime,
777         OBJ_uptime_short,
778 #ifdef SETI
779         OBJ_seti_prog,
780         OBJ_seti_progbar,
781         OBJ_seti_credit,
782 #endif
783 #ifdef MPD
784         OBJ_mpd_title,
785         OBJ_mpd_artist,
786         OBJ_mpd_album,
787         OBJ_mpd_vol,
788         OBJ_mpd_bitrate,
789         OBJ_mpd_status,
790         OBJ_mpd_host,
791         OBJ_mpd_port,
792         OBJ_mpd_bar,
793         OBJ_mpd_elapsed,
794         OBJ_mpd_length,
795         OBJ_mpd_percent,
796 #endif
797 };
798
799 struct text_object {
800         int type;
801         int a, b;
802         unsigned int c, d;
803         union {
804                 char *s;        /* some string */
805                 int i;          /* some integer */
806                 long l;         /* some other integer */
807                 struct net_stat *net;
808                 struct fs_stat *fs;
809                 unsigned char loadavg[3];
810
811                 struct {
812                         struct fs_stat *fs;
813                         int w, h;
814                 } fsbar;        /* 3 */
815
816                 struct {
817                         int l;
818                         int w, h;
819                 } mixerbar;     /* 3 */
820
821                 struct {
822                         int fd;
823                         int arg;
824                         char devtype[256];
825                         char type[64];
826                 } i2c;          /* 2 */
827                 struct {
828                         int pos;
829                         char *s;
830                 } ifblock;
831                 struct {
832                         int num;
833                         int type;
834                 } top;
835
836                 struct {
837                         int wantedlines;
838                         int readlines;
839                         char *logfile;
840                         double last_update;
841                         float interval;
842                         char *buffer;
843                 } tail;
844
845                 struct {
846                         double last_update;
847                         float interval;
848                         char *cmd;
849                         char *buffer;
850                 } execi;        /* 5 */
851
852                 struct {
853                         int a, b;
854                 } pair;         /* 2 */
855         } data;
856 };
857
858 static unsigned int text_object_count;
859 static struct text_object *text_objects;
860
861 /* new_text_object() allocates a new zeroed text_object */
862 static struct text_object *new_text_object()
863 {
864         text_object_count++;
865         text_objects = (struct text_object *) realloc(text_objects,
866                                                       sizeof(struct
867                                                              text_object) *
868                                                       text_object_count);
869         memset(&text_objects[text_object_count - 1], 0,
870                sizeof(struct text_object));
871
872         return &text_objects[text_object_count - 1];
873 }
874
875 static void free_text_objects()
876 {
877         unsigned int i;
878
879         for (i = 0; i < text_object_count; i++) {
880                 switch (text_objects[i].type) {
881                 case OBJ_acpitemp:
882                         close(text_objects[i].data.i);
883                         break;
884                 case OBJ_acpitempf:
885                         close(text_objects[i].data.i);
886                         break;
887                 case OBJ_i2c:
888                         close(text_objects[i].data.i2c.fd);
889                         break;
890                 case OBJ_time:
891                 case OBJ_utime:
892                 case OBJ_if_existing:
893                 case OBJ_if_mounted:
894                 case OBJ_if_running:
895                         free(text_objects[i].data.ifblock.s);
896                         break;
897                 case OBJ_text:
898                 case OBJ_exec:
899                 case OBJ_execbar:
900                 case OBJ_execgraph:
901 #ifdef MPD
902                 case OBJ_mpd_title:
903                 case OBJ_mpd_artist:
904                 case OBJ_mpd_album:
905                 case OBJ_mpd_status:
906                 case OBJ_mpd_host:
907 #endif
908                 case OBJ_pre_exec:
909                 case OBJ_battery:
910                         free(text_objects[i].data.s);
911                         break;
912
913                 case OBJ_execi:
914                         free(text_objects[i].data.execi.cmd);
915                         free(text_objects[i].data.execi.buffer);
916                         break;
917                 }
918         }
919
920         free(text_objects);
921         text_objects = NULL;
922         text_object_count = 0;
923 }
924
925 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
926 {
927         char buf1[64];
928         int n;
929
930         if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
931                 *a = mixer_init(buf1);
932                 (void) scan_bar(arg + n, w, h);
933         } else {
934                 *a = mixer_init(0);
935                 (void) scan_bar(arg, w, h);
936         }
937 }
938
939 /* construct_text_object() creates a new text_object */
940 static void construct_text_object(const char *s, const char *arg)
941 {
942         struct text_object *obj = new_text_object();
943
944 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
945 #define END ; } } else
946
947 #ifdef X11      
948 if (s[0] == '#') {
949                 obj->type = OBJ_color;
950                 obj->data.l = get_x11_color(s);
951         } else
952 #endif /* X11 */
953         OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
954         END OBJ(acpitempf, 0) obj->data.i = open_acpi_temperature(arg);
955         END OBJ(acpiacadapter, 0)
956         END OBJ(freq, 0);
957         END OBJ(freq_g, 0);
958         END OBJ(acpifan, 0);
959         END OBJ(battery, 0);
960         char bat[64];
961         if (arg)
962                 sscanf(arg, "%63s", bat);
963         else
964                 strcpy(bat, "BAT0");
965         obj->data.s = strdup(bat);
966         END OBJ(buffers, INFO_BUFFERS)
967         END OBJ(cached, INFO_BUFFERS)
968         END OBJ(cpu, INFO_CPU)
969         END OBJ(cpubar, INFO_CPU)
970          (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
971         END OBJ(cpugraph, INFO_CPU)
972                         (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d);
973         END OBJ(color, 0) 
974 #ifdef X11
975                         obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
976 #endif /* X11 */
977         END
978                         OBJ(font, 0)
979                         obj->data.s = scan_font(arg);
980                         END
981                         OBJ(downspeed, INFO_NET) obj->data.net = get_net_stat(arg);
982         END OBJ(downspeedf, INFO_NET) obj->data.net = get_net_stat(arg);
983         END OBJ(downspeedgraph, INFO_NET)
984                         (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d);
985         char buf[64];
986         sscanf(arg, "%63s %*i,%*i %*i", buf);
987         obj->data.net = get_net_stat(buf);
988         if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
989                 if (sscanf(arg, "%*s %d,%d", &obj->b, &obj->a) <= 1) {
990                         obj->a = 0;
991                         obj->b = 25;
992                 }
993         }
994         END OBJ(
995                        else
996                        , 0)
997         if (blockdepth) {
998                 text_objects[blockstart[blockdepth - 1] -
999                              1].data.ifblock.pos = text_object_count;
1000                 blockstart[blockdepth - 1] = text_object_count;
1001                 obj->data.ifblock.pos = text_object_count + 2;
1002         } else {
1003                 ERR("$else: no matching $if_*");
1004         }
1005         END OBJ(endif, 0)
1006         if (blockdepth) {
1007                 blockdepth--;
1008                 text_objects[blockstart[blockdepth] - 1].data.ifblock.pos =
1009                     text_object_count;
1010         } else {
1011                 ERR("$endif: no matching $if_*");
1012         }
1013         END
1014 #ifdef HAVE_POPEN
1015             OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
1016         END OBJ(execbar, 0) obj->data.s = strdup(arg ? arg : "");
1017         END OBJ(execgraph, 0) obj->data.s = strdup(arg ? arg : "");
1018         END OBJ(execi, 0) unsigned int n;
1019
1020         if (!arg
1021             || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1022                 char buf[256];
1023                 ERR("${execi <interval> command}");
1024                 obj->type = OBJ_text;
1025                 snprintf(buf, 256, "${%s}", s);
1026                 obj->data.s = strdup(buf);
1027         } else {
1028                 obj->data.execi.cmd = strdup(arg + n);
1029                 obj->data.execi.buffer =
1030                     (char *) calloc(1, TEXT_BUFFER_SIZE);
1031         }
1032         END OBJ(pre_exec, 0) obj->type = OBJ_text;
1033         if (arg) {
1034                 FILE *fp = popen(arg, "r");
1035                 unsigned int n;
1036                 char buf[2048];
1037
1038                 n = fread(buf, 1, 2048, fp);
1039                 buf[n] = '\0';
1040
1041                 if (n && buf[n - 1] == '\n')
1042                         buf[n - 1] = '\0';
1043
1044                 (void) pclose(fp);
1045
1046                 obj->data.s = strdup(buf);
1047         } else
1048                 obj->data.s = strdup("");
1049         END
1050 #endif
1051             OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
1052         arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1053         if (arg) {
1054                 while (isspace(*arg))
1055                         arg++;
1056                 if (*arg == '\0')
1057                         arg = "/";
1058         } else
1059                 arg = "/";
1060         obj->data.fsbar.fs = prepare_fs_stat(arg);
1061         END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
1062         if (arg) {
1063                 unsigned int n;
1064                 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
1065                         arg += n;
1066         } else
1067                 arg = "/";
1068         obj->data.fsbar.fs = prepare_fs_stat(arg);
1069         END OBJ(fs_free, INFO_FS) if (!arg)
1070                  arg = "/";
1071         obj->data.fs = prepare_fs_stat(arg);
1072         END OBJ(fs_used_perc, INFO_FS) if (!arg)
1073                  arg = "/";
1074         obj->data.fs = prepare_fs_stat(arg);
1075         END OBJ(fs_free_perc, INFO_FS) if (!arg)
1076                  arg = "/";
1077         obj->data.fs = prepare_fs_stat(arg);
1078         END OBJ(fs_size, INFO_FS) if (!arg)
1079                  arg = "/";
1080         obj->data.fs = prepare_fs_stat(arg);
1081         END OBJ(fs_used, INFO_FS) if (!arg)
1082                  arg = "/";
1083         obj->data.fs = prepare_fs_stat(arg);
1084         END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
1085         END OBJ(offset, 0) obj->data.i = arg ? atoi(arg) : 1;
1086         END OBJ(voffset, 0) obj->data.i = arg ? atoi(arg) : 1;
1087         END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
1088         int n;
1089
1090         if (!arg) {
1091                 ERR("i2c needs arguments");
1092                 obj->type = OBJ_text;
1093                 obj->data.s = strdup("${i2c}");
1094                 return;
1095         }
1096
1097         if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
1098                 /* if scanf couldn't read three values, read type and num and use
1099                  * default device */
1100                 sscanf(arg, "%63s %d", buf2, &n);
1101                 obj->data.i2c.fd =
1102                     open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
1103                                     obj->data.i2c.devtype);
1104                 strcpy(obj->data.i2c.type, buf2);
1105         } else {
1106                 obj->data.i2c.fd =
1107                     open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
1108                                     obj->data.i2c.devtype);
1109                 strcpy(obj->data.i2c.type, buf2);
1110         }
1111
1112         END OBJ(top, INFO_TOP)
1113         char buf[64];
1114         int n;
1115         if (!arg) {
1116                 ERR("top needs arguments");
1117                 obj->type = OBJ_text;
1118                 obj->data.s = strdup("${top}");
1119                 return;
1120         }
1121         if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1122                 if (strcmp(buf, "name") == 0) {
1123                         obj->data.top.type = TOP_NAME;
1124                 } else if (strcmp(buf, "cpu") == 0) {
1125                         obj->data.top.type = TOP_CPU;
1126                 } else if (strcmp(buf, "pid") == 0) {
1127                         obj->data.top.type = TOP_PID;
1128                 } else if (strcmp(buf, "mem") == 0) {
1129                         obj->data.top.type = TOP_MEM;
1130                 } else {
1131                         ERR("invalid arg for top");
1132                         return;
1133                 }
1134                 if (n < 1 || n > 10) {
1135                         CRIT_ERR("invalid arg for top");
1136                         return;
1137                 } else {
1138                         obj->data.top.num = n - 1;
1139                         top_cpu = 1;
1140                 }
1141         } else {
1142                 ERR("invalid args given for top");
1143                 return;
1144         }
1145         END OBJ(top_mem, INFO_TOP)
1146         char buf[64];
1147         int n;
1148         if (!arg) {
1149                 ERR("top_mem needs arguments");
1150                 obj->type = OBJ_text;
1151                 obj->data.s = strdup("${top_mem}");
1152                 return;
1153         }
1154         if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1155                 if (strcmp(buf, "name") == 0) {
1156                         obj->data.top.type = TOP_NAME;
1157                 } else if (strcmp(buf, "cpu") == 0) {
1158                         obj->data.top.type = TOP_CPU;
1159                 } else if (strcmp(buf, "pid") == 0) {
1160                         obj->data.top.type = TOP_PID;
1161                 } else if (strcmp(buf, "mem") == 0) {
1162                         obj->data.top.type = TOP_MEM;
1163                 } else {
1164                         ERR("invalid arg for top");
1165                         return;
1166                 }
1167                 if (n < 1 || n > 10) {
1168                         CRIT_ERR("invalid arg for top");
1169                         return;
1170                 } else {
1171                         obj->data.top.num = n - 1;
1172                         top_mem = 1;
1173                 }
1174         } else {
1175                 ERR("invalid args given for top");
1176                 return;
1177         }
1178         END OBJ(addr, INFO_NET) obj->data.net = get_net_stat(arg);
1179         END OBJ(linkstatus, INFO_WIFI) obj->data.net = get_net_stat(arg);
1180         END OBJ(tail, 0)
1181         char buf[64];
1182         int n1, n2;
1183         if (!arg) {
1184                 ERR("tail needs arguments");
1185                 obj->type = OBJ_text;
1186                 obj->data.s = strdup("${tail}");
1187                 return;
1188         }
1189         if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
1190                 if (n1 < 1 || n1 > 30) {
1191                         CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
1192                         return;
1193                 } else {
1194                         FILE *fp;
1195                         fp = fopen(buf, "rt");
1196                         if (fp != NULL) {
1197                                 obj->data.tail.logfile =
1198                                     malloc(TEXT_BUFFER_SIZE);
1199                                 strcpy(obj->data.tail.logfile, buf);
1200                                 obj->data.tail.wantedlines = n1 - 1;
1201                                 obj->data.tail.interval =
1202                                     update_interval * 2;
1203                                 fclose(fp);
1204                         } else {
1205                                 //fclose (fp);
1206                                 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
1207                         }
1208                 }
1209         } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
1210                 if (n1 < 1 || n1 > 30) {
1211                         CRIT_ERR
1212                             ("invalid arg for tail, number of lines must be between 1 and 30");
1213                         return;
1214                 } else if (n2 < 1 || n2 < update_interval) {
1215                         CRIT_ERR
1216                             ("invalid arg for tail, interval must be greater than 0 and Conky's interval");
1217                         return;
1218                 } else {
1219                         FILE *fp;
1220                         fp = fopen(buf, "rt");
1221                         if (fp != NULL) {
1222                                 obj->data.tail.logfile =
1223                                     malloc(TEXT_BUFFER_SIZE);
1224                                 strcpy(obj->data.tail.logfile, buf);
1225                                 obj->data.tail.wantedlines = n1 - 1;
1226                                 obj->data.tail.interval = n2;
1227                                 fclose(fp);
1228                         } else {
1229                                 //fclose (fp);
1230                                 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
1231                         }
1232                 }
1233         }
1234
1235         else {
1236                 ERR("invalid args given for tail");
1237                 return;
1238         }
1239         obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 6); /* asumming all else worked */
1240         END OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
1241         if (arg) {
1242                 r = sscanf(arg, "%d %d %d", &a, &b, &c);
1243                 if (r >= 3 && (c < 1 || c > 3))
1244                         r--;
1245                 if (r >= 2 && (b < 1 || b > 3))
1246                         r--, b = c;
1247                 if (r >= 1 && (a < 1 || a > 3))
1248                         r--, a = b, b = c;
1249         }
1250         obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1251         obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
1252         obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
1253         END OBJ(if_existing, 0)
1254         if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1255                 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1256         }
1257         if (!arg) {
1258                 ERR("if_existing needs an argument");
1259                 obj->data.ifblock.s = 0;
1260         } else
1261                 obj->data.ifblock.s = strdup(arg);
1262         blockstart[blockdepth] = text_object_count;
1263         obj->data.ifblock.pos = text_object_count + 2;
1264         blockdepth++;
1265         END OBJ(if_mounted, 0)
1266         if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1267                 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1268         }
1269         if (!arg) {
1270                 ERR("if_mounted needs an argument");
1271                 obj->data.ifblock.s = 0;
1272         } else
1273                 obj->data.ifblock.s = strdup(arg);
1274         blockstart[blockdepth] = text_object_count;
1275         obj->data.ifblock.pos = text_object_count + 2;
1276         blockdepth++;
1277         END OBJ(if_running, 0)
1278         if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1279                 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1280         }
1281         if (arg) {
1282                 char buf[256];
1283                 snprintf(buf, 256, "pidof %s >/dev/null", arg);
1284                 obj->data.ifblock.s = strdup(buf);
1285         } else {
1286                 ERR("if_running needs an argument");
1287                 obj->data.ifblock.s = 0;
1288         }
1289         blockstart[blockdepth] = text_object_count;
1290         obj->data.ifblock.pos = text_object_count + 2;
1291         blockdepth++;
1292         END OBJ(kernel, 0)
1293         END OBJ(machine, 0)
1294         END OBJ(mails, INFO_MAIL)
1295         END OBJ(mem, INFO_MEM)
1296         END OBJ(memmax, INFO_MEM)
1297         END OBJ(memperc, INFO_MEM)
1298         END OBJ(membar, INFO_MEM)
1299          (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1300         END OBJ(memgraph, INFO_MEM)
1301                         (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d);
1302         END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
1303         END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
1304         END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
1305         END OBJ(mixerbar, INFO_MIXER)
1306             scan_mixer_bar(arg, &obj->data.mixerbar.l,
1307                            &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1308         END OBJ(mixerlbar, INFO_MIXER)
1309             scan_mixer_bar(arg, &obj->data.mixerbar.l,
1310                            &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1311         END OBJ(mixerrbar, INFO_MIXER)
1312             scan_mixer_bar(arg, &obj->data.mixerbar.l,
1313                            &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1314         END
1315 #ifdef MLDONKEY
1316             OBJ(ml_upload_counter, INFO_MLDONKEY)
1317         END OBJ(ml_download_counter, INFO_MLDONKEY)
1318         END OBJ(ml_nshared_files, INFO_MLDONKEY)
1319         END OBJ(ml_shared_counter, INFO_MLDONKEY)
1320         END OBJ(ml_tcp_upload_rate, INFO_MLDONKEY)
1321         END OBJ(ml_tcp_download_rate, INFO_MLDONKEY)
1322         END OBJ(ml_udp_upload_rate, INFO_MLDONKEY)
1323         END OBJ(ml_udp_download_rate, INFO_MLDONKEY)
1324         END OBJ(ml_ndownloaded_files, INFO_MLDONKEY)
1325         END OBJ(ml_ndownloading_files, INFO_MLDONKEY) END
1326 #endif
1327          OBJ(new_mails, INFO_MAIL)
1328         END OBJ(nodename, 0)
1329         END OBJ(processes, INFO_PROCS)
1330         END OBJ(running_processes, INFO_RUN_PROCS)
1331         END OBJ(shadecolor, 0)
1332 #ifdef X11
1333             obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
1334 #endif /* X11 */
1335         END OBJ(outlinecolor, 0)
1336 #ifdef X11
1337             obj->data.l = arg ? get_x11_color(arg) : default_out_color;
1338 #endif /* X11 */
1339         END OBJ(stippled_hr, 0)
1340 #ifdef X11
1341 int a = stippled_borders, b = 1;
1342         if (arg) {
1343                 if (sscanf(arg, "%d %d", &a, &b) != 2)
1344                         sscanf(arg, "%d", &b);
1345         }
1346         if (a <= 0)
1347                 a = 1;
1348         obj->data.pair.a = a;
1349         obj->data.pair.b = b;
1350 #endif /* X11 */
1351         END OBJ(swap, INFO_MEM)
1352         END OBJ(swapmax, INFO_MEM)
1353         END OBJ(swapperc, INFO_MEM)
1354         END OBJ(swapbar, INFO_MEM)
1355          (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1356         END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
1357         obj->data.i2c.fd =
1358             open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
1359                             obj->data.i2c.devtype);
1360         END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
1361         obj->data.i2c.fd =
1362             open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
1363                             obj->data.i2c.devtype);
1364         END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1365         END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1366         END OBJ(totaldown, INFO_NET) obj->data.net = get_net_stat(arg);
1367         END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
1368         END OBJ(updates, 0)
1369         END OBJ(alignr, 0) obj->data.i = arg ? atoi(arg) : 0;
1370         END OBJ(alignc, 0) obj->data.i = arg ? atoi(arg) : 0;
1371         END OBJ(upspeed, INFO_NET) obj->data.net = get_net_stat(arg);
1372         END OBJ(upspeedf, INFO_NET) obj->data.net = get_net_stat(arg);
1373         END OBJ(upspeedgraph, INFO_NET)
1374                         (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d);
1375         char buf[64];
1376         sscanf(arg, "%63s %*i,%*i %*i", buf);
1377         obj->data.net = get_net_stat(buf);
1378         if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
1379                 if (sscanf(arg, "%*s %d,%d", &obj->a, &obj->a) <= 1) {
1380                         obj->a = 0;
1381                         obj->b = 25;
1382                 }
1383         }
1384         END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
1385             OBJ(adt746xcpu, 0) END OBJ(adt746xfan, 0) END
1386 #ifdef SETI
1387          OBJ(seti_prog, INFO_SETI) END OBJ(seti_progbar, INFO_SETI)
1388          (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1389         END OBJ(seti_credit, INFO_SETI) END
1390 #endif
1391 #ifdef MPD
1392          OBJ(mpd_artist, INFO_MPD)
1393         END OBJ(mpd_title, INFO_MPD)
1394         END OBJ(mpd_elapsed, INFO_MPD)
1395         END OBJ(mpd_length, INFO_MPD)
1396         END OBJ(mpd_percent, INFO_MPD)
1397         END OBJ(mpd_album, INFO_MPD) END OBJ(mpd_vol,
1398                                              INFO_MPD) END OBJ(mpd_bitrate,
1399                                                                INFO_MPD)
1400         END OBJ(mpd_status, INFO_MPD) END OBJ(mpd_bar, INFO_MPD)
1401          (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1402         END
1403 #endif
1404         {
1405                 char buf[256];
1406                 ERR("unknown variable %s", s);
1407                 obj->type = OBJ_text;
1408                 snprintf(buf, 256, "${%s}", s);
1409                 obj->data.s = strdup(buf);
1410         }
1411 #undef OBJ
1412 }
1413
1414 /* append_text() appends text to last text_object if it's text, if it isn't
1415  * it creates a new text_object */
1416 static void append_text(const char *s)
1417 {
1418         struct text_object *obj;
1419
1420         if (s == NULL || *s == '\0')
1421                 return;
1422
1423         obj = text_object_count ? &text_objects[text_object_count - 1] : 0;
1424
1425         /* create a new text object? */
1426         if (!obj || obj->type != OBJ_text) {
1427                 obj = new_text_object();
1428                 obj->type = OBJ_text;
1429                 obj->data.s = strdup(s);
1430         } else {
1431                 /* append */
1432                 obj->data.s = (char *) realloc(obj->data.s,
1433                                                strlen(obj->data.s) +
1434                                                strlen(s) + 1);
1435                 strcat(obj->data.s, s);
1436         }
1437 }
1438
1439 static void extract_variable_text(const char *p)
1440 {
1441         const char *s = p;
1442
1443         free_text_objects();
1444
1445         while (*p) {
1446                 if (*p == '$') {
1447                         *(char *) p = '\0';
1448                         append_text(s);
1449                         *(char *) p = '$';
1450                         p++;
1451                         s = p;
1452
1453                         if (*p != '$') {
1454                                 char buf[256];
1455                                 const char *var;
1456                                 unsigned int len;
1457
1458                                 /* variable is either $foo or ${foo} */
1459                                 if (*p == '{') {
1460                                         p++;
1461                                         s = p;
1462                                         while (*p && *p != '}')
1463                                                 p++;
1464                                 } else {
1465                                         s = p;
1466                                         if (*p == '#')
1467                                                 p++;
1468                                         while (*p && (isalnum((int) *p)
1469                                                       || *p == '_'))
1470                                                 p++;
1471                                 }
1472
1473                                 /* copy variable to buffer */
1474                                 len = (p - s > 255) ? 255 : (p - s);
1475                                 strncpy(buf, s, len);
1476                                 buf[len] = '\0';
1477
1478                                 if (*p == '}')
1479                                         p++;
1480                                 s = p;
1481
1482                                 var = getenv(buf);
1483
1484                                 /* if variable wasn't found from environment, use some special */
1485                                 if (!var) {
1486                                         char *p;
1487                                         char *arg = 0;
1488
1489                                         /* split arg */
1490                                         if (strchr(buf, ' ')) {
1491                                                 arg = strchr(buf, ' ');
1492                                                 *arg = '\0';
1493                                                 arg++;
1494                                                 while (isspace((int) *arg))
1495                                                         arg++;
1496                                                 if (!*arg)
1497                                                         arg = 0;
1498                                         }
1499
1500                                         /* lowercase variable name */
1501                                         p = buf;
1502                                         while (*p) {
1503                                                 *p = tolower(*p);
1504                                                 p++;
1505                                         }
1506
1507                                         construct_text_object(buf, arg);
1508                                 }
1509                                 continue;
1510                         } else
1511                                 append_text("$");
1512                 }
1513
1514                 p++;
1515         }
1516         append_text(s);
1517         if (blockdepth) {
1518                 ERR("one or more $endif's are missing");
1519         }
1520 }
1521
1522 double current_update_time, last_update_time;
1523
1524 static void generate_text()
1525 {
1526         unsigned int i, n;
1527         struct information *cur = &info;
1528         char *p;
1529
1530         special_count = 0;
1531
1532         /* update info */
1533
1534         current_update_time = get_time();
1535
1536         update_stuff(cur);
1537
1538         /* generate text */
1539
1540         n = TEXT_BUFFER_SIZE * 4 - 2;
1541         p = text_buffer;
1542
1543         for (i = 0; i < text_object_count; i++) {
1544                 struct text_object *obj = &text_objects[i];
1545
1546 #define OBJ(a) break; case OBJ_##a:
1547
1548                 switch (obj->type) {
1549                 default:
1550                         {
1551                                 ERR("not implemented obj type %d",
1552                                     obj->type);
1553                         }
1554                         OBJ(acpitemp) {
1555                                 /* does anyone have decimals in acpi temperature? */
1556                                 if (!use_spacer)
1557                                         snprintf(p, n, "%d", (int)
1558                                                         get_acpi_temperature(obj->
1559                                                                         data.
1560                                                                         i));
1561                                 else
1562                                         snprintf(p, 5, "%d    ", (int)
1563                                                         get_acpi_temperature(obj->
1564                                                                         data.
1565                                                                         i));
1566                         }
1567                         OBJ(acpitempf) {
1568                                 /* does anyone have decimals in acpi temperature? */
1569                                 if (!use_spacer)
1570                                         snprintf(p, n, "%d", (int)
1571                                                         ((get_acpi_temperature(obj->
1572                                                                         data.
1573                                                                         i)+ 40) * 9.0 / 5 - 40));
1574                                 else
1575                                         snprintf(p, 5, "%d    ", (int)
1576                                                         ((get_acpi_temperature(obj->
1577                                                                         data.
1578                                                                         i)+ 40) * 9.0 / 5 - 40));
1579                         }
1580                         OBJ(freq) {
1581                                 snprintf(p, n, "%sMhz", get_freq());
1582                         }
1583                         OBJ(freq_g) {
1584                                 float ghz = (float)(atof(get_freq())/1000);
1585                                 //printf("%f\n", ghz);
1586                                 snprintf(p, n, "%'.2fGhz", ghz);
1587                         }
1588                         OBJ(adt746xcpu) {
1589                                 snprintf(p, n, "%s", get_adt746x_cpu());
1590                         }
1591                         OBJ(adt746xfan) {
1592                                 snprintf(p, n, "%s", get_adt746x_fan());
1593                         }
1594                         OBJ(acpifan) {
1595                                 snprintf(p, n, "%s", get_acpi_fan());
1596                         }
1597                         OBJ(acpiacadapter) {
1598                                 snprintf(p, n, "%s",
1599                                          get_acpi_ac_adapter());
1600                         }
1601                         OBJ(battery) {
1602                                 get_battery_stuff(p, n, obj->data.s);
1603                         }
1604                         OBJ(buffers) {
1605                                 human_readable(cur->buffers * 1024, p,
1606                                                255);
1607                         }
1608                         OBJ(cached) {
1609                                 human_readable(cur->cached * 1024, p, 255);
1610                         }
1611                         OBJ(cpu) {
1612                                 if (!use_spacer)
1613                                         snprintf(p, n, "%*d", pad_percents,
1614                                                  (int) (cur->cpu_usage *
1615                                                         100.0));
1616                                 else
1617                                         snprintf(p, 4, "%*d    ",
1618                                                  pad_percents,
1619                                                  (int) (cur->cpu_usage *
1620                                                         100.0));
1621                         }
1622                         OBJ(cpubar) {
1623                                 new_bar(p, obj->data.pair.a,
1624                                         obj->data.pair.b,
1625                                         (int) (cur->cpu_usage * 255.0));
1626                         }
1627                         OBJ(cpugraph) {
1628                                 new_graph(p, obj->a,
1629                                           obj->b, obj->c, obj->d,
1630                                           (unsigned int) (cur->cpu_usage *
1631                                                           100), 0);
1632                         }
1633                         OBJ(color) {
1634                                 new_fg(p, obj->data.l);
1635                         }
1636 #ifdef X11
1637                         OBJ(font) {
1638                                 new_font(p, obj->data.s);
1639                         }
1640 #endif /* X11 */
1641                         OBJ(downspeed) {
1642                                 if (!use_spacer) {
1643                                         snprintf(p, n, "%d",
1644                                                  (int) (obj->data.net->
1645                                                         recv_speed /
1646                                                         1024));
1647                                 } else
1648                                         snprintf(p, 6, "%d     ",
1649                                                  (int) (obj->data.net->
1650                                                         recv_speed /
1651                                                         1024));
1652                         }
1653                         OBJ(downspeedf) {
1654                                 if (!use_spacer)
1655                                         snprintf(p, n, "%.1f",
1656                                                  obj->data.net->
1657                                                  recv_speed / 1024.0);
1658                                 else
1659                                         snprintf(p, 8, "%.1f       ",
1660                                                  obj->data.net->
1661                                                  recv_speed / 1024.0);
1662                         }
1663                         OBJ(downspeedgraph) {
1664                                 if (obj->data.net->recv_speed == 0)     // this is just to make the ugliness at start go away
1665                                         obj->data.net->recv_speed = 0.01;
1666                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1667                                           (obj->data.net->recv_speed /
1668                                 1024.0), 1);
1669                         }
1670                         OBJ(
1671                                    else
1672                         ) {
1673                                 if (!if_jumped) {
1674                                         i = obj->data.ifblock.pos - 2;
1675                                 } else {
1676                                         if_jumped = 0;
1677                                 }
1678                         }
1679                         OBJ(endif) {
1680                                 if_jumped = 0;
1681                         }
1682 #ifdef HAVE_POPEN
1683                         OBJ(addr) {
1684                                 snprintf(p, n, "%u.%u.%u.%u",
1685                                          obj->data.net->addr.
1686                                          sa_data[2] & 255,
1687                                          obj->data.net->addr.
1688                                          sa_data[3] & 255,
1689                                          obj->data.net->addr.
1690                                          sa_data[4] & 255,
1691                                          obj->data.net->addr.
1692                                          sa_data[5] & 255);
1693
1694                         }
1695                         OBJ(linkstatus) {
1696                                 snprintf(p, n, "%d",
1697                                          obj->data.net->linkstatus);
1698                         }
1699
1700                         OBJ(exec) {
1701                                 char *p2 = p;
1702                                 FILE *fp = popen(obj->data.s, "r");
1703                                 int n2 = fread(p, 1, n, fp);
1704                                 (void) pclose(fp);
1705
1706                                 p[n2] = '\0';
1707                                 if (n2 && p[n2 - 1] == '\n')
1708                                         p[n2 - 1] = '\0';
1709
1710                                 while (*p2) {
1711                                         if (*p2 == '\001')
1712                                                 *p2 = ' ';
1713                                         p2++;
1714                                 }
1715                         }
1716                         OBJ(execbar) {
1717                                 char *p2 = p;
1718                                 FILE *fp = popen(obj->data.s, "r");
1719                                 int n2 = fread(p, 1, n, fp);
1720                                 (void) pclose(fp);
1721
1722                                 p[n2] = '\0';
1723                                 if (n2 && p[n2 - 1] == '\n')
1724                                         p[n2 - 1] = '\0';
1725
1726                                 while (*p2) {
1727                                         if (*p2 == '\001')
1728                                                 *p2 = ' ';
1729                                         p2++;
1730                                 }
1731                                 double barnum;
1732                                 if (sscanf(p, "%lf", &barnum) == 0) {
1733                                         ERR("reading execbar value failed (perhaps it's not the correct format?)");
1734                                 }
1735                                 if (barnum > 100 || barnum < 0) {
1736                                         ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
1737                                 } else {
1738                                         barnum = barnum / 100.0;
1739                                         new_bar(p, 0,
1740                                                 4, (int) (barnum * 255.0));
1741                                 }
1742
1743                         }
1744                         OBJ(execgraph) {
1745                                 char *p2 = p;
1746                                 FILE *fp = popen(obj->data.s, "r");
1747                                 int n2 = fread(p, 1, n, fp);
1748                                 (void) pclose(fp);
1749
1750                                 p[n2] = '\0';
1751                                 if (n2 && p[n2 - 1] == '\n')
1752                                         p[n2 - 1] = '\0';
1753
1754                                 while (*p2) {
1755                                         if (*p2 == '\001')
1756                                                 *p2 = ' ';
1757                                         p2++;
1758                                 }
1759                                 double barnum;
1760                                 if (sscanf(p, "%lf", &barnum) == 0) {
1761                                         ERR("reading execgraph value failed (perhaps it's not the correct format?)");
1762                                 }
1763                                 if (barnum > 100 || barnum < 0) {
1764                                         ERR("your execgraph value is not between 0 and 100, therefore it will be ignored");
1765                                 } else {
1766                                         new_graph(p, 0,
1767                                         25, obj->c, obj->d, (int) (barnum), 0);
1768                                 }
1769
1770                         }
1771                         OBJ(execi) {
1772                                 if (current_update_time -
1773                                     obj->data.execi.last_update <
1774                                     obj->data.execi.interval) {
1775                                         snprintf(p, n, "%s",
1776                                                  obj->data.execi.buffer);
1777                                 } else {
1778                                         char *p2 = obj->data.execi.buffer;
1779                                         FILE *fp =
1780                                             popen(obj->data.execi.cmd,
1781                                                   "r");
1782                                         int n2 =
1783                                             fread(p2, 1, TEXT_BUFFER_SIZE,
1784                                                   fp);
1785                                         (void) pclose(fp);
1786
1787                                         p2[n2] = '\0';
1788                                         if (n2 && p2[n2 - 1] == '\n')
1789                                                 p2[n2 - 1] = '\0';
1790
1791                                         while (*p2) {
1792                                                 if (*p2 == '\001')
1793                                                         *p2 = ' ';
1794                                                 p2++;
1795                                         }
1796
1797                                         snprintf(p, n, "%s",
1798                                                  obj->data.execi.buffer);
1799
1800                                         obj->data.execi.last_update =
1801                                             current_update_time;
1802                                 }
1803                         }
1804 #endif
1805                         OBJ(fs_bar) {
1806                                 if (obj->data.fs != NULL) {
1807                                         if (obj->data.fs->size == 0)
1808                                                 new_bar(p,
1809                                                         obj->data.fsbar.w,
1810                                                         obj->data.fsbar.h,
1811                                                         255);
1812                                         else
1813                                                 new_bar(p,
1814                                                         obj->data.fsbar.w,
1815                                                         obj->data.fsbar.h,
1816                                                         (int) (255 -
1817                                                                obj->data.
1818                                                                fsbar.fs->
1819                                                                avail *
1820                                                                255 /
1821                                                                obj->data.
1822                                                                fs->size));
1823                                 }
1824                         }
1825                         OBJ(fs_free) {
1826                                 if (obj->data.fs != NULL)
1827                                         human_readable(obj->data.fs->avail,
1828                                                        p, 255);
1829                         }
1830                         OBJ(fs_free_perc) {
1831                                 if (obj->data.fs != NULL) {
1832                                         if (obj->data.fs->size)
1833                                                 snprintf(p, n, "%*d",
1834                                                          pad_percents,
1835                                                          (int) ((obj->data.
1836                                                                  fs->
1837                                                                  avail *
1838                                                                  100) /
1839                                                                 obj->data.
1840                                                                 fs->size));
1841                                         else
1842                                                 snprintf(p, n, "0");
1843                                 }
1844                         }
1845                         OBJ(fs_size) {
1846                                 if (obj->data.fs != NULL)
1847                                         human_readable(obj->data.fs->size,
1848                                                        p, 255);
1849                         }
1850                         OBJ(fs_used) {
1851                                 if (obj->data.fs != NULL)
1852                                         human_readable(obj->data.fs->size -
1853                                                        obj->data.fs->avail,
1854                                                        p, 255);
1855                         }
1856                         OBJ(fs_bar_free) {
1857                                 if (obj->data.fs != NULL) {
1858                                         if (obj->data.fs->size == 0)
1859                                                 new_bar(p,
1860                                                         obj->data.fsbar.w,
1861                                                         obj->data.fsbar.h,
1862                                                         255);
1863                                         else
1864                                                 new_bar(p,
1865                                                         obj->data.fsbar.w,
1866                                                         obj->data.fsbar.h,
1867                                                         (int) (obj->data.
1868                                                                fsbar.fs->
1869                                                                avail *
1870                                                                255 /
1871                                                                obj->data.
1872                                                                fs->size));
1873                                 }
1874                         }
1875                         OBJ(fs_used_perc) {
1876                                 if (obj->data.fs != NULL) {
1877                                         if (obj->data.fs->size)
1878                                                 snprintf(p, 4, "%d",
1879                                                          100 - ((int)
1880                                                                 ((obj->
1881                                                                   data.fs->
1882                                                                   avail *
1883                                                                   100) /
1884                                                                  obj->data.
1885                                                                  fs->
1886                                                                  size)));
1887                                         else
1888                                                 snprintf(p, n, "0");
1889                                 }
1890                         }
1891                         OBJ(loadavg) {
1892                                 float *v = info.loadavg;
1893
1894                                 if (obj->data.loadavg[2])
1895                                         snprintf(p, n, "%.2f %.2f %.2f",
1896                                                  v[obj->data.loadavg[0] -
1897                                                    1],
1898                                                  v[obj->data.loadavg[1] -
1899                                                    1],
1900                                                  v[obj->data.loadavg[2] -
1901                                                    1]);
1902                                 else if (obj->data.loadavg[1])
1903                                         snprintf(p, n, "%.2f %.2f",
1904                                                  v[obj->data.loadavg[0] -
1905                                                    1],
1906                                                  v[obj->data.loadavg[1] -
1907                                                    1]);
1908                                 else if (obj->data.loadavg[0])
1909                                         snprintf(p, n, "%.2f",
1910                                                  v[obj->data.loadavg[0] -
1911                                                    1]);
1912                         }
1913                         OBJ(hr) {
1914                                 new_hr(p, obj->data.i);
1915                         }
1916                         OBJ(offset) {
1917                                 new_offset(p, obj->data.i);
1918                         }
1919                         OBJ(voffset) {
1920                                 new_voffset(p, obj->data.i);
1921                         }
1922                         OBJ(i2c) {
1923                                 double r;
1924
1925                                 r = get_i2c_info(&obj->data.i2c.fd,
1926                                                  obj->data.i2c.arg,
1927                                                  obj->data.i2c.devtype,
1928                                                  obj->data.i2c.type);
1929
1930                                 if (r >= 100.0 || r == 0)
1931                                         snprintf(p, n, "%d", (int) r);
1932                                 else
1933                                         snprintf(p, n, "%.1f", r);
1934                         }
1935                         OBJ(alignr) {
1936                                 new_alignr(p, obj->data.i);
1937                         }
1938                         OBJ(alignc) {
1939                                 new_alignc(p, obj->data.i);
1940                         }
1941                         OBJ(if_existing) {
1942                                 struct stat tmp;
1943                                 if ((obj->data.ifblock.s)
1944                                     && (stat(obj->data.ifblock.s, &tmp) ==
1945                                         -1)) {
1946                                         i = obj->data.ifblock.pos - 2;
1947                                         if_jumped = 1;
1948                                 } else
1949                                         if_jumped = 0;
1950                         }
1951                         OBJ(if_mounted) {
1952                                 if ((obj->data.ifblock.s)
1953                                     && (!check_mount(obj->data.ifblock.s))) {
1954                                         i = obj->data.ifblock.pos - 2;
1955                                         if_jumped = 1;
1956                                 } else
1957                                         if_jumped = 0;
1958                         }
1959                         OBJ(if_running) {
1960                                 if ((obj->data.ifblock.s)
1961                                     && system(obj->data.ifblock.s)) {
1962                                         i = obj->data.ifblock.pos - 2;
1963                                         if_jumped = 1;
1964                                 } else
1965                                         if_jumped = 0;
1966                         }
1967                         OBJ(kernel) {
1968                                 snprintf(p, n, "%s", cur->uname_s.release);
1969                         }
1970                         OBJ(machine) {
1971                                 snprintf(p, n, "%s", cur->uname_s.machine);
1972                         }
1973
1974                         /* memory stuff */
1975                         OBJ(mem) {
1976                                 human_readable(cur->mem * 1024, p, 6);
1977                         }
1978                         OBJ(memmax) {
1979                                 human_readable(cur->memmax * 1024, p, 255);
1980                         }
1981                         OBJ(memperc) {
1982                                 if (cur->memmax) {
1983                                         if (!use_spacer)
1984                                                 snprintf(p, n, "%*d",
1985                                                          pad_percents,
1986                                                          (cur->mem * 100) /
1987                                                          (cur->memmax));
1988                                         else
1989                                                 snprintf(p, 4, "%*d   ",
1990                                                          pad_percents,
1991                                                          (cur->mem * 100) /
1992                                                          (cur->memmax));
1993                                 }
1994                         }
1995                         OBJ(membar) {
1996                                 new_bar(p, obj->data.pair.a,
1997                                         obj->data.pair.b,
1998                                         cur->memmax ? (cur->mem * 255) /
1999                                         (cur->memmax) : 0);
2000                         }
2001
2002                         OBJ(memgraph) {
2003                                 new_graph(p, obj->a,
2004                                 obj->b, obj->c, obj->d,
2005                                 cur->memmax ? (cur->mem * 100.0) /
2006                                                 (cur->memmax) : 0.0, 0);
2007                         }
2008                         /* mixer stuff */
2009                         OBJ(mixer) {
2010                                 snprintf(p, n, "%d",
2011                                          mixer_get_avg(obj->data.l));
2012                         }
2013                         OBJ(mixerl) {
2014                                 snprintf(p, n, "%d",
2015                                          mixer_get_left(obj->data.l));
2016                         }
2017                         OBJ(mixerr) {
2018                                 snprintf(p, n, "%d",
2019                                          mixer_get_right(obj->data.l));
2020                         }
2021                         OBJ(mixerbar) {
2022                                 new_bar(p, obj->data.mixerbar.w,
2023                                         obj->data.mixerbar.h,
2024                                         mixer_get_avg(obj->data.mixerbar.
2025                                                       l) * 255 / 100);
2026                         }
2027                         OBJ(mixerlbar) {
2028                                 new_bar(p, obj->data.mixerbar.w,
2029                                         obj->data.mixerbar.h,
2030                                         mixer_get_left(obj->data.mixerbar.
2031                                                        l) * 255 / 100);
2032                         }
2033                         OBJ(mixerrbar) {
2034                                 new_bar(p, obj->data.mixerbar.w,
2035                                         obj->data.mixerbar.h,
2036                                         mixer_get_right(obj->data.mixerbar.
2037                                                         l) * 255 / 100);
2038                         }
2039
2040                         /* mail stuff */
2041                         OBJ(mails) {
2042                                 snprintf(p, n, "%d", cur->mail_count);
2043                         }
2044                         OBJ(new_mails) {
2045                                 snprintf(p, n, "%d", cur->new_mail_count);
2046                         }
2047 #ifdef MLDONKEY
2048                         OBJ(ml_upload_counter) {
2049                                 snprintf(p, n, "%lld",
2050                                          mlinfo.upload_counter / 1048576);
2051                         }
2052                         OBJ(ml_download_counter) {
2053                                 snprintf(p, n, "%lld",
2054                                          mlinfo.download_counter /
2055                                          1048576);
2056                         }
2057                         OBJ(ml_nshared_files) {
2058                                 snprintf(p, n, "%i", mlinfo.nshared_files);
2059                         }
2060                         OBJ(ml_shared_counter) {
2061                                 snprintf(p, n, "%lld",
2062                                          mlinfo.shared_counter / 1048576);
2063                         }
2064                         OBJ(ml_tcp_upload_rate) {
2065                                 snprintf(p, n, "%.2f",
2066                                          (float) mlinfo.tcp_upload_rate /
2067                                          1024);
2068                         }
2069                         OBJ(ml_tcp_download_rate) {
2070                                 snprintf(p, n, "%.2f",
2071                                          (float) mlinfo.tcp_download_rate /
2072                                          1024);
2073                         }
2074                         OBJ(ml_udp_upload_rate) {
2075                                 snprintf(p, n, "%.2f",
2076                                          (float) mlinfo.udp_upload_rate /
2077                                          1024);
2078                         }
2079                         OBJ(ml_udp_download_rate) {
2080                                 snprintf(p, n, "%.2f",
2081                                          (float) mlinfo.udp_download_rate /
2082                                          1024);
2083                         }
2084                         OBJ(ml_ndownloaded_files) {
2085                                 snprintf(p, n, "%i",
2086                                          mlinfo.ndownloaded_files);
2087                         }
2088                         OBJ(ml_ndownloading_files) {
2089                                 snprintf(p, n, "%i",
2090                                          mlinfo.ndownloading_files);
2091                         }
2092 #endif
2093
2094                         OBJ(nodename) {
2095                                 snprintf(p, n, "%s",
2096                                          cur->uname_s.nodename);
2097                         }
2098                         OBJ(outlinecolor) {
2099                                 new_outline(p, obj->data.l);
2100                         }
2101                         OBJ(processes) {
2102                                 if (!use_spacer)
2103                                         snprintf(p, n, "%d", cur->procs);
2104                                 else
2105                                         snprintf(p, 5, "%d    ",
2106                                                  cur->procs);
2107                         }
2108                         OBJ(running_processes) {
2109                                 if (!use_spacer)
2110                                         snprintf(p, n, "%d",
2111                                                  cur->run_procs);
2112                                 else
2113                                         snprintf(p, 3, "%d     ",
2114                                                  cur->run_procs);
2115                         }
2116                         OBJ(text) {
2117                                 snprintf(p, n, "%s", obj->data.s);
2118                         }
2119                         OBJ(shadecolor) {
2120                                 new_bg(p, obj->data.l);
2121                         }
2122                         OBJ(stippled_hr) {
2123                                 new_stippled_hr(p, obj->data.pair.a,
2124                                                 obj->data.pair.b);
2125                         }
2126                         OBJ(swap) {
2127                                 human_readable(cur->swap * 1024, p, 255);
2128                         }
2129                         OBJ(swapmax) {
2130                                 human_readable(cur->swapmax * 1024, p,
2131                                                255);
2132                         }
2133                         OBJ(swapperc) {
2134                                 if (cur->swapmax == 0) {
2135                                         strncpy(p, "No swap", 255);
2136                                 } else {
2137                                         if (!use_spacer)
2138                                                 snprintf(p, 255, "%*u",
2139                                                          pad_percents,
2140                                                          (cur->swap *
2141                                                           100) /
2142                                                          cur->swapmax);
2143                                         else
2144                                                 snprintf(p, 4, "%*u   ",
2145                                                          pad_percents,
2146                                                          (cur->swap *
2147                                                           100) /
2148                                                          cur->swapmax);
2149                                 }
2150                         }
2151                         OBJ(swapbar) {
2152                                 new_bar(p, obj->data.pair.a,
2153                                         obj->data.pair.b,
2154                                         cur->swapmax ? (cur->swap * 255) /
2155                                         (cur->swapmax) : 0);
2156                         }
2157                         OBJ(sysname) {
2158                                 snprintf(p, n, "%s", cur->uname_s.sysname);
2159                         }
2160                         OBJ(time) {
2161                                 time_t t = time(NULL);
2162                                 struct tm *tm = localtime(&t);
2163                                 setlocale(LC_TIME, "");
2164                                 strftime(p, n, obj->data.s, tm);
2165                         }
2166                         OBJ(utime) {
2167                                 time_t t = time(NULL);
2168                                 struct tm *tm = gmtime(&t);
2169                                 strftime(p, n, obj->data.s, tm);
2170                         }
2171                         OBJ(totaldown) {
2172                                 human_readable(obj->data.net->recv, p,
2173                                                255);
2174                         }
2175                         OBJ(totalup) {
2176                                 human_readable(obj->data.net->trans, p,
2177                                                255);
2178                         }
2179                         OBJ(updates) {
2180                                 snprintf(p, n, "%d", total_updates);
2181                         }
2182                         OBJ(upspeed) {
2183                                 if (!use_spacer)
2184                                         snprintf(p, n, "%d",
2185                                                  (int) (obj->data.net->
2186                                                         trans_speed /
2187                                                         1024));
2188                                 else
2189                                         snprintf(p, 5, "%d     ",
2190                                                  (int) (obj->data.net->
2191                                                         trans_speed /
2192                                                         1024));
2193                         }
2194                         OBJ(upspeedf) {
2195                                 if (!use_spacer)
2196                                         snprintf(p, n, "%.1f",
2197                                                  obj->data.net->
2198                                                  trans_speed / 1024.0);
2199                                 else
2200                                         snprintf(p, 8, "%.1f       ",
2201                                                  obj->data.net->
2202                                                  trans_speed / 1024.0);
2203                         }
2204                         OBJ(upspeedgraph) {
2205                                 if (obj->data.net->trans_speed == 0)    // this is just to make the ugliness at start go away
2206                                         obj->data.net->trans_speed = 0.01;
2207                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2208                                           (obj->data.net->trans_speed /
2209                                 1024.0), 1);
2210                         }
2211                         OBJ(uptime_short) {
2212                                 format_seconds_short(p, n,
2213                                                      (int) cur->uptime);
2214                         }
2215                         OBJ(uptime) {
2216                                 format_seconds(p, n, (int) cur->uptime);
2217                         }
2218
2219 #ifdef SETI
2220                         OBJ(seti_prog) {
2221                                 snprintf(p, n, "%.2f",
2222                                          cur->seti_prog * 100.0f);
2223                         }
2224                         OBJ(seti_progbar) {
2225                                 new_bar(p, obj->data.pair.a,
2226                                         obj->data.pair.b,
2227                                         (int) (cur->seti_prog * 255.0f));
2228                         }
2229                         OBJ(seti_credit) {
2230                                 snprintf(p, n, "%.0f", cur->seti_credit);
2231                         }
2232 #endif
2233
2234 #ifdef MPD
2235                         OBJ(mpd_title) {
2236                                 snprintf(p, n, "%s", cur->mpd.title);
2237                         }
2238                         OBJ(mpd_artist) {
2239                                 snprintf(p, n, "%s", cur->mpd.artist);
2240                         }
2241                         OBJ(mpd_album) {
2242                                 snprintf(p, n, "%s", cur->mpd.album);
2243                         }
2244                         OBJ(mpd_vol) {
2245                                 snprintf(p, n, "%i", cur->mpd.volume);
2246                         }
2247                         OBJ(mpd_bitrate) {
2248                                 snprintf(p, n, "%i", cur->mpd.bitrate);
2249                         }
2250                         OBJ(mpd_status) {
2251                                 snprintf(p, n, "%s", cur->mpd.status);
2252                         }
2253                         OBJ(mpd_elapsed) {
2254                                 int days = 0, hours = 0, minutes =
2255                                     0, seconds = 0;
2256                                 int tmp = cur->mpd.elapsed;
2257                                 while (tmp >= 86400) {
2258                                         tmp -= 86400;
2259                                         days++;
2260                                 }
2261                                 while (tmp >= 3600) {
2262                                         tmp -= 3600;
2263                                         hours++;
2264                                 }
2265                                 while (tmp >= 60) {
2266                                         tmp -= 60;
2267                                         minutes++;
2268                                 }
2269                                 seconds = tmp;
2270                                 if (days > 0)
2271                                         snprintf(p, n, "%i days %i:%i:%2i",
2272                                                  days, hours, minutes,
2273                                                  seconds);
2274                                 else if (days > 0)
2275                                         snprintf(p, n, "%i:%i:%02i", hours,
2276                                                  minutes, seconds);
2277                                 else
2278                                         snprintf(p, n, "%i:%02i", minutes,
2279                                                  seconds);
2280                         }
2281                         OBJ(mpd_length) {
2282                                 int days = 0, hours = 0, minutes =
2283                                     0, seconds = 0;
2284                                 int tmp = cur->mpd.length;
2285                                 while (tmp >= 86400) {
2286                                         tmp -= 86400;
2287                                         days++;
2288                                 }
2289                                 while (tmp >= 3600) {
2290                                         tmp -= 3600;
2291                                         hours++;
2292                                 }
2293                                 while (tmp >= 60) {
2294                                         tmp -= 60;
2295                                         minutes++;
2296                                 }
2297                                 seconds = tmp;
2298                                 if (days > 0)
2299                                         snprintf(p, n,
2300                                                  "%i days %i:%i:%02i",
2301                                                  days, hours, minutes,
2302                                                  seconds);
2303                                 else if (days > 0)
2304                                         snprintf(p, n, "%i:%i:%02i", hours,
2305                                                  minutes, seconds);
2306                                 else
2307                                         snprintf(p, n, "%i:%02i", minutes,
2308                                                  seconds);
2309                         }
2310                         OBJ(mpd_percent) {
2311                                 snprintf(p, n, "%2.0f",
2312                                          cur->mpd.progress * 100);
2313                         }
2314                         OBJ(mpd_bar) {
2315                                 new_bar(p, obj->data.pair.a,
2316                                         obj->data.pair.b,
2317                                         (int) (cur->mpd.progress *
2318                                                255.0f));
2319                         }
2320 #endif
2321                         OBJ(top) {
2322                                 if (obj->data.top.type == TOP_NAME
2323                                     && obj->data.top.num >= 0
2324                                     && obj->data.top.num < 10) {
2325                                         // if we limit the buffer and add a bunch of space after, it stops the thing from
2326                                         // moving other shit around, which is really fucking annoying
2327                                         snprintf(p, 17,
2328                                                  "%s                              ",
2329                                                  cur->cpu[obj->data.top.
2330                                                           num]->name);
2331                                 } else if (obj->data.top.type == TOP_CPU
2332                                            && obj->data.top.num >= 0
2333                                            && obj->data.top.num < 10) {
2334                                         snprintf(p, 7, "%3.2f      ",
2335                                                  cur->cpu[obj->data.top.
2336                                                           num]->amount);
2337                                 } else if (obj->data.top.type == TOP_PID
2338                                            && obj->data.top.num >= 0
2339                                            && obj->data.top.num < 10) {
2340                                         snprintf(p, 8, "%i           ",
2341                                                  cur->cpu[obj->data.top.
2342                                                           num]->pid);
2343                                 } else if (obj->data.top.type == TOP_MEM
2344                                            && obj->data.top.num >= 0
2345                                            && obj->data.top.num < 10) {
2346                                         snprintf(p, 7, "%3.2f       ",
2347                                                  cur->cpu[obj->data.top.
2348                                                           num]->totalmem);
2349                                 }
2350                         }
2351                         OBJ(top_mem) {
2352                                 if (obj->data.top.type == TOP_NAME
2353                                     && obj->data.top.num >= 0
2354                                     && obj->data.top.num < 10) {
2355                                         // if we limit the buffer and add a bunch of space after, it stops the thing from
2356                                         // moving other shit around, which is really fucking annoying
2357                                         snprintf(p, 17,
2358                                                  "%s                              ",
2359                                                  cur->memu[obj->data.top.
2360                                                            num]->name);
2361                                 } else if (obj->data.top.type == TOP_CPU
2362                                            && obj->data.top.num >= 0
2363                                            && obj->data.top.num < 10) {
2364                                         snprintf(p, 7, "%3.2f      ",
2365                                                  cur->memu[obj->data.top.
2366                                                            num]->amount);
2367                                 } else if (obj->data.top.type == TOP_PID
2368                                            && obj->data.top.num >= 0
2369                                            && obj->data.top.num < 10) {
2370                                         snprintf(p, 8, "%i           ",
2371                                                  cur->memu[obj->data.top.
2372                                                            num]->pid);
2373                                 } else if (obj->data.top.type == TOP_MEM
2374                                            && obj->data.top.num >= 0
2375                                            && obj->data.top.num < 10) {
2376                                         snprintf(p, 7, "%3.2f       ",
2377                                                  cur->memu[obj->data.top.
2378                                                            num]->totalmem);
2379                                 }
2380                         }
2381
2382
2383
2384                         /*
2385                          * I'm tired of everything being packed in
2386                          * pee
2387                          * poop
2388                          */
2389
2390
2391                         OBJ(tail) {
2392                                 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
2393                                         snprintf(p, n, "%s", obj->data.tail.buffer);
2394                                 } else {
2395                                         obj->data.tail.last_update = current_update_time;
2396                                         FILE *fp;
2397                                         int i;
2398                                         int added = 0;
2399                                         tailstring *head = NULL;
2400                                         tailstring *headtmp = NULL;
2401                                         fp = fopen(obj->data.tail.logfile, "rt");
2402                                         if (fp == NULL) {
2403                                                 ERR("tail logfile failed to open");
2404                                         }
2405                                         else {
2406                                                 obj->data.tail.readlines = 0;
2407
2408                                                 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*4, fp) != NULL) {
2409                                                         if (added >= 30) {
2410                                                                 freelasttail(head);
2411                                                         }
2412                                                         else {
2413                                                                 added++;
2414                                                         }
2415                                                         addtail(&head, obj->data.tail.buffer);
2416                                                         obj->data.tail.readlines++;
2417                                                 }
2418
2419                                                 fclose(fp);
2420
2421                                                 if (obj->data.tail.readlines > 0) {
2422                                                         for (i = 0;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
2423                                                                 addtail(&headtmp, head->data);
2424                                                                 head = head->next;
2425                                                         }
2426                                                         strcpy(obj->data.tail.buffer, headtmp->data);
2427                                                         headtmp = headtmp->next;
2428                                                         for (i = 1;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
2429                                                                 if (headtmp) {
2430                                                                         strncat(obj->data.tail.buffer, headtmp->data, (TEXT_BUFFER_SIZE * 6 / obj->data.tail.wantedlines) - strlen(obj->data.tail.buffer)); /* without strlen() at the end this becomes a possible */
2431                                                                         headtmp = headtmp->next;
2432                                                                 }
2433                                                         }
2434
2435                                                         /* get rid of any ugly newlines at the end */
2436                                                         if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
2437                                                                 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
2438                                                         }
2439                                                         snprintf(p, n, "%s", obj->data.tail.buffer);
2440
2441                                                         freetail(headtmp);
2442                                                 }
2443                                                 else {
2444                                                         strcpy(obj->data.tail.buffer, "Logfile Empty");
2445                                                         snprintf(p, n, "Logfile Empty");
2446                                                 }
2447                                                 freetail(head);
2448                                         }
2449                                 }
2450                         }
2451
2452
2453
2454                         break;
2455                 }
2456
2457                 {
2458                         unsigned int a = strlen(p);
2459                         p += a;
2460                         n -= a;
2461                 }
2462         }
2463
2464         if (stuff_in_upper_case) {
2465                 char *p;
2466
2467                 p = text_buffer;
2468                 while (*p) {
2469                         *p = toupper(*p);
2470                         p++;
2471                 }
2472         }
2473
2474         last_update_time = current_update_time;
2475         total_updates++;
2476         //free(p);
2477 }
2478
2479 #ifdef X11
2480 static void set_font()
2481 {
2482 #ifdef XFT
2483         if (use_xft) {
2484                         if (window.xftdraw != NULL)
2485                                 XftDrawDestroy(window.xftdraw);
2486                         window.xftdraw = XftDrawCreate(display, window.drawable,
2487                                         DefaultVisual(display,
2488                                                         screen),
2489                                         DefaultColormap(display,
2490                                                         screen));
2491                 } else
2492 #endif
2493 {
2494         XSetFont(display, window.gc, fonts[selected_font].font->fid);
2495 }
2496 }
2497
2498
2499 /*
2500  * text size
2501  */
2502
2503 static int text_start_x, text_start_y;  /* text start position in window */
2504 static int text_width, text_height;
2505
2506 #endif /* X11 */
2507
2508 static inline int get_string_width(const char *s)
2509 {
2510 #ifdef X11
2511         return *s ? calc_text_width(s, strlen(s)) : 0;
2512 #else
2513         return strlen(s);
2514 #endif /* X11 */
2515 }
2516
2517 int fontchange = 0;
2518
2519 #ifdef X11
2520 static void text_size_updater(char *s)
2521 {
2522         int w = 0;
2523         char *p;
2524         int h = font_height();
2525         /* get string widths and skip specials */
2526         p = s;
2527         while (*p) {
2528                 if (*p == SPECIAL_CHAR) {
2529                         *p = '\0';
2530                         w += get_string_width(s);
2531                         *p = SPECIAL_CHAR;
2532
2533                         if (specials[special_index].type == BAR
2534                             || specials[special_index].type == GRAPH) {
2535                                 w += specials[special_index].width;
2536                                 if (specials[special_index].height > h) {
2537                                         h = specials[special_index].height;
2538                                         h += font_ascent();
2539                                 }
2540                         }
2541                         
2542                         else if (specials[special_index].type == OFFSET) {
2543                                 w += specials[special_index].arg + get_string_width("a"); /* filthy, but works */
2544                         }
2545                         else if (specials[special_index].type == VOFFSET) {
2546                                 h += specials[special_index].arg;
2547                         }
2548                         else if (specials[special_index].type == FONT) {
2549                                 fontchange = specials[special_index].font_added;
2550                                 selected_font = specials[special_index].font_added;
2551                                 h = font_height();
2552                         }
2553
2554                         
2555                         special_index++;
2556                         s = p + 1;
2557                 }
2558                 p++;
2559         }
2560                 w += get_string_width(s);
2561         if (w > text_width)
2562                 text_width = w;
2563
2564         text_height += h;
2565         if (fontchange) {
2566                 selected_font = 0;
2567         }
2568 }
2569 #endif /* X11 */
2570
2571
2572 #ifdef X11
2573 static void update_text_area()
2574 {
2575         int x, y;
2576
2577         /* update text size if it isn't fixed */
2578 #ifdef OWN_WINDOW
2579         if (!fixed_size)
2580 #endif
2581         {
2582                 text_width = minimum_width;
2583                 text_height = 0;
2584                 special_index = 0;
2585                 for_each_line(text_buffer, text_size_updater);
2586                 text_width += 1;
2587                 if (text_height < minimum_height)
2588                         text_height = minimum_height;
2589         }
2590
2591         /* get text position on workarea */
2592         switch (text_alignment) {
2593         case TOP_LEFT:
2594                 x = gap_x;
2595                 y = gap_y;
2596                 break;
2597
2598         case TOP_RIGHT:
2599                 x = workarea[2] - text_width - gap_x;
2600                 y = gap_y;
2601                 break;
2602
2603         default:
2604         case BOTTOM_LEFT:
2605                 x = gap_x;
2606                 y = workarea[3] - text_height - gap_y;
2607                 break;
2608
2609         case BOTTOM_RIGHT:
2610                 x = workarea[2] - text_width - gap_x;
2611                 y = workarea[3] - text_height - gap_y;
2612                 break;
2613         
2614 #ifdef OWN_WINDOW
2615         case NONE: // Let the WM manage the window
2616                 x = window.x;
2617                 y = window.y;
2618
2619                 fixed_pos  = 1;
2620                 fixed_size = 1;
2621                 break;
2622 #endif
2623         }
2624 #ifdef OWN_WINDOW
2625
2626         if (own_window && !fixed_pos) {
2627                 x += workarea[0];
2628                 y += workarea[1];
2629                 text_start_x = border_margin + 1;
2630                 text_start_y = border_margin + 1;
2631                 window.x = x - border_margin - 1;
2632                 window.y = y - border_margin - 1;
2633         } else
2634 #endif
2635         {
2636                 /* If window size doesn't match to workarea's size, then window
2637                  * probably includes panels (gnome).
2638                  * Blah, doesn't work on KDE. */
2639                 if (workarea[2] != window.width
2640                     || workarea[3] != window.height) {
2641                         y += workarea[1];
2642                         x += workarea[0];
2643                 }
2644
2645                 text_start_x = x;
2646                 text_start_y = y;
2647         }
2648 }
2649
2650 /*
2651  * drawing stuff
2652  */
2653
2654 static int cur_x, cur_y;        /* current x and y for drawing */
2655 static int draw_mode;           /* FG, BG or OUTLINE */
2656 static long current_color;
2657
2658 static inline void set_foreground_color(long c)
2659 {
2660         current_color = c;
2661         XSetForeground(display, window.gc, c);
2662 }
2663 #endif /* X11 */
2664
2665 static void draw_string(const char *s)
2666 {
2667         if (s[0] == '\0')
2668                 return;
2669         int i, i2, pos, width_of_s;
2670         int max=0;
2671         int added;
2672         width_of_s = get_string_width(s);
2673         if (out_to_console) {
2674                 printf("%s\n", s);
2675         }
2676         /* daemon_run(s);  the daemon can be called here, but we need to have a buffer in daemon_run() and we need to tell it when everything is ready to be sent */
2677         strcpy(tmpstring1, s);
2678         pos = 0;
2679         added = 0;
2680         char space[2];
2681         snprintf(space, 2, " ");
2682 #ifdef X11
2683         max = ((text_width - width_of_s) / get_string_width(space));
2684 #endif /* X11 */
2685         /*
2686          * This code looks for tabs in the text and coverts them to spaces.
2687          * The trick is getting the correct number of spaces,
2688          * and not going over the window's size without forcing
2689          * the window larger.
2690          */
2691         for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
2692                 if (tmpstring1[i] == '\t')      // 9 is ascii tab
2693                 {
2694                         i2 = 0;
2695                         for (i2 = 0;
2696                              i2 < (8 - (1 + pos) % 8) && added <= max;
2697                              i2++) {
2698                                 tmpstring2[pos + i2] = ' ';
2699                                 added++;
2700                         }
2701                         pos += i2;
2702                 } else {
2703                         if (tmpstring1[i] != 9) {
2704                                 tmpstring2[pos] = tmpstring1[i];
2705                                 pos++;
2706                         }
2707                 }
2708         }
2709         s = tmpstring2;
2710 #ifdef X11
2711 #ifdef XFT
2712         if (use_xft) {
2713                 XColor c;
2714                 XftColor c2;
2715                 c.pixel = current_color;
2716                 XQueryColor(display, DefaultColormap(display, screen), &c);
2717
2718                 c2.pixel = c.pixel;
2719                 c2.color.red = c.red;
2720                 c2.color.green = c.green;
2721                 c2.color.blue = c.blue;
2722                 c2.color.alpha = fonts[selected_font].font_alpha;
2723                 if (utf8_mode) {
2724                         XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2725                                           cur_x, cur_y, (XftChar8 *) s,
2726                                           strlen(s));
2727                 } else {
2728                         XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2729                                        cur_x, cur_y, (XftChar8 *) s,
2730                                        strlen(s));
2731                 }
2732         } else
2733 #endif
2734         {
2735                 XDrawString(display, window.drawable, window.gc,
2736                             cur_x, cur_y, s, strlen(s));
2737         }
2738         cur_x += width_of_s;
2739 #endif /* X11 */
2740         memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
2741 }
2742
2743 long redmask, greenmask, bluemask;
2744 short colour_depth = 0;
2745
2746 void set_up_gradient()
2747 {
2748         colour_depth = DisplayPlanes(display, screen);
2749         if (colour_depth != 24 && colour_depth != 16) {
2750                 ERR("using non-standard colour depth, gradients may look like a lolly-pop");
2751         }
2752         int i;
2753         redmask = 0;
2754         greenmask = 0;
2755         bluemask = 0;
2756         for(i = (colour_depth / 3)-1; i>=0; i--) {
2757                 redmask |= 1 << i;
2758                 greenmask |= 1 << i;
2759                 bluemask |= 1 << i;
2760         }
2761         if (colour_depth%3 == 1) {
2762                 greenmask |= 1 << (colour_depth / 3);
2763         }
2764         redmask = redmask << (2*colour_depth / 3 + colour_depth%3);
2765         greenmask = greenmask << (colour_depth / 3);
2766 }
2767
2768 inline unsigned long do_gradient(unsigned long first_colour, unsigned long last_colour) { /* this function returns the next colour between two colours for a gradient */
2769         int tmp_color = 0;
2770         int red1, green1, blue1; // first colour
2771         int red2, green2, blue2; // second colour
2772         int red3 = 0, green3 = 0, blue3 = 0; // difference
2773         short redshift = (2*colour_depth / 3 + colour_depth%3);
2774         short greenshift = (colour_depth / 3);
2775         red1 = (first_colour & redmask) >> redshift;
2776         green1 = (first_colour & greenmask) >> greenshift;
2777         blue1 = first_colour & bluemask;
2778         red2 = (last_colour & redmask) >> redshift;
2779         green2 = (last_colour & greenmask) >> greenshift;
2780         blue2 = last_colour & bluemask;
2781         if (red1 > red2) {
2782                 red3 = -1;
2783         }
2784         if (red1 < red2) {
2785                 red3 = 1;
2786         }
2787         if (green1 > green2) {
2788                 green3 = -1;
2789         }
2790         if (green1 < green2) {
2791                 green3 = 1;
2792         }
2793         if (blue1 > blue2) {
2794                 blue3 = -1;
2795         }
2796         if (blue1 < blue2) {
2797                 blue3 = 1;
2798         }
2799         red1 += red3;
2800         green1 += green3;
2801         blue1 += blue3;
2802         if (red1 < 0) {
2803                 red1 = 0;
2804         }
2805         if (green1 < 0) {
2806                 green1 = 0;
2807         }
2808         if (blue1 < 0) {
2809                 blue1 = 0;
2810         }
2811         if (red1 > bluemask) {
2812                 red1 = bluemask;
2813         }
2814         if (green1 > bluemask) {
2815                 green1 = bluemask;
2816         }
2817         if (blue1 > bluemask) {
2818                 blue1 = bluemask;
2819         }
2820         tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
2821         return tmp_color;
2822 }
2823
2824 inline unsigned long gradient_max(unsigned long first_colour, unsigned long last_colour) { /* this function returns the max diff for a gradient */
2825         if (colour_depth == 0) {
2826                 set_up_gradient();
2827         }
2828         int red1, green1, blue1; // first colour
2829         int red2, green2, blue2; // second colour
2830         long redshift = (2*colour_depth / 3 + colour_depth%3);
2831         long greenshift = (colour_depth / 3);
2832         int red3 = 0, green3 = 0, blue3 = 0; // difference
2833         red1 = (first_colour & redmask) >> redshift;
2834         green1 = (first_colour & greenmask) >> greenshift;
2835         blue1 = first_colour & bluemask;
2836         red2 = (last_colour & redmask) >> redshift;
2837         green2 = (last_colour & greenmask) >> greenshift;
2838         blue2 = last_colour & bluemask;
2839         red3 = abs(red1 - red2);
2840         green3 = abs(green1 - green2);
2841         blue3 = abs(blue1 - blue2);
2842         int max = red3;
2843         if (green3 > max)
2844                 max = green3;
2845         if (blue3 > max)
2846                 max = blue3;
2847         return max;
2848 }
2849
2850 static void draw_line(char *s)
2851 {
2852 #ifdef X11
2853         char *p;
2854         cur_x = text_start_x;
2855         cur_y += font_ascent();
2856         int cur_y_add = 0;
2857         short font_h = font_height();
2858
2859         /* find specials and draw stuff */
2860         p = s;
2861         while (*p) {
2862                 if (*p == SPECIAL_CHAR) {
2863                         int w = 0;
2864
2865                         /* draw string before special */
2866                         *p = '\0';
2867                         draw_string(s);
2868                         *p = SPECIAL_CHAR;
2869                         s = p + 1;
2870
2871                         /* draw special */
2872                         switch (specials[special_index].type) {
2873                         case HORIZONTAL_LINE:
2874                                 {
2875                                         int h =
2876                                             specials[special_index].height;
2877                                         int mid = font_ascent() / 2;
2878                                         w = text_start_x + text_width -
2879                                             cur_x;
2880
2881                                         XSetLineAttributes(display,
2882                                                            window.gc, h,
2883                                                            LineSolid,
2884                                                            CapButt,
2885                                                            JoinMiter);
2886                                         XDrawLine(display, window.drawable,
2887                                                   window.gc, cur_x,
2888                                                   cur_y - mid / 2,
2889                                                   cur_x + w,
2890                                                   cur_y - mid / 2);
2891                                 }
2892                                 break;
2893
2894                         case STIPPLED_HR:
2895                                 {
2896                                         int h =
2897                                             specials[special_index].height;
2898                                         int s =
2899                                             specials[special_index].arg;
2900                                         int mid = font_ascent() / 2;
2901                                         char ss[2] = { s, s };
2902                                         w = text_start_x + text_width -
2903                                             cur_x - 1;
2904
2905                                         XSetLineAttributes(display,
2906                                                            window.gc, h,
2907                                                            LineOnOffDash,
2908                                                            CapButt,
2909                                                            JoinMiter);
2910                                         XSetDashes(display, window.gc, 0,
2911                                                    ss, 2);
2912                                         XDrawLine(display, window.drawable,
2913                                                   window.gc, cur_x,
2914                                                   cur_y - mid / 2,
2915                                                   cur_x + w,
2916                                                   cur_y - mid / 2);
2917                                 }
2918                                 break;
2919
2920                         case BAR:
2921                                 {
2922                                         int h =
2923                                             specials[special_index].height;
2924                                         int bar_usage =
2925                                             specials[special_index].arg;
2926                                         int by =
2927                                             cur_y - (font_ascent() +
2928                                                      h) / 2 - 1;
2929                                         w = specials[special_index].width;
2930                                         if (w == 0)
2931                                                 w = text_start_x +
2932                                                     text_width - cur_x - 1;
2933                                         if (w < 0)
2934                                                 w = 0;
2935
2936                                         XSetLineAttributes(display,
2937                                                            window.gc, 1,
2938                                                            LineSolid,
2939                                                            CapButt,
2940                                                            JoinMiter);
2941
2942                                         XDrawRectangle(display,
2943                                                        window.drawable,
2944                                                        window.gc, cur_x,
2945                                                        by, w, h);
2946                                         XFillRectangle(display,
2947                                                        window.drawable,
2948                                                        window.gc, cur_x,
2949                                                        by,
2950                                                        w * bar_usage / 255,
2951                                                        h);
2952                                         if (specials[special_index].
2953                                             height > cur_y_add
2954                                             && specials[special_index].
2955                                             height > font_h) {
2956                                                 cur_y_add =
2957                                                     specials
2958                                                     [special_index].height;
2959                                         }
2960                                 }
2961                                 break;
2962
2963                         case GRAPH:
2964                                 {
2965                                         int h =
2966                                             specials[special_index].height;
2967                                         int by;
2968 #ifdef XFT
2969                                         if (use_xft) {
2970                                             by = cur_y - (font_ascent() +
2971                                                      h) / 2 - 1;
2972                                         } else
2973 #endif
2974                                         {
2975                                                 by = cur_y - (font_ascent()/2);
2976                                         }
2977                                         w = specials[special_index].width;
2978                                         if (w == 0)
2979                                                 w = text_start_x + text_width - cur_x - 1;
2980                                         if (w < 0)
2981                                                 w = 0;
2982                                         XSetLineAttributes(display,
2983                                                            window.gc, 1,
2984                                                            LineSolid,
2985                                                            CapButt,
2986                                                            JoinMiter);
2987                                         XDrawRectangle(display,
2988                                                        window.drawable,
2989                                                        window.gc, cur_x,
2990                                                        by, w, h);
2991                                         XSetLineAttributes(display,
2992                                                            window.gc, 1,
2993                                                            LineSolid,
2994                                                            CapButt,
2995                                                            JoinMiter);
2996         int i;
2997         int j = 0;
2998         int gradient_size = 0;
2999         float gradient_factor = 0;
3000         float gradient_update = 0;
3001         unsigned long tmpcolour = current_color;
3002         if (specials[special_index].first_colour != specials[special_index].last_colour) {
3003                 tmpcolour = specials[special_index].first_colour;
3004                 gradient_size = gradient_max(specials[special_index].first_colour, specials[special_index].last_colour);
3005                 gradient_factor = (float)gradient_size / (w - 3);
3006         }
3007         for (i = 0; i < w - 3; i++) {
3008                 if (specials[special_index].first_colour != specials[special_index].last_colour) {
3009                         XSetForeground(display, window.gc, tmpcolour);
3010                         gradient_update += gradient_factor;
3011                         while (gradient_update > 0) {
3012                                 tmpcolour = do_gradient(tmpcolour, specials[special_index].last_colour);
3013                                 gradient_update--;
3014                         }
3015                 }
3016                 if (i / ((float) (w - 3) / (specials[special_index].graph_width)) > j) {
3017                         j++;
3018                                                 }
3019                                                 XDrawLine(display,  window.drawable, window.gc, cur_x + i + 2, by + h, cur_x + i + 2, by + h - specials[special_index].graph[j] * (h - 1) / specials[special_index].graph_scale);       /* this is mugfugly, but it works */
3020         }
3021                                         if (specials[special_index].
3022                                             height > cur_y_add
3023                                             && specials[special_index].
3024                                             height > font_h) {
3025                                                 cur_y_add =
3026                                                     specials
3027                                                     [special_index].height;
3028                                         }
3029                                 }
3030                                 if (draw_mode == BG) {
3031                                         set_foreground_color(default_bg_color);
3032                                 }
3033                                 else if (draw_mode == OUTLINE) {
3034                                         set_foreground_color(default_out_color);
3035                                 } else {
3036                                         set_foreground_color(default_fg_color);
3037                                 }
3038                                 break;
3039                         
3040                                 case FONT:
3041                                 if (fontchange) {
3042                                         cur_y -= font_ascent();
3043                                         selected_font = specials[special_index].font_added;
3044                                         cur_y += font_ascent();
3045 #ifdef XFT
3046                                         if (!use_xft)
3047 #endif
3048                                         {
3049                                                 set_font();
3050                                         }
3051                                 }
3052                                 break;
3053                         case FG:
3054                                 if (draw_mode == FG)
3055                                         set_foreground_color(specials
3056                                                              [special_index].
3057                                                              arg);
3058                                 break;
3059
3060                         case BG:
3061                                 if (draw_mode == BG)
3062                                         set_foreground_color(specials
3063                                                              [special_index].
3064                                                              arg);
3065                                 break;
3066
3067                         case OUTLINE:
3068                                 if (draw_mode == OUTLINE)
3069                                         set_foreground_color(specials
3070                                                              [special_index].
3071                                                              arg);
3072                                 break;
3073
3074                                 case OFFSET:
3075                                 {
3076                                         w += specials[special_index].arg;
3077                                 }
3078                                 break;
3079                                 case VOFFSET:
3080                                 {
3081                                         cur_y += specials[special_index].arg;
3082                                 }
3083                                 break;
3084
3085                         case ALIGNR:
3086                                 {
3087                                         int pos_x = text_width + gap_x - get_string_width(p) /*- border_margin*2 - 1*/;
3088                                         /*printf("pos_x %i text_start_x %i text_width %i cur_x %i get_string_width(p) %i gap_x %i specials[special_index].arg %i border_margin %i border_width %i\n", pos_x, text_start_x, text_width, cur_x, get_string_width(p), gap_x, specials[special_index].arg, border_margin, border_width);*/
3089                                         if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3090                                         cur_x = pos_x - specials[special_index].arg;
3091                                 }
3092                                 }
3093                                 break;
3094
3095                         case ALIGNC:
3096                                 {
3097                                         int pos_x = (text_width)/2 - get_string_width(p)/2 - (cur_x - text_start_x);
3098                                         /*printf("pos_x %i text_start_x %i text_width %i cur_x %i get_string_width(p) %i gap_x %i specials[special_index].arg %i\n", pos_x, text_start_x, text_width, cur_x, get_string_width(p), gap_x, specials[special_index].arg);*/
3099                                         if (pos_x >
3100                                             specials[special_index].arg)
3101                                                 w = pos_x -
3102                                                     specials
3103                                                     [special_index].arg;
3104                                 }
3105                                 break;
3106
3107                         }
3108
3109                         cur_x += w;
3110
3111                         special_index++;
3112                 }
3113
3114                 p++;
3115         }
3116 #else
3117         draw_string(s);
3118 #endif
3119 #ifdef X11
3120         if (cur_y_add > 0) {
3121                 cur_y += cur_y_add;
3122                 cur_y -= font_descent();
3123         }
3124
3125         draw_string(s);
3126
3127         cur_y += font_descent();
3128         if (fontchange) {
3129                 selected_font = 0;
3130         }
3131 #endif /* X11 */
3132 }
3133
3134 static void draw_text()
3135 {
3136 #ifdef X11
3137         cur_y = text_start_y;
3138
3139         /* draw borders */
3140         if (draw_borders && border_width > 0) {
3141                 unsigned int b = (border_width + 1) / 2;
3142
3143                 if (stippled_borders) {
3144                         char ss[2] =
3145                             { stippled_borders, stippled_borders };
3146                         XSetLineAttributes(display, window.gc,
3147                                            border_width, LineOnOffDash,
3148                                            CapButt, JoinMiter);
3149                         XSetDashes(display, window.gc, 0, ss, 2);
3150                 } else {
3151                         XSetLineAttributes(display, window.gc,
3152                                            border_width, LineSolid,
3153                                            CapButt, JoinMiter);
3154                 }
3155
3156                 XDrawRectangle(display, window.drawable, window.gc,
3157                                text_start_x - border_margin + b,
3158                                text_start_y - border_margin + b,
3159                                text_width + border_margin * 2 - 1 - b * 2,
3160                                text_height + border_margin * 2 - 1 -
3161                                b * 2);
3162         }
3163
3164         /* draw text */
3165         special_index = 0;
3166 #endif /* X11 */
3167         for_each_line(text_buffer, draw_line);
3168 }
3169
3170 static void draw_stuff()
3171 {
3172 #ifdef X11
3173         if (draw_shades && !draw_outline) {
3174                 text_start_x++;
3175                 text_start_y++;
3176                 set_foreground_color(default_bg_color);
3177                 draw_mode = BG;
3178                 draw_text();
3179                 text_start_x--;
3180                 text_start_y--;
3181         }
3182
3183         if (draw_outline) {
3184                 int i, j;
3185                 for (i = -1; i < 2; i++)
3186                         for (j = -1; j < 2; j++) {
3187                                 if (i == 0 && j == 0)
3188                                         continue;
3189                                 text_start_x += i;
3190                                 text_start_y += j;
3191                                 set_foreground_color(default_out_color);
3192                                 draw_mode = OUTLINE;
3193                                 draw_text();
3194                                 text_start_x -= i;
3195                                 text_start_y -= j;
3196                         }
3197         }
3198
3199         set_foreground_color(default_fg_color);
3200         draw_mode = FG;
3201 #endif /* X11 */
3202         draw_text();
3203 #ifdef X11
3204 #ifdef XDBE
3205         if (use_xdbe) {
3206                 XdbeSwapInfo swap;
3207                 swap.swap_window = window.window;
3208                 swap.swap_action = XdbeBackground;
3209                 XdbeSwapBuffers(display, &swap, 1);
3210         }
3211 #endif
3212 #endif /* X11 */
3213 }
3214 #ifdef X11
3215 static void clear_text(int exposures)
3216 {
3217 #ifdef XDBE
3218         if (use_xdbe)
3219                 return;         /* The swap action is XdbeBackground, which clears */
3220 #endif
3221         /* there is some extra space for borders and outlines */
3222         XClearArea(display, window.drawable,
3223                    text_start_x - border_margin - 1,
3224                    text_start_y - border_margin - 1,
3225                    text_width + border_margin * 2 + 2,
3226                    text_height + border_margin * 2 + 2,
3227                    exposures ? True : 0);
3228 }
3229 #endif /* X11 */
3230
3231 static int need_to_update;
3232
3233 /* update_text() generates new text and clears old text area */
3234 static void update_text()
3235 {
3236         generate_text();
3237 #ifdef X11
3238         clear_text(1);
3239 #endif /* X11 */
3240         need_to_update = 1;
3241 }
3242
3243 static void main_loop()
3244 {
3245 #ifdef X11
3246         Region region = XCreateRegion();
3247 #endif /* X11 */
3248
3249         info.looped = 0;
3250         while (total_run_times == 0 || info.looped < total_run_times - 1) {
3251                 info.looped++;
3252 #ifdef X11
3253                 XFlush(display);
3254
3255                 /* wait for X event or timeout */
3256
3257                 if (!XPending(display)) {
3258                         fd_set fdsr;
3259                         struct timeval tv;
3260                         int s;
3261                         double t =
3262                             update_interval - (get_time() -
3263                                                last_update_time);
3264
3265                         if (t < 0)
3266                                 t = 0;
3267
3268                         tv.tv_sec = (long) t;
3269                         tv.tv_usec = (long) (t * 1000000) % 1000000;
3270                         FD_ZERO(&fdsr);
3271                         FD_SET(ConnectionNumber(display), &fdsr);
3272
3273
3274                         s = select(ConnectionNumber(display) + 1, &fdsr, 0,
3275                                    0, &tv);
3276 #else
3277                         usleep(update_interval*1000000); /* FIXME just sleep for the interval time if no X11 */
3278 #endif /* X11 */
3279 #ifdef X11
3280                         if (s == -1) {
3281                                 if (errno != EINTR)
3282                                         ERR("can't select(): %s",
3283                                             strerror(errno));
3284                         } else {
3285                                 /* timeout */
3286                                 if (s == 0)
3287 #endif /* X11 */
3288                                         update_text();
3289 #ifdef X11
3290                         }
3291                 }
3292
3293                 if (need_to_update) {
3294 #ifdef OWN_WINDOW
3295                         int wx = window.x, wy = window.y;
3296 #endif
3297
3298                         need_to_update = 0;
3299
3300                         update_text_area();
3301
3302 #ifdef OWN_WINDOW
3303                         if (own_window) {
3304                                 /* resize window if it isn't right size */
3305                                 if (!fixed_size &&
3306                                     (text_width + border_margin * 2 !=
3307                                      window.width
3308                                      || text_height + border_margin * 2 !=
3309                                      window.height)) {
3310                                         window.width =
3311                                             text_width +
3312                                             border_margin * 2 + 1;
3313                                         window.height =
3314                                             text_height +
3315                                             border_margin * 2 + 1;
3316                                         XResizeWindow(display,
3317                                                       window.window,
3318                                                       window.width,
3319                                                       window.height);
3320                                 }
3321
3322                                 /* move window if it isn't in right position */
3323                                 if (!fixed_pos
3324                                     && (window.x != wx
3325                                         || window.y != wy)) {
3326                                         XMoveWindow(display, window.window,
3327                                                     window.x, window.y);
3328                                 }
3329                         }
3330 #endif
3331
3332                         clear_text(1);
3333
3334 #ifdef XDBE
3335                         if (use_xdbe) {
3336                                 XRectangle r;
3337                                 r.x = text_start_x - border_margin;
3338                                 r.y = text_start_y - border_margin;
3339                                 r.width = text_width + border_margin * 2;
3340                                 r.height = text_height + border_margin * 2;
3341                                 XUnionRectWithRegion(&r, region, region);
3342                         }
3343 #endif
3344                 }
3345
3346                 /* handle X events */
3347
3348                 while (XPending(display)) {
3349                         XEvent ev;
3350                         XNextEvent(display, &ev);
3351
3352                         switch (ev.type) {
3353                         case Expose:
3354                                 {
3355                                         XRectangle r;
3356                                         r.x = ev.xexpose.x;
3357                                         r.y = ev.xexpose.y;
3358                                         r.width = ev.xexpose.width;
3359                                         r.height = ev.xexpose.height;
3360                                         XUnionRectWithRegion(&r, region,
3361                                                              region);
3362                                 }
3363                                 break;
3364
3365 #ifdef OWN_WINDOW
3366                         case ReparentNotify:
3367                                 /* set background to ParentRelative for all parents */
3368                                 if (own_window)
3369                                         set_transparent_background(window.
3370                                                                    window);
3371                                 break;
3372
3373                         case ConfigureNotify:
3374                                 if (own_window) {
3375                                         /* if window size isn't what expected, set fixed size */
3376                                         if (ev.xconfigure.width !=
3377                                             window.width
3378                                             || ev.xconfigure.height !=
3379                                             window.height) {
3380                                                 if (window.width != 0
3381                                                     && window.height != 0)
3382                                                         fixed_size = 1;
3383
3384                                                 /* clear old stuff before screwing up size and pos */
3385                                                 clear_text(1);
3386
3387                                                 {
3388                                                         XWindowAttributes
3389                                                             attrs;
3390                                                         if (XGetWindowAttributes(display, window.window, &attrs)) {
3391                                                                 window.
3392                                                                     width =
3393                                                                     attrs.
3394                                                                     width;
3395                                                                 window.
3396                                                                     height
3397                                                                     =
3398                                                                     attrs.
3399                                                                     height;
3400                                                         }
3401                                                 }
3402
3403                                                 text_width =
3404                                                     window.width -
3405                                                     border_margin * 2 - 1;
3406                                                 text_height =
3407                                                     window.height -
3408                                                     border_margin * 2 - 1;
3409                                         }
3410
3411                                         /* if position isn't what expected, set fixed pos, total_updates
3412                                          * avoids setting fixed_pos when window is set to weird locations
3413                                          * when started */
3414                                         if (total_updates >= 2
3415                                             && !fixed_pos
3416                                             && (window.x != ev.xconfigure.x
3417                                                 || window.y !=
3418                                                 ev.xconfigure.y)
3419                                             && (ev.xconfigure.x != 0
3420                                                 || ev.xconfigure.y != 0)) {
3421                                                 fixed_pos = 1;
3422                                         }
3423                                 }
3424                                 break;
3425 #endif
3426
3427                         default:
3428                                 break;
3429                         }
3430                 }
3431
3432                 /* XDBE doesn't seem to provide a way to clear the back buffer without
3433                  * interfering with the front buffer, other than passing XdbeBackground
3434                  * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
3435                  * redraw the text even if it wasn't part of the exposed area. OTOH,
3436                  * if we're not going to call draw_stuff at all, then no swap happens
3437                  * and we can safely do nothing.
3438                  */
3439
3440                 if (!XEmptyRegion(region)) {
3441 #ifdef XDBE
3442                         if (use_xdbe) {
3443                                 XRectangle r;
3444                                 r.x = text_start_x - border_margin;
3445                                 r.y = text_start_y - border_margin;
3446                                 r.width = text_width + border_margin * 2;
3447                                 r.height = text_height + border_margin * 2;
3448                                 XUnionRectWithRegion(&r, region, region);
3449                         }
3450 #endif
3451                         XSetRegion(display, window.gc, region);
3452 #ifdef XFT
3453                         if (use_xft)
3454                                 XftDrawSetClip(window.xftdraw, region);
3455 #endif
3456 #endif /* X11 */
3457                         draw_stuff();
3458 #ifdef X11
3459                         XDestroyRegion(region);
3460                         region = XCreateRegion();
3461                 }
3462 #endif /* X11 */
3463
3464         }
3465 }
3466
3467 static void load_config_file(const char *);
3468
3469 /* signal handler that reloads config file */
3470 static void reload_handler(int a)
3471 {
3472         ERR("Conky: received signal %d, reloading config\n", a);
3473
3474         if (current_config) {
3475                 clear_fs_stats();
3476                 load_config_file(current_config);
3477 #ifdef X11
3478                 load_fonts();
3479                 set_font();
3480 #endif /* X11 */
3481                 extract_variable_text(text);
3482                 free(text);
3483                 text = NULL;
3484                 update_text();
3485         }
3486 }
3487
3488 static void clean_up()
3489 {
3490 #ifdef X11
3491 #ifdef XDBE
3492         if (use_xdbe)
3493                 XdbeDeallocateBackBufferName(display, window.back_buffer);
3494 #endif
3495 #ifdef OWN_WINDOW
3496         if (own_window)
3497                 XDestroyWindow(display, window.window);
3498         else
3499 #endif
3500         {
3501                 XClearWindow(display, RootWindow(display, screen));
3502                 clear_text(1);
3503                 XFlush(display);
3504         }
3505
3506         XFreeGC(display, window.gc);
3507 #endif /* X11 */
3508
3509
3510         /* it is really pointless to free() memory at the end of program but ak|ra
3511          * wants me to do this */
3512
3513         free_text_objects();
3514
3515         if (text != original_text)
3516                 free(text);
3517
3518         free(current_config);
3519         free(current_mail_spool);
3520 #ifdef SETI
3521         free(seti_dir);
3522 #endif
3523 }
3524
3525 static void term_handler(int a)
3526 {
3527         a = a;                  /* to get rid of warning */
3528         clean_up();
3529         exit(0);
3530 }
3531
3532 static int string_to_bool(const char *s)
3533 {
3534         if (!s)
3535                 return 1;
3536         if (strcasecmp(s, "yes") == 0)
3537                 return 1;
3538         if (strcasecmp(s, "true") == 0)
3539                 return 1;
3540         if (strcasecmp(s, "1") == 0)
3541                 return 1;
3542         return 0;
3543 }
3544 #ifdef X11
3545 static enum alignment string_to_alignment(const char *s)
3546 {
3547         if (strcasecmp(s, "top_left") == 0)
3548                 return TOP_LEFT;
3549         else if (strcasecmp(s, "top_right") == 0)
3550                 return TOP_RIGHT;
3551         else if (strcasecmp(s, "bottom_left") == 0)
3552                 return BOTTOM_LEFT;
3553         else if (strcasecmp(s, "bottom_right") == 0)
3554                 return BOTTOM_RIGHT;
3555         else if (strcasecmp(s, "tl") == 0)
3556                 return TOP_LEFT;
3557         else if (strcasecmp(s, "tr") == 0)
3558                 return TOP_RIGHT;
3559         else if (strcasecmp(s, "bl") == 0)
3560                 return BOTTOM_LEFT;
3561         else if (strcasecmp(s, "br") == 0)
3562                 return BOTTOM_RIGHT;
3563         else if (strcasecmp(s, "none") == 0)
3564                 return NONE;
3565         return TOP_LEFT;
3566 }
3567 #endif /* X11 */
3568
3569
3570 static void set_default_configurations(void)
3571 {
3572         fork_to_background = 0;
3573         total_run_times = 0;
3574         info.cpu_avg_samples = 2;
3575         info.net_avg_samples = 2;
3576         info.memmax = 0;
3577         top_cpu = 0;
3578         top_mem = 0;
3579 #ifdef MPD
3580         strcpy(info.mpd.host, "localhost");
3581         info.mpd.port = 6600;
3582         info.mpd.status = "Checking status...";
3583 #endif
3584         use_spacer = 0;
3585 #ifdef X11
3586         out_to_console = 0;
3587 #else
3588         out_to_console = 1;
3589 #endif
3590 #ifdef X11
3591         default_fg_color = WhitePixel(display, screen);
3592         default_bg_color = BlackPixel(display, screen);
3593         default_out_color = BlackPixel(display, screen);
3594         draw_borders = 0;
3595         draw_shades = 1;
3596         draw_outline = 0;
3597         set_first_font("6x10");
3598         gap_x = 5;
3599         gap_y = 5;
3600         minimum_width = 5;
3601         minimum_height = 5;
3602 #ifdef OWN_WINDOW
3603         own_window = 0;
3604 #endif
3605         stippled_borders = 0;
3606         border_margin = 3;
3607         border_width = 1;
3608         text_alignment = BOTTOM_LEFT;
3609         on_bottom = 1;
3610 #endif /* X11 */
3611
3612         free(current_mail_spool);
3613         {
3614                 char buf[256];
3615                 variable_substitute(MAIL_FILE, buf, 256);
3616                 if (buf[0] != '\0')
3617                         current_mail_spool = strdup(buf);
3618         }
3619
3620         no_buffers = 1;
3621         update_interval = 10.0;
3622         stuff_in_upper_case = 0;
3623 #ifdef MLDONKEY
3624         mlconfig.mldonkey_hostname = "127.0.0.1";
3625         mlconfig.mldonkey_port = 4001;
3626         mlconfig.mldonkey_login = NULL;
3627         mlconfig.mldonkey_password = NULL;
3628 #endif
3629 }
3630
3631 static void load_config_file(const char *f)
3632 {
3633 #define CONF_ERR ERR("%s: %d: config file error", f, line)
3634         int line = 0;
3635         FILE *fp;
3636
3637         set_default_configurations();
3638
3639         fp = open_file(f, 0);
3640         if (!fp)
3641                 return;
3642
3643         while (!feof(fp)) {
3644                 char buf[256], *p, *p2, *name, *value;
3645                 line++;
3646                 if (fgets(buf, 256, fp) == NULL)
3647                         break;
3648
3649                 p = buf;
3650
3651                 /* break at comment */
3652                 p2 = strchr(p, '#');
3653                 if (p2)
3654                         *p2 = '\0';
3655
3656                 /* skip spaces */
3657                 while (*p && isspace((int) *p))
3658                         p++;
3659                 if (*p == '\0')
3660                         continue;       /* empty line */
3661
3662                 name = p;
3663
3664                 /* skip name */
3665                 p2 = p;
3666                 while (*p2 && !isspace((int) *p2))
3667                         p2++;
3668                 if (*p2 != '\0') {
3669                         *p2 = '\0';     /* break at name's end */
3670                         p2++;
3671                 }
3672
3673                 /* get value */
3674                 if (*p2) {
3675                         p = p2;
3676                         while (*p && isspace((int) *p))
3677                                 p++;
3678
3679                         value = p;
3680
3681                         p2 = value + strlen(value);
3682                         while (isspace((int) *(p2 - 1)))
3683                                 *--p2 = '\0';
3684                 } else {
3685                         value = 0;
3686                 }
3687
3688 #define CONF2(a) if (strcasecmp(name, a) == 0)
3689 #define CONF(a) else CONF2(a)
3690 #define CONF3(a,b) \
3691 else if (strcasecmp(name, a) == 0 || strcasecmp(name, a) == 0)
3692
3693
3694 #ifdef X11
3695                 CONF2("alignment") {
3696         if (value) {
3697                 int a = string_to_alignment(value);
3698                 if (a <= 0)
3699                         CONF_ERR;
3700                 else
3701                         text_alignment = a;
3702         } else
3703                 CONF_ERR;
3704                 }
3705                 CONF("on_bottom") {
3706                         if(value)
3707                                 on_bottom = string_to_bool(value);
3708                         else
3709                                 CONF_ERR;
3710                 }
3711                 CONF("background") {
3712                         fork_to_background = string_to_bool(value);
3713                 }
3714
3715 #else
3716                 CONF2("background") {
3717         fork_to_background = string_to_bool(value);
3718                 }
3719 #endif /* X11 */
3720 #ifdef X11
3721                 CONF("border_margin") {
3722                         if (value)
3723                                 border_margin = strtol(value, 0, 0);
3724                         else
3725                                 CONF_ERR;
3726                 }
3727                 CONF("border_width") {
3728                         if (value)
3729                                 border_width = strtol(value, 0, 0);
3730                         else
3731                                 CONF_ERR;
3732                 }
3733                 CONF("default_color") {
3734                         if (value)
3735                                 default_fg_color = get_x11_color(value);
3736                         else
3737                                 CONF_ERR;
3738                 }
3739                 CONF3("default_shade_color", "default_shadecolor") {
3740                         if (value)
3741                                 default_bg_color = get_x11_color(value);
3742                         else
3743                                 CONF_ERR;
3744                 }
3745                 CONF3("default_outline_color", "default_outlinecolor") {
3746                         if (value)
3747                                 default_out_color = get_x11_color(value);
3748                         else
3749                                 CONF_ERR;
3750                 }
3751 #endif /* X11 */
3752 #ifdef MPD
3753                 CONF("mpd_host") {
3754                         if (value)
3755                                 strcpy(info.mpd.host, value);
3756                         else
3757                                 CONF_ERR;
3758                 }
3759                 CONF("mpd_port") {
3760                         if (value) {
3761                                 info.mpd.port = strtol(value, 0, 0);
3762                                 if (info.mpd.port < 1
3763                                     || info.mpd.port > 0xffff)
3764                                         CONF_ERR;
3765                         }
3766                 }
3767 #endif
3768                 CONF("cpu_avg_samples") {
3769                         if (value) {
3770                                 cpu_avg_samples = strtol(value, 0, 0);
3771                                 if (cpu_avg_samples < 1
3772                                     || cpu_avg_samples > 14)
3773                                         CONF_ERR;
3774                                 else
3775                                         info.
3776                                             cpu_avg_samples
3777                                             = cpu_avg_samples;
3778                         } else
3779                                 CONF_ERR;
3780                 }
3781                 CONF("net_avg_samples") {
3782                         if (value) {
3783                                 net_avg_samples = strtol(value, 0, 0);
3784                                 if (net_avg_samples < 1
3785                                     || net_avg_samples > 14)
3786                                         CONF_ERR;
3787                                 else
3788                                         info.
3789                                             net_avg_samples
3790                                             = net_avg_samples;
3791                         } else
3792                                 CONF_ERR;
3793                 }
3794
3795
3796
3797
3798
3799
3800 #ifdef XDBE
3801                 CONF("double_buffer") {
3802         if (!own_window) {
3803                 use_xdbe = string_to_bool(value);
3804         }
3805                 }
3806 #endif
3807 #ifdef X11
3808                 CONF("override_utf8_locale") {
3809         utf8_mode = string_to_bool(value);
3810                 }
3811
3812                 CONF("draw_borders") {
3813                         draw_borders = string_to_bool(value);
3814                 }
3815                 CONF("draw_shades") {
3816                         draw_shades = string_to_bool(value);
3817                 }
3818                 CONF("draw_outline") {
3819                         draw_outline = string_to_bool(value);
3820                 }
3821 #endif /* X11 */
3822                 CONF("out_to_console") {
3823                         out_to_console = string_to_bool(value);
3824                 }
3825                 CONF("use_spacer") {
3826                         use_spacer = string_to_bool(value);
3827                 }
3828 #ifdef X11
3829 #ifdef XFT
3830                 CONF("use_xft") {
3831                         use_xft = string_to_bool(value);
3832                 }
3833                 CONF("font") {
3834                         if (!use_xft) {
3835                                 if (value) {
3836                                         set_first_font(value);
3837                                 } else
3838                                         CONF_ERR;
3839                         }
3840                 }
3841                 CONF("xftalpha") {
3842                         if (value && font_count >= 0)
3843                                 fonts[0].font_alpha = atof(value)
3844                                     * 65535.0;
3845                         else
3846                                 CONF_ERR;
3847                 }
3848                 CONF("xftfont") {
3849 #else
3850                 CONF("use_xft") {
3851                         if (string_to_bool(value))
3852                                 ERR("Xft not enabled");
3853                 }
3854                 CONF("xftfont") {
3855                         /* xftfont silently ignored when no Xft */
3856                 }
3857                 CONF("xftalpha") {
3858                         /* xftalpha is silently ignored when no Xft */
3859                 }
3860                 CONF("font") {
3861 #endif
3862                         if (value) {
3863                                 set_first_font(value);
3864                         } else
3865                                 CONF_ERR;
3866                 }
3867                 CONF("gap_x") {
3868                         if (value)
3869                                 gap_x = atoi(value);
3870                         else
3871                                 CONF_ERR;
3872                 }
3873                 CONF("gap_y") {
3874                         if (value)
3875                                 gap_y = atoi(value);
3876                         else
3877                                 CONF_ERR;
3878                 }
3879 #endif /* X11 */
3880                 CONF("mail_spool") {
3881                         if (value) {
3882                                 char buf[256];
3883                                 variable_substitute(value, buf, 256);
3884
3885                                 if (buf[0]
3886                                     != '\0') {
3887                                         if (current_mail_spool)
3888                                                 free(current_mail_spool);
3889                                         current_mail_spool = strdup(buf);
3890                                 }
3891                         } else
3892                                 CONF_ERR;
3893                 }
3894 #ifdef X11
3895                 CONF("minimum_size") {
3896                         if (value) {
3897                                 if (sscanf
3898                                     (value, "%d %d", &minimum_width,
3899                                      &minimum_height) != 2)
3900                                         if (sscanf
3901                                             (value, "%d",
3902                                              &minimum_width) != 1)
3903                                                 CONF_ERR;
3904                         } else
3905                                 CONF_ERR;
3906                 }
3907 #endif /* X11 */
3908                 CONF("no_buffers") {
3909                         no_buffers = string_to_bool(value);
3910                 }
3911 #ifdef MLDONKEY
3912                 CONF("mldonkey_hostname") {
3913                         if (value) {
3914                                 if (mlconfig.mldonkey_hostname != NULL) {
3915                                         free(mlconfig.mldonkey_hostname);
3916                                 }
3917                         mlconfig.mldonkey_hostname = strdup(value);
3918                         }
3919                         else
3920                                 CONF_ERR;
3921                 }
3922                 CONF("mldonkey_port") {
3923                         if (value)
3924                                 mlconfig.mldonkey_port = atoi(value);
3925                         else
3926                                 CONF_ERR;
3927                 }
3928                 CONF("mldonkey_login") {
3929                         if (value) {
3930                                 if (mlconfig.mldonkey_login != NULL) {
3931                                         free(mlconfig.mldonkey_login);
3932                                 }
3933                                 mlconfig.mldonkey_login = strdup(value);
3934                         }
3935                         else
3936                                 CONF_ERR;
3937                 }
3938                 CONF("mldonkey_password") {
3939                         if (value) {
3940                                 if (mlconfig.mldonkey_password != NULL) {
3941                                         free(mlconfig.mldonkey_password);
3942                                 }
3943                                 mlconfig.mldonkey_password = strdup(value);
3944                         }
3945                         else
3946                                 CONF_ERR;
3947                 }
3948 #endif
3949                 CONF("pad_percents") {
3950         pad_percents = atoi(value);
3951                 }
3952 #ifdef X11
3953 #ifdef OWN_WINDOW
3954                 CONF("own_window") {
3955                         own_window = string_to_bool(value);
3956                         use_xdbe = 0;
3957                 }
3958 #endif
3959                 CONF("stippled_borders") {
3960                         if (value)
3961                                 stippled_borders = strtol(value, 0, 0);
3962                         else
3963                                 stippled_borders = 4;
3964                 }
3965 #endif /* X11 */
3966                 CONF("temp1") {
3967                         ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
3968                 }
3969                 CONF("temp1") {
3970                         ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
3971                 }
3972                 CONF("update_interval") {
3973                         if (value)
3974                                 update_interval = strtod(value, 0);
3975                         else
3976                                 CONF_ERR;
3977                 }
3978                 CONF("total_run_times") {
3979                         if (value)
3980                                 total_run_times = strtod(value, 0);
3981                         else
3982                                 CONF_ERR;
3983                 }
3984                 CONF("uppercase") {
3985                         stuff_in_upper_case = string_to_bool(value);
3986                 }
3987 #ifdef SETI
3988                 CONF("seti_dir") {
3989                         seti_dir = (char *)
3990                             malloc(strlen(value)
3991                                    + 1);
3992                         strcpy(seti_dir, value);
3993                 }
3994 #endif
3995                 CONF("text") {
3996                         if (text != original_text)
3997                                 free(text);
3998
3999                         text = (char *)
4000                             malloc(1);
4001                         text[0]
4002                             = '\0';
4003
4004                         while (!feof(fp)) {
4005                                 unsigned
4006                                 int l = strlen(text);
4007                                 if (fgets(buf, 256, fp) == NULL)
4008                                         break;
4009                                 text = (char *)
4010                                     realloc(text, l + strlen(buf)
4011                                             + 1);
4012                                 strcat(text, buf);
4013
4014                                 if (strlen(text) > 1024 * 8)
4015                                         break;
4016                         }
4017                         fclose(fp);
4018                         return;
4019                 }
4020                 else
4021                 ERR("%s: %d: no such configuration: '%s'", f, line, name);
4022
4023 #undef CONF
4024 #undef CONF2
4025         }
4026
4027         fclose(fp);
4028 #undef CONF_ERR
4029 }
4030
4031                                                                                                                                                                                         /* : means that character before that takes an argument */
4032 static const char *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
4033 #ifdef X11
4034                 "x:y:w:a:f:"
4035 #ifdef OWN_WINDOW
4036     "o"
4037 #endif
4038 #ifdef XDBE
4039     "b"
4040 #endif
4041 #endif /* X11 */
4042     ;
4043
4044
4045 int main(int argc, char **argv)
4046 {
4047         /* handle command line parameters that don't change configs */
4048 #ifdef X11
4049         char *s;
4050         char temp[10];
4051         unsigned int x;
4052
4053         if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && 
4054                      *s) || ((s = getenv("LANG")) && *s)) {
4055                 strcpy(temp, s);
4056                 for(x = 0; x < strlen(s) ; x++) {
4057                         temp[x] = tolower(s[x]);
4058                 }
4059                 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
4060                         utf8_mode = 1;
4061                 }
4062         }
4063         if (!setlocale(LC_CTYPE, "")) {
4064                 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
4065                 return 1;
4066         }
4067 #endif /* X11 */
4068         while (1) {
4069                 int c = getopt(argc,
4070                                argv,
4071                                getopt_string);
4072                 if (c == -1)
4073                         break;
4074
4075                 switch (c) {
4076                 case 'v':
4077                 case 'V':
4078                         printf
4079                             ("Conky " VERSION " compiled " __DATE__ "\n");
4080                         return 0;
4081
4082                 case 'c':
4083                         /* if current_config is set to a strdup of CONFIG_FILE, free it (even
4084                          * though free() does the NULL check itself;), then load optarg value */
4085                         if (current_config)
4086                                 free(current_config);
4087                         current_config = strdup(optarg);
4088                         break;
4089
4090                 case 'h':
4091                         printf
4092                                         ("Usage: %s [OPTION]...\n"
4093                                         "Conky is a system monitor that renders text on desktop or to own transparent\n"
4094                                         "window. Command line options will override configurations defined in config\n"
4095                                         "file.\n"
4096                                         "   -V            version\n"
4097                                         "   -c FILE       config file to load instead of "
4098                                         CONFIG_FILE
4099                                         "\n"
4100                                         "   -d            daemonize, fork to background\n"
4101                                         "   -h            help\n"
4102 #ifdef X11
4103                                         "   -a ALIGNMENT  text alignment on screen, {top,bottom}_{left,right}\n"
4104                                         "   -f FONT       font to use\n"
4105 #ifdef OWN_WINDOW
4106                                         "   -o            create own window to draw\n"
4107 #endif
4108 #ifdef XDBE
4109                                         "   -b            double buffer (prevents flickering)\n"
4110 #endif
4111                                         "   -w WIN_ID     window id to draw\n"
4112                                         "   -x X          x position\n"
4113                                         "   -y Y          y position\n"
4114 #endif /* X11 */
4115                                         "   -t TEXT       text to render, remember single quotes, like -t '$uptime'\n"
4116                                         "   -u SECS       update interval\n"
4117                                         "   -i NUM        number of times to update Conky\n", argv[0]);
4118                         return 0;
4119 #ifdef X11
4120                 case 'w':
4121                         window.window = strtol(optarg, 0, 0);
4122                         break;
4123 #endif /* X11 */
4124
4125                 case '?':
4126                         exit(EXIT_FAILURE);
4127                 }
4128         }
4129 #ifdef X11
4130         /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
4131         init_X11();
4132 #endif /* X11 */
4133
4134         tmpstring1 = (char *)
4135             malloc(TEXT_BUFFER_SIZE);
4136         tmpstring2 = (char *)
4137             malloc(TEXT_BUFFER_SIZE);
4138
4139         /* load current_config or CONFIG_FILE */
4140
4141 #ifdef CONFIG_FILE
4142         if (current_config == NULL) {
4143                 /* load default config file */
4144                 char buf[256];
4145
4146                 variable_substitute(CONFIG_FILE, buf, 256);
4147
4148                 if (buf[0] != '\0')
4149                         current_config = strdup(buf);
4150         }
4151 #endif
4152
4153         if (current_config != NULL)
4154                 load_config_file(current_config);
4155         else
4156                 set_default_configurations();
4157
4158 #ifdef MAIL_FILE
4159         if (current_mail_spool == NULL) {
4160                 char buf[256];
4161                 variable_substitute(MAIL_FILE, buf, 256);
4162
4163                 if (buf[0] != '\0')
4164                         current_mail_spool = strdup(buf);
4165         }
4166 #endif
4167
4168         /* handle other command line arguments */
4169
4170         optind = 0;
4171
4172         while (1) {
4173                 int c = getopt(argc,
4174                                argv,
4175                                getopt_string);
4176                 if (c == -1)
4177                         break;
4178
4179                 switch (c) {
4180                 case 'd':
4181                         fork_to_background = 1;
4182                         break;
4183
4184 #ifdef X11
4185                         case 'f':
4186                         set_first_font(optarg);
4187                         break;
4188                         case 'a':
4189                                 text_alignment = string_to_alignment(optarg);
4190                                 break;
4191
4192 #ifdef OWN_WINDOW
4193                 case 'o':
4194                         own_window = 1;
4195                         use_xdbe = 0;
4196                         break;
4197 #endif
4198 #ifdef XDBE
4199                 case 'b':
4200                         use_xdbe = 1;
4201                         break;
4202 #endif
4203 #endif /* X11 */
4204                 case 't':
4205                         if (text != original_text)
4206                                 free(text);
4207                         text = strdup(optarg);
4208                         convert_escapes(text);
4209                         break;
4210
4211                 case 'u':
4212                         update_interval = strtod(optarg, 0);
4213                         break;
4214
4215                 case 'i':
4216                         total_run_times = strtod(optarg, 0);
4217                         break;
4218 #ifdef X11
4219                 case 'x':
4220                         gap_x = atoi(optarg);
4221                         break;
4222
4223                 case 'y':
4224                         gap_y = atoi(optarg);
4225                         break;
4226 #endif /* X11 */
4227
4228                 case '?':
4229                         exit(EXIT_FAILURE);
4230                 }
4231         }
4232
4233 #ifdef X11
4234         /* load font */
4235         load_fonts();
4236 #endif /* X11 */
4237
4238         /* generate text and get initial size */
4239         extract_variable_text(text);
4240         if (text != original_text) {
4241                 free(text);
4242         }
4243         text = NULL;
4244
4245         update_uname();
4246
4247         generate_text();
4248 #ifdef X11
4249         update_text_area();     /* to get initial size of the window */
4250
4251 #if defined OWN_WINDOW
4252         init_window
4253             (own_window,
4254              text_width + border_margin * 2 + 1,
4255              text_height + border_margin * 2 + 1,
4256              on_bottom, fixed_pos);
4257 #else
4258         init_window
4259                 (own_window,
4260                  text_width + border_margin * 2 + 1,
4261                  text_height + border_margin * 2 + 1,
4262                  on_bottom);
4263         
4264 #endif
4265
4266         update_text_area();     /* to position text/window on screen */
4267 #endif /* X11 */
4268
4269 /*#ifdef CAIRO
4270 // why the fuck not?
4271 //do_it();
4272 #endif*/
4273
4274 #ifdef X11
4275 #ifdef OWN_WINDOW
4276         if (own_window && !fixed_pos)
4277                 XMoveWindow(display, window.window, window.x, window.y);
4278 #endif
4279
4280         create_gc();
4281
4282         set_font();
4283         draw_stuff();
4284 #endif /* X11 */
4285
4286         /* fork */
4287         if (fork_to_background) {
4288                 int ret = fork();
4289                 switch (ret) {
4290                 case -1:
4291                         ERR("can't fork() to background: %s",
4292                             strerror(errno));
4293                         break;
4294
4295                 case 0:
4296                         break;
4297
4298                 default:
4299                         fprintf
4300                             (stderr,
4301                              "Conky: forked to background, pid is %d\n",
4302                              ret);
4303                         return 0;
4304                 }
4305         }
4306
4307         /* set SIGUSR1, SIGINT and SIGTERM handlers */
4308         {
4309                 struct
4310                 sigaction sa;
4311
4312                 sa.sa_handler = reload_handler;
4313                 sigemptyset(&sa.sa_mask);
4314                 sa.sa_flags = SA_RESTART;
4315                 if (sigaction(SIGUSR1, &sa, NULL) != 0)
4316                         ERR("can't set signal handler for SIGUSR1: %s",
4317                             strerror(errno));
4318
4319                 sa.sa_handler = term_handler;
4320                 sigemptyset(&sa.sa_mask);
4321                 sa.sa_flags = SA_RESTART;
4322                 if (sigaction(SIGINT, &sa, NULL) != 0)
4323                         ERR("can't set signal handler for SIGINT: %s",
4324                             strerror(errno));
4325
4326                 sa.sa_handler = term_handler;
4327                 sigemptyset(&sa.sa_mask);
4328                 sa.sa_flags = SA_RESTART;
4329                 if (sigaction(SIGTERM, &sa, NULL) != 0)
4330                         ERR("can't set signal handler for SIGTERM: %s",
4331                             strerror(errno));
4332         }
4333         main_loop();
4334         free(tmpstring1);
4335         free(tmpstring2);
4336         return 0;
4337 }