Use %ld not %l
[exim.git] / src / exim_monitor / em_init.c
1 /*************************************************
2 *                  Exim monitor                  *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
9 /* This module contains code to initialize things from the
10 environment and the arguments. */
11
12
13 #include "em_hdr.h"
14
15
16
17 /*************************************************
18 *            Decode stripchart config            *
19 *************************************************/
20
21 /* First determine how many are requested, then compile the
22 regular expressions and save the title strings. Note that
23 stripchart_number is initialized to 1 or 2 to count the always-
24 present queue stripchart, and the optional size-monitoring
25 stripchart. */
26
27 static void decode_stripchart_config(uschar *s)
28 {
29 int i;
30
31 /* Loop: first time just counts, second time does the
32 work. */
33
34 for (i = 0; i <= 1; i++)
35   {
36   int first = 1;
37   int count = 0;
38   uschar *p = s;
39
40   if (*p == '/') p++;   /* allow optional / at start */
41
42   /* This loops for all the substrings, using the first flag
43   to determine whether each is the first or second of the pairs. */
44
45   while (*p)
46     {
47     uschar *pp;
48     /* Handle continuations */
49     if (*p == '\n')
50       {
51       while (*(++p) == ' ' || *p == '\t');
52       if (*p == '/') p++;
53       }
54
55     /* Find the end of the string and count if first string */
56
57     pp = p;
58     while (*p && *p != '/') p++;
59     if (first) count++;
60
61     /* Take action on the second time round. */
62
63     if (i != 0)
64       {
65       uschar buffer[256];
66       int indx = count + stripchart_varstart - 1;
67       Ustrncpy(buffer, pp, p-pp);
68       buffer[p-pp] = 0;
69       if (first)
70         {
71         size_t offset;
72         int err;
73
74         if (!(stripchart_regex[indx] =
75                 pcre2_compile((PCRE2_SPTR)buffer,
76                       PCRE2_ZERO_TERMINATED, PCRE_COPT,
77                       &err, &offset, NULL)))
78           {
79           uschar errbuf[128];
80           pcre2_get_error_message(err, errbuf, sizeof(errbuf));
81           printf("regular expression error: %s at offset %ld "
82             "while compiling %s\n", errbuf, (long)offset, buffer);
83           exit(99);
84           }
85         }
86       else stripchart_title[indx] = string_copy(buffer);
87       }
88
89     /* Advance past the delimiter and flip the first/second flag */
90
91     p++;
92     first = !first;
93     }
94
95   /* On the first pass, we now know the number of stripcharts. Get
96   store for holding the pointers to the regular expressions and
97   title strings. */
98
99   if (i == 0)
100     {
101     stripchart_number += count;
102     stripchart_regex = (pcre2_code **)store_malloc(stripchart_number * sizeof(pcre2_code *));
103     stripchart_title = (uschar **)store_malloc(stripchart_number * sizeof(uschar *));
104     }
105   }
106 }
107
108
109 /*************************************************
110 *                    Initialize                  *
111 *************************************************/
112
113 void init(int argc, uschar **argv)
114 {
115 int x;
116 size_t erroroffset;
117 uschar *s;
118 const uschar *error;
119
120 argc = argc;     /* These are currently unused. */
121 argv = argv;
122
123 /* Deal with simple values in the environment. */
124
125 s = US getenv("ACTION_OUTPUT");
126 if (s != NULL)
127   {
128   if (Ustrcmp(s, "no") == 0) action_output = FALSE;
129   if (Ustrcmp(s, "yes") == 0) action_output = TRUE;
130   }
131
132 s = US getenv("ACTION_QUEUE_UPDATE");
133 if (s != NULL)
134   {
135   if (Ustrcmp(s, "no") == 0) action_queue_update = FALSE;
136   if (Ustrcmp(s, "yes") == 0) action_queue_update = TRUE;
137   }
138
139 s = US getenv("BODY_MAX");
140 if (s != NULL && (x = Uatoi(s)) != 0) body_max = x;
141
142 s = US getenv("EXIM_PATH");
143 if (s != NULL) exim_path = string_copy(s);
144
145 s = US getenv("EXIMON_EXIM_CONFIG");
146 if (s != NULL) alternate_config = string_copy(s);
147
148 s = US getenv("LOG_BUFFER");
149 if (s != NULL)
150   {
151   uschar c[1];
152   if (sscanf(CS s, "%d%c", &x, c) > 0)
153     {
154     if (c[0] == 'K' || c[0] == 'k') x *= 1024;
155     if (x < 1024) x = 1024;
156     log_buffer_size = x;
157     }
158   }
159
160 s = US getenv("LOG_DEPTH");
161 if (s != NULL && (x = Uatoi(s)) != 0) log_depth = x;
162
163 s = US getenv("LOG_FILE_NAME");
164 if (s != NULL) log_file = string_copy(s);
165
166 s = US getenv("LOG_FONT");
167 if (s != NULL) log_font = string_copy(s);
168
169 s = US getenv("LOG_WIDTH");
170 if (s != NULL && (x = Uatoi(s)) != 0) log_width = x;
171
172 s = US getenv("MENU_EVENT");
173 if (s != NULL) menu_event = string_copy(s);
174
175 s = US getenv("MIN_HEIGHT");
176 if (s != NULL && (x = Uatoi(s)) > 0) min_height = x;
177
178 s = US getenv("MIN_WIDTH");
179 if (s != NULL && (x = Uatoi(s)) > 0) min_width = x;
180
181 s = US getenv("QUALIFY_DOMAIN");
182 if (s != NULL) qualify_domain = string_copy(s);
183   else qualify_domain = US"";  /* Don't want NULL */
184
185 s = US getenv("QUEUE_DEPTH");
186 if (s != NULL && (x = Uatoi(s)) != 0) queue_depth = x;
187
188 s = US getenv("QUEUE_FONT");
189 if (s != NULL) queue_font = string_copy(s);
190
191 s = US getenv("QUEUE_INTERVAL");
192 if (s != NULL && (x = Uatoi(s)) != 0) queue_update = x;
193
194 s = US getenv("QUEUE_MAX_ADDRESSES");
195 if (s != NULL && (x = Uatoi(s)) != 0) queue_max_addresses = x;
196
197 s = US getenv("QUEUE_WIDTH");
198 if (s != NULL && (x = Uatoi(s)) != 0) queue_width = x;
199
200 s = US getenv("SPOOL_DIRECTORY");
201 if (s != NULL) spool_directory = string_copy(s);
202
203 s = US getenv("START_SMALL");
204 if (s != NULL && Ustrcmp(s, "yes") == 0) start_small = 1;
205
206 s = US getenv("TEXT_DEPTH");
207 if (s != NULL && (x = Uatoi(s)) != 0) text_depth = x;
208
209 s = US getenv("WINDOW_TITLE");
210 if (s != NULL) window_title = string_copy(s);
211
212 /* Deal with stripchart configuration. First see if we are monitoring
213 the size of a partition, then deal with log stripcharts in a separate
214 function */
215
216 s = US getenv("SIZE_STRIPCHART");
217 if (s != NULL && *s != 0)
218   {
219   stripchart_number++;
220   stripchart_varstart++;
221   size_stripchart = string_copy(s);
222   s = US getenv("SIZE_STRIPCHART_NAME");
223   if (s != NULL && *s != 0) size_stripchart_name = string_copy(s);
224   }
225
226 s = US getenv("LOG_STRIPCHARTS");
227 if (s != NULL) decode_stripchart_config(s);
228
229 s = US getenv("STRIPCHART_INTERVAL");
230 if (s != NULL && (x = Uatoi(s)) != 0) stripchart_update = x;
231
232 s = US getenv("QUEUE_STRIPCHART_NAME");
233 queue_stripchart_name = (s != NULL)? string_copy(s) : US"queue";
234
235 /* Compile the regex for matching yyyy-mm-dd at the start of a string. */
236
237 yyyymmdd_regex = pcre2_compile((PCRE2_SPTR)"^\\d{4}-\\d\\d-\\d\\d\\s",
238   PCRE2_ZERO_TERMINATED, PCRE_COPT, &x, &erroroffset, NULL);
239 }
240
241 /* End of em_init.c */