Переглянути джерело

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 роки тому
джерело
коміт
782f70e7fc
5 змінених файлів з 44 додано та 42 видалено
  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 Переглянути файл

14
 
14
 
15
 #include "ttail.h"
15
 #include "ttail.h"
16
 
16
 
17
+/**@todo set logfiles as "leadings" option */
17
 #define TTAIL_LONG_OPT {\
18
 #define TTAIL_LONG_OPT {\
18
 	{"verbose", no_argument, 0, 'v'},\
19
 	{"verbose", no_argument, 0, 'v'},\
19
 	{"re-prefix", required_argument, 0, 'r'},\
20
 	{"re-prefix", required_argument, 0, 'r'},\
23
 	{"date-format", required_argument, 0, 'f'},\
24
 	{"date-format", required_argument, 0, 'f'},\
24
 	{"date-min", required_argument, 0, 'd'},\
25
 	{"date-min", required_argument, 0, 'd'},\
25
 	{"date-max", required_argument, 0, 'm'},\
26
 	{"date-max", required_argument, 0, 'm'},\
26
-	{"logfile", required_argument, 0, 'l'},\
27
+	{"help", no_argument, 0, 'h'},\
27
 	{0,	0,	0,	0 }\
28
 	{0,	0,	0,	0 }\
28
 }
29
 }
30
+#define TTAIL_SHORT_OPT "vr:Eip:f:d:l:m:h"
29
 
31
 
30
 #define TTAIL_OPT_HELP {\
32
 #define TTAIL_OPT_HELP {\
31
 	{"Augment the verbosity level",NULL},\
33
 	{"Augment the verbosity level",NULL},\
37
 	{"Set the date format (see man strptime for supported date formats"\
39
 	{"Set the date format (see man strptime for supported date formats"\
38
 ,"FORMAT"},\
40
 ,"FORMAT"},\
39
 	{"Start to output loglines starting from this date","DATE"},\
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
 	{"",NULL}\
44
 	{"",NULL}\
44
 }
45
 }
45
-#define TTAIL_SHORT_OPT "r:EIp:f:d:l:m:"
46
 
46
 
47
 /**<! Print help & usage */
47
 /**<! Print help & usage */
48
 void usage();
48
 void usage();
52
  *@param char** argv
52
  *@param char** argv
53
  *@return NULL on error
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
  *@param ttail_t* t
58
  *@param ttail_t* t
59
  *@return 0 if no error -1 if fatal error
59
  *@return 0 if no error -1 if fatal error
60
  */
60
  */

+ 5
- 0
src/include/ttail_search.h Переглянути файл

49
  */
49
  */
50
 int ttail_search_closest(ttail_t*, const struct tm*);
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
 /**@brief @ref ttail_search_closest() implementation for stdin
57
 /**@brief @ref ttail_search_closest() implementation for stdin
53
  *@param ttail_t*
58
  *@param ttail_t*
54
  *@param struct tm* tmin
59
  *@param struct tm* tmin

+ 10
- 0
src/main.c Переглянути файл

22
 		return 1;
22
 		return 1;
23
 	}
23
 	}
24
 	res = ttail_search_closest(ttail, &(ttail->date_min));
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
 	ttail_free(ttail);
35
 	ttail_free(ttail);
26
 	return res;
36
 	return res;
27
 }
37
 }

+ 13
- 20
src/ttail_init.c Переглянути файл

5
 	static struct option opts[] = TTAIL_LONG_OPT;
5
 	static struct option opts[] = TTAIL_LONG_OPT;
6
 	static char *help[][2] = TTAIL_OPT_HELP;
6
 	static char *help[][2] = TTAIL_OPT_HELP;
7
 	size_t i;
7
 	size_t i;
8
-	printf("Usage : ttail -d DATE [OPTIONS]\n");
8
+	printf("Usage : ttail -d DATE [OPTIONS] [LOGFILES]\n");
9
 	printf("\t\n");
9
 	printf("\t\n");
10
 	printf("Options list\n");
10
 	printf("Options list\n");
11
 	i=0;
11
 	i=0;
29
 		printf("\t\t%s\n\n", help[i][0]);
29
 		printf("\t\t%s\n\n", help[i][0]);
30
 		i++;
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
 ttail_t *ttail_init(int argc, char **argv)
36
 ttail_t *ttail_init(int argc, char **argv)
35
 {
37
 {
36
 	ttail_t *res;
38
 	ttail_t *res;
37
 	int c, opt_i, ret;
39
 	int c, opt_i, ret;
40
+	size_t i;
38
 	char *dates[2] = { NULL, NULL };
41
 	char *dates[2] = { NULL, NULL };
39
 	char *date;
42
 	char *date;
40
 
43
 
118
 				}
121
 				}
119
 
122
 
120
 				break;
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
 			case 'd':
127
 			case 'd':
129
 			case 'm':
128
 			case 'm':
130
 				date = malloc(sizeof(char)*(strlen(optarg)+1));
129
 				date = malloc(sizeof(char)*(strlen(optarg)+1));
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
 	if(ttail_set_dates(res, dates) < 0)
157
 	if(ttail_set_dates(res, dates) < 0)
164
 	}
160
 	}
165
 
161
 
166
 	return res;
162
 	return res;
167
-
163
+	init_badarg:
164
+	fprintf(stderr, "Unknown option '%s'\n", argv[optind]);
168
 	ttail_init_err:
165
 	ttail_init_err:
169
 	if(dates[0]) { free(dates[0]); }
166
 	if(dates[0]) { free(dates[0]); }
170
 	if(dates[1]) { free(dates[1]); }
167
 	if(dates[1]) { free(dates[1]); }
194
 			return -1;
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
 	if(!(t->flag & TTAIL_FLAG_DATE_MIN))
194
 	if(!(t->flag & TTAIL_FLAG_DATE_MIN))
202
 	{
195
 	{
203
 		fprintf(stderr, "--date-min -d is mandatory\n");
196
 		fprintf(stderr, "--date-min -d is mandatory\n");

+ 9
- 15
src/ttail_search.c Переглянути файл

8
 		fprintf(stderr, "A session is allready started\n");
8
 		fprintf(stderr, "A session is allready started\n");
9
 		return -1;
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
 	ret = ttail->logfile_sz?\
11
 	ret = ttail->logfile_sz?\
21
 		_ttail_search_closest_files(ttail, tm):\
12
 		_ttail_search_closest_files(ttail, tm):\
22
 		_ttail_search_closest_stdin(ttail, tm);
13
 		_ttail_search_closest_stdin(ttail, tm);
23
 	if(ret < 0)
14
 	if(ret < 0)
24
 	{
15
 	{
25
-		fprintf(stderr, "Error will searching\n");
26
 		return -1;
16
 		return -1;
27
 	}
17
 	}
28
 	if(ret > 0)
18
 	if(ret > 0)
29
 	{
19
 	{
30
 		return ret;
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
 	else
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
 int _ttail_search_closest_stdin(ttail_t* t, const struct tm* tm)
38
 int _ttail_search_closest_stdin(ttail_t* t, const struct tm* tm)

Loading…
Відмінити
Зберегти