123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- #include "ttail_init.h"
-
- ttail_t *ttail_init(int argc, char **argv)
- {
- ttail_t *res;
- int c, opt_i, ret;
- char *dates[2] = { NULL, NULL };
- char *date;
-
- res = malloc(sizeof(ttail_t));
- if(!res)
- {
- perror("Unable to allocate memory");
- goto ttail_init_alloc_err;
- }
-
- opterr = 0;
- optind = 0;
- res->verbose = 0;
- res->fmt = NULL;
- res->flag = 0;
- res->logfile = NULL;
- res->logfile_name = NULL;
- res->logfile_sz = 0;
- res->prefix_sz = -1;
- memset(&(res->date_min), 0, sizeof(struct tm));
- memset(&(res->date_max), 0, sizeof(struct tm));
-
- while(1)
- {
- static struct option long_options[] = TTAIL_LONG_OPT;
-
- c = getopt_long(argc, argv, TTAIL_SHORT_OPT, long_options, &opt_i);
- if(c == -1)
- break;
-
- switch(c)
- {
- case 'v':
- res->verbose++;
- break;
- case 'r':
- if(ttail_set_prefix(res, optarg))
- {
- goto ttail_init_err;
- }
- break;
- case 'p':
- if(res->flag & TTAIL_FLAG_PREFIX)
- {
- fprintf(stderr, "Preffix allready \
- set\n");
- goto ttail_init_err;
- }
- res->flag |= TTAIL_FLAG_PREFIX;
- res->prefix_sz = atoi(optarg);
- if(res->prefix_sz <= 0)
- {
- fprintf(stderr, "Prefix size have \
- to be > 0");
- goto ttail_init_err;
- }
- break;
- case 'E':
- case 'I':
- /**@todo checks */
- if(res->flag & TTAIL_FLAG_PREFIX)
- {
- fprintf(stderr, "-E & -I options \
- have to be set BEFORE -r option\n");
- goto ttail_init_err;
- }
- res->flag |= c=='E'?TTAIL_FLAG_EXTENDED_RE:\
- REG_ICASE;
- break;
- case 'f':
- if(res->flag & TTAIL_FLAG_FORMAT)
- {
- fprintf(stderr,"Multiple date format \
- given\n");
- goto ttail_init_err;
- }
- res->fmt = malloc(
- sizeof(char)*(strlen(optarg)+1));
- if(!res->fmt)
- {
- perror("Unable to allocate memory\
- for date format");
- goto ttail_init_err;
- }
- strcpy(res->fmt, optarg);
- res->flag |= TTAIL_FLAG_FORMAT;
-
- break;
- case 'l':
- fprintf(stderr,"-l with %s\n", optarg);
- ret = ttail_add_logfile(res, optarg);
- if(ret < 0)
- {
- goto ttail_init_err;
- }
- break;
- case 'd':
- case 'm':
- date = malloc(sizeof(char)*(strlen(optarg)+1));
- if(!date)
- {
- goto ttail_init_err;
- }
- strcpy(date, optarg);
- dates[c=='d'?0:1] = date;
- break;
- case '?':
- optind--;
- goto init_badarg;
- break;
- default:
- fprintf(stderr, "Bad argument\n");
- goto init_badarg;
-
- }
- }
-
- if(optind < argc)
- {
- init_badarg:
- fprintf(stderr, "bad option :");
- while(optind < argc)
- {
- fprintf(stderr, argv[optind++]);
- }
- fprintf(stderr, "\n");
- goto ttail_init_err;
- }
-
- if(ttail_set_dates(res, dates) < 0)
- {
- goto ttail_init_err;
- }
-
- return res;
-
- ttail_init_err:
- if(dates[0]) { free(dates[0]); }
- if(dates[1]) { free(dates[1]); }
- ttail_free(res);
- ttail_init_alloc_err:
- return NULL;
- }
-
- int ttail_init_check(ttail_t* t)
- {
- if(t->flag & TTAIL_FLAG_DATE_MAX)
- {
- fprintf(stderr, "Warning : date-max not yet implemented\n");
- }
- if(!(t->flag & TTAIL_FLAG_DATE_MIN))
- {
- fprintf(stderr, "--date-min -d is mandatory\n");
- return -1;
- }
- if(!(t->flag & TTAIL_FLAG_FORMAT))
- {
- fprintf(stderr, "No date format set nor detected. Abording.\n");
- return -1;
- }
- return 0;
- }
-
- int ttail_add_logfile(ttail_t* res, const char* filename)
- {
- void *tmp;
- FILE *fp;
- char *fname;
- int ret, i;
-
- for(i=0; i<res->logfile_sz; i++)
- {
- if(strcmp(filename, res->logfile_name[i]) == 0)
- {
- fprintf(stderr, "File '%s' allready added\n",
- filename);
- return -1;
- }
- }
-
- res->logfile_sz++;
- tmp = res->logfile;
- res->logfile = realloc(res->logfile,
- sizeof(FILE*)*res->logfile_sz);
- ret = 0;
- if(!res->logfile)
- {
- perror("Unable to allocate memory for logfiles");
- res->logfile = tmp;
- goto ttail_add_logfile_fpalloc_err;
- }
- fp = fopen(filename, "r");
- if(!fp)
- {
- fprintf(stderr, "Unable to open file : %s\n", filename);
- ret = 1;
- }
- res->logfile[res->logfile_sz-1] = fp;
-
- tmp = res->logfile_name;
- res->logfile_name = realloc(res->logfile_name,
- sizeof(char*)*res->logfile_sz);
- if(!res->logfile_name)
- {
- perror("Unable to allocate memory for logfiles");
- res->logfile_name = tmp;
- goto ttail_add_logfile_fnalloc_err;
-
- }
- fname = malloc(sizeof(char)*(strlen(filename)+1));
- if(!fname)
- {
- perror("Unable to allocate memory for logfiles");
- goto ttail_add_logfile_fnalloc_err;
- }
- strcpy(fname, filename);
- res->logfile_name[res->logfile_sz-1] = fname;
-
- return ret;
-
- ttail_add_logfile_fnalloc_err:
- fclose(res->logfile[res->logfile_sz-2]);
- ttail_add_logfile_fpalloc_err:
- res->logfile_sz--;
- return -1;
- }
-
- int ttail_set_prefix(ttail_t* res, const char* regex)
- {
- int ret, cflags;
- char *re_errbuff;
- size_t re_errbuff_sz;
-
- if(res->flag & TTAIL_FLAG_PREFIX)
- {
- fprintf(stderr, "Regex prefix allready set");
- return 1;
- }
- res->flag |= TTAIL_FLAG_PREFIX;
- cflags = 0; /** @todo checks */
- if(res->flag & TTAIL_FLAG_EXTENDED_RE)
- {
- cflags |= REG_EXTENDED;
- }
- if(res->flag & TTAIL_FLAG_EXTENDED_CI)
- {
- cflags |= REG_ICASE;
- }
- ret = regcomp(&res->date_prefix, regex, cflags);
- if(!ret)
- {
- return 0;
- }
- /*compilation error */
- res->flag ^= TTAIL_FLAG_PREFIX;
- re_errbuff_sz = regerror(ret,
- &res->date_prefix, NULL, 0);
- re_errbuff = malloc(
- sizeof(char)*re_errbuff_sz);
- if(!re_errbuff)
- {
- perror("Failed to allocate memory for regex compilation \
- error message");
- goto ttail_set_prefix_err;
- }
- regerror(ret, &res->date_prefix,
- re_errbuff,
- sizeof(char)*re_errbuff_sz);
- fprintf(stderr, "Regex compilation fails : %s", re_errbuff);
-
- free(re_errbuff);
- ttail_set_prefix_err:
- return -1;
- }
-
- int ttail_set_dates(ttail_t* res, char* dates[2])
- {
- int c, ret;
- char *date;
-
- for(c=0;c<2;c++)
- {
- memset(c==0?&(res->date_min):&(res->date_max), 0, \
- sizeof(struct tm));
- if(!dates[c])
- {
- continue;
- }
- if(!(res->flag & TTAIL_FLAG_FORMAT))
- {
- /* no format specified */
- ret = ttail_format_guess(res, dates[c],
- c==0?&(res->date_min):&(res->date_max));
- if(ret < 0)
- {
- fprintf(stderr, "Unable to guess format for \
- date '%s'\n", dates[c]);
- return -1;
- }
- res->flag |= c?TTAIL_FLAG_DATE_MAX:TTAIL_FLAG_DATE_MIN;
- continue;
- }
- date = strptime(dates[c], res->fmt,
- c==0?&(res->date_min):&(res->date_max));
- if(!date)
- {
- fprintf(stderr, "Unable to parse date-%s '%s' with \
- format '%s'\n", c==0?"min":"max", dates[c], res->fmt);
- return -1;
- }
- else if(*date != '\0')
- {
- fprintf(stderr, "Leading caracters for date-%s : %s\n",\
- c==0?"min":"max", date);
- }
- res->flag |= c?TTAIL_FLAG_DATE_MAX:TTAIL_FLAG_DATE_MIN;
- }
-
- if(dates[0]) { free(dates[0]); dates[0] = NULL; }
- if(dates[1]) { free(dates[1]); dates[1] = NULL; }
-
- return 0;
- }
-
- int ttail_format_guess(ttail_t* t, const char* date_str, struct tm* tm)
- {
- int i, res;
- char *res_ret, *ret;
- char *fmt[] = TTAIL_DEFAULT_FORMATS;
- struct tm dte;
-
- memset(&dte, 0, sizeof(struct tm));
-
- if(t->flag & TTAIL_FLAG_FORMAT)
- {
- fprintf(stderr, "Format allready set\n");
- return -2;
- }
- res = -1;
- res_ret = NULL;
- i=0;
- while(fmt[i])
- {
- ret = strptime(date_str, fmt[i], &dte);
- if(ret)
- {
- if(!res_ret || strlen(res_ret) > strlen(ret))
- {
- res_ret = ret;
- res = i;
- if(tm)
- {
- memcpy(tm, &dte, sizeof(struct tm));
- }
- if(t->fmt)
- {
- free(t->fmt);
- }
- t->fmt = malloc(
- sizeof(char)*(strlen(fmt[i])+1));
- if(!t->fmt)
- {
- perror("Unable to allocate memory\
- for date format");
- t->flag ^= TTAIL_FLAG_FORMAT;
- return -1;
- }
- strcpy(t->fmt, fmt[i]);
- t->flag |= TTAIL_FLAG_FORMAT;
- }
- }
- i++;
- }
- if(!res_ret)
- {
- if(tm)
- {
- memset(tm, 0, sizeof(struct tm));
- }
- return -1;
- }
-
- return res;
- }
-
- void ttail_free(ttail_t* t)
- {
- size_t i;
- if(t->flag & TTAIL_FLAG_PREFIX && t->prefix_sz < 0)
- {
- regfree(&(t->date_prefix));
- }
- for(i=0; i<t->logfile_sz; i++)
- {
- if(t->logfile[i])
- {
- fclose(t->logfile[i]);
- }
- if(t->logfile_name[i])
- {
- free(t->logfile_name[i]);
- }
- }
- free(t->logfile);
- free(t->logfile_name);
- t->logfile_sz = 0;
- if(t->fmt != NULL)
- {
- free(t->fmt);
- }
- free(t);
- optind=0;
- }
|