1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*  Fluent Bit
4  *  ==========
5  *  Copyright (C) 2019-2021 The Fluent Bit Authors
6  *  Copyright (C) 2015-2018 Treasure Data Inc.
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  */
20 
21 #include <fluent-bit/flb_info.h>
22 #include <fluent-bit/flb_env.h>
23 #include <fluent-bit/flb_mem.h>
24 #include <fluent-bit/flb_meta.h>
25 
26 /*
27  * A meta is a way to extend the configuration through specific commands, e.g:
28  *
29  *   @SET a=b
30  *
31  * the meta command is prefixed with @, the command it self is 'SET' and have
32  * the parameters 'a=b'.
33  *
34  * Each command have their own handler function: meta_cmd_ABC().
35  */
36 
37 /* @SET command: register a key/value as a configuration variable */
meta_cmd_set(struct flb_config * ctx,const char * params)38 static int meta_cmd_set(struct flb_config *ctx, const char *params)
39 {
40     int ret;
41     int len;
42     char *p;
43     char *key;
44     char *val;
45 
46     p = strchr(params, '=');
47     if (!p) {
48         fprintf(stderr, "[meta SET] invalid parameter '%s'\n", params);
49         return -1;
50     }
51 
52     len = strlen(params);
53     key = mk_string_copy_substr(params, 0, p - params);
54     if (!key) {
55         return -1;
56     }
57 
58     val = mk_string_copy_substr(params, (p - params) + 1, len);
59     if (!val) {
60         flb_free(key);
61         return -1;
62     }
63 
64     /* Set the variable in our local environment */
65     ret = flb_env_set(ctx->env, key, val);
66     flb_free(key);
67     flb_free(val);
68 
69     return ret;
70 }
71 
72 /* Run a specific command */
flb_meta_run(struct flb_config * ctx,const char * cmd,const char * params)73 int flb_meta_run(struct flb_config *ctx, const char *cmd, const char *params)
74 {
75     if (strcasecmp(cmd, "SET") == 0) {
76         return meta_cmd_set(ctx, params);
77     }
78 
79     return -1;
80 }
81