home
/
code
/
khatus.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Enable extra warnings and remove unused variable
[khatus.git]
/
x5
/
khatus.c
diff --git
a/x5/khatus.c
b/x5/khatus.c
index
a41351a
..
3cccf56
100644
(file)
--- a/
x5/khatus.c
+++ b/
x5/khatus.c
@@
-1,6
+1,8
@@
#include <fcntl.h>
#include <unistd.h>
#include <sys/select.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/select.h>
+#include <sys/stat.h>
+#include <X11/Xlib.h>
#include <assert.h>
#include <ctype.h>
#include <assert.h>
#include <ctype.h>
@@
-17,6
+19,7
@@
char *argv0;
char *argv0;
+/* TODO: Convert file list to file array. */
typedef struct File File;
struct File {
char *name;
typedef struct File File;
struct File {
char *name;
@@
-35,12
+38,14
@@
struct Config {
File * files;
int file_count;
int total_width;
File * files;
int file_count;
int total_width;
+ int output_to_x_root_window;
} defaults = {
.interval = 1,
.separator = "|",
.files = NULL,
.file_count = 0,
.total_width = 0,
} defaults = {
.interval = 1,
.separator = "|",
.files = NULL,
.file_count = 0,
.total_width = 0,
+ .output_to_x_root_window = 0,
};
void
};
void
@@
-169,6
+174,11
@@
parse_opts_opt(Config *cfg, int argc, char *argv[], int i)
switch (argv[i][1]) {
case 'i': parse_opts_opt_i(cfg, argc, argv, ++i); break; /* TODO: Generic set_int */
case 's': parse_opts_opt_s(cfg, argc, argv, ++i); break; /* TODO: Generic set_str */
switch (argv[i][1]) {
case 'i': parse_opts_opt_i(cfg, argc, argv, ++i); break; /* TODO: Generic set_int */
case 's': parse_opts_opt_s(cfg, argc, argv, ++i); break; /* TODO: Generic set_str */
+ case 'x': {
+ cfg->output_to_x_root_window = 1;
+ opts_parse_any(cfg, argc, argv, ++i);
+ break;
+ }
default : usage("Option \"%s\" is invalid\n", argv[i]);
}
}
default : usage("Option \"%s\" is invalid\n", argv[i]);
}
}
@@
-218,7
+228,7
@@
opts_parse_any(Config *cfg, int argc, char *argv[], int i)
}
void
}
void
-opts_parse(Config *cfg, int argc, char *argv[]
, int i
)
+opts_parse(Config *cfg, int argc, char *argv[])
{
opts_parse_any(cfg, argc, argv, 1);
{
opts_parse_any(cfg, argc, argv, 1);
@@
-235,27
+245,21
@@
opts_parse(Config *cfg, int argc, char *argv[], int i)
void
read_one(File *f, char *buf)
{
void
read_one(File *f, char *buf)
{
- ssize_t n;
+ ssize_t current;
+ ssize_t total;
char *b;
char *b;
+ char c;
+ current = 0;
+ total = 0;
+ c = '\0';
b = buf + f->pos;
memset(b, ' ', f->width);
b = buf + f->pos;
memset(b, ' ', f->width);
- while ((n = read(f->fd, b, f->width)) > 0) {
- b += n;
- debug("read %zd from %s\n", n, f->name);
- }
-
- if (n > -1) {
- if (*(b - 1) == '\n')
- *(b - 1) = ' ';
- } else {
- error(
- "Failed to read: \"%s\". Error: %s\n",
- f->name,
- strerror(errno)
- );
- }
-
+ while ((current = read(f->fd, &c, 1)) && c != '\n' && c != '\0' && total++ < f->width)
+ *b++ = c;
+ if (current == -1)
+ error("Failed to read: \"%s\". Error: %s\n", f->name, strerror(errno));
+ /* TODO Record timestamp read */
close(f->fd);
f->fd = -1;
}
close(f->fd);
f->fd = -1;
}
@@
-266,11
+270,16
@@
read_all(Config *cfg, char *buf)
fd_set fds;
int maxfd;
int ready;
fd_set fds;
int maxfd;
int ready;
+ struct stat st;
FD_ZERO(&fds);
FD_ZERO(&fds);
-
- /* TODO: stat then check TTL */
+ /* TODO: Check TTL */
for (File *f = cfg->files; f; f = f->next) {
for (File *f = cfg->files; f; f = f->next) {
+ /* TODO: Create the FIFO if it doesn't already exist. */
+ if (lstat(f->name, &st) < 0)
+ fatal("Cannot stat \"%s\". Error: %s\n", f->name, strerror(errno));
+ if (!(st.st_mode & S_IFIFO))
+ fatal("\"%s\" is not a FIFO\n", f->name);
debug("opening: %s\n", f->name);
if (f->fd < 0)
f->fd = open(f->name, O_RDONLY | O_NONBLOCK);
debug("opening: %s\n", f->name);
if (f->fd < 0)
f->fd = open(f->name, O_RDONLY | O_NONBLOCK);
@@
-304,10
+313,11
@@
main(int argc, char *argv[])
int prefix = 0;
char *buf;
Config *cfg = &defaults;
int prefix = 0;
char *buf;
Config *cfg = &defaults;
+ Display *display;
argv0 = argv[0];
argv0 = argv[0];
- opts_parse(cfg, argc, argv
, 1
);
+ opts_parse(cfg, argc, argv);
debug("argv0 = %s\n", argv0);
config_print(cfg);
if (cfg->files == NULL)
debug("argv0 = %s\n", argv0);
config_print(cfg);
if (cfg->files == NULL)
@@
-336,10
+346,23
@@
main(int argc, char *argv[])
}
}
}
}
- printf("%s\n", buf);
+ if (cfg->output_to_x_root_window && !(display = XOpenDisplay(NULL)))
+ fatal("XOpenDisplay failed with: %p\n", display);
/* TODO: nanosleep and nano time diff */
for (;;) {
/* TODO: nanosleep and nano time diff */
for (;;) {
+ /* TODO: Check TTL and maybe blank-out */
+ /* TODO: How to trigger TTL check? On select? Alarm signal? */
+ /* TODO: Set timeout on read_all based on diff of last time of
+ * read_all and desired time of next TTL check.
+ * */
read_all(cfg, buf);
read_all(cfg, buf);
- printf("%s\n", buf);
+ if (cfg->output_to_x_root_window) {
+ if (XStoreName(display, DefaultRootWindow(display), buf) < 0)
+ fatal("XStoreName failed.\n");
+ XFlush(display);
+ } else {
+ puts(buf);
+ fflush(stdout);
+ }
}
}
}
}
This page took
0.034728 seconds
and
4
git commands to generate.