src.dualinventive.com/mtinfo/tcpserver/legacy/lib/include/di-util/config.h

206 lines
9.1 KiB
C

/*
************************************************************************
**
** Copyright (c) 2009..2010 by
** Core|Vision B.V.
** Cereslaan 10b
** 5384 VT Heesch
** The Netherlands
**
** All Rights Reserved
**
************************************************************************
*/
/*
************************************************************************
**
** Project name: Dual Inventive: Utility Library
** Filename: config.h
** Author: Jack Weeland
** Date: October 27, 2009
** File version: 1.00 of October 27, 2009
**
************************************************************************
*/
/*
************************************************************************
**
** Configuration - definitions
**
************************************************************************
*/
#ifndef __CONFIG_H
#define __CONFIG_H
#include <stdarg.h>
//
// Access to the configuration settings
//
// 'what' parameters returns default/remarks
// ------ ---------- ------- ---------------
// CFG_LIBVER (none) <version> read-only, returns
// the <version> as
// (<major> << 8) | <minor>
// CFG_LIBVER_STR char*,size_t (get) strlen read-only
// CFG_TCP_SERVER char*,size_t (get) strlen di-tcp.dualinventive.com:3000
// const char* (set) strlen
// CFG_SMS_SERVER char*,size_t (get) strlen di-sms.dualinventive.com:3030
// const char* (set) strlen
// CFG_SECURE_SERVER char*,size_t (get) strlen di-secure.dualinventive.com:3300
// const char* (set) strlen
// CFG_CA_SERVER char*,size_t (get) strlen di-ca.dualinventive.com:3330
// const char* (set) strlen
// CFG_MTINFO_URL char*,size_t(get) strlen https://mtinfo.dualinventive.com/
// const char* (set) strlen
// CFG_CONFIG_DIR char*,size_t (get) strlen /etc/di/
// const char* (set) strlen
// CFG_CONFIG_FILE char*,size_t (get) strlen $(CFG_CONFIG_DIR)/$(CFG_PROGRAM).cnf
// const char* (set) strlen
// CFG_APP_DIR char*,size_t (get) strlen $(CFG_CONFIG_DIR)/$(CFG_PROGRAM)/
// const char* (set) strlen
// CFG_LOG_DIR char*,size_t (get) strlen /var/log/di/
// const char* (set) strlen
// CFG_RUNTIME_DIR char*,size_t (get) strlen /var/lib/di/
// const char* (set) strlen
// CFG_SCRIPT_DIR char*,size_t (get) strlen /usr/local/lib/di
// const char* (set) strlen
// CFG_EXTENSION_DIR char*,size_t (get) strlen /usr/local/lib/di
// const char* (set) strlen
// CFG_FIFO_DIR char*,size_t (get) strlen $(CFG_RUNTIME_DIR)/fifo/
// const char* (set) strlen
// CFG_CERT_DIR char*,size_t (get) strlen /etc/di/cert
// const char* (set) strlen
// CFG_TEXTDOMAIN_DIR char*,size_t (get) strlen /usr/local/share/locale/
// const char* (set) strlen
// CFG_TEXTDOMAIN char*,size_t (get) strlen copied from CFG_PROGRAM
// const char* (set) strlen
// CFG_SERVER_CERT char*,size_t (get) strlen $(CFG_CERT_DIR)/$(CFG_PROGRAM).crt
// const char* (set) strlen
// CFG_SERVER_KEY char*,size_t (get) strlen $(CFG_CERT_DIR)/$(CFG_PROGRAM)/$(CFG_PROGRAM).key
// const char* (set) strlen
// CFG_SERVER_PASSWD char*,size_t (get) strlen $(CFG_CERT_DIR)/$(CFG_PROGRAM)/$(CFG_PROGRAM).passphrase
// const char* (set) strlen
// CFG_TRUST_CERT char*,size_t (get) strlen $(CFG_CERT_DIR)/ca.pem
// const char* (set) strlen
// CFG_CLIENT_CERT char*,size_t (get) strlen (not set)
// const char* (set) strlen
// CFG_PROGRAM char*,size_t (get) strlen read-only, set by cfg_init()
// CFG_EXECUTABLE char*,size_t (get) strlen read-only, set by cfg_init()
// CFG_SERVERID char*,size_t (get) strlen read-only, hostname:port (program app-version)
// CFG_HOSTNAME char*,size_t (get) strlen `hostname`
// const char* (set) strlen
// CFG_SERVICE char*,size_t (get) strlen (not set)
// const char* (set) strlen
// CFG_PORT int* (get) sizeof (not set; 'get' returns -1)
// int (set) sizeof
// CFG_DATABASE char*,size_t (get) strlen (not set)
// const char* (set) strlen
// CFG_MAILCMD char*,size_t (get) strlen sendmail -t -i
// const char* (set) strlen
// CFG_HTTPGETCMD char*,size_t (get) strlen wget
// const char* (set) strlen
//
// Notes: when retrieving a string, its full length is returned
// which may be larger than the buffer size (size_t); in the
// case the buffer is too small, the behaviour of 'cfg_get_value()'
// is _undefined_. Some calls may return the first part of the
// string, some may not return anything (mostly depending on whether
// it's useful to return a partial string).
// In other words, always make sure that the buffer is large enough.
// (see recommendations for the buffer size below)
// It is allowed to call 'cfg_get_value()' with a the arguments
// "NULL, 0" to retrieve the length of a string, to make it
// possible to allocate a buffer to size.
// definitions
typedef unsigned int cfg_setting_t;
#define CFG_LIBVER 0 // library version as: major << 8 | minor
#define CFG_LIBVER_STR 1 // string "major.minor[.patch]-date", rec. bufsz >= 16
#define CFG_TCP_SERVER 8 // (default) TCP-server
#define CFG_SMS_SERVER 9 // (default) SMS-server
#define CFG_SECURE_SERVER 10 // (default) Secure Server
#define CFG_CA_SERVER 11 // (default) CA-server
#define CFG_MTINFO_URL 12 // (default) MTinfo URL
#define CFG_CONFIG_DIR 16 // recommended bufsz = PATH_MAX
#define CFG_APP_DIR 17 // recommended bufsz = PATH_MAX
#define CFG_LOG_DIR 18 // recommended bufsz = PATH_MAX
#define CFG_RUNTIME_DIR 19 // recommended bufsz = PATH_MAX
#define CFG_SCRIPT_DIR 20 // recommended bufsz = PATH_MAX
#define CFG_FIFO_DIR 21 // recommended bufsz = PATH_MAX
#define CFG_CERT_DIR 22 // recommended bufsz = PATH_MAX
#define CFG_EXTENSION_DIR 24 // recommended bufsz = PATH_MAX
#define CFG_TEXTDOMAIN_DIR 23 // recommended bufsz = PATH_MAX
#define CFG_TEXTDOMAIN 32 // recommended bufsz = PATH_MAX
#define CFG_CONFIG_FILE 33 // recommended bufsz = PATH_MAX
#define CFG_SERVER_CERT 34 // recommended bufsz = PATH_MAX
#define CFG_CLIENT_CERT 35 // recommended bufsz = PATH_MAX
#define CFG_TRUST_CERT 36 // recommended bufsz = PATH_MAX
#define CFG_SERVER_KEY 37 // recommended bufsz = PATH_MAX
#define CFG_SERVER_PASSWD 38 // recommended bufsz = PATH_MAX
#define CFG_PROGRAM 48 // program name as set with cfg_init()
#define CFG_EXECUTABLE 49 // program name as set with cfg_init()
#define CFG_SERVERID 50 // server identification; recommended bufsz = PATH_MAX
#define CFG_HOSTNAME 51 // host name; recommended bufsz = HOST_NAME_MAX
#define CFG_PORT 52 // TCP default port for a server
#define CFG_SERVICE 53 // service name from /etc/services (if so added), e.g.
// "di-tcp", "di-secure", etc
#define CFG_DATABASE 64 // location and name of the database; recommended bufsz = PATH_MAX
#define CFG_MAILCMD 65 // mail command; recommanded bufsz = PATH_MAX
#define CFG_HTTPGETCMD 66 // command to make an HTTP request; recommanded bufsz = PATH_MAX
// Type (or location) of either a configuration setting or the contents
// of a configuration file (used in 'cfg_definition_t')
typedef enum CFG_TYPE { // 'item' is a cfg_setting_t or a global variable
CFG_SETTING, // - 'item.cfg_setting' is a cfg_setting_t
CFG_BOOL, // - 'item.data' points to an 'int' (value in config file may be "ON", "OFF", "TRUE", "FALSE", "0", "1" etc)
CFG_INT, // - 'item.data' points to an 'int'
CFG_UINT, // - 'item.data' points to an 'unsigned int'
CFG_FLOAT, // - 'item.data' points to a 'double'
CFG_STR, // - 'item.data' is a 'char[]' (must be "large enough", at the programmer's discretion)
CFG_CSTR, // - 'item.data' points to a 'const char*' (malloced by 'cfg_read()')
CFG_FCN // - 'item.fcn' is a pointer to a function (cfg_type_fcn_t)
} cfg_type_t;
// Callback for 'CFG_FCN'; returns a CP3000 status or error code
typedef int (*cfg_type_fcn_t)(const char*);
typedef struct CFG_DEFINTION
{
const char *name; // name in the config file
cfg_type_t location; // see definition of 'cfg_type_t' above
union {
int cfg_setting; // identifier for the cfg_setting_t
void *data; // pointer to the data item
cfg_type_fcn_t fcn; // pointer to a function
} item;
} cfg_definition_t;
// initialization
int cfg_init(const char *program_name, const char *exec_name, const char *app_version_fmt, ...);
// funtions to retrieve or change the settings; return -1 on error
// or an integer value as indicated above.
int cfg_get_value(cfg_setting_t what, ...);
int cfg_set_value(cfg_setting_t what, ...);
// get the type for the setting (i.e. what the above return or expect as paramter)
cfg_type_t cfg_get_type(cfg_setting_t what);
// retrieve a directory and append the name of a file; bufsz should be PATH_MAX
int cfg_make_path(cfg_setting_t dir, const char *fname, char *buffer, size_t bufsz);
/*
** Read a configuration file
*/
// read a configuration file; the array of 'cfg_definition_t' items must be terminated
// by an entry where the 'name' field is NULL.
// returns TCPSTAT_OK or a TCPERR_xxx
int cfg_read(const char *fname, const cfg_definition_t*);
// print configuration using 'dbgxprintf()'
int cfg_print_settings(unsigned int dbgxprintf_flags, const cfg_definition_t*);
#endif /* __CONFIG_H */