129
147
au->source_list = tmp;
131
149
case AUSOURCE_BUFFER:
132
tmp = malloc(2*sizeof(char *));
135
au->source_list = tmp;
152
if (databuf_init(&au->databuf, len, DATABUF_FLAG_PRESERVE_HEAD) < 0) goto bad_exit;
153
if (databuf_append(&au->databuf, buf, len) < 0) goto bad_exit;
137
155
case AUSOURCE_BUFFER_ARRAY:
141
tmp = malloc((n+1)*sizeof(char *));
143
tmp[i] = strdup(bb[i]);
145
au->source_list = tmp;
157
for (n = 0; (buf = bb[n]); n++) {
159
if (bb[n][len-1] != '\n') {
165
if (databuf_init(&au->databuf, size, DATABUF_FLAG_PRESERVE_HEAD) < 0) goto bad_exit;
166
for (n = 0; (buf = bb[n]); n++) {
168
if (databuf_append(&au->databuf, buf, len) < 0) goto bad_exit;
147
171
case AUSOURCE_DESCRIPTOR:
202
databuf_free(&au->databuf);
208
void auparse_add_callback(auparse_state_t *au, auparse_callback_ptr callback,
209
void *user_data, user_destroy user_destroy_func)
211
if (au->callback_user_data_destroy) {
212
(*au->callback_user_data_destroy)(au->callback_user_data);
213
au->callback_user_data = NULL;
216
au->callback = callback;
217
au->callback_user_data = user_data;
218
au->callback_user_data_destroy = user_destroy_func;
221
static void consume_feed(auparse_state_t *au, int flush)
223
while (auparse_next_event(au) > 0) {
224
if (au->callback) (*au->callback)(au, AUPARSE_CB_EVENT_READY, au->callback_user_data);
227
// FIXME: We might have to add a call here to force auparse_next_event()
228
// to consume any partial data not fully consumed.
229
if (au->parse_state == EVENT_ACCUMULATING) {
230
// Emit the event, set event cursors to initial position
231
aup_list_first(&au->le);
232
aup_list_first_field(&au->le);
233
au->parse_state = EVENT_EMITTED;
234
if (au->callback) (*au->callback)(au, AUPARSE_CB_EVENT_READY, au->callback_user_data);
239
int auparse_feed(auparse_state_t *au, const char *data, size_t data_len)
241
if (databuf_append(&au->databuf, data, data_len) < 0) return -1;
242
//if (debug) databuf_print(&au->databuf, 1, "auparse_feed() added %d bytes", data_len);
247
int auparse_flush_feed(auparse_state_t *au)
178
253
int auparse_reset(auparse_state_t *au)
180
255
if (au == NULL) {
351
435
* successfully read a blank line (e.g. containing only a single newline).
352
436
* cur_buf will have been newly allocated with malloc.
354
* Returns < 0 on error, 0 if input is exhausted, and 1 if success.
356
438
* Note: cur_buf will be freed the next time this routine is called if
357
439
* cur_buf is not NULL, callers who retain a reference to the cur_buf
358
440
* pointer will need to set cur_buf to NULL to cause the previous cur_buf
359
* allocation to persist.*/
441
* allocation to persist.
444
* 1 if successful (errno == 0)
445
* 0 if non-blocking input unavailable (errno == 0)
446
* -1 if error (errno contains non-zero error code)
447
* -2 if EOF (errno == 0)
361
450
static int readline_file(auparse_state_t *au)
375
464
if ((rc = getline(&au->cur_buf, &n, au->in)) <= 0) {
376
if (rc < 0 && !feof(au->in)) return -1;
465
// Note: getline always malloc's if lineptr==NULL or n==0,
466
// on failure malloc'ed memory is left uninitialized, caller must free it.
470
// Note: feof() does not set errno
472
// return EOF condition
476
// return error condition, error code in errno
379
479
p_last_char = au->cur_buf + (rc-1);
380
480
if (*p_last_char == '\n') { /* nuke newline */
381
481
*p_last_char = 0;
390
492
* successfully read a blank line (e.g. containing only a single
393
* Returns < 0 on error, 0 if input is exhausted, and 1 if success.
395
495
* Note: cur_buf will be freed the next time this routine is called if
396
496
* cur_buf is not NULL, callers who retain a reference to the cur_buf
397
497
* pointer will need to set cur_buf to NULL to cause the previous cur_buf
398
* allocation to persist.*/
498
* allocation to persist.
501
* 1 if successful (errno == 0)
502
* 0 if non-blocking input unavailable (errno == 0)
503
* -1 if error (errno contains non-zero error code)
504
* -2 if EOF (errno == 0)
400
508
static int readline_buf(auparse_state_t *au)
402
char *ptr,*new_ptr=NULL;
510
char *p_newline=NULL;
404
513
if (au->cur_buf != NULL) {
405
514
free(au->cur_buf);
406
515
au->cur_buf = NULL;
409
ptr = au->next_buf + au->off;
518
//if (debug) databuf_print(&au->databuf, 1, "readline_buf");
519
if (au->databuf.len == 0) {
520
// return EOF condition
525
if ((p_newline = strnchr(databuf_beg(&au->databuf), '\n', au->databuf.len)) != NULL) {
526
line_len = p_newline - databuf_beg(&au->databuf);
529
au->cur_buf = malloc(line_len+1); // +1 for null terminator
530
if (au->cur_buf == NULL) return -1; // return error condition, errno set
531
strncpy(au->cur_buf, databuf_beg(&au->databuf), line_len);
532
au->cur_buf[line_len] = 0;
534
if (databuf_advance(&au->databuf, line_len+1) < 0) return -1;
540
// return no data available
413
new_ptr = strchr(ptr, '\n');
415
// found a line dup it
418
au->cur_buf = strdup(ptr);
420
au->off += strlen(au->cur_buf)+1;
423
// there is text but no new line
424
au->cur_buf = strdup(ptr);
425
au->off += strlen(au->cur_buf);
432
546
static int str2event(char *s, au_event_t *e)
512
626
* cur_buf data it should copy the cur_buf pointer and set cur_buf to
515
* Returns < 0 on error, 0 if input is exhausted, and 1 if success. */
630
* 1 if successful (errno == 0)
631
* 0 if non-blocking input unavailable (errno == 0)
632
* -1 if error (errno contains non-zero error code)
633
* -2 if EOF (errno == 0)
517
636
static int retrieve_next_line(auparse_state_t *au)
640
// If line was pushed back for re-reading return that
641
if (au->line_pushed) {
642
// Starting new event, clear previous event data,
643
// previous line is returned again for new parsing
521
649
switch (au->source)
523
651
case AUSOURCE_DESCRIPTOR:
524
652
case AUSOURCE_FILE_POINTER:
653
rc = readline_file(au);
654
if (rc > 0) au->line_number++;
526
656
case AUSOURCE_LOGS:
527
657
case AUSOURCE_FILE:
528
658
case AUSOURCE_FILE_ARRAY:
529
659
// if the first time through, open file
530
660
if (au->list_idx == 0 && au->in == NULL) {
531
662
au->in = fopen(au->source_list[au->list_idx], "r");
532
663
if (au->in == NULL) return -1;
535
666
// loop reading lines from a file
537
// Get next buffer from file
538
if ((rc = readline_file(au)) > 0) return 1;
539
if (rc < 0) return -1;
540
// end of file, open next file, try readline again
544
if (au->source_list[au->list_idx]) {
545
au->in = fopen(au->source_list[au->list_idx], "r");
546
if (au->in == NULL) return -1;
668
if ((rc = readline_file(au)) == -2) {
669
// end of file, open next file, try readline again
674
if (au->source_list[au->list_idx]) {
675
au->in = fopen(au->source_list[au->list_idx], "r");
676
if (au->in == NULL) return -1;
679
if (rc > 0) au->line_number++;
683
return -2; // return EOF
550
684
case AUSOURCE_BUFFER:
551
685
case AUSOURCE_BUFFER_ARRAY:
552
if (au->list_idx == 0 && au->next_buf == NULL)
553
au->next_buf = au->source_list[au->list_idx];
555
while (au->next_buf) {
556
if (readline_buf(au) > 0) return 1;
557
// end of buffer, advance to next buffer, try readline again
560
au->next_buf = au->source_list[au->list_idx];
686
rc = readline_buf(au);
687
if (rc > 0) au->line_number++;
690
rc = readline_buf(au);
691
// No such thing as EOF for feed, translate EOF to data not available
695
if (rc > 0) au->line_number++;
566
700
return -1; /* should never reach here */
703
static void push_line(auparse_state_t *au)
570
710
* Functions that traverse events.
670
810
int auparse_next_event(auparse_state_t *au)
677
// Tear down old event info
678
aup_list_clear(&au->le);
682
au->cur_buf = au->saved_buf;
683
au->saved_buf = NULL;
815
if (au->parse_state == EVENT_EMITTED) {
816
// If the last call resulted in emitting event data then
817
// clear previous event data in preparation to accumulate new event data
818
aup_list_clear(&au->le);
819
au->parse_state = EVENT_EMPTY;
822
// accumulate new event data
685
824
rc = retrieve_next_line(au);
687
return rc; // Error or empty
693
extract_timestamp(au->cur_buf, &e);
695
aup_list_set_event(&au->le, &e);
697
if (events_are_equal(&au->le.e, &e)) {
698
// If they are equal, add this to the event
699
aup_list_append(&au->le, &e, au->cur_buf);
701
// The list took custody of the line so empty pointer
704
rc = retrieve_next_line(au);
706
return rc; // Error stop
708
goto reset_current; // Consumed all data
710
// Events are not equal...stop...emit event
711
au->saved_buf = au->cur_buf;
718
// Set cursor to first record of new event
719
aup_list_first(&au->le);
720
// Set field cursor to 1st field of cur record
721
aup_list_first_field(&au->le);
722
if (aup_list_get_cnt(&au->le))
825
if (debug) printf("next_line(%d) '%s'\n", rc, au->cur_buf);
826
if (rc == 0) return 0; // No data now
828
// We're at EOF, did we read any data previously?
829
// If so return data available, else return no data available
830
if (au->parse_state == EVENT_ACCUMULATING) {
831
if (debug) printf("EOF, EVENT_EMITTED\n");
832
au->parse_state = EVENT_EMITTED;
833
return 1; // data is available
837
if (rc > 0) { // Input available
838
extract_timestamp(au->cur_buf, &event);
839
if (au->parse_state == EVENT_EMPTY) {
840
// First record in new event, initialize event
841
if (debug) printf("First record in new event, initialize event\n");
842
aup_list_set_event(&au->le, &event);
843
aup_list_append(&au->le, au->cur_buf, au->list_idx, au->line_number);
844
au->parse_state = EVENT_ACCUMULATING;
846
} else if (events_are_equal(&au->le.e, &event)) {
847
// Accumulate data into existing event
848
if (debug) printf("Accumulate data into existing event\n");
849
aup_list_append(&au->le, au->cur_buf, au->list_idx, au->line_number);
850
au->parse_state = EVENT_ACCUMULATING;
853
// New event, save current input for next invocation
854
if (debug) printf("New event, save current input for next invocation, EVENT_EMITTED\n");
856
// Emit the event, set event cursors to initial position
857
aup_list_first(&au->le);
858
aup_list_first_field(&au->le);
859
au->parse_state = EVENT_EMITTED;
860
return 1; // data is available
862
} else { // Read error