123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- /*
- * Copyright 2017 Yann Weber
- *
- * This file is part of Ttail.
- *
- * Ttail 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
- * any later version.
- *
- * Ttail 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 Ttail. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "ttail_search_std.h"
-
- int TTAIL_STDIN_FD = 0;
- FILE *TTAIL_STDIN = NULL;
-
- int ttail_search_std_init(ttail_t* t)
- {
- if(!TTAIL_STDIN_FD)
- {
- TTAIL_STDIN = stdin;
- }
- else
- {
- TTAIL_STDIN = fdopen(TTAIL_STDIN_FD, "r");
- if(!TTAIL_STDIN)
- {
- perror("Fail to open stdin");
- return -1;
- }
- }
-
- t->session = (ttail_search_t*)malloc(sizeof(ttail_search_stdin_t));
- if(!t->session)
- {
- perror("Unable to allocate memory for search session");
- return -1;
- }
- t->session->std.buff = NULL;
- t->session->std.buff_sz = 0;
- if(_ttail_search_std_fmt_init(t) < 0)
- {
- return -1;
- }
- return 0;
- }
-
- int _ttail_search_std_fmt_init(ttail_t* t)
- {
- ssize_t rd_sz;
- const char *buff;
- int fmt_id, max_lines, i;
- const char *fmt[] = TTAIL_DEFAULT_FORMATS;
-
- if(t->flag & TTAIL_FLAG_FORMAT)
- {
- return 1;
- }
-
- max_lines = 10;
-
- for(i=0; i<max_lines; i++)
- {
- if((rd_sz = ttail_std_getline(t)) < 0)
- {
- fprintf(stderr, "Unable to detect date from stdin\n");
- return -1;
- }
-
- buff = ttail_logline_subst(t, ttail_std_getline_buff(t));
- if(!buff)
- {
- if(ttail_permissive(t))
- {
- continue;
- }
-
- fprintf(stderr, "Unable to find prefix in logline");
- if(t->verbose > 0)
- {
- fprintf(stderr, " : '%s'",
- ttail_std_getline_buff(t));
- }
- fprintf(stderr, "\n");
- return -1;
- }
- fmt_id = ttail_format_guess(buff, NULL);
- if(fmt_id >= 0)
- {
- break;
- }
- if(!ttail_permissive(t))
- {
- ttail_strict_msg();
- break;
- }
- }
- if(fmt_id < 0)
- {
- fprintf(stderr, "Unable to detect date format from stdin\
- after %d lines were readed\n", max_lines);
- return -1;
- }
- buff = fmt[fmt_id];
- t->fmt = malloc(sizeof(char) * (strlen(buff)+1));
- if(!t->fmt)
- {
- perror("Unable to allocate memory for date format");
- return -1;
- }
- strcpy(t->fmt, buff);
- t->flag |= TTAIL_FLAG_FORMAT;
- return 0;
- }
-
- int _ttail_search_closest_stdin(ttail_t* t)
- {
- struct tm tm;
- ssize_t rd_sz;
- int ret, tmp;
- if(!(t->flag & TTAIL_FLAG_DATE_MIN))
- {
- return 0;
- }
- if(! t->session->std.buff)
- {
- if((rd_sz = ttail_std_getline(t)) < 0)
- {
- perror("Unable to read stdin");
- return -1;
- }
- }
- else
- {
- /*first line allready in the buffer thank's to format
- *detection*/
- rd_sz = strlen(ttail_std_getline_buff(t)) + 1;
- }
- while(rd_sz > 0)
- {
- ttail_tm_init(&tm);
- ret = ttail_logline2date(t, ttail_std_getline_buff(t), &tm);
- if(ret < 0)
- {
- return -1;
- }
- else if(ret > 0)
- {
- if(!ttail_permissive(t))
- {
- fprintf(stderr,
- "Unable to find the %s in logline",
- ret==1?"prefix":"date");
- if(t->verbose > 0)
- {
- fprintf(stderr, " : '%s'\n",
- ttail_file_getline_buf(t));
- }
- else
- {
- fprintf(stderr, "\n");
- }
- ttail_strict_msg();
- return -1;
- }
- rd_sz = ttail_std_getline(t);
- continue;
- }
- ret = ttail_tm_cmp(&tm, &(t->date_min));
- if(ret >= 0)
- {
- //buffer contains the first line to print
- return 0;
- }
- rd_sz = ttail_std_getline(t);
- }
- if(rd_sz < 0)
- {
- tmp = errno;
- ret = feof(TTAIL_STDIN);
- if(ret <= 0)
- {
- errno = tmp;
- perror("Error while reading loglines from stdin");
- return -1;
- }
- }
- return 1;
- }
-
- void _ttail_search_print_stdin(ttail_t* t, int fd)
- {
- struct tm tm;
- ssize_t rd_sz;
- int ret, tmp;
-
- rd_sz = strlen(ttail_std_getline_buff(t));
- if(!(t->flag & TTAIL_FLAG_DATE_MAX))
- {
- do
- {
- ret = write(1, ttail_std_getline_buff(t),
- sizeof(char)*rd_sz);
- if(ret < 0)
- {
- perror("Unable to write to stdout");
- return;
- }
- }
- while((rd_sz = ttail_std_getline(t)) > 0);
- }
- else
- {
- do
- {
- ttail_tm_init(&tm);
- ret = ttail_logline2date(t, ttail_std_getline_buff(t),
- &tm);
- //dropping errors
- if(ret == 0)
- {
- ret = ttail_tm_cmp(&tm, &(t->date_max));
- if(ret > 0)
- {
- return;
- }
- }
- ret = write(1, ttail_std_getline_buff(t),
- sizeof(char)*rd_sz);
- if(ret < 0)
- {
- perror("Unable to write to stdout");
- return;
- }
- }
- while((rd_sz = ttail_std_getline(t)) > 0);
- }
- if(rd_sz < 0)
- {
- tmp = errno;
- ret = feof(TTAIL_STDIN);
- if(ret <= 0)
- {
- errno = tmp;
- perror("Error while reading loglines from stdin");
- }
- }
- return;
- }
-
- void _ttail_search_stdin_free(ttail_t* t)
- {
- if(TTAIL_STDIN)
- {
- fclose(TTAIL_STDIN);
- }
- if(t->session->std.buff)
- {
- free(t->session->std.buff);
- }
- }
-
- void _ttail_set_stdin(int std)
- {
- TTAIL_STDIN_FD = std;
- TTAIL_STDIN = NULL;
- }
|