configfile.3
来自「minix操作系统最新版本(3.1.1)的源代码」· 3 代码 · 共 195 行
3
195 行
.TH CONFIGFILE 3.SH NAMEconfigfile, config_read, config_delete, config_renewed, config_length, config_issub, config_isatom, config_isstring \- generic configuration file functions.SH SYNOPSIS.ft B.nf#include <configfile.h>config_t *config_read(const char *\fIfile\fP, int \fIflags\fP, config_t *\fIcfg\fP)void config_delete(config_t *\fIcfg\fP)int config_renewed(config_t *\fIcfg\fP)size_t config_length(config_t *\fIcfg\fP)int config_issub(config_t *\fIcfg\fP)int config_isatom(config_t *\fIcfg\fP)int config_isstring(config_t *\fIcfg\fP).fi.ft P.SH DESCRIPTIONThe.B configfileroutines operate on a generic configuration file that follows the syntaxdescribed in.BR configfile (5)..PPThe interface presented by the functions above uses the following type anddefinitions from <configfile.h>:.PP.if n .in +2.if t .RS.nf.ta +\w'type'u +\w'const charmm'u +\w'word[];mm'utypedef const struct config { config_t *next; /* Next configuration file thing. */ config_t *list; /* For a { sublist }. */ const char *file; /* File and line where this is found. */ unsigned line; int flags; /* Special flags. */ char word[]; /* Payload. */} config_t;.ta +\w'#definem'u +\w'CFG_SUBLISTm'u +\w'0x0000mm'u#define CFG_CLONG 0x0001 /* strtol(word, &end, 0) is valid. */#define CFG_OLONG 0x0002 /* strtol(word, &end, 010). */#define CFG_DLONG 0x0004 /* strtol(word, &end, 10). */#define CFG_XLONG 0x0008 /* strtol(word, &end, 0x10). */#define CFG_CULONG 0x0010 /* strtoul(word, &end, 0). */#define CFG_OULONG 0x0020 /* strtoul(word, &end, 010). */#define CFG_DULONG 0x0040 /* strtoul(word, &end, 10). */#define CFG_XULONG 0x0080 /* strtoul(word, &end, 0x10). */#define CFG_STRING 0x0100 /* The word is enclosed in quotes. */#define CFG_SUBLIST 0x0200 /* This is a sublist, so no word. */#define CFG_ESCAPED 0x0400 /* Escapes are still marked with \e. */.fi.if n .in -2.if t .RE.PPIn memory a configuration file is represented as a list of.B config_tcells linked together with the.B nextfield ending with a null pointer. A sublist between braces is attached to acell at the.B listfield.Words and strings are put in the.B wordfield, a null terminated string. The.B flagsfield records the type and features of a cell. The.B CFG_*LONGflags are set if a word is a number according to one of the.B strtolor.B strtoulcalls. Purely a number, no quotes or trailing garbage. The.B CFG_STRINGflag is set if the object was enclosed in double quotes. Lastly.B CFG_SUBLISTtells if the cell is only a pointer to a sublist in braces..PPCharacters in a word or string may have been formed with the.B \eescape character. They have been parsed and expanded, but the \e is stillpresent if.B CFG_ESCAPEDis set. The.B wordarray may be changed, as long as it doesn't grow longer, so one may removethe \es like this:.PP.RS.ta +4n +4n.nfif (cfg->flags & CFG_ESCAPED) { char *p, *q; p= q= cfg->word; for (;;) { if ((*q = *p) == '\e\e') *q = *++p; if (*q == 0) break; p++; q++; }}.fi.RE.PPThe low level syntax of a config file is checked when it is read. If anerror is encountered a message is printed and the program exits with exitcode 1. What the data means is not checked, thatshould be done by the program using the data. Only the atom.B includeat the beginning of a list is special. It should be followed by a string.The string is seen as the name of a file, that is opened, read, and insertedin place of the.BR include .Unless the name of the file starts with a.BR / ,it is sought relative to the directory the current file is found in.Nonexistent files are treated as being empty..PPThe.B fileand.B linefields in each cell tell where the cell was read..SS FunctionsA configuration file is read with.BR config_read .The first argument is the file to read. The second is either.B 0or.B CFG_ESCAPEDto tell whether \e escapes should be fully expanded without leaving a trace,or if they should still be marked with a \e so that the caller knows wherethe excapes are.The third argument,.IR cfg ,should be a null pointer on the first call. If you want to reread a configfile that may have changed then.I cfgshould be what you previously read..PPWith.B config_deleteone can free up the memory that has been acquired with.BR malloc (3)to hold the contents of the configuration file..PPTo determine if the contents of configuration file has changed when rereadone uses.BR config_renewedafter.BR config_read .It returns a "changed" flag that is set when the configuration file changedand then clears that flag. It returns true on the very first call. For thefunction to work you need to feed the old data back into.BR config_read ,not delete and reread..PPThe length of a series of config structures is told by.BR config_length .It follows the.B nextfields, so a sublist between braces counts as one extra..PPThe.BR config_issub ,.BR config_isatomand.BR config_isstringfunctions are just pretty macros to test if a cell references a sublist, isa word/string, or is just a string..B CFG_SUBLISTand.B CFG_STRINGtell the same story..SH FILES.TP \w'*/etc/*.confmmmm'u.B */etc/*.confSeveral files in several.B etcdirectories..SH "SEE ALSO".BR configfile (5)..SH NOTESThe syntax of a config file puts some constraints on what you find in memory.The top level list consists entirely of sublist cells. These point to liststhat start with at least an atom, followed by a mix of atoms and sublist cells.These sublists in turn point to a list of only sublist cells (recurse now.).PPThe struct config shown above is not exactly proper C to aidreadability, read <configfile.h> itself to see why..SH AUTHORKees J. Bot (kjb@cs.vu.nl)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?