]> git.decadent.org.uk Git - dak.git/blob - tools/dsync-0.0/libdsync/contrib/configuration.cc
Merge commit 'godog/master' into merge
[dak.git] / tools / dsync-0.0 / libdsync / contrib / configuration.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description                                                          /*{{{*/
3 // $Id: configuration.cc,v 1.5 1999/11/17 05:59:29 jgg Exp $
4 /* ######################################################################
5
6    Configuration Class
7    
8    This class provides a configuration file and command line parser
9    for a tree-oriented configuration environment. All runtime configuration
10    is stored in here.
11    
12    ##################################################################### */
13                                                                         /*}}}*/
14 // Include files                                                        /*{{{*/
15 #ifdef __GNUG__
16 #pragma implementation "dsync/configuration.h"
17 #endif
18 #include <dsync/configuration.h>
19 #include <dsync/error.h>
20 #include <dsync/strutl.h>
21
22 #include <stdio.h>
23 #include <fstream>
24 #include <iostream>
25 using namespace std;
26                                                                         /*}}}*/
27
28 Configuration *_config = new Configuration;
29
30 // Configuration::Configuration - Constructor                           /*{{{*/
31 // ---------------------------------------------------------------------
32 /* */
33 Configuration::Configuration()
34 {
35    Root = new Item;
36 }
37                                                                         /*}}}*/
38 // Configuration::Lookup - Lookup a single item                         /*{{{*/
39 // ---------------------------------------------------------------------
40 /* This will lookup a single item by name below another item. It is a 
41    helper function for the main lookup function */
42 Configuration::Item *Configuration::Lookup(Item *Head,const char *S,
43                                            unsigned long Len,bool Create)
44 {
45    int Res = 1;
46    Item *I = Head->Child;
47    Item **Last = &Head->Child;
48    
49    // Empty strings match nothing. They are used for lists.
50    if (Len != 0)
51    {
52       for (; I != 0; Last = &I->Next, I = I->Next)
53          if ((Res = stringcasecmp(I->Tag.c_str(), I->Tag.c_str() + strlen(I->Tag.c_str()),S,S + Len)) == 0)
54             break;
55    }
56    else
57       for (; I != 0; Last = &I->Next, I = I->Next);
58       
59    if (Res == 0)
60       return I;
61    if (Create == false)
62       return 0;
63    
64    I = new Item;
65    I->Tag = string(S,Len);
66    I->Next = *Last;
67    I->Parent = Head;
68    *Last = I;
69    return I;
70 }
71                                                                         /*}}}*/
72 // Configuration::Lookup - Lookup a fully scoped item                   /*{{{*/
73 // ---------------------------------------------------------------------
74 /* This performs a fully scoped lookup of a given name, possibly creating
75    new items */
76 Configuration::Item *Configuration::Lookup(const char *Name,bool Create)
77 {
78    if (Name == 0)
79       return Root->Child;
80    
81    const char *Start = Name;
82    const char *End = Start + strlen(Name);
83    const char *TagEnd = Name;
84    Item *Itm = Root;
85    for (; End - TagEnd >= 2; TagEnd++)
86    {
87       if (TagEnd[0] == ':' && TagEnd[1] == ':')
88       {
89          Itm = Lookup(Itm,Start,TagEnd - Start,Create);
90          if (Itm == 0)
91             return 0;
92          TagEnd = Start = TagEnd + 2;    
93       }
94    }   
95
96    // This must be a trailing ::, we create unique items in a list
97    if (End - Start == 0)
98    {
99       if (Create == false)
100          return 0;
101    }
102    
103    Itm = Lookup(Itm,Start,End - Start,Create);
104    return Itm;
105 }
106                                                                         /*}}}*/
107 // Configuration::Find - Find a value                                   /*{{{*/
108 // ---------------------------------------------------------------------
109 /* */
110 string Configuration::Find(const char *Name,const char *Default)
111 {
112    Item *Itm = Lookup(Name,false);
113    if (Itm == 0 || Itm->Value.empty() == true)
114    {
115       if (Default == 0)
116          return string();
117       else
118          return Default;
119    }
120    
121    return Itm->Value;
122 }
123                                                                         /*}}}*/
124 // Configuration::FindFile - Find a Filename                            /*{{{*/
125 // ---------------------------------------------------------------------
126 /* Directories are stored as the base dir in the Parent node and the
127    sub directory in sub nodes with the final node being the end filename
128  */
129 string Configuration::FindFile(const char *Name,const char *Default)
130 {
131    Item *Itm = Lookup(Name,false);
132    if (Itm == 0 || Itm->Value.empty() == true)
133    {
134       if (Default == 0)
135          return string();
136       else
137          return Default;
138    }
139    
140    // Absolute path
141    if (Itm->Value[0] == '/' || Itm->Parent == 0)
142       return Itm->Value;
143    
144    // ./ is also considered absolute as is anything with ~ in it
145    if (Itm->Value[0] != 0 && 
146        ((Itm->Value[0] == '.' && Itm->Value[1] == '/') ||
147         (Itm->Value[0] == '~' && Itm->Value[1] == '/')))
148       return Itm->Value;
149    
150    if (Itm->Parent->Value.end()[-1] == '/')
151       return Itm->Parent->Value + Itm->Value;
152    else
153       return Itm->Parent->Value + '/' + Itm->Value;
154 }
155                                                                         /*}}}*/
156 // Configuration::FindDir - Find a directory name                       /*{{{*/
157 // ---------------------------------------------------------------------
158 /* This is like findfile execept the result is terminated in a / */
159 string Configuration::FindDir(const char *Name,const char *Default)
160 {
161    string Res = FindFile(Name,Default);
162    if (Res.end()[-1] != '/')
163       return Res + '/';
164    return Res;
165 }
166                                                                         /*}}}*/
167 // Configuration::FindI - Find an integer value                         /*{{{*/
168 // ---------------------------------------------------------------------
169 /* */
170 int Configuration::FindI(const char *Name,int Default)
171 {
172    Item *Itm = Lookup(Name,false);
173    if (Itm == 0 || Itm->Value.empty() == true)
174       return Default;
175    
176    char *End;
177    int Res = strtol(Itm->Value.c_str(),&End,0);
178    if (End == Itm->Value.c_str())
179       return Default;
180    
181    return Res;
182 }
183                                                                         /*}}}*/
184 // Configuration::FindB - Find a boolean type                           /*{{{*/
185 // ---------------------------------------------------------------------
186 /* */
187 bool Configuration::FindB(const char *Name,bool Default)
188 {
189    Item *Itm = Lookup(Name,false);
190    if (Itm == 0 || Itm->Value.empty() == true)
191       return Default;
192    
193    return StringToBool(Itm->Value,Default);
194 }
195                                                                         /*}}}*/
196 // Configuration::Set - Set a value                                     /*{{{*/
197 // ---------------------------------------------------------------------
198 /* */
199 void Configuration::Set(const char *Name,string Value)
200 {
201    Item *Itm = Lookup(Name,true);
202    if (Itm == 0)
203       return;
204    Itm->Value = Value;
205 }
206                                                                         /*}}}*/
207 // Configuration::Set - Set an integer value                            /*{{{*/
208 // ---------------------------------------------------------------------
209 /* */
210 void Configuration::Set(const char *Name,int Value)
211 {
212    Item *Itm = Lookup(Name,true);
213    if (Itm == 0)
214       return;
215    char S[300];
216    snprintf(S,sizeof(S),"%i",Value);
217    Itm->Value = S;
218 }
219                                                                         /*}}}*/
220 // Configuration::Exists - Returns true if the Name exists              /*{{{*/
221 // ---------------------------------------------------------------------
222 /* */
223 bool Configuration::Exists(const char *Name)
224 {
225    Item *Itm = Lookup(Name,false);
226    if (Itm == 0)
227       return false;
228    return true;
229 }
230                                                                         /*}}}*/
231 // Configuration::Dump - Dump the config                                /*{{{*/
232 // ---------------------------------------------------------------------
233 /* Dump the entire configuration space */
234 void Configuration::Dump()
235 {
236    /* Write out all of the configuration directives by walking the 
237       configuration tree */
238    const Configuration::Item *Top = _config->Tree(0);
239    for (; Top != 0;)
240    {
241       clog << Top->FullTag() << " \"" << Top->Value << "\";" << endl;
242       
243       if (Top->Child != 0)
244       {
245          Top = Top->Child;
246          continue;
247       }
248       
249       while (Top != 0 && Top->Next == 0)
250          Top = Top->Parent;
251       if (Top != 0)
252          Top = Top->Next;
253    }   
254 }
255                                                                         /*}}}*/
256
257 // Configuration::Item::FullTag - Return the fully scoped tag           /*{{{*/
258 // ---------------------------------------------------------------------
259 /* */
260 string Configuration::Item::FullTag() const
261 {
262    if (Parent == 0 || Parent->Parent == 0)
263       return Tag;
264    return Parent->FullTag() + "::" + Tag;
265 }
266                                                                         /*}}}*/
267
268 // ReadConfigFile - Read a configuration file                           /*{{{*/
269 // ---------------------------------------------------------------------
270 /* The configuration format is very much like the named.conf format
271    used in bind8, in fact this routine can parse most named.conf files. */
272 bool ReadConfigFile(Configuration &Conf,string FName)
273 {
274    // Open the stream for reading
275    ifstream F(FName.c_str(),ios::in);
276    if (!F != 0)
277       return _error->Errno("ifstream::ifstream","Opening configuration file %s",FName.c_str());
278    
279    char Buffer[300];
280    string LineBuffer;
281    string Stack[100];
282    unsigned int StackPos = 0;
283    
284    // Parser state
285    string ParentTag;
286    
287    int CurLine = 0;
288    bool InComment = false;
289    while (F.eof() == false)
290    {
291       F.getline(Buffer,sizeof(Buffer));
292       CurLine++;
293       _strtabexpand(Buffer,sizeof(Buffer));
294       _strstrip(Buffer);
295
296       // Multi line comment
297       if (InComment == true)
298       {
299          for (const char *I = Buffer; *I != 0; I++)
300          {
301             if (*I == '*' && I[1] == '/')
302             {
303                memmove(Buffer,I+2,strlen(I+2) + 1);
304                InComment = false;
305                break;
306             }       
307          }
308          if (InComment == true)
309             continue;
310       }
311       
312       // Discard single line comments
313       bool InQuote = false;
314       for (char *I = Buffer; *I != 0; I++)
315       {
316          if (*I == '"')
317             InQuote = !InQuote;
318          if (InQuote == true)
319             continue;
320          
321          if (*I == '/' && I[1] == '/')
322          {
323             *I = 0;
324             break;
325          }
326       }
327       
328       // Look for multi line comments
329       for (char *I = Buffer; *I != 0; I++)
330       {
331          if (*I == '"')
332             InQuote = !InQuote;
333          if (InQuote == true)
334             continue;
335          
336          if (*I == '/' && I[1] == '*')
337          {
338             InComment = true;
339             for (char *J = Buffer; *J != 0; J++)
340             {
341                if (*J == '*' && J[1] == '/')
342                {
343                   memmove(I,J+2,strlen(J+2) + 1);
344                   InComment = false;
345                   break;
346                }               
347             }
348             
349             if (InComment == true)
350             {
351                *I = 0;
352                break;
353             }       
354          }
355       }
356       
357       // Blank
358       if (Buffer[0] == 0)
359          continue;
360       
361       // We now have a valid line fragment
362       for (char *I = Buffer; *I != 0;)
363       {
364          if (*I == '{' || *I == ';' || *I == '}')
365          {
366             // Put the last fragement into the buffer
367             char *Start = Buffer;
368             char *Stop = I;
369             for (; Start != I && isspace(*Start) != 0; Start++);
370             for (; Stop != Start && isspace(Stop[-1]) != 0; Stop--);
371             if (LineBuffer.empty() == false && Stop - Start != 0)
372                LineBuffer += ' ';
373             LineBuffer += string(Start,Stop - Start);
374             
375             // Remove the fragment
376             char TermChar = *I;
377             memmove(Buffer,I + 1,strlen(I + 1) + 1);
378             I = Buffer;
379
380             // Move up a tag
381             if (TermChar == '}')
382             {
383                if (StackPos == 0)
384                   ParentTag = string();
385                else
386                   ParentTag = Stack[--StackPos];
387             }
388             
389             // Syntax Error
390             if (TermChar == '{' && LineBuffer.empty() == true)
391                return _error->Error("Syntax error %s:%u: Block starts with no name.",FName.c_str(),CurLine);
392             
393             if (LineBuffer.empty() == true)
394                continue;
395
396             // Parse off the tag
397             string Tag;
398             const char *Pos = LineBuffer.c_str();
399             if (ParseQuoteWord(Pos,Tag) == false)
400                return _error->Error("Syntax error %s:%u: Malformed Tag",FName.c_str(),CurLine);     
401             
402             // Go down a level
403             if (TermChar == '{')
404             {
405                if (StackPos <= 100)
406                   Stack[StackPos++] = ParentTag;
407                if (ParentTag.empty() == true)
408                   ParentTag = Tag;
409                else
410                   ParentTag += string("::") + Tag;
411                Tag = string();
412             }
413
414             // Parse off the word
415             string Word;
416             if (ParseCWord(Pos,Word) == false)
417             {
418                if (TermChar != '{')
419                {
420                   Word = Tag;
421                   Tag = "";
422                }               
423             }
424             
425             // Generate the item name
426             string Item;
427             if (ParentTag.empty() == true)
428                Item = Tag;
429             else
430             {
431                if (TermChar != '{' || Tag.empty() == false)
432                   Item = ParentTag + "::" + Tag;
433                else
434                   Item = ParentTag;
435             }
436             
437             // Set the item in the configuration class
438             Conf.Set(Item,Word);
439                             
440             // Empty the buffer
441             LineBuffer = string();
442          }
443          else
444             I++;
445       }
446
447       // Store the fragment
448       const char *Stripd = _strstrip(Buffer);
449       if (*Stripd != 0 && LineBuffer.empty() == false)
450          LineBuffer += " ";
451       LineBuffer += Stripd;
452    }
453    
454    return true;
455 }
456                                                                         /*}}}*/