1 /* rsyslog testbench tool to mangle .qi files
2  *
3  * Copyright (C) 2016 by Rainer Gerhards
4  * Released uner ASL 2.0
5  */
6 #include "config.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <ctype.h>
11 #include <string.h>
12 
13 static int debug = 0;
14 
15 void
usage(void)16 usage(void)
17 {
18 	fprintf(stderr, "mangle_qi -d -q <.qi-file>\n"
19 		"-d enables debug messages\n");
20 	exit(1);
21 }
22 
23 void
processQI(FILE * const __restrict__ qi)24 processQI(FILE *const __restrict__ qi)
25 {
26 	char lnbuf[4096];
27 	char propname[64];
28 	int rectype;
29 	int length;
30 	int queuesize;
31 	int i;
32 	int c;
33 	fgets(lnbuf, sizeof(lnbuf), qi);
34 	fputs(lnbuf, stdout);
35 	/* we now read the queue size line */
36 	/* note: this is quick and dirty, no error checks
37 	 * are done!
38 	 */
39 	fgetc(qi); /* skip '+' */
40 	for(i = 0 ; (c = fgetc(qi)) != ':' ; ++i) {
41 		propname[i] = c;
42 	}
43 	propname[i] = '\0';
44 	if(strcmp(propname, "iQueueSize")) {
45 		fprintf(stderr, ".qi file format unknown: line 2 does "
46 			"not contain iQueueSize property, instead '%s'\n",
47 			propname);
48 		exit(1);
49 	}
50 
51 	rectype = 0;
52 	for(c = fgetc(qi) ; isdigit(c) ; c = fgetc(qi))
53 		rectype = rectype * 10 + c - '0';
54 
55 	length = 0;
56 	for(c = fgetc(qi) ; isdigit(c) ; c = fgetc(qi))
57 		length = length * 10 + c - '0';
58 
59 	queuesize = 0;
60 	for(c = fgetc(qi) ; isdigit(c) ; c = fgetc(qi))
61 		queuesize = queuesize * 10 + c - '0';
62 
63 	int maxval_for_length = 10;
64 	for(i = 1 ; i < length ; ++i)
65 		maxval_for_length *= 10;	/* simulate int-exp() */
66 
67 	if(debug) {
68 		fprintf(stderr, "rectype: %d\n", rectype);
69 		fprintf(stderr, "length: %d\n", length);
70 		fprintf(stderr, "queuesize: %d\n", queuesize);
71 		fprintf(stderr, "maxval_for_length: %d\n", maxval_for_length);
72 	}
73 
74 	queuesize += 1; /* fake invalid queue size */
75 	if(queuesize > maxval_for_length)
76 		++length;
77 
78 	/* ready to go, write mangled queue size */
79 	printf("+%s:%d:%d:%d:", propname, rectype, length, queuesize);
80 	/* copy rest of file */
81 	while((c = fgetc(qi)) != EOF)
82 		putchar(c);
83 }
84 
85 int
main(int argc,char * argv[])86 main(int argc, char *argv[])
87 {
88 	char *qifile = NULL;
89 	FILE *qi;
90 	int opt;
91 	while((opt = getopt(argc, argv, "dq:")) != -1) {
92 		switch (opt) {
93 		case 'q':	qifile = optarg;
94 				break;
95 		case 'd':	debug = 1;
96 				break;
97 		default:	usage();
98 				break;
99 		}
100 	}
101 
102 	if(qifile == NULL) {
103 		fprintf(stderr, "ERROR: -q option MUST be specified\n");
104 		usage();
105 	}
106 
107 	if((qi = fopen(qifile, "r")) == NULL) {
108 		perror(qifile);
109 		exit(1);
110 	}
111 	processQI(qi);
112 	return 0;
113 }
114