-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqpush.c
127 lines (117 loc) · 3.23 KB
/
qpush.c
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
/*
* libqueue - provides persistent, named data storage queues
* Copyright (C) 2014-2016 Jens Oliver John <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* */
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include <getopt.h>
#include "queue.h"
#include <signal.h>
#include "queueutils.h"
/* global so that we can close the database properly in case of an
* interrupt while reading from stdin */
struct Queue *q;
static inline void push_buf(struct Queue *q,
struct QueueData *d, char*linebuf, size_t*linebuflen) {
d->v = linebuf;
d->vlen = *linebuflen-(2*sizeof(char));//remove trailing newline
if(queue_push(q, d) != LIBQUEUE_SUCCESS){
fputs("Failed to push value onto the queue: ", stdout);
puts(linebuf);
}
memset(linebuf, 0, *linebuflen);
}
void sighandler(int s) {
puts("Cought SIGINT and SIGTERM while reading from stdin.\n"
"I'll try to exit gracefully...");
exit(EXIT_FAILURE);
}
void do_at_exit(void) {
if(queue_close(q) != LIBQUEUE_SUCCESS)
puts("Failed to close the queue");
}
int main(int argc, char **argv) {
struct QueueData d;
int i = 0;
char *cq = NULL;
int opt = 0;
int use_stdin = 0;
int use_null = 0;
char *linebuf = NULL;
size_t linebuflen = 0;
while((opt = getopt(argc, argv, "0rhq:")) != -1)
switch(opt) {
case 'q':
cq = strdup(optarg);
break;
case 'r':
use_stdin = 1;
break;
case '0':
use_null = 1;
break;
default:
case 'h':
puts("Usage: qpush [-h] [-q queue-name] [--] <args>");
return EXIT_FAILURE;
}
i = optind-1;
q = queue_open(SELECTQUEUE(cq));
if(0 == queue_is_opened(q)) {
fprintf(stderr,"Failed to open the queue:%s\n", queue_get_last_error(q));
if (LIBQUEUE_FAILURE == queue_close(q)) {
fprintf(stderr,"Failed to close queue:%s\n", queue_get_last_error(q));
}
return EXIT_FAILURE;
}
atexit(do_at_exit);
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);
switch(use_stdin) {
default:
case 0:
while(argv[++i]) {
d.v = argv[i];
d.vlen = sizeof(char)*(strlen(argv[i])+1);
if(queue_push(q, &d) != LIBQUEUE_SUCCESS) {
fputs("Failed to push value onto the queue: ", stdout);
puts(argv[i]);
}
}
break;
case 1:
linebuf = calloc(256, sizeof(char));
linebuflen = 256*sizeof(char);
switch(use_null) {
default:
case 0:
while(getline(&linebuf, &linebuflen, stdin) != -1)
push_buf(q, &d, linebuf, &linebuflen);
break; // case 0:use_null
case 1:
while(getdelim(&linebuf, &linebuflen, '\0', stdin) != -1)
push_buf(q, &d, linebuf, &linebuflen);
break;
}
break; // case 1:use_stdin
free(linebuf);
}
if(cq != NULL)
free(cq);
return 0;
}