root/src/noit_check_tools.c

Revision ce72dc2cb9539d80628ed5642eaabe6e8bac9f0d, 1.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 years ago)

add an intperpolation tool, to helping SNMP checks -- when they get here

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7 #include "noit_check_tools.h"
8
9 int
10 noit_check_interpolate(char *buff, int len, const char *fmt,
11                        noit_hash_table *attrs,
12                        noit_hash_table *config) {
13   char *copy = NULL;
14   char closer;
15   const char *fmte, *key;
16   int replaced_something = 1;
17   int iterations = 3;
18
19   while(replaced_something && iterations > 0) {
20     char *cp = buff, * const end = buff + len;
21     iterations--;
22     replaced_something = 0;
23     while(*fmt && cp < end) {
24       switch(*fmt) {
25         case '%':
26           if(fmt[1] == '{' || fmt[1] == '[') {
27             closer = (fmt[1] == '{') ? '}' : ']';
28             fmte = fmt + 2;
29             key = fmte;
30             while(*fmte && *fmte != closer) fmte++;
31             if(*fmte == closer) {
32               /* We have a full key here */
33               const char *replacement;
34               if(!noit_hash_retrieve((closer == '}') ?  config : attrs,
35                                      key, fmte - key, (void **)&replacement))
36                 replacement = "";
37               fmt = fmte + 1; /* Format points just after the end of the key */
38               strlcpy(cp, replacement, end-cp);
39               cp += strlen(cp);
40               replaced_something = 1;
41               break;
42             }
43           }
44         default:
45           *cp++ = *fmt++;
46       }
47     }
48     *cp = '\0';
49     if(copy) free(copy);
50     if(replaced_something)
51       copy = strdup(buff);
52     fmt = copy;
53   }
54   return strlen(buff);
55 }
56
Note: See TracBrowser for help on using the browser.