1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#include <sys/types.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/event.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int printer_ready;
char * printer;
char * queue_dir;
char * trigger;
#define BUFLEN 50
char queue_dir_new[BUFLEN];
char trigger_cmd[BUFLEN];
int open_and_subscribe(struct kevent * change, const char * path)
{
int f;
f = open(path, O_RDONLY);
if (f == -1) {
perror("open");
return -1;
}
EV_SET(change, f, EVFILT_VNODE,
EV_ADD | EV_ENABLE | EV_ONESHOT,
NOTE_DELETE | NOTE_EXTEND |
NOTE_WRITE | NOTE_ATTRIB,
0, 0);
}
int check_printer_status(){
struct stat buf;
//printf("%s\n", printer);
int r = stat(printer, &buf);
if (r<0) {
if (printer_ready != 0) {
printf("Printer offline\n");
}
printer_ready = 0;
if (errno == ENOENT) {
printf("Printer not detected\n");
} else {
perror("stat");
return -1;
}
} else {
if (printer_ready != 1) {
printf("Printer online\n");
print_loop(printer, queue_dir);
}
printer_ready = 1;
}
return 0;
}
int print_loop()
{
FILE * p;
int status;
char buf[BUFLEN];
printf("Print loop\n");
p = popen(trigger_cmd, "r");
if (p == NULL)
perror("popen");
while (fgets(buf, BUFLEN, p) != NULL)
printf("%s", buf);
status = pclose(p);
if (status != 0) {
printf("Trigger exited with status %d\n", status);
}
return status;
}
int main(int argc, const char * argv[])
{
int fd, fq, kq, nev;
struct kevent change;
struct kevent event;
int len;
if (argc != 4) {
fprintf(stderr, "usage: %s printer queue_dir trigger\n", argv[0]);
return -1;
}
printer = strdup(argv[1]);
queue_dir = strdup(argv[2]);
trigger = strdup(argv[3]);
len = snprintf(trigger_cmd, BUFLEN, "%s %s %s", trigger, printer, queue_dir);
if (len >= BUFLEN - 1) {
printf("Buffer too small\n");
return -1;
}
len = snprintf(queue_dir_new, BUFLEN, "%s/new", queue_dir);
if (len >= BUFLEN - 1) {
printf("Buffer too small\n");
return -1;
}
printer_ready = 0;
kq = kqueue();
if (kq == -1)
perror("kqueue");
fq = open_and_subscribe(&change, queue_dir_new);
struct timespec timeout;
timeout.tv_sec = 5;
timeout.tv_nsec = 0;
check_printer_status();
for (;;) {
nev = kevent(kq, &change, 1, &event, 1, &timeout);
if (nev == -1)
perror("kevent");
if (nev == 0) { // timeout
check_printer_status();
} else if (nev > 0) {
if (event.fflags & NOTE_DELETE) {
continue;
}
printf("Got event for %d\n", event.ident);
if (printer_ready) {
print_loop(printer, queue_dir, trigger);
}
}
}
close(kq);
close(fq);
close(fd);
return EXIT_SUCCESS;
}
|