add simple test for command()
[nsca-ng.git] / src / client / parse.c
blobb37be8e879c3a9de5e37d8eca6049833a2da9bba
1 /*
2 * Copyright (c) 2013 Holger Weiss <holger@weiss.in-berlin.de>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
28 #if HAVE_CONFIG_H
29 # include <config.h>
30 #endif
32 #include <stdlib.h>
33 #include <string.h>
34 #include <time.h>
36 #include "log.h"
37 #include "parse.h"
38 #include "system.h"
39 #include "util.h"
40 #include "wrappers.h"
42 static char *escape(const char *);
45 * Exported functions.
48 char *
49 parse_command(const char *line)
51 char *command;
53 debug("Parsing monitoring command");
55 line = skip_whitespace(line);
56 if (line[0] == '[')
57 command = xstrdup(line);
58 else
59 xasprintf(&command, "[%lu] %s", (unsigned long)time(NULL),
60 line);
62 return command;
65 char *
66 parse_check_result(const char *input, char delimiter)
68 const char *fields[4] = { NULL, NULL, NULL, NULL };
69 char *command, *escaped;
70 int lengths[3];
71 int n, pos, start_pos;
73 debug("Parsing check result");
75 if (strpbrk(input, "\\\n") != NULL)
76 input = escaped = escape(input);
77 else
78 escaped = NULL;
80 fields[0] = input;
81 start_pos = 0;
82 n = 1;
84 for (pos = 0; n < 4 && input[pos] != '\0'; pos++)
85 if (input[pos] == delimiter) {
86 lengths[n - 1] = pos - start_pos;
87 debug("Check result field %d has %d characters (%d-%d)",
88 n, lengths[n - 1], start_pos, pos - 1);
90 /* Handle the next field. */
91 start_pos = pos + 1;
92 fields[n] = &input[start_pos];
93 n++;
96 switch (n) {
97 case 3:
98 debug("Got host check result");
99 xasprintf(&command,
100 "[%lu] PROCESS_HOST_CHECK_RESULT;%.*s;%.*s;%s",
101 (unsigned long)time(NULL),
102 lengths[0], fields[0],
103 lengths[1], fields[1],
104 fields[2]);
105 break;
106 case 4:
107 debug("Got service check result");
108 xasprintf(&command,
109 "[%lu] PROCESS_SERVICE_CHECK_RESULT;%.*s;%.*s;%.*s;%s",
110 (unsigned long)time(NULL),
111 lengths[0], fields[0],
112 lengths[1], fields[1],
113 lengths[2], fields[2],
114 fields[3]);
115 break;
116 default:
117 die("Input format incorrect, see the %s(8) man page",
118 getprogname());
121 if (escaped != NULL)
122 free(escaped);
124 return command;
128 * Static functions.
131 static char *
132 escape(const char *input)
134 const char *in;
135 char *escaped, *out;
136 size_t size = strlen(input) + 1;
138 for (in = input; *in != '\0'; in++)
139 if (*in == '\\' || *in == '\n')
140 size++;
142 escaped = xmalloc(size);
144 for (in = input, out = escaped; *in != '\0'; in++, out++)
145 switch (*in) {
146 case '\\':
147 *out++ = '\\';
148 *out = '\\';
149 break;
150 case '\n':
151 *out++ = '\\';
152 *out = 'n';
153 break;
154 default:
155 *out = *in;
157 *out = '\0';
159 return escaped;
162 /* vim:set joinspaces noexpandtab textwidth=80 cinoptions=(4,u0: */