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