configure and makefile updates
[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 #ifdef X11
2749         colour_depth = DisplayPlanes(display, screen);
2750 #else
2751         colour_depth = 16;
2752 #endif /* X11 */
2753         if (colour_depth != 24 && colour_depth != 16) {
2754                 ERR("using non-standard colour depth, gradients may look like a lolly-pop");
2755         }
2756         int i;
2757         redmask = 0;
2758         greenmask = 0;
2759         bluemask = 0;
2760         for(i = (colour_depth / 3)-1; i>=0; i--) {
2761                 redmask |= 1 << i;
2762                 greenmask |= 1 << i;
2763                 bluemask |= 1 << i;
2764         }
2765         if (colour_depth%3 == 1) {
2766                 greenmask |= 1 << (colour_depth / 3);
2767         }
2768         redmask = redmask << (2*colour_depth / 3 + colour_depth%3);
2769         greenmask = greenmask << (colour_depth / 3);
2770 }
2771
2772 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 */
2773         int tmp_color = 0;
2774         int red1, green1, blue1; // first colour
2775         int red2, green2, blue2; // second colour
2776         int red3 = 0, green3 = 0, blue3 = 0; // difference
2777         short redshift = (2*colour_depth / 3 + colour_depth%3);
2778         short greenshift = (colour_depth / 3);
2779         red1 = (first_colour & redmask) >> redshift;
2780         green1 = (first_colour & greenmask) >> greenshift;
2781         blue1 = first_colour & bluemask;
2782         red2 = (last_colour & redmask) >> redshift;
2783         green2 = (last_colour & greenmask) >> greenshift;
2784         blue2 = last_colour & bluemask;
2785         if (red1 > red2) {
2786                 red3 = -1;
2787         }
2788         if (red1 < red2) {
2789                 red3 = 1;
2790         }
2791         if (green1 > green2) {
2792                 green3 = -1;
2793         }
2794         if (green1 < green2) {
2795                 green3 = 1;
2796         }
2797         if (blue1 > blue2) {
2798                 blue3 = -1;
2799         }
2800         if (blue1 < blue2) {
2801                 blue3 = 1;
2802         }
2803         red1 += red3;
2804         green1 += green3;
2805         blue1 += blue3;
2806         if (red1 < 0) {
2807                 red1 = 0;
2808         }
2809         if (green1 < 0) {
2810                 green1 = 0;
2811         }
2812         if (blue1 < 0) {
2813                 blue1 = 0;
2814         }
2815         if (red1 > bluemask) {
2816                 red1 = bluemask;
2817         }
2818         if (green1 > bluemask) {
2819                 green1 = bluemask;
2820         }
2821         if (blue1 > bluemask) {
2822                 blue1 = bluemask;
2823         }
2824         tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
2825         return tmp_color;
2826 }
2827
2828 inline unsigned long gradient_max(unsigned long first_colour, unsigned long last_colour) { /* this function returns the max diff for a gradient */
2829         if (colour_depth == 0) {
2830                 set_up_gradient();
2831         }
2832         int red1, green1, blue1; // first colour
2833         int red2, green2, blue2; // second colour
2834         long redshift = (2*colour_depth / 3 + colour_depth%3);
2835         long greenshift = (colour_depth / 3);
2836         int red3 = 0, green3 = 0, blue3 = 0; // difference
2837         red1 = (first_colour & redmask) >> redshift;
2838         green1 = (first_colour & greenmask) >> greenshift;
2839         blue1 = first_colour & bluemask;
2840         red2 = (last_colour & redmask) >> redshift;
2841         green2 = (last_colour & greenmask) >> greenshift;
2842         blue2 = last_colour & bluemask;
2843         red3 = abs(red1 - red2);
2844         green3 = abs(green1 - green2);
2845         blue3 = abs(blue1 - blue2);
2846         int max = red3;
2847         if (green3 > max)
2848                 max = green3;
2849         if (blue3 > max)
2850                 max = blue3;
2851         return max;
2852 }
2853
2854 static void draw_line(char *s)
2855 {
2856 #ifdef X11
2857         char *p;
2858         cur_x = text_start_x;
2859         cur_y += font_ascent();
2860         int cur_y_add = 0;
2861         short font_h = font_height();
2862
2863         /* find specials and draw stuff */
2864         p = s;
2865         while (*p) {
2866                 if (*p == SPECIAL_CHAR) {
2867                         int w = 0;
2868
2869                         /* draw string before special */
2870                         *p = '\0';
2871                         draw_string(s);
2872                         *p = SPECIAL_CHAR;
2873                         s = p + 1;
2874
2875                         /* draw special */
2876                         switch (specials[special_index].type) {
2877                         case HORIZONTAL_LINE:
2878                                 {
2879                                         int h =
2880                                             specials[special_index].height;
2881                                         int mid = font_ascent() / 2;
2882                                         w = text_start_x + text_width -
2883                                             cur_x;
2884
2885                                         XSetLineAttributes(display,
2886                                                            window.gc, h,
2887                                                            LineSolid,
2888                                                            CapButt,
2889                                                            JoinMiter);
2890                                         XDrawLine(display, window.drawable,
2891                                                   window.gc, cur_x,
2892                                                   cur_y - mid / 2,
2893                                                   cur_x + w,
2894                                                   cur_y - mid / 2);
2895                                 }
2896                                 break;
2897
2898                         case STIPPLED_HR:
2899                                 {
2900                                         int h =
2901                                             specials[special_index].height;
2902                                         int s =
2903                                             specials[special_index].arg;
2904                                         int mid = font_ascent() / 2;
2905                                         char ss[2] = { s, s };
2906                                         w = text_start_x + text_width -
2907                                             cur_x - 1;
2908
2909                                         XSetLineAttributes(display,
2910                                                            window.gc, h,
2911                                                            LineOnOffDash,
2912                                                            CapButt,
2913                                                            JoinMiter);
2914                                         XSetDashes(display, window.gc, 0,
2915                                                    ss, 2);
2916                                         XDrawLine(display, window.drawable,
2917                                                   window.gc, cur_x,
2918                                                   cur_y - mid / 2,
2919                                                   cur_x + w,
2920                                                   cur_y - mid / 2);
2921                                 }
2922                                 break;
2923
2924                         case BAR:
2925                                 {
2926                                         int h =
2927                                             specials[special_index].height;
2928                                         int bar_usage =
2929                                             specials[special_index].arg;
2930                                         int by =
2931                                             cur_y - (font_ascent() +
2932                                                      h) / 2 - 1;
2933                                         w = specials[special_index].width;
2934                                         if (w == 0)
2935                                                 w = text_start_x +
2936                                                     text_width - cur_x - 1;
2937                                         if (w < 0)
2938                                                 w = 0;
2939
2940                                         XSetLineAttributes(display,
2941                                                            window.gc, 1,
2942                                                            LineSolid,
2943                                                            CapButt,
2944                                                            JoinMiter);
2945
2946                                         XDrawRectangle(display,
2947                                                        window.drawable,
2948                                                        window.gc, cur_x,
2949                                                        by, w, h);
2950                                         XFillRectangle(display,
2951                                                        window.drawable,
2952                                                        window.gc, cur_x,
2953                                                        by,
2954                                                        w * bar_usage / 255,
2955                                                        h);
2956                                         if (specials[special_index].
2957                                             height > cur_y_add
2958                                             && specials[special_index].
2959                                             height > font_h) {
2960                                                 cur_y_add =
2961                                                     specials
2962                                                     [special_index].height;
2963                                         }
2964                                 }
2965                                 break;
2966
2967                         case GRAPH:
2968                                 {
2969                                         int h =
2970                                             specials[special_index].height;
2971                                         int by;
2972 #ifdef XFT
2973                                         if (use_xft) {
2974                                             by = cur_y - (font_ascent() +
2975                                                      h) / 2 - 1;
2976                                         } else
2977 #endif
2978                                         {
2979                                                 by = cur_y - (font_ascent()/2);
2980                                         }
2981                                         w = specials[special_index].width;
2982                                         if (w == 0)
2983                                                 w = text_start_x + text_width - cur_x - 1;
2984                                         if (w < 0)
2985                                                 w = 0;
2986                                         XSetLineAttributes(display,
2987                                                            window.gc, 1,
2988                                                            LineSolid,
2989                                                            CapButt,
2990                                                            JoinMiter);
2991                                         XDrawRectangle(display,
2992                                                        window.drawable,
2993                                                        window.gc, cur_x,
2994                                                        by, w, h);
2995                                         XSetLineAttributes(display,
2996                                                            window.gc, 1,
2997                                                            LineSolid,
2998                                                            CapButt,
2999                                                            JoinMiter);
3000         int i;
3001         int j = 0;
3002         int gradient_size = 0;
3003         float gradient_factor = 0;
3004         float gradient_update = 0;
3005         unsigned long tmpcolour = current_color;
3006         if (specials[special_index].first_colour != specials[special_index].last_colour) {
3007                 tmpcolour = specials[special_index].first_colour;
3008                 gradient_size = gradient_max(specials[special_index].first_colour, specials[special_index].last_colour);
3009                 gradient_factor = (float)gradient_size / (w - 3);
3010         }
3011         for (i = 0; i < w - 3; i++) {
3012                 if (specials[special_index].first_colour != specials[special_index].last_colour) {
3013                         XSetForeground(display, window.gc, tmpcolour);
3014                         gradient_update += gradient_factor;
3015                         while (gradient_update > 0) {
3016                                 tmpcolour = do_gradient(tmpcolour, specials[special_index].last_colour);
3017                                 gradient_update--;
3018                         }
3019                 }
3020                 if (i / ((float) (w - 3) / (specials[special_index].graph_width)) > j) {
3021                         j++;
3022                                                 }
3023                                                 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 */
3024         }
3025                                         if (specials[special_index].
3026                                             height > cur_y_add
3027                                             && specials[special_index].
3028                                             height > font_h) {
3029                                                 cur_y_add =
3030                                                     specials
3031                                                     [special_index].height;
3032                                         }
3033                                 }
3034                                 if (draw_mode == BG) {
3035                                         set_foreground_color(default_bg_color);
3036                                 }
3037                                 else if (draw_mode == OUTLINE) {
3038                                         set_foreground_color(default_out_color);
3039                                 } else {
3040                                         set_foreground_color(default_fg_color);
3041                                 }
3042                                 break;
3043                         
3044                                 case FONT:
3045                                 if (fontchange) {
3046                                         cur_y -= font_ascent();
3047                                         selected_font = specials[special_index].font_added;
3048                                         cur_y += font_ascent();
3049 #ifdef XFT
3050                                         if (!use_xft)
3051 #endif
3052                                         {
3053                                                 set_font();
3054                                         }
3055                                 }
3056                                 break;
3057                         case FG:
3058                                 if (draw_mode == FG)
3059                                         set_foreground_color(specials
3060                                                              [special_index].
3061                                                              arg);
3062                                 break;
3063
3064                         case BG:
3065                                 if (draw_mode == BG)
3066                                         set_foreground_color(specials
3067                                                              [special_index].
3068                                                              arg);
3069                                 break;
3070
3071                         case OUTLINE:
3072                                 if (draw_mode == OUTLINE)
3073                                         set_foreground_color(specials
3074                                                              [special_index].
3075                                                              arg);
3076                                 break;
3077
3078                                 case OFFSET:
3079                                 {
3080                                         w += specials[special_index].arg;
3081                                 }
3082                                 break;
3083                                 case VOFFSET:
3084                                 {
3085                                         cur_y += specials[special_index].arg;
3086                                 }
3087                                 break;
3088
3089                         case ALIGNR:
3090                                 {
3091                                         int pos_x = text_width + gap_x - get_string_width(p) /*- border_margin*2 - 1*/;
3092                                         /*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);*/
3093                                         if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3094                                         cur_x = pos_x - specials[special_index].arg;
3095                                 }
3096                                 }
3097                                 break;
3098
3099                         case ALIGNC:
3100                                 {
3101                                         int pos_x = (text_width)/2 - get_string_width(p)/2 - (cur_x - text_start_x);
3102                                         /*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);*/
3103                                         if (pos_x >
3104                                             specials[special_index].arg)
3105                                                 w = pos_x -
3106                                                     specials
3107                                                     [special_index].arg;
3108                                 }
3109                                 break;
3110
3111                         }
3112
3113                         cur_x += w;
3114
3115                         special_index++;
3116                 }
3117
3118                 p++;
3119         }
3120 #else
3121         draw_string(s);
3122 #endif
3123 #ifdef X11
3124         if (cur_y_add > 0) {
3125                 cur_y += cur_y_add;
3126                 cur_y -= font_descent();
3127         }
3128
3129         draw_string(s);
3130
3131         cur_y += font_descent();
3132         if (fontchange) {
3133                 selected_font = 0;
3134         }
3135 #endif /* X11 */
3136 }
3137
3138 static void draw_text()
3139 {
3140 #ifdef X11
3141         cur_y = text_start_y;
3142
3143         /* draw borders */
3144         if (draw_borders && border_width > 0) {
3145                 unsigned int b = (border_width + 1) / 2;
3146
3147                 if (stippled_borders) {
3148                         char ss[2] =
3149                             { stippled_borders, stippled_borders };
3150                         XSetLineAttributes(display, window.gc,
3151                                            border_width, LineOnOffDash,
3152                                            CapButt, JoinMiter);
3153                         XSetDashes(display, window.gc, 0, ss, 2);
3154                 } else {
3155                         XSetLineAttributes(display, window.gc,
3156                                            border_width, LineSolid,
3157                                            CapButt, JoinMiter);
3158                 }
3159
3160                 XDrawRectangle(display, window.drawable, window.gc,
3161                                text_start_x - border_margin + b,
3162                                text_start_y - border_margin + b,
3163                                text_width + border_margin * 2 - 1 - b * 2,
3164                                text_height + border_margin * 2 - 1 -
3165                                b * 2);
3166         }
3167
3168         /* draw text */
3169         special_index = 0;
3170 #endif /* X11 */
3171         for_each_line(text_buffer, draw_line);
3172 }
3173
3174 static void draw_stuff()
3175 {
3176 #ifdef X11
3177         if (draw_shades && !draw_outline) {
3178                 text_start_x++;
3179                 text_start_y++;
3180                 set_foreground_color(default_bg_color);
3181                 draw_mode = BG;
3182                 draw_text();
3183                 text_start_x--;
3184                 text_start_y--;
3185         }
3186
3187         if (draw_outline) {
3188                 int i, j;
3189                 for (i = -1; i < 2; i++)
3190                         for (j = -1; j < 2; j++) {
3191                                 if (i == 0 && j == 0)
3192                                         continue;
3193                                 text_start_x += i;
3194                                 text_start_y += j;
3195                                 set_foreground_color(default_out_color);
3196                                 draw_mode = OUTLINE;
3197                                 draw_text();
3198                                 text_start_x -= i;
3199                                 text_start_y -= j;
3200                         }
3201         }
3202
3203         set_foreground_color(default_fg_color);
3204         draw_mode = FG;
3205 #endif /* X11 */
3206         draw_text();
3207 #ifdef X11
3208 #ifdef XDBE
3209         if (use_xdbe) {
3210                 XdbeSwapInfo swap;
3211                 swap.swap_window = window.window;
3212                 swap.swap_action = XdbeBackground;
3213                 XdbeSwapBuffers(display, &swap, 1);
3214         }
3215 #endif
3216 #endif /* X11 */
3217 }
3218 #ifdef X11
3219 static void clear_text(int exposures)
3220 {
3221 #ifdef XDBE
3222         if (use_xdbe)
3223                 return;         /* The swap action is XdbeBackground, which clears */
3224 #endif
3225         /* there is some extra space for borders and outlines */
3226         XClearArea(display, window.drawable,
3227                    text_start_x - border_margin - 1,
3228                    text_start_y - border_margin - 1,
3229                    text_width + border_margin * 2 + 2,
3230                    text_height + border_margin * 2 + 2,
3231                    exposures ? True : 0);
3232 }
3233 #endif /* X11 */
3234
3235 static int need_to_update;
3236
3237 /* update_text() generates new text and clears old text area */
3238 static void update_text()
3239 {
3240         generate_text();
3241 #ifdef X11
3242         clear_text(1);
3243 #endif /* X11 */
3244         need_to_update = 1;
3245 }
3246
3247 static void main_loop()
3248 {
3249 #ifdef X11
3250         Region region = XCreateRegion();
3251 #endif /* X11 */
3252
3253         info.looped = 0;
3254         while (total_run_times == 0 || info.looped < total_run_times - 1) {
3255                 info.looped++;
3256 #ifdef X11
3257                 XFlush(display);
3258
3259                 /* wait for X event or timeout */
3260
3261                 if (!XPending(display)) {
3262                         fd_set fdsr;
3263                         struct timeval tv;
3264                         int s;
3265                         double t =
3266                             update_interval - (get_time() -
3267                                                last_update_time);
3268
3269                         if (t < 0)
3270                                 t = 0;
3271
3272                         tv.tv_sec = (long) t;
3273                         tv.tv_usec = (long) (t * 1000000) % 1000000;
3274                         FD_ZERO(&fdsr);
3275                         FD_SET(ConnectionNumber(display), &fdsr);
3276
3277
3278                         s = select(ConnectionNumber(display) + 1, &fdsr, 0,
3279                                    0, &tv);
3280 #else
3281                         usleep(update_interval*1000000); /* FIXME just sleep for the interval time if no X11 */
3282 #endif /* X11 */
3283 #ifdef X11
3284                         if (s == -1) {
3285                                 if (errno != EINTR)
3286                                         ERR("can't select(): %s",
3287                                             strerror(errno));
3288                         } else {
3289                                 /* timeout */
3290                                 if (s == 0)
3291 #endif /* X11 */
3292                                         update_text();
3293 #ifdef X11
3294                         }
3295                 }
3296
3297                 if (need_to_update) {
3298 #ifdef OWN_WINDOW
3299                         int wx = window.x, wy = window.y;
3300 #endif
3301
3302                         need_to_update = 0;
3303
3304                         update_text_area();
3305
3306 #ifdef OWN_WINDOW
3307                         if (own_window) {
3308                                 /* resize window if it isn't right size */
3309                                 if (!fixed_size &&
3310                                     (text_width + border_margin * 2 !=
3311                                      window.width
3312                                      || text_height + border_margin * 2 !=
3313                                      window.height)) {
3314                                         window.width =
3315                                             text_width +
3316                                             border_margin * 2 + 1;
3317                                         window.height =
3318                                             text_height +
3319                                             border_margin * 2 + 1;
3320                                         XResizeWindow(display,
3321                                                       window.window,
3322                                                       window.width,
3323                                                       window.height);
3324                                 }
3325
3326                                 /* move window if it isn't in right position */
3327                                 if (!fixed_pos
3328                                     && (window.x != wx
3329                                         || window.y != wy)) {
3330                                         XMoveWindow(display, window.window,
3331                                                     window.x, window.y);
3332                                 }
3333                         }
3334 #endif
3335
3336                         clear_text(1);
3337
3338 #ifdef XDBE
3339                         if (use_xdbe) {
3340                                 XRectangle r;
3341                                 r.x = text_start_x - border_margin;
3342                                 r.y = text_start_y - border_margin;
3343                                 r.width = text_width + border_margin * 2;
3344                                 r.height = text_height + border_margin * 2;
3345                                 XUnionRectWithRegion(&r, region, region);
3346                         }
3347 #endif
3348                 }
3349
3350                 /* handle X events */
3351
3352                 while (XPending(display)) {
3353                         XEvent ev;
3354                         XNextEvent(display, &ev);
3355
3356                         switch (ev.type) {
3357                         case Expose:
3358                                 {
3359                                         XRectangle r;
3360                                         r.x = ev.xexpose.x;
3361                                         r.y = ev.xexpose.y;
3362                                         r.width = ev.xexpose.width;
3363                                         r.height = ev.xexpose.height;
3364                                         XUnionRectWithRegion(&r, region,
3365                                                              region);
3366                                 }
3367                                 break;
3368
3369 #ifdef OWN_WINDOW
3370                         case ReparentNotify:
3371                                 /* set background to ParentRelative for all parents */
3372                                 if (own_window)
3373                                         set_transparent_background(window.
3374                                                                    window);
3375                                 break;
3376
3377                         case ConfigureNotify:
3378                                 if (own_window) {
3379                                         /* if window size isn't what expected, set fixed size */
3380                                         if (ev.xconfigure.width !=
3381                                             window.width
3382                                             || ev.xconfigure.height !=
3383                                             window.height) {
3384                                                 if (window.width != 0
3385                                                     && window.height != 0)
3386                                                         fixed_size = 1;
3387
3388                                                 /* clear old stuff before screwing up size and pos */
3389                                                 clear_text(1);
3390
3391                                                 {
3392                                                         XWindowAttributes
3393                                                             attrs;
3394                                                         if (XGetWindowAttributes(display, window.window, &attrs)) {
3395                                                                 window.
3396                                                                     width =
3397                                                                     attrs.
3398                                                                     width;
3399                                                                 window.
3400                                                                     height
3401                                                                     =
3402                                                                     attrs.
3403                                                                     height;
3404                                                         }
3405                                                 }
3406
3407                                                 text_width =
3408                                                     window.width -
3409                                                     border_margin * 2 - 1;
3410                                                 text_height =
3411                                                     window.height -
3412                                                     border_margin * 2 - 1;
3413                                         }
3414
3415                                         /* if position isn't what expected, set fixed pos, total_updates
3416                                          * avoids setting fixed_pos when window is set to weird locations
3417                                          * when started */
3418                                         if (total_updates >= 2
3419                                             && !fixed_pos
3420                                             && (window.x != ev.xconfigure.x
3421                                                 || window.y !=
3422                                                 ev.xconfigure.y)
3423                                             && (ev.xconfigure.x != 0
3424                                                 || ev.xconfigure.y != 0)) {
3425                                                 fixed_pos = 1;
3426                                         }
3427                                 }
3428                                 break;
3429 #endif
3430
3431                         default:
3432                                 break;
3433                         }
3434                 }
3435
3436                 /* XDBE doesn't seem to provide a way to clear the back buffer without
3437                  * interfering with the front buffer, other than passing XdbeBackground
3438                  * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
3439                  * redraw the text even if it wasn't part of the exposed area. OTOH,
3440                  * if we're not going to call draw_stuff at all, then no swap happens
3441                  * and we can safely do nothing.
3442                  */
3443
3444                 if (!XEmptyRegion(region)) {
3445 #ifdef XDBE
3446                         if (use_xdbe) {
3447                                 XRectangle r;
3448                                 r.x = text_start_x - border_margin;
3449                                 r.y = text_start_y - border_margin;
3450                                 r.width = text_width + border_margin * 2;
3451                                 r.height = text_height + border_margin * 2;
3452                                 XUnionRectWithRegion(&r, region, region);
3453                         }
3454 #endif
3455                         XSetRegion(display, window.gc, region);
3456 #ifdef XFT
3457                         if (use_xft)
3458                                 XftDrawSetClip(window.xftdraw, region);
3459 #endif
3460 #endif /* X11 */
3461                         draw_stuff();
3462 #ifdef X11
3463                         XDestroyRegion(region);
3464                         region = XCreateRegion();
3465                 }
3466 #endif /* X11 */
3467
3468         }
3469 }
3470
3471 static void load_config_file(const char *);
3472
3473 /* signal handler that reloads config file */
3474 static void reload_handler(int a)
3475 {
3476         ERR("Conky: received signal %d, reloading config\n", a);
3477
3478         if (current_config) {
3479                 clear_fs_stats();
3480                 load_config_file(current_config);
3481 #ifdef X11
3482                 load_fonts();
3483                 set_font();
3484 #endif /* X11 */
3485                 extract_variable_text(text);
3486                 free(text);
3487                 text = NULL;
3488                 update_text();
3489         }
3490 }
3491
3492 static void clean_up()
3493 {
3494 #ifdef X11
3495 #ifdef XDBE
3496         if (use_xdbe)
3497                 XdbeDeallocateBackBufferName(display, window.back_buffer);
3498 #endif
3499 #ifdef OWN_WINDOW
3500         if (own_window)
3501                 XDestroyWindow(display, window.window);
3502         else
3503 #endif
3504         {
3505                 XClearWindow(display, RootWindow(display, screen));
3506                 clear_text(1);
3507                 XFlush(display);
3508         }
3509
3510         XFreeGC(display, window.gc);
3511 #endif /* X11 */
3512
3513
3514         /* it is really pointless to free() memory at the end of program but ak|ra
3515          * wants me to do this */
3516
3517         free_text_objects();
3518
3519         if (text != original_text)
3520                 free(text);
3521
3522         free(current_config);
3523         free(current_mail_spool);
3524 #ifdef SETI
3525         free(seti_dir);
3526 #endif
3527 }
3528
3529 static void term_handler(int a)
3530 {
3531         a = a;                  /* to get rid of warning */
3532         clean_up();
3533         exit(0);
3534 }
3535
3536 static int string_to_bool(const char *s)
3537 {
3538         if (!s)
3539                 return 1;
3540         if (strcasecmp(s, "yes") == 0)
3541                 return 1;
3542         if (strcasecmp(s, "true") == 0)
3543                 return 1;
3544         if (strcasecmp(s, "1") == 0)
3545                 return 1;
3546         return 0;
3547 }
3548 #ifdef X11
3549 static enum alignment string_to_alignment(const char *s)
3550 {
3551         if (strcasecmp(s, "top_left") == 0)
3552                 return TOP_LEFT;
3553         else if (strcasecmp(s, "top_right") == 0)
3554                 return TOP_RIGHT;
3555         else if (strcasecmp(s, "bottom_left") == 0)
3556                 return BOTTOM_LEFT;
3557         else if (strcasecmp(s, "bottom_right") == 0)
3558                 return BOTTOM_RIGHT;
3559         else if (strcasecmp(s, "tl") == 0)
3560                 return TOP_LEFT;
3561         else if (strcasecmp(s, "tr") == 0)
3562                 return TOP_RIGHT;
3563         else if (strcasecmp(s, "bl") == 0)
3564                 return BOTTOM_LEFT;
3565         else if (strcasecmp(s, "br") == 0)
3566                 return BOTTOM_RIGHT;
3567         else if (strcasecmp(s, "none") == 0)
3568                 return NONE;
3569         return TOP_LEFT;
3570 }
3571 #endif /* X11 */
3572
3573
3574 static void set_default_configurations(void)
3575 {
3576         fork_to_background = 0;
3577         total_run_times = 0;
3578         info.cpu_avg_samples = 2;
3579         info.net_avg_samples = 2;
3580         info.memmax = 0;
3581         top_cpu = 0;
3582         top_mem = 0;
3583 #ifdef MPD
3584         strcpy(info.mpd.host, "localhost");
3585         info.mpd.port = 6600;
3586         info.mpd.status = "Checking status...";
3587 #endif
3588         use_spacer = 0;
3589 #ifdef X11
3590         out_to_console = 0;
3591 #else
3592         out_to_console = 1;
3593 #endif
3594 #ifdef X11
3595         default_fg_color = WhitePixel(display, screen);
3596         default_bg_color = BlackPixel(display, screen);
3597         default_out_color = BlackPixel(display, screen);
3598         draw_borders = 0;
3599         draw_shades = 1;
3600         draw_outline = 0;
3601         set_first_font("6x10");
3602         gap_x = 5;
3603         gap_y = 5;
3604         minimum_width = 5;
3605         minimum_height = 5;
3606 #ifdef OWN_WINDOW
3607         own_window = 0;
3608 #endif
3609         stippled_borders = 0;
3610         border_margin = 3;
3611         border_width = 1;
3612         text_alignment = BOTTOM_LEFT;
3613         on_bottom = 1;
3614 #endif /* X11 */
3615
3616         free(current_mail_spool);
3617         {
3618                 char buf[256];
3619                 variable_substitute(MAIL_FILE, buf, 256);
3620                 if (buf[0] != '\0')
3621                         current_mail_spool = strdup(buf);
3622         }
3623
3624         no_buffers = 1;
3625         update_interval = 10.0;
3626         stuff_in_upper_case = 0;
3627 #ifdef MLDONKEY
3628         mlconfig.mldonkey_hostname = "127.0.0.1";
3629         mlconfig.mldonkey_port = 4001;
3630         mlconfig.mldonkey_login = NULL;
3631         mlconfig.mldonkey_password = NULL;
3632 #endif
3633 }
3634
3635 static void load_config_file(const char *f)
3636 {
3637 #define CONF_ERR ERR("%s: %d: config file error", f, line)
3638         int line = 0;
3639         FILE *fp;
3640
3641         set_default_configurations();
3642
3643         fp = open_file(f, 0);
3644         if (!fp)
3645                 return;
3646
3647         while (!feof(fp)) {
3648                 char buf[256], *p, *p2, *name, *value;
3649                 line++;
3650                 if (fgets(buf, 256, fp) == NULL)
3651                         break;
3652
3653                 p = buf;
3654
3655                 /* break at comment */
3656                 p2 = strchr(p, '#');
3657                 if (p2)
3658                         *p2 = '\0';
3659
3660                 /* skip spaces */
3661                 while (*p && isspace((int) *p))
3662                         p++;
3663                 if (*p == '\0')
3664                         continue;       /* empty line */
3665
3666                 name = p;
3667
3668                 /* skip name */
3669                 p2 = p;
3670                 while (*p2 && !isspace((int) *p2))
3671                         p2++;
3672                 if (*p2 != '\0') {
3673                         *p2 = '\0';     /* break at name's end */
3674                         p2++;
3675                 }
3676
3677                 /* get value */
3678                 if (*p2) {
3679                         p = p2;
3680                         while (*p && isspace((int) *p))
3681                                 p++;
3682
3683                         value = p;
3684
3685                         p2 = value + strlen(value);
3686                         while (isspace((int) *(p2 - 1)))
3687                                 *--p2 = '\0';
3688                 } else {
3689                         value = 0;
3690                 }
3691
3692 #define CONF2(a) if (strcasecmp(name, a) == 0)
3693 #define CONF(a) else CONF2(a)
3694 #define CONF3(a,b) \
3695 else if (strcasecmp(name, a) == 0 || strcasecmp(name, a) == 0)
3696
3697
3698 #ifdef X11
3699                 CONF2("alignment") {
3700         if (value) {
3701                 int a = string_to_alignment(value);
3702                 if (a <= 0)
3703                         CONF_ERR;
3704                 else
3705                         text_alignment = a;
3706         } else
3707                 CONF_ERR;
3708                 }
3709                 CONF("on_bottom") {
3710                         if(value)
3711                                 on_bottom = string_to_bool(value);
3712                         else
3713                                 CONF_ERR;
3714                 }
3715                 CONF("background") {
3716                         fork_to_background = string_to_bool(value);
3717                 }
3718
3719 #else
3720                 CONF2("background") {
3721         fork_to_background = string_to_bool(value);
3722                 }
3723 #endif /* X11 */
3724 #ifdef X11
3725                 CONF("border_margin") {
3726                         if (value)
3727                                 border_margin = strtol(value, 0, 0);
3728                         else
3729                                 CONF_ERR;
3730                 }
3731                 CONF("border_width") {
3732                         if (value)
3733                                 border_width = strtol(value, 0, 0);
3734                         else
3735                                 CONF_ERR;
3736                 }
3737                 CONF("default_color") {
3738                         if (value)
3739                                 default_fg_color = get_x11_color(value);
3740                         else
3741                                 CONF_ERR;
3742                 }
3743                 CONF3("default_shade_color", "default_shadecolor") {
3744                         if (value)
3745                                 default_bg_color = get_x11_color(value);
3746                         else
3747                                 CONF_ERR;
3748                 }
3749                 CONF3("default_outline_color", "default_outlinecolor") {
3750                         if (value)
3751                                 default_out_color = get_x11_color(value);
3752                         else
3753                                 CONF_ERR;
3754                 }
3755 #endif /* X11 */
3756 #ifdef MPD
3757                 CONF("mpd_host") {
3758                         if (value)
3759                                 strcpy(info.mpd.host, value);
3760                         else
3761                                 CONF_ERR;
3762                 }
3763                 CONF("mpd_port") {
3764                         if (value) {
3765                                 info.mpd.port = strtol(value, 0, 0);
3766                                 if (info.mpd.port < 1
3767                                     || info.mpd.port > 0xffff)
3768                                         CONF_ERR;
3769                         }
3770                 }
3771 #endif
3772                 CONF("cpu_avg_samples") {
3773                         if (value) {
3774                                 cpu_avg_samples = strtol(value, 0, 0);
3775                                 if (cpu_avg_samples < 1
3776                                     || cpu_avg_samples > 14)
3777                                         CONF_ERR;
3778                                 else
3779                                         info.
3780                                             cpu_avg_samples
3781                                             = cpu_avg_samples;
3782                         } else
3783                                 CONF_ERR;
3784                 }
3785                 CONF("net_avg_samples") {
3786                         if (value) {
3787                                 net_avg_samples = strtol(value, 0, 0);
3788                                 if (net_avg_samples < 1
3789                                     || net_avg_samples > 14)
3790                                         CONF_ERR;
3791                                 else
3792                                         info.
3793                                             net_avg_samples
3794                                             = net_avg_samples;
3795                         } else
3796                                 CONF_ERR;
3797                 }
3798
3799
3800
3801
3802
3803
3804 #ifdef XDBE
3805                 CONF("double_buffer") {
3806         if (!own_window) {
3807                 use_xdbe = string_to_bool(value);
3808         }
3809                 }
3810 #endif
3811 #ifdef X11
3812                 CONF("override_utf8_locale") {
3813         utf8_mode = string_to_bool(value);
3814                 }
3815
3816                 CONF("draw_borders") {
3817                         draw_borders = string_to_bool(value);
3818                 }
3819                 CONF("draw_shades") {
3820                         draw_shades = string_to_bool(value);
3821                 }
3822                 CONF("draw_outline") {
3823                         draw_outline = string_to_bool(value);
3824                 }
3825 #endif /* X11 */
3826                 CONF("out_to_console") {
3827                         out_to_console = string_to_bool(value);
3828                 }
3829                 CONF("use_spacer") {
3830                         use_spacer = string_to_bool(value);
3831                 }
3832 #ifdef X11
3833 #ifdef XFT
3834                 CONF("use_xft") {
3835                         use_xft = string_to_bool(value);
3836                 }
3837                 CONF("font") {
3838                         if (!use_xft) {
3839                                 if (value) {
3840                                         set_first_font(value);
3841                                 } else
3842                                         CONF_ERR;
3843                         }
3844                 }
3845                 CONF("xftalpha") {
3846                         if (value && font_count >= 0)
3847                                 fonts[0].font_alpha = atof(value)
3848                                     * 65535.0;
3849                         else
3850                                 CONF_ERR;
3851                 }
3852                 CONF("xftfont") {
3853 #else
3854                 CONF("use_xft") {
3855                         if (string_to_bool(value))
3856                                 ERR("Xft not enabled");
3857                 }
3858                 CONF("xftfont") {
3859                         /* xftfont silently ignored when no Xft */
3860                 }
3861                 CONF("xftalpha") {
3862                         /* xftalpha is silently ignored when no Xft */
3863                 }
3864                 CONF("font") {
3865 #endif
3866                         if (value) {
3867                                 set_first_font(value);
3868                         } else
3869                                 CONF_ERR;
3870                 }
3871                 CONF("gap_x") {
3872                         if (value)
3873                                 gap_x = atoi(value);
3874                         else
3875                                 CONF_ERR;
3876                 }
3877                 CONF("gap_y") {
3878                         if (value)
3879                                 gap_y = atoi(value);
3880                         else
3881                                 CONF_ERR;
3882                 }
3883 #endif /* X11 */
3884                 CONF("mail_spool") {
3885                         if (value) {
3886                                 char buf[256];
3887                                 variable_substitute(value, buf, 256);
3888
3889                                 if (buf[0]
3890                                     != '\0') {
3891                                         if (current_mail_spool)
3892                                                 free(current_mail_spool);
3893                                         current_mail_spool = strdup(buf);
3894                                 }
3895                         } else
3896                                 CONF_ERR;
3897                 }
3898 #ifdef X11
3899                 CONF("minimum_size") {
3900                         if (value) {
3901                                 if (sscanf
3902                                     (value, "%d %d", &minimum_width,
3903                                      &minimum_height) != 2)
3904                                         if (sscanf
3905                                             (value, "%d",
3906                                              &minimum_width) != 1)
3907                                                 CONF_ERR;
3908                         } else
3909                                 CONF_ERR;
3910                 }
3911 #endif /* X11 */
3912                 CONF("no_buffers") {
3913                         no_buffers = string_to_bool(value);
3914                 }
3915 #ifdef MLDONKEY
3916                 CONF("mldonkey_hostname") {
3917                         if (value) {
3918                                 if (mlconfig.mldonkey_hostname != NULL) {
3919                                         free(mlconfig.mldonkey_hostname);
3920                                 }
3921                         mlconfig.mldonkey_hostname = strdup(value);
3922                         }
3923                         else
3924                                 CONF_ERR;
3925                 }
3926                 CONF("mldonkey_port") {
3927                         if (value)
3928                                 mlconfig.mldonkey_port = atoi(value);
3929                         else
3930                                 CONF_ERR;
3931                 }
3932                 CONF("mldonkey_login") {
3933                         if (value) {
3934                                 if (mlconfig.mldonkey_login != NULL) {
3935                                         free(mlconfig.mldonkey_login);
3936                                 }
3937                                 mlconfig.mldonkey_login = strdup(value);
3938                         }
3939                         else
3940                                 CONF_ERR;
3941                 }
3942                 CONF("mldonkey_password") {
3943                         if (value) {
3944                                 if (mlconfig.mldonkey_password != NULL) {
3945                                         free(mlconfig.mldonkey_password);
3946                                 }
3947                                 mlconfig.mldonkey_password = strdup(value);
3948                         }
3949                         else
3950                                 CONF_ERR;
3951                 }
3952 #endif
3953                 CONF("pad_percents") {
3954         pad_percents = atoi(value);
3955                 }
3956 #ifdef X11
3957 #ifdef OWN_WINDOW
3958                 CONF("own_window") {
3959                         own_window = string_to_bool(value);
3960 #ifdef XDBE
3961                         use_xdbe = 0;
3962 #endif
3963                 }
3964 #endif
3965                 CONF("stippled_borders") {
3966                         if (value)
3967                                 stippled_borders = strtol(value, 0, 0);
3968                         else
3969                                 stippled_borders = 4;
3970                 }
3971 #endif /* X11 */
3972                 CONF("temp1") {
3973                         ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
3974                 }
3975                 CONF("temp1") {
3976                         ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
3977                 }
3978                 CONF("update_interval") {
3979                         if (value)
3980                                 update_interval = strtod(value, 0);
3981                         else
3982                                 CONF_ERR;
3983                 }
3984                 CONF("total_run_times") {
3985                         if (value)
3986                                 total_run_times = strtod(value, 0);
3987                         else
3988                                 CONF_ERR;
3989                 }
3990                 CONF("uppercase") {
3991                         stuff_in_upper_case = string_to_bool(value);
3992                 }
3993 #ifdef SETI
3994                 CONF("seti_dir") {
3995                         seti_dir = (char *)
3996                             malloc(strlen(value)
3997                                    + 1);
3998                         strcpy(seti_dir, value);
3999                 }
4000 #endif
4001                 CONF("text") {
4002                         if (text != original_text)
4003                                 free(text);
4004
4005                         text = (char *)
4006                             malloc(1);
4007                         text[0]
4008                             = '\0';
4009
4010                         while (!feof(fp)) {
4011                                 unsigned
4012                                 int l = strlen(text);
4013                                 if (fgets(buf, 256, fp) == NULL)
4014                                         break;
4015                                 text = (char *)
4016                                     realloc(text, l + strlen(buf)
4017                                             + 1);
4018                                 strcat(text, buf);
4019
4020                                 if (strlen(text) > 1024 * 8)
4021                                         break;
4022                         }
4023                         fclose(fp);
4024                         return;
4025                 }
4026                 else
4027                 ERR("%s: %d: no such configuration: '%s'", f, line, name);
4028
4029 #undef CONF
4030 #undef CONF2
4031         }
4032
4033         fclose(fp);
4034 #undef CONF_ERR
4035 }
4036
4037                                                                                                                                                                                         /* : means that character before that takes an argument */
4038 static const char *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
4039 #ifdef X11
4040                 "x:y:w:a:f:"
4041 #ifdef OWN_WINDOW
4042     "o"
4043 #endif
4044 #ifdef XDBE
4045     "b"
4046 #endif
4047 #endif /* X11 */
4048     ;
4049
4050
4051 int main(int argc, char **argv)
4052 {
4053         /* handle command line parameters that don't change configs */
4054 #ifdef X11
4055         char *s;
4056         char temp[10];
4057         unsigned int x;
4058
4059         if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && 
4060                      *s) || ((s = getenv("LANG")) && *s)) {
4061                 strcpy(temp, s);
4062                 for(x = 0; x < strlen(s) ; x++) {
4063                         temp[x] = tolower(s[x]);
4064                 }
4065                 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
4066                         utf8_mode = 1;
4067                 }
4068         }
4069         if (!setlocale(LC_CTYPE, "")) {
4070                 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
4071                 return 1;
4072         }
4073 #endif /* X11 */
4074         while (1) {
4075                 int c = getopt(argc,
4076                                argv,
4077                                getopt_string);
4078                 if (c == -1)
4079                         break;
4080
4081                 switch (c) {
4082                 case 'v':
4083                 case 'V':
4084                         printf
4085                             ("Conky " VERSION " compiled " __DATE__ "\n");
4086                         return 0;
4087
4088                 case 'c':
4089                         /* if current_config is set to a strdup of CONFIG_FILE, free it (even
4090                          * though free() does the NULL check itself;), then load optarg value */
4091                         if (current_config)
4092                                 free(current_config);
4093                         current_config = strdup(optarg);
4094                         break;
4095
4096                 case 'h':
4097                         printf
4098                                         ("Usage: %s [OPTION]...\n"
4099                                         "Conky is a system monitor that renders text on desktop or to own transparent\n"
4100                                         "window. Command line options will override configurations defined in config\n"
4101                                         "file.\n"
4102                                         "   -V            version\n"
4103                                         "   -c FILE       config file to load instead of "
4104                                         CONFIG_FILE
4105                                         "\n"
4106                                         "   -d            daemonize, fork to background\n"
4107                                         "   -h            help\n"
4108 #ifdef X11
4109                                         "   -a ALIGNMENT  text alignment on screen, {top,bottom}_{left,right}\n"
4110                                         "   -f FONT       font to use\n"
4111 #ifdef OWN_WINDOW
4112                                         "   -o            create own window to draw\n"
4113 #endif
4114 #ifdef XDBE
4115                                         "   -b            double buffer (prevents flickering)\n"
4116 #endif
4117                                         "   -w WIN_ID     window id to draw\n"
4118                                         "   -x X          x position\n"
4119                                         "   -y Y          y position\n"
4120 #endif /* X11 */
4121                                         "   -t TEXT       text to render, remember single quotes, like -t '$uptime'\n"
4122                                         "   -u SECS       update interval\n"
4123                                         "   -i NUM        number of times to update Conky\n", argv[0]);
4124                         return 0;
4125 #ifdef X11
4126                 case 'w':
4127                         window.window = strtol(optarg, 0, 0);
4128                         break;
4129 #endif /* X11 */
4130
4131                 case '?':
4132                         exit(EXIT_FAILURE);
4133                 }
4134         }
4135 #ifdef X11
4136         /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
4137         init_X11();
4138 #endif /* X11 */
4139
4140         tmpstring1 = (char *)
4141             malloc(TEXT_BUFFER_SIZE);
4142         tmpstring2 = (char *)
4143             malloc(TEXT_BUFFER_SIZE);
4144
4145         /* load current_config or CONFIG_FILE */
4146
4147 #ifdef CONFIG_FILE
4148         if (current_config == NULL) {
4149                 /* load default config file */
4150                 char buf[256];
4151
4152                 variable_substitute(CONFIG_FILE, buf, 256);
4153
4154                 if (buf[0] != '\0')
4155                         current_config = strdup(buf);
4156         }
4157 #endif
4158
4159         if (current_config != NULL)
4160                 load_config_file(current_config);
4161         else
4162                 set_default_configurations();
4163
4164 #ifdef MAIL_FILE
4165         if (current_mail_spool == NULL) {
4166                 char buf[256];
4167                 variable_substitute(MAIL_FILE, buf, 256);
4168
4169                 if (buf[0] != '\0')
4170                         current_mail_spool = strdup(buf);
4171         }
4172 #endif
4173
4174         /* handle other command line arguments */
4175
4176         optind = 0;
4177
4178         while (1) {
4179                 int c = getopt(argc,
4180                                argv,
4181                                getopt_string);
4182                 if (c == -1)
4183                         break;
4184
4185                 switch (c) {
4186                 case 'd':
4187                         fork_to_background = 1;
4188                         break;
4189
4190 #ifdef X11
4191                         case 'f':
4192                         set_first_font(optarg);
4193                         break;
4194                         case 'a':
4195                                 text_alignment = string_to_alignment(optarg);
4196                                 break;
4197
4198 #ifdef OWN_WINDOW
4199                 case 'o':
4200                         own_window = 1;
4201 #ifdef XDBE
4202                         use_xdbe = 0;
4203 #endif
4204                         break;
4205 #endif
4206 #ifdef XDBE
4207                 case 'b':
4208                         use_xdbe = 1;
4209                         break;
4210 #endif
4211 #endif /* X11 */
4212                 case 't':
4213                         if (text != original_text)
4214                                 free(text);
4215                         text = strdup(optarg);
4216                         convert_escapes(text);
4217                         break;
4218
4219                 case 'u':
4220                         update_interval = strtod(optarg, 0);
4221                         break;
4222
4223                 case 'i':
4224                         total_run_times = strtod(optarg, 0);
4225                         break;
4226 #ifdef X11
4227                 case 'x':
4228                         gap_x = atoi(optarg);
4229                         break;
4230
4231                 case 'y':
4232                         gap_y = atoi(optarg);
4233                         break;
4234 #endif /* X11 */
4235
4236                 case '?':
4237                         exit(EXIT_FAILURE);
4238                 }
4239         }
4240
4241 #ifdef X11
4242         /* load font */
4243         load_fonts();
4244 #endif /* X11 */
4245
4246         /* generate text and get initial size */
4247         extract_variable_text(text);
4248         if (text != original_text) {
4249                 free(text);
4250         }
4251         text = NULL;
4252
4253         update_uname();
4254
4255         generate_text();
4256 #ifdef X11
4257         update_text_area();     /* to get initial size of the window */
4258
4259 #if defined OWN_WINDOW
4260         init_window
4261             (own_window,
4262              text_width + border_margin * 2 + 1,
4263              text_height + border_margin * 2 + 1,
4264              on_bottom, fixed_pos);
4265 #else
4266         init_window
4267                 (own_window,
4268                  text_width + border_margin * 2 + 1,
4269                  text_height + border_margin * 2 + 1,
4270                  on_bottom);
4271         
4272 #endif
4273
4274         update_text_area();     /* to position text/window on screen */
4275 #endif /* X11 */
4276
4277 /*#ifdef CAIRO
4278 // why the fuck not?
4279 //do_it();
4280 #endif*/
4281
4282 #ifdef X11
4283 #ifdef OWN_WINDOW
4284         if (own_window && !fixed_pos)
4285                 XMoveWindow(display, window.window, window.x, window.y);
4286 #endif
4287
4288         create_gc();
4289
4290         set_font();
4291         draw_stuff();
4292 #endif /* X11 */
4293
4294         /* fork */
4295         if (fork_to_background) {
4296                 int ret = fork();
4297                 switch (ret) {
4298                 case -1:
4299                         ERR("can't fork() to background: %s",
4300                             strerror(errno));
4301                         break;
4302
4303                 case 0:
4304                         break;
4305
4306                 default:
4307                         fprintf
4308                             (stderr,
4309                              "Conky: forked to background, pid is %d\n",
4310                              ret);
4311                         return 0;
4312                 }
4313         }
4314
4315         /* set SIGUSR1, SIGINT and SIGTERM handlers */
4316         {
4317                 struct
4318                 sigaction sa;
4319
4320                 sa.sa_handler = reload_handler;
4321                 sigemptyset(&sa.sa_mask);
4322                 sa.sa_flags = SA_RESTART;
4323                 if (sigaction(SIGUSR1, &sa, NULL) != 0)
4324                         ERR("can't set signal handler for SIGUSR1: %s",
4325                             strerror(errno));
4326
4327                 sa.sa_handler = term_handler;
4328                 sigemptyset(&sa.sa_mask);
4329                 sa.sa_flags = SA_RESTART;
4330                 if (sigaction(SIGINT, &sa, NULL) != 0)
4331                         ERR("can't set signal handler for SIGINT: %s",
4332                             strerror(errno));
4333
4334                 sa.sa_handler = term_handler;
4335                 sigemptyset(&sa.sa_mask);
4336                 sa.sa_flags = SA_RESTART;
4337                 if (sigaction(SIGTERM, &sa, NULL) != 0)
4338                         ERR("can't set signal handler for SIGTERM: %s",
4339                             strerror(errno));
4340         }
4341         main_loop();
4342         free(tmpstring1);
4343         free(tmpstring2);
4344         return 0;
4345 }