source: CMT/HEAD/source/cmt_symbol.cxx @ 549

Last change on this file since 549 was 549, checked in by rybkin, 14 years ago

See C.L. 434

  • Property svn:eol-style set to native
File size: 83.1 KB
Line 
1//-----------------------------------------------------------
2// Copyright Christian Arnault LAL-Orsay CNRS
3// arnault@lal.in2p3.fr
4// See the complete license in cmt_license.txt "http://www.cecill.info".
5//-----------------------------------------------------------
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <ctype.h>
11
12#include "cmt_use.h"
13#include "cmt_symbol.h"
14#include "cmt_system.h"
15#include "cmt_database.h"
16#include "cmt_log.h"
17
18// Global inhibitor of any display
19bool Symbol::m_inhibit_display = false;
20
21//-------------------------------------------------------------
22class SetBuilder : public ValueBuilder
23{
24public:
25  const cmt_string build (const Symbol& symbol,
26                          const cmt_string& tag_name = "");
27  const cmt_string clean (const Symbol& symbol,
28                          const cmt_string& tag_name = "")
29      {
30        static const cmt_string empty = "";
31        return (empty);
32      }
33};
34//-------------------------------------------------------------
35
36//-------------------------------------------------------------
37class PathBuilder : public ValueBuilder
38{
39public:
40  const cmt_string build (const Symbol& symbol,
41                          const cmt_string& tag_name = "");
42  const cmt_string clean (const Symbol& symbol,
43                          const cmt_string& tag_name = "");
44};
45//-------------------------------------------------------------
46
47//-------------------------------------------------------------
48class MacroBuilder : public ValueBuilder
49{
50public:
51  const cmt_string build (const Symbol& symbol,
52                          const cmt_string& tag_name = "");
53  const cmt_string clean (const Symbol& symbol,
54                          const cmt_string& tag_name = "")
55      {
56        static const cmt_string empty = "";
57        return (empty);
58      }
59};
60//-------------------------------------------------------------
61
62//-------------------------------------------------------------
63class ScriptBuilder : public ValueBuilder
64{
65public:
66  const cmt_string build (const Symbol& symbol,
67                          const cmt_string& tag_name = "");
68  const cmt_string clean (const Symbol& symbol,
69                          const cmt_string& tag_name = "")
70      {
71        static const cmt_string empty = "";
72        return (empty);
73      }
74};
75//-------------------------------------------------------------
76
77//-------------------------------------------------------------
78class ActionBuilder : public ValueBuilder
79{
80public:
81  const cmt_string build (const Symbol& symbol,
82                          const cmt_string& tag_name = "");
83  const cmt_string clean (const Symbol& symbol,
84                          const cmt_string& tag_name = "")
85      {
86        static const cmt_string empty = "";
87        return (empty);
88      }
89};
90//-------------------------------------------------------------
91
92//-------------------------------------------------------------
93class symbol_marker
94{
95public:
96  symbol_marker ()
97  {
98    ptr = cmt_string::npos;
99    pattern = 0;
100    intro = 0;
101  }
102
103  symbol_marker (int a_ptr, char a_pattern, int a_intro)
104  {
105    ptr = a_ptr;
106    pattern = a_pattern;
107    intro = a_intro;
108  }
109
110  symbol_marker (const symbol_marker& other)
111  {
112    ptr = other.ptr;
113    pattern = other.pattern;
114    intro = other.intro;
115  }
116
117  void set (int a_ptr, char a_pattern, int a_intro)
118  {
119    ptr = a_ptr;
120    pattern = a_pattern;
121    intro = a_intro;
122  }
123
124  static symbol_marker& get_lowest (symbol_marker markers[], int count)
125  {
126    static symbol_marker result;
127    int real_count = 0;
128    int i;
129
130      // Check that at least one marker has result
131
132    for (i = 0; i < count; i++)
133      {
134        if (markers[i].ptr != cmt_string::npos) real_count++;
135      }
136
137    if (real_count == 0) return (result);
138
139    // since we've passed the previous test,
140    // at least one entry is not npos.
141    // Now discards other npos by moving them to the end
142   
143    for (i = 0; i < count;)
144      {
145        if (markers[i].ptr == cmt_string::npos)
146          {
147            markers[i] = markers[count-1];
148            count--;
149            if (count == 0) break;
150          }
151        else
152          {
153            i++;
154          }
155      }
156   
157    if (count == 0) return (result);
158   
159    // now all entries in [0, count-1] are not npos
160    // let's sort the lowest one in [0]
161   
162    for (i = 1; i < count;)
163      {
164        if (markers[0].ptr > markers[i].ptr)
165          {
166            symbol_marker temp = markers[0];
167            markers[0] = markers[i];
168            markers[i] = temp;
169            i = 1;
170          }
171        else
172          {
173            i++;
174          }
175      }
176   
177    return (markers[0]);
178  }
179
180  int ptr;
181  char pattern;
182  int intro;
183};
184//-------------------------------------------------------------
185
186/**
187
188   Attempt to substitute all occurrences of
189
190      ${<symbol_name>}
191      $(<symbol_name>)
192      %<symbol_name>%    [on Windows only]
193
194      by the specified value in the given text.
195
196    This is for one symbol only
197
198 */
199static void resolve_value (cmt_string& text,
200                           const cmt_string& symbol_name,
201                           const cmt_string& value)
202{
203  static cmt_string pattern;
204
205  pattern = "${";
206  pattern += symbol_name;
207  pattern += "}";
208
209  text.replace_all (pattern, value);
210
211  pattern = "$(";
212  pattern += symbol_name;
213  pattern += ")";
214
215  text.replace_all (pattern, value);
216
217#ifdef WIN32
218  pattern = "%";
219  pattern += symbol_name;
220  pattern += "%";
221
222  text.replace_all (pattern, value);
223#endif
224}
225
226/**
227
228   Attempt to substitute into the specified text all occurrences of
229
230      ${xxx}
231      $(xxx)
232      `xxx`
233      %xxx%    [on Windows only]
234
235      by the appropriate value:
236
237        for `xxx` :
238
239            xxx is considered as a shell command. Value is the result of its execution
240
241        for other patterns:
242
243            if xxx is a symbol name, its value is substituted to the pattern
244            otherwise, xxx is tried as an environment variable
245
246
247     ===> In all cases, the pattern is filtered away.
248
249
250 */
251static void resolve_value (cmt_string& text)
252{
253  //static cmt_regexp reg ("[$%`]");
254
255  //if (!reg.match (text)) return;
256
257  cmt_string pattern;
258  cmt_string symbol_name;
259  char end_pattern;
260
261  int start = 0;
262
263  for (;;)
264    {
265      int begin;
266      int end;
267
268      symbol_marker markers[4];
269      int num = 0;
270
271      markers[num].set (text.find (start, "$("), ')', 2); num++;
272      markers[num].set (text.find (start, "${"), '}', 2); num++;
273      markers[num].set (text.find (start, "`"), '`', 1); num++;
274
275#ifdef WIN32
276      markers[num].set (text.find (start, "%"), '%', 1); num++;
277#endif
278
279      // Find the first matching pattern
280
281      symbol_marker& marker = symbol_marker::get_lowest (markers, num);
282
283      begin = marker.ptr;
284
285      if (begin == cmt_string::npos) break;
286
287      end_pattern = marker.pattern;
288      start = begin + marker.intro;
289
290      end = text.find (start, end_pattern);
291      if (end == cmt_string::npos)
292        {
293          // The pattern is a fake one (no ending!)
294          start++;
295          continue;
296        }
297
298      // This should never happen...
299      if (end < begin) break;
300
301      // Extract the complete pattern
302      text.substr (begin, end - begin + 1, pattern);
303
304      // Then only the symbol name
305      text.substr (begin + marker.intro, end - begin - marker.intro, symbol_name);
306
307      if (text[begin] == '`')
308        {
309          cmt_string command = symbol_name;
310          resolve_value (command);
311
312            // The value is a shell command that first needs
313            // to be applied. The output of the command is then substituted
314
315          cmt_string result;
316
317          Symbol::all_set ();
318          CmtSystem::execute (command, result);
319         
320          int pos;
321          pos = result.find ('\n');
322          if (pos != cmt_string::npos) result.erase (pos);
323          pos = result.find ('\r');
324          if (pos != cmt_string::npos) result.erase (pos);
325         
326          if (Cmt::get_debug ())
327            {
328              cout << "   Executing [" << command << "] to expand a symbol value =>[" 
329                   << result << "]" << endl;
330            }
331         
332          text.replace_all (pattern, result);
333
334          // The substitution will restart from the same place
335          // allowing for recursive replacements
336          start = begin;
337        }
338      else
339        {
340          Symbol* symbol = Symbol::find (symbol_name);
341          if (symbol != 0)
342            {
343                // Symbol found
344              cmt_string value = symbol->resolve_macro_value ();
345              text.replace_all (pattern, value);
346             
347                // The substitution will restart from the same place
348                // allowing for recursive replacements
349              start = begin;
350            }
351          else
352            {
353                // Symbol not found. Look for env. variable
354              cmt_string value = CmtSystem::getenv (symbol_name);
355             
356                // When the env. variable is not defined, the replacement is empty
357                // thus all $(xxx) ${xxx} %xxx% patterns are always filtered away.
358              text.replace_all (pattern, value);
359             
360                // The substitution will restart from the same place
361                // allowing for recursive replacements
362              start = begin;
363            }
364        }
365    }
366}
367
368/**
369
370   Attempt to substitute all occurrences of
371
372      ${xxx}
373      $(xxx)
374      `xxx`
375      %xxx%    [on Windows only]
376
377      by the appropriate value:
378
379        for `xxx` :
380
381            xxx is considered as a shell command. Value is the result of its execution
382
383        for other patterns:
384
385            if xxx is a macro name, its value is substituted to the pattern
386
387            if xxx is a set or a path, it is kept in place (after fixing the pattern
388            according to the OS style)
389
390            otherwise it is simply kept in place
391
392 */
393static void resolve_value_for_macros (cmt_string& text)
394{
395  cmt_string pattern;
396  cmt_string symbol_name;
397  char end_pattern;
398
399  int start = 0;
400
401  for (;;)
402    {
403      //
404      // Try and substitute all ${xxx} $(xxx) or %xxx% patterns
405      // using symbol values, only when the symbol is a macro.
406      //
407
408      int begin;
409      int end;
410
411      symbol_marker markers[4];
412      int num = 0;
413
414      markers[num].set (text.find (start, "$("), ')', 2); num++;
415      markers[num].set (text.find (start, "${"), '}', 2); num++;
416
417#ifdef WIN32
418      markers[num].set (text.find (start, "%"), '%', 1); num++;
419#endif
420
421      markers[num].set (text.find (start, "`"), '`', 1); num++;
422
423      // Find the first of three patterns
424
425      symbol_marker& marker = symbol_marker::get_lowest (markers, num);
426
427      begin = marker.ptr;
428
429      if (begin == cmt_string::npos) break;
430
431      end_pattern = marker.pattern;
432      start = begin + marker.intro;
433
434      end = text.find (start, end_pattern);
435      if (end == cmt_string::npos)
436        {
437          // The pattern is a fake one (no ending!)
438          start++;
439          continue;
440        }
441
442      // This should never happen...
443      if (end < begin) break;
444
445      // Extract the complete pattern
446      text.substr (begin, end - begin + 1, pattern);
447
448      // Then only the macro name
449      text.substr (begin + marker.intro, end - begin - marker.intro, symbol_name);
450
451      if (text[begin] == '`')
452        {
453          cmt_string command = symbol_name;
454          resolve_value (command);
455
456            // The macro value is a shell command that first needs
457            // to be applied. The output of the command is substituted
458
459          cmt_string result;
460
461          Symbol::all_set ();
462          CmtSystem::execute (command, result);
463         
464          int pos;
465          pos = result.find ('\n');
466          if (pos != cmt_string::npos) result.erase (pos);
467          pos = result.find ('\r');
468          if (pos != cmt_string::npos) result.erase (pos);
469         
470          if (Cmt::get_debug ())
471            {
472              cout << "   Executing [" << command << "] to expand a macro value =>[" 
473                   << result << "]" << endl;
474            }
475         
476          text.replace_all (pattern, result);
477
478          // The substitution will restart from the same place
479          // allowing for recursive replacements
480          start = begin;
481        }
482      else
483        {
484          Symbol* macro = Symbol::find (symbol_name);
485          if ((macro != 0) && 
486              (macro->type == Symbol::SymbolMacro))
487            {
488                // Macro found
489              cmt_string value = macro->resolve_macro_value ();
490              text.replace_all (pattern, value);
491
492                // The substitution will restart from the same place
493                // allowing for recursive replacements
494              start = begin;
495            }
496          else if ((macro == 0) || 
497                   ((macro->type == Symbol::SymbolSet) || (macro->type == Symbol::SymbolPath)))
498            {
499                // Set found
500                // ensure that the delimiters will match the OS dependent
501                // delimiters.
502             
503              cmt_string pattern_close = marker.pattern;
504             
505              if (pattern_close != CmtSystem::ev_close ())
506                {
507                  cmt_string new_pattern;
508                 
509                  new_pattern = CmtSystem::ev_open ();
510                  new_pattern += symbol_name;
511                  new_pattern += CmtSystem::ev_close ();
512                 
513                  text.replace (pattern, new_pattern);
514                }
515             
516              start = end + 1;
517            }
518          else
519            {
520              start = end + 1;
521            }
522        }
523    }
524}
525
526/**
527
528   This function suppress all OS delimiters ie ${ } or % % and replaces them
529   by the pure macro delimiters $( )
530
531   `xxx` pattern is replaced by the result of the execution of the command
532
533 */
534static void suppress_OS_delimiters (cmt_string& text)
535{
536  cmt_string pattern;
537  cmt_string symbol_name;
538  char end_pattern;
539
540  int start = 0;
541
542  for (;;)
543    {
544      int begin;
545      int end;
546
547      symbol_marker markers[3];
548      int num = 0;
549
550      markers[num].set (text.find (start, "${"), '}', 2); num++;
551      markers[num].set (text.find (start, "`"), '`', 1); num++;
552
553#ifdef WIN32
554      markers[num].set (text.find (start, "%"), '%', 1); num++;
555#endif
556
557      // Find the first of three patterns
558
559      symbol_marker& marker = symbol_marker::get_lowest (markers, num);
560
561      begin = marker.ptr;
562
563      if (begin == cmt_string::npos) break;
564
565      end_pattern = marker.pattern;
566      start = begin + marker.intro;
567
568      end = text.find (start, end_pattern);
569      if (end == cmt_string::npos)
570        {
571          // The pattern is a fake one (no ending!)
572          start++;
573          continue;
574        }
575
576      // This should never happen...
577      if (end < begin) break;
578
579      // Extract the complete pattern
580      text.substr (begin, end - begin + 1, pattern);
581
582      // Then only the macro name
583      text.substr (begin + marker.intro, end - begin - marker.intro, symbol_name);
584
585      if (text[begin] == '`')
586        {
587          cmt_string command = symbol_name;
588          resolve_value (command);
589
590            // The macro value is a shell command that first needs
591            // to be applied. The output of the command is substituted
592
593          cmt_string result;
594
595          Symbol::all_set ();
596          CmtSystem::execute (command, result);
597         
598          int pos;
599          pos = result.find ('\n');
600          if (pos != cmt_string::npos) result.erase (pos);
601          pos = result.find ('\r');
602          if (pos != cmt_string::npos) result.erase (pos);
603         
604          if (Cmt::get_debug ())
605            {
606              cout << "   Executing [" << command << "] to expand a macro value =>[" 
607                   << result << "]" << endl;
608            }
609         
610          text.replace_all (pattern, result);
611
612          // The substitution will restart from the same place
613          // allowing for recursive replacements
614          start = begin;
615        }
616      else
617        {
618          cmt_string new_pattern;
619
620          new_pattern = "$(";
621          new_pattern += symbol_name;
622          new_pattern += ")";
623
624          text.replace (pattern, new_pattern);
625
626          start = begin;
627        }
628    }
629}
630
631//-------------------------------------------------------------
632/*                                                          */
633/*  Operations on SymbolValues                              */
634/*                                                          */
635//-------------------------------------------------------------
636
637//-------------------------------------------------------------
638SymbolValue::SymbolValue ()
639{
640  tag = 0;
641}
642
643//-------------------------------------------------------------
644SymbolValue::~SymbolValue ()
645{
646  tag = 0;
647}
648
649//-------------------------------------------------------------
650/*                                                          */
651/*  Operations on Symbols                                   */
652/*                                                          */
653//-------------------------------------------------------------
654
655//-------------------------------------------------------------
656Symbol* Symbol::create (const cmt_string& name,
657                        CommandType command,
658                        Use* use)
659{
660  static SetBuilder Set;
661  static PathBuilder Path;
662  static MacroBuilder Macro;
663  static ScriptBuilder Script;
664  static ActionBuilder Action;
665
666  static SymbolVector& Symbols = symbols ();
667  static SymbolMap& SymbolMap = symbol_map ();
668
669  SymbolType type = SymbolUndefined;
670
671  switch (command)
672    {
673    case CommandSet:
674    case CommandSetAppend:
675    case CommandSetPrepend:
676    case CommandSetRemove:
677    case CommandSetRemoveRegexp:
678      type = SymbolSet;
679      break;
680    case CommandPath:
681    case CommandPathAppend:
682    case CommandPathPrepend:
683    case CommandPathRemove:
684    case CommandPathRemoveRegexp:
685      type = SymbolPath;
686      break;
687    case CommandMacro:
688    case CommandMacroAppend:
689    case CommandMacroPrepend:
690    case CommandMacroRemove:
691    case CommandMacroRemoveRegexp:
692    case CommandMacroRemoveAll:
693    case CommandMacroRemoveAllRegexp:
694      type = SymbolMacro;
695      break;
696    case CommandAction:
697      type = SymbolAction;
698      break;
699    case CommandAlias:
700      type = SymbolAlias;
701      break;
702    case CommandSetupScript:
703      type = SymbolSetupScript;
704      break;
705    case CommandCleanupScript:
706      type = SymbolCleanupScript;
707      break;
708    }
709
710  {
711    Symbol* symbol;
712
713    symbol = find (name);
714    if (symbol != 0) 
715      {
716        if (symbol->type != type)
717          {
718            ActionType action = Cmt::get_action ();
719
720            if ((!Cmt::get_quiet ()) &&
721                (action != action_build_constituents_config) &&
722                (action != action_build_constituent_config))
723              {
724                cmt_string s1;
725                cmt_string s2;
726
727                switch (symbol->type)
728                  {
729                  case SymbolSet:
730                    s1 = "set";
731                    break;
732                  case SymbolPath:
733                    s1 = "path";
734                    break;
735                  case SymbolMacro:
736                    s1 = "macro";
737                    break;
738                  case SymbolSetupScript:
739                    s1 = "setup_script";
740                    break;
741                  case SymbolCleanupScript:
742                    s1 = "cleanup_script";
743                    break;
744                  case SymbolAction:
745                    s1 = "action";
746                    break;
747                  case SymbolAlias:
748                    s1 = "alias";
749                    break;
750                  } 
751
752                switch (type)
753                  {
754                  case SymbolSet:
755                    s2 = "set";   
756                    break;
757                  case SymbolPath:
758                    s2 = "path";   
759                    break;
760                  case SymbolMacro:
761                    s2 = "macro";   
762                    break;
763                  case SymbolSetupScript:
764                    s2 = "setup_script"; 
765                    break;
766                  case SymbolCleanupScript:
767                    s2 = "cleanup_script"; 
768                    break;
769                  case SymbolAction:
770                    s2 = "action";
771                    break;
772                  case SymbolAlias:
773                    s2 = "alias";
774                    break;
775                  } 
776
777                CmtMessage::warning ("Symbol " + name
778                                     + " inconsistently redeclared from " + s1
779                                     + " to " + s2
780                                     + ( (use != 0) ?
781                                         " in package " + use->get_package_name () :
782                                         "" )
783                                     );
784                /*
785                cerr << "#CMT> Warning: Symbol " << name
786                     << " inconsistently redeclared from " << s1 << " to " << s2;
787                if (use != 0) cerr << " in package " << use->get_package_name ();
788                cerr << endl;
789                */
790              }
791          }
792
793        return (symbol);
794      }
795  }
796
797  Symbol& symbol = Symbols.add ();
798  SymbolMap.add (name, symbol);
799
800  symbol.name  = name;
801  symbol.scope = use->get_current_scope ();
802  symbol.type  = type;
803
804  symbol.value_lists.clear ();
805
806  switch (type)
807    {
808    case SymbolSet:
809      symbol.builder = &Set;
810      break;
811    case SymbolPath:
812      symbol.builder = &Path;
813      break;
814    case SymbolAlias:
815      symbol.builder = &Set;
816      break;
817    case SymbolMacro:
818      symbol.builder = &Macro;
819      break;
820    case SymbolSetupScript:
821    case SymbolCleanupScript:
822      symbol.builder = &Script;
823      break;
824    case SymbolAction:
825      symbol.builder = &Action;
826      break;
827    }
828
829  symbol.selected_value = -1;
830  symbol.printed = false;
831
832  return (&symbol);
833}
834
835//-------------------------------------------------------------
836Symbol* Symbol::find (const cmt_string& name)
837{
838  static SymbolMap& SymbolMap = symbol_map ();
839
840  Symbol* result = 0;
841
842  result = SymbolMap.find (name);
843
844  return (result);
845}
846
847//-------------------------------------------------------------
848int Symbol::symbol_number ()
849{
850  static SymbolVector& Symbols = symbols ();
851
852  return (Symbols.size ());
853}
854
855//-------------------------------------------------------------
856Symbol::SymbolVector& Symbol::symbols ()
857{
858  static Database& db = Database::instance ();
859  static SymbolVector& Symbols = db.symbols ();
860
861  return (Symbols);
862}
863
864//-------------------------------------------------------------
865Symbol::SymbolMap& Symbol::symbol_map ()
866{
867  static Database& db = Database::instance ();
868  static SymbolMap& SymbolMap = db.symbol_map ();
869
870  return (SymbolMap);
871}
872
873/**
874   Filter out faulty items of a path-like symbol value
875 */
876void Symbol::filter_path_value (const cmt_string& name, cmt_string& text)
877{
878
879  CmtSystem::cmt_string_vector paths;
880  CmtSystem::cmt_string_vector normalyzed_paths;   
881                 
882  CmtSystem::split (text, CmtSystem::path_separator (), paths);
883                 
884  for (int j = 0; j < paths.size (); ++j)
885    {
886      cmt_string& t = paths[j];
887
888      if (!CmtSystem::test_directory (t))
889          {
890            if (CmtMessage::active (Verbose))
891              CmtMessage::warning ("Non-existent directory " + t + " in " + name);
892            /*
893              if (Cmt::get_warnings ())
894              {
895                  cerr << "#CMT> Warning: Wrong path item " << t << " in " << name << endl;
896              }
897            */
898          }
899           
900      int exist = 0; 
901      for (int i = 0; i < j; ++i)
902      {
903          cmt_string& u = paths[i];
904          if (u == t)
905             exist = 1;
906      }           
907      if (exist==0)
908      {
909          cmt_string& s = normalyzed_paths.add ();
910          s = t;
911      }             
912    }
913   
914  Cmt::vector_to_string (normalyzed_paths, CmtSystem::path_separator (), text);
915
916  for (;;)
917    {
918      int sz = text.size ();
919
920      if (sz == 0) break;
921
922      if ((text[0] == ';') || (text[0] == ':'))
923        {
924          text.erase (0, 1);
925        }
926      else if ((text[sz-1] == ';') || (text[sz-1] == ':'))
927        {
928          text.erase (sz-1, 1);
929        }
930      else
931        {
932          break;
933        }
934    }
935
936  text.replace_all ("::", ":");
937  text.replace_all (";;", ";");
938}
939
940
941//-------------------------------------------------------------
942Symbol& Symbol::symbol (int index)
943{
944  static SymbolVector& Symbols = symbols ();
945
946  return (Symbols[index]);
947}
948
949//-------------------------------------------------------------
950void Symbol::action (const CmtSystem::cmt_string_vector& words,
951                     CommandType command_type,
952                     Use* use)
953{
954  int number;
955  Symbol* symbol;
956  Tag* tag;
957
958  if (words.size () < 1) return;
959  cmt_string name = words[1];
960
961  if ((command_type == CommandSetupScript) ||
962      (command_type == CommandCleanupScript))
963    {
964      cmt_string full_name;
965
966      Symbol::expand (name);
967
968      if (name != "")
969        {
970          if (CmtSystem::absolute_path (name)) 
971            {
972              full_name = name;
973            }
974          else
975            {
976              if (use->get_strategy ("SetupRoot") &&
977                  use->get_package_name () != "cmt_standalone")
978                {
979#ifdef WIN32
980              full_name = "%";
981#else
982              full_name = "${";
983#endif
984              full_name += use->prefix;
985              full_name += "ROOT";
986#ifdef WIN32
987              full_name += "%";
988#else
989              full_name += "}";
990#endif
991                }
992              else
993                {
994              full_name = use->get_full_path ();
995                }
996              if (use->style == cmt_style)
997                {
998                  full_name += CmtSystem::file_separator ();
999                  full_name += "cmt";
1000                }
1001              else if (use->style == mgr_style)
1002                {
1003                  full_name += CmtSystem::file_separator ();
1004                  full_name += "mgr";
1005                }
1006              //              else if (use->style == no_version_style) full_name += "cmt";
1007              //              else full_name += "mgr";
1008              full_name += CmtSystem::file_separator ();
1009              full_name += name;
1010            }
1011
1012          symbol = create (full_name, command_type, use);
1013          symbol->add_value_to_list (command_type, use,
1014                                     Tag::get_default (), full_name);
1015        }
1016    }
1017  else
1018    {
1019      if (words.size () < 2) return;
1020      const cmt_string& default_value = words[2];
1021
1022      Cmt::reset_all_sets_done ();
1023
1024      if (Cmt::get_debug ())
1025        {
1026          cout << "Symbol::action> name:" << name
1027               << " access:" << Cmt::get_current_access () 
1028               << " scope:" << use->get_current_scope () << endl;
1029        }
1030
1031      if (Cmt::get_current_access () == UserMode)
1032        {
1033          /*
1034            The following statements mean that some symbols are
1035            always private.
1036            This list is hardcoded. This should be replaced by a
1037            database of "private" symbols.
1038           */
1039          if (name == "constituents") return;
1040          if (name == "constituentscclean") return;
1041
1042          if (use->get_current_scope () == ScopePrivate) return;
1043        }
1044
1045      symbol = create (name, command_type, use);
1046
1047      /*
1048        Parse the default value.
1049      */
1050     
1051      symbol->add_value_to_list (command_type, use,
1052                                 Tag::get_default (), default_value);
1053     
1054      /*
1055        Then parse all specific values
1056       
1057        <tag> <value>
1058        ...
1059      */
1060     
1061      number = 3;
1062      while (number < (words.size () - 1))
1063        {
1064          cmt_string tag_name = words[number];
1065          const cmt_string& value = words[number + 1];
1066
1067          expand (tag_name);
1068
1069          if (Cmt::get_debug ())
1070            {
1071              cout << "Symbol::action> tag_name=" << tag_name << endl;
1072            }
1073
1074          tag = Tag::find (tag_name);
1075          if (tag == 0)
1076            {
1077              tag = Tag::add (tag_name, PriorityUserTag, "use", use);
1078            }
1079
1080          symbol->add_value_to_list (command_type, use, tag, value);
1081         
1082          number += 2;
1083        }
1084
1085      if (name == "CMTPATH")
1086        {
1087          Cmt::configure_cmt_path (use);
1088        }
1089      else if (name == "CMTPROJECTPATH")
1090        {
1091          Cmt::configure_cmt_path (use);
1092        }
1093      else if (name == "CMTSITE")
1094        {
1095          Cmt::configure_site_tag (use);
1096        }
1097      else if (name == "CMTCONFIG")
1098        {
1099            //cerr << "redefining CMTCONFIG" << endl;
1100          Cmt::configure_tags (use);
1101        }
1102      else if (name == "CMTHOME")
1103        {
1104          Cmt::configure_home (use);
1105        }
1106      else if (name == "CMTUSERCONTEXT")
1107        {
1108          Cmt::configure_user_context (use);
1109        }
1110      else if (name.find ("_native_version") != cmt_string::npos)
1111        {
1112          cmt_string n = use->get_package_name ();
1113          n += "_native_version";
1114
1115          if (name == n)
1116            {
1117              use->set_native_version (true);
1118            }
1119        }
1120    }
1121}
1122
1123//-------------------------------------------------------------
1124int Symbol::is_selected (const cmt_string& name)
1125{
1126  Symbol* symbol;
1127  int number;
1128  int value_number;
1129
1130  symbol = find (name);
1131  if (symbol == 0) return (0);
1132
1133  if (symbol->value_lists.size () == 0) return (0);
1134
1135  for (number = 0;
1136       number < symbol->value_lists.size ();
1137       number++)
1138    {
1139      const SymbolValueList& value_list = symbol->value_lists[number];
1140
1141      if (value_list.discarded) continue;
1142
1143      if ((value_list.command_type == CommandMacro) ||
1144          (value_list.command_type == CommandSet) ||
1145          (value_list.command_type == CommandSetAppend) ||
1146          (value_list.command_type == CommandSetPrepend) ||
1147          (value_list.command_type == CommandSetRemove) ||
1148          (value_list.command_type == CommandSetRemoveRegexp) ||
1149          (value_list.command_type == CommandAlias) ||
1150          (value_list.command_type == CommandAction))
1151        {
1152          for (value_number = 0;
1153               value_number < value_list.values.size ();
1154               value_number++)
1155            {
1156              Tag* tag;
1157
1158              SymbolValue& value = value_list.values[value_number];
1159
1160              tag = value.tag;
1161              if ((tag == 0) ||
1162                  (tag == Tag::get_default ()) ||
1163                  (tag->is_selected () != 0))
1164                {
1165                  return (1);
1166                }
1167            }
1168        }
1169    }
1170
1171  return (0);
1172}
1173
1174//-------------------------------------------------------------
1175Symbol::Symbol ()
1176{
1177  name = "";
1178}
1179
1180//-------------------------------------------------------------
1181Symbol::~Symbol ()
1182{
1183}
1184
1185/**
1186 *  Characterizes if a value is provided as the reference to itself.
1187 */
1188bool Symbol::value_is_reflexive (const cmt_string& text) const
1189{
1190  bool result = false;
1191  int text_length = text.size ();
1192
1193  if (text_length == (name.size () + 3))
1194    {
1195      static cmt_string temp;
1196             
1197      if (text[0] == '$')
1198        {
1199          if (text[1] == '(')
1200            {
1201              temp = "$(";
1202              temp += name;
1203              temp += ")";
1204             
1205              if (text == temp)
1206                {
1207                  result = true;
1208                }
1209            }
1210          else if (text[1] == '{')
1211            {
1212              temp = "${";
1213              temp += name;
1214              temp += "}";
1215             
1216              if (text == temp)
1217                {
1218                  result = true;
1219                }
1220            }
1221        }
1222    }
1223  else if (text_length == (name.size () + 2))
1224    {
1225      static cmt_string temp;
1226
1227      temp = "%";
1228      temp += name;
1229      temp += "%";
1230
1231      if (text == temp)
1232        {
1233          result = true;
1234        }
1235    }
1236
1237  return (result);
1238}
1239
1240//-------------------------------------------------------------
1241void Symbol::add_value_to_list (CommandType command_type,
1242                                Use* use,
1243                                Tag* tag,
1244                                const cmt_string& text)
1245{
1246  SymbolValueList* value_list = 0;
1247  bool is_reflexive = false;
1248
1249    //
1250    // First pickup the most recent value_list
1251    //
1252  if (value_lists.size () > 0) value_list = &(value_lists.back ());
1253
1254    //
1255    //  Create a new value list is we switch to another use or
1256    //  if we switch to a new command_type (eg. switching from
1257    //  macro to macro_append).
1258    //
1259  if ((value_list == 0) ||
1260      (use != value_list->use) ||
1261      (command_type != value_list->command_type) ||
1262      (tag == Tag::get_default ()))
1263    {
1264      value_list = &(value_lists.add ());
1265      value_list->use = use;
1266      value_list->command_type = command_type;
1267      value_list->values.clear ();
1268      value_list->discarded = false;
1269      value_list->is_reflexive = false;
1270    }
1271
1272/*
1273  else
1274    {
1275        value_list = &(value_lists[value_lists.size () - 1]);
1276    }
1277*/
1278
1279  is_reflexive = value_list->is_reflexive;
1280
1281    //
1282    //  If the command_type is command_macro or command_set,
1283    // this is considered as a full re-set of this symbol
1284    //   In this case, we have to discard all previous values
1285    //
1286    //  However, we'd like to exclude from this logic the cases where
1287    //  the value is **exactly*
1288    //
1289    //     $(<symbol>)
1290    //     ${<symbol>}
1291    //     %<symbol>%
1292    //
1293    //   which would then mean that we do not reset the value but rather
1294    //  override it.
1295    //
1296
1297    //
1298    // Inside this value_list, we add this new tag-value pair.
1299    //
1300
1301  if ((command_type == CommandMacro) ||
1302      (command_type == CommandSet) ||
1303      (command_type == CommandPath) ||
1304      (command_type == CommandAction))
1305    {
1306        //
1307        // Check whether we have to hide previous settings by this new definition
1308        //  (of course this is only useful if there WERE previous settings)
1309        //
1310      if ((value_lists.size () >= 1) && (!is_reflexive))
1311        {
1312          if (value_is_reflexive (text))
1313            {
1314              value_list->is_reflexive = true;
1315              is_reflexive = true;
1316            }
1317          else
1318            {
1319              //cerr << "...discarding old values for symbol " << name << " text=[" << text << "]" << endl;
1320                 
1321              for (int i = 0; i < (value_lists.size () - 1); i++)
1322                {
1323                  SymbolValueList& vl = value_lists[i];
1324                 
1325                  if ((vl.use != 0) &&
1326                      (vl.use->discarded))
1327                    {
1328                      //vl.discarded = true;
1329                    }
1330                }
1331            }
1332        }
1333    }
1334
1335  SymbolValue& value = value_list->values.add ();
1336
1337  value.tag = tag;
1338  value.text = text;
1339  value.selected = 0;
1340}
1341
1342/**
1343   Compute the current value of all environment variables and set them to the
1344   shell
1345 */
1346void Symbol::all_set ()
1347{
1348  //cerr << "all_set" << endl;
1349
1350  static bool running = false;
1351
1352
1353  if (Cmt::get_debug ())
1354    {
1355      cout << "\nSymbol::all_set> done=" << running << endl;
1356    }
1357
1358  if (Cmt::get_all_sets_done ()) return;
1359
1360  if (running) return;
1361
1362  running = true;
1363  Cmt::set_all_sets_done ();
1364
1365  static SymbolVector& Symbols = symbols ();
1366  Use::UsePtrVector& Uses = Use::get_ordered_uses ();
1367
1368  int number;
1369
1370  if (Symbols.size () == 0) 
1371    {
1372      running = false;
1373      return;
1374    }
1375
1376  cmt_string value;
1377
1378  for (number = 0; number < Symbol::symbol_number (); number++)
1379    {
1380      Symbol& symbol = Symbol::symbol (number);
1381
1382      if (symbol.type != SymbolSet) continue;
1383
1384      value = symbol.build_macro_value ();
1385      if (value != "")
1386        {
1387          Symbol::expand (value);
1388
1389          CmtSystem::putenv (symbol.name, value);
1390        }
1391    }
1392
1393  cmt_string cmtconfig = CmtSystem::get_cmt_config ();
1394
1395  if (Uses.size () > 0)
1396    {
1397      int number;
1398
1399      for (number = 0; number < Uses.size (); number++)
1400        {
1401          Use& use = *(Uses[number]);
1402
1403          if (use.discarded) continue;
1404
1405          if (use.get_package_name () == "cmt_standalone") continue;
1406
1407          if (use.get_strategy ("SetupConfig"))
1408            {
1409              cmt_string temp;
1410
1411              temp = use.prefix;
1412              temp += "CONFIG";
1413
1414              CmtSystem::putenv (temp, cmtconfig);
1415            }
1416
1417          if (use.get_strategy ("SetupRoot"))
1418            {
1419              cmt_string temp;
1420
1421              temp = use.prefix;
1422              temp += "ROOT";
1423
1424              CmtSystem::putenv (temp, use.get_full_path ());
1425            }
1426        }
1427    }
1428
1429  {
1430    Use& use = Use::current ();
1431
1432    if (use.get_package_name () != "cmt_standalone")
1433      {
1434        if (use.get_strategy ("SetupConfig"))
1435          {
1436            cmt_string temp;
1437
1438            temp = use.prefix;
1439            temp += "CONFIG";
1440
1441            CmtSystem::putenv (temp, cmtconfig);
1442          }
1443       
1444        if (use.get_strategy ("SetupRoot"))
1445          {
1446            cmt_string temp;
1447
1448            temp = use.prefix;
1449            temp += "ROOT";
1450
1451            CmtSystem::putenv (temp, use.get_full_path ());
1452          }
1453      }
1454  }
1455
1456  for (number = 0; number < Symbol::symbol_number (); number++)
1457    {
1458      Symbol& symbol = Symbol::symbol (number);
1459
1460      if ((symbol.type != SymbolPath)) continue;
1461
1462      value = symbol.build_macro_value ();
1463      if (value != "")
1464        {
1465          Symbol::expand (value);
1466          filter_path_value (symbol.name, value);
1467
1468#ifdef WIN32
1469          value.replace_all ("/", "\\");
1470#endif
1471
1472          if (Cmt::get_debug ())
1473            {
1474              cerr << "Symbol::all_set-2> " << symbol.name << " = " << value << endl;
1475            }
1476
1477          CmtSystem::putenv (symbol.name, value);
1478        }
1479    }
1480
1481  running = false;
1482}
1483
1484//-------------------------------------------------------------
1485void Symbol::all_print (PrintMode mode)
1486{
1487  static SymbolVector& Symbols = symbols ();
1488
1489  int number;
1490
1491  if (Symbols.size () == 0) return;
1492
1493  switch (mode)
1494    {
1495    case Requirements :
1496      for (number = 0; number < Symbol::symbol_number (); number++)
1497        {
1498          Symbol& symbol = Symbol::symbol (number);
1499
1500          if ((symbol.type == SymbolSet) ||
1501              (symbol.type == SymbolAlias) ||
1502              (symbol.type == SymbolSetupScript) ||
1503              (symbol.type == SymbolPath) ||
1504              (symbol.type == SymbolMacro))
1505            {
1506              if (symbol.print_macro (mode))
1507                {
1508                  //              cout << endl;
1509                }
1510            }
1511        }
1512      break;
1513    default :
1514      for (number = 0; number < Symbol::symbol_number (); number++)
1515        {
1516          Symbol& symbol = Symbol::symbol (number);
1517         
1518          if ((symbol.type == SymbolSet) ||
1519              (symbol.type == SymbolAlias) ||
1520              (symbol.type == SymbolSetupScript) ||
1521              (symbol.type == SymbolPath))
1522            {
1523              if (symbol.print (mode))
1524                {
1525                  if (mode == Bat)
1526                    {
1527                      cout << endl;
1528                    }
1529                  else
1530                    {
1531                      cout << endl;
1532                    }
1533                }
1534            }
1535        }
1536      break;
1537    }
1538}
1539
1540//-------------------------------------------------------------
1541void Symbol::check_all_paths ()
1542{
1543  static SymbolVector& Symbols = symbols ();
1544
1545  int number;
1546
1547  if (Symbols.size () == 0) return;
1548
1549  for (number = 0; number < Symbol::symbol_number (); number++)
1550    {
1551      Symbol& symbol = Symbol::symbol (number);
1552
1553      if (symbol.type == SymbolPath)
1554        {
1555          cmt_string temp;
1556
1557          temp = symbol.build_macro_value ();
1558          expand (temp);
1559
1560          Symbol::filter_path_value (symbol.name, temp);
1561        }
1562    }
1563}
1564
1565//-------------------------------------------------------------
1566void Symbol::all_print_clean (PrintMode mode)
1567{
1568  static SymbolVector& Symbols = symbols ();
1569
1570  int number;
1571
1572  if (Symbols.size () == 0) return;
1573
1574  for (number = Symbols.size () - 1; number >= 0; number--)
1575    {
1576      Symbol& symbol = Symbols[number];
1577
1578      if ((symbol.type == SymbolSet) ||
1579          (symbol.type == SymbolAlias) ||
1580          (symbol.type == SymbolCleanupScript))
1581        {
1582          if (symbol.print_clean (mode))
1583            {
1584              cout << endl;
1585            }
1586        }
1587    }
1588
1589  for (number = Symbols.size () - 1; number >= 0; number--)
1590    {
1591      Symbol& symbol = Symbols[number];
1592
1593      if ((symbol.type != SymbolPath)) continue;
1594
1595      if (symbol.print_clean (mode))
1596        {
1597          cout << endl;
1598        }
1599    }
1600}
1601
1602//-------------------------------------------------------------
1603int Symbol::print_clean (PrintMode mode)
1604{
1605  int result = 0;
1606  static cmt_string temp;
1607
1608  if (name == "CMTCONFIG") return (0);
1609
1610  switch (type)
1611    {
1612    case SymbolSet :
1613      switch (mode)
1614        {
1615        case Csh :
1616          cout << "unsetenv " << name;
1617          result = 1;
1618          break;
1619        case Sh :
1620          cout << "unset " << name;
1621          result = 1;
1622          break;
1623        case Bat :
1624          cout << "set " << name << "=";
1625          result = 1;
1626          break;
1627        }
1628      break;
1629    case SymbolAlias :
1630      switch (mode)
1631        {
1632          case Csh :
1633            cout << "unalias " << name;
1634            result = 1;
1635            break;
1636          case Sh :
1637            cout << "unset " << name;
1638            result = 1;
1639            break;
1640        }
1641      break;
1642    case SymbolPath :
1643      temp = clean_macro_value ();
1644      switch (mode)
1645        {
1646        case Csh :
1647          if (temp == "")
1648            {
1649              cout << "unsetenv " << name;
1650            }
1651          else
1652            {
1653              cout << "setenv " << name << " " << temp;
1654            }
1655          result = 1;
1656          break;
1657        case Sh :
1658          cout << name << "=" << temp << "; export " << name;
1659          result = 1;
1660          break;
1661        case Bat :
1662          cout << "set " << name << "=" << temp;
1663          result = 1;
1664          break;
1665        }
1666      break;
1667    case SymbolCleanupScript :
1668      switch (mode)
1669        {
1670        case Csh :
1671          cout << "if ( -f " << name << ".csh ) then" << endl;
1672          cout << "  source " << name << ".csh" << endl;
1673          cout <<
1674            "if ( $status != 0 ) then\n"
1675            "    set cmtcleanupstatus=1\n"
1676            "endif\n";
1677          cout << "endif" << endl;
1678          result = 1;
1679          break;
1680        case Sh :
1681          cout << "if test -f " << name << ".sh; then" << endl;
1682          cout << "  . " << name << ".sh" << endl;
1683          cout <<
1684            "if test $? != 0; then\n"
1685            "    cmtcleanupstatus=1\n"
1686            "fi\n";
1687          cout << "fi" << endl;
1688          result = 1;
1689          break;
1690        case Bat :
1691          cout << "call " << name;
1692          result = 1;
1693          break;
1694        }
1695      break;
1696    }
1697
1698  return (result);
1699}
1700
1701//-------------------------------------------------------------
1702int Symbol::print (PrintMode mode)
1703{
1704  int result = 0;
1705  cmt_string temp;
1706
1707  temp = build_macro_value ();
1708
1709  bool empty = (temp.size () == 0) ? true : false;
1710
1711  if (type == SymbolPath)
1712    {
1713      expand (temp);
1714      Symbol::filter_path_value (name, temp);
1715    }
1716
1717  switch (type)
1718    {
1719      case SymbolSet :
1720      case SymbolPath :
1721        switch (mode)
1722          {
1723            case Csh :
1724              if (empty) cout << "unsetenv " << name;
1725              else cout << "setenv " << name << " \"" << temp << "\"";
1726
1727              result = 1;
1728              break;
1729            case Sh :
1730              if (empty) cout << "unset " << name;
1731              else cout << name << "=\"" << temp << "\"; export " << name;
1732
1733              result = 1;
1734              break;
1735            case Bat :
1736              temp.replace_all ("/", "\\");
1737              cout << "set " << name << "=" << temp;
1738              result = 1;
1739              break;
1740          }
1741        break;
1742      case SymbolAlias :
1743        switch (mode)
1744          {
1745            case Csh :
1746              cout << "alias " << name <<
1747                  " \"" << temp << "\"";
1748              result = 1;
1749              break;
1750            case Sh :
1751              cout << "alias " << name <<
1752                  "=\"" << temp << "\"";
1753              result = 1;
1754              break;
1755            case Bat :
1756              cout << "set " << name <<
1757                  "=" << temp;
1758              result = 1;
1759              break;
1760          }
1761        break;
1762      default :
1763        break;
1764    }
1765
1766  if (temp != "")
1767    {
1768      switch (type)
1769        {
1770          case SymbolSetupScript :
1771            switch (mode)
1772              {
1773                case Csh :
1774                  cout << "if ( -f " << name << ".csh ) then" << endl;
1775                  cout << "  source " << name << ".csh" << endl;
1776                  cout <<
1777                    "if ( $status != 0 ) then\n"
1778                    "    set cmtsetupstatus=1\n"
1779                    "endif\n";
1780                  cout << "endif" << endl;
1781                  result = 1;
1782                  break;
1783                case Sh :
1784                  cout << "if test -f " << name << ".sh; then" << endl;
1785                  cout << "  . " << name << ".sh" << endl;
1786                  cout <<
1787                    "if test $? != 0; then\n"
1788                    "    cmtsetupstatus=1\n"
1789                    "fi\n";
1790                  cout << "fi" << endl;
1791                  result = 1;
1792                  break;
1793                case Bat :
1794                  cout << "call " << name;
1795                  result = 1;
1796                  break;
1797              }
1798            break;
1799          default:
1800            break;
1801        }
1802    }
1803
1804  return (result);
1805}
1806
1807//-------------------------------------------------------------
1808cmt_string Symbol::build_macro_value (bool display_it) const
1809{
1810  cmt_string temp;
1811
1812  if (display_it)
1813    {
1814      temp = builder->build_and_display (*this);
1815    }
1816  else
1817    {
1818      temp = builder->build (*this);
1819    }
1820
1821  return (temp);
1822}
1823
1824//-------------------------------------------------------------
1825int Symbol::print_macro (PrintMode mode, ostream& out) const
1826{
1827  if (mode != Requirements || printed) return 0;
1828  return builder->print (*this, out);
1829}
1830
1831//-------------------------------------------------------------
1832cmt_string Symbol::clean_macro_value () const
1833{
1834  cmt_string temp;
1835
1836  temp = builder->clean (*this);
1837
1838  return (temp);
1839}
1840
1841/**
1842
1843   Attempt to substitute into the symbol value all occurrences of
1844
1845      ${xxx}
1846      $(xxx)
1847      `xxx`
1848      %xxx%    [on Windows only]
1849
1850      by the appropriate value:
1851
1852        for `xxx` :
1853
1854            xxx is considered as a shell command. Value is the result of its execution
1855
1856        for other patterns:
1857
1858            if xxx is a symbol name, its value is substituted to the pattern
1859            otherwise, xxx is tried as an environment variable
1860
1861
1862     ===> In all cases, the pattern is filtered away.
1863
1864  */
1865cmt_string Symbol::resolve_macro_value (const cmt_string& tag_name)
1866{
1867  cmt_string temp = builder->build (*this, tag_name);
1868
1869  resolve_value (temp);
1870
1871  return (temp);
1872}
1873
1874//-------------------------------------------------------------
1875void Symbol::show_macro (PrintMode mode, ostream& out)
1876//void Symbol::show_macro (PrintMode mode)
1877{
1878  if (Cmt::get_debug ())
1879    {
1880      cout << "Symbol::show_macro> " << name << endl;
1881    }
1882
1883  ActionType action = Cmt::get_action ();
1884
1885  cmt_string value = build_macro_value (true);
1886
1887  if ((!Cmt::get_quiet ()) &&
1888      (action != action_build_tag_makefile) &&
1889      (action != action_build_constituents_config) &&
1890      (action != action_build_constituent_config) &&
1891      (action != action_show_macros) &&
1892      (action != action_show_actions) &&
1893      (action != action_show_sets))
1894    {
1895      out << "#" << endl;
1896      out << "# Selection : " << endl;
1897    }
1898
1899  if (value.size () > 0)
1900    {
1901      if ((action == action_show_macro) ||
1902          (action == action_show_macros) ||
1903          (action == action_show_sets) ||
1904          (action == action_show_set) ||
1905          (action == action_show_actions) ||
1906          (action == action_show_action) ||
1907          (action == action_build_tag_makefile) ||
1908          (action == action_build_constituents_config) ||
1909          (action == action_build_constituent_config) ||
1910          (action == action_load) ||
1911          (!Cmt::get_quiet ()))
1912        {
1913          if (mode == Make)
1914            {
1915              out << name << "=";
1916            }
1917          else
1918            {
1919              out << name << "='";
1920            }
1921        }
1922
1923      if ((action == action_show_macro_value) ||
1924          (action == action_show_set_value) ||
1925          (action == action_show_action_value))
1926        {
1927          expand (value);
1928        }
1929      else if (action == action_build_tag_makefile ||
1930               action == action_build_constituents_config ||
1931               action == action_build_constituent_config)
1932        {
1933            /*
1934               Unfortunately, the %xxx% pattern has to be kept on Unix. Therefore
1935               we cannot force all patterns to become $(xxx)
1936
1937               This was useful on Windows so as to only keep $(xxx)
1938             */
1939#ifdef WIN32
1940          suppress_OS_delimiters (value);
1941#endif
1942        }
1943
1944      out << value;
1945
1946      if ((action == action_show_macro) ||
1947          (action == action_show_macros) ||
1948          (action == action_show_sets) ||
1949          (action == action_show_set) ||
1950          (action == action_show_actions) ||
1951          (action == action_show_action) ||
1952          (action == action_build_tag_makefile) ||
1953          (action == action_build_constituents_config) ||
1954          (action == action_build_constituent_config) ||
1955          (action == action_load) ||
1956          (!Cmt::get_quiet ()))
1957        {
1958          if (mode != Make)
1959            {
1960              out << "'";
1961            }
1962#ifdef WIN32
1963          else
1964            {
1965              out << " ";
1966            }
1967#endif
1968        }
1969
1970      out << endl;
1971    }
1972}
1973
1974//-------------------------------------------------------------
1975void Symbol::clear_all ()
1976{
1977  static SymbolVector& Symbols = symbols ();
1978  static SymbolMap& SymbolMap = symbol_map ();
1979
1980  SymbolMap.clear ();
1981  Symbols.clear ();
1982}
1983
1984//-------------------------------------------------------------
1985void Symbol::expand (cmt_string& text)
1986{
1987  static cmt_regexp reg ("[$%`]");
1988
1989  if (!reg.match (text)) return;
1990
1991  resolve_value (text);
1992}
1993
1994//-------------------------------------------------------------
1995ValueBuilder::ValueBuilder ()
1996{
1997  m_display_it = false;
1998}
1999
2000//-------------------------------------------------------------
2001const cmt_string ValueBuilder::build_and_display (const Symbol& symbol)
2002{
2003  cmt_string temp;
2004
2005  m_display_it = true;
2006  temp = build (symbol);
2007  m_display_it = false;
2008
2009  return (temp);
2010}
2011
2012//-------------------------------------------------------------
2013int ValueBuilder::print (const Symbol& symbol, ostream& out)
2014{
2015  int result (0);
2016
2017  bool first_definition = true;
2018 
2019  for (int i = 0; i < symbol.value_lists.size (); i++)
2020    {
2021      const SymbolValueList& value_list = symbol.value_lists[i];
2022
2023      if ((value_list.use != 0) &&
2024          (value_list.use->discarded)) continue;
2025
2026      const int selected = value_list.select_first ();
2027
2028      if (selected < 0) continue;
2029
2030      SymbolValue& value = value_list.values[selected];
2031
2032      result += value_list.print (symbol, value, first_definition);
2033    }
2034
2035  return result;
2036}
2037
2038//-------------------------------------------------------------
2039const cmt_string SetBuilder::build (const Symbol& symbol,
2040                                    const cmt_string& /*tag_name*/)
2041{
2042    // Control of recursivity
2043  static int level = 0;
2044
2045  bool show_it = false;
2046
2047  cmt_string temp;
2048  cmt_string previous_temp;
2049  cmt_string new_value;
2050  static const cmt_string empty;
2051
2052  ActionType action = Cmt::get_action ();
2053
2054  if (action == action_show_set)
2055    {
2056      if (symbol.name == Cmt::get_current_target ())
2057        {
2058             // Should not display on recursive calls
2059          if (level == 0) show_it = m_display_it;
2060        }
2061    }
2062
2063  level++;
2064
2065  temp = "";
2066
2067  bool first_definition = true;
2068
2069  for (int i = 0; i < symbol.value_lists.size (); i++)
2070    {
2071      const SymbolValueList& value_list = symbol.value_lists[i];
2072
2073      if ((value_list.use != 0) &&
2074          (value_list.use->discarded)) continue;
2075
2076      const int selected = value_list.select_first ();
2077
2078      if (selected < 0) continue;
2079
2080      SymbolValue& value = value_list.values[selected];
2081
2082      if (show_it && !Symbol::get_inhibit_display())
2083        {
2084          value_list.show (symbol, value, first_definition);
2085        }
2086     
2087      if (value_list.discarded) continue;
2088
2089        //
2090        // One should accumulate values if it refers to
2091        // itself.
2092        //
2093     
2094      new_value = value.text;
2095     
2096      resolve_value_for_macros (new_value);
2097     
2098      switch (value_list.command_type)
2099        {
2100          case CommandSet :
2101
2102            if (!value_list.is_reflexive || 
2103                !symbol.value_is_reflexive (value.text))
2104              {
2105                resolve_value (new_value, symbol.name, temp);
2106                temp = new_value;
2107              }
2108            else if (temp == "")
2109              {
2110                temp = CmtSystem::getenv (symbol.name);
2111              }
2112
2113            break;
2114          case CommandSetAppend :
2115           
2116            if (new_value != "")
2117              {
2118                temp += new_value;
2119              }
2120           
2121            break;
2122          case CommandSetPrepend :
2123           
2124            if (new_value != "")
2125              {
2126                previous_temp = temp;
2127                temp = new_value;
2128                temp += previous_temp;
2129              }
2130           
2131            break;
2132          case CommandSetRemove :
2133           
2134            if (new_value != "")
2135              {
2136                temp.replace_all (new_value, empty);
2137              }
2138           
2139            break;
2140          case CommandSetRemoveRegexp :
2141           
2142            if (new_value != "")
2143              {
2144                cmt_regexp e (new_value);
2145                cmt_regexp::iterator it;
2146
2147                for (;;)
2148                  {
2149                    it = e.begin (temp);
2150                    if (it == e.end ()) break;
2151
2152                    temp.erase (it._pos, it._length);
2153                  }
2154              }
2155           
2156            break;
2157          case CommandAlias :
2158           
2159            resolve_value (new_value, symbol.name, temp);
2160            temp = new_value;
2161           
2162            break;
2163        }
2164    }
2165
2166  level--;
2167
2168  return (temp);
2169}
2170
2171static bool find_path_entry (const cmt_string& paths, const cmt_string& value)
2172{
2173  static const cmt_string path_separator = CmtSystem::path_separator ();
2174
2175  cmt_string here = CmtSystem::pwd ();
2176  cmt_string rvalue = value;
2177
2178  if (CmtSystem::cd (value))
2179    {
2180      rvalue = CmtSystem::pwd ();
2181    }
2182  else
2183    {
2184      CmtSystem::compress_path (rvalue);
2185    }
2186
2187  CmtSystem::cmt_string_vector items;
2188  CmtSystem::split (paths, path_separator, items);
2189
2190  bool found = false;
2191
2192  for (int i = 0; i < items.size (); i++)
2193    {
2194      const cmt_string& item = items[i];
2195      cmt_string ritem = item;
2196      if (CmtSystem::cd (item))
2197        {
2198          ritem = CmtSystem::pwd ();
2199        }
2200      else
2201        {
2202          CmtSystem::compress_path (ritem);
2203        }
2204
2205      if (ritem == rvalue)
2206        {
2207          found = true;
2208          break;
2209        }
2210    }
2211
2212  CmtSystem::cd (here);
2213  return (found);
2214}
2215
2216//-------------------------------------------------------------
2217const cmt_string PathBuilder::build (const Symbol& symbol,
2218                                     const cmt_string& /*tag_name*/)
2219{
2220    // Control of recursivity
2221  static int level = 0;
2222
2223  bool show_it = false;
2224
2225  cmt_string temp;
2226  cmt_string previous_temp;
2227  cmt_string new_value;
2228  static const cmt_string empty;
2229
2230  static cmt_string path_separator = CmtSystem::path_separator ();
2231
2232  ActionType action = Cmt::get_action ();
2233
2234  if (action == action_show_set)
2235    {
2236      if (symbol.name == Cmt::get_current_target ())
2237        {
2238            // Should not display on recursive calls
2239          if (level == 0) show_it = m_display_it;
2240        }
2241    }
2242
2243  level++;
2244
2245  temp = CmtSystem::getenv (symbol.name);
2246
2247  bool first_definition = true;
2248
2249  for (int i = 0; i < symbol.value_lists.size (); i++)
2250    {
2251      const SymbolValueList& value_list = symbol.value_lists[i];
2252
2253      if ((value_list.use != 0) &&
2254          (value_list.use->discarded)) continue;
2255
2256      const int selected = value_list.select_first ();
2257     
2258      if (selected < 0) continue;
2259 
2260      SymbolValue& value = value_list.values[selected];
2261         
2262      if (show_it && !Symbol::get_inhibit_display())
2263        {
2264          value_list.show (symbol, value, first_definition);
2265        }
2266         
2267      if (value_list.discarded) continue;
2268
2269      new_value = value.text;
2270         
2271          //resolve_value (new_value);
2272      resolve_value_for_macros (new_value);
2273         
2274      switch (value_list.command_type)
2275        {
2276          case CommandPath :
2277           
2278            if (!value_list.is_reflexive || 
2279                !symbol.value_is_reflexive (value.text))
2280              {
2281                resolve_value (new_value, symbol.name, temp);
2282                temp = new_value;
2283              }
2284
2285            break;
2286          case CommandPathAppend :
2287             
2288            if (new_value != "")
2289              {
2290                if (!find_path_entry (temp, new_value))
2291                  {
2292                    if (temp != "") temp += path_separator;
2293                     
2294                    temp += new_value;
2295                  }
2296              }
2297                 
2298            break;
2299          case CommandPathPrepend :
2300             
2301            if (new_value != "")
2302              {
2303                if (!find_path_entry (temp, new_value))
2304                  {
2305                    previous_temp = temp;
2306                    temp = new_value;
2307                    if (previous_temp != "") temp += path_separator;
2308                    temp += previous_temp;
2309                  }
2310              }
2311                 
2312            break;
2313          case CommandPathRemove :
2314             
2315            if (new_value != "")
2316              {
2317                CmtSystem::cmt_string_vector paths;
2318                 
2319                CmtSystem::split (temp, path_separator, paths);
2320                 
2321                for (int j = 0; j < paths.size (); ++j)
2322                  {
2323                    cmt_string& s = paths[j];
2324                     
2325                    if (s.find (new_value) != cmt_string::npos)
2326                      {
2327                        s = "";
2328                      }
2329                  }
2330
2331                Cmt::vector_to_string (paths, path_separator, temp);
2332              }
2333             
2334            break;
2335          case CommandPathRemoveRegexp :
2336
2337            if (new_value != "")
2338              {
2339                cmt_regexp e (new_value);
2340
2341                CmtSystem::cmt_string_vector paths;
2342                 
2343                CmtSystem::split (temp, path_separator, paths);
2344                 
2345                for (int j = 0; j < paths.size (); ++j)
2346                  {
2347                    cmt_string& s = paths[j];
2348
2349                    if (CmtSystem::getenv ("TESTPRR") != "")
2350                      {
2351                        cerr << "PRR> s=[" << s << "]";
2352                      }
2353
2354                    if (e.match (s))
2355                      {
2356                        s = "";
2357
2358                        if (CmtSystem::getenv ("TESTPRR") != "")
2359                          {
2360                            cerr << " match ";
2361                          }
2362                      }
2363                    else
2364                      {
2365                        if (CmtSystem::getenv ("TESTPRR") != "")
2366                          {
2367                            cerr << " no match ";
2368                          }
2369                      }
2370
2371                    if (CmtSystem::getenv ("TESTPRR") != "")
2372                      {
2373                        cerr << endl;
2374                      }
2375                  }
2376
2377                Cmt::vector_to_string (paths, path_separator, temp);
2378              }
2379             
2380            break;
2381        }
2382
2383    }
2384
2385  level--;
2386
2387  for (;;)
2388    {
2389      int sz = temp.size ();
2390
2391      if (sz == 0) break;
2392
2393      if ((temp[0] == ';') || (temp[0] == ':'))
2394        {
2395          temp.erase (0, 1);
2396        }
2397      else if ((temp[sz-1] == ';') || (temp[sz-1] == ':'))
2398        {
2399          temp.erase (sz-1, 1);
2400        }
2401      else
2402        {
2403          break;
2404        }
2405    }
2406
2407  temp.replace_all ("::", ":");
2408  temp.replace_all (";;", ";");
2409 
2410  return (temp);
2411}
2412
2413//-------------------------------------------------------------
2414const cmt_string PathBuilder::clean (const Symbol& symbol,
2415                                     const cmt_string& /*tag_name*/)
2416{
2417    // Control of recursivity
2418  static int level = 0;
2419
2420  cmt_string temp;
2421  cmt_string new_value;
2422  static const cmt_string empty;
2423
2424  static cmt_string path_separator = CmtSystem::path_separator ();
2425
2426  temp = CmtSystem::getenv (symbol.name);
2427
2428    //cerr << "#####1 temp=" << temp << endl;
2429
2430  for (int i = 0; i < symbol.value_lists.size (); i++)
2431    {
2432      const SymbolValueList& value_list = symbol.value_lists[i];
2433
2434      if (value_list.discarded) continue;
2435
2436      if ((value_list.use != 0) &&
2437          (value_list.use->discarded)) continue;
2438
2439      const int selected = value_list.select_first ();
2440     
2441      if (selected < 0) continue;
2442 
2443      SymbolValue& value = value_list.values[selected];
2444         
2445      new_value = value.text;
2446         
2447          //resolve_value (new_value);
2448
2449        //cerr << "#####1 new_value=" << new_value << endl;
2450
2451      resolve_value_for_macros (new_value);
2452      resolve_value (new_value);
2453         
2454        //cerr << "#####2 new_value=" << new_value << endl;
2455
2456      switch (value_list.command_type)
2457        {
2458          case CommandPath :
2459           
2460            temp = "";
2461           
2462            break;
2463          case CommandPathAppend :
2464          case CommandPathPrepend :
2465          case CommandPathRemove :
2466
2467            if (new_value != "")
2468              {
2469                CmtSystem::cmt_string_vector paths;
2470                 
2471                CmtSystem::split (temp, path_separator, paths);
2472                 
2473                for (int j = 0; j < paths.size (); ++j)
2474                  {
2475                    cmt_string& s = paths[j];
2476                     
2477                    if (s.find (new_value) != cmt_string::npos)
2478                      {
2479                        s = "";
2480                      }
2481
2482                    if (j > 0)
2483                      {
2484                        cmt_string& s2 = paths[j-1];
2485                        if (s2 == s)
2486                          {
2487                            s2 = "";
2488                          }
2489                      }
2490                  }
2491
2492                Cmt::vector_to_string (paths, path_separator, temp);
2493                temp.replace_all ("::", ":");
2494                temp.replace_all (";;", ";");
2495              }
2496             
2497            break;
2498          case CommandPathRemoveRegexp :
2499
2500            if (new_value != "")
2501              {
2502                cmt_regexp e (new_value);
2503
2504                CmtSystem::cmt_string_vector paths;
2505                 
2506                CmtSystem::split (temp, path_separator, paths);
2507                 
2508                for (int j = 0; j < paths.size (); ++j)
2509                  {
2510                    cmt_string& s = paths[j];
2511                     
2512                    if (e.match (s))
2513                      {
2514                        s = "";
2515                      }
2516
2517                    if (j > 0)
2518                      {
2519                        cmt_string& s2 = paths[j-1];
2520                        if (s2 == s)
2521                          {
2522                            s2 = "";
2523                          }
2524                      }
2525                  }
2526
2527                Cmt::vector_to_string (paths, path_separator, temp);
2528                temp.replace_all ("::", ":");
2529                temp.replace_all (";;", ";");
2530              }
2531             
2532            break;
2533        }
2534    }
2535
2536    //cerr << "#####2 temp=" << temp << endl;
2537 
2538  return (temp);
2539}
2540
2541//-------------------------------------------------------------
2542const cmt_string MacroBuilder::build (const Symbol& symbol,
2543                                      const cmt_string& tag_name)
2544{
2545    // Control of recursivity
2546  static int level = 0;
2547
2548  cmt_string temp;
2549  cmt_string previous_temp;
2550  static const cmt_string empty;
2551  bool show_it = false;
2552
2553  ActionType action = Cmt::get_action ();
2554
2555  if (action == action_show_macro)
2556    {
2557      if (symbol.name == Cmt::get_current_target ())
2558        {
2559             // Should not display on recursive calls
2560          if (level == 0) show_it = m_display_it;
2561        }
2562    }
2563
2564  level++;
2565
2566  temp = "";
2567
2568  int i;
2569
2570  bool first_definition = true;
2571
2572  for (i = 0; i < symbol.value_lists.size (); i++)
2573    {
2574      const SymbolValueList& value_list = symbol.value_lists[i];
2575
2576      if ((value_list.use != 0) &&
2577          (value_list.use->discarded)) continue;
2578
2579      if (value_list.command_type != CommandMacroPrepend) continue;
2580
2581      const int selected = value_list.select_first (tag_name);
2582
2583      if (selected < 0) continue;
2584
2585      SymbolValue& value = value_list.values[selected];
2586
2587      if (show_it && !Symbol::get_inhibit_display())
2588        {
2589          value_list.show (symbol, value, first_definition);
2590        }
2591
2592      if (value_list.discarded) continue;
2593
2594      previous_temp = temp;
2595      temp = value.text;
2596      temp += previous_temp;
2597    }
2598
2599  previous_temp = temp;
2600  temp = "";
2601
2602  first_definition = true;
2603
2604  for (i = 0; i < symbol.value_lists.size (); i++)
2605    {
2606      const SymbolValueList& value_list = symbol.value_lists[i];
2607
2608      if ((value_list.use != 0) &&
2609          (value_list.use->discarded)) continue;
2610
2611      if (value_list.command_type != CommandMacro) continue;
2612
2613      const int selected = value_list.select_first (tag_name);
2614
2615      if (selected < 0) continue;
2616
2617      SymbolValue& value = value_list.values[selected];
2618
2619      if (show_it && !Symbol::get_inhibit_display())
2620        {
2621          value_list.show (symbol, value, first_definition);
2622        }
2623
2624      // WARNING:
2625      // Commented just for a test : should be uncommented after the test
2626      if (value_list.discarded) continue;
2627     
2628      if (!value_list.is_reflexive || 
2629          !symbol.value_is_reflexive (value.text))
2630        {
2631          temp = value.text;
2632        }
2633    }
2634
2635  previous_temp += temp;
2636  temp = previous_temp;
2637
2638  for (i = 0; i < symbol.value_lists.size (); i++)
2639    {
2640      const SymbolValueList& value_list = symbol.value_lists[i];
2641
2642      if ((value_list.use != 0) &&
2643          (value_list.use->discarded)) continue;
2644
2645      if (value_list.command_type != CommandMacroAppend) continue;
2646
2647      const int selected = value_list.select_first (tag_name);
2648
2649      if (selected < 0) continue;
2650
2651      SymbolValue& value = value_list.values[selected];
2652
2653      if (show_it && !Symbol::get_inhibit_display())
2654        {
2655          value_list.show (symbol, value, first_definition);
2656        }
2657
2658      if (value_list.discarded) continue;
2659
2660      temp += value.text;
2661    }
2662
2663  for (i = 0; i < symbol.value_lists.size (); i++)
2664    {
2665      const SymbolValueList& value_list = symbol.value_lists[i];
2666
2667      if ((value_list.use != 0) &&
2668          (value_list.use->discarded)) continue;
2669
2670      if ((value_list.command_type != CommandMacroRemove) &&
2671          (value_list.command_type != CommandMacroRemoveRegexp) &&
2672          (value_list.command_type != CommandMacroRemoveAll) &&
2673          (value_list.command_type != CommandMacroRemoveAllRegexp)) continue;
2674
2675      const int selected = value_list.select_first (tag_name);
2676
2677      if (selected < 0) continue;
2678
2679      SymbolValue& value = value_list.values[selected];
2680
2681      if (show_it && !Symbol::get_inhibit_display())
2682        {
2683          value_list.show (symbol, value, first_definition);
2684        }
2685
2686      if (value_list.discarded) continue;
2687
2688      switch (value_list.command_type)
2689        {
2690          case CommandMacroRemove :
2691            temp.replace (value.text, empty);
2692            break;
2693          case CommandMacroRemoveRegexp :
2694            if (value.text != "")
2695              {
2696                cmt_regexp e (value.text);
2697                cmt_regexp::iterator it;
2698
2699                it = e.begin (temp);
2700                if (it != e.end ())
2701                  {
2702                    temp.erase (it._pos, it._length);
2703                  }
2704              }
2705            break;
2706          case CommandMacroRemoveAll :
2707            temp.replace_all (value.text, empty);
2708            break;
2709          case CommandMacroRemoveAllRegexp :
2710            if (value.text != "")
2711              {
2712                cmt_regexp e (value.text);
2713                cmt_regexp::iterator it;
2714
2715                for (;;)
2716                  {
2717                    it = e.begin (temp);
2718                    if (it != e.end ())
2719                      {
2720                        temp.erase (it._pos, it._length);
2721                      }
2722                    else
2723                      {
2724                        break;
2725                      }
2726                  }
2727              }
2728            break;
2729        }
2730    }
2731
2732  level--;
2733
2734  return (temp);
2735}
2736
2737//-------------------------------------------------------------
2738const cmt_string ScriptBuilder::build (const Symbol& symbol,
2739                                       const cmt_string& tag_name)
2740{
2741    // Control of recursivity
2742  static int level = 0;
2743
2744  static const cmt_string empty = "";
2745
2746  if (symbol.value_lists.size () > 0)
2747    {
2748      const SymbolValueList& value_list = symbol.value_lists[0];
2749
2750      if (value_list.discarded) return (empty);
2751
2752      if ((value_list.use != 0) &&
2753          (value_list.use->discarded)) return (empty);
2754    }
2755
2756  return (symbol.name);
2757}
2758
2759//-------------------------------------------------------------
2760const cmt_string ActionBuilder::build (const Symbol& symbol,
2761                                       const cmt_string& tag_name)
2762{
2763    // Control of recursivity
2764  static int level = 0;
2765
2766  cmt_string temp;
2767  cmt_string previous_temp;
2768  static const cmt_string empty;
2769  bool show_it = false;
2770
2771  ActionType action = Cmt::get_action ();
2772
2773  if (action == action_show_action)
2774    {
2775      if (symbol.name == Cmt::get_current_target ())
2776        {
2777             // Should not display on recursive calls
2778          if (level == 0) show_it = m_display_it;
2779        }
2780    }
2781
2782  level++;
2783
2784  int i;
2785
2786  bool first_definition = true;
2787
2788  temp = "";
2789
2790  for (i = 0; i < symbol.value_lists.size (); i++)
2791    {
2792      const SymbolValueList& value_list = symbol.value_lists[i];
2793
2794      if ((value_list.use != 0) &&
2795          (value_list.use->discarded)) continue;
2796
2797      if (value_list.command_type != CommandAction) continue;
2798
2799      const int selected = value_list.select_first (tag_name);
2800
2801      if (selected < 0) continue;
2802
2803      SymbolValue& value = value_list.values[selected];
2804
2805      if (show_it && !Symbol::get_inhibit_display())
2806        {
2807          value_list.show (symbol, value, first_definition);
2808        }
2809
2810      // WARNING:
2811      // Commented just for a test : should be uncommented after the test
2812      if (value_list.discarded) continue;
2813     
2814      if (!value_list.is_reflexive || 
2815          !symbol.value_is_reflexive (value.text))
2816        {
2817          temp = value.text;
2818        }
2819    }
2820
2821  level--;
2822
2823  return (temp);
2824}
2825
2826//-------------------------------------------------------------
2827int SymbolValueList::select_first (const cmt_string& tag_name) const
2828{
2829  int priority = 0;
2830  int value_number;
2831  int selected = -1;
2832
2833  Tag* the_tag = 0;
2834
2835  if (tag_name != "") the_tag = Tag::find (tag_name);
2836
2837  for (value_number = 0;
2838       value_number < values.size ();
2839       value_number++)
2840    {
2841      const SymbolValue& value = values[value_number];
2842
2843      const Tag* tag = value.tag;
2844
2845      if (the_tag == 0)
2846        {
2847          if (!tag->is_selected ()) continue;
2848          selected = value_number;
2849          if (tag == Tag::get_default ())
2850            continue;
2851        }
2852      else
2853        {
2854          if (tag != the_tag) continue;
2855          selected = value_number;
2856        }
2857
2858      //
2859      // Only the value for the first alternative (i.e. non-default)
2860      // matching tag expression is
2861      // selected (which means the selection stops here)
2862      //
2863      break;
2864
2865      //
2866      // Only the first value at a given priority is
2867      // selected (which implies the '>' test instead
2868      // of '>=')
2869      //
2870      /*
2871      if (tag->get_priority () > priority)
2872        {
2873          priority = tag->get_priority ();
2874          selected = value_number;
2875        }
2876      */
2877    }
2878
2879  return (selected);
2880}
2881
2882//-------------------------------------------------------------
2883int SymbolValueList::select_last () const
2884{
2885  int priority = 0;
2886  int value_number;
2887  int selected = -1;
2888
2889  for (value_number = 0;
2890       value_number < values.size ();
2891       value_number++)
2892    {
2893      SymbolValue& value = values[value_number];
2894
2895      const Tag* tag = value.tag;
2896
2897      if (tag->is_selected ())
2898        {
2899          //
2900          // The last value at a given priority is
2901          // selected (which implies the '>=' test instead
2902          // of '>')
2903          //
2904
2905          if (tag->get_priority () >= priority)
2906            {
2907              priority = tag->get_priority ();
2908              selected = value_number;
2909            }
2910        }
2911    }
2912
2913  return (selected);
2914}
2915
2916//-------------------------------------------------------------
2917void SymbolValueList::show (const Symbol& symbol, 
2918                            const SymbolValue& value, 
2919                            bool& first_definition) const
2920{
2921  cmt_string discarded_text;
2922  cmt_string define_text;
2923  ActionType action = Cmt::get_action ();
2924
2925  switch (command_type)
2926    {
2927      //case CommandSet :
2928      case CommandSetAppend :
2929      case CommandSetPrepend :
2930      case CommandSetRemove :
2931      case CommandSetRemoveRegexp :
2932        //case CommandAlias :
2933        //case CommandPath :
2934      case CommandPathAppend :
2935      case CommandPathPrepend :
2936      case CommandPathRemove :
2937      case CommandPathRemoveRegexp :
2938      case CommandMacroPrepend :
2939        //case CommandMacro :
2940      case CommandMacroAppend :
2941      case CommandMacroRemove :
2942      case CommandMacroRemoveRegexp :
2943      case CommandMacroRemoveAll :
2944      case CommandMacroRemoveAllRegexp :
2945        //case CommandAction :
2946        if (value.text == "") return;
2947        break;
2948    }
2949
2950  if (discarded) discarded_text = " (discarded by override)";
2951  else discarded_text = "";
2952
2953  if (first_definition) define_text = "defines";
2954  else define_text = "overrides";
2955
2956  cout << "# Package ";
2957  if (use != 0)
2958    {
2959      cout << use->get_package_name () << " " << use->version;
2960    }
2961
2962  switch (command_type)
2963    {
2964      case CommandSet :
2965        cout << " " << define_text << " set " << symbol.name << " as ";
2966        first_definition = false;
2967        break;
2968      case CommandSetAppend :
2969        cout << " appends to set " << symbol.name << " : ";
2970        break;
2971      case CommandSetPrepend :
2972        cout << " prepends to set " << symbol.name << " : ";
2973        break;
2974      case CommandSetRemove :
2975        cout << " removes from set " << symbol.name << " : ";
2976        break;
2977      case CommandSetRemoveRegexp :
2978        cout << " removes RE from set " << symbol.name << " : ";
2979        break;
2980      case CommandAlias :
2981        cout << " " << define_text << " alias " << symbol.name << " as ";
2982        first_definition = false;
2983        break;
2984      case CommandPath :
2985        cout << " " << define_text << " path " << symbol.name << " as ";
2986        first_definition = false;
2987        break;
2988      case CommandPathAppend :
2989        cout << " appends to path " << symbol.name << " : ";
2990        break;
2991      case CommandPathPrepend :
2992        cout << " prepends to path " << symbol.name << " : ";
2993        break;
2994      case CommandPathRemove :
2995        cout << " removes from path " << symbol.name << " : ";
2996        break;
2997      case CommandPathRemoveRegexp :
2998        cout << " removes RE from path " << symbol.name << " : ";
2999        break;
3000      case CommandMacroPrepend :
3001        cout << " prepends to macro " << symbol.name << " : ";
3002        break;
3003      case CommandMacro :
3004        cout << " " << define_text << " macro " << symbol.name << " as ";
3005        break;
3006      case CommandMacroAppend :
3007        cout << " appends to macro " << symbol.name << " : ";
3008        break;
3009      case CommandMacroRemove :
3010        cout << " remove from macro " << symbol.name << " : ";
3011        break;
3012      case CommandMacroRemoveRegexp :
3013        cout << " remove RE from macro " << symbol.name << " : ";
3014        break;
3015      case CommandMacroRemoveAll :
3016        cout << " remove all from macro " << symbol.name << " : ";
3017        break;
3018      case CommandMacroRemoveAllRegexp :
3019        cout << " remove all RE from macro " << symbol.name << " : ";
3020        break;
3021      case CommandAction :
3022        cout << " " << define_text << " action " << symbol.name << " as ";
3023        first_definition = false;
3024        break;
3025    }
3026
3027  cout << "'" << value.text << "'";
3028         
3029  Tag* selected_tag = value.tag;
3030         
3031  if ((selected_tag == 0) ||
3032      (selected_tag == Tag::get_default ()))
3033    {
3034      cout << " for default tag";
3035    }
3036  else
3037    {
3038      cout << " for tag '" << selected_tag->get_name () << "'";
3039    }
3040 
3041  cout << discarded_text << endl;
3042}
3043
3044//-------------------------------------------------------------
3045//  Quote separators (spaces and tabs) with double quotes,
3046//  double quotes with single quotes,
3047//  single quotes with double quotes in text.
3048//  Note: quotes preceded by backslash (i.e., \" or \') are NOT quoted
3049//  (considered escaped)
3050//-------------------------------------------------------------
3051static cmt_string quote (const cmt_string& text,
3052                         const cmt_string& separators)
3053{
3054  //cerr << "quote: `" << text << "'" << endl;
3055  cmt_string result;
3056  if (text.size () == 0) return result;
3057
3058  int allocated = 3 * text.size (); // if EACH character of text quoted with " or '
3059  char* const buffer = (char*) malloc (allocated + 1);
3060  char* b (buffer);
3061
3062  //  char* const beg (buffer);
3063  const char* p = text.c_str ();
3064  const char* const text_c (p);
3065  //  const char* const beg_t (p);
3066  //  cerr << "quote: p = `" << p << "'" << endl;
3067
3068  while (*p)
3069    //  while (*p != '\0')
3070    {
3071      size_t l_nonsep = strcspn (p, separators.c_str ());
3072      //cerr << "quote: l_nonsep = " << l_nonsep << " *p = '" << *p << "'" << endl;
3073      while (l_nonsep--)
3074        {
3075          if (*p == '\"' &&
3076              (p > text_c && *(p - 1) != '\\' || p == text_c))
3077            { // quote " with '
3078              *b++ = '\'';
3079              *b++ = *p++;
3080              *b++ = '\'';
3081            }
3082          else if (*p == '\'' &&
3083                   (p > text_c && *(p - 1) != '\\' || p == text_c))
3084            { // quote ' with "
3085              *b++ = '\"';
3086              *b++ = *p++;
3087              *b++ = '\"';
3088            }
3089          else
3090            { // simply copy
3091              *b++ = *p++;
3092            }
3093        }
3094      size_t l_sep = strspn (p, separators.c_str ());
3095      //cerr << "quote: l_sep = " << l_sep << " *p = '" << *p << "'" << endl;
3096      if (l_sep)
3097        { // quote separators with "
3098          // place quote before all backslashes preceding separators, if any
3099          char* r = b;
3100          while (r > buffer && *(r - 1) == '\\')
3101            r--;
3102          if (r == b)
3103            *b++ = '\"';
3104          else
3105            *r = '\"', *b++ = '\\';
3106          while (l_sep--)
3107            *b++ = *p++;
3108          *b++ = '\"';
3109        }
3110    }
3111  *b = '\0';
3112  result = buffer;
3113  free (buffer);
3114  return result;
3115}
3116
3117//-------------------------------------------------------------
3118int SymbolValueList::print (const Symbol& symbol, 
3119                            const SymbolValue& value, 
3120                            bool& first_definition) const
3121{
3122  int result (0);
3123
3124  if (use->get_package_name () == "CMT") return result;
3125
3126  cmt_string discarded_text;
3127  cmt_string define_text;
3128  ActionType action = Cmt::get_action ();
3129
3130  switch (command_type)
3131    {
3132      //case CommandSet :
3133      case CommandSetAppend :
3134      case CommandSetPrepend :
3135      case CommandSetRemove :
3136      case CommandSetRemoveRegexp :
3137        //case CommandAlias :
3138        //case CommandPath :
3139      case CommandPathAppend :
3140      case CommandPathPrepend :
3141      case CommandPathRemove :
3142      case CommandPathRemoveRegexp :
3143      case CommandMacroPrepend :
3144        //case CommandMacro :
3145      case CommandMacroAppend :
3146      case CommandMacroRemove :
3147      case CommandMacroRemoveRegexp :
3148      case CommandMacroRemoveAll :
3149      case CommandMacroRemoveAllRegexp :
3150        //case CommandAction :
3151        if (value.text == "") return result;
3152        break;
3153    }
3154
3155  if (discarded) discarded_text = " (discarded by override)";
3156  else discarded_text = "";
3157
3158  if (first_definition) define_text = "defines";
3159  else define_text = "overrides";
3160
3161  cout << "# Package ";
3162  if (use != 0)
3163    {
3164      cout << use->get_package_name () << " " << use->version;
3165    }
3166
3167  switch (command_type)
3168    {
3169      case CommandSet :
3170        cout << " " << define_text << " set " << symbol.name << " as " << endl;
3171        //        cout << " " << define_text << " set " << symbol.name << " as ";
3172        cout << "set " << symbol.name;
3173        first_definition = false;
3174        result = 1;
3175        break;
3176      case CommandSetAppend :
3177        cout << " appends to set " << symbol.name << " : " << endl;;
3178        //        cout << " appends to set " << symbol.name << " : ";
3179        cout << "set_append " << symbol.name;
3180        result = 1;
3181        break;
3182      case CommandSetPrepend :
3183        cout << " prepends to set " << symbol.name << " : " << endl;
3184        //        cout << " prepends to set " << symbol.name << " : ";
3185        cout << "set_prepend " << symbol.name;
3186        result = 1;
3187        break;
3188      case CommandSetRemove :
3189        cout << " removes from set " << symbol.name << " : " << endl;
3190        //        cout << " removes from set " << symbol.name << " : ";
3191        cout << "set_remove " << symbol.name;
3192        result = 1;
3193        break;
3194      case CommandSetRemoveRegexp :
3195        cout << " removes RE from set " << symbol.name << " : " << endl;
3196        //        cout << " removes RE from set " << symbol.name << " : ";
3197        cout << "set_remove_regexp " << symbol.name;
3198        result = 1;
3199        break;
3200      case CommandAlias :
3201        cout << " " << define_text << " alias " << symbol.name << endl;
3202        //        cout << " " << define_text << " alias " << symbol.name << " as ";
3203        cout << "alias " << symbol.name;
3204        first_definition = false;
3205        result = 1;
3206        break;
3207      case CommandPath :
3208        cout << " " << define_text << " path " << symbol.name << endl;
3209        //        cout << " " << define_text << " path " << symbol.name << " as ";
3210        cout << "path " << symbol.name;
3211        first_definition = false;
3212        result = 1;
3213        break;
3214      case CommandPathAppend :
3215        cout << " appends to path " << symbol.name << endl;
3216        //        cout << " appends to path " << symbol.name << " : ";
3217        cout << "path_append " << symbol.name;
3218        result = 1;
3219        break;
3220      case CommandPathPrepend :
3221        cout << " prepends to path " << symbol.name << " : " << endl;
3222        //        cout << " prepends to path " << symbol.name << " : ";
3223        cout << "path_prepend " << symbol.name;
3224        result = 1;
3225        break;
3226      case CommandPathRemove :
3227        cout << " removes from path " << symbol.name << " : " << endl;
3228        //        cout << " removes from path " << symbol.name << " : ";
3229        cout << "path_remove " << symbol.name;
3230        result = 1;
3231        break;
3232      case CommandPathRemoveRegexp :
3233        cout << " removes RE from path " << symbol.name << " : " << endl;
3234        //        cout << " removes RE from path " << symbol.name << " : ";
3235        cout << "path_remove_regexp " << symbol.name;
3236        result = 1;
3237        break;
3238      case CommandMacro :
3239        cout << " " << define_text << " macro " << symbol.name << " as " << endl;
3240        //        cout << " " << define_text << " macro " << symbol.name << " as ";
3241        cout << "macro " << symbol.name;
3242        result = 1;
3243        break;
3244      case CommandMacroPrepend :
3245        cout << " prepends to macro " << symbol.name << " : " << endl;
3246        //        cout << " prepends to macro " << symbol.name << " : ";
3247        cout << "macro_prepend " << symbol.name;
3248        result = 1;
3249        break;
3250      case CommandMacroAppend :
3251        cout << " appends to macro " << symbol.name << " : " << endl;
3252        //        cout << " appends to macro " << symbol.name << " : ";
3253        cout << "macro_append " << symbol.name;
3254        result = 1;
3255        break;
3256      case CommandMacroRemove :
3257        cout << " remove from macro " << symbol.name << " : " << endl;
3258        //        cout << " remove from macro " << symbol.name << " : ";
3259        cout << "macro_remove " << symbol.name;
3260        result = 1;
3261        break;
3262      case CommandMacroRemoveRegexp :
3263        cout << " remove RE from macro " << symbol.name << " : " << endl;
3264        //        cout << " remove RE from macro " << symbol.name << " : ";
3265        cout << "macro_remove_regexp " << symbol.name;
3266        result = 1;
3267        break;
3268      case CommandMacroRemoveAll :
3269        cout << " remove all from macro " << symbol.name << " : " << endl;
3270        //        cout << " remove all from macro " << symbol.name << " : ";
3271        cout << "macro_remove_all " << symbol.name;
3272        result = 1;
3273        break;
3274      case CommandMacroRemoveAllRegexp :
3275        cout << " remove all RE from macro " << symbol.name << " : " << endl;
3276        //        cout << " remove all RE from macro " << symbol.name << " : ";
3277        cout << "macro_remove_all_regexp " << symbol.name;
3278        result = 1;
3279        break;
3280      case CommandSetupScript :
3281        cout << " " << define_text << " setup script " << symbol.name << endl;
3282        //       cout << " " << define_text << " action " << symbol.name << " as ";
3283        cout << "setup_script" /* << symbol.name */;
3284        first_definition = false;
3285        result = 1;
3286        break;
3287        /*
3288      case CommandAction :
3289        cout << " " << define_text << " action " << symbol.name << " as ";
3290        first_definition = false;
3291        break;
3292        */
3293    }
3294
3295  cout << " " << quote (value.text, " \t");
3296  //  cout << "'" << value.text << "'";
3297         
3298  /*
3299  Tag* selected_tag = value.tag;
3300         
3301  if ((selected_tag == 0) ||
3302      (selected_tag == Tag::get_default ()))
3303    {
3304      cout << " for default tag";
3305    }
3306  else
3307    {
3308      cout << " for tag '" << selected_tag->get_name () << "'";
3309    }
3310  */
3311
3312  cout << endl;
3313  //  cout << discarded_text << endl;
3314  return result;
3315}
3316
3317//-------------------------------------------------------------
3318bool Symbol::check_tag_used (Tag* tag)
3319{
3320  if (tag == 0) return (false);
3321
3322  static SymbolVector& Symbols = symbols ();
3323
3324  if (Symbols.size () == 0) 
3325    {
3326      return (false);
3327    }
3328
3329  for (int number = 0; number < Symbol::symbol_number (); number++)
3330    {
3331      Symbol& symbol = Symbol::symbol (number);
3332
3333      for (int i = 0; i < symbol.value_lists.size (); i++)
3334        {
3335          const SymbolValueList& value_list = symbol.value_lists[i];
3336
3337          for (int j = 0; j < value_list.values.size (); j++)
3338            {
3339              const SymbolValue& value = value_list.values[j];
3340              Tag* t = value.tag;
3341
3342              if (t != 0)
3343                {
3344                  if (t->use_operand (tag)) return (true);
3345                }
3346            }
3347        }
3348    }
3349
3350  return (false);
3351}
3352
3353//-------------------------------------------------------------
3354cmt_string Symbol::get_env_value (const cmt_string& name)
3355{
3356  cmt_string s;
3357
3358  Symbol* symbol = Symbol::find (name);
3359  if (symbol != 0)
3360    {
3361      m_inhibit_display = true;
3362
3363      s = symbol->build_macro_value ();
3364      Symbol::expand (s);
3365
3366      m_inhibit_display = false;
3367    }
3368  else
3369    {
3370      s = CmtSystem::getenv (name);
3371    }
3372
3373  return (s);
3374}
3375
3376bool Symbol::get_inhibit_display ()
3377{
3378  return (m_inhibit_display);
3379}
3380
Note: See TracBrowser for help on using the repository browser.