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