source: CMT/v1r23/source/cmt_symbol.cxx @ 615

Last change on this file since 615 was 565, checked in by rybkin, 13 years ago

See C.L. 448

  • Property svn:eol-style set to native
File size: 83.2 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                // <PACKAGE>ROOT macro is defined in
777                // Use::fill_standard_macros <- SyntaxParser::parse_requirements
778                // "cmt -requirements setup" may generate <PACKAGE>ROOT set
779                // hence, warning
780                // Symbol <PACKAGE>ROOT inconsistently redeclared from macro to set in package <package>
781                // below, we suppress the warning for current package
782                if (use != &(Use::current()) ||
783                    (Use::current().prefix + "ROOT") != name ||
784                    symbol->type != SymbolMacro ||
785                    type != SymbolSet)
786                  CmtMessage::warning ("Symbol " + name
787                                       + " inconsistently redeclared from " + s1
788                                       + " to " + s2
789                                       + ( (use != 0) ?
790                                           " in package " + use->get_package_name () :
791                                           "" )
792                                       );
793                /*
794                cerr << "#CMT> Warning: Symbol " << name
795                     << " inconsistently redeclared from " << s1 << " to " << s2;
796                if (use != 0) cerr << " in package " << use->get_package_name ();
797                cerr << endl;
798                */
799              }
800          }
801
802        return (symbol);
803      }
804  }
805
806  Symbol& symbol = Symbols.add ();
807  SymbolMap.add (name, symbol);
808
809  symbol.name  = name;
810  symbol.scope = use->get_current_scope ();
811  symbol.type  = type;
812
813  symbol.value_lists.clear ();
814
815  switch (type)
816    {
817    case SymbolSet:
818      symbol.builder = &Set;
819      break;
820    case SymbolPath:
821      symbol.builder = &Path;
822      break;
823    case SymbolAlias:
824      symbol.builder = &Set;
825      break;
826    case SymbolMacro:
827      symbol.builder = &Macro;
828      break;
829    case SymbolSetupScript:
830    case SymbolCleanupScript:
831      symbol.builder = &Script;
832      break;
833    case SymbolAction:
834      symbol.builder = &Action;
835      break;
836    }
837
838  symbol.selected_value = -1;
839  symbol.printed = false;
840
841  return (&symbol);
842}
843
844//-------------------------------------------------------------
845Symbol* Symbol::find (const cmt_string& name)
846{
847  static SymbolMap& SymbolMap = symbol_map ();
848
849  Symbol* result = 0;
850
851  result = SymbolMap.find (name);
852
853  return (result);
854}
855
856//-------------------------------------------------------------
857int Symbol::symbol_number ()
858{
859  static SymbolVector& Symbols = symbols ();
860
861  return (Symbols.size ());
862}
863
864//-------------------------------------------------------------
865Symbol::SymbolVector& Symbol::symbols ()
866{
867  static Database& db = Database::instance ();
868  static SymbolVector& Symbols = db.symbols ();
869
870  return (Symbols);
871}
872
873//-------------------------------------------------------------
874Symbol::SymbolMap& Symbol::symbol_map ()
875{
876  static Database& db = Database::instance ();
877  static SymbolMap& SymbolMap = db.symbol_map ();
878
879  return (SymbolMap);
880}
881
882/**
883   Filter out faulty items of a path-like symbol value
884 */
885void Symbol::filter_path_value (const cmt_string& name, cmt_string& text)
886{
887
888  CmtSystem::cmt_string_vector paths;
889  CmtSystem::cmt_string_vector normalyzed_paths;   
890                 
891  CmtSystem::split (text, CmtSystem::path_separator (), paths);
892                 
893  for (int j = 0; j < paths.size (); ++j)
894    {
895      cmt_string& t = paths[j];
896
897      if (!CmtSystem::test_directory (t))
898          {
899            if (CmtMessage::active (Verbose))
900              CmtMessage::warning ("Non-existent directory " + t + " in " + name);
901            /*
902              if (Cmt::get_warnings ())
903              {
904                  cerr << "#CMT> Warning: Wrong path item " << t << " in " << name << endl;
905              }
906            */
907          }
908           
909      int exist = 0; 
910      for (int i = 0; i < j; ++i)
911      {
912          cmt_string& u = paths[i];
913          if (u == t)
914             exist = 1;
915      }           
916      if (exist==0)
917      {
918          cmt_string& s = normalyzed_paths.add ();
919          s = t;
920      }             
921    }
922   
923  Cmt::vector_to_string (normalyzed_paths, CmtSystem::path_separator (), text);
924
925  for (;;)
926    {
927      int sz = text.size ();
928
929      if (sz == 0) break;
930
931      if ((text[0] == ';') || (text[0] == ':'))
932        {
933          text.erase (0, 1);
934        }
935      else if ((text[sz-1] == ';') || (text[sz-1] == ':'))
936        {
937          text.erase (sz-1, 1);
938        }
939      else
940        {
941          break;
942        }
943    }
944
945  text.replace_all ("::", ":");
946  text.replace_all (";;", ";");
947}
948
949
950//-------------------------------------------------------------
951Symbol& Symbol::symbol (int index)
952{
953  static SymbolVector& Symbols = symbols ();
954
955  return (Symbols[index]);
956}
957
958//-------------------------------------------------------------
959void Symbol::action (const CmtSystem::cmt_string_vector& words,
960                     CommandType command_type,
961                     Use* use)
962{
963  int number;
964  Symbol* symbol;
965  Tag* tag;
966
967  if (words.size () < 1) return;
968  cmt_string name = words[1];
969
970  if ((command_type == CommandSetupScript) ||
971      (command_type == CommandCleanupScript))
972    {
973      cmt_string full_name;
974
975      Symbol::expand (name);
976
977      if (name != "")
978        {
979          if (CmtSystem::absolute_path (name)) 
980            {
981              full_name = name;
982            }
983          else
984            {
985              if (use->get_strategy ("SetupRoot") &&
986                  use->get_package_name () != "cmt_standalone")
987                {
988#ifdef WIN32
989              full_name = "%";
990#else
991              full_name = "${";
992#endif
993              full_name += CmtSystem::mangle (use->prefix);
994              full_name += "ROOT";
995#ifdef WIN32
996              full_name += "%";
997#else
998              full_name += "}";
999#endif
1000                }
1001              else
1002                {
1003              full_name = use->get_full_path ();
1004                }
1005              if (use->style == cmt_style)
1006                {
1007                  full_name += CmtSystem::file_separator ();
1008                  full_name += "cmt";
1009                }
1010              else if (use->style == mgr_style)
1011                {
1012                  full_name += CmtSystem::file_separator ();
1013                  full_name += "mgr";
1014                }
1015              //              else if (use->style == no_version_style) full_name += "cmt";
1016              //              else full_name += "mgr";
1017              full_name += CmtSystem::file_separator ();
1018              full_name += name;
1019            }
1020
1021          symbol = create (full_name, command_type, use);
1022          symbol->add_value_to_list (command_type, use,
1023                                     Tag::get_default (), full_name);
1024        }
1025    }
1026  else
1027    {
1028      if (words.size () < 2) return;
1029      const cmt_string& default_value = words[2];
1030
1031      Cmt::reset_all_sets_done ();
1032
1033      if (Cmt::get_debug ())
1034        {
1035          cout << "Symbol::action> name:" << name
1036               << " access:" << Cmt::get_current_access () 
1037               << " scope:" << use->get_current_scope () << endl;
1038        }
1039
1040      if (Cmt::get_current_access () == UserMode)
1041        {
1042          /*
1043            The following statements mean that some symbols are
1044            always private.
1045            This list is hardcoded. This should be replaced by a
1046            database of "private" symbols.
1047           */
1048          if (name == "constituents") return;
1049          if (name == "constituentscclean") return;
1050
1051          if (use->get_current_scope () == ScopePrivate) return;
1052        }
1053
1054      symbol = create (name, command_type, use);
1055
1056      /*
1057        Parse the default value.
1058      */
1059     
1060      symbol->add_value_to_list (command_type, use,
1061                                 Tag::get_default (), default_value);
1062     
1063      /*
1064        Then parse all specific values
1065       
1066        <tag> <value>
1067        ...
1068      */
1069     
1070      number = 3;
1071      while (number < (words.size () - 1))
1072        {
1073          cmt_string tag_name = words[number];
1074          const cmt_string& value = words[number + 1];
1075
1076          expand (tag_name);
1077
1078          if (Cmt::get_debug ())
1079            {
1080              cout << "Symbol::action> tag_name=" << tag_name << endl;
1081            }
1082
1083          tag = Tag::find (tag_name);
1084          if (tag == 0)
1085            {
1086              tag = Tag::add (tag_name, PriorityUserTag, "use", use);
1087            }
1088
1089          symbol->add_value_to_list (command_type, use, tag, value);
1090         
1091          number += 2;
1092        }
1093
1094      if (name == "CMTPATH")
1095        {
1096          Cmt::configure_cmt_path (use);
1097        }
1098      else if (name == "CMTPROJECTPATH")
1099        {
1100          Cmt::configure_cmt_path (use);
1101        }
1102      else if (name == "CMTSITE")
1103        {
1104          Cmt::configure_site_tag (use);
1105        }
1106      else if (name == "CMTCONFIG")
1107        {
1108            //cerr << "redefining CMTCONFIG" << endl;
1109          Cmt::configure_tags (use);
1110        }
1111      else if (name == "CMTHOME")
1112        {
1113          Cmt::configure_home (use);
1114        }
1115      else if (name == "CMTUSERCONTEXT")
1116        {
1117          Cmt::configure_user_context (use);
1118        }
1119      else if (name.find ("_native_version") != cmt_string::npos)
1120        {
1121          cmt_string n = use->get_package_name ();
1122          n += "_native_version";
1123
1124          if (name == n)
1125            {
1126              use->set_native_version (true);
1127            }
1128        }
1129    }
1130}
1131
1132//-------------------------------------------------------------
1133int Symbol::is_selected (const cmt_string& name)
1134{
1135  Symbol* symbol;
1136  int number;
1137  int value_number;
1138
1139  symbol = find (name);
1140  if (symbol == 0) return (0);
1141
1142  if (symbol->value_lists.size () == 0) return (0);
1143
1144  for (number = 0;
1145       number < symbol->value_lists.size ();
1146       number++)
1147    {
1148      const SymbolValueList& value_list = symbol->value_lists[number];
1149
1150      if (value_list.discarded) continue;
1151
1152      if ((value_list.command_type == CommandMacro) ||
1153          (value_list.command_type == CommandSet) ||
1154          (value_list.command_type == CommandSetAppend) ||
1155          (value_list.command_type == CommandSetPrepend) ||
1156          (value_list.command_type == CommandSetRemove) ||
1157          (value_list.command_type == CommandSetRemoveRegexp) ||
1158          (value_list.command_type == CommandAlias) ||
1159          (value_list.command_type == CommandAction))
1160        {
1161          for (value_number = 0;
1162               value_number < value_list.values.size ();
1163               value_number++)
1164            {
1165              Tag* tag;
1166
1167              SymbolValue& value = value_list.values[value_number];
1168
1169              tag = value.tag;
1170              if ((tag == 0) ||
1171                  (tag == Tag::get_default ()) ||
1172                  (tag->is_selected () != 0))
1173                {
1174                  return (1);
1175                }
1176            }
1177        }
1178    }
1179
1180  return (0);
1181}
1182
1183//-------------------------------------------------------------
1184Symbol::Symbol ()
1185{
1186  name = "";
1187}
1188
1189//-------------------------------------------------------------
1190Symbol::~Symbol ()
1191{
1192}
1193
1194/**
1195 *  Characterizes if a value is provided as the reference to itself.
1196 */
1197bool Symbol::value_is_reflexive (const cmt_string& text) const
1198{
1199  bool result = false;
1200  int text_length = text.size ();
1201
1202  if (text_length == (name.size () + 3))
1203    {
1204      static cmt_string temp;
1205             
1206      if (text[0] == '$')
1207        {
1208          if (text[1] == '(')
1209            {
1210              temp = "$(";
1211              temp += name;
1212              temp += ")";
1213             
1214              if (text == temp)
1215                {
1216                  result = true;
1217                }
1218            }
1219          else if (text[1] == '{')
1220            {
1221              temp = "${";
1222              temp += name;
1223              temp += "}";
1224             
1225              if (text == temp)
1226                {
1227                  result = true;
1228                }
1229            }
1230        }
1231    }
1232  else if (text_length == (name.size () + 2))
1233    {
1234      static cmt_string temp;
1235
1236      temp = "%";
1237      temp += name;
1238      temp += "%";
1239
1240      if (text == temp)
1241        {
1242          result = true;
1243        }
1244    }
1245
1246  return (result);
1247}
1248
1249//-------------------------------------------------------------
1250void Symbol::add_value_to_list (CommandType command_type,
1251                                Use* use,
1252                                Tag* tag,
1253                                const cmt_string& text)
1254{
1255  SymbolValueList* value_list = 0;
1256  bool is_reflexive = false;
1257
1258    //
1259    // First pickup the most recent value_list
1260    //
1261  if (value_lists.size () > 0) value_list = &(value_lists.back ());
1262
1263    //
1264    //  Create a new value list is we switch to another use or
1265    //  if we switch to a new command_type (eg. switching from
1266    //  macro to macro_append).
1267    //
1268  if ((value_list == 0) ||
1269      (use != value_list->use) ||
1270      (command_type != value_list->command_type) ||
1271      (tag == Tag::get_default ()))
1272    {
1273      value_list = &(value_lists.add ());
1274      value_list->use = use;
1275      value_list->command_type = command_type;
1276      value_list->values.clear ();
1277      value_list->discarded = false;
1278      value_list->is_reflexive = false;
1279    }
1280
1281/*
1282  else
1283    {
1284        value_list = &(value_lists[value_lists.size () - 1]);
1285    }
1286*/
1287
1288  is_reflexive = value_list->is_reflexive;
1289
1290    //
1291    //  If the command_type is command_macro or command_set,
1292    // this is considered as a full re-set of this symbol
1293    //   In this case, we have to discard all previous values
1294    //
1295    //  However, we'd like to exclude from this logic the cases where
1296    //  the value is **exactly*
1297    //
1298    //     $(<symbol>)
1299    //     ${<symbol>}
1300    //     %<symbol>%
1301    //
1302    //   which would then mean that we do not reset the value but rather
1303    //  override it.
1304    //
1305
1306    //
1307    // Inside this value_list, we add this new tag-value pair.
1308    //
1309
1310  if ((command_type == CommandMacro) ||
1311      (command_type == CommandSet) ||
1312      (command_type == CommandPath) ||
1313      (command_type == CommandAction))
1314    {
1315        //
1316        // Check whether we have to hide previous settings by this new definition
1317        //  (of course this is only useful if there WERE previous settings)
1318        //
1319      if ((value_lists.size () >= 1) && (!is_reflexive))
1320        {
1321          if (value_is_reflexive (text))
1322            {
1323              value_list->is_reflexive = true;
1324              is_reflexive = true;
1325            }
1326          else
1327            {
1328              //cerr << "...discarding old values for symbol " << name << " text=[" << text << "]" << endl;
1329                 
1330              for (int i = 0; i < (value_lists.size () - 1); i++)
1331                {
1332                  SymbolValueList& vl = value_lists[i];
1333                 
1334                  if ((vl.use != 0) &&
1335                      (vl.use->discarded))
1336                    {
1337                      //vl.discarded = true;
1338                    }
1339                }
1340            }
1341        }
1342    }
1343
1344  SymbolValue& value = value_list->values.add ();
1345
1346  value.tag = tag;
1347  value.text = text;
1348  value.selected = 0;
1349}
1350
1351/**
1352   Compute the current value of all environment variables and set them to the
1353   shell
1354 */
1355void Symbol::all_set ()
1356{
1357  //cerr << "all_set" << endl;
1358
1359  static bool running = false;
1360
1361
1362  if (Cmt::get_debug ())
1363    {
1364      cout << "\nSymbol::all_set> done=" << running << endl;
1365    }
1366
1367  if (Cmt::get_all_sets_done ()) return;
1368
1369  if (running) return;
1370
1371  running = true;
1372  Cmt::set_all_sets_done ();
1373
1374  static SymbolVector& Symbols = symbols ();
1375  Use::UsePtrVector& Uses = Use::get_ordered_uses ();
1376
1377  int number;
1378
1379  if (Symbols.size () == 0) 
1380    {
1381      running = false;
1382      return;
1383    }
1384
1385  cmt_string value;
1386
1387  for (number = 0; number < Symbol::symbol_number (); number++)
1388    {
1389      Symbol& symbol = Symbol::symbol (number);
1390
1391      if (symbol.type != SymbolSet) continue;
1392
1393      value = symbol.build_macro_value ();
1394      if (value != "")
1395        {
1396          Symbol::expand (value);
1397
1398          CmtSystem::putenv (symbol.name, value);
1399        }
1400    }
1401
1402  cmt_string cmtconfig = CmtSystem::get_cmt_config ();
1403
1404  if (Uses.size () > 0)
1405    {
1406      int number;
1407
1408      for (number = 0; number < Uses.size (); number++)
1409        {
1410          Use& use = *(Uses[number]);
1411
1412          if (use.discarded) continue;
1413
1414          if (use.get_package_name () == "cmt_standalone") continue;
1415
1416          if (use.get_strategy ("SetupConfig"))
1417            {
1418              cmt_string temp;
1419
1420              temp = use.prefix;
1421              temp += "CONFIG";
1422
1423              CmtSystem::putenv (temp, cmtconfig);
1424            }
1425
1426          if (use.get_strategy ("SetupRoot"))
1427            {
1428              cmt_string temp;
1429
1430              temp = use.prefix;
1431              temp += "ROOT";
1432
1433              CmtSystem::putenv (temp, use.get_full_path ());
1434            }
1435        }
1436    }
1437
1438  {
1439    Use& use = Use::current ();
1440
1441    if (use.get_package_name () != "cmt_standalone")
1442      {
1443        if (use.get_strategy ("SetupConfig"))
1444          {
1445            cmt_string temp;
1446
1447            temp = use.prefix;
1448            temp += "CONFIG";
1449
1450            CmtSystem::putenv (temp, cmtconfig);
1451          }
1452       
1453        if (use.get_strategy ("SetupRoot"))
1454          {
1455            cmt_string temp;
1456
1457            temp = use.prefix;
1458            temp += "ROOT";
1459
1460            CmtSystem::putenv (temp, use.get_full_path ());
1461          }
1462      }
1463  }
1464
1465  for (number = 0; number < Symbol::symbol_number (); number++)
1466    {
1467      Symbol& symbol = Symbol::symbol (number);
1468
1469      if ((symbol.type != SymbolPath)) continue;
1470
1471      value = symbol.build_macro_value ();
1472      if (value != "")
1473        {
1474          Symbol::expand (value);
1475          filter_path_value (symbol.name, value);
1476
1477#ifdef WIN32
1478          value.replace_all ("/", "\\");
1479#endif
1480
1481          if (Cmt::get_debug ())
1482            {
1483              cerr << "Symbol::all_set-2> " << symbol.name << " = " << value << endl;
1484            }
1485
1486          CmtSystem::putenv (symbol.name, value);
1487        }
1488    }
1489
1490  running = false;
1491}
1492
1493//-------------------------------------------------------------
1494void Symbol::all_print (PrintMode mode)
1495{
1496  static SymbolVector& Symbols = symbols ();
1497
1498  int number;
1499
1500  if (Symbols.size () == 0) return;
1501
1502  switch (mode)
1503    {
1504    case Requirements :
1505      for (number = 0; number < Symbol::symbol_number (); number++)
1506        {
1507          Symbol& symbol = Symbol::symbol (number);
1508
1509          if ((symbol.type == SymbolSet) ||
1510              (symbol.type == SymbolAlias) ||
1511              (symbol.type == SymbolSetupScript) ||
1512              (symbol.type == SymbolPath) ||
1513              (symbol.type == SymbolMacro))
1514            {
1515              if (symbol.print_macro (mode))
1516                {
1517                  //              cout << endl;
1518                }
1519            }
1520        }
1521      break;
1522    default :
1523      for (number = 0; number < Symbol::symbol_number (); number++)
1524        {
1525          Symbol& symbol = Symbol::symbol (number);
1526         
1527          if ((symbol.type == SymbolSet) ||
1528              (symbol.type == SymbolAlias) ||
1529              (symbol.type == SymbolSetupScript) ||
1530              (symbol.type == SymbolPath))
1531            {
1532              if (symbol.print (mode))
1533                {
1534                  if (mode == Bat)
1535                    {
1536                      cout << endl;
1537                    }
1538                  else if (mode == Xml)
1539                    {
1540                      //                      cout << endl;
1541                    }
1542                  else
1543                    {
1544                      cout << endl;
1545                    }
1546                }
1547            }
1548        }
1549      break;
1550    }
1551}
1552
1553//-------------------------------------------------------------
1554void Symbol::check_all_paths ()
1555{
1556  static SymbolVector& Symbols = symbols ();
1557
1558  int number;
1559
1560  if (Symbols.size () == 0) return;
1561
1562  for (number = 0; number < Symbol::symbol_number (); number++)
1563    {
1564      Symbol& symbol = Symbol::symbol (number);
1565
1566      if (symbol.type == SymbolPath)
1567        {
1568          cmt_string temp;
1569
1570          temp = symbol.build_macro_value ();
1571          expand (temp);
1572
1573          Symbol::filter_path_value (symbol.name, temp);
1574        }
1575    }
1576}
1577
1578//-------------------------------------------------------------
1579void Symbol::all_print_clean (PrintMode mode)
1580{
1581  static SymbolVector& Symbols = symbols ();
1582
1583  int number;
1584
1585  if (Symbols.size () == 0) return;
1586
1587  for (number = Symbols.size () - 1; number >= 0; number--)
1588    {
1589      Symbol& symbol = Symbols[number];
1590
1591      if ((symbol.type == SymbolSet) ||
1592          (symbol.type == SymbolAlias) ||
1593          (symbol.type == SymbolCleanupScript))
1594        {
1595          if (symbol.print_clean (mode))
1596            {
1597              cout << endl;
1598            }
1599        }
1600    }
1601
1602  for (number = Symbols.size () - 1; number >= 0; number--)
1603    {
1604      Symbol& symbol = Symbols[number];
1605
1606      if ((symbol.type != SymbolPath)) continue;
1607
1608      if (symbol.print_clean (mode))
1609        {
1610          cout << endl;
1611        }
1612    }
1613}
1614
1615//-------------------------------------------------------------
1616int Symbol::print_clean (PrintMode mode)
1617{
1618  int result = 0;
1619  static cmt_string temp;
1620
1621  if (name == "CMTCONFIG") return (0);
1622
1623  cmt_string _name;
1624  switch (type)
1625    {
1626    case SymbolSet :
1627    case SymbolPath :
1628      if (0 != CmtSystem::mangle (name, _name))
1629        CmtMessage::info ("Replace " + name + " with " + _name);
1630      break;
1631    }
1632
1633  switch (type)
1634    {
1635    case SymbolSet :
1636      switch (mode)
1637        {
1638        case Csh :
1639          cout << "unsetenv " << _name;
1640          result = 1;
1641          break;
1642        case Sh :
1643          cout << "unset " << _name;
1644          result = 1;
1645          break;
1646        case Bat :
1647          cout << "set " << _name << "=";
1648          result = 1;
1649          break;
1650        }
1651      break;
1652    case SymbolAlias :
1653      switch (mode)
1654        {
1655          case Csh :
1656            cout << "unalias " << name;
1657            result = 1;
1658            break;
1659          case Sh :
1660            cout << "unset " << name;
1661            result = 1;
1662            break;
1663        }
1664      break;
1665    case SymbolPath :
1666      temp = clean_macro_value ();
1667      switch (mode)
1668        {
1669        case Csh :
1670          if (temp == "")
1671            {
1672              cout << "unsetenv " << _name;
1673            }
1674          else
1675            {
1676              cout << "setenv " << _name << " " << CmtSystem::quote (temp, " \t");
1677            }
1678          result = 1;
1679          break;
1680        case Sh :
1681          cout << _name << "=" << CmtSystem::quote (temp, " \t") << "; export " << _name;
1682          result = 1;
1683          break;
1684        case Bat :
1685          cout << "set " << _name << "=" << CmtSystem::quote (temp, " \t");
1686          result = 1;
1687          break;
1688        }
1689      break;
1690    case SymbolCleanupScript :
1691      switch (mode)
1692        {
1693        case Csh :
1694          cout << "if ( -f " << CmtSystem::quote (name, " \t") << ".csh ) then" << endl;
1695          cout << "  source " << CmtSystem::quote (name, " \t") << ".csh" << endl;
1696          cout <<
1697            "if ( $status != 0 ) then\n"
1698            "    set cmtcleanupstatus=1\n"
1699            "endif\n";
1700          cout << "endif" << endl;
1701          result = 1;
1702          break;
1703        case Sh :
1704          cout << "if test -f " << CmtSystem::quote (name, " \t") << ".sh; then" << endl;
1705          cout << "  . " << CmtSystem::quote (name, " \t") << ".sh" << endl;
1706          cout <<
1707            "if test $? != 0; then\n"
1708            "    cmtcleanupstatus=1\n"
1709            "fi\n";
1710          cout << "fi" << endl;
1711          result = 1;
1712          break;
1713        case Bat :
1714          cout << "call " << CmtSystem::quote (name, " \t");
1715          result = 1;
1716          break;
1717        }
1718      break;
1719    }
1720
1721  return (result);
1722}
1723
1724//-------------------------------------------------------------
1725int Symbol::print (PrintMode mode)
1726{
1727  int result = 0;
1728
1729  cmt_string temp = build_macro_value ();
1730  bool empty = (temp.size () == 0) ? true : false;
1731
1732  if (type == SymbolPath)
1733    {
1734      expand (temp);
1735      Symbol::filter_path_value (name, temp);
1736    }
1737
1738  cmt_string _name;
1739  switch (type)
1740    {
1741      case SymbolSet :
1742      case SymbolPath :
1743        if (0 != CmtSystem::mangle (name, _name))
1744          CmtMessage::info ("Replace " + name + " with " + _name);
1745        switch (mode)
1746          {
1747            case Csh :
1748              if (empty) cout << "unsetenv " << _name;
1749              else cout << "setenv " << _name << " \"" << temp << "\"";
1750              //else cout << "setenv " << name << " " << CmtSystem::quote (temp, " \t");
1751
1752              result = 1;
1753              break;
1754            case Sh :
1755              if (empty) cout << "unset " << _name;
1756              else cout << _name << "=\"" << temp << "\"; export " << _name;
1757              //else cout << name << "=" << CmtSystem::quote (temp, " \t") << "; export " << name;
1758
1759              result = 1;
1760              break;
1761            case Bat :
1762              temp.replace_all ("/", "\\");
1763              cout << "set " << _name << "=" << CmtSystem::quote (temp, " \t");
1764              //cout << "set " << name << "=" << temp;
1765              result = 1;
1766              break;
1767            case Xml :
1768              cout << "<variable><name>" << _name << "</name>"
1769                   << "<value>" << temp << "</value></variable>";
1770              result = 1;
1771              break;
1772          }
1773        break;
1774      case SymbolAlias :
1775        switch (mode)
1776          {
1777            case Csh :
1778              cout << "alias " << name <<
1779                " \"" << temp << "\"";
1780              //CmtSystem::quote (temp, " \t");
1781              result = 1;
1782              break;
1783            case Sh :
1784              cout << "alias " << name <<
1785                "=\"" << temp << "\"";
1786              //"=" << CmtSystem::quote (temp, " \t");
1787              result = 1;
1788              break;
1789            case Bat :
1790              cout << "set " << name <<
1791                "=" << CmtSystem::quote (temp, " \t");
1792              //"=" << temp;
1793              result = 1;
1794              break;
1795            case Xml :
1796              cout << "<alias><name>" << name << "</name>"
1797                   << "<value>" << temp << "</value></alias>";
1798              result = 1;
1799              break;
1800          }
1801        break;
1802      default :
1803        break;
1804    }
1805
1806  if (temp != "")
1807    {
1808      switch (type)
1809        {
1810          case SymbolSetupScript :
1811            switch (mode)
1812              {
1813                case Csh :
1814                  cout << "if ( -f " << CmtSystem::quote (name, " \t") << ".csh ) then" << endl;
1815                  cout << "  source " << CmtSystem::quote (name, " \t") << ".csh" << endl;
1816                  cout <<
1817                    "if ( $status != 0 ) then\n"
1818                    "    set cmtsetupstatus=1\n"
1819                    "endif\n";
1820                  cout << "endif" << endl;
1821                  result = 1;
1822                  break;
1823                case Sh :
1824                  cout << "if test -f " << CmtSystem::quote (name, " \t") << ".sh; then" << endl;
1825                  cout << "  . " << CmtSystem::quote (name, " \t") << ".sh" << endl;
1826                  cout <<
1827                    "if test $? != 0; then\n"
1828                    "    cmtsetupstatus=1\n"
1829                    "fi\n";
1830                  cout << "fi" << endl;
1831                  result = 1;
1832                  break;
1833                case Bat :
1834                  cout << "call " << CmtSystem::quote (name, " \t");
1835                  result = 1;
1836                  break;
1837                case Xml :
1838                  cout << "<script>" << name << "</script>";
1839                  result = 1;
1840                  break;
1841              }
1842            break;
1843          default:
1844            break;
1845        }
1846    }
1847
1848  return (result);
1849}
1850
1851//-------------------------------------------------------------
1852cmt_string Symbol::build_macro_value (bool display_it) const
1853{
1854  cmt_string temp;
1855
1856  if (display_it)
1857    {
1858      temp = builder->build_and_display (*this);
1859    }
1860  else
1861    {
1862      temp = builder->build (*this);
1863    }
1864
1865  return (temp);
1866}
1867
1868//-------------------------------------------------------------
1869int Symbol::print_macro (PrintMode mode, ostream& out) const
1870{
1871  if (mode != Requirements || printed) return 0;
1872  return builder->print (*this, out);
1873}
1874
1875//-------------------------------------------------------------
1876cmt_string Symbol::clean_macro_value () const
1877{
1878  cmt_string temp;
1879
1880  temp = builder->clean (*this);
1881
1882  return (temp);
1883}
1884
1885/**
1886
1887   Attempt to substitute into the symbol value all occurrences of
1888
1889      ${xxx}
1890      $(xxx)
1891      `xxx`
1892      %xxx%    [on Windows only]
1893
1894      by the appropriate value:
1895
1896        for `xxx` :
1897
1898            xxx is considered as a shell command. Value is the result of its execution
1899
1900        for other patterns:
1901
1902            if xxx is a symbol name, its value is substituted to the pattern
1903            otherwise, xxx is tried as an environment variable
1904
1905
1906     ===> In all cases, the pattern is filtered away.
1907
1908  */
1909cmt_string Symbol::resolve_macro_value (const cmt_string& tag_name)
1910{
1911  cmt_string temp = builder->build (*this, tag_name);
1912
1913  resolve_value (temp);
1914
1915  return (temp);
1916}
1917
1918//-------------------------------------------------------------
1919void Symbol::show_macro (PrintMode mode, ostream& out)
1920//void Symbol::show_macro (PrintMode mode)
1921{
1922  if (Cmt::get_debug ())
1923    {
1924      cout << "Symbol::show_macro> " << name << endl;
1925    }
1926
1927  ActionType action = Cmt::get_action ();
1928
1929  cmt_string value = build_macro_value (true);
1930
1931  if ((!Cmt::get_quiet ()) &&
1932      (action != action_build_tag_makefile) &&
1933      (action != action_build_constituents_config) &&
1934      (action != action_build_constituent_config) &&
1935      (action != action_show_macros) &&
1936      (action != action_show_actions) &&
1937      (action != action_show_sets))
1938    {
1939      out << "#" << endl;
1940      out << "# Selection : " << endl;
1941    }
1942
1943  if (value.size () > 0)
1944    {
1945      if ((action == action_show_macro) ||
1946          (action == action_show_macros) ||
1947          (action == action_show_sets) ||
1948          (action == action_show_set) ||
1949          (action == action_show_actions) ||
1950          (action == action_show_action) ||
1951          (action == action_build_tag_makefile) ||
1952          (action == action_build_constituents_config) ||
1953          (action == action_build_constituent_config) ||
1954          (action == action_load) ||
1955          (!Cmt::get_quiet ()))
1956        {
1957          if (mode == Make)
1958            {
1959              out << name << "=";
1960            }
1961          else
1962            {
1963              out << name << "='";
1964            }
1965        }
1966
1967      if ((action == action_show_macro_value) ||
1968          (action == action_show_set_value) ||
1969          (action == action_show_action_value))
1970        {
1971          expand (value);
1972        }
1973      else if (action == action_build_tag_makefile ||
1974               action == action_build_constituents_config ||
1975               action == action_build_constituent_config)
1976        {
1977            /*
1978               Unfortunately, the %xxx% pattern has to be kept on Unix. Therefore
1979               we cannot force all patterns to become $(xxx)
1980
1981               This was useful on Windows so as to only keep $(xxx)
1982             */
1983#ifdef WIN32
1984          suppress_OS_delimiters (value);
1985#endif
1986        }
1987
1988      out << value;
1989
1990      if ((action == action_show_macro) ||
1991          (action == action_show_macros) ||
1992          (action == action_show_sets) ||
1993          (action == action_show_set) ||
1994          (action == action_show_actions) ||
1995          (action == action_show_action) ||
1996          (action == action_build_tag_makefile) ||
1997          (action == action_build_constituents_config) ||
1998          (action == action_build_constituent_config) ||
1999          (action == action_load) ||
2000          (!Cmt::get_quiet ()))
2001        {
2002          if (mode != Make)
2003            {
2004              out << "'";
2005            }
2006#ifdef WIN32
2007          else
2008            {
2009              out << " ";
2010            }
2011#endif
2012        }
2013
2014      out << endl;
2015    }
2016}
2017
2018//-------------------------------------------------------------
2019void Symbol::clear_all ()
2020{
2021  static SymbolVector& Symbols = symbols ();
2022  static SymbolMap& SymbolMap = symbol_map ();
2023
2024  SymbolMap.clear ();
2025  Symbols.clear ();
2026}
2027
2028//-------------------------------------------------------------
2029void Symbol::expand (cmt_string& text)
2030{
2031  static cmt_regexp reg ("[$%`]");
2032
2033  if (!reg.match (text)) return;
2034
2035  resolve_value (text);
2036}
2037
2038//-------------------------------------------------------------
2039ValueBuilder::ValueBuilder ()
2040{
2041  m_display_it = false;
2042}
2043
2044//-------------------------------------------------------------
2045const cmt_string ValueBuilder::build_and_display (const Symbol& symbol)
2046{
2047  cmt_string temp;
2048
2049  m_display_it = true;
2050  temp = build (symbol);
2051  m_display_it = false;
2052
2053  return (temp);
2054}
2055
2056//-------------------------------------------------------------
2057int ValueBuilder::print (const Symbol& symbol, ostream& out)
2058{
2059  int result (0);
2060
2061  bool first_definition = true;
2062 
2063  for (int i = 0; i < symbol.value_lists.size (); i++)
2064    {
2065      const SymbolValueList& value_list = symbol.value_lists[i];
2066
2067      if ((value_list.use != 0) &&
2068          (value_list.use->discarded)) continue;
2069
2070      const int selected = value_list.select_first ();
2071
2072      if (selected < 0) continue;
2073
2074      SymbolValue& value = value_list.values[selected];
2075
2076      result += value_list.print (symbol, value, first_definition);
2077    }
2078
2079  return result;
2080}
2081
2082//-------------------------------------------------------------
2083const cmt_string SetBuilder::build (const Symbol& symbol,
2084                                    const cmt_string& /*tag_name*/)
2085{
2086    // Control of recursivity
2087  static int level = 0;
2088
2089  bool show_it = false;
2090
2091  cmt_string temp;
2092  cmt_string previous_temp;
2093  cmt_string new_value;
2094  static const cmt_string empty;
2095
2096  ActionType action = Cmt::get_action ();
2097
2098  if (action == action_show_set)
2099    {
2100      if (symbol.name == Cmt::get_current_target ())
2101        {
2102             // Should not display on recursive calls
2103          if (level == 0) show_it = m_display_it;
2104        }
2105    }
2106
2107  level++;
2108
2109  temp = "";
2110
2111  bool first_definition = true;
2112
2113  for (int i = 0; i < symbol.value_lists.size (); i++)
2114    {
2115      const SymbolValueList& value_list = symbol.value_lists[i];
2116
2117      if ((value_list.use != 0) &&
2118          (value_list.use->discarded)) continue;
2119
2120      const int selected = value_list.select_first ();
2121
2122      if (selected < 0) continue;
2123
2124      SymbolValue& value = value_list.values[selected];
2125
2126      if (show_it && !Symbol::get_inhibit_display())
2127        {
2128          value_list.show (symbol, value, first_definition);
2129        }
2130     
2131      if (value_list.discarded) continue;
2132
2133        //
2134        // One should accumulate values if it refers to
2135        // itself.
2136        //
2137     
2138      new_value = value.text;
2139     
2140      resolve_value_for_macros (new_value);
2141     
2142      switch (value_list.command_type)
2143        {
2144          case CommandSet :
2145
2146            if (!value_list.is_reflexive || 
2147                !symbol.value_is_reflexive (value.text))
2148              {
2149                resolve_value (new_value, symbol.name, temp);
2150                temp = new_value;
2151              }
2152            else if (temp == "")
2153              {
2154                temp = CmtSystem::getenv (symbol.name);
2155              }
2156
2157            break;
2158          case CommandSetAppend :
2159           
2160            if (new_value != "")
2161              {
2162                temp += new_value;
2163              }
2164           
2165            break;
2166          case CommandSetPrepend :
2167           
2168            if (new_value != "")
2169              {
2170                previous_temp = temp;
2171                temp = new_value;
2172                temp += previous_temp;
2173              }
2174           
2175            break;
2176          case CommandSetRemove :
2177           
2178            if (new_value != "")
2179              {
2180                temp.replace_all (new_value, empty);
2181              }
2182           
2183            break;
2184          case CommandSetRemoveRegexp :
2185           
2186            if (new_value != "")
2187              {
2188                cmt_regexp e (new_value);
2189                cmt_regexp::iterator it;
2190
2191                for (;;)
2192                  {
2193                    it = e.begin (temp);
2194                    if (it == e.end ()) break;
2195
2196                    temp.erase (it._pos, it._length);
2197                  }
2198              }
2199           
2200            break;
2201          case CommandAlias :
2202           
2203            resolve_value (new_value, symbol.name, temp);
2204            temp = new_value;
2205           
2206            break;
2207        }
2208    }
2209
2210  level--;
2211
2212  return (temp);
2213}
2214
2215static bool find_path_entry (const cmt_string& paths, const cmt_string& value)
2216{
2217  static const cmt_string path_separator = CmtSystem::path_separator ();
2218
2219  cmt_string here = CmtSystem::pwd ();
2220  cmt_string rvalue = value;
2221
2222  if (CmtSystem::cd (value))
2223    {
2224      rvalue = CmtSystem::pwd ();
2225    }
2226  else
2227    {
2228      CmtSystem::compress_path (rvalue);
2229    }
2230
2231  CmtSystem::cmt_string_vector items;
2232  CmtSystem::split (paths, path_separator, items);
2233
2234  bool found = false;
2235
2236  for (int i = 0; i < items.size (); i++)
2237    {
2238      const cmt_string& item = items[i];
2239      cmt_string ritem = item;
2240      if (CmtSystem::cd (item))
2241        {
2242          ritem = CmtSystem::pwd ();
2243        }
2244      else
2245        {
2246          CmtSystem::compress_path (ritem);
2247        }
2248
2249      if (ritem == rvalue)
2250        {
2251          found = true;
2252          break;
2253        }
2254    }
2255
2256  CmtSystem::cd (here);
2257  return (found);
2258}
2259
2260//-------------------------------------------------------------
2261const cmt_string PathBuilder::build (const Symbol& symbol,
2262                                     const cmt_string& /*tag_name*/)
2263{
2264    // Control of recursivity
2265  static int level = 0;
2266
2267  bool show_it = false;
2268
2269  cmt_string temp;
2270  cmt_string previous_temp;
2271  cmt_string new_value;
2272  static const cmt_string empty;
2273
2274  static cmt_string path_separator = CmtSystem::path_separator ();
2275
2276  ActionType action = Cmt::get_action ();
2277
2278  if (action == action_show_set)
2279    {
2280      if (symbol.name == Cmt::get_current_target ())
2281        {
2282            // Should not display on recursive calls
2283          if (level == 0) show_it = m_display_it;
2284        }
2285    }
2286
2287  level++;
2288
2289  temp = CmtSystem::getenv (symbol.name);
2290
2291  bool first_definition = true;
2292
2293  for (int i = 0; i < symbol.value_lists.size (); i++)
2294    {
2295      const SymbolValueList& value_list = symbol.value_lists[i];
2296
2297      if ((value_list.use != 0) &&
2298          (value_list.use->discarded)) continue;
2299
2300      const int selected = value_list.select_first ();
2301     
2302      if (selected < 0) continue;
2303 
2304      SymbolValue& value = value_list.values[selected];
2305         
2306      if (show_it && !Symbol::get_inhibit_display())
2307        {
2308          value_list.show (symbol, value, first_definition);
2309        }
2310         
2311      if (value_list.discarded) continue;
2312
2313      new_value = value.text;
2314         
2315          //resolve_value (new_value);
2316      resolve_value_for_macros (new_value);
2317         
2318      switch (value_list.command_type)
2319        {
2320          case CommandPath :
2321           
2322            if (!value_list.is_reflexive || 
2323                !symbol.value_is_reflexive (value.text))
2324              {
2325                resolve_value (new_value, symbol.name, temp);
2326                temp = new_value;
2327              }
2328
2329            break;
2330          case CommandPathAppend :
2331             
2332            if (new_value != "")
2333              {
2334                if (!find_path_entry (temp, new_value))
2335                  {
2336                    if (temp != "") temp += path_separator;
2337                     
2338                    temp += new_value;
2339                  }
2340              }
2341                 
2342            break;
2343          case CommandPathPrepend :
2344             
2345            if (new_value != "")
2346              {
2347                if (!find_path_entry (temp, new_value))
2348                  {
2349                    previous_temp = temp;
2350                    temp = new_value;
2351                    if (previous_temp != "") temp += path_separator;
2352                    temp += previous_temp;
2353                  }
2354              }
2355                 
2356            break;
2357          case CommandPathRemove :
2358             
2359            if (new_value != "")
2360              {
2361                CmtSystem::cmt_string_vector paths;
2362                 
2363                CmtSystem::split (temp, path_separator, paths);
2364                 
2365                for (int j = 0; j < paths.size (); ++j)
2366                  {
2367                    cmt_string& s = paths[j];
2368                     
2369                    if (s.find (new_value) != cmt_string::npos)
2370                      {
2371                        s = "";
2372                      }
2373                  }
2374
2375                Cmt::vector_to_string (paths, path_separator, temp);
2376              }
2377             
2378            break;
2379          case CommandPathRemoveRegexp :
2380
2381            if (new_value != "")
2382              {
2383                cmt_regexp e (new_value);
2384
2385                CmtSystem::cmt_string_vector paths;
2386                 
2387                CmtSystem::split (temp, path_separator, paths);
2388                 
2389                for (int j = 0; j < paths.size (); ++j)
2390                  {
2391                    cmt_string& s = paths[j];
2392
2393                    if (CmtSystem::getenv ("TESTPRR") != "")
2394                      {
2395                        cerr << "PRR> s=[" << s << "]";
2396                      }
2397
2398                    if (e.match (s))
2399                      {
2400                        s = "";
2401
2402                        if (CmtSystem::getenv ("TESTPRR") != "")
2403                          {
2404                            cerr << " match ";
2405                          }
2406                      }
2407                    else
2408                      {
2409                        if (CmtSystem::getenv ("TESTPRR") != "")
2410                          {
2411                            cerr << " no match ";
2412                          }
2413                      }
2414
2415                    if (CmtSystem::getenv ("TESTPRR") != "")
2416                      {
2417                        cerr << endl;
2418                      }
2419                  }
2420
2421                Cmt::vector_to_string (paths, path_separator, temp);
2422              }
2423             
2424            break;
2425        }
2426
2427    }
2428
2429  level--;
2430
2431  for (;;)
2432    {
2433      int sz = temp.size ();
2434
2435      if (sz == 0) break;
2436
2437      if ((temp[0] == ';') || (temp[0] == ':'))
2438        {
2439          temp.erase (0, 1);
2440        }
2441      else if ((temp[sz-1] == ';') || (temp[sz-1] == ':'))
2442        {
2443          temp.erase (sz-1, 1);
2444        }
2445      else
2446        {
2447          break;
2448        }
2449    }
2450
2451  temp.replace_all ("::", ":");
2452  temp.replace_all (";;", ";");
2453 
2454  return (temp);
2455}
2456
2457//-------------------------------------------------------------
2458const cmt_string PathBuilder::clean (const Symbol& symbol,
2459                                     const cmt_string& /*tag_name*/)
2460{
2461    // Control of recursivity
2462  static int level = 0;
2463
2464  cmt_string temp;
2465  cmt_string new_value;
2466  static const cmt_string empty;
2467
2468  static cmt_string path_separator = CmtSystem::path_separator ();
2469
2470  temp = CmtSystem::getenv (symbol.name);
2471
2472    //cerr << "#####1 temp=" << temp << endl;
2473
2474  for (int i = 0; i < symbol.value_lists.size (); i++)
2475    {
2476      const SymbolValueList& value_list = symbol.value_lists[i];
2477
2478      if (value_list.discarded) continue;
2479
2480      if ((value_list.use != 0) &&
2481          (value_list.use->discarded)) continue;
2482
2483      const int selected = value_list.select_first ();
2484     
2485      if (selected < 0) continue;
2486 
2487      SymbolValue& value = value_list.values[selected];
2488         
2489      new_value = value.text;
2490         
2491          //resolve_value (new_value);
2492
2493        //cerr << "#####1 new_value=" << new_value << endl;
2494
2495      resolve_value_for_macros (new_value);
2496      resolve_value (new_value);
2497         
2498        //cerr << "#####2 new_value=" << new_value << endl;
2499
2500      switch (value_list.command_type)
2501        {
2502          case CommandPath :
2503           
2504            temp = "";
2505           
2506            break;
2507          case CommandPathAppend :
2508          case CommandPathPrepend :
2509          case CommandPathRemove :
2510
2511            if (new_value != "")
2512              {
2513                CmtSystem::cmt_string_vector paths;
2514                 
2515                CmtSystem::split (temp, path_separator, paths);
2516                 
2517                for (int j = 0; j < paths.size (); ++j)
2518                  {
2519                    cmt_string& s = paths[j];
2520                     
2521                    if (s.find (new_value) != cmt_string::npos)
2522                      {
2523                        s = "";
2524                      }
2525
2526                    if (j > 0)
2527                      {
2528                        cmt_string& s2 = paths[j-1];
2529                        if (s2 == s)
2530                          {
2531                            s2 = "";
2532                          }
2533                      }
2534                  }
2535
2536                Cmt::vector_to_string (paths, path_separator, temp);
2537                temp.replace_all ("::", ":");
2538                temp.replace_all (";;", ";");
2539              }
2540             
2541            break;
2542          case CommandPathRemoveRegexp :
2543
2544            if (new_value != "")
2545              {
2546                cmt_regexp e (new_value);
2547
2548                CmtSystem::cmt_string_vector paths;
2549                 
2550                CmtSystem::split (temp, path_separator, paths);
2551                 
2552                for (int j = 0; j < paths.size (); ++j)
2553                  {
2554                    cmt_string& s = paths[j];
2555                     
2556                    if (e.match (s))
2557                      {
2558                        s = "";
2559                      }
2560
2561                    if (j > 0)
2562                      {
2563                        cmt_string& s2 = paths[j-1];
2564                        if (s2 == s)
2565                          {
2566                            s2 = "";
2567                          }
2568                      }
2569                  }
2570
2571                Cmt::vector_to_string (paths, path_separator, temp);
2572                temp.replace_all ("::", ":");
2573                temp.replace_all (";;", ";");
2574              }
2575             
2576            break;
2577        }
2578    }
2579
2580    //cerr << "#####2 temp=" << temp << endl;
2581 
2582  return (temp);
2583}
2584
2585//-------------------------------------------------------------
2586const cmt_string MacroBuilder::build (const Symbol& symbol,
2587                                      const cmt_string& tag_name)
2588{
2589    // Control of recursivity
2590  static int level = 0;
2591
2592  cmt_string temp;
2593  cmt_string previous_temp;
2594  static const cmt_string empty;
2595  bool show_it = false;
2596
2597  ActionType action = Cmt::get_action ();
2598
2599  if (action == action_show_macro)
2600    {
2601      if (symbol.name == Cmt::get_current_target ())
2602        {
2603             // Should not display on recursive calls
2604          if (level == 0) show_it = m_display_it;
2605        }
2606    }
2607
2608  level++;
2609
2610  temp = "";
2611
2612  int i;
2613
2614  bool first_definition = true;
2615
2616  for (i = 0; i < symbol.value_lists.size (); i++)
2617    {
2618      const SymbolValueList& value_list = symbol.value_lists[i];
2619
2620      if ((value_list.use != 0) &&
2621          (value_list.use->discarded)) continue;
2622
2623      if (value_list.command_type != CommandMacroPrepend) continue;
2624
2625      const int selected = value_list.select_first (tag_name);
2626
2627      if (selected < 0) continue;
2628
2629      SymbolValue& value = value_list.values[selected];
2630
2631      if (show_it && !Symbol::get_inhibit_display())
2632        {
2633          value_list.show (symbol, value, first_definition);
2634        }
2635
2636      if (value_list.discarded) continue;
2637
2638      previous_temp = temp;
2639      temp = value.text;
2640      temp += previous_temp;
2641    }
2642
2643  previous_temp = temp;
2644  temp = "";
2645
2646  first_definition = true;
2647
2648  for (i = 0; i < symbol.value_lists.size (); i++)
2649    {
2650      const SymbolValueList& value_list = symbol.value_lists[i];
2651
2652      if ((value_list.use != 0) &&
2653          (value_list.use->discarded)) continue;
2654
2655      if (value_list.command_type != CommandMacro) continue;
2656
2657      const int selected = value_list.select_first (tag_name);
2658
2659      if (selected < 0) continue;
2660
2661      SymbolValue& value = value_list.values[selected];
2662
2663      if (show_it && !Symbol::get_inhibit_display())
2664        {
2665          value_list.show (symbol, value, first_definition);
2666        }
2667
2668      // WARNING:
2669      // Commented just for a test : should be uncommented after the test
2670      if (value_list.discarded) continue;
2671     
2672      if (!value_list.is_reflexive || 
2673          !symbol.value_is_reflexive (value.text))
2674        {
2675          temp = value.text;
2676        }
2677    }
2678
2679  previous_temp += temp;
2680  temp = previous_temp;
2681
2682  for (i = 0; i < symbol.value_lists.size (); i++)
2683    {
2684      const SymbolValueList& value_list = symbol.value_lists[i];
2685
2686      if ((value_list.use != 0) &&
2687          (value_list.use->discarded)) continue;
2688
2689      if (value_list.command_type != CommandMacroAppend) continue;
2690
2691      const int selected = value_list.select_first (tag_name);
2692
2693      if (selected < 0) continue;
2694
2695      SymbolValue& value = value_list.values[selected];
2696
2697      if (show_it && !Symbol::get_inhibit_display())
2698        {
2699          value_list.show (symbol, value, first_definition);
2700        }
2701
2702      if (value_list.discarded) continue;
2703
2704      temp += value.text;
2705    }
2706
2707  for (i = 0; i < symbol.value_lists.size (); i++)
2708    {
2709      const SymbolValueList& value_list = symbol.value_lists[i];
2710
2711      if ((value_list.use != 0) &&
2712          (value_list.use->discarded)) continue;
2713
2714      if ((value_list.command_type != CommandMacroRemove) &&
2715          (value_list.command_type != CommandMacroRemoveRegexp) &&
2716          (value_list.command_type != CommandMacroRemoveAll) &&
2717          (value_list.command_type != CommandMacroRemoveAllRegexp)) continue;
2718
2719      const int selected = value_list.select_first (tag_name);
2720
2721      if (selected < 0) continue;
2722
2723      SymbolValue& value = value_list.values[selected];
2724
2725      if (show_it && !Symbol::get_inhibit_display())
2726        {
2727          value_list.show (symbol, value, first_definition);
2728        }
2729
2730      if (value_list.discarded) continue;
2731
2732      switch (value_list.command_type)
2733        {
2734          case CommandMacroRemove :
2735            temp.replace (value.text, empty);
2736            break;
2737          case CommandMacroRemoveRegexp :
2738            if (value.text != "")
2739              {
2740                cmt_regexp e (value.text);
2741                cmt_regexp::iterator it;
2742
2743                it = e.begin (temp);
2744                if (it != e.end ())
2745                  {
2746                    temp.erase (it._pos, it._length);
2747                  }
2748              }
2749            break;
2750          case CommandMacroRemoveAll :
2751            temp.replace_all (value.text, empty);
2752            break;
2753          case CommandMacroRemoveAllRegexp :
2754            if (value.text != "")
2755              {
2756                cmt_regexp e (value.text);
2757                cmt_regexp::iterator it;
2758
2759                for (;;)
2760                  {
2761                    it = e.begin (temp);
2762                    if (it != e.end ())
2763                      {
2764                        temp.erase (it._pos, it._length);
2765                      }
2766                    else
2767                      {
2768                        break;
2769                      }
2770                  }
2771              }
2772            break;
2773        }
2774    }
2775
2776  level--;
2777
2778  return (temp);
2779}
2780
2781//-------------------------------------------------------------
2782const cmt_string ScriptBuilder::build (const Symbol& symbol,
2783                                       const cmt_string& tag_name)
2784{
2785    // Control of recursivity
2786  static int level = 0;
2787
2788  static const cmt_string empty = "";
2789
2790  if (symbol.value_lists.size () > 0)
2791    {
2792      const SymbolValueList& value_list = symbol.value_lists[0];
2793
2794      if (value_list.discarded) return (empty);
2795
2796      if ((value_list.use != 0) &&
2797          (value_list.use->discarded)) return (empty);
2798    }
2799
2800  return (symbol.name);
2801}
2802
2803//-------------------------------------------------------------
2804const cmt_string ActionBuilder::build (const Symbol& symbol,
2805                                       const cmt_string& tag_name)
2806{
2807    // Control of recursivity
2808  static int level = 0;
2809
2810  cmt_string temp;
2811  cmt_string previous_temp;
2812  static const cmt_string empty;
2813  bool show_it = false;
2814
2815  ActionType action = Cmt::get_action ();
2816
2817  if (action == action_show_action)
2818    {
2819      if (symbol.name == Cmt::get_current_target ())
2820        {
2821             // Should not display on recursive calls
2822          if (level == 0) show_it = m_display_it;
2823        }
2824    }
2825
2826  level++;
2827
2828  int i;
2829
2830  bool first_definition = true;
2831
2832  temp = "";
2833
2834  for (i = 0; i < symbol.value_lists.size (); i++)
2835    {
2836      const SymbolValueList& value_list = symbol.value_lists[i];
2837
2838      if ((value_list.use != 0) &&
2839          (value_list.use->discarded)) continue;
2840
2841      if (value_list.command_type != CommandAction) continue;
2842
2843      const int selected = value_list.select_first (tag_name);
2844
2845      if (selected < 0) continue;
2846
2847      SymbolValue& value = value_list.values[selected];
2848
2849      if (show_it && !Symbol::get_inhibit_display())
2850        {
2851          value_list.show (symbol, value, first_definition);
2852        }
2853
2854      // WARNING:
2855      // Commented just for a test : should be uncommented after the test
2856      if (value_list.discarded) continue;
2857     
2858      if (!value_list.is_reflexive || 
2859          !symbol.value_is_reflexive (value.text))
2860        {
2861          temp = value.text;
2862        }
2863    }
2864
2865  level--;
2866
2867  return (temp);
2868}
2869
2870//-------------------------------------------------------------
2871int SymbolValueList::select_first (const cmt_string& tag_name) const
2872{
2873  int priority = 0;
2874  int value_number;
2875  int selected = -1;
2876
2877  Tag* the_tag = 0;
2878
2879  if (tag_name != "") the_tag = Tag::find (tag_name);
2880
2881  for (value_number = 0;
2882       value_number < values.size ();
2883       value_number++)
2884    {
2885      const SymbolValue& value = values[value_number];
2886
2887      const Tag* tag = value.tag;
2888
2889      if (the_tag == 0)
2890        {
2891          if (!tag->is_selected ()) continue;
2892          selected = value_number;
2893          if (tag == Tag::get_default ())
2894            continue;
2895        }
2896      else
2897        {
2898          if (tag != the_tag) continue;
2899          selected = value_number;
2900        }
2901
2902      //
2903      // Only the value for the first alternative (i.e. non-default)
2904      // matching tag expression is
2905      // selected (which means the selection stops here)
2906      //
2907      break;
2908
2909      //
2910      // Only the first value at a given priority is
2911      // selected (which implies the '>' test instead
2912      // of '>=')
2913      //
2914      /*
2915      if (tag->get_priority () > priority)
2916        {
2917          priority = tag->get_priority ();
2918          selected = value_number;
2919        }
2920      */
2921    }
2922
2923  return (selected);
2924}
2925
2926//-------------------------------------------------------------
2927int SymbolValueList::select_last () const
2928{
2929  int priority = 0;
2930  int value_number;
2931  int selected = -1;
2932
2933  for (value_number = 0;
2934       value_number < values.size ();
2935       value_number++)
2936    {
2937      SymbolValue& value = values[value_number];
2938
2939      const Tag* tag = value.tag;
2940
2941      if (tag->is_selected ())
2942        {
2943          //
2944          // The last value at a given priority is
2945          // selected (which implies the '>=' test instead
2946          // of '>')
2947          //
2948
2949          if (tag->get_priority () >= priority)
2950            {
2951              priority = tag->get_priority ();
2952              selected = value_number;
2953            }
2954        }
2955    }
2956
2957  return (selected);
2958}
2959
2960//-------------------------------------------------------------
2961void SymbolValueList::show (const Symbol& symbol, 
2962                            const SymbolValue& value, 
2963                            bool& first_definition) const
2964{
2965  cmt_string discarded_text;
2966  cmt_string define_text;
2967  ActionType action = Cmt::get_action ();
2968
2969  switch (command_type)
2970    {
2971      //case CommandSet :
2972      case CommandSetAppend :
2973      case CommandSetPrepend :
2974      case CommandSetRemove :
2975      case CommandSetRemoveRegexp :
2976        //case CommandAlias :
2977        //case CommandPath :
2978      case CommandPathAppend :
2979      case CommandPathPrepend :
2980      case CommandPathRemove :
2981      case CommandPathRemoveRegexp :
2982      case CommandMacroPrepend :
2983        //case CommandMacro :
2984      case CommandMacroAppend :
2985      case CommandMacroRemove :
2986      case CommandMacroRemoveRegexp :
2987      case CommandMacroRemoveAll :
2988      case CommandMacroRemoveAllRegexp :
2989        //case CommandAction :
2990        if (value.text == "") return;
2991        break;
2992    }
2993
2994  if (discarded) discarded_text = " (discarded by override)";
2995  else discarded_text = "";
2996
2997  if (first_definition) define_text = "defines";
2998  else define_text = "overrides";
2999
3000  cout << "# Package ";
3001  if (use != 0)
3002    {
3003      cout << use->get_package_name () << " " << use->version;
3004    }
3005
3006  switch (command_type)
3007    {
3008      case CommandSet :
3009        cout << " " << define_text << " set " << symbol.name << " as ";
3010        first_definition = false;
3011        break;
3012      case CommandSetAppend :
3013        cout << " appends to set " << symbol.name << " : ";
3014        break;
3015      case CommandSetPrepend :
3016        cout << " prepends to set " << symbol.name << " : ";
3017        break;
3018      case CommandSetRemove :
3019        cout << " removes from set " << symbol.name << " : ";
3020        break;
3021      case CommandSetRemoveRegexp :
3022        cout << " removes RE from set " << symbol.name << " : ";
3023        break;
3024      case CommandAlias :
3025        cout << " " << define_text << " alias " << symbol.name << " as ";
3026        first_definition = false;
3027        break;
3028      case CommandPath :
3029        cout << " " << define_text << " path " << symbol.name << " as ";
3030        first_definition = false;
3031        break;
3032      case CommandPathAppend :
3033        cout << " appends to path " << symbol.name << " : ";
3034        break;
3035      case CommandPathPrepend :
3036        cout << " prepends to path " << symbol.name << " : ";
3037        break;
3038      case CommandPathRemove :
3039        cout << " removes from path " << symbol.name << " : ";
3040        break;
3041      case CommandPathRemoveRegexp :
3042        cout << " removes RE from path " << symbol.name << " : ";
3043        break;
3044      case CommandMacroPrepend :
3045        cout << " prepends to macro " << symbol.name << " : ";
3046        break;
3047      case CommandMacro :
3048        cout << " " << define_text << " macro " << symbol.name << " as ";
3049        break;
3050      case CommandMacroAppend :
3051        cout << " appends to macro " << symbol.name << " : ";
3052        break;
3053      case CommandMacroRemove :
3054        cout << " remove from macro " << symbol.name << " : ";
3055        break;
3056      case CommandMacroRemoveRegexp :
3057        cout << " remove RE from macro " << symbol.name << " : ";
3058        break;
3059      case CommandMacroRemoveAll :
3060        cout << " remove all from macro " << symbol.name << " : ";
3061        break;
3062      case CommandMacroRemoveAllRegexp :
3063        cout << " remove all RE from macro " << symbol.name << " : ";
3064        break;
3065      case CommandAction :
3066        cout << " " << define_text << " action " << symbol.name << " as ";
3067        first_definition = false;
3068        break;
3069    }
3070
3071  cout << "'" << value.text << "'";
3072         
3073  Tag* selected_tag = value.tag;
3074         
3075  if ((selected_tag == 0) ||
3076      (selected_tag == Tag::get_default ()))
3077    {
3078      cout << " for default tag";
3079    }
3080  else
3081    {
3082      cout << " for tag '" << selected_tag->get_name () << "'";
3083    }
3084 
3085  cout << discarded_text << endl;
3086}
3087
3088//-------------------------------------------------------------
3089int SymbolValueList::print (const Symbol& symbol, 
3090                            const SymbolValue& value, 
3091                            bool& first_definition) const
3092{
3093  int result (0);
3094
3095  //if (use->get_package_name () == "CMT") return result;
3096
3097  cmt_string discarded_text;
3098  cmt_string define_text;
3099  ActionType action = Cmt::get_action ();
3100
3101  switch (command_type)
3102    {
3103      //case CommandSet :
3104      case CommandSetAppend :
3105      case CommandSetPrepend :
3106      case CommandSetRemove :
3107      case CommandSetRemoveRegexp :
3108        //case CommandAlias :
3109        //case CommandPath :
3110      case CommandPathAppend :
3111      case CommandPathPrepend :
3112      case CommandPathRemove :
3113      case CommandPathRemoveRegexp :
3114      case CommandMacroPrepend :
3115        //case CommandMacro :
3116      case CommandMacroAppend :
3117      case CommandMacroRemove :
3118      case CommandMacroRemoveRegexp :
3119      case CommandMacroRemoveAll :
3120      case CommandMacroRemoveAllRegexp :
3121        //case CommandAction :
3122        if (value.text == "") return result;
3123        break;
3124    }
3125
3126  if (discarded) discarded_text = " (discarded by override)";
3127  else discarded_text = "";
3128
3129  if (first_definition) define_text = "defines";
3130  else define_text = "overrides";
3131
3132  cout << "# Package ";
3133  if (use != 0)
3134    {
3135      cout << use->get_package_name () << " " << use->version;
3136    }
3137
3138  switch (command_type)
3139    {
3140      case CommandSet :
3141        cout << " " << define_text << " set " << symbol.name << " as " << endl;
3142        //        cout << " " << define_text << " set " << symbol.name << " as ";
3143        cout << "set " << symbol.name;
3144        first_definition = false;
3145        result = 1;
3146        break;
3147      case CommandSetAppend :
3148        cout << " appends to set " << symbol.name << " : " << endl;;
3149        //        cout << " appends to set " << symbol.name << " : ";
3150        cout << "set_append " << symbol.name;
3151        result = 1;
3152        break;
3153      case CommandSetPrepend :
3154        cout << " prepends to set " << symbol.name << " : " << endl;
3155        //        cout << " prepends to set " << symbol.name << " : ";
3156        cout << "set_prepend " << symbol.name;
3157        result = 1;
3158        break;
3159      case CommandSetRemove :
3160        cout << " removes from set " << symbol.name << " : " << endl;
3161        //        cout << " removes from set " << symbol.name << " : ";
3162        cout << "set_remove " << symbol.name;
3163        result = 1;
3164        break;
3165      case CommandSetRemoveRegexp :
3166        cout << " removes RE from set " << symbol.name << " : " << endl;
3167        //        cout << " removes RE from set " << symbol.name << " : ";
3168        cout << "set_remove_regexp " << symbol.name;
3169        result = 1;
3170        break;
3171      case CommandAlias :
3172        cout << " " << define_text << " alias " << symbol.name << endl;
3173        //        cout << " " << define_text << " alias " << symbol.name << " as ";
3174        cout << "alias " << symbol.name;
3175        first_definition = false;
3176        result = 1;
3177        break;
3178      case CommandPath :
3179        cout << " " << define_text << " path " << symbol.name << endl;
3180        //        cout << " " << define_text << " path " << symbol.name << " as ";
3181        cout << "path " << symbol.name;
3182        first_definition = false;
3183        result = 1;
3184        break;
3185      case CommandPathAppend :
3186        cout << " appends to path " << symbol.name << endl;
3187        //        cout << " appends to path " << symbol.name << " : ";
3188        cout << "path_append " << symbol.name;
3189        result = 1;
3190        break;
3191      case CommandPathPrepend :
3192        cout << " prepends to path " << symbol.name << " : " << endl;
3193        //        cout << " prepends to path " << symbol.name << " : ";
3194        cout << "path_prepend " << symbol.name;
3195        result = 1;
3196        break;
3197      case CommandPathRemove :
3198        cout << " removes from path " << symbol.name << " : " << endl;
3199        //        cout << " removes from path " << symbol.name << " : ";
3200        cout << "path_remove " << symbol.name;
3201        result = 1;
3202        break;
3203      case CommandPathRemoveRegexp :
3204        cout << " removes RE from path " << symbol.name << " : " << endl;
3205        //        cout << " removes RE from path " << symbol.name << " : ";
3206        cout << "path_remove_regexp " << symbol.name;
3207        result = 1;
3208        break;
3209      case CommandMacro :
3210        cout << " " << define_text << " macro " << symbol.name << " as " << endl;
3211        //        cout << " " << define_text << " macro " << symbol.name << " as ";
3212        cout << "macro " << symbol.name;
3213        result = 1;
3214        break;
3215      case CommandMacroPrepend :
3216        cout << " prepends to macro " << symbol.name << " : " << endl;
3217        //        cout << " prepends to macro " << symbol.name << " : ";
3218        cout << "macro_prepend " << symbol.name;
3219        result = 1;
3220        break;
3221      case CommandMacroAppend :
3222        cout << " appends to macro " << symbol.name << " : " << endl;
3223        //        cout << " appends to macro " << symbol.name << " : ";
3224        cout << "macro_append " << symbol.name;
3225        result = 1;
3226        break;
3227      case CommandMacroRemove :
3228        cout << " remove from macro " << symbol.name << " : " << endl;
3229        //        cout << " remove from macro " << symbol.name << " : ";
3230        cout << "macro_remove " << symbol.name;
3231        result = 1;
3232        break;
3233      case CommandMacroRemoveRegexp :
3234        cout << " remove RE from macro " << symbol.name << " : " << endl;
3235        //        cout << " remove RE from macro " << symbol.name << " : ";
3236        cout << "macro_remove_regexp " << symbol.name;
3237        result = 1;
3238        break;
3239      case CommandMacroRemoveAll :
3240        cout << " remove all from macro " << symbol.name << " : " << endl;
3241        //        cout << " remove all from macro " << symbol.name << " : ";
3242        cout << "macro_remove_all " << symbol.name;
3243        result = 1;
3244        break;
3245      case CommandMacroRemoveAllRegexp :
3246        cout << " remove all RE from macro " << symbol.name << " : " << endl;
3247        //        cout << " remove all RE from macro " << symbol.name << " : ";
3248        cout << "macro_remove_all_regexp " << symbol.name;
3249        result = 1;
3250        break;
3251      case CommandSetupScript :
3252        cout << " " << define_text << " setup script " << symbol.name << endl;
3253        //       cout << " " << define_text << " action " << symbol.name << " as ";
3254        cout << "setup_script" /* << symbol.name */;
3255        first_definition = false;
3256        result = 1;
3257        break;
3258        /*
3259      case CommandAction :
3260        cout << " " << define_text << " action " << symbol.name << " as ";
3261        first_definition = false;
3262        break;
3263        */
3264    }
3265
3266  cout << " " << CmtSystem::quote (value.text, " \t");
3267  //  cout << "'" << value.text << "'";
3268         
3269  /*
3270  Tag* selected_tag = value.tag;
3271         
3272  if ((selected_tag == 0) ||
3273      (selected_tag == Tag::get_default ()))
3274    {
3275      cout << " for default tag";
3276    }
3277  else
3278    {
3279      cout << " for tag '" << selected_tag->get_name () << "'";
3280    }
3281  */
3282
3283  cout << endl;
3284  //  cout << discarded_text << endl;
3285  return result;
3286}
3287
3288//-------------------------------------------------------------
3289bool Symbol::check_tag_used (Tag* tag)
3290{
3291  if (tag == 0) return (false);
3292
3293  static SymbolVector& Symbols = symbols ();
3294
3295  if (Symbols.size () == 0) 
3296    {
3297      return (false);
3298    }
3299
3300  for (int number = 0; number < Symbol::symbol_number (); number++)
3301    {
3302      Symbol& symbol = Symbol::symbol (number);
3303
3304      for (int i = 0; i < symbol.value_lists.size (); i++)
3305        {
3306          const SymbolValueList& value_list = symbol.value_lists[i];
3307
3308          for (int j = 0; j < value_list.values.size (); j++)
3309            {
3310              const SymbolValue& value = value_list.values[j];
3311              Tag* t = value.tag;
3312
3313              if (t != 0)
3314                {
3315                  if (t->use_operand (tag)) return (true);
3316                }
3317            }
3318        }
3319    }
3320
3321  return (false);
3322}
3323
3324//-------------------------------------------------------------
3325cmt_string Symbol::get_env_value (const cmt_string& name)
3326{
3327  cmt_string s;
3328
3329  Symbol* symbol = Symbol::find (name);
3330  if (symbol != 0)
3331    {
3332      m_inhibit_display = true;
3333
3334      s = symbol->build_macro_value ();
3335      Symbol::expand (s);
3336
3337      m_inhibit_display = false;
3338    }
3339  else
3340    {
3341      s = CmtSystem::getenv (name);
3342    }
3343
3344  return (s);
3345}
3346
3347bool Symbol::get_inhibit_display ()
3348{
3349  return (m_inhibit_display);
3350}
3351
Note: See TracBrowser for help on using the repository browser.