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

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

See C.L. 465

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