1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
/* a small tool to squash multiline message - probably to be removed
* later and integrated into the "mainstream" tools.
* Copyright (C) 2015 by Rainer Gerhards
* Released under ASL 2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
char *
getmsg(regex_t *const preg, char *const buf, size_t len)
{
static size_t lenln = 0;
static char lnbuf[1024*64];
size_t iDst = 0;
int nlines = 0;
if(lenln) { /* have previous segment? */
memcpy(buf+iDst, lnbuf, lenln);
iDst += lenln;
++nlines;
}
while(fgets(lnbuf, sizeof(lnbuf), stdin)) {
lenln = strlen(lnbuf);
if(lnbuf[lenln-1] == '\n') {
lnbuf[lenln-1] = '\0';
lenln--;
}
const int is_match =
!regexec(preg, lnbuf, 0, NULL, 0);
if(is_match) {
break; /* previous message complete */
} else {
if(iDst != 0) {
buf[iDst++] = '\\';
buf[iDst++] = 'n';
}
memcpy(buf+iDst, lnbuf, lenln);
iDst += lenln;
++nlines;
}
}
if(nlines == 0 && lenln > 0) { /* handle single lines */
memcpy(buf+iDst, lnbuf, lenln);
iDst += lenln;
lenln = 0;
}
buf[iDst] = '\0';
}
int
main(int argc, char *argv[])
{
if(argc != 2) {
fprintf(stderr, "usage: squashml regex\n");
exit(1);
}
regex_t preg;
if(regcomp(&preg, argv[1], REG_EXTENDED)) {
perror("regcomp");
exit(1);
}
char msg[1024*256];
while(!feof(stdin)) {
getmsg(&preg, msg, sizeof(msg));
printf("%s\n", msg);
}
}
|