#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "client.h" int update_interval; static XrmOptionDescRec opDescList[] = { {"-ready", "*ready", XrmoptionSepArg, NULL}, {"-standby", "*standby", XrmoptionSepArg, NULL}, {"-alert", "*alert", XrmoptionSepArg, NULL}, {"-alertflash", "*alertFlash", XrmoptionSepArg, NULL}, {"-feint", "*feint", XrmoptionSepArg, NULL}, {"-readyfeint", "*readyFeint", XrmoptionSepArg, NULL}, {"-foreground", "*foreground", XrmoptionSepArg, NULL}, {"-background", "*background", XrmoptionSepArg, NULL}, {"-fg", "*foreground", XrmoptionSepArg, NULL}, {"-bg", "*background", XrmoptionSepArg, NULL}, {"-fn", "*Font", XrmoptionSepArg, NULL}, {"-stripe", "*Stripe", XrmoptionSepArg, NULL}, {"-geometry", "*geometry", XrmoptionSepArg, NULL}, {"-display", ".display", XrmoptionSepArg, NULL}, {"-port", ".port", XrmoptionSepArg, NULL}, {"-help", ".help", XrmoptionNoArg, "on"}, {"-xrm", NULL, XrmoptionResArg, NULL}, {"-update", ".update", XrmoptionSepArg, NULL} }; #define opDescLen (sizeof(opDescList)/sizeof(XrmOptionDescRec)) static XrmDatabase commandlineDB=NULL, rDB; char *displayname=NULL; char *progname="loadmonitor"; static void usage(FILE *stream, int exitstatus) { fprintf(stream, "LoadMonitor version 1.1 protocol 1\n" "Usage: %s [-options ...] [hostname]\n" "\n" "where options are:\n" " -ready colour for normal operation\n" " -standby colour for port unreachable\n" " -alert colour for host unreachable\n" " -alertflash second colour for host unreachable\n" " -feint colour for load lines\n" " -readyfeint second colour for load lines\n" " -foreground -fg foregound colour\n" " -background -bg background colour\n" " -fn font used for displaying text\n" " -geometry initial geometry\n" " -stripe height of status bar\n" " -display name of X server\n" " -port udp port to use [default 400]\n" " -update update interval in seconds\n" " -xrm resource line\n" " -help display usage\n", progname); if (fflush(stream) || ferror(stream)) exit(1); exit(exitstatus); } void options_parse(int argc, char *argv[]) { struct hostent *hostent; char *slash; char *type; char *optname; char **newargv; XrmValue value; int i; if (argc>0) progname=argv[0]; slash=strrchr(progname, '/'); if (slash) progname=slash+1; newargv=malloc((1+argc)*sizeof(char *)); if (!newargv) { fprintf(stderr, "%s: malloc: %s\n", progname, strerror(ENOMEM)); exit(1); } for (i=0; i0) update_interval=atoi((value.addr)); } strcpy(optname, progname); strcat(optname, ".display"); if (XrmGetResource(commandlineDB, optname, "LoadMonitor.Display", &type, &value)) { displayname=malloc(value.size+1); if (!displayname) exit(1); memcpy(displayname, value.addr, value.size); displayname[value.size]=0; } strcpy(optname, progname); strcat(optname, ".help"); if (XrmGetResource(commandlineDB, optname, "LoadMonitor.Help", &type, &value)) { usage(stdout, 0); } if (argc>2) { usage(stderr, 1); } host=argc>1?argv[1]:"localhost"; if (!(hostent=gethostbyname(host))) { fprintf(stderr, "%s: Can't lookup hostname '%s'\n", progname, host); exit(1); } if (hostent->h_length!=4) { fprintf(stderr, "%s: address length for %s != 4\n", progname, host); exit(1); } memcpy(&addr.sin_addr, hostent->h_addr, 4); leafname=strdup(host); if (!leafname) { fprintf(stderr, "%s: strdup: %s\n", progname, strerror(ENOMEM)); exit(1); } slash=strchr(leafname, '.'); if (slash) *slash=0; } void options_xrdb(Display *display) { char *homedir, *env; homedir=getenv("HOME"); if (!homedir) { struct passwd *pw; char *user=getenv("USER"); pw=user? getpwnam(user):getpwuid(getuid()); homedir=pw? pw->pw_dir:NULL; } if (homedir) { homedir=strdup(homedir); if (!homedir) goto nomem; } rDB=XrmGetFileDatabase(APPLOADDIR CLASS_NAME); if (((_XPrivDisplay)display)->xdefaults) { XrmDatabase serverDB; serverDB=XrmGetStringDatabase( ((_XPrivDisplay)display)->xdefaults); XrmMergeDatabases(serverDB, &rDB); } else { if (homedir) { XrmDatabase serverDB; char *xdefaults=malloc(strlen(homedir)+11); if (!xdefaults) goto nomem; strcpy(xdefaults, homedir); strcat(xdefaults, "/.Xdefaults"); serverDB=XrmGetFileDatabase(xdefaults); free(xdefaults); XrmMergeDatabases(serverDB, &rDB); } } env=getenv("XENVIRONMENT"); if (env) { XrmDatabase envDB; envDB=XrmGetFileDatabase(env); XrmMergeDatabases(envDB, &rDB); } else { char hostname[MAXHOSTNAMELEN+1]; if (!gethostname(hostname, MAXHOSTNAMELEN)) { XrmDatabase hostDB; char *host; hostname[MAXHOSTNAMELEN]=0; host=malloc(strlen(homedir)+strlen(hostname)+12); if (!host) goto nomem; strcpy(host, homedir); strcat(host, "/.Xdefaults-"); strcat(host, hostname); hostDB=XrmGetFileDatabase(host); XrmMergeDatabases(hostDB, &rDB); } } XrmMergeDatabases(commandlineDB, &rDB); return; nomem: fprintf(stderr, "%s: malloc: %s\n", progname, strerror(ENOMEM)); exit(1); } char *options_get(char *xinstance, char *xclass, char *valuedefault) { char *fullinstance, *fullclass; char *type, *valuestr; XrmValue value; fullinstance=malloc(strlen(progname)+strlen(xinstance)+1); if (!fullinstance) goto nomem; strcpy(fullinstance, progname); strcat(fullinstance, xinstance); fullclass=malloc(strlen(progname)+strlen(xclass)+1); if (!fullclass) goto nomem; strcpy(fullclass, progname); strcat(fullclass, xclass); if (XrmGetResource(rDB, fullinstance, fullclass, &type, &value)) { valuestr=malloc(value.size+1); if (!valuestr) goto nomem; memcpy(valuestr, value.addr, value.size); valuestr[value.size]=0; free(fullinstance); free(fullclass); return valuestr; } free(fullinstance); free(fullclass); if (!valuedefault) return NULL; valuestr=strdup(valuedefault); if (valuestr) return valuestr; nomem: fprintf(stderr, "%s: malloc: %s\n", progname, strerror(ENOMEM)); exit(1); } void options_daemonize(void) { }