1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_SRC_FORMAT_CONFIG_H
10 #define SQUID_SRC_FORMAT_CONFIG_H
11 
12 #include "format/Format.h"
13 #include "SquidString.h"
14 
15 #include <list>
16 
17 class StoreEntry;
18 
19 namespace Format
20 {
21 
22 class TokenTableEntry;
23 
24 /// A namespace or 'set' of tokens
25 /// components register their namespace prefix and an array of tokens
26 /// which can then be embeded in any format.
27 class TokenNamespace
28 {
29 public:
TokenNamespace(const String & nsName,TokenTableEntry const * tSet)30     TokenNamespace(const String &nsName, TokenTableEntry const *tSet) : prefix(nsName), tokenSet(tSet) {}
31 
32     /// prefix namespace name (excluding '::')
33     String prefix;
34 
35     /// array of tokens inside this namespace
36     /// The set of tokens may change, but the location of it pointed to from here must not.
37     TokenTableEntry const *tokenSet;
38 };
39 
40 /// The set of custom formats defined in squid.conf
41 ///
42 class FmtConfig
43 {
44 public:
45     /// Parse a log format directive line (logfile_format)
46     void parseFormats();
47 
48     /// Dump/display the formats currently known to the provided StoreEntry object
dumpFormats(StoreEntry * e,const char * name)49     void dumpFormats(StoreEntry *e, const char *name) {
50         formats->dump(e, name);
51     }
52 
53     /* Register a namespace set of tokens to be accepted by the format parser.
54      * Multiple arrays can be registered, they will be scanned for
55      * in order registered. So care needs to be taken that arrays registered
56      * first do not overlap or consume tokens registered later for a namespace.
57      */
58     void registerTokens(const String &nsName, TokenTableEntry const *tokenArray);
59 
60     /// Linked list of custom formats
61     Format *formats;
62 
63     /// list of token namespaces registered
64     std::list<TokenNamespace> tokens;
65 
66 #if USE_ADAPTATION
67     bool hasAdaptToken;
68 #endif
69 
70 #if ICAP_CLIENT
71     bool hasIcapToken;
72 #endif
73 };
74 
75 extern FmtConfig TheConfig;
76 
77 } // namespace Format
78 
79 // Legacy parsing wrappers
80 #define parse_format(X)  (X)->parseFormats()
81 #define free_format(X)   do{ delete (*(X)).formats; (*(X)).formats=NULL; }while(false)
82 #define dump_format(E,N,D) (D).dumpFormats((E),(N))
83 
84 #endif
85 
86