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

Last change on this file since 607 was 607, checked in by rybkin, 12 years ago

See C.L. 482

  • Property svn:eol-style set to native
File size: 84.4 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 << "[ -z ${" << _name << "+CMT} ] || unset " << _name;
1644          //          cout << "unset " << _name;
1645          result = 1;
1646          break;
1647        case Bat :
1648          cout << "set " << _name << "=";
1649          result = 1;
1650          break;
1651        }
1652      break;
1653    case SymbolAlias :
1654      switch (mode)
1655        {
1656          case Csh :
1657            cout << "unalias " << name;
1658            result = 1;
1659            break;
1660          case Sh :
1661            cout << "[ -z ${" << name << "+CMT} ] || unset " << name;
1662          //            cout << "unset " << name;
1663            result = 1;
1664            break;
1665        }
1666      break;
1667    case SymbolPath :
1668      temp = clean_macro_value ();
1669      switch (mode)
1670        {
1671        case Csh :
1672          if (temp == "")
1673            {
1674              cout << "unsetenv " << _name;
1675            }
1676          else
1677            {
1678              cout << "setenv " << _name << " " << CmtSystem::quote (temp, " \t");
1679            }
1680          result = 1;
1681          break;
1682        case Sh :
1683          cout << _name << "=" << CmtSystem::quote (temp, " \t") << "; export " << _name;
1684          result = 1;
1685          break;
1686        case Bat :
1687          cout << "set " << _name << "=" << CmtSystem::quote (temp, " \t");
1688          result = 1;
1689          break;
1690        }
1691      break;
1692    case SymbolCleanupScript :
1693      switch (mode)
1694        {
1695        case Csh :
1696          cout << "if ( -f " << CmtSystem::quote (name, " \t") << ".csh ) then" << endl;
1697          cout << "  source " << CmtSystem::quote (name, " \t") << ".csh" << endl;
1698          cout <<
1699            "if ( $status != 0 ) then\n"
1700            "    set cmtcleanupstatus=1\n"
1701            "endif\n";
1702          cout << "endif" << endl;
1703          result = 1;
1704          break;
1705        case Sh :
1706          cout << "if test -f " << CmtSystem::quote (name, " \t") << ".sh; then" << endl;
1707          cout << "  . " << CmtSystem::quote (name, " \t") << ".sh" << endl;
1708          cout <<
1709            "if test $? != 0; then\n"
1710            "    cmtcleanupstatus=1\n"
1711            "fi\n";
1712          cout << "fi" << endl;
1713          result = 1;
1714          break;
1715        case Bat :
1716          cout << "call " << CmtSystem::quote (name, " \t");
1717          result = 1;
1718          break;
1719        }
1720      break;
1721    }
1722
1723  return (result);
1724}
1725
1726//-------------------------------------------------------------
1727int Symbol::print (PrintMode mode)
1728{
1729  int result = 0;
1730
1731  cmt_string temp = build_macro_value ();
1732  bool empty = (temp.size () == 0) ? true : false;
1733
1734  if (type == SymbolPath)
1735    {
1736      expand (temp);
1737      Symbol::filter_path_value (name, temp);
1738    }
1739
1740  cmt_string _name;
1741  switch (type)
1742    {
1743      case SymbolSet :
1744      case SymbolPath :
1745        if (0 != CmtSystem::mangle (name, _name))
1746          CmtMessage::info ("Replace " + name + " with " + _name);
1747        switch (mode)
1748          {
1749            case Csh :
1750              if (empty) cout << "unsetenv " << _name;
1751              else cout << "setenv " << _name << " \"" << temp << "\"";
1752              //else cout << "setenv " << name << " " << CmtSystem::quote (temp, " \t");
1753
1754              result = 1;
1755              break;
1756            case Sh :
1757              if (empty) cout << "[ -z ${" << _name << "+CMT} ] || unset " << _name;
1758              //              if (empty) cout << "unset " << _name;
1759              else cout << _name << "=\"" << temp << "\"; export " << _name;
1760              //else cout << name << "=" << CmtSystem::quote (temp, " \t") << "; export " << name;
1761
1762              result = 1;
1763              break;
1764            case Bat :
1765              temp.replace_all ("/", "\\");
1766              cout << "set " << _name << "=" << CmtSystem::quote (temp, " \t");
1767              //cout << "set " << name << "=" << temp;
1768              result = 1;
1769              break;
1770            case Xml :
1771              cout << "<variable><name>" << _name << "</name>"
1772                   << "<value>" << temp << "</value></variable>";
1773              result = 1;
1774              break;
1775          }
1776        break;
1777      case SymbolAlias :
1778        switch (mode)
1779          {
1780            case Csh :
1781              cout << "alias " << name <<
1782                " \"" << temp << "\"";
1783              //CmtSystem::quote (temp, " \t");
1784              result = 1;
1785              break;
1786            case Sh :
1787              cout << "alias " << name <<
1788                "=\"" << temp << "\"";
1789              //"=" << CmtSystem::quote (temp, " \t");
1790              result = 1;
1791              break;
1792            case Bat :
1793              cout << "set " << name <<
1794                "=" << CmtSystem::quote (temp, " \t");
1795              //"=" << temp;
1796              result = 1;
1797              break;
1798            case Xml :
1799              cout << "<alias><name>" << name << "</name>"
1800                   << "<value>" << temp << "</value></alias>";
1801              result = 1;
1802              break;
1803          }
1804        break;
1805      default :
1806        break;
1807    }
1808
1809  if (temp != "")
1810    {
1811      switch (type)
1812        {
1813          case SymbolSetupScript :
1814            switch (mode)
1815              {
1816                case Csh :
1817                  cout << "if ( -f " << CmtSystem::quote (name, " \t") << ".csh ) then" << endl;
1818                  cout << "  source " << CmtSystem::quote (name, " \t") << ".csh" << endl;
1819                  cout <<
1820                    "if ( $status != 0 ) then\n"
1821                    "    set cmtsetupstatus=1\n"
1822                    "endif\n";
1823                  cout << "endif" << endl;
1824                  result = 1;
1825                  break;
1826                case Sh :
1827                  cout << "if test -f " << CmtSystem::quote (name, " \t") << ".sh; then" << endl;
1828                  cout << "  . " << CmtSystem::quote (name, " \t") << ".sh" << endl;
1829                  cout <<
1830                    "if test $? != 0; then\n"
1831                    "    cmtsetupstatus=1\n"
1832                    "fi\n";
1833                  cout << "fi" << endl;
1834                  result = 1;
1835                  break;
1836                case Bat :
1837                  cout << "call " << CmtSystem::quote (name, " \t");
1838                  result = 1;
1839                  break;
1840                case Xml :
1841                  cout << "<script>" << name << "</script>";
1842                  result = 1;
1843                  break;
1844              }
1845            break;
1846          default:
1847            break;
1848        }
1849    }
1850
1851  return (result);
1852}
1853
1854//-------------------------------------------------------------
1855cmt_string Symbol::build_macro_value (bool display_it) const
1856{
1857  cmt_string temp;
1858
1859  if (display_it)
1860    {
1861      temp = builder->build_and_display (*this);
1862    }
1863  else
1864    {
1865      temp = builder->build (*this);
1866    }
1867
1868  return (temp);
1869}
1870
1871//-------------------------------------------------------------
1872int Symbol::print_macro (PrintMode mode, ostream& out) const
1873{
1874  if (mode != Requirements || printed) return 0;
1875  return builder->print (*this, out);
1876}
1877
1878//-------------------------------------------------------------
1879cmt_string Symbol::clean_macro_value () const
1880{
1881  cmt_string temp;
1882
1883  temp = builder->clean (*this);
1884
1885  return (temp);
1886}
1887
1888/**
1889
1890   Attempt to substitute into the symbol value all occurrences of
1891
1892      ${xxx}
1893      $(xxx)
1894      `xxx`
1895      %xxx%    [on Windows only]
1896
1897      by the appropriate value:
1898
1899        for `xxx` :
1900
1901            xxx is considered as a shell command. Value is the result of its execution
1902
1903        for other patterns:
1904
1905            if xxx is a symbol name, its value is substituted to the pattern
1906            otherwise, xxx is tried as an environment variable
1907
1908
1909     ===> In all cases, the pattern is filtered away.
1910
1911  */
1912cmt_string Symbol::resolve_macro_value (const cmt_string& tag_name) const
1913{
1914  cmt_string temp = builder->build (*this, tag_name);
1915
1916  resolve_value (temp);
1917
1918  return (temp);
1919}
1920
1921//-------------------------------------------------------------
1922void Symbol::show_macro (PrintMode mode, ostream& out)
1923//void Symbol::show_macro (PrintMode mode)
1924{
1925  if (Cmt::get_debug ())
1926    {
1927      cout << "Symbol::show_macro> " << name << endl;
1928    }
1929
1930  ActionType action = Cmt::get_action ();
1931
1932  cmt_string value = build_macro_value (true);
1933
1934  if ((!Cmt::get_quiet ()) &&
1935      (action != action_build_tag_makefile) &&
1936      (action != action_build_constituents_config) &&
1937      (action != action_build_constituent_config) &&
1938      (action != action_build_broadcast_config) &&
1939      (action != action_show_macros) &&
1940      (action != action_show_actions) &&
1941      (action != action_show_sets))
1942    {
1943      out << "#" << endl;
1944      out << "# Selection : " << endl;
1945    }
1946
1947  if (value.size () > 0)
1948    {
1949      if ((action == action_show_macro) ||
1950          (action == action_show_macros) ||
1951          (action == action_show_sets) ||
1952          (action == action_show_set) ||
1953          (action == action_show_actions) ||
1954          (action == action_show_action) ||
1955          (action == action_build_tag_makefile) ||
1956          (action == action_build_constituents_config) ||
1957          (action == action_build_constituent_config) ||
1958          (action == action_build_broadcast_config) ||
1959          (action == action_load) ||
1960          (!Cmt::get_quiet ()))
1961        {
1962          if (mode == Make)
1963            {
1964              out << name << "=";
1965            }
1966          else
1967            {
1968              out << name << "='";
1969            }
1970        }
1971
1972      if ((action == action_show_macro_value) ||
1973          (action == action_show_set_value) ||
1974          (action == action_show_action_value))
1975        {
1976          expand (value);
1977        }
1978      else if (action == action_build_tag_makefile ||
1979               action == action_build_constituents_config ||
1980               action == action_build_constituent_config ||
1981               action == action_build_broadcast_config)
1982        {
1983            /*
1984               Unfortunately, the %xxx% pattern has to be kept on Unix. Therefore
1985               we cannot force all patterns to become $(xxx)
1986
1987               This was useful on Windows so as to only keep $(xxx)
1988             */
1989#ifdef WIN32
1990          suppress_OS_delimiters (value);
1991#endif
1992        }
1993
1994      out << value;
1995
1996      if ((action == action_show_macro) ||
1997          (action == action_show_macros) ||
1998          (action == action_show_sets) ||
1999          (action == action_show_set) ||
2000          (action == action_show_actions) ||
2001          (action == action_show_action) ||
2002          (action == action_build_tag_makefile) ||
2003          (action == action_build_constituents_config) ||
2004          (action == action_build_constituent_config) ||
2005          (action == action_build_broadcast_config) ||
2006          (action == action_load) ||
2007          (!Cmt::get_quiet ()))
2008        {
2009          if (mode != Make)
2010            {
2011              out << "'";
2012            }
2013#ifdef WIN32
2014          else
2015            {
2016              out << " ";
2017            }
2018#endif
2019        }
2020
2021      out << endl;
2022    }
2023}
2024
2025//-------------------------------------------------------------
2026void Symbol::clear_all ()
2027{
2028  static SymbolVector& Symbols = symbols ();
2029  static SymbolMap& SymbolMap = symbol_map ();
2030
2031  SymbolMap.clear ();
2032  Symbols.clear ();
2033}
2034
2035//-------------------------------------------------------------
2036void Symbol::expand (cmt_string& text)
2037{
2038  static cmt_regexp reg ("[$%`]");
2039
2040  if (!reg.match (text)) return;
2041
2042  resolve_value (text);
2043}
2044
2045//-------------------------------------------------------------
2046ValueBuilder::ValueBuilder ()
2047{
2048  m_display_it = false;
2049}
2050
2051//-------------------------------------------------------------
2052const cmt_string ValueBuilder::build_and_display (const Symbol& symbol)
2053{
2054  cmt_string temp;
2055
2056  m_display_it = true;
2057  temp = build (symbol);
2058  m_display_it = false;
2059
2060  return (temp);
2061}
2062
2063//-------------------------------------------------------------
2064int ValueBuilder::print (const Symbol& symbol, ostream& out)
2065{
2066  int result (0);
2067
2068  bool first_definition = true;
2069 
2070  for (int i = 0; i < symbol.value_lists.size (); i++)
2071    {
2072      const SymbolValueList& value_list = symbol.value_lists[i];
2073
2074      if ((value_list.use != 0) &&
2075          (value_list.use->discarded)) continue;
2076
2077      const int selected = value_list.select_first ();
2078
2079      if (selected < 0) continue;
2080
2081      SymbolValue& value = value_list.values[selected];
2082
2083      result += value_list.print (symbol, value, first_definition);
2084    }
2085
2086  return result;
2087}
2088
2089//-------------------------------------------------------------
2090const cmt_string SetBuilder::build (const Symbol& symbol,
2091                                    const cmt_string& /*tag_name*/)
2092{
2093    // Control of recursivity
2094  static int level = 0;
2095
2096  bool show_it = false;
2097
2098  cmt_string temp;
2099  cmt_string previous_temp;
2100  cmt_string new_value;
2101  static const cmt_string empty;
2102
2103  ActionType action = Cmt::get_action ();
2104
2105  if (action == action_show_set)
2106    {
2107      if (symbol.name == Cmt::get_current_target ())
2108        {
2109             // Should not display on recursive calls
2110          if (level == 0) show_it = m_display_it;
2111        }
2112    }
2113
2114  level++;
2115
2116  temp = "";
2117
2118  bool first_definition = true;
2119
2120  for (int i = 0; i < symbol.value_lists.size (); i++)
2121    {
2122      const SymbolValueList& value_list = symbol.value_lists[i];
2123
2124      if ((value_list.use != 0) &&
2125          (value_list.use->discarded)) continue;
2126
2127      const int selected = value_list.select_first ();
2128
2129      if (selected < 0) continue;
2130
2131      SymbolValue& value = value_list.values[selected];
2132
2133      if (show_it && !Symbol::get_inhibit_display())
2134        {
2135          value_list.show (symbol, value, first_definition);
2136        }
2137     
2138      if (value_list.discarded) continue;
2139
2140        //
2141        // One should accumulate values if it refers to
2142        // itself.
2143        //
2144     
2145      new_value = value.text;
2146     
2147      resolve_value_for_macros (new_value);
2148     
2149      switch (value_list.command_type)
2150        {
2151          case CommandSet :
2152
2153            if (!value_list.is_reflexive || 
2154                !symbol.value_is_reflexive (value.text))
2155              {
2156                resolve_value (new_value, symbol.name, temp);
2157                temp = new_value;
2158              }
2159            else if (temp == "")
2160              {
2161                temp = CmtSystem::getenv (symbol.name);
2162              }
2163
2164            break;
2165          case CommandSetAppend :
2166           
2167            if (new_value != "")
2168              {
2169                temp += new_value;
2170              }
2171           
2172            break;
2173          case CommandSetPrepend :
2174           
2175            if (new_value != "")
2176              {
2177                previous_temp = temp;
2178                temp = new_value;
2179                temp += previous_temp;
2180              }
2181           
2182            break;
2183          case CommandSetRemove :
2184           
2185            if (new_value != "")
2186              {
2187                temp.replace_all (new_value, empty);
2188              }
2189           
2190            break;
2191          case CommandSetRemoveRegexp :
2192           
2193            if (new_value != "")
2194              {
2195                cmt_regexp e (new_value);
2196                cmt_regexp::iterator it;
2197
2198                for (;;)
2199                  {
2200                    it = e.begin (temp);
2201                    if (it == e.end ()) break;
2202
2203                    temp.erase (it._pos, it._length);
2204                  }
2205              }
2206           
2207            break;
2208          case CommandAlias :
2209           
2210            resolve_value (new_value, symbol.name, temp);
2211            temp = new_value;
2212           
2213            break;
2214        }
2215    }
2216
2217  level--;
2218
2219  return (temp);
2220}
2221
2222static bool find_path_entry (const cmt_string& paths, const cmt_string& value)
2223{
2224  static const cmt_string path_separator = CmtSystem::path_separator ();
2225
2226  //  cmt_string here = CmtSystem::pwd ();
2227  cmt_string rvalue = value;
2228
2229  /*
2230  if (CmtSystem::cd (value))
2231    {
2232      rvalue = CmtSystem::pwd ();
2233    }
2234  else
2235  */
2236  if (!CmtSystem::realpath_ (value, rvalue))
2237    {
2238      CmtSystem::compress_path (rvalue);
2239    }
2240
2241  CmtSystem::cmt_string_vector items;
2242  CmtSystem::split (paths, path_separator, items);
2243
2244  bool found = false;
2245
2246  for (int i = 0; i < items.size (); i++)
2247    {
2248      const cmt_string& item = items[i];
2249      cmt_string ritem = item;
2250      /*
2251      if (CmtSystem::cd (item))
2252        {
2253          ritem = CmtSystem::pwd ();
2254        }
2255      else
2256      */
2257      if (!CmtSystem::realpath_ (item, ritem))
2258        {
2259          CmtSystem::compress_path (ritem);
2260        }
2261
2262      if (ritem == rvalue)
2263        {
2264          found = true;
2265          break;
2266        }
2267    }
2268
2269  //  CmtSystem::cd (here);
2270  return (found);
2271}
2272
2273//-------------------------------------------------------------
2274const cmt_string PathBuilder::build (const Symbol& symbol,
2275                                     const cmt_string& /*tag_name*/)
2276{
2277    // Control of recursivity
2278  static int level = 0;
2279
2280  bool show_it = false;
2281
2282  cmt_string temp;
2283  cmt_string previous_temp;
2284  cmt_string new_value;
2285  static const cmt_string empty;
2286
2287  static cmt_string path_separator = CmtSystem::path_separator ();
2288
2289  ActionType action = Cmt::get_action ();
2290
2291  if (action == action_show_set)
2292    {
2293      if (symbol.name == Cmt::get_current_target ())
2294        {
2295            // Should not display on recursive calls
2296          if (level == 0) show_it = m_display_it;
2297        }
2298    }
2299
2300  level++;
2301
2302  temp = CmtSystem::getenv (symbol.name);
2303
2304  bool first_definition = true;
2305
2306  for (int i = 0; i < symbol.value_lists.size (); i++)
2307    {
2308      const SymbolValueList& value_list = symbol.value_lists[i];
2309
2310      if ((value_list.use != 0) &&
2311          (value_list.use->discarded)) continue;
2312
2313      const int selected = value_list.select_first ();
2314     
2315      if (selected < 0) continue;
2316 
2317      SymbolValue& value = value_list.values[selected];
2318         
2319      if (show_it && !Symbol::get_inhibit_display())
2320        {
2321          value_list.show (symbol, value, first_definition);
2322        }
2323         
2324      if (value_list.discarded) continue;
2325
2326      new_value = value.text;
2327         
2328          //resolve_value (new_value);
2329      resolve_value_for_macros (new_value);
2330         
2331      switch (value_list.command_type)
2332        {
2333          case CommandPath :
2334           
2335            if (!value_list.is_reflexive || 
2336                !symbol.value_is_reflexive (value.text))
2337              {
2338                resolve_value (new_value, symbol.name, temp);
2339                temp = new_value;
2340              }
2341
2342            break;
2343          case CommandPathAppend :
2344             
2345            if (new_value != "")
2346              {
2347                if (!find_path_entry (temp, new_value))
2348                  {
2349                    if (temp != "") temp += path_separator;
2350                     
2351                    temp += new_value;
2352                  }
2353              }
2354                 
2355            break;
2356          case CommandPathPrepend :
2357             
2358            if (new_value != "")
2359              {
2360                if (!find_path_entry (temp, new_value))
2361                  {
2362                    previous_temp = temp;
2363                    temp = new_value;
2364                    if (previous_temp != "") temp += path_separator;
2365                    temp += previous_temp;
2366                  }
2367              }
2368                 
2369            break;
2370          case CommandPathRemove :
2371             
2372            if (new_value != "")
2373              {
2374                CmtSystem::cmt_string_vector paths;
2375                 
2376                CmtSystem::split (temp, path_separator, paths);
2377                 
2378                for (int j = 0; j < paths.size (); ++j)
2379                  {
2380                    cmt_string& s = paths[j];
2381                     
2382                    if (s.find (new_value) != cmt_string::npos)
2383                      {
2384                        s = "";
2385                      }
2386                  }
2387
2388                Cmt::vector_to_string (paths, path_separator, temp);
2389              }
2390             
2391            break;
2392          case CommandPathRemoveRegexp :
2393
2394            if (new_value != "")
2395              {
2396                cmt_regexp e (new_value);
2397
2398                CmtSystem::cmt_string_vector paths;
2399                 
2400                CmtSystem::split (temp, path_separator, paths);
2401                 
2402                for (int j = 0; j < paths.size (); ++j)
2403                  {
2404                    cmt_string& s = paths[j];
2405
2406                    if (CmtSystem::getenv ("TESTPRR") != "")
2407                      {
2408                        cerr << "PRR> s=[" << s << "]";
2409                      }
2410
2411                    if (e.match (s))
2412                      {
2413                        s = "";
2414
2415                        if (CmtSystem::getenv ("TESTPRR") != "")
2416                          {
2417                            cerr << " match ";
2418                          }
2419                      }
2420                    else
2421                      {
2422                        if (CmtSystem::getenv ("TESTPRR") != "")
2423                          {
2424                            cerr << " no match ";
2425                          }
2426                      }
2427
2428                    if (CmtSystem::getenv ("TESTPRR") != "")
2429                      {
2430                        cerr << endl;
2431                      }
2432                  }
2433
2434                Cmt::vector_to_string (paths, path_separator, temp);
2435              }
2436             
2437            break;
2438        }
2439
2440    }
2441
2442  level--;
2443
2444  for (;;)
2445    {
2446      int sz = temp.size ();
2447
2448      if (sz == 0) break;
2449
2450      if ((temp[0] == ';') || (temp[0] == ':'))
2451        {
2452          temp.erase (0, 1);
2453        }
2454      else if ((temp[sz-1] == ';') || (temp[sz-1] == ':'))
2455        {
2456          temp.erase (sz-1, 1);
2457        }
2458      else
2459        {
2460          break;
2461        }
2462    }
2463
2464  temp.replace_all ("::", ":");
2465  temp.replace_all (";;", ";");
2466 
2467  return (temp);
2468}
2469
2470//-------------------------------------------------------------
2471const cmt_string PathBuilder::clean (const Symbol& symbol,
2472                                     const cmt_string& /*tag_name*/)
2473{
2474    // Control of recursivity
2475  static int level = 0;
2476
2477  cmt_string temp;
2478  cmt_string new_value;
2479  static const cmt_string empty;
2480
2481  static cmt_string path_separator = CmtSystem::path_separator ();
2482
2483  temp = CmtSystem::getenv (symbol.name);
2484
2485    //cerr << "#####1 temp=" << temp << endl;
2486
2487  for (int i = 0; i < symbol.value_lists.size (); i++)
2488    {
2489      const SymbolValueList& value_list = symbol.value_lists[i];
2490
2491      if (value_list.discarded) continue;
2492
2493      if ((value_list.use != 0) &&
2494          (value_list.use->discarded)) continue;
2495
2496      const int selected = value_list.select_first ();
2497     
2498      if (selected < 0) continue;
2499 
2500      SymbolValue& value = value_list.values[selected];
2501         
2502      new_value = value.text;
2503         
2504          //resolve_value (new_value);
2505
2506        //cerr << "#####1 new_value=" << new_value << endl;
2507
2508      resolve_value_for_macros (new_value);
2509      resolve_value (new_value);
2510         
2511        //cerr << "#####2 new_value=" << new_value << endl;
2512
2513      switch (value_list.command_type)
2514        {
2515          case CommandPath :
2516           
2517            temp = "";
2518           
2519            break;
2520          case CommandPathAppend :
2521          case CommandPathPrepend :
2522          case CommandPathRemove :
2523
2524            if (new_value != "")
2525              {
2526                CmtSystem::cmt_string_vector paths;
2527                 
2528                CmtSystem::split (temp, path_separator, paths);
2529                 
2530                for (int j = 0; j < paths.size (); ++j)
2531                  {
2532                    cmt_string& s = paths[j];
2533                     
2534                    if (s.find (new_value) != cmt_string::npos)
2535                      {
2536                        s = "";
2537                      }
2538
2539                    if (j > 0)
2540                      {
2541                        cmt_string& s2 = paths[j-1];
2542                        if (s2 == s)
2543                          {
2544                            s2 = "";
2545                          }
2546                      }
2547                  }
2548
2549                Cmt::vector_to_string (paths, path_separator, temp);
2550                temp.replace_all ("::", ":");
2551                temp.replace_all (";;", ";");
2552              }
2553             
2554            break;
2555          case CommandPathRemoveRegexp :
2556
2557            if (new_value != "")
2558              {
2559                cmt_regexp e (new_value);
2560
2561                CmtSystem::cmt_string_vector paths;
2562                 
2563                CmtSystem::split (temp, path_separator, paths);
2564                 
2565                for (int j = 0; j < paths.size (); ++j)
2566                  {
2567                    cmt_string& s = paths[j];
2568                     
2569                    if (e.match (s))
2570                      {
2571                        s = "";
2572                      }
2573
2574                    if (j > 0)
2575                      {
2576                        cmt_string& s2 = paths[j-1];
2577                        if (s2 == s)
2578                          {
2579                            s2 = "";
2580                          }
2581                      }
2582                  }
2583
2584                Cmt::vector_to_string (paths, path_separator, temp);
2585                temp.replace_all ("::", ":");
2586                temp.replace_all (";;", ";");
2587              }
2588             
2589            break;
2590        }
2591    }
2592
2593    //cerr << "#####2 temp=" << temp << endl;
2594 
2595  return (temp);
2596}
2597
2598//-------------------------------------------------------------
2599const cmt_string MacroBuilder::build (const Symbol& symbol,
2600                                      const cmt_string& tag_name)
2601{
2602    // Control of recursivity
2603  static int level = 0;
2604
2605  cmt_string temp;
2606  cmt_string previous_temp;
2607  static const cmt_string empty;
2608  bool show_it = false;
2609
2610  ActionType action = Cmt::get_action ();
2611
2612  if (action == action_show_macro)
2613    {
2614      if (symbol.name == Cmt::get_current_target ())
2615        {
2616             // Should not display on recursive calls
2617          if (level == 0) show_it = m_display_it;
2618        }
2619    }
2620
2621  level++;
2622
2623  temp = "";
2624
2625  int i;
2626
2627  bool first_definition = true;
2628
2629  for (i = 0; i < symbol.value_lists.size (); i++)
2630    {
2631      const SymbolValueList& value_list = symbol.value_lists[i];
2632
2633      if ((value_list.use != 0) &&
2634          (value_list.use->discarded)) continue;
2635
2636      if (value_list.command_type != CommandMacroPrepend) continue;
2637
2638      const int selected = value_list.select_first (tag_name);
2639
2640      if (selected < 0) continue;
2641
2642      SymbolValue& value = value_list.values[selected];
2643
2644      if (show_it && !Symbol::get_inhibit_display())
2645        {
2646          value_list.show (symbol, value, first_definition);
2647        }
2648
2649      if (value_list.discarded) continue;
2650
2651      previous_temp = temp;
2652      temp = value.text;
2653      temp += previous_temp;
2654    }
2655
2656  previous_temp = temp;
2657  temp = "";
2658
2659  first_definition = true;
2660
2661  for (i = 0; i < symbol.value_lists.size (); i++)
2662    {
2663      const SymbolValueList& value_list = symbol.value_lists[i];
2664
2665      if ((value_list.use != 0) &&
2666          (value_list.use->discarded)) continue;
2667
2668      if (value_list.command_type != CommandMacro) continue;
2669
2670      const int selected = value_list.select_first (tag_name);
2671
2672      if (selected < 0) continue;
2673
2674      SymbolValue& value = value_list.values[selected];
2675
2676      if (show_it && !Symbol::get_inhibit_display())
2677        {
2678          value_list.show (symbol, value, first_definition);
2679        }
2680
2681      // WARNING:
2682      // Commented just for a test : should be uncommented after the test
2683      if (value_list.discarded) continue;
2684     
2685      if (!value_list.is_reflexive || 
2686          !symbol.value_is_reflexive (value.text))
2687        {
2688          temp = value.text;
2689        }
2690    }
2691
2692  previous_temp += temp;
2693  temp = previous_temp;
2694
2695  for (i = 0; i < symbol.value_lists.size (); i++)
2696    {
2697      const SymbolValueList& value_list = symbol.value_lists[i];
2698
2699      if ((value_list.use != 0) &&
2700          (value_list.use->discarded)) continue;
2701
2702      if (value_list.command_type != CommandMacroAppend) continue;
2703
2704      const int selected = value_list.select_first (tag_name);
2705
2706      if (selected < 0) continue;
2707
2708      SymbolValue& value = value_list.values[selected];
2709
2710      if (show_it && !Symbol::get_inhibit_display())
2711        {
2712          value_list.show (symbol, value, first_definition);
2713        }
2714
2715      if (value_list.discarded) continue;
2716
2717      temp += value.text;
2718    }
2719
2720  for (i = 0; i < symbol.value_lists.size (); i++)
2721    {
2722      const SymbolValueList& value_list = symbol.value_lists[i];
2723
2724      if ((value_list.use != 0) &&
2725          (value_list.use->discarded)) continue;
2726
2727      if ((value_list.command_type != CommandMacroRemove) &&
2728          (value_list.command_type != CommandMacroRemoveRegexp) &&
2729          (value_list.command_type != CommandMacroRemoveAll) &&
2730          (value_list.command_type != CommandMacroRemoveAllRegexp)) continue;
2731
2732      const int selected = value_list.select_first (tag_name);
2733
2734      if (selected < 0) continue;
2735
2736      SymbolValue& value = value_list.values[selected];
2737
2738      if (show_it && !Symbol::get_inhibit_display())
2739        {
2740          value_list.show (symbol, value, first_definition);
2741        }
2742
2743      if (value_list.discarded) continue;
2744
2745      switch (value_list.command_type)
2746        {
2747          case CommandMacroRemove :
2748            temp.replace (value.text, empty);
2749            break;
2750          case CommandMacroRemoveRegexp :
2751            if (value.text != "")
2752              {
2753                cmt_regexp e (value.text);
2754                cmt_regexp::iterator it;
2755
2756                it = e.begin (temp);
2757                if (it != e.end ())
2758                  {
2759                    temp.erase (it._pos, it._length);
2760                  }
2761              }
2762            break;
2763          case CommandMacroRemoveAll :
2764            temp.replace_all (value.text, empty);
2765            break;
2766          case CommandMacroRemoveAllRegexp :
2767            if (value.text != "")
2768              {
2769                cmt_regexp e (value.text);
2770                cmt_regexp::iterator it;
2771
2772                for (;;)
2773                  {
2774                    it = e.begin (temp);
2775                    if (it != e.end ())
2776                      {
2777                        temp.erase (it._pos, it._length);
2778                      }
2779                    else
2780                      {
2781                        break;
2782                      }
2783                  }
2784              }
2785            break;
2786        }
2787    }
2788
2789  level--;
2790
2791  return (temp);
2792}
2793
2794//-------------------------------------------------------------
2795const cmt_string ScriptBuilder::build (const Symbol& symbol,
2796                                       const cmt_string& tag_name)
2797{
2798    // Control of recursivity
2799  static int level = 0;
2800
2801  static const cmt_string empty = "";
2802
2803  if (symbol.value_lists.size () > 0)
2804    {
2805      const SymbolValueList& value_list = symbol.value_lists[0];
2806
2807      if (value_list.discarded) return (empty);
2808
2809      if ((value_list.use != 0) &&
2810          (value_list.use->discarded)) return (empty);
2811    }
2812
2813  return (symbol.name);
2814}
2815
2816//-------------------------------------------------------------
2817const cmt_string ActionBuilder::build (const Symbol& symbol,
2818                                       const cmt_string& tag_name)
2819{
2820    // Control of recursivity
2821  static int level = 0;
2822
2823  cmt_string temp;
2824  cmt_string previous_temp;
2825  static const cmt_string empty;
2826  bool show_it = false;
2827
2828  ActionType action = Cmt::get_action ();
2829
2830  if (action == action_show_action)
2831    {
2832      if (symbol.name == Cmt::get_current_target ())
2833        {
2834             // Should not display on recursive calls
2835          if (level == 0) show_it = m_display_it;
2836        }
2837    }
2838
2839  level++;
2840
2841  int i;
2842
2843  bool first_definition = true;
2844
2845  temp = "";
2846
2847  for (i = 0; i < symbol.value_lists.size (); i++)
2848    {
2849      const SymbolValueList& value_list = symbol.value_lists[i];
2850
2851      if ((value_list.use != 0) &&
2852          (value_list.use->discarded)) continue;
2853
2854      if (value_list.command_type != CommandAction) continue;
2855
2856      const int selected = value_list.select_first (tag_name);
2857
2858      if (selected < 0) continue;
2859
2860      SymbolValue& value = value_list.values[selected];
2861
2862      if (show_it && !Symbol::get_inhibit_display())
2863        {
2864          value_list.show (symbol, value, first_definition);
2865        }
2866
2867      // WARNING:
2868      // Commented just for a test : should be uncommented after the test
2869      if (value_list.discarded) continue;
2870     
2871      if (!value_list.is_reflexive || 
2872          !symbol.value_is_reflexive (value.text))
2873        {
2874          temp = value.text;
2875        }
2876    }
2877
2878  level--;
2879
2880  return (temp);
2881}
2882
2883//-------------------------------------------------------------
2884int SymbolValueList::select_first (const cmt_string& tag_name) const
2885{
2886  int priority = 0;
2887  int value_number;
2888  int selected = -1;
2889
2890  Tag* the_tag = 0;
2891
2892  if (tag_name != "") the_tag = Tag::find (tag_name);
2893
2894  for (value_number = 0;
2895       value_number < values.size ();
2896       value_number++)
2897    {
2898      const SymbolValue& value = values[value_number];
2899
2900      const Tag* tag = value.tag;
2901
2902      if (the_tag == 0)
2903        {
2904          if (!tag->is_selected ()) continue;
2905          selected = value_number;
2906          if (tag == Tag::get_default ())
2907            continue;
2908        }
2909      else
2910        {
2911          if (tag != the_tag) continue;
2912          selected = value_number;
2913        }
2914
2915      //
2916      // Only the value for the first alternative (i.e. non-default)
2917      // matching tag expression is
2918      // selected (which means the selection stops here)
2919      //
2920      break;
2921
2922      //
2923      // Only the first value at a given priority is
2924      // selected (which implies the '>' test instead
2925      // of '>=')
2926      //
2927      /*
2928      if (tag->get_priority () > priority)
2929        {
2930          priority = tag->get_priority ();
2931          selected = value_number;
2932        }
2933      */
2934    }
2935
2936  return (selected);
2937}
2938
2939//-------------------------------------------------------------
2940int SymbolValueList::select_last () const
2941{
2942  int priority = 0;
2943  int value_number;
2944  int selected = -1;
2945
2946  for (value_number = 0;
2947       value_number < values.size ();
2948       value_number++)
2949    {
2950      SymbolValue& value = values[value_number];
2951
2952      const Tag* tag = value.tag;
2953
2954      if (tag->is_selected ())
2955        {
2956          //
2957          // The last value at a given priority is
2958          // selected (which implies the '>=' test instead
2959          // of '>')
2960          //
2961
2962          if (tag->get_priority () >= priority)
2963            {
2964              priority = tag->get_priority ();
2965              selected = value_number;
2966            }
2967        }
2968    }
2969
2970  return (selected);
2971}
2972
2973//-------------------------------------------------------------
2974void SymbolValueList::show (const Symbol& symbol, 
2975                            const SymbolValue& value, 
2976                            bool& first_definition) const
2977{
2978  cmt_string discarded_text;
2979  cmt_string define_text;
2980  ActionType action = Cmt::get_action ();
2981
2982  switch (command_type)
2983    {
2984      //case CommandSet :
2985      case CommandSetAppend :
2986      case CommandSetPrepend :
2987      case CommandSetRemove :
2988      case CommandSetRemoveRegexp :
2989        //case CommandAlias :
2990        //case CommandPath :
2991      case CommandPathAppend :
2992      case CommandPathPrepend :
2993      case CommandPathRemove :
2994      case CommandPathRemoveRegexp :
2995      case CommandMacroPrepend :
2996        //case CommandMacro :
2997      case CommandMacroAppend :
2998      case CommandMacroRemove :
2999      case CommandMacroRemoveRegexp :
3000      case CommandMacroRemoveAll :
3001      case CommandMacroRemoveAllRegexp :
3002        //case CommandAction :
3003        if (value.text == "") return;
3004        break;
3005    }
3006
3007  if (discarded) discarded_text = " (discarded by override)";
3008  else discarded_text = "";
3009
3010  if (first_definition) define_text = "defines";
3011  else define_text = "overrides";
3012
3013  cout << "# Package ";
3014  if (use != 0)
3015    {
3016      cout << use->get_package_name () << " " << use->version;
3017    }
3018
3019  switch (command_type)
3020    {
3021      case CommandSet :
3022        cout << " " << define_text << " set " << symbol.name << " as ";
3023        first_definition = false;
3024        break;
3025      case CommandSetAppend :
3026        cout << " appends to set " << symbol.name << " : ";
3027        break;
3028      case CommandSetPrepend :
3029        cout << " prepends to set " << symbol.name << " : ";
3030        break;
3031      case CommandSetRemove :
3032        cout << " removes from set " << symbol.name << " : ";
3033        break;
3034      case CommandSetRemoveRegexp :
3035        cout << " removes RE from set " << symbol.name << " : ";
3036        break;
3037      case CommandAlias :
3038        cout << " " << define_text << " alias " << symbol.name << " as ";
3039        first_definition = false;
3040        break;
3041      case CommandPath :
3042        cout << " " << define_text << " path " << symbol.name << " as ";
3043        first_definition = false;
3044        break;
3045      case CommandPathAppend :
3046        cout << " appends to path " << symbol.name << " : ";
3047        break;
3048      case CommandPathPrepend :
3049        cout << " prepends to path " << symbol.name << " : ";
3050        break;
3051      case CommandPathRemove :
3052        cout << " removes from path " << symbol.name << " : ";
3053        break;
3054      case CommandPathRemoveRegexp :
3055        cout << " removes RE from path " << symbol.name << " : ";
3056        break;
3057      case CommandMacroPrepend :
3058        cout << " prepends to macro " << symbol.name << " : ";
3059        break;
3060      case CommandMacro :
3061        cout << " " << define_text << " macro " << symbol.name << " as ";
3062        break;
3063      case CommandMacroAppend :
3064        cout << " appends to macro " << symbol.name << " : ";
3065        break;
3066      case CommandMacroRemove :
3067        cout << " remove from macro " << symbol.name << " : ";
3068        break;
3069      case CommandMacroRemoveRegexp :
3070        cout << " remove RE from macro " << symbol.name << " : ";
3071        break;
3072      case CommandMacroRemoveAll :
3073        cout << " remove all from macro " << symbol.name << " : ";
3074        break;
3075      case CommandMacroRemoveAllRegexp :
3076        cout << " remove all RE from macro " << symbol.name << " : ";
3077        break;
3078      case CommandAction :
3079        cout << " " << define_text << " action " << symbol.name << " as ";
3080        first_definition = false;
3081        break;
3082    }
3083
3084  cout << "'" << value.text << "'";
3085         
3086  Tag* selected_tag = value.tag;
3087         
3088  if ((selected_tag == 0) ||
3089      (selected_tag == Tag::get_default ()))
3090    {
3091      cout << " for default tag";
3092    }
3093  else
3094    {
3095      cout << " for tag '" << selected_tag->get_name () << "'";
3096    }
3097 
3098  cout << discarded_text << endl;
3099}
3100
3101//-------------------------------------------------------------
3102int SymbolValueList::print (const Symbol& symbol, 
3103                            const SymbolValue& value, 
3104                            bool& first_definition) const
3105{
3106  int result (0);
3107
3108  //if (use->get_package_name () == "CMT") return result;
3109
3110  cmt_string discarded_text;
3111  cmt_string define_text;
3112  ActionType action = Cmt::get_action ();
3113
3114  switch (command_type)
3115    {
3116      //case CommandSet :
3117      case CommandSetAppend :
3118      case CommandSetPrepend :
3119      case CommandSetRemove :
3120      case CommandSetRemoveRegexp :
3121        //case CommandAlias :
3122        //case CommandPath :
3123      case CommandPathAppend :
3124      case CommandPathPrepend :
3125      case CommandPathRemove :
3126      case CommandPathRemoveRegexp :
3127      case CommandMacroPrepend :
3128        //case CommandMacro :
3129      case CommandMacroAppend :
3130      case CommandMacroRemove :
3131      case CommandMacroRemoveRegexp :
3132      case CommandMacroRemoveAll :
3133      case CommandMacroRemoveAllRegexp :
3134        //case CommandAction :
3135        if (value.text == "") return result;
3136        break;
3137    }
3138
3139  if (discarded) discarded_text = " (discarded by override)";
3140  else discarded_text = "";
3141
3142  if (first_definition) define_text = "defines";
3143  else define_text = "overrides";
3144
3145  if (CmtMessage::active (Verbose))
3146    cout << "# Package " << (use != 0 ? use->get_package_name () + " " + use->version : "");
3147  /*
3148  if (use != 0)
3149    {
3150      cout << use->get_package_name () << " " << use->version;
3151    }
3152  */
3153
3154  switch (command_type)
3155    {
3156      case CommandSet :
3157        if (CmtMessage::active (Verbose))
3158          cout << " " << define_text << " set " << symbol.name << " as " << endl;
3159        //        cout << " " << define_text << " set " << symbol.name << " as ";
3160        cout << "set " << symbol.name;
3161        first_definition = false;
3162        result = 1;
3163        break;
3164      case CommandSetAppend :
3165        if (CmtMessage::active (Verbose))
3166          cout << " appends to set " << symbol.name << " : " << endl;
3167        //        cout << " appends to set " << symbol.name << " : ";
3168        cout << "set_append " << symbol.name;
3169        result = 1;
3170        break;
3171      case CommandSetPrepend :
3172        if (CmtMessage::active (Verbose))
3173          cout << " prepends to set " << symbol.name << " : " << endl;
3174        //        cout << " prepends to set " << symbol.name << " : ";
3175        cout << "set_prepend " << symbol.name;
3176        result = 1;
3177        break;
3178      case CommandSetRemove :
3179        if (CmtMessage::active (Verbose))
3180          cout << " removes from set " << symbol.name << " : " << endl;
3181        //        cout << " removes from set " << symbol.name << " : ";
3182        cout << "set_remove " << symbol.name;
3183        result = 1;
3184        break;
3185      case CommandSetRemoveRegexp :
3186        if (CmtMessage::active (Verbose))
3187          cout << " removes RE from set " << symbol.name << " : " << endl;
3188        //        cout << " removes RE from set " << symbol.name << " : ";
3189        cout << "set_remove_regexp " << symbol.name;
3190        result = 1;
3191        break;
3192      case CommandAlias :
3193        if (CmtMessage::active (Verbose))
3194          cout << " " << define_text << " alias " << symbol.name << endl;
3195        //        cout << " " << define_text << " alias " << symbol.name << " as ";
3196        cout << "alias " << symbol.name;
3197        first_definition = false;
3198        result = 1;
3199        break;
3200      case CommandPath :
3201        if (CmtMessage::active (Verbose))
3202          cout << " " << define_text << " path " << symbol.name << endl;
3203        //        cout << " " << define_text << " path " << symbol.name << " as ";
3204        cout << "path " << symbol.name;
3205        first_definition = false;
3206        result = 1;
3207        break;
3208      case CommandPathAppend :
3209        if (CmtMessage::active (Verbose))
3210          cout << " appends to path " << symbol.name << endl;
3211        //        cout << " appends to path " << symbol.name << " : ";
3212        cout << "path_append " << symbol.name;
3213        result = 1;
3214        break;
3215      case CommandPathPrepend :
3216        if (CmtMessage::active (Verbose))
3217          cout << " prepends to path " << symbol.name << " : " << endl;
3218        //        cout << " prepends to path " << symbol.name << " : ";
3219        cout << "path_prepend " << symbol.name;
3220        result = 1;
3221        break;
3222      case CommandPathRemove :
3223        if (CmtMessage::active (Verbose))
3224          cout << " removes from path " << symbol.name << " : " << endl;
3225        //        cout << " removes from path " << symbol.name << " : ";
3226        cout << "path_remove " << symbol.name;
3227        result = 1;
3228        break;
3229      case CommandPathRemoveRegexp :
3230        if (CmtMessage::active (Verbose))
3231          cout << " removes RE from path " << symbol.name << " : " << endl;
3232        //        cout << " removes RE from path " << symbol.name << " : ";
3233        cout << "path_remove_regexp " << symbol.name;
3234        result = 1;
3235        break;
3236      case CommandMacro :
3237        if (CmtMessage::active (Verbose))
3238          cout << " " << define_text << " macro " << symbol.name << " as " << endl;
3239        //        cout << " " << define_text << " macro " << symbol.name << " as ";
3240        cout << "macro " << symbol.name;
3241        result = 1;
3242        break;
3243      case CommandMacroPrepend :
3244        if (CmtMessage::active (Verbose))
3245          cout << " prepends to macro " << symbol.name << " : " << endl;
3246        //        cout << " prepends to macro " << symbol.name << " : ";
3247        cout << "macro_prepend " << symbol.name;
3248        result = 1;
3249        break;
3250      case CommandMacroAppend :
3251        if (CmtMessage::active (Verbose))
3252          cout << " appends to macro " << symbol.name << " : " << endl;
3253        //        cout << " appends to macro " << symbol.name << " : ";
3254        cout << "macro_append " << symbol.name;
3255        result = 1;
3256        break;
3257      case CommandMacroRemove :
3258        if (CmtMessage::active (Verbose))
3259          cout << " remove from macro " << symbol.name << " : " << endl;
3260        //        cout << " remove from macro " << symbol.name << " : ";
3261        cout << "macro_remove " << symbol.name;
3262        result = 1;
3263        break;
3264      case CommandMacroRemoveRegexp :
3265        if (CmtMessage::active (Verbose))
3266          cout << " remove RE from macro " << symbol.name << " : " << endl;
3267        //        cout << " remove RE from macro " << symbol.name << " : ";
3268        cout << "macro_remove_regexp " << symbol.name;
3269        result = 1;
3270        break;
3271      case CommandMacroRemoveAll :
3272        if (CmtMessage::active (Verbose))
3273          cout << " remove all from macro " << symbol.name << " : " << endl;
3274        //        cout << " remove all from macro " << symbol.name << " : ";
3275        cout << "macro_remove_all " << symbol.name;
3276        result = 1;
3277        break;
3278      case CommandMacroRemoveAllRegexp :
3279        if (CmtMessage::active (Verbose))
3280          cout << " remove all RE from macro " << symbol.name << " : " << endl;
3281        //        cout << " remove all RE from macro " << symbol.name << " : ";
3282        cout << "macro_remove_all_regexp " << symbol.name;
3283        result = 1;
3284        break;
3285      case CommandSetupScript :
3286        if (CmtMessage::active (Verbose))
3287          cout << " " << define_text << " setup script " << symbol.name << endl;
3288        //       cout << " " << define_text << " action " << symbol.name << " as ";
3289        cout << "setup_script" /* << symbol.name */;
3290        first_definition = false;
3291        result = 1;
3292        break;
3293        /*
3294      case CommandAction :
3295        cout << " " << define_text << " action " << symbol.name << " as ";
3296        first_definition = false;
3297        break;
3298        */
3299    }
3300
3301  cout << " " << CmtSystem::quote (value.text, " \t");
3302  //  cout << "'" << value.text << "'";
3303         
3304  /*
3305  Tag* selected_tag = value.tag;
3306         
3307  if ((selected_tag == 0) ||
3308      (selected_tag == Tag::get_default ()))
3309    {
3310      cout << " for default tag";
3311    }
3312  else
3313    {
3314      cout << " for tag '" << selected_tag->get_name () << "'";
3315    }
3316  */
3317
3318  cout << endl;
3319  //  cout << discarded_text << endl;
3320  return result;
3321}
3322
3323//-------------------------------------------------------------
3324bool Symbol::check_tag_used (const Tag* tag)
3325{
3326  if (tag == 0) return (false);
3327
3328  static SymbolVector& Symbols = symbols ();
3329
3330  if (Symbols.size () == 0) 
3331    {
3332      return (false);
3333    }
3334
3335  for (int number = 0; number < Symbol::symbol_number (); number++)
3336    {
3337      Symbol& symbol = Symbol::symbol (number);
3338
3339      for (int i = 0; i < symbol.value_lists.size (); i++)
3340        {
3341          const SymbolValueList& value_list = symbol.value_lists[i];
3342
3343          for (int j = 0; j < value_list.values.size (); j++)
3344            {
3345              const SymbolValue& value = value_list.values[j];
3346              Tag* t = value.tag;
3347
3348              if (t != 0)
3349                {
3350                  if (t->use_operand (tag)) return (true);
3351                }
3352            }
3353        }
3354    }
3355
3356  return (false);
3357}
3358
3359//-------------------------------------------------------------
3360cmt_string Symbol::get_env_value (const cmt_string& name)
3361{
3362  cmt_string s;
3363
3364  Symbol* symbol = Symbol::find (name);
3365  if (symbol != 0)
3366    {
3367      m_inhibit_display = true;
3368
3369      s = symbol->build_macro_value ();
3370      Symbol::expand (s);
3371
3372      m_inhibit_display = false;
3373    }
3374  else
3375    {
3376      s = CmtSystem::getenv (name);
3377    }
3378
3379  return (s);
3380}
3381
3382bool Symbol::get_inhibit_display ()
3383{
3384  return (m_inhibit_display);
3385}
3386
Note: See TracBrowser for help on using the repository browser.