Browse Source

Enhancement & changes in arguments parsing

add a -h --help option, deleted -l --logfile options
+ spliting the ttail_search_closest() & ttail_search_print_res() functions
Yann Weber 7 years ago
parent
commit
782f70e7fc
5 changed files with 44 additions and 42 deletions
  1. 7
    7
      src/include/ttail_init.h
  2. 5
    0
      src/include/ttail_search.h
  3. 10
    0
      src/main.c
  4. 13
    20
      src/ttail_init.c
  5. 9
    15
      src/ttail_search.c

+ 7
- 7
src/include/ttail_init.h View File

@@ -14,6 +14,7 @@
14 14
 
15 15
 #include "ttail.h"
16 16
 
17
+/**@todo set logfiles as "leadings" option */
17 18
 #define TTAIL_LONG_OPT {\
18 19
 	{"verbose", no_argument, 0, 'v'},\
19 20
 	{"re-prefix", required_argument, 0, 'r'},\
@@ -23,9 +24,10 @@
23 24
 	{"date-format", required_argument, 0, 'f'},\
24 25
 	{"date-min", required_argument, 0, 'd'},\
25 26
 	{"date-max", required_argument, 0, 'm'},\
26
-	{"logfile", required_argument, 0, 'l'},\
27
+	{"help", no_argument, 0, 'h'},\
27 28
 	{0,	0,	0,	0 }\
28 29
 }
30
+#define TTAIL_SHORT_OPT "vr:Eip:f:d:l:m:h"
29 31
 
30 32
 #define TTAIL_OPT_HELP {\
31 33
 	{"Augment the verbosity level",NULL},\
@@ -37,12 +39,10 @@ POSIX) ",NULL},\
37 39
 	{"Set the date format (see man strptime for supported date formats"\
38 40
 ,"FORMAT"},\
39 41
 	{"Start to output loglines starting from this date","DATE"},\
40
-	{"Not implemented","DATE"},\
41
-	{"Indicate logfiles","FILE"},\
42
-	{"",NULL},\
42
+	{"Stop to output loglines before this date","DATE"},\
43
+	{"Print this help and exit",NULL},\
43 44
 	{"",NULL}\
44 45
 }
45
-#define TTAIL_SHORT_OPT "r:EIp:f:d:l:m:"
46 46
 
47 47
 /**<! Print help & usage */
48 48
 void usage();
@@ -52,9 +52,9 @@ void usage();
52 52
  *@param char** argv
53 53
  *@return NULL on error
54 54
  */
55
-ttail_t *ttail_init(int argc, char **argv);
55
+ttail_t *ttail_init(int, char**);
56 56
 
57
-/**@brief Checks that init lead to a valid runtime
57
+/**@brief Checks that init returns a valid ttail_t runtime
58 58
  *@param ttail_t* t
59 59
  *@return 0 if no error -1 if fatal error
60 60
  */

+ 5
- 0
src/include/ttail_search.h View File

@@ -49,6 +49,11 @@ union _ttail_search_u
49 49
  */
50 50
 int ttail_search_closest(ttail_t*, const struct tm*);
51 51
 
52
+/**@brief Print search result on t->out_fd
53
+ *@param ttail_t*
54
+ */
55
+void ttail_search_print_res(ttail_t*);
56
+
52 57
 /**@brief @ref ttail_search_closest() implementation for stdin
53 58
  *@param ttail_t*
54 59
  *@param struct tm* tmin

+ 10
- 0
src/main.c View File

@@ -22,6 +22,16 @@ int main(int argc, char **argv)
22 22
 		return 1;
23 23
 	}
24 24
 	res = ttail_search_closest(ttail, &(ttail->date_min));
25
+	if(res < 0)
26
+	{
27
+		fprintf(stderr, "Error while searching through files\n");
28
+		return 1;
29
+	}
30
+	else if (res == 1)
31
+	{
32
+		return 0; /* not found, no line to print */
33
+	}
34
+	ttail_search_print_res(ttail);
25 35
 	ttail_free(ttail);
26 36
 	return res;
27 37
 }

+ 13
- 20
src/ttail_init.c View File

@@ -5,7 +5,7 @@ void usage()
5 5
 	static struct option opts[] = TTAIL_LONG_OPT;
6 6
 	static char *help[][2] = TTAIL_OPT_HELP;
7 7
 	size_t i;
8
-	printf("Usage : ttail -d DATE [OPTIONS]\n");
8
+	printf("Usage : ttail -d DATE [OPTIONS] [LOGFILES]\n");
9 9
 	printf("\t\n");
10 10
 	printf("Options list\n");
11 11
 	i=0;
@@ -29,12 +29,15 @@ void usage()
29 29
 		printf("\t\t%s\n\n", help[i][0]);
30 30
 		i++;
31 31
 	}
32
+	printf("\tLOGFILES\n\t\tLogfiles, can take '-' as value to tell ttail \
33
+to read from stdin instead of files\n");
32 34
 }
33 35
 
34 36
 ttail_t *ttail_init(int argc, char **argv)
35 37
 {
36 38
 	ttail_t *res;
37 39
 	int c, opt_i, ret;
40
+	size_t i;
38 41
 	char *dates[2] = { NULL, NULL };
39 42
 	char *date;
40 43
 
@@ -118,13 +121,9 @@ given\n");
118 121
 				}
119 122
 
120 123
 				break;
121
-			case 'l':
122
-				ret = ttail_add_logfile(res, optarg);
123
-				if(ret < 0)
124
-				{
125
-					goto ttail_init_err;
126
-				}
127
-				break;
124
+			case 'h':
125
+				usage();
126
+				goto ttail_init_err;
128 127
 			case 'd':
129 128
 			case 'm':
130 129
 				date = malloc(sizeof(char)*(strlen(optarg)+1));
@@ -146,16 +145,13 @@ given\n");
146 145
 		}
147 146
 	}
148 147
 	
149
-	if(optind < argc)
148
+	for(i=optind; i<argc; i++)
150 149
 	{
151
-		init_badarg:
152
-		fprintf(stderr, "bad option :");
153
-		while(optind < argc)
150
+		ret = ttail_add_logfile(res, argv[i]);
151
+		if(ret < 0)
154 152
 		{
155
-			fprintf(stderr, argv[optind++]);
153
+			goto ttail_init_err;
156 154
 		}
157
-		fprintf(stderr, "\n");
158
-		goto ttail_init_err;
159 155
 	}
160 156
 
161 157
 	if(ttail_set_dates(res, dates) < 0)
@@ -164,7 +160,8 @@ given\n");
164 160
 	}
165 161
 
166 162
 	return res;
167
-
163
+	init_badarg:
164
+	fprintf(stderr, "Unknown option '%s'\n", argv[optind]);
168 165
 	ttail_init_err:
169 166
 	if(dates[0]) { free(dates[0]); }
170 167
 	if(dates[1]) { free(dates[1]); }
@@ -194,10 +191,6 @@ given as argument\n");
194 191
 			return -1;
195 192
 		}
196 193
 	}
197
-	if(t->flag & TTAIL_FLAG_DATE_MAX)
198
-	{
199
-		fprintf(stderr, "Warning : date-max not yet implemented\n");
200
-	}
201 194
 	if(!(t->flag & TTAIL_FLAG_DATE_MIN))
202 195
 	{
203 196
 		fprintf(stderr, "--date-min -d is mandatory\n");

+ 9
- 15
src/ttail_search.c View File

@@ -8,37 +8,31 @@ int ttail_search_closest(ttail_t* ttail, const struct tm *tm)
8 8
 		fprintf(stderr, "A session is allready started\n");
9 9
 		return -1;
10 10
 	}
11
-	/*
12
-	ttail->session = (ttail_search_t*)malloc(ttail->logfile_sz?
13
-		sizeof(ttail_search_file_t):sizeof(ttail_search_stdin_t));
14
-	if(!ttail->session)
15
-	{
16
-		perror("Unable to allocate memory for search session");
17
-		return -1;
18
-	}
19
-	*/
20 11
 	ret = ttail->logfile_sz?\
21 12
 		_ttail_search_closest_files(ttail, tm):\
22 13
 		_ttail_search_closest_stdin(ttail, tm);
23 14
 	if(ret < 0)
24 15
 	{
25
-		fprintf(stderr, "Error will searching\n");
26 16
 		return -1;
27 17
 	}
28 18
 	if(ret > 0)
29 19
 	{
30 20
 		return ret;
31 21
 	}
32
-	if(ttail->logfile_sz)
22
+	return 0;
23
+}
24
+
25
+void ttail_search_print_res(ttail_t* t)
26
+{
27
+	if(t->logfile_sz)
33 28
 	{
34
-		_ttail_search_print_files(ttail, 1);
35
-		_ttail_search_file_free(ttail);
29
+		_ttail_search_print_files(t, 1);
30
+		_ttail_search_file_free(t);
36 31
 	}
37 32
 	else
38 33
 	{
39
-		_ttail_search_print_stdin(ttail, 1);
34
+		_ttail_search_print_stdin(t, 1);
40 35
 	}
41
-	return 0;
42 36
 }
43 37
 
44 38
 int _ttail_search_closest_stdin(ttail_t* t, const struct tm* tm)

Loading…
Cancel
Save