Browse Source

Fix #2 Implements stdin operations

Seems to work with stdin
Yann Weber 7 years ago
parent
commit
67e9aa752a

+ 1
- 2
src/Makefile.am View File

@@ -1,9 +1,8 @@
1 1
 bin_PROGRAMS = ttail
2 2
 noinst_LIBRARIES= libttail.a
3 3
 
4
-libttail_a_SOURCES = ttail_init.c ttail_search.c ttail_search_files.c
4
+libttail_a_SOURCES = 	ttail_init.c ttail_search.c ttail_search_files.c ttail_search_std.c
5 5
 ttail_SOURCES = main.c $(libttail_a_SOURCES)
6
-#ttail_SOURCES = main.c ttail_init.c ttail_search.c ttail_search_files.c
7 6
 if HUGEFILE
8 7
 ttail_CFLAGS = @CFLAGS@ -DTTAIL_HUGE_FILE
9 8
 libttail_a_CFLAGS = @CFLAGS@ -DTTAIL_HUGE_FILE

+ 1
- 42
src/include/ttail_search.h View File

@@ -10,7 +10,6 @@
10 10
 #include <unistd.h>
11 11
 
12 12
 typedef union _ttail_search_u ttail_search_t;
13
-typedef struct _ttail_search_stdin_s ttail_search_stdin_t;
14 13
 #include "ttail.h"
15 14
 typedef char* (*ttail_search_subst_f)(ttail_t*, const char*);
16 15
 /**@brief Comparison results
@@ -22,27 +21,9 @@ typedef char* (*ttail_search_subst_f)(ttail_t*, const char*);
22 21
  */
23 22
 typedef int ttail_cmp_res;
24 23
 
25
-/**@brief Convenient wrapper for getline
26
- *@param ttail_t* TTAIL
27
- *@param size_t ID file id
28
- *@return @ref getline()
29
- */
30
-#define ttail_getline(TTAIL, ID, SESS) (getline(\
31
-	&(TTAIL->session->SESS.buf), &(TTAIL->session->SESS.buf_sz),\
32
-	TTAIL->logfile[ID]))
33
-/*<!Accessor to getline wrapper buffer */
34
-#define ttail_std_getline_buf(TTAIL) (TTAIL->session->std.buf)
35
-#define ttail_std_getline(TTAIL, ID) ttail_getline(TTAIL, ID, std)
36
-
37 24
 #include "config.h"
38 25
 #include "ttail_search_files.h"
39
-
40
-/*<! Private search session for stdin */
41
-struct _ttail_search_stdin_s
42
-{
43
-	char *buff;
44
-	size_t buff_sz;
45
-};
26
+#include "ttail_search_std.h"
46 27
 
47 28
 /**@brief Search session is an union depending in the search method
48 29
  * employed
@@ -65,20 +46,6 @@ int ttail_search_closest(ttail_t*);
65 46
  */
66 47
 void ttail_search_print_res(ttail_t*);
67 48
 
68
-/**@brief @ref ttail_search_closest() implementation for stdin
69
- *@param ttail_t*
70
- *@param struct tm* tmin
71
- *@return 0 if ok -1 if fatal error 1 if not found
72
- *@todo implement me
73
- */
74
-int _ttail_search_closest_stdin(ttail_t*);
75
-/**@brief Output result loglines to stdout
76
- *@param ttail_t*
77
- *@param int fd
78
- *@todo implement me
79
- */
80
-void _ttail_search_print_stdin(ttail_t*, int);
81
-
82 49
 /**@brief Loglines comparison function
83 50
  *@param ttail_t* ttail
84 51
  *@param const char* logline a
@@ -87,14 +54,6 @@ void _ttail_search_print_stdin(ttail_t*, int);
87 54
  */
88 55
 ttail_cmp_res ttail_cmp_loglines(ttail_t*, const char*, const char*);
89 56
 
90
-/**@brief compare a logline to a date
91
- *@param ttail_t* ttail
92
- *@param const char* logline
93
- *@param struct tm* @see time.h
94
- *@return @ref ttail_cmp_res
95
- */
96
-ttail_cmp_res ttail_cmp_logline(ttail_t*, const char*, struct tm*);
97
-
98 57
 /**@brief Fetch a date from a logline
99 58
  *@param ttail_t* ttail
100 59
  *@param const char* logline

+ 4
- 1
src/include/ttail_search_files.h View File

@@ -62,7 +62,10 @@ struct _ttail_search_file_s
62 62
  *@param size_t ID file id
63 63
  *@return @ref getline()
64 64
  */
65
-#define ttail_file_getline(TTAIL, ID) (ttail_getline(TTAIL, ID, file))
65
+#define ttail_file_getline(TTAIL, ID) (getline(\
66
+	&(TTAIL->session->file.buf), &(TTAIL->session->file.buf_sz),\
67
+	TTAIL->logfile[ID]))
68
+
66 69
 /*<!Accessor to getline wrapper buffer */
67 70
 #define ttail_file_getline_buf(TTAIL) (TTAIL->session->file.buf)
68 71
 

+ 59
- 0
src/include/ttail_search_std.h View File

@@ -0,0 +1,59 @@
1
+#ifndef _ttail_search_std_h__
2
+#define _ttail_search_std_h__
3
+
4
+#include <errno.h>
5
+#include <string.h>
6
+#include <stdlib.h>
7
+
8
+typedef struct _ttail_search_stdin_s ttail_search_stdin_t;
9
+/*<! Private search session for stdin */
10
+struct _ttail_search_stdin_s
11
+{
12
+	char *buff;
13
+	size_t buff_sz;
14
+};
15
+
16
+#include "config.h"
17
+#include "ttail.h"
18
+#include "ttail_search.h"
19
+
20
+/**@brief Convenient wrapper for getline
21
+ *@param ttail_t* TTAIL
22
+ *@param size_t ID file id
23
+ *@return @ref getline()
24
+ */
25
+#define ttail_std_getline(TTAIL) (getline(\
26
+	&(TTAIL->session->std.buff), &(TTAIL->session->std.buff_sz), stdin))
27
+/*<!Accessor to getline wrapper buffer */
28
+#define ttail_std_getline_buff(TTAIL) (TTAIL->session->std.buff)
29
+
30
+/**@brief init private session members
31
+ *@patam ttail_t* t
32
+ *@return -1 on error else 0
33
+ *@todo checks
34
+ */
35
+int _ttail_search_closest_std_init(ttail_t* t);
36
+
37
+/**@brief @ref ttail_search_closest() implementation for stdin
38
+ *@note when returns ttail->session->std.buff contains the first
39
+ *line to print
40
+ *@param ttail_t*
41
+ *@param struct tm* tmin
42
+ *@return 0 if ok -1 if fatal error 1 if not found
43
+ *@todo checks
44
+ */
45
+int _ttail_search_closest_stdin(ttail_t*);
46
+/**@brief Output result loglines to stdout
47
+ *@param ttail_t*
48
+ *@param int fd
49
+ *@todo checks
50
+ */
51
+void _ttail_search_print_stdin(ttail_t*, int);
52
+
53
+/**@brief free private session members
54
+ *@patam ttail_t* t
55
+ */
56
+void _ttail_search_stdin_free(ttail_t* t);
57
+
58
+#endif
59
+

+ 1
- 10
src/ttail_search.c View File

@@ -32,19 +32,10 @@ void ttail_search_print_res(ttail_t* t)
32 32
 	else
33 33
 	{
34 34
 		_ttail_search_print_stdin(t, 1);
35
+		_ttail_search_stdin_free(t);
35 36
 	}
36 37
 }
37 38
 
38
-int _ttail_search_closest_stdin(ttail_t* t)
39
-{
40
-	return 0;
41
-}
42
-
43
-void _ttail_search_print_stdin(ttail_t* t, int fd)
44
-{
45
-	return;
46
-}
47
-
48 39
 int ttail_logline2date(ttail_t* ttail, const char* logline, struct tm* tm)
49 40
 {
50 41
 	const char *subst, *ret;

+ 129
- 0
src/ttail_search_std.c View File

@@ -0,0 +1,129 @@
1
+#include "ttail_search_std.h"
2
+
3
+int _ttail_search_closest_std_init(ttail_t* t)
4
+{
5
+	t->session = (ttail_search_t*)malloc(sizeof(ttail_search_stdin_t));
6
+	if(!t->session)
7
+	{
8
+		perror("Unable to allocate memory for search session");
9
+		return -1;
10
+	}
11
+	t->session->std.buff = NULL;
12
+	t->session->std.buff_sz = 0;
13
+	return 0;
14
+}
15
+
16
+int _ttail_search_closest_stdin(ttail_t* t)
17
+{
18
+	struct tm tm;
19
+	ssize_t rd_sz;
20
+	int ret, tmp;
21
+	if(_ttail_search_closest_std_init(t) < 0)
22
+	{
23
+		return -1;
24
+	}
25
+	if(!(t->flag & TTAIL_FLAG_DATE_MIN))
26
+	{
27
+		return 0;
28
+	}
29
+	while((rd_sz = ttail_std_getline(t)) > 0)
30
+	{
31
+		ttail_tm_init(&tm);
32
+		ret = ttail_logline2date(t, ttail_std_getline_buff(t), &tm);
33
+		if(ret < 0)
34
+		{
35
+			return -1;
36
+		}
37
+		else if(ret > 0)
38
+		{
39
+			continue;
40
+		}
41
+		ret = ttail_tm_cmp(&tm, &(t->date_min));
42
+		if(ret >= 0)
43
+		{
44
+			//buffer contains the first line to print
45
+			return 0;
46
+		}
47
+	}
48
+	if(rd_sz < 0)
49
+	{
50
+		tmp = errno;
51
+		ret = feof(stdin);
52
+		if(ret <= 0)
53
+		{
54
+			errno = tmp;
55
+			perror("Error while reading loglines from stdin");
56
+			return -1;
57
+		}
58
+	}
59
+	return 0;
60
+}
61
+
62
+void _ttail_search_print_stdin(ttail_t* t, int fd)
63
+{
64
+	struct tm tm;
65
+	ssize_t rd_sz;
66
+	int ret, tmp;
67
+	
68
+	rd_sz = strlen(ttail_std_getline_buff(t));
69
+	if(!(t->flag & TTAIL_FLAG_DATE_MAX))
70
+	{
71
+		do
72
+		{
73
+			ret = write(1, ttail_std_getline_buff(t),
74
+				sizeof(char)*rd_sz);
75
+			if(ret < 0)
76
+			{
77
+				perror("Unable to write to stdout");
78
+				return;
79
+			}
80
+		}
81
+		while((rd_sz = ttail_std_getline(t)) > 0);
82
+	}
83
+	else
84
+	{
85
+		do
86
+		{
87
+			ttail_tm_init(&tm);
88
+			ret = ttail_logline2date(t, ttail_std_getline_buff(t),
89
+				&tm);
90
+			//dropping errors
91
+			if(ret == 0)
92
+			{
93
+				ret = ttail_tm_cmp(&tm, &(t->date_max));
94
+				if(ret > 0)
95
+				{
96
+					return;
97
+				}
98
+			}
99
+			ret = write(1, ttail_std_getline_buff(t),
100
+				sizeof(char)*rd_sz);
101
+			if(ret < 0)
102
+			{
103
+				perror("Unable to write to stdout");
104
+				return;
105
+			}
106
+		}
107
+		while((rd_sz = ttail_std_getline(t)) > 0);
108
+	}
109
+	if(rd_sz < 0)
110
+	{
111
+		tmp = errno;
112
+		ret = feof(stdin);
113
+		if(ret <= 0)
114
+		{
115
+			errno = tmp;
116
+			perror("Error while reading loglines from stdin");
117
+		}
118
+	}
119
+	return;
120
+}
121
+
122
+void _ttail_search_stdin_free(ttail_t* t)
123
+{
124
+	if(t->session->std.buff)
125
+	{
126
+		free(t->session->std.buff);
127
+	}
128
+}
129
+

Loading…
Cancel
Save