File: conf.h

package info (click to toggle)
pangoterm 0~bzr613-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 180 kB
  • sloc: ansic: 2,256; makefile: 86; sh: 15
file content (131 lines) | stat: -rw-r--r-- 5,540 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef __CONF_H__
#define __CONF_H__

#include <stdbool.h>

typedef enum {
  CONF_TYPE_STRING,
  CONF_TYPE_INT,
  CONF_TYPE_DOUBLE,
  CONF_TYPE_BOOL,
} ConfigType;

typedef union {
  char *s;
  int i;
  double d;
} ConfigValue;

typedef struct ConfigEntry ConfigEntry;
struct ConfigEntry {
  ConfigEntry *next;

  const char *longname;
  char shortname;

  ConfigType type;
  bool is_parametric;

  union {
    void *var;    /* ptr to char* or int or double */
    void (*apply)(int key, ConfigValue value);
  };
  int var_set;

  const char *desc;
  const char *argdesc;

  ConfigValue from_file;
  int var_set_from_file;

  ConfigValue dflt;
};

extern ConfigEntry *configs;

#define CONF_STRING(name,shortname_,dflt_,desc_,argdesc_) \
  static char *CONF_##name; \
  static void __attribute__((constructor)) DECLARE_##name(void) { \
    static ConfigEntry config = {                                 \
      .longname = #name,                                          \
      .shortname = shortname_,                                    \
      .type = CONF_TYPE_STRING,                                   \
      .var = &CONF_##name,                                        \
      .var_set = FALSE,                                           \
      .desc = desc_,                                              \
      .argdesc = argdesc_,                                        \
      .dflt.s = dflt_,                                            \
    };                                                            \
    config.next = configs;                                        \
    configs = &config;                                            \
  }

#define CONF_PARAMETRIC_STRING(name,shortname_,func,desc_,argdesc_) \
  static void __attribute__((constructor)) DECLARE_##name(void) { \
    static ConfigEntry config = {                                 \
      .longname = #name,                                          \
      .shortname = shortname_,                                    \
      .type = CONF_TYPE_STRING,                                   \
      .is_parametric = true,                                      \
      .apply = func,                                              \
      .desc = desc_,                                              \
      .argdesc = argdesc_,                                        \
    };                                                            \
    config.next = configs;                                        \
    configs = &config;                                            \
  }

#define CONF_INT(name,shortname_,dflt_,desc_,argdesc_) \
  static int CONF_##name = -1; \
  static void __attribute__((constructor)) DECLARE_##name(void) { \
    static ConfigEntry config = {                                 \
      .longname = #name,                                          \
      .shortname = shortname_,                                    \
      .type = CONF_TYPE_INT,                                      \
      .var = &CONF_##name,                                        \
      .var_set = FALSE,                                           \
      .desc = desc_,                                              \
      .argdesc = argdesc_,                                        \
      .dflt.i = dflt_,                                            \
    };                                                            \
    config.next = configs;                                        \
    configs = &config;                                            \
  }

#define CONF_DOUBLE(name,shortname_,dflt_,desc_,argdesc_) \
  static double CONF_##name = -1.0; \
  static void __attribute__((constructor)) DECLARE_##name(void) { \
    static ConfigEntry config = {                                 \
      .longname = #name,                                          \
      .shortname = shortname_,                                    \
      .type = CONF_TYPE_DOUBLE,                                   \
      .var = &CONF_##name,                                        \
      .var_set = FALSE,                                           \
      .desc = desc_,                                              \
      .argdesc = argdesc_,                                        \
      .dflt.d = dflt_,                                            \
    };                                                            \
    config.next = configs;                                        \
    configs = &config;                                            \
  }

#define CONF_BOOL(name,shortname_,dflt_,desc_) \
  static int CONF_##name = FALSE; \
  static void __attribute__((constructor)) DECLARE_##name(void) { \
    static ConfigEntry config = {                                 \
      .longname = #name,                                          \
      .shortname = shortname_,                                    \
      .type = CONF_TYPE_BOOL,                                     \
      .var = &CONF_##name,                                        \
      .var_set = FALSE,                                           \
      .desc = desc_,                                              \
      .argdesc = NULL,                                            \
      .dflt.i = dflt_,                                            \
    };                                                            \
    config.next = configs;                                        \
    configs = &config;                                            \
  }

int conf_parse(int *argcp, char ***argvp);

#endif