source: CMT/HEAD/source/cmt_parser.cxx @ 561

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

See C.L. 444

  • Property svn:eol-style set to native
File size: 240.5 KB
Line 
1//-----------------------------------------------------------
2// Copyright Christian Arnault LAL-Orsay CNRS
3// arnault@lal.in2p3.fr
4// Modified by garonne@lal.in2p3.fr
5// See the complete license in cmt_license.txt "http://www.cecill.info".
6//-----------------------------------------------------------
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <ctype.h>
12
13//----------------------------------------------------------
14
15#include "cmt_parser.h"
16#include "cmt_version.h"
17
18#include "cmt_database.h"
19#include "cmt_include.h"
20#include "cmt_script.h"
21#include "cmt_generator.h"
22#include "cmt_system.h"
23#include "cmt.h"
24#include "cmt_error.h"
25#include "cmt_cvs.h"
26#include "cmt_vcs.h"
27#include "cmt_lock.h"
28#include "cmt_triggers.h"
29#include "cmt_model.h"
30#include "cmt_awk.h"
31#include "cmt_syntax.h"
32#include "cmt_install_area.h"
33#include "cmt_cmtpath_pattern.h"
34#include "cmt_sequence.h"
35#include "cmt_map.h"
36#include "cmt_project.h"
37#include "cmt_log.h"
38#include "cmt_commands.h"
39
40//----------------------------------------------------------
41//
42//  Static object definitions for the CmtContext class.
43//
44
45static CmtContext Me;
46
47CmtContext::CmtContext ()
48{
49  clear ();
50}
51
52CmtContext::~CmtContext ()
53{
54}
55
56void CmtContext::clear ()
57{
58  m_action         = action_none;
59  m_build_nmake    = false;
60  m_cmt_config     = "";
61  //m_cmt_path.clear ();
62  //m_cmt_path_pwds.clear ();
63  //m_cmt_path_sources.clear ();
64  m_cmt_root       = "";
65  m_cmt_version    = "";
66  m_use_cmt        = true;
67  m_current_dir     = "";
68  m_current_file_path = "";
69  m_current_package = "";
70  m_current_config  = "";
71  m_current_path    = "";
72  m_current_prefix  = "";
73  m_current_cmtpath = "";
74  m_current_offset  = "";
75
76  m_current_access   = DeveloperMode;
77
78  m_current_tag      = "";
79  m_current_target   = "";
80  m_current_version  = "";
81  m_default_path     = "";
82
83  //m_quiet            = false;
84
85  m_disable_warnings = false;
86  m_warnings         = false;
87  m_recursive        = false;
88
89  m_scope_filtering_mode = default_filtering_mode;
90  m_simulation       = false;
91
92  m_standard_macros_done = false;
93  m_current_access = UserMode;
94  m_current_style = cmt_style;
95  m_current_structuring_style = default_structuring_style;
96  m_all_sets_done = false;
97  m_autoconfigure_cmtpath = false;
98  m_debug = false;
99  if (getenv ("CMTDEBUG") != 0) m_debug = true;
100}
101
102//----------------------------------------------------------
103
104
105//----------------------------------------------------------
106//
107//   Utility classes
108//
109//----------------------------------------------------------
110
111/**
112 *  This PathScanner actor simply displays the package name/version/path
113 *  It is used by the cmt show packages operation
114 */
115class PackageViewer : public PathScanner::actor
116{
117public:
118  void run (const cmt_string& package,
119            const cmt_string& version,
120            const cmt_string& path,
121            const bool without_version_directory=false);
122};
123
124
125/**
126 *  This PathScanner actor accumulates all found packages into a cmt_string_vector
127 *  It is used by the broadcast operation
128 */
129class PackageSelector : public PathScanner::actor
130{
131public:
132  PackageSelector (CmtSystem::cmt_string_vector& uses);
133  void run (const cmt_string& package,
134            const cmt_string& version,
135            const cmt_string& path,
136            const bool without_version_directory=false);
137private:
138  CmtSystem::cmt_string_vector& m_uses;
139};
140
141
142
143/**
144 *  This PathScanner actor collects all packages clients of the specified one
145 *  It is used by the cmt show clients operation
146 */
147class ClientCollector : public PathScanner::actor
148{
149public:
150  ClientCollector (const cmt_string& package,
151                   const cmt_string& version);
152  void run (const cmt_string& package,
153            const cmt_string& version,
154            const cmt_string& path,
155            const bool without_version_directory=false);
156  int count ();
157
158private:
159  const cmt_string& m_package;
160  const cmt_string& m_version;
161  int m_count;
162};
163
164//----------------------------------------------------------
165void PackageViewer::run (const cmt_string& package,
166                         const cmt_string& version,
167                         const cmt_string& path,
168                         const bool without_version_directory)
169{
170  cout << package << " " << version << " " << path << endl;
171}
172
173//----------------------------------------------------------
174PackageSelector::PackageSelector (CmtSystem::cmt_string_vector& uses) : m_uses(uses)
175{
176}
177
178//----------------------------------------------------------
179void PackageSelector::run (const cmt_string& package,
180                           const cmt_string& version,
181                           const cmt_string& path,
182                           const bool without_version_directory)
183{
184  //
185  // this might be called on a package with no version directory.
186  // then simply the version argument is empty.
187  //
188
189  cmt_string r = CmtSystem::file_separator ();
190  r += "requirements";
191
192  cmt_string temp;
193
194  if (version == "")
195    {
196      temp = path;
197      //temp += CmtSystem::file_separator ();
198      //temp += package;
199      temp += CmtSystem::file_separator ();
200      temp += "cmt";
201      temp += r;
202     
203      if (!CmtSystem::test_file (temp)) return;
204    }
205  else
206    {
207      temp = path;
208      //temp += CmtSystem::file_separator ();
209      //temp += package;
210      temp += CmtSystem::file_separator ();
211      temp += version;
212      temp += CmtSystem::file_separator ();
213      temp += "cmt";
214      temp += r;
215     
216      if (!CmtSystem::test_file (temp))
217        {
218          temp = path;
219          //temp += CmtSystem::file_separator ();
220          //temp += package;
221          temp += CmtSystem::file_separator ();
222          temp += version;
223          temp += CmtSystem::file_separator ();
224          temp += "mgr";
225          temp += r;
226         
227          if (!CmtSystem::test_file (temp))
228            {
229              return;
230            }
231        }
232    }
233
234  temp.replace (r.c_str(), "");
235  cmt_string& use = m_uses.add ();
236  use = temp;
237}
238
239//----------------------------------------------------------
240ClientCollector::ClientCollector (const cmt_string& package,
241                                  const cmt_string& version) :
242  m_package (package), m_version (version), m_count (0)
243{
244}
245
246//----------------------------------------------------------
247void ClientCollector::run (const cmt_string& package,
248                           const cmt_string& version,
249                           const cmt_string& path,
250                           const bool without_version_directory)
251{
252  cmt_string dir = path;
253  dir += CmtSystem::file_separator ();
254  dir += package;
255  dir += CmtSystem::file_separator ();     
256  if (version != "" && ! without_version_directory)
257    {
258      dir += version;
259      dir += CmtSystem::file_separator ();
260    }
261
262  cmt_string req;
263 
264  req  = dir;
265  req += "cmt";
266  req += CmtSystem::file_separator ();
267  req += "requirements";
268
269  cmt_string requirements;
270  cmt_string line;
271  CmtSystem::cmt_string_vector words;
272
273  if (CmtSystem::test_file (req))
274    {
275      requirements.read (req);
276    }
277  else
278    {
279      req = dir;
280      req += "mgr";
281      req += CmtSystem::file_separator ();
282      req += "requirements";
283      if (CmtSystem::test_file (req))
284        {
285          requirements.read (req);
286        }
287    }
288
289  if (requirements != "")
290    {
291      int pos = 0;
292      int max_pos = requirements.size ();
293
294      while (pos < max_pos)
295        {
296          int cr = requirements.find (pos, "\r\n");
297          int nl = requirements.find (pos, '\n');
298          int first = nl;
299          int length = 1;
300               
301          if (cr != cmt_string::npos)
302            {
303              if (nl == cmt_string::npos)
304                {
305                  first = cr;
306                  length = 2;
307                }
308              else
309                {
310                  first = (nl < cr) ? nl : cr;
311                  length = (nl < cr) ? 1 : 2;
312                }
313            }
314               
315          if (first == cmt_string::npos)
316            {
317              requirements.substr (pos, line);
318              pos = max_pos;
319            }
320          else if (first > pos)
321            {
322              requirements.substr (pos, first - pos, line);
323              pos = first + length;
324            }
325          else
326            {
327              line.erase (0);
328              pos += length;
329            }
330
331          CmtSystem::split (line, " \t", words);
332
333          if ((words.size () >= 2) && (words[0] == "use")) 
334            {
335              if ((words[1] == m_package) && 
336                  ( (m_version == "") || (words[2] == m_version)))
337                {
338                  cout << "# " << package << " " << version << " " << path;
339                  if (m_version == "" && words.size () > 2)
340                    {
341                      cout << " (use version " << words[2] << ")";
342                    }
343                  cout << endl;
344                  m_count++;
345                }
346            }
347        }
348    }
349}
350
351//----------------------------------------------------------
352int ClientCollector::count ()
353{
354  return (m_count);
355}
356
357
358
359
360//----------------------------------------------------------
361//
362//   The Cmt methods
363//
364//----------------------------------------------------------
365
366
367
368/**
369 *   Append "CONFIG" to the prefix
370 */
371void Cmt::build_config (const cmt_string& prefix,
372                        cmt_string& config)
373{
374  /*
375    Building the config from <prefix>
376  */
377
378  config = prefix;
379  config += "CONFIG";
380}
381
382//----------------------------------------------------------
383void Cmt::build_makefile (const cmt_string& target)
384{
385  Constituent* constituent = 0;
386
387  if (target.size () > 0)
388    {
389      /*
390        Do genmake for one specific target.
391      */
392      constituent = Constituent::find (target);
393      if (constituent != 0)
394        {
395          constituent->build_makefile (Me.m_simulation);
396        }
397    }
398  else
399    {
400      /*
401        Do genmake for all possible targets.
402      */
403      Constituent::build_all_makefiles (Me.m_simulation);
404    }
405}
406
407//----------------------------------------------------------
408void Cmt::build_msdev_file (const cmt_string& target)
409{
410  Constituent* constituent = 0;
411
412  set_standard_macros ();
413
414  if (target != "")
415    {
416      /*
417        Do genmsdev for one specific target.
418      */
419      constituent = Constituent::find (target);
420      if (constituent != 0)
421        {
422          constituent->build_msdev_file (Me.m_simulation);
423        }
424    }
425  else
426    {
427      /*
428        Do genmsdev for all possible targets.
429      */
430      Constituent::build_all_msdev_files (Me.m_simulation);
431    }
432}
433
434/**
435   Visual Studio.net Support                                 
436*/
437void Cmt::build_vsnet_file (const cmt_string& target)       
438{                                                           
439  Constituent* constituent = 0;                             
440                                                             
441  set_standard_macros ();                                   
442                                                             
443  if (target != "")                                         
444    {                                                       
445      /*                                                     
446                                                             Do genvsnet for one specific target.                 
447      */                                                     
448      constituent = Constituent::find (target);             
449      if (constituent != 0)                                 
450        {                                                   
451          constituent->build_vsnet_file (Me.m_simulation);       
452        }                                                   
453    }                                                       
454  else                                                       
455    {                                                       
456      /*                                                     
457                                                             Do genvsnet for all possible targets.               
458      */                                                     
459      Constituent::build_all_vsnet_files (Me.m_simulation);       
460    }                                                       
461}                                                           
462
463//----------------------------------------------------------
464bool Cmt::build_nmake ()
465{
466  return (Me.m_build_nmake);
467}
468
469//----------------------------------------------------------
470void Cmt::build_OS9_makefile (const cmt_string& target)
471{
472  build_makefile (target);
473}
474
475/**
476 *   Convert a package name to its upper case copy
477 */
478void Cmt::build_prefix (const cmt_string& package, cmt_string& prefix)
479{
480  int pos;
481  char c;
482
483  /*
484    Building the prefix from <package>
485  */
486
487  prefix = package;
488
489  for (pos = 0; pos < package.size (); pos++)
490    {
491      c = package[pos];
492      prefix[pos] = toupper (c);
493    }
494}
495
496//----------------------------------------------------------
497void Cmt::clear ()
498{
499  Me.m_quiet = false;
500  Me.clear ();
501
502  Database::clear ();
503  Include::clear_all ();
504  Script::clear_all ();
505  CmtError::clear ();
506}
507
508//----------------------------------------------------------
509//void Cmt::configure ()
510void Cmt::configure (const ArgParser& ap)
511{
512  Log;
513  static bool configured = false;
514
515  if (configured) return;
516
517  Me.clear ();
518
519  log << "configure_cmt_message" << log_endl;
520  //configure_cmt_message ();
521  configure_cmt_message (ap);
522
523  log << "configure_default_path" << log_endl;
524  configure_default_path ();
525  log << "configure_version_tag" << log_endl;
526  configure_version_tag ();
527  log << "configure_uname_tag" << log_endl;
528  configure_uname_tag ();
529  log << "configure_hosttype_tag" << log_endl;
530  configure_hosttype_tag ();
531  log << "configure_config_tag" << log_endl;
532  configure_config_tag ();
533  log << "configure_site_tag" << log_endl;
534  configure_site_tag (0);
535  log << "configure_current_dir" << log_endl;
536  configure_current_dir ();
537  log << "configure_home" << log_endl;
538  // CMTHOME, CMTUSERCONTEXT and CMTPATH
539  configure_home (0);
540  //  log << "configure_current_dir" << log_endl;
541
542
543  bool save_quiet = Me.m_quiet;
544  Me.m_quiet = true;
545
546  Me.m_autoconfigure_cmtpath = true;
547  configure_cmt_path (0);
548  Me.m_autoconfigure_cmtpath = false;
549
550  Me.m_quiet = save_quiet;
551
552  //  configure_current_dir ();
553
554  guess_current_project ();
555
556  log << "configure_current_structuring_style" << log_endl;
557  //  configure_current_structuring_style ();
558  configure_current_structuring_style (ap);
559
560  log << "configure_current_package" << log_endl;
561  configure_current_package ();
562
563  Use& use = Use::current();
564
565  use.set (Me.m_current_package,
566           Me.m_current_version,
567           Me.m_current_path,
568           "",
569           "");
570
571  use.style = Me.m_current_style;
572  use.structuring_style = Me.m_current_structuring_style;
573
574  use.change_path (Me.m_current_path);
575
576  if (CmtError::has_pending_error ()) 
577    {
578      Me.m_configure_error = CmtError::get_last_error ();
579    }
580}
581
582//----------------------------------------------------------
583void Cmt::configure_cmt_path (Use* use)
584{
585  if (!Me.m_autoconfigure_cmtpath) return;
586
587  cmt_string s;
588
589  s = Symbol::get_env_value ("CMTPATH");
590
591  {
592    bool q = Me.m_quiet;
593    Me.m_quiet = true; 
594
595    Symbol::filter_path_value ("CMTPATH", s);
596
597    Me.m_quiet = q;
598  }
599
600  IProjectFactory& factory = ProjectFactory::instance ();
601  factory.reset ();
602
603  CmtSystem::get_cmt_paths (factory, s, Me.m_cmt_user_context, Me.m_cmt_home);
604}
605
606//----------------------------------------------------------
607void Cmt::configure_config_tag ()
608{
609  Me.m_cmt_config = CmtSystem::get_cmt_config ();
610  if (Me.m_cmt_config != "")
611    {
612      Tag* tag;
613
614      tag = Tag::add (Me.m_cmt_config, PriorityConfig, "CMTCONFIG", 0);
615      tag->mark ();
616    }
617}
618
619//----------------------------------------------------------
620void Cmt::configure_current_cmtpath ()
621{
622  Use& current_use = Use::current ();
623
624  Me.m_current_cmtpath = "";
625  Me.m_current_offset = "";
626
627  Me.m_current_cmtpath = Project::find_in_cmt_paths (current_use.path);
628
629  if (Me.m_current_cmtpath != "")
630    {
631      static const cmt_string empty_string;
632      static const cmt_string fs = CmtSystem::file_separator ();
633
634      Me.m_current_offset = current_use.path;
635
636      /**
637         try to remove this current CMTPATH entry from path.  This
638         has a meaning when the specified path already contains an
639         absolute path.
640      */
641     
642      Me.m_current_offset.replace (Me.m_current_cmtpath, empty_string);
643      if (Me.m_current_offset[0] == CmtSystem::file_separator ())
644        {
645          // Just in case there is a part left after removing the cmtpath entry
646         
647          Me.m_current_offset.replace (fs, empty_string);
648        }
649    }
650}
651
652class CmtMountFilterParser : public FAwk
653{
654public:
655
656  CmtMountFilterParser ()
657  {
658    reset ();
659  }
660
661  void reset ()
662  {
663    m_current_dir = CmtSystem::pwd ();
664
665    m_done = false;
666    m_prefix = "";
667  }
668
669  bool is_done () const
670  {
671    return (m_done);
672  }
673
674  const cmt_string& get_current_dir () const
675  {
676    return (m_current_dir);
677  }
678
679  void set_prefix (const cmt_string& prefix)
680  {
681    m_prefix = prefix;
682  }
683
684  void filter (const cmt_string& line)
685  {
686    if (m_done) 
687      {
688        stop ();
689        return;
690      }
691
692    CmtSystem::cmt_string_vector words;
693
694    CmtSystem::split (line, " \t", words);
695
696    int requested = 2;
697
698    if (m_prefix != "")
699      {
700        requested++;
701      }
702
703    if (words.size () < requested) return;
704
705    int n = 0;
706
707    if (m_prefix != "")
708      {
709        if (words[n] != m_prefix) return;
710        n++;
711      }
712
713    cmt_string& path_name = words[n];
714    cmt_string& replacement = words[n+1];
715   
716    if (m_current_dir.find (path_name) != cmt_string::npos)
717      {
718        m_current_dir.replace (path_name, replacement);
719        m_done = true;
720        stop ();
721      }
722  }
723
724private:
725  bool m_done;
726  cmt_string m_prefix;
727  cmt_string m_current_dir;
728};
729
730//----------------------------------------------------------
731void Cmt::configure_current_dir ()
732{
733  cmt_string file_name;
734
735  /*
736    Building current_dir :
737
738    o we first get the physical value (using getwd)
739    o then this value is possibly filtered using the
740    cmt_mount_filter file.
741  */
742
743  CmtMountFilterParser mount_filter;
744
745  /**
746     First try with ${CMTROOT}/mgr/cmt_mount_filter
747     with no prefix on lines
748  */
749
750  file_name = Me.m_default_path;
751  if (file_name != "")
752    {
753      file_name += CmtSystem::file_separator ();
754      file_name += "CMT";
755      file_name += CmtSystem::file_separator ();
756      file_name += Me.m_cmt_version;
757      file_name += CmtSystem::file_separator ();
758      file_name += "mgr";
759      file_name += CmtSystem::file_separator ();
760    }
761
762  file_name += "cmt_mount_filter";
763
764  mount_filter.run (file_name);
765
766  /**
767     Now try with .cmtrc
768     with "mount_filter" keyword
769  */
770
771  mount_filter.set_prefix ("mount_filter");
772
773  mount_filter.run (".cmtrc");
774
775  /**
776     Now try with ${HOME}/.cmtrc
777     with "mount_filter" keyword
778  */
779
780  if (CmtSystem::get_home_directory (file_name))
781    {
782      file_name += CmtSystem::file_separator ();
783      file_name += ".cmtrc";
784      mount_filter.run (file_name);
785    }
786
787  Me.m_current_dir = mount_filter.get_current_dir ();
788  Me.m_current_file_path = mount_filter.get_current_dir ();
789
790  cmt_string t = Me.m_current_file_path;
791}
792
793//----------------------------------------------------------
794void Cmt::configure_current_package ()
795{
796  /*
797    Build current_package and current_prefix.
798
799    This is only possible if we are within the cmt/mgr branch of a
800    standard directory tree (i.e. <package>/<version>/cmt or mgr)
801  */
802
803  cmt_string req = "..";
804  req += CmtSystem::file_separator ();
805  req += "cmt";
806  req += CmtSystem::file_separator ();
807  req += "requirements";
808
809  if (CmtSystem::test_file (req))
810    {
811      Me.m_current_style = cmt_style;
812    }
813  else
814    {
815      cmt_string req = "..";
816      req += CmtSystem::file_separator ();
817      req += "mgr";
818      req += CmtSystem::file_separator ();
819      req += "requirements";
820
821      if (CmtSystem::test_file (req))
822        {
823          Me.m_current_style = mgr_style;
824        }
825      else
826        {
827          // This package is probably a standalone one
828          Me.m_current_style = none_style;
829        }
830    }
831
832
833  if (Me.m_current_style != none_style)
834    {
835      //
836      //  Here there is a ../cmt or ../mgr branch in front of us
837      //  and there is a requirements file there
838      //
839
840      cmt_string up_dir;
841      cmt_string up_branch;
842
843      CmtSystem::dirname (Me.m_current_dir, up_dir);
844      CmtSystem::basename (up_dir, up_branch);
845
846      cmt_string cmt_dir = "..";
847      cmt_dir += CmtSystem::file_separator ();
848      switch (Me.m_current_style)
849        {
850        case cmt_style:
851          cmt_dir += "cmt";
852          break;
853        case mgr_style:
854          cmt_dir += "mgr";
855          break;
856        default:
857          break;
858        }
859      //      version_file += CmtSystem::file_separator ();
860      //      version_file += "version.cmt";
861
862      //      if (CmtSystem::test_file (version_file))
863      if (CmtSystem::test_file (cmt_dir
864                                + CmtSystem::file_separator ()
865                                + Package::get_version_file_name ())
866          )
867        {
868          //
869          // There is an explicit version descriptor. This one takes precedence
870          // and forces the structuring style to no directory
871          //
872
873          Me.m_current_package = up_branch;
874          CmtSystem::dirname (up_dir, Me.m_current_path);
875
876          /*
877          if (Me.m_current_version.read (version_file))
878            {
879              int pos;
880
881              pos = Me.m_current_version.find ('\n');
882              if (pos != cmt_string::npos) Me.m_current_version.erase (pos);
883              pos = Me.m_current_version.find ('\r');
884              if (pos != cmt_string::npos) Me.m_current_version.erase (pos);
885            }
886          else
887            {
888              Me.m_current_version = "v*";
889              CmtMessage::warning ("Could not read `" + version_file + "'."
890                                   " Default version `" + Me.m_current_version + "'");
891            }
892          */
893          cmt_string v;
894          //      if (Package::get_version (v, version_file))
895          if (Package::get_version (v, cmt_dir))
896            {
897              Me.m_current_version = v;
898            }
899          else
900            {
901              Me.m_current_version = "v*";
902              CmtMessage::warning ("Structuring style used `without_version_directory'."
903                                   " Could not determine version in `" + cmt_dir + "'."
904                                   " Default version `" + Me.m_current_version + "'");
905            }
906
907          if (Me.m_debug)
908            {
909              cout << "Cmt::configure_current_package>" << endl
910                   << " m_current_package " << Me.m_current_package << endl
911                   << " m_current_version " << Me.m_current_version << endl
912                   << " m_current_dir " << Me.m_current_dir << endl
913                   << " pwd " << CmtSystem::pwd ()
914                   << endl;
915            }
916
917          Me.m_current_structuring_style = without_version_directory;
918          //          Me.m_current_style = no_version_style;
919        }
920      else
921        {
922          // Try to use directory structure and
923          // package statement in requirements, if available
924          cmt_string up_up_dir;
925          cmt_string up_up_branch;
926          CmtSystem::dirname (up_dir, up_up_dir);
927          CmtSystem::basename (up_up_dir, up_up_branch);
928
929          cmt_string n;
930          bool done (false);
931          if (Package::get_name (n, cmt_dir))
932            {
933              if (n == up_branch)
934                {
935                  Me.m_current_package = up_branch;
936                  Me.m_current_path = up_up_dir;
937                  Me.m_current_structuring_style = without_version_directory;
938                  cmt_string v;
939                  if (Package::get_version (v, cmt_dir))
940                    {
941                      Me.m_current_version = v;
942                    }
943                  else
944                    {
945                      // No version descriptor
946                      // No version directory. The version is defaulted to v*
947                      Me.m_current_version = "v*";
948                      CmtMessage::warning ("Structuring style used `without_version_directory'."
949                                           " Could not determine version in `" + cmt_dir + "'."
950                                           " Default version `" + Me.m_current_version + "'");
951                    }
952                  done =true;
953                }
954              else if (n == up_up_branch)
955                {
956                  // The up branch IS a version directory.
957                  Me.m_current_version = up_branch;
958                  Me.m_current_package = up_up_branch;
959                  CmtSystem::dirname (up_up_dir, Me.m_current_path);
960                  Me.m_current_structuring_style = with_version_directory;
961                  done =true;
962                }
963              else
964                {
965                  CmtMessage::warning ("Package name `" + n + "' in `" + cmt_dir +
966                                       "' does not correspond to directory"
967                                       " structure");
968                }
969            }
970
971          if (!done)
972            {
973          // Try to determine the structuring style from:
974          //
975          CmtStructuringStyle structuring_style(default_structuring_style);
976          CmtStructuringStyle style(default_structuring_style);
977          // - the command line options
978          //   or
979          // - the CMTSTRUCTURINGSTYLE environment variable
980          if (structuring_style == default_structuring_style)
981            {
982              structuring_style = get_current_structuring_style ();
983            }
984          // - the project the package belongs to
985          if (structuring_style == default_structuring_style)
986            {
987              Project* p = Project::get_current ();
988              if (p)
989                {
990                  style = p->get_strategy ("VersionDirectory") ? with_version_directory : without_version_directory ;
991                  StrategyDef* def = StrategyMgr::find_strategy ("VersionDirectory");
992                  if (def != 0)
993                    {
994                      if (p->is_specified (def)) structuring_style = style;
995                    }
996                }
997            }
998          // - the child projects of the project the package belongs to
999          if (structuring_style == default_structuring_style)
1000            {
1001              structuring_style = style;
1002            }
1003          // - the default value of the Structure strategy
1004          if (structuring_style == default_structuring_style)
1005            {
1006              structuring_style = StrategyMgr::get_default_strategy ("VersionDirectory") ? with_version_directory : without_version_directory ;
1007            }
1008
1009          // Make use of the structuring style
1010          //
1011          if (structuring_style == with_version_directory && CmtSystem::is_version_directory (up_branch))
1012            {
1013              // The up branch IS a version directory.
1014              Me.m_current_version = up_branch;
1015              Me.m_current_package = up_up_branch;
1016              CmtSystem::dirname (up_up_dir, Me.m_current_path);
1017              /*
1018              CmtSystem::dirname (up_dir, up_dir);
1019              CmtSystem::basename (up_dir, Me.m_current_package);
1020              CmtSystem::dirname (up_dir, Me.m_current_path);
1021              */
1022              Me.m_current_structuring_style = with_version_directory;
1023            }
1024          else
1025            {
1026              if (structuring_style == with_version_directory)
1027                {
1028                  CmtMessage::warning ("`" + up_branch + "' is not correct version syntax."
1029                                       " Assuming it is package name");
1030                }
1031              Me.m_current_package = up_branch;
1032              Me.m_current_path = up_up_dir;
1033              //              CmtSystem::dirname (up_dir, Me.m_current_path);
1034              Me.m_current_structuring_style = without_version_directory;
1035              cmt_string v;
1036              if (Package::get_version (v, cmt_dir))
1037                {
1038                  Me.m_current_version = v;
1039                }
1040              else
1041                {
1042              // No version descriptor
1043              // No version directory. The version is defaulted to v*
1044                  Me.m_current_version = "v*";
1045                  CmtMessage::warning ("Structuring style used `without_version_directory'."
1046                                       " Could not determine version in `" + cmt_dir + "'."
1047                                       " Default version `" + Me.m_current_version + "'");
1048                }
1049             
1050              //              Me.m_current_style = no_version_style;
1051            }
1052            }
1053        }
1054      build_prefix (Me.m_current_package, Me.m_current_prefix);
1055      build_config (Me.m_current_prefix, Me.m_current_config);
1056    }
1057  else
1058    {
1059      Me.m_current_package = "cmt_standalone";
1060      Me.m_current_version = "v0";
1061      Me.m_current_path = Me.m_current_dir;
1062      // Do NOT (re)set m_current_structuring_style yet
1063      // (as requirements may NOT exist, so this is not a package)
1064      // rather do it in reach_current_package (if/when requirements is read)
1065      //Me.m_current_structuring_style = without_version_directory;
1066      build_prefix (Me.m_current_package, Me.m_current_prefix);
1067      build_config (Me.m_current_prefix, Me.m_current_config);
1068    }
1069}
1070
1071//----------------------------------------------------------
1072//void Cmt::configure_current_structuring_style ()
1073void Cmt::configure_current_structuring_style (const ArgParser& ap)
1074{
1075  cmt_string s;
1076  s = CmtSystem::getenv ("CMTSTRUCTURINGSTYLE");
1077  if (s == "without_version_directory")
1078    {
1079      Me.m_current_structuring_style = without_version_directory;
1080    }
1081  else if (s == "with_version_directory")
1082    {
1083      Me.m_current_structuring_style = with_version_directory;
1084    } 
1085
1086  CmtSystem::cmt_string_vector flags;
1087  Cmt::get_cmt_flags (flags);
1088  for (int i = 0; i < flags.size (); i++)
1089    {
1090      const cmt_string& flag = flags[i];
1091      if (flag == "-without_version_directory")
1092        {
1093          Me.m_current_structuring_style = without_version_directory;
1094        }
1095      else if (flag == "-with_version_directory")
1096        {
1097          Me.m_current_structuring_style = with_version_directory;
1098        } 
1099    }
1100
1101  for (int i = 1; i < ap.argc; i++)
1102    {
1103      const cmt_string& arg = ap.argv[i];
1104      if (arg[0] != '-') break;
1105      if (arg == "-without_v" ||
1106          arg == "-without_ve" ||
1107          arg == "-without_ver" ||
1108          arg == "-without_vers" ||
1109          arg == "-without_versi" ||
1110          arg == "-without_versio" ||
1111          arg == "-without_version" ||
1112          arg == "-without_version_" ||
1113          arg == "-without_version_d" ||
1114          arg == "-without_version_di" ||
1115          arg == "-without_version_dir" ||
1116          arg == "-without_version_dire" ||
1117          arg == "-without_version_direc" ||
1118          arg == "-without_version_direct" ||
1119          arg == "-without_version_directo" ||
1120          arg == "-without_version_director" ||
1121          arg == "-without_version_directory")
1122        {
1123          Me.m_current_structuring_style = without_version_directory;
1124          //      if (!CmtSystem::putenv ("CMTSTRUCTURINGSTYLE", "without_version_directory"))
1125          //        CmtMessage::error ("Cannot set `CMTSTRUCTURINGSTYLE' to"
1126          //                           " `without_version_directory' in the environment");
1127          if (!Cmt::add_cmt_flag ("-without_version_directory"))
1128            CmtMessage::error ("Cannot add flag `-without_version_directory'");
1129        }
1130      else if (arg == "-with_v" ||
1131               arg == "-with_ve" ||
1132               arg == "-with_ver" ||
1133               arg == "-with_vers" ||
1134               arg == "-with_versi" ||
1135               arg == "-with_versio" ||
1136               arg == "-with_version" ||
1137               arg == "-with_version_" ||
1138               arg == "-with_version_d" ||
1139               arg == "-with_version_di" ||
1140               arg == "-with_version_dir" ||
1141               arg == "-with_version_dire" ||
1142               arg == "-with_version_direc" ||
1143               arg == "-with_version_direct" ||
1144               arg == "-with_version_directo" ||
1145               arg == "-with_version_director" ||
1146               arg == "-with_version_directory")
1147        {
1148          Me.m_current_structuring_style = with_version_directory;
1149          //      if (!CmtSystem::putenv ("CMTSTRUCTURINGSTYLE", "with_version_directory"))
1150          //        CmtMessage::error ("Cannot set `CMTSTRUCTURINGSTYLE' to"
1151          //                           " `with_version_directory' in the environment");
1152          if (!Cmt::add_cmt_flag ("-with_version_directory"))
1153            CmtMessage::error ("Cannot add flag `-with_version_directory'");
1154        }
1155    }
1156}
1157
1158//----------------------------------------------------------
1159void Cmt::configure_default_path ()
1160{
1161  Me.m_default_path = CmtSystem::get_cmt_root ();
1162  CmtSystem::get_cmt_version (Me.m_cmt_version);
1163  Me.m_cmt_root = Me.m_default_path;
1164  Me.m_cmt_root += CmtSystem::file_separator ();
1165  Me.m_cmt_root += "CMT";
1166  Me.m_cmt_root += CmtSystem::file_separator ();
1167  Me.m_cmt_root += Me.m_cmt_version;
1168}
1169
1170//----------------------------------------------------------
1171void Cmt::configure_home (Use* use)
1172{
1173  Me.m_cmt_home = "";
1174
1175  Symbol* symbol = Symbol::find ("CMTHOME");
1176  if (symbol != 0)
1177    {
1178      Me.m_cmt_home = symbol->build_macro_value ();
1179      Symbol::expand (Me.m_cmt_home);
1180    }
1181  else if (CmtSystem::testenv ("CMTHOME"))
1182    {
1183      Me.m_cmt_home = CmtSystem::getenv ("CMTHOME");
1184    }
1185
1186  if ((Me.m_cmt_home != "") && !CmtSystem::test_directory (Me.m_cmt_home))
1187    {
1188      Me.m_cmt_home = "";
1189    }
1190
1191  configure_user_context (0);
1192}
1193
1194//----------------------------------------------------------
1195void Cmt::configure_user_context (Use* use)
1196{
1197  Me.m_cmt_user_context = "";
1198
1199  Symbol* symbol = Symbol::find ("CMTUSERCONTEXT");
1200  if (symbol != 0)
1201    {
1202      Me.m_cmt_user_context = symbol->build_macro_value ();
1203      Symbol::expand (Me.m_cmt_user_context);
1204    }
1205  else if (CmtSystem::testenv ("CMTUSERCONTEXT"))
1206    {
1207      Me.m_cmt_user_context = CmtSystem::getenv ("CMTUSERCONTEXT");
1208    }
1209
1210  if ((Me.m_cmt_user_context != "") && !CmtSystem::test_directory (Me.m_cmt_user_context))
1211    {
1212      Me.m_cmt_user_context = "";
1213    }
1214
1215  if (Me.m_debug) cout << "configure_user_context> user_context=" << Me.m_cmt_user_context << endl;
1216
1217  configure_cmt_path (0);
1218}
1219
1220//----------------------------------------------------------
1221void Cmt::configure_hosttype_tag ()
1222{
1223  cmt_string hosttype;
1224
1225  CmtSystem::get_hosttype (hosttype);
1226
1227  if (hosttype != "")
1228    {
1229      Tag* tag;
1230
1231      tag = Tag::add (hosttype, PriorityUname, "HOSTTYPE", 0);
1232      tag->mark ();
1233    }
1234}
1235
1236//----------------------------------------------------------
1237void Cmt::configure_site_tag (Use* use)
1238{
1239  Symbol* symbol = Symbol::find ("CMTSITE");
1240  if (symbol != 0)
1241    {
1242      Me.m_cmt_site = symbol->build_macro_value ();
1243      Symbol::expand (Me.m_cmt_site);
1244    }
1245  else
1246    {
1247      Me.m_cmt_site = CmtSystem::get_cmt_site ();
1248    }
1249
1250  if (Me.m_cmt_site != "")
1251    {
1252      cmt_string s = "CMTSITE";
1253
1254      if (use != 0)
1255        {
1256          s += " in ";
1257        }
1258
1259      Tag* tag;
1260
1261      tag = Tag::add (Me.m_cmt_site, PrioritySite, s, use);
1262      tag->mark ();
1263    }
1264}
1265
1266//----------------------------------------------------------
1267void Cmt::restore_all_tags (Use* use)
1268{
1269  //cerr << "restore_all_tags" << endl;
1270
1271  Cmt::configure_tags (use);
1272
1273  /*
1274    Then get existing extra tags
1275  */
1276
1277  if (CmtSystem::testenv ("CMTEXTRATAGS"))
1278    {
1279      cmt_string s = "CMTEXTRATAGS";
1280
1281      if (use != 0)
1282        {
1283          s += " in ";
1284        }
1285
1286      Tag* tag;
1287      CmtSystem::cmt_string_vector words;
1288     
1289      cmt_string tags = CmtSystem::getenv ("CMTEXTRATAGS");
1290     
1291      CmtSystem::split (tags, " \t,", words);
1292
1293      Me.m_extra_tags = ",";
1294     
1295      for (int i = 0; i < words.size (); i++)
1296        {
1297          const cmt_string& a = words[i];
1298
1299          Me.m_extra_tags += a;
1300          Me.m_extra_tags += ",";
1301         
1302          tag = Tag::add (a, PriorityUserTag, s, use);
1303         
1304          tag->mark ();
1305        }
1306    }
1307}
1308
1309//----------------------------------------------------------
1310void Cmt::configure_tags (Use* use)
1311{
1312  cmt_string config_tag;
1313
1314  Log;
1315
1316  log << "current_tag=" << Me.m_current_tag << log_endl;
1317
1318  Symbol* symbol = Symbol::find ("CMTCONFIG");
1319  if (symbol != 0)
1320    {
1321      bool show_set_hidden = false;
1322
1323      if (Me.m_action == action_show_set)
1324        {
1325          show_set_hidden = true;
1326          Me.m_action = action_none;
1327        }
1328
1329      config_tag = symbol->build_macro_value ();
1330      Symbol::expand (config_tag);
1331
1332      if (show_set_hidden)
1333        {
1334          show_set_hidden = false;
1335          Me.m_action = action_show_set;
1336        }
1337    }
1338  else if (CmtSystem::testenv ("CMTCONFIG"))
1339    {
1340      config_tag = CmtSystem::getenv ("CMTCONFIG");
1341    }
1342  else if (CmtSystem::testenv ("CMTBIN"))
1343    {
1344      config_tag = CmtSystem::getenv ("CMTBIN");
1345    }
1346
1347  if (config_tag == "")
1348    {
1349      CmtSystem::get_uname (config_tag);
1350    }
1351
1352  log << "current_tag=" << Me.m_current_tag << log_endl;
1353
1354  cmt_string s = "CMTCONFIG";
1355
1356  if (use != 0)
1357    {
1358      s += " in ";
1359    }
1360
1361  Tag* tag;
1362
1363  tag = Tag::add (config_tag, PriorityConfig, s, use);
1364  tag->mark ();
1365
1366  //Me.m_current_tag = config_tag;
1367}
1368
1369//----------------------------------------------------------
1370void Cmt::configure_uname_tag ()
1371{
1372  cmt_string uname;
1373
1374  CmtSystem::get_uname (uname);
1375
1376  if (uname != "")
1377    {
1378      Tag* tag;
1379
1380      tag = Tag::add (uname, PriorityUname, "uname", 0);
1381      tag->mark ();
1382    }
1383}
1384
1385//----------------------------------------------------------
1386void Cmt::configure_devenv_tag ()
1387{
1388  cmt_string devenv_command;
1389  Symbol* devenv_command_macro = Symbol::find ("devenv_command");
1390  if (0 == devenv_command_macro) return;
1391
1392  devenv_command = devenv_command_macro->resolve_macro_value ();
1393  if (devenv_command == "") return;
1394  devenv_command += " 2>&1";
1395
1396  cmt_string output;
1397  if (0 != CmtSystem::execute (devenv_command, output)) return;
1398  cmt_string devenv;
1399  int pos;
1400  pos = output.find ("Microsoft");
1401  if (pos != cmt_string::npos) devenv = "vs";
1402  else return;
1403
1404  //
1405  // Make the output as one single big line.
1406  //
1407
1408  output.replace_all ("\n", " ");
1409  output.replace_all ("\r", " ");
1410
1411  CmtSystem::cmt_string_vector words;
1412  CmtSystem::split (output, " \t", words);
1413
1414  cmt_string version;
1415  bool need_version (false);
1416  for (int i = 0; i < words.size (); i++)
1417    {
1418      if (need_version && words[i] != "")
1419        {
1420          version = words[i];
1421          break;
1422        }
1423      if (words[i] == "Version")
1424        need_version = true;
1425    }
1426
1427  if (version == "")
1428    {
1429      devenv += "00";
1430    }
1431  else
1432    {
1433      CmtSystem::cmt_string_vector nbrs;
1434      CmtSystem::split (version, ".", nbrs);
1435      if (1 == nbrs.size ())
1436        devenv += nbrs[0];
1437      else
1438        devenv += nbrs[0] + nbrs[1];
1439    }
1440
1441  if (devenv != "")
1442    {
1443      Tag* tag;
1444
1445      tag = Tag::add (devenv, PriorityUname, "devenv", 0);
1446      tag->mark ();
1447    }
1448}
1449
1450//----------------------------------------------------------
1451void Cmt::configure_version_tag ()
1452{
1453  int v = 0;
1454  int r = 0;
1455  int p = 0;
1456
1457  CmtSystem::is_version_directory (CMTVERSION, v, r, p);
1458
1459  Tag* tag;
1460
1461  static char temp[80];
1462
1463  sprintf (temp, "CMTv%d", v);
1464
1465  tag = Tag::add (temp, PriorityVersion, "CMTVERSION", 0);
1466  tag->mark ();
1467
1468  sprintf (temp, "CMTr%d", r);
1469
1470  tag = Tag::add (temp, PriorityVersion, "CMTVERSION", 0);
1471  tag->mark ();
1472
1473  sprintf (temp, "CMTp%d", p);
1474
1475  tag = Tag::add (temp, PriorityVersion, "CMTVERSION", 0);
1476  tag->mark ();
1477
1478}
1479
1480//----------------------------------------------------------
1481//void Cmt::configure_cmt_message ()
1482void Cmt::configure_cmt_message (const ArgParser& ap)
1483{
1484  cmt_string s;
1485
1486  s = CmtSystem::getenv ("CMTMSGPREFIX");
1487  if (s != "")
1488    CmtMessage::set_prefix (s);
1489
1490  s = CmtSystem::getenv ("CMTFATAL");
1491  if (s != "")
1492    {
1493      CmtMessage::set_level (Fatal);
1494    }
1495
1496  s = CmtSystem::getenv ("CMTERROR");
1497  if (s != "")
1498    {
1499      CmtMessage::set_level (Error);
1500    }
1501
1502  s = CmtSystem::getenv ("CMTWARNING");
1503  if (s != "")
1504    {
1505      CmtMessage::set_level (Warning);
1506    }
1507
1508  s = CmtSystem::getenv ("CMTINFO");
1509  if (s != "")
1510    {
1511      CmtMessage::set_level (Info);
1512    }
1513
1514  s = CmtSystem::getenv ("CMTVERBOSE");
1515  if (s != "")
1516    {
1517      CmtMessage::set_level (Verbose);
1518    }
1519
1520  s = CmtSystem::getenv ("CMTDEBUG");
1521  if (s != "")
1522    {
1523      CmtMessage::set_level (Debug);
1524    }
1525
1526  CmtSystem::cmt_string_vector flags;
1527  Cmt::get_cmt_flags (flags);
1528  for (int i = 0; i < flags.size (); i++)
1529    {
1530      const cmt_string& flag = flags[i];
1531      if (flag == "-disable_warnings")
1532        {
1533          Me.m_disable_warnings = true;
1534          if (CmtMessage::level () <= Warning)
1535            CmtMessage::set_level (Error);
1536        }
1537      else if (flag == "-quiet")
1538        {
1539          Me.m_quiet = true;
1540          if (CmtMessage::level () <= Error)
1541            CmtMessage::set_level (Fatal);
1542        }
1543      else if (flag == "-warnings")
1544        {
1545          Me.m_warnings = true;
1546          if (CmtMessage::level () != Warning)
1547            CmtMessage::set_level (Warning);
1548          //      if (CmtMessage::level () > Warning)
1549        }
1550    }
1551
1552  for (int i = 1; i < ap.argc; i++)
1553    {
1554      const cmt_string& arg = ap.argv[i];
1555      if (arg[0] != '-') break;
1556      if (arg == "-d" ||
1557          arg == "-di" ||
1558          arg == "-dis" ||
1559          arg == "-disa" ||
1560          arg == "-disab" ||
1561          arg == "-disabl" ||
1562          arg == "-disable" ||
1563          arg == "-disable_" ||
1564          arg == "-disable_w" ||
1565          arg == "-disable_wa" ||
1566          arg == "-disable_war" ||
1567          arg == "-disable_warn" ||
1568          arg == "-disable_warni" ||
1569          arg == "-disable_warnin" ||
1570          arg == "-disable_warning" ||
1571          arg == "-disable_warnings")
1572        {
1573          Me.m_disable_warnings = true;
1574          if (CmtMessage::level () <= Warning)
1575            {
1576              CmtMessage::set_level (Error);
1577              //              if (!CmtSystem::putenv ("CMTERROR", "1"))
1578              //                CmtMessage::error ("Cannot set `CMTERROR' in the environment");
1579            }
1580          if (!Cmt::add_cmt_flag ("-disable_warnings"))
1581            CmtMessage::error ("Cannot add flag `-disable_warnings'");
1582        }
1583      else if (arg == "-q" ||
1584               arg == "-qu" ||
1585               arg == "-qui" ||
1586               arg == "-quie" ||
1587               arg == "-quiet")
1588        {
1589          Me.m_quiet = true;
1590          if (CmtMessage::level () <= Error)
1591            {
1592              CmtMessage::set_level (Fatal);
1593              //              if (!CmtSystem::putenv ("CMTFATAL", "1"))
1594              //                CmtMessage::error ("Cannot set `CMTFATAL' in the environment");
1595            }
1596          if (!Cmt::add_cmt_flag ("-quiet"))
1597            CmtMessage::error ("Cannot add flag `-quiet'");
1598        }
1599      else if (arg == "-warn" ||
1600               arg == "-warni" ||
1601               arg == "-warnin" ||
1602               arg == "-warning" ||
1603               arg == "-warnings")
1604        {
1605          Me.m_warnings = true;
1606          //      if (CmtMessage::level () > Warning)
1607          if (CmtMessage::level () != Warning)
1608            {
1609              CmtMessage::set_level (Warning);
1610              //              if (!CmtSystem::putenv ("CMTWARNING", "1"))
1611              //                CmtMessage::error ("Cannot set `CMTWARNING' in the environment");
1612            }
1613          if (!Cmt::add_cmt_flag ("-warnings"))
1614            CmtMessage::error ("Cannot add flag `-warnings'");
1615        }
1616    }
1617}
1618
1619//----------------------------------------------------------
1620//
1621//   Actions
1622//
1623//----------------------------------------------------------
1624
1625class AwkActor : public Awk
1626{
1627public:
1628
1629  void filter (const cmt_string& line)
1630  {
1631    cout << line << endl;
1632  }
1633};
1634
1635//----------------------------------------------------------
1636void Cmt::do_awk (const ArgParser& ap)
1637{
1638  if (ap.arguments.size () < 1)
1639    {
1640      CmtMessage::error ("cmt awk <file> <pattern>");
1641      //      cerr << "#CMT> cmt awk <file> <pattern>" << endl;
1642      return;
1643    }
1644
1645  const cmt_string& file = ap.arguments[0];
1646  const cmt_string& pattern = ap.arguments[1];
1647  cmt_string text;
1648
1649  text.read (file);
1650
1651  static AwkActor a;
1652
1653  cmt_regexp exp (pattern);
1654
1655  a.run (text, exp);
1656}
1657
1658//----------------------------------------------------------
1659void Cmt::do_broadcast (const ArgParser& ap)
1660{
1661  Use::UsePtrVector& Uses = Use::get_ordered_uses ();
1662
1663  CmtSystem::cmt_string_vector uses;
1664  CmtSystem::cmt_string_vector packages;
1665  CmtSystem::cmt_string_vector versions;
1666  CmtSystem::cmt_string_vector path_selections;
1667  CmtSystem::cmt_string_vector selections;
1668  CmtSystem::cmt_string_vector exclusions;
1669  cmt_string begin;
1670  cmt_string command;
1671  bool is_cmt = false;
1672  int first = 0;
1673  int i;
1674  bool ignore_errors = false;
1675  bool all_packages = false;
1676
1677  bool local = true;
1678
1679  for (i = 0; i < ap.arguments.size (); i++)
1680    {
1681      const cmt_string& w = ap.arguments[i];
1682
1683      //cerr << "broadcast: arg=[" << w << "]" << endl;
1684
1685      if (command == "")
1686        {
1687          if (w.substr (0, 13) == "-all_packages")
1688            {
1689              local = false;
1690              all_packages = true;
1691            }
1692          else if (w.substr (0, 7) == "-depth=")
1693            {
1694              local = false;
1695
1696              cmt_string depth_str;
1697              int depth_value = 0;
1698                         
1699              w.substr (7, depth_str);
1700              if ((sscanf (depth_str.c_str (), "%d", &depth_value) < 1) ||
1701                  (depth_value < 1))
1702                {
1703                  // Syntax error
1704                  //  We shall restrict to packages found within
1705                  // the <depth_value> first elements of CMTPATH.
1706                  //  If CMTPATH is empty, nothing is selected.
1707                  // depth=1 is equivalent to local
1708                }
1709
1710              Project::fill_selection (depth_value, path_selections);
1711            }
1712          else if (w.substr (0, 9) == "-exclude=")
1713            {
1714              cmt_string exclusion;
1715
1716              w.substr (9, exclusion);
1717
1718              int size = exclusion.size ();
1719             
1720              if (size >= 2)
1721                {
1722                  if (((exclusion[0] == '"') && (exclusion[size - 1] == '"')) ||
1723                      ((exclusion[0] == '\'') && (exclusion[size - 1] == '\'')))
1724                    {
1725                      exclusion.erase (size - 1);
1726                      exclusion.erase (0, 1);
1727                    }
1728
1729                  CmtSystem::split (exclusion, " \t", exclusions);
1730                }
1731            }
1732          else if (w.substr (0, 7) == "-global")
1733            {
1734              path_selections.clear ();
1735
1736              local = false;
1737            }
1738          else if (w.substr (0, 6) == "-local")
1739            {
1740              local = true;
1741            }
1742          else if (w.substr (0, 8) == "-select=")
1743            {
1744              cmt_string selection;
1745
1746              w.substr (8, selection);
1747
1748              //cerr << "Select=[" << selection << "]" << endl;
1749
1750              int size = selection.size ();
1751             
1752              if (size >= 2)
1753                {
1754                  if (((selection[0] == '"') && (selection[size - 1] == '"')) ||
1755                      ((selection[0] == '\'') && (selection[size - 1] == '\'')))
1756                    {
1757                      selection.erase (size - 1);
1758                      selection.erase (0, 1);
1759                    }
1760                }
1761
1762              CmtSystem::split (selection, " \t", selections);
1763
1764              //cerr << "Selections.size () = " << selections.size () << endl;
1765            }
1766          else if (w.substr (0, 7) == "-begin=")
1767            {
1768              w.substr (7, begin);
1769            }
1770          else
1771            {
1772              command = w;
1773            }
1774        }
1775      else
1776        {
1777          command += " ";
1778          command += w;
1779        }
1780
1781    }
1782
1783  if (local)
1784    {
1785      Project::fill_selection (1, path_selections);
1786    }
1787
1788  if (command[0] == '-')
1789    {
1790      ignore_errors = true;
1791      command.erase (0, 1);
1792    }
1793
1794  //if (command.substr (0, 3) == "cmt") is_cmt = true;
1795
1796  cmt_string curuse = CmtSystem::pwd ();
1797  if (all_packages)
1798    {
1799      PackageSelector selector (uses);
1800      PathScanner scanner;
1801      Project::scan_paths (scanner, selector);
1802    }
1803  else
1804    {
1805      for (i = Uses.size () - 1; i >= 0; i--)
1806        {
1807          Use* use = Uses[i];
1808                 
1809          if (use->discarded) continue;
1810          if (use->m_hidden) continue;
1811
1812          if (!use->located ())
1813            {
1814              CmtMessage::warning ("package " + use->get_package_name ()
1815                                   + " " + use->version + " " + use->path
1816                                   + " not found");
1817              /*
1818              if (!Me.m_quiet)
1819                {
1820                  cerr << "#CMT> package " << use->get_package_name () <<
1821                    " " << use->version << " " << use->path <<
1822                    " not found" <<
1823                    endl;
1824                }
1825              */
1826            }
1827          else
1828            {
1829              if (use->get_package_name () != "CMT")
1830                {
1831                  cmt_string& s = uses.add ();
1832
1833                  use->get_full_path (s);
1834
1835                  s += CmtSystem::file_separator ();
1836                  if (use->style == mgr_style) s += "mgr";
1837                  else s += "cmt";
1838
1839                  cmt_string& v = versions.add ();
1840                  v = use->version;
1841
1842                  cmt_string& p = packages.add ();
1843                  p = use->get_package_name ();
1844                }
1845            }
1846        }
1847         
1848      {
1849        cmt_string& s = uses.add ();
1850                 
1851        Use* use = &(Use::current ());
1852
1853        if (use->get_package_name ().find ("cmt_standalone") != cmt_string::npos)
1854          {
1855            s = CmtSystem::pwd ();
1856          }
1857        else
1858          {
1859            use->get_full_path (s);
1860
1861            s += CmtSystem::file_separator ();
1862                 
1863            if (use->style == mgr_style) s += "mgr";
1864            else s += "cmt";
1865          }
1866
1867        curuse = s;
1868
1869        cmt_string& v = versions.add ();
1870        v = use->version;
1871
1872        cmt_string& p = packages.add ();
1873        p = use->get_package_name ();
1874      }
1875    }
1876
1877  bool started = false;
1878
1879  if (begin == "") started = true;
1880
1881  set_standard_macros ();
1882  Cmt::reset_all_sets_done ();
1883  Symbol::all_set ();
1884
1885  for (i = 0; i < uses.size (); i++)
1886    {
1887      const cmt_string& s = uses[i];
1888      const cmt_string& v = versions[i];
1889      const cmt_string& p = packages[i];
1890      cmt_string cmtpath;
1891
1892      bool ok = true;
1893      bool selected = true;
1894      bool excluded = false;
1895
1896      /**
1897         Ensure that the current package is not skipped
1898         due to symlinks in the paths
1899       */
1900      //      if (path_selections.size () > 0)
1901      if (path_selections.size () > 0 && s != curuse)
1902        {
1903          selected = false;
1904
1905          for (int j = 0; j < path_selections.size (); j++)
1906            {
1907              const cmt_string& sel = path_selections[j];
1908             
1909              if (s.find (sel) != cmt_string::npos) 
1910                {
1911                  cmtpath = sel;
1912                  selected = true;
1913                  break;
1914                }
1915            }
1916
1917          ok = selected;
1918        }
1919
1920      if (ok)
1921        {
1922          if (selections.size () > 0)
1923            {
1924              selected = false;
1925             
1926              for (int j = 0; j < selections.size (); j++)
1927                {
1928                  const cmt_string& sel = selections[j];
1929                 
1930                  if (s.find (sel) != cmt_string::npos) 
1931                    {
1932                      selected = true;
1933                      break;
1934                    }
1935                }
1936             
1937              ok = selected;
1938            }
1939        }
1940
1941      if (ok && !started)
1942        {
1943          if (s.find (begin) != cmt_string::npos)
1944            {
1945              started = true;
1946              ok = true;
1947            }
1948          else
1949            {
1950              ok = false;
1951            }
1952        }
1953
1954
1955      if (ok)
1956        {
1957          excluded = false;
1958
1959          for (int j = 0; j < exclusions.size (); j++)
1960            {
1961              const cmt_string& exc = exclusions[j];
1962             
1963              if (s.find (exc) != cmt_string::npos) 
1964                {
1965                  excluded = true;
1966                  break;
1967                }
1968            }
1969
1970          if (excluded) ok = false;
1971        }
1972
1973      if (!ok) 
1974        {
1975          continue;
1976        }
1977
1978
1979
1980      if (!CmtSystem::cd (s))
1981        {
1982          if (s.find ("cmt_standalone") != cmt_string::npos)
1983            {
1984              CmtMessage::warning ("Currently not in a CMT package");
1985              //              cerr << "#CMT> Currently not in a CMT package" << endl;
1986            }
1987          else
1988            {
1989              char num[32]; sprintf (num, "%d/%d", i+1, uses.size ());
1990              CmtMessage::error ("Cannot move to the package in " + s
1991                                 + " (" + num + ")");
1992              /*
1993              cerr << "#CMT> Cannot move to the package in " << s << " (" << i+1 << "/"
1994                   << uses.size () << ")"<< endl;
1995              */
1996            }
1997
1998          if (!ignore_errors) break;
1999
2000          continue;
2001        }
2002
2003      if (CmtLock::check () == CmtLock::locked_by_another_user)
2004        {
2005          char num[32]; sprintf (num, "%d/%d", i+1, uses.size ());
2006          CmtMessage::warning ("Ignore locked package in " + s
2007                               + " (" + num + ")");
2008          /*
2009          cerr << "#CMT> Ignore locked package in " << s << " (" << i+1 << "/"
2010               << uses.size () << ")" << endl;
2011          */
2012          continue;
2013        }
2014
2015      if (cmtpath == "")
2016        {
2017          cmt_string sel = CmtSystem::pwd ();
2018          cmtpath = Project::find_in_cmt_paths (sel);
2019        }
2020
2021      cmt_string cmd = command;
2022      static const cmt_string version_template = "<version>";
2023      cmd.replace_all (version_template, v);
2024
2025      static const cmt_string package_template = "<package>";
2026      cmd.replace_all (package_template, p);
2027
2028      static const cmt_string cmtpath_template = "<package_cmtpath>";
2029      cmd.replace_all (cmtpath_template, cmtpath);
2030
2031      static const cmt_string offset_template = "<package_offset>";
2032
2033      static const cmt_string empty_string;
2034      static const cmt_string fs = CmtSystem::file_separator ();
2035      cmt_string offset = s;
2036      offset.replace (cmtpath, empty_string);
2037      if (offset[0] == CmtSystem::file_separator ())
2038        {
2039          offset.replace (fs, empty_string);
2040        }
2041      CmtSystem::dirname (offset, offset);
2042
2043      cmt_string n;
2044      CmtSystem::basename (offset, n);
2045      if (n == p)
2046        {
2047          CmtSystem::dirname (offset, offset);
2048        }
2049      else
2050        {
2051          CmtSystem::dirname (offset, offset);
2052          CmtSystem::dirname (offset, offset);
2053        }
2054
2055      cmd.replace_all (offset_template, offset);
2056
2057
2058  if (CmtMessage::active (Error))
2059    {
2060      cout << "#--------------------------------------------------------------" << endl;
2061      cout << "# Now trying [" << cmd << "] in " << s << " (" << i+1 << "/" << uses.size () 
2062           << ")" << endl;
2063      cout << "#--------------------------------------------------------------" << endl;
2064    }
2065
2066      if (is_cmt)
2067        {
2068          //
2069          //  There is a bug in the recursive use of the parser. Macros are not set correctly.
2070          //  Thus the recursive optimization is now discarded.
2071          //
2072          if (parser (cmd) != 0)
2073            {
2074              CmtError::set (CmtError::execution_error, cmd);
2075              break;
2076            }
2077        }
2078      else
2079        {
2080          int status = CmtSystem::execute (cmd);
2081
2082          //cerr << "do_broadcast> status=" << status << " ignore_errors=" << ignore_errors << endl;
2083
2084          if ((status != 0) && !ignore_errors)
2085            //if ((status != 0) && !ignore_errors)
2086            {
2087              //              if (status != 2) CmtError::set (CmtError::execution_error, cmd);
2088              CmtError::set (CmtError::execution_error, cmd);
2089              break;
2090            }
2091        }
2092    }
2093}
2094
2095//----------------------------------------------------------
2096void Cmt::do_build_constituent_makefile (const ArgParser& ap)
2097{
2098  if (CmtLock::check () == CmtLock::locked_by_another_user)
2099    {
2100      CmtError::set (CmtError::conflicting_lock, "build_constituent_makefile>");
2101      return;
2102    }
2103  set_standard_macros ();
2104
2105  cmt_string out, name;
2106  for (int i = 0; i < ap.arguments.size (); i++)
2107    {
2108      const cmt_string& arg = ap.arguments[i];
2109      if (arg.substr (0, 5) == "-out=")
2110        arg.substr (5, out);
2111      else
2112        name = arg;
2113    }
2114
2115  const Constituent* constituent = Constituent::find (name);
2116  if (constituent == 0)
2117    {
2118      CmtError::set (CmtError::constituent_not_found, name);
2119      return;
2120    }
2121  Generator::build_constituent_makefile (*constituent, out);
2122//   if (ap.arguments.size () > 0)
2123//     {
2124//       set_standard_macros ();
2125//       Generator::build_constituent_makefile (ap.arguments);
2126//     }
2127}
2128
2129//----------------------------------------------------------
2130void Cmt::do_build_constituent_config (const ArgParser& ap)
2131{
2132  if (CmtLock::check () == CmtLock::locked_by_another_user)
2133    {
2134      CmtError::set (CmtError::conflicting_lock, "build_constituent_config>");
2135      return;
2136    }
2137  set_standard_macros ();
2138
2139  CmtSystem::cmt_string_vector files (2);
2140  cmt_string  out, outdir, name;
2141  for (int i = 0; i < ap.arguments.size (); i++)
2142    {
2143      const cmt_string arg = ap.arguments[i];
2144      if (arg.substr (0, 5) == "-out=")
2145        {
2146          arg.substr (5, out);
2147          CmtSystem::dirname (out, outdir);
2148        }
2149      else
2150        {
2151          name = arg;
2152        }
2153    }
2154
2155  //  Generator::build_constituent_makefile (ap.arguments);
2156
2157  const Constituent* constituent = Constituent::find (name);
2158  if (constituent == 0)
2159    {
2160      CmtError::set (CmtError::constituent_not_found, name);
2161      return;
2162    }
2163  Generator::build_constituent_makefile (*constituent, out);
2164  if (!constituent->has_target_tag) return;
2165  //  cerr << "constituent: " << name << endl;
2166
2167  cmt_string tag;
2168  {
2169    Symbol* macro = Symbol::find ("tag");
2170    if (macro != 0)
2171      {
2172        tag = macro->build_macro_value ();
2173        Symbol::expand (tag);
2174      }
2175  }
2176
2177  //cmt_local_tagfile = $(bin)$(tag).make
2178  //cmt_final_setup = $(bin)setup.make
2179  //cmt_local_tagfile_${CONSTITUENT} = $(bin)$(${PACKAGE}_tag)_${CONSTITUENT}.make
2180  //cmt_final_setup_${CONSTITUENT} = $(bin)setup_${CONSTITUENT}.make
2181  files[0] = tag + "_" + name;
2182  files[1] = "setup_" + name;
2183  for (int i = 0; i < files.size (); i++)
2184    {
2185      if (Cmt::build_nmake ())
2186        files[i] += ".nmake";
2187      else
2188        files[i] += ".make";
2189
2190      if (outdir != "")
2191        {
2192          if (outdir [outdir.size () - 1] != CmtSystem::file_separator ())
2193            outdir += CmtSystem::file_separator ();
2194          files[i] = outdir + files[i];
2195        }
2196      //      cerr << "files[" << i << "]: " << files[i] << endl;
2197    }
2198
2199  ofstream t (files[0]);
2200  if (!t)
2201    {
2202      CmtError::set (CmtError::file_access_error, files[0]);
2203      return;
2204    }
2205  t.exceptions (ios::failbit | ios::badbit);
2206  try
2207    {
2208      print_macros (Make, "", t);
2209      t.close (); // ios_base::failbit
2210    }
2211  catch (const ios::failure& e)
2212    {
2213      CmtSystem::close_ostream (NULL, files[0] + ": " + cmt_string (e.what ()));
2214    }
2215
2216  bool gen_setup (true);
2217  ostringstream os;
2218  ofstream s;
2219  s.open (files[1], ios::in);
2220  if (s) // file already exists
2221    {
2222      //      cerr << files[1] << " already exists" << endl;
2223      do_show_setup (ap, os);
2224      ostringstream osn;
2225      osn << s.rdbuf ();
2226      if (os.str () == osn.str ())
2227        {
2228          //      cerr << files[1] << " up-to-date" << endl;
2229          gen_setup = false;
2230        }
2231    }
2232  s.clear ();
2233  s.close ();
2234  s.clear ();
2235  if (gen_setup)
2236    {
2237      s.open (files[1]);
2238      if (!s)
2239        {
2240          CmtError::set (CmtError::file_access_error, files[1]);
2241          return;
2242        }
2243      s.exceptions (ios::failbit | ios::badbit);
2244      try
2245        {
2246          if (os.str ().size () != 0)
2247            {
2248              //              cerr << files[1] << " contents already generated" << endl;
2249              s << os.str ();
2250            }
2251          else
2252            {
2253              do_show_setup (ap, s);
2254            }
2255          s.close (); // ios_base::failbit
2256        }
2257      catch (const ios::failure& e)
2258        {
2259          CmtSystem::close_ostream (NULL, files[1] + ": " + cmt_string (e.what ()));
2260        }
2261    }
2262}
2263
2264//----------------------------------------------------------
2265void Cmt::do_build_constituents_makefile (const ArgParser& ap)
2266{
2267  if (CmtLock::check () == CmtLock::locked_by_another_user)
2268    {
2269      CmtError::set (CmtError::conflicting_lock, "build_constituents_makefile>");
2270      return;
2271    }
2272  set_standard_macros ();
2273
2274  cmt_string out;
2275  for (int i = 0; i < ap.arguments.size (); i++)
2276    {
2277      const cmt_string& arg = ap.arguments[i];
2278      if (arg.substr (0, 5) == "-out=")
2279        {
2280          arg.substr (5, out);
2281        }
2282    }
2283
2284  Generator::build_constituents_makefile (Me.m_current_package, out);
2285  //  Generator::build_constituents_makefile (Me.m_current_package, ap.arguments);
2286}
2287
2288//----------------------------------------------------------
2289void Cmt::do_build_constituents_config (const ArgParser& ap)
2290{
2291  if (CmtLock::check () == CmtLock::locked_by_another_user)
2292    {
2293      CmtError::set (CmtError::conflicting_lock, "build_constituents_config>");
2294      return;
2295    }
2296  set_standard_macros ();
2297
2298  CmtSystem::cmt_string_vector files (2);
2299  cmt_string out, outdir;
2300  bool usecmt (true);
2301  for (int i = 0; i < ap.arguments.size (); i++)
2302    {
2303      const cmt_string& arg = ap.arguments[i];
2304      if (arg.substr (0, 5) == "-out=")
2305        {
2306          arg.substr (5, out);
2307          CmtSystem::dirname (out, outdir);
2308        }
2309      else if (arg == "-with_cmt")
2310        usecmt = true;
2311      else if (arg == "-without_cmt")
2312        usecmt = false;
2313    }
2314  //  cerr << "outdir: " << outdir << endl;
2315
2316  cmt_string tag;
2317  {
2318    Symbol* macro = Symbol::find ("tag");
2319    if (macro != 0)
2320      {
2321        tag = macro->build_macro_value ();
2322        Symbol::expand (tag);
2323      }
2324  }
2325
2326  //cmt_local_tagfile = $(bin)$(tag).make
2327  //cmt_final_setup = $(bin)setup.make
2328  files[0] = tag;
2329  files[1] = "setup";
2330  for (int i = 0; i < files.size (); i++)
2331    {
2332      if (Cmt::build_nmake ())
2333        files[i] += ".nmake";
2334      else
2335        files[i] += ".make";
2336
2337      if (outdir != "")
2338        {
2339          if (outdir [outdir.size () - 1] != CmtSystem::file_separator ())
2340            outdir += CmtSystem::file_separator ();
2341          files[i] = outdir + files[i];
2342        }
2343      //      cerr << "files[" << i << "]: " << files[i] << endl;
2344    }
2345
2346  //  files[0] = "/dev/full";
2347  ofstream t (files[0]);
2348  if (!t)
2349    {
2350      CmtError::set (CmtError::file_access_error, files[0]);
2351      return;
2352    }
2353  t.exceptions (ios::failbit | ios::badbit);
2354  try
2355    {
2356      print_macros (Make, "", t);
2357      t.close (); // ios_base::failbit
2358    }
2359  catch (const ios::failure& e)
2360    {
2361      CmtSystem::close_ostream (NULL, files[0] + ": " + cmt_string (e.what ()));
2362    }
2363
2364  bool gen_setup (true);
2365  ostringstream os;
2366  ofstream s;
2367  s.open (files[1], ios::in);
2368  if (s) // file already exists
2369    {
2370      //      cerr << files[1] << " already exists" << endl;
2371      do_show_setup (ap, os);
2372      ostringstream osn;
2373      osn << s.rdbuf ();
2374      if (os.str () == osn.str ())
2375        {
2376          //      cerr << files[1] << " up-to-date" << endl;
2377          gen_setup = false;
2378        }
2379    }
2380  s.clear ();
2381  s.close ();
2382  s.clear ();
2383  if (gen_setup)
2384    {
2385      s.open (files[1]);
2386      if (!s)
2387        {
2388          CmtError::set (CmtError::file_access_error, files[1]);
2389          return;
2390        }
2391      s.exceptions (ios::failbit | ios::badbit);
2392      try
2393        {
2394          if (os.str ().size () != 0)
2395            {
2396              //              cerr << files[1] << " contents already generated" << endl;
2397              s << os.str ();
2398            }
2399          else
2400            {
2401              do_show_setup (ap, s);
2402            }
2403          s.close (); // ios_base::failbit
2404        }
2405      catch (const ios::failure& e)
2406        {
2407          CmtSystem::close_ostream (NULL, files[1] + ": " + cmt_string (e.what ()));
2408        }
2409    }
2410
2411  Generator::build_constituents_makefile (Me.m_current_package, out);
2412  //  Generator::build_constituents_makefile (Me.m_current_package, ap.arguments);
2413
2414  const Constituent::ConstituentVector& constituents =
2415    Constituent::constituents ();
2416  for (int n = 0; n < constituents.size (); n++)
2417    {
2418      const Constituent& constituent = constituents[n];
2419      if (constituent.has_target_tag) continue;
2420      Generator::build_constituent_infile (constituent, outdir, usecmt);
2421    }
2422}
2423
2424//----------------------------------------------------------
2425void Cmt::do_build_dependencies (const ArgParser& ap,
2426                                 int argc,
2427                                 char* argv[])
2428{
2429  if (CmtLock::check () == CmtLock::locked_by_another_user)
2430    {
2431      CmtError::set (CmtError::conflicting_lock, "build_dependencies>");
2432      return;
2433    }
2434  if (ap.arguments.size () > 0)
2435    {
2436      set_standard_macros ();
2437
2438      Generator::build_dependencies (ap.arguments);
2439    }
2440}
2441
2442//----------------------------------------------------------
2443void Cmt::do_build_library_links (const ArgParser& ap)
2444{
2445  cmt_string cmtinstallarea = "";
2446  cmt_string tag = "";
2447  cmt_string shlibsuffix;
2448  cmt_string symlinkcmd;
2449
2450  if (CmtLock::check () == CmtLock::locked_by_another_user)
2451    {
2452      CmtError::set (CmtError::conflicting_lock, "build_library_links>");
2453      return;
2454    }
2455
2456  set_standard_macros ();
2457
2458  Use::UsePtrVector& Uses = Use::get_ordered_uses ();
2459  Use& current_use = Use::current ();
2460  int i;
2461
2462  {
2463    Symbol* macro = Symbol::find ("shlibsuffix");
2464    if (macro == 0) return;
2465    shlibsuffix = macro->build_macro_value ();
2466    Symbol::expand (shlibsuffix);
2467  }
2468
2469  {
2470    Symbol* macro = Symbol::find ("library_install_command");
2471    if (macro != 0)
2472      {
2473        symlinkcmd = macro->build_macro_value ();
2474        Symbol::expand (symlinkcmd);
2475      }
2476  }
2477
2478  if (current_use.get_strategy ("InstallArea"))
2479    {
2480      const CmtInstallAreaMgr& ia_mgr = CmtInstallAreaMgr::instance ();
2481     
2482      cmt_string s1 = ia_mgr.get_installarea ();
2483
2484      {
2485        Symbol* symbol = Symbol::find ("CMTINSTALLAREA");
2486        if (symbol != 0)
2487          {
2488            s1 = symbol->build_macro_value ();
2489            Symbol::expand (s1);
2490          }
2491      }
2492
2493      cmtinstallarea = s1;
2494                 
2495      cmt_string s2;
2496
2497      {
2498        Symbol* macro = Symbol::find ("tag");
2499        if (macro != 0)
2500          {
2501            s2 = macro->build_macro_value ();
2502            Symbol::expand (s2);
2503          }
2504      }
2505
2506      tag = s2;
2507
2508      cmt_string s = s1;
2509      s += CmtSystem::file_separator ();
2510      s += s2;
2511      s += CmtSystem::file_separator ();
2512      s += "lib";
2513
2514      CmtSystem::mkdir (s);
2515    }
2516
2517  current_use.build_library_links (cmtinstallarea, tag, shlibsuffix, symlinkcmd);
2518
2519  for (i = 0; i < Uses.size (); i++)
2520    {
2521      Use* use = Uses[i];
2522
2523      if (use == 0) continue;
2524      if (use->discarded) continue;
2525      if (use->m_hidden) continue;
2526
2527      if (use->get_package_name () == "CMT") continue;
2528      if (use->get_package_name () == current_use.get_package_name ()) continue;
2529
2530      use->build_library_links (cmtinstallarea, tag, shlibsuffix, symlinkcmd);
2531    }
2532}
2533
2534//----------------------------------------------------------
2535void Cmt::do_build_make_setup (const ArgParser& ap)
2536{
2537  if (CmtLock::check () == CmtLock::locked_by_another_user)
2538    {
2539      CmtError::set (CmtError::conflicting_lock, "build_make_setup>");
2540      return;
2541    }
2542  set_standard_macros ();
2543  Generator::build_make_setup (Me.m_current_package);
2544}
2545
2546//----------------------------------------------------------
2547void Cmt::do_build_msdev (const ArgParser& ap)
2548{
2549  if (CmtLock::check () == CmtLock::locked_by_another_user)
2550    {
2551      CmtError::set (CmtError::conflicting_lock, "build_msdev>");
2552      return;
2553    }
2554
2555  if (true)
2556    {
2557      set_standard_macros ();
2558      if (ap.arguments.size () > 0) build_msdev_file (ap.arguments[0]);
2559      else build_msdev_file ("");
2560    }
2561}
2562
2563void Cmt::do_build_CMT_pacman (const ArgParser& ap)
2564{
2565  cmt_string pacman_file;
2566
2567  pacman_file = Me.m_cmt_root;
2568  pacman_file += CmtSystem::file_separator ();
2569  pacman_file += "mgr";
2570  pacman_file += CmtSystem::file_separator ();
2571  pacman_file += "CMT.pacman";
2572
2573  cmt_string pacman;
2574  cmt_string pattern = "<version>";
2575  cmt_string replacement = CMTVERSION;
2576
2577  pacman.read (pacman_file);
2578
2579  pacman.replace_all (pattern, replacement);
2580
2581  cout << pacman << endl;
2582}
2583
2584// Visual Studio.net Support                                                 
2585//----------------------------------------------------------                 
2586void Cmt::do_build_vsnet (const ArgParser& ap)     
2587{                                                                           
2588  if (CmtLock::check () == CmtLock::locked_by_another_user)                 
2589    {                                                                       
2590      CmtError::set (CmtError::conflicting_lock, "build_vsnet>");           
2591      return;                                                               
2592    }                                                                       
2593                                                                             
2594  if (true)                                                                 
2595    {                                                                       
2596      set_standard_macros ();                                               
2597      if (ap.arguments.size () > 0) build_vsnet_file (ap.arguments[0]);           
2598      else build_vsnet_file ("");                                           
2599    }                                                                       
2600}                                                                           
2601
2602//----------------------------------------------------------
2603void Cmt::do_build_os9_makefile (const ArgParser& ap)
2604{
2605  if (CmtLock::check () == CmtLock::locked_by_another_user)
2606    {
2607      CmtError::set (CmtError::conflicting_lock, "build_os9_makefile>");
2608      return;
2609    }
2610
2611  if (ap.arguments.size () > 0) 
2612    {
2613      set_standard_macros ();
2614      build_OS9_makefile (ap.arguments[0]);
2615    }
2616}
2617
2618//----------------------------------------------------------
2619void Cmt::do_build_prototype (const ArgParser& ap)
2620{
2621  if (CmtLock::check () == CmtLock::locked_by_another_user)
2622    {
2623      CmtError::set (CmtError::conflicting_lock, "build_prototype>");
2624      return;
2625    }
2626
2627  if (ap.arguments.size () > 0) 
2628    {
2629      set_standard_macros ();
2630      Generator::build_prototype (ap.arguments[0]);
2631    }
2632}
2633
2634//----------------------------------------------------------
2635void Cmt::do_build_readme (const ArgParser& ap)
2636{
2637  if (CmtLock::check () == CmtLock::locked_by_another_user)
2638    {
2639      CmtError::set (CmtError::conflicting_lock, "build_readme>");
2640      return;
2641    }
2642
2643  set_standard_macros ();
2644  Generator::build_readme (ap.arguments);
2645}
2646
2647//----------------------------------------------------------
2648void Cmt::do_build_tag_makefile (const ArgParser& ap)
2649{
2650  if (CmtLock::check () == CmtLock::locked_by_another_user)
2651    {
2652      CmtError::set (CmtError::conflicting_lock, "build_tag_makefile>");
2653      return;
2654    }
2655
2656  print_macros (Make);
2657}
2658
2659//----------------------------------------------------------
2660void Cmt::do_build_temporary_name (const ArgParser& ap)
2661{
2662  cmt_string name = CmtSystem::get_temporary_name ();
2663  cout << name << endl;
2664}
2665
2666//----------------------------------------------------------
2667void Cmt::do_build_triggers (const ArgParser& ap)
2668{
2669  if (CmtLock::check () == CmtLock::locked_by_another_user)
2670    {
2671      CmtError::set (CmtError::conflicting_lock, "build_tag_makefile>");
2672      return;
2673    }
2674
2675  if (ap.arguments.size () > 0) 
2676    {
2677      set_standard_macros ();
2678      TriggerGenerator::run (ap.arguments[0]);
2679    }
2680}
2681
2682//----------------------------------------------------------
2683void Cmt::do_build_windefs (const ArgParser& ap)
2684{
2685  if (CmtLock::check () == CmtLock::locked_by_another_user)
2686    {
2687      CmtError::set (CmtError::conflicting_lock, "build_windefs>");
2688      return;
2689    }
2690
2691  if (ap.arguments.size () > 0) 
2692    {
2693      set_standard_macros ();
2694      //      Generator::build_windefs (ap.arguments[0]);
2695      Generator::build_windefs (ap.arguments);
2696    }
2697}
2698
2699//----------------------------------------------------------
2700void Cmt::do_check_configuration (const ArgParser& ap)
2701{
2702  cmt_string env;
2703  Tag* tag;
2704
2705  set_standard_macros ();
2706
2707  static CmtSystem::cmt_string_vector tags;
2708
2709  CmtSystem::split (Me.m_extra_tags, " \t,", tags);
2710
2711  for (int i = 0; i < tags.size (); i++)
2712    {
2713      const cmt_string& t = tags[i];
2714
2715      tag = Tag::find (t);
2716      if (tag == 0) continue;
2717
2718      if (!Tag::check_tag_used (tag) && !Symbol::check_tag_used (tag)) 
2719        {
2720          CmtMessage::warning ("The tag " + t + " is not used in any tag expression. Please check spelling");
2721          //      cerr << "#CMT> The tag " << t << " is not used in any tag expression. Please check spelling" << endl;
2722        }
2723    }
2724
2725  Symbol::check_all_paths ();
2726
2727  env = CmtSystem::getenv ("CMTSITE");
2728  if (env == "") 
2729    {
2730      return;
2731    }
2732
2733  tag = Tag::find (env);
2734  if (tag == 0) 
2735    {
2736      return;
2737    }
2738
2739  if (!Tag::check_tag_used (tag) && !Symbol::check_tag_used (tag) && Cmt::get_warnings ()) 
2740    {
2741      CmtMessage::warning ("The CMTSITE value " + env + " is not used in any tag expression. Please check spelling");
2742      //      cerr << "#CMT> The CMTSITE value " << env << " is not used in any tag expression. Please check spelling" << endl;
2743    }
2744
2745}
2746
2747//----------------------------------------------------------
2748void Cmt::do_check_files (const ArgParser& ap)
2749{
2750  if (ap.arguments.size () >= 2) 
2751    {
2752      cmt_string first_file = ap.arguments[0];
2753      cmt_string second_file = ap.arguments[1];
2754         
2755      if (first_file == "") return;
2756      if (second_file == "") return;
2757         
2758      CmtSystem::compare_and_update_files (first_file, second_file);
2759    }
2760}
2761
2762//----------------------------------------------------------
2763void Cmt::do_check_version (const ArgParser& ap)
2764{
2765  if (ap.arguments.size () > 0)
2766    {
2767      cmt_string name = ap.arguments[0];
2768         
2769      if (name == "") return;
2770      int v = 0;
2771      int r = 0;
2772      int p = 0;
2773         
2774      bool ok = CmtSystem::is_version_directory (name, v, r, p);
2775         
2776      if (ok)
2777        {
2778          cout << "# " << name << " is version " << v << " release " << r << " patch " << p << endl;
2779        }
2780      else
2781        {
2782          cout << "# " << name << " is not a version tag" << endl;
2783        }
2784    }
2785}
2786
2787//----------------------------------------------------------
2788void Cmt::do_checkout (const ArgParser& ap)
2789{
2790  Vcs::checkout (ap.arguments);
2791  //  Cvs::checkout (ap.arguments);
2792}
2793
2794//----------------------------------------------------------
2795void Cmt::do_cleanup (const ArgParser& ap)
2796{
2797  print_clean (ap.mode);
2798}
2799
2800//----------------------------------------------------------
2801void Cmt::do_config (const ArgParser& ap)
2802{
2803  if (CmtLock::check () == CmtLock::locked_by_another_user)
2804    {
2805      CmtError::set (CmtError::conflicting_lock, "config>");
2806      return;
2807    }
2808
2809  //Use::UsePtrVector& Uses = Use::get_ordered_uses ();
2810
2811  Use& current_use = Use::current ();
2812
2813  if (Me.m_debug)
2814    {
2815      cout << "Cmt::do_config> " << endl;
2816      cout << "pwd " << CmtSystem::pwd () << endl;
2817      cout << "current_dir " << Me.m_current_dir << endl;
2818      cout << "default_path " << Me.m_default_path << endl;
2819      cout << "cmt config " <<
2820        Me.m_current_package << " " <<
2821        Me.m_current_version << " " <<
2822        Me.m_current_path << endl;
2823    }
2824
2825  if (Me.m_current_package == "CMT") return;
2826  if (Me.m_current_package == "methods") return;
2827
2828  cmt_string branch;
2829
2830  CmtSystem::basename (Me.m_current_dir, branch);
2831
2832  if ((branch != "mgr") && (branch != "cmt"))
2833    {
2834
2835      //
2836      // Here we are in a standalone package (ie completely unstructured)
2837      //
2838
2839      if (CmtSystem::test_file ("requirements"))
2840        {
2841          if (CmtMessage::active (Error))
2842            {
2843          cout << "------------------------------------------" << endl;
2844          cout << "Configuring environment for standalone package." << endl;
2845          cout << "CMT version " << Me.m_cmt_version << "." << endl;
2846          cout << "System is " << Me.m_cmt_config << endl;
2847          cout << "------------------------------------------" << endl;
2848            }
2849
2850          install_setup_scripts ();
2851          install_cleanup_scripts ();
2852          //install_test_setup_scripts ();
2853          //install_test_cleanup_scripts ();
2854
2855          Generator::build_default_makefile ();
2856        }
2857      else
2858        {
2859          cout << "==============================================" << endl;
2860          cout << "cmt config must be operated either upon "
2861            "an existing package" << endl;
2862          cout << " (ie. when a requirements file already exists)" << endl;
2863          cout << "   > cd ..." << endl;
2864          cout << "   > cmt config" << endl;
2865          cout << "or to create a new package" << endl;
2866          cout << "   > cmt config <package> <version> [<path>]" << endl;
2867          cout << "==============================================" << endl;
2868        }
2869
2870      return;
2871    }
2872
2873  //  configure_current_package ();
2874
2875  //  Generator::build_default_makefile ();
2876
2877  CmtSystem::cmt_string_vector makes;
2878  cmt_regexp expression ("[.]n?make(sav)?$");
2879
2880  CmtSystem::scan_dir (".", expression, makes);
2881 
2882  if (makes.size () > 0)
2883    {
2884      if (CmtMessage::active (Info))
2885        cout << "Removing all previous make fragments from " << branch << endl;
2886
2887      for (int i = 0; i < makes.size (); i++)
2888        {
2889          const cmt_string& s = makes[i];
2890          CmtSystem::cmt_string_vector words;
2891          CmtSystem::split (s, '.', words);
2892          if (words.size()>=2)
2893            if (CmtSystem::get_makefile_suffix () == "nmake" && words[1] == "nmake")
2894              CmtSystem::remove_file (s);
2895            else if (CmtSystem::get_makefile_suffix () == "make" && words[1] == "make")
2896              CmtSystem::remove_file (s);
2897        }
2898    }
2899 
2900
2901  CmtSystem::cd ("..");
2902
2903  CmtSystem::scan_dir (Me.m_cmt_config, expression, makes); 
2904   
2905  if (makes.size () > 0) 
2906    {
2907      if (CmtMessage::active (Info))
2908        {
2909          cout << "Removing all previous make fragments from "
2910               << Me.m_cmt_config << endl; 
2911        }
2912
2913      for (int i = 0; i < makes.size (); i++) 
2914        { 
2915          const cmt_string& s = makes[i];
2916          CmtSystem::cmt_string_vector words;
2917          CmtSystem::split (s, '.', words);
2918          if (words.size()>=2)
2919            if (CmtSystem::get_makefile_suffix () == "nmake" && words[1] == "nmake")
2920              CmtSystem::remove_file (s);
2921            else if (CmtSystem::get_makefile_suffix () == "make" && words[1] == "make")
2922              CmtSystem::remove_file (s);
2923        }   
2924    } 
2925
2926  CmtSystem::cd (branch); 
2927
2928  Use& use = Use::current ();
2929
2930  use.set (Me.m_current_package,
2931           Me.m_current_version,
2932           Me.m_current_path,
2933           "",
2934           "");
2935
2936  use.change_path (Me.m_current_path);
2937  use.style     = Me.m_current_style;
2938  use.structuring_style = Me.m_current_structuring_style;
2939
2940  Me.m_quiet = true;
2941
2942  if (!reach_current_package ())
2943    {
2944      CmtMessage::error ("Cannot read the requirements file");
2945      //      cout << "Cannot read the requirements file" << endl;
2946      return;
2947    }
2948
2949  install_setup_scripts ();
2950  install_cleanup_scripts ();
2951  install_native_version_file ();
2952
2953  Generator::build_default_makefile ();
2954
2955  CmtSystem::cd ("..");
2956
2957  Branch::BranchVector& branches = Branch::branches ();
2958
2959  int i;
2960
2961  for (i = 0; i < branches.size (); i++)
2962    {
2963      const Branch& branch = branches[i];
2964      const cmt_string& branch_name = branch.name ();
2965
2966      if (!CmtSystem::test_directory (branch_name))
2967        {
2968          if (!CmtSystem::mkdir (branch_name))
2969            {
2970              CmtMessage::error ("Cannot create the " + branch_name + " branch");
2971              //              cout << "Cannot create the " << branch_name <<" branch" << endl;
2972            }
2973          else
2974            {
2975              if (CmtMessage::active (Info))
2976                cout << "Installing the " << branch_name << " directory" << endl;
2977            }
2978        }
2979      else
2980        {
2981          if (CmtMessage::active (Info))
2982            cout << branch_name << " directory already installed" << endl;
2983        }
2984    }
2985}
2986
2987
2988//----------------------------------------------------------
2989void Cmt::do_relocate (const ArgParser& ap)
2990{
2991  if (CmtLock::check () == CmtLock::locked_by_another_user)
2992    {
2993      CmtError::set (CmtError::conflicting_lock, "relocate>");
2994      return;
2995    }
2996
2997  Use& current_use = Use::current ();
2998
2999  if (Me.m_debug)
3000    {
3001      cout << "Cmt::do_relocate> " << endl;
3002      cout << "pwd "          << CmtSystem::pwd () << endl;
3003      cout << "current_dir "  << Me.m_current_dir << endl;
3004      cout << "default_path " << Me.m_default_path << endl;
3005      cout << "cmt config "   <<
3006        Me.m_current_package << " " <<
3007        Me.m_current_version << " " <<
3008        Me.m_current_path << endl;
3009    }
3010
3011  if (Me.m_current_package == "CMT")     return;
3012  if (Me.m_current_package == "methods") return;
3013
3014  cmt_string branch;
3015
3016  CmtSystem::basename (Me.m_current_dir, branch);
3017
3018  if ((branch != "mgr") && (branch != "cmt"))
3019    {
3020
3021      //
3022      // Here we are in a standalone package (ie completely unstructured)
3023      //
3024
3025      if (CmtSystem::test_file ("requirements"))
3026        {
3027          if (CmtMessage::active (Error))
3028            {
3029          cout << "------------------------------------------" << endl;
3030          cout << "Configuring environment for standalone package." << endl;
3031          cout << "CMT version " << Me.m_cmt_version << "." << endl;
3032          cout << "System is "   << Me.m_cmt_config << endl;
3033          cout << "------------------------------------------" << endl;
3034            }
3035          install_setup_scripts ();
3036          install_cleanup_scripts ();
3037          //install_test_setup_scripts ();
3038          //install_test_cleanup_scripts ();
3039
3040          Generator::build_default_makefile ();
3041        }
3042      else
3043        {
3044          cout << "==============================================" << endl;
3045          cout << "cmt relocate must be operated either upon "
3046            "an existing package" << endl;
3047          cout << " (ie. when a requirements file already exists)" << endl;
3048          cout << "   > cd ..." << endl;
3049          cout << "   > cmt relocate" << endl;
3050          cout << "or to create a new package" << endl;
3051          cout << "   > cmt relocate <package> <version> [<path>]" << endl;
3052          cout << "==============================================" << endl;
3053        }
3054
3055      return;
3056    }
3057
3058  //  configure_current_package ();
3059
3060  //  Generator::build_default_makefile ();
3061 
3062  CmtSystem::cd ("..");   
3063  CmtSystem::cd (branch); 
3064
3065  Use& use = Use::current ();
3066
3067  use.set (Me.m_current_package,
3068           Me.m_current_version,
3069           Me.m_current_path,
3070           "",
3071           "");
3072
3073  use.change_path (Me.m_current_path);
3074  use.style     = Me.m_current_style;
3075  use.structuring_style = Me.m_current_structuring_style;
3076
3077  Me.m_quiet = true;
3078   
3079  if (!reach_current_package ())
3080    {
3081      CmtMessage::error ("Cannot read the requirements file");
3082      //      cout << "Cannot read the requirements file" << endl;
3083      return;
3084    }
3085
3086  install_setup_scripts ();
3087  install_cleanup_scripts ();
3088
3089  Generator::build_default_makefile ();
3090
3091  CmtSystem::cd ("..");
3092
3093  Branch::BranchVector& branches = Branch::branches ();
3094
3095  int i;
3096
3097  for (i = 0; i < branches.size (); i++)
3098    {
3099      const Branch& branch = branches[i];
3100      const cmt_string& branch_name = branch.name ();
3101
3102      if (!CmtSystem::test_directory (branch_name))
3103        {
3104          if (!CmtSystem::mkdir (branch_name))
3105            {
3106              CmtMessage::error ("Cannot create the " + branch_name + " branch");
3107              //              cout << "Cannot create the " << branch_name <<" branch" << endl;
3108            }
3109          else
3110            {
3111              if (CmtMessage::active (Info))
3112                cout << "Installing the " << branch_name << " directory" << endl;
3113            }
3114        }
3115      else
3116        {
3117          if (CmtMessage::active (Info))
3118            cout << branch_name << " directory already installed" << endl;
3119        }
3120    }
3121}
3122
3123//----------------------------------------------------------
3124void Cmt::do_create (const ArgParser& ap)
3125{
3126  if (ap.arguments.size () < 2) return;
3127
3128  const cmt_string& package = ap.arguments[0];
3129  const cmt_string& version = ap.arguments[1];
3130  cmt_string offset;
3131  if (ap.arguments.size () >= 3) offset = ap.arguments[2];
3132
3133  if (Me.m_debug)
3134    {
3135      cout << "do_create>Me.m_current_package=" << Me.m_current_package << endl;
3136      cout << "do_create>package=" << package << endl;
3137    }
3138
3139  //if (Me.m_current_package == "CMT") return;
3140  //if (Me.m_current_package == "methods") return;
3141
3142  cmt_string the_path;
3143
3144  the_path = CmtSystem::pwd ();
3145
3146  if (offset != "")
3147    {
3148      if (!CmtSystem::absolute_path (offset))
3149        {
3150          // offset is really a relative offset
3151          the_path += CmtSystem::file_separator ();
3152          the_path += offset;
3153        }
3154      else // absolute path
3155        {
3156          the_path = offset;
3157        }
3158    }
3159
3160  CmtSystem::compress_path (the_path);
3161
3162  // Now 'the_path' contains the complete path where the package will be created
3163
3164  if (CmtMessage::active (Error))
3165    {
3166  cout << "------------------------------------------" << endl;
3167  cout << "Configuring environment for package " << package <<
3168    " version " << version << "." << endl;
3169  cout << "CMT version " << Me.m_cmt_version << "." << endl;
3170  cout << "Root set to " << the_path << "." << endl;
3171  cout << "System is " << Me.m_cmt_config << endl;
3172  cout << "------------------------------------------" << endl;
3173    }
3174
3175  if (!CmtSystem::test_directory (the_path))
3176    {
3177      if (!CmtSystem::mkdir (the_path))
3178        {
3179          CmtMessage::error ("Cannot create the path directory");
3180          //          cout << "Cannot create the path directory" << endl;
3181          return;
3182        }
3183      else
3184        {
3185          if (CmtMessage::active (Info))
3186            cout << "Installing the path directory" << endl;
3187        }
3188    }
3189
3190  CmtSystem::cd (the_path);
3191
3192  if (!CmtSystem::test_directory (package))
3193    {
3194      if (!CmtSystem::mkdir (package))
3195        {
3196          CmtMessage::error ("Cannot create the package directory");
3197          //          cout << "Cannot create the package directory" << endl;
3198          return;
3199        }
3200      else
3201        {
3202          if (CmtMessage::active (Info))
3203            cout << "Installing the package directory" << endl;
3204        }
3205    }
3206  else
3207    {
3208      if (CmtMessage::active (Info))
3209        cout << "Package directory already installed" << endl;
3210    }
3211
3212  CmtSystem::cd (package);
3213
3214  Use& current_use = Use::current ();
3215
3216  bool with_version_dir = current_use.get_strategy ("VersionDirectory");
3217
3218  CmtStructuringStyle style = get_current_structuring_style ();
3219
3220  if (style != default_structuring_style)
3221    {
3222      if (with_version_dir)
3223        {
3224          if (style == without_version_directory)
3225            {
3226              with_version_dir = false;
3227            }
3228        }
3229      else
3230        {
3231          if (style == with_version_directory)
3232            {
3233              with_version_dir = true;
3234            }
3235        }
3236    }
3237
3238  if (with_version_dir)
3239    {
3240      if (!CmtSystem::test_directory (version))
3241        {
3242          if (!CmtSystem::mkdir (version))
3243            {
3244              CmtMessage::error ("Cannot create the version directory");
3245              //              cout << "Cannot create the version directory" << endl;
3246              return;
3247            }
3248          else
3249            {
3250              if (CmtMessage::active (Info))
3251                cout << "Installing the version directory" << endl;
3252            }
3253        }
3254      else
3255        {
3256          if (CmtMessage::active (Info))
3257            cout << "Version directory already installed" << endl;
3258        }
3259
3260      CmtSystem::cd (version);
3261    }
3262  else
3263    {
3264      if (CmtMessage::active (Info))
3265        cout << "Version directory will not be created due to structuring style" << endl;
3266    }
3267
3268  if (!CmtSystem::test_directory ("cmt"))
3269    {
3270      if (!CmtSystem::test_directory ("mgr"))
3271        {
3272          if (!CmtSystem::mkdir ("cmt"))
3273            {
3274              CmtMessage::error ("Cannot create the cmt directory");
3275//              cout << "Cannot create the cmt directory" << endl;
3276              return;
3277            }
3278          else
3279            {
3280              Me.m_current_style = cmt_style;
3281              if (with_version_dir)
3282                {
3283                  Me.m_current_structuring_style = with_version_directory;
3284                }
3285              else
3286                {
3287                  Me.m_current_structuring_style = without_version_directory;
3288                  //                  Me.m_current_style = no_version_style;
3289                }
3290
3291              if (CmtMessage::active (Info))
3292                cout << "Installing the cmt directory" << endl;
3293            }
3294        }
3295      else
3296        {
3297          Me.m_current_style = mgr_style;
3298          if (with_version_dir)
3299            {
3300              Me.m_current_structuring_style = with_version_directory;
3301              //              Me.m_current_style = mgr_style;
3302            }
3303          else
3304            {
3305              Me.m_current_structuring_style = without_version_directory;
3306              //              Me.m_current_style = no_version_style;
3307            }
3308         
3309          if (CmtMessage::active (Info))
3310            cout << "Mgr directory already installed" << endl;
3311        }
3312    }
3313  else
3314    {
3315      Me.m_current_style = cmt_style;
3316      if (with_version_dir)
3317        {
3318          Me.m_current_structuring_style = with_version_directory;
3319          //          Me.m_current_style = cmt_style;
3320        }
3321      else
3322        {
3323          Me.m_current_structuring_style = without_version_directory;
3324          //          Me.m_current_style = no_version_style;
3325        }
3326
3327      if (CmtMessage::active (Info))
3328        cout << "Cmt directory already installed" << endl;
3329    }
3330
3331  if (!CmtSystem::test_directory ("src"))
3332    {
3333      if (!CmtSystem::mkdir ("src"))
3334        {
3335          CmtMessage::error ("Cannot create the src directory");
3336          //          cout << "Cannot create the src directory" << endl;
3337          return;
3338        }
3339      else
3340        {
3341          if (CmtMessage::active (Info))
3342            cout << "Installing the src directory" << endl;
3343        }
3344    }
3345  else
3346    {
3347      if (CmtMessage::active (Info))
3348        cout << "src directory already installed" << endl;
3349    }
3350
3351  switch (Me.m_current_style)
3352    {
3353    case cmt_style:
3354      //    case no_version_style:
3355      CmtSystem::cd ("cmt");
3356      break;
3357    case mgr_style:
3358      CmtSystem::cd ("mgr");
3359      break;
3360    }
3361
3362  Generator::build_default_makefile ();
3363
3364  if (!CmtSystem::test_file ("requirements"))
3365    {
3366      // create an empty requirement file.
3367      ofstream f ("requirements");
3368      if (f)
3369        {
3370          f << "package " << package << endl;
3371          f << endl;
3372          f.close ();
3373        }
3374    }
3375
3376  if (!with_version_dir)
3377    {
3378      //      ofstream f ("version.cmt");
3379      ofstream f (Package::get_version_file_name ());
3380      if (f)
3381        {
3382          f << version << endl;
3383          f.close ();
3384        }
3385    }
3386
3387  Me.m_current_package = package;
3388  Me.m_current_version = version;
3389  Me.m_current_path    = the_path;
3390  Me.m_current_dir     = CmtSystem::pwd ();
3391
3392  do_config (ap);
3393}
3394
3395//----------------------------------------------------------
3396void Cmt::do_create_project (const ArgParser& ap)
3397{
3398  int argc = ap.arguments.size ();
3399  int arg = 0;
3400
3401  if (argc < 1) return;
3402
3403  cmt_string project;
3404  cmt_string release;
3405  cmt_string path;
3406
3407  project = ap.arguments[arg]; argc--; arg++;
3408
3409  if (argc > 0)
3410    {
3411      release = ap.arguments[arg];
3412
3413      if (release[0] != '-')
3414        {
3415          argc--; 
3416          arg++;
3417          if (argc > 0)
3418            {
3419              path = ap.arguments[arg];
3420              if (path[0] != '-')
3421                {
3422                  argc--; arg++;
3423                }
3424              else
3425                {
3426                  path = "";
3427                }
3428            }
3429        }
3430      else
3431        {
3432          release = "";
3433        }
3434    }
3435
3436  if (Project::create (project, release, path))
3437    {
3438      while (argc > 0)
3439        {
3440          const cmt_string& argument = ap.arguments[arg]; argc--; arg++;
3441         
3442          if (argument.substr (0, 5) == "-use=")
3443            {
3444              cmt_string use;
3445
3446              argument.substr (5, use);
3447
3448              CmtSystem::cmt_string_vector words;
3449              CmtSystem::split (use, ":", words);
3450
3451              ofstream f (Project::get_project_file_name (), ios_base::app);
3452              if (f)
3453                {
3454                  f << "use " << words[0] << " " << words[1] << " " << words[2] << endl;
3455                  f << endl;
3456                  f.close ();
3457                }
3458            }
3459        }
3460    }
3461}
3462
3463//----------------------------------------------------------
3464void Cmt::do_cvsbranches (const ArgParser& ap)
3465{
3466  Cvs::branches (ap.arguments[0]);
3467}
3468
3469//----------------------------------------------------------
3470void Cmt::do_cvssubpackages (const ArgParser& ap)
3471{
3472  Cvs::subpackages (ap.arguments[0]);
3473}
3474
3475//----------------------------------------------------------
3476void Cmt::do_cvssubprojects (const ArgParser& ap)
3477{
3478  Cvs::subprojects (ap.arguments[0]);
3479}
3480
3481//----------------------------------------------------------
3482void Cmt::do_cvstags (const ArgParser& ap)
3483{
3484  Cvs::tags (ap.arguments);
3485}
3486
3487//----------------------------------------------------------
3488void Cmt::do_do (const ArgParser& ap)
3489{
3490  if (ap.arguments.size () > 0) 
3491    {
3492      set_standard_macros ();
3493      Cmt::reset_all_sets_done ();
3494      Symbol::all_set ();
3495      Generator::build_default_makefile ();
3496
3497      Symbol* symbol = Symbol::find (ap.arguments[0]);
3498
3499      if (symbol == 0)
3500        {
3501          Me.m_action = action_show_action_names;
3502          /*
3503            if (!Me.m_quiet)
3504            {
3505            cerr << "Existing actions:" << endl;
3506            print_symbol_names (ap.mode);
3507            }
3508          */
3509          CmtError::set (CmtError::unknown_command, ap.arguments[0]);
3510          return;
3511        }
3512
3513      /*
3514        We convert extra arguments into the standard macro cmt_args
3515      */
3516
3517      cmt_string args;
3518
3519      for (int i = 1; i < ap.arguments.size (); i++)
3520        {
3521          cmt_string s = ap.arguments[i];
3522          if (i > 1) args += " ";
3523          args += s;
3524        }
3525
3526      cmt_string r = "macro cmt_args \"";
3527      r += args;
3528      r += "\"";
3529
3530      Use* current_use = &(Use::current ());
3531     
3532      SyntaxParser::parse_requirements_line (r, current_use);
3533
3534      cmt_string cmd = symbol->build_macro_value ();
3535      Symbol::expand (cmd);
3536
3537      CmtMessage::info ("Execute action " + ap.arguments[0] + " => " + cmd);
3538
3539      int status = CmtSystem::execute (cmd);
3540      if (status != 0)
3541        {
3542          CmtError::set (CmtError::execution_failed, ap.arguments[0], status);
3543        }
3544    }
3545}
3546
3547//----------------------------------------------------------
3548void Cmt::do_expand_model (const ArgParser& ap)
3549{
3550  set_standard_macros ();
3551
3552  if ((ap.arguments[0] == "-strict") && (ap.arguments.size () > 1))
3553    {
3554      CmtModel::strict_expand (ap.arguments[1]);
3555    }
3556  else if ((ap.arguments[0] == "-test") && (ap.arguments.size () > 2))
3557    {
3558      CmtModel::test_regexp (ap.arguments[1], ap.arguments[2]);
3559    }
3560  else if (ap.arguments.size () > 0)
3561    {
3562      CmtModel::expand (ap.arguments[0]);
3563    }
3564}
3565
3566/**
3567 *  Handle free filtering of text files containing $(xxx) or ${xxx} patterns
3568 *
3569 *  Substitution is performed against CMT macros and environment variables.
3570 *
3571 *  Arguments:
3572 *
3573 *    cmt filter input-file-name output-file-name
3574 *
3575 */
3576void Cmt::do_filter (const ArgParser& ap)
3577{
3578  if (ap.arguments.size () < 2) return;
3579
3580  cmt_string& input = ap.arguments[0];
3581  cmt_string& output = ap.arguments[1];
3582
3583  if (!CmtSystem::test_file (input))
3584    {
3585      CmtError::set (CmtError::path_not_found, input);
3586      //      CmtMessage::error ("File " + input + " not found");
3587      return;
3588    }
3589
3590  cmt_string text;
3591
3592  text.read (input);
3593
3594  set_standard_macros ();
3595
3596  Symbol::expand (text);
3597
3598  FILE* file = fopen (output, "wb");
3599  if (file != NULL)
3600    {
3601      text.write (file);
3602      CmtSystem::close_ostream (file, output);
3603      //      fclose (file);
3604    }
3605  else
3606    {
3607      CmtError::set (CmtError::file_access_error, output);
3608      //      CmtMessage::error ("Cannot write filtered file " + output);
3609    }
3610}
3611
3612//----------------------------------------------------------
3613void Cmt::do_help (const ArgParser& ap)
3614{
3615  //cerr << "ap.help_action=" << ap.help_action << " Me.m_action=" << Me.m_action << endl;
3616  if (Me.m_action == action_none)
3617    {
3618      CommandHelp::show_all ();
3619    }
3620  else
3621    {
3622      CommandHelp::show (Me.m_action);
3623    }
3624}
3625
3626//----------------------------------------------------------
3627void Cmt::do_lock (const ArgParser& ap)
3628{
3629  const cmt_string& package = Me.m_current_package;
3630  const cmt_string& version = Me.m_current_version;
3631  const cmt_string& path    = Me.m_current_path;
3632
3633  //(unsused) Use& use = Use::current();
3634
3635  CmtMessage::info ("try to lock package " + package + " in " + CmtSystem::pwd ());
3636  //  cerr << "try to lock package " << package << " in " << CmtSystem::pwd () << endl;
3637
3638  set_standard_macros ();
3639
3640  CmtLock::status status = CmtLock::lock ();
3641}
3642
3643//----------------------------------------------------------
3644void Cmt::do_remove (const ArgParser& ap)
3645{
3646  const cmt_string& package = Me.m_current_package;
3647  const cmt_string& version = Me.m_current_version;
3648  const cmt_string& path    = Me.m_current_path;
3649
3650  //Use::UsePtrVector& Uses = Use::get_ordered_uses ();
3651
3652  if (Me.m_current_package == "CMT") return;
3653  if (Me.m_current_package == "methods") return;
3654
3655  cmt_string the_path;
3656
3657  //the_path = Me.m_default_path;
3658  the_path = CmtSystem::pwd ();
3659
3660  if (path != "")
3661    {
3662      if (!CmtSystem::absolute_path (path))
3663        {
3664          // path is just a suffix
3665          the_path += CmtSystem::file_separator ();
3666          the_path += path;
3667        }
3668      else // absolute path
3669        {
3670          the_path = path;
3671        }
3672    }
3673
3674  CmtSystem::compress_path (the_path);
3675
3676  if (CmtMessage::active (Info))
3677    {
3678  cout << "------------------------------------------" << endl;
3679  cout << "Removing package " << package <<
3680    " version " << version << "." << endl;
3681  cout << "CMT version " << Me.m_cmt_version << "." << endl;
3682  cout << "Root set to " << the_path << "." << endl;
3683  cout << "System is " << Me.m_cmt_config << endl;
3684  cout << "------------------------------------------" << endl;
3685    }
3686
3687  the_path += CmtSystem::file_separator ();
3688  the_path += package;
3689
3690  if (!CmtSystem::cd (the_path))
3691    {
3692      CmtMessage::error ("Path " + the_path + " not reachable");
3693      //      cerr << "Path " << the_path << " not reachable" << endl;
3694      return;
3695    }
3696
3697  if (CmtSystem::test_directory (version))
3698    {
3699      if (CmtSystem::remove_directory (version))
3700        {
3701          if (CmtMessage::active (Info))
3702            cerr << "Version " << version << " has been removed from " << the_path << endl;
3703          CmtSystem::cmt_string_vector contents;
3704          CmtSystem::scan_dir (".", contents);
3705          if (contents.size () == 0)
3706            {
3707              CmtSystem::cd ("..");
3708              if (CmtSystem::remove_directory (package))
3709                {
3710                  if (CmtMessage::active (Info))
3711                    cerr << "Package " << package << " has no more versions. Thus it has been removed."<< endl;
3712                }
3713            }
3714        }
3715      else
3716        {
3717          CmtMessage::warning ("Impossible to remove version " + version + " from " + the_path);
3718          //          cerr << "Impossible to remove version " << version << " from " << the_path << endl;
3719        }
3720    }
3721  else if (CmtSystem::test_directory ("cmt"))
3722    {
3723      CmtSystem::cd ("cmt");
3724
3725      cmt_string v;
3726
3727      //      v.read ("version.cmt");
3728      v.read (Package::get_version_file_name ());
3729      if (v == version)
3730        {
3731          CmtSystem::cd ("..");
3732          if (!CmtSystem::remove_directory ("cmt"))
3733            {
3734              if (CmtMessage::active (Info))
3735                {
3736              cerr << "Unstructured version " << version
3737                   << " has been removed from " << the_path << endl;
3738                }
3739            }
3740          else
3741            {
3742              CmtMessage::warning ("Impossible to remove unstructured version " + version + " from " + the_path);
3743              //              cerr << "Impossible to remove unstructured version " << version
3744              //                   << " from " << the_path << endl;
3745            }
3746        }
3747      else
3748        {
3749          CmtMessage::warning ("Version " + version + " not found");
3750          //          cerr << "Version " << version << " not found" << endl;
3751        }
3752    }
3753  else
3754    {
3755      CmtMessage::warning ("Version " + version + " not found");
3756      //      cerr << "Version " << version << " not found" << endl;
3757    }
3758}
3759
3760//----------------------------------------------------------
3761void Cmt::do_remove_library_links (const ArgParser& ap)
3762{
3763  if (CmtLock::check () == CmtLock::locked_by_another_user)
3764    {
3765      CmtError::set (CmtError::conflicting_lock, "remove_library_links>");
3766      return;
3767    }
3768
3769  set_standard_macros ();
3770
3771  Use::UsePtrVector& Uses = Use::get_ordered_uses ();
3772  Use& current_use = Use::current ();
3773  Use::UsePtrVector uses (Uses);
3774  uses.push_back (&Use::current ());
3775  int i;
3776  cmt_string shlibsuffix;
3777  cmt_string symunlink;
3778
3779  {
3780    Symbol* macro = Symbol::find ("shlibsuffix");
3781    if (macro == 0) return;
3782    shlibsuffix = macro->build_macro_value ();
3783    Symbol::expand (shlibsuffix);
3784  }
3785
3786  {
3787    Symbol* macro = Symbol::find ("symunlink");
3788    if (macro == 0) return;
3789    symunlink = macro->build_macro_value ();
3790    Symbol::expand (symunlink);
3791  }
3792
3793//   for (i = 0; i < Uses.size (); i++)
3794//     {
3795//       Use* use = Uses[i];
3796  for (i = 0; i < uses.size (); i++)
3797    {
3798      Use* use = uses[i];
3799
3800      if (use->discarded) continue;
3801      if (use->m_hidden) continue;
3802
3803      if (!use->located ())
3804        {
3805          CmtMessage::warning ("package " + use->get_package_name ()
3806                               + " " + use->version + " " + use->path
3807                               + " not found");
3808              /*
3809          if (!Me.m_quiet)
3810            {
3811              cerr << "#CMT> package " << use->get_package_name () <<
3812                " " << use->version << " " << use->path <<
3813                " not found" <<
3814                endl;
3815            }
3816              */
3817        }
3818      else
3819        {
3820          if (use->get_package_name () == "CMT") continue;
3821//           if (use->get_package_name () == current_use.get_package_name ()) continue;
3822
3823          cmt_string s;
3824
3825          s = use->get_package_name ();
3826          s += "_libraries";
3827
3828          Symbol* libraries_macro = Symbol::find (s);
3829
3830          if (libraries_macro == 0) continue;
3831
3832          cmt_string libraries = libraries_macro->build_macro_value ();
3833          Symbol::expand (libraries);
3834
3835          static CmtSystem::cmt_string_vector values;
3836
3837          CmtSystem::split (libraries, " \t", values);
3838
3839          for (int j = 0; j < values.size (); j++)
3840            {
3841              const cmt_string& library = values[j];
3842
3843              static cmt_string libname;
3844              static cmt_string name;
3845
3846              // Is it a simple name or a complete path?
3847
3848              libname = library;
3849              Symbol::expand (libname);
3850
3851              if (CmtSystem::absolute_path (libname))
3852                {
3853                  /**
3854                   *   We assume here that "library" contains a complete path.
3855                   *   (including the complete syntax libxxx.so)
3856                   */
3857
3858                  cmt_string suffix;
3859                  CmtSystem::basename (library, name);
3860                }
3861              else
3862                {
3863                  /**
3864                   *   Here we expect that only the base name of the library
3865                   *   is given : ie it should not contain the "lib" prefix,
3866                   *   nor the suffix .so, nor any path prefix.
3867                   *    This of course should generally correspond to a constituent name.
3868                   */
3869
3870                  name = "lib";
3871                  name += libname;
3872                  name += ".";
3873                  name += shlibsuffix;
3874                }
3875
3876              Symbol::expand (libname);
3877
3878              if (current_use.get_strategy ("InstallArea"))
3879                {
3880                  const CmtInstallAreaMgr& ia_mgr = CmtInstallAreaMgr::instance ();
3881
3882                  cmt_string s1 = ia_mgr.get_installarea ();
3883
3884                  {
3885                    Symbol* symbol = Symbol::find ("CMTINSTALLAREA");
3886                    if (symbol != 0)
3887                      {
3888                        s1 = symbol->build_macro_value ();
3889                        Symbol::expand (s1);
3890                      }
3891                  }
3892                 
3893                  cmt_string s2;
3894
3895                  {
3896                    Symbol* macro = Symbol::find ("tag");
3897                    if (macro != 0)
3898                      {
3899                        s2 = macro->build_macro_value ();
3900                        Symbol::expand (s2);
3901                      }
3902                  }
3903
3904                  // Now deleting the reference file
3905
3906                  s = symunlink;
3907                  s += " ";
3908                  s += s1;
3909                  s += CmtSystem::file_separator ();
3910                  s += s2;
3911                  s += CmtSystem::file_separator ();
3912                  s += "lib";
3913                  s += CmtSystem::file_separator ();
3914                  s += name;
3915                  s += ".cmtref";
3916                  s += " ";
3917                  s += s1;
3918                  s += CmtSystem::file_separator ();
3919                  s += s2;
3920                  s += CmtSystem::file_separator ();
3921                  s += "lib";
3922                  s += CmtSystem::file_separator ();
3923                  s += name;
3924                }
3925              else
3926                {
3927                  s = symunlink;
3928                  s += " ../$(";
3929                  s += current_use.get_package_name ();
3930                  s += "_tag)/";
3931                  s += name;
3932                }
3933
3934              Symbol::expand (s);
3935
3936              if (!Me.m_quiet) cerr << s << endl;
3937
3938              int status = CmtSystem::execute (s);
3939
3940              if (status != 0)
3941                {
3942                  //                  if (status != 2) CmtError::set (CmtError::execution_error, s);
3943                  CmtError::set (CmtError::execution_error, s);
3944
3945                  CmtMessage::warning ("Cannot remove the symbolic link " + s);
3946                  //                  cerr << "#CMT> Cannot remove the symbolic link " << s << endl;
3947
3948                  break;
3949                }
3950            }
3951        }
3952    }
3953}
3954
3955//----------------------------------------------------------
3956void Cmt::do_run (const ArgParser& ap)
3957{
3958  if (ap.arguments.size () > 0) 
3959    {
3960      set_standard_macros ();
3961      Cmt::reset_all_sets_done ();
3962      Symbol::all_set ();
3963
3964      cmt_string cmd;
3965
3966      for (int i = 0; i < ap.arguments.size (); i++)
3967        {
3968          cmd += ap.arguments[i];
3969          cmd += " ";
3970        }
3971
3972      int status = CmtSystem::execute (cmd);
3973      if (status != 0)
3974        {
3975          CmtError::set (CmtError::execution_failed, ap.arguments[0], status);
3976        }
3977      //      CmtSystem::execute (cmd);
3978    }
3979}
3980
3981//----------------------------------------------------------
3982void Cmt::do_run_sequence (const ArgParser& ap)
3983{
3984  if (ap.arguments.size () == 0)
3985    CmtMessage::warning ("run_sequence: no sequence specified");
3986  //cerr << "#CMT> run_sequence: no sequence specified" << endl;
3987
3988  SequenceRunner runner;
3989
3990  if (CmtMessage::active (Info))
3991    cerr << "# cmt run_sequence: sequence " << ap.arguments[0] << endl;
3992
3993  runner.run (ap.arguments[0]);
3994}
3995
3996//----------------------------------------------------------
3997void Cmt::do_set_version (const ArgParser& ap)
3998{
3999  if (ap.arguments.size () == 0)
4000    CmtMessage::warning ("set version: no version specified");
4001  //cerr << "#CMT> set version: no version specified" << endl;
4002
4003  const cmt_string& version = ap.arguments[0];
4004
4005  int v, r, p;
4006
4007  if (!CmtSystem::is_version_directory (version, v, r, p))
4008    {
4009      CmtMessage::error ("set version " + version + " is not a correct version syntax");
4010      //      cerr << "#CMT> set version " << version << " is not a correct version syntax" << endl;
4011      return;
4012    }
4013
4014  if ((v == -1) || (r == -1) || (p == -1))
4015    {
4016      CmtMessage::error ("set version " + version
4017                         + " You cannot use wild card to set a version");
4018      //      cerr << "#CMT> set version " << version
4019      //           << " You cannot use wild card to set a version" << endl;
4020      return;
4021    }
4022
4023  // We want to install the version.cmt file
4024
4025  // We first check we are in a cmt branch
4026  cmt_string h = CmtSystem::pwd ();
4027  cmt_string branch;
4028  CmtSystem::basename (h, branch);
4029
4030  if (branch != "cmt")
4031    {
4032      CmtMessage::error ("set version " + version
4033                         + " must be applied in a cmt directory");
4034      //      cerr << "#CMT> set version " << version << " must be applied in a cmt directory"
4035      //           << endl;
4036      return;
4037    }
4038
4039  CmtSystem::dirname (h, h);
4040  CmtSystem::basename (h, branch);
4041
4042  if (branch == version)
4043    {
4044      CmtMessage::warning ("set version " + version
4045                           + " is already available as a version directory");
4046      //      cerr << "#CMT> set version " << version << " is already available as a version directory"
4047      //           << endl;
4048      return;
4049    }
4050
4051  CmtMessage::info ("Writing version file : " + version);
4052  //  cerr << "Writing version file : " << version << endl;
4053
4054  //  version.write ("version.cmt");
4055  version.write (Package::get_version_file_name ());
4056}
4057
4058//----------------------------------------------------------
4059void Cmt::do_set_versions (const ArgParser& ap)
4060{
4061  CmtSystem::cmt_string_vector args;
4062
4063  args = ap.arguments;
4064  {
4065    cmt_string& s = args.add ();
4066    s = "cmt";
4067  }
4068  {
4069    cmt_string& s = args.add ();
4070    s = "set";
4071  }
4072  {
4073    cmt_string& s = args.add ();
4074    s = "version";
4075  }
4076  {
4077    cmt_string& s = args.add ();
4078    s = "<version>";
4079  }
4080
4081  Me.m_action = action_broadcast;
4082
4083  do_broadcast (ap);
4084}
4085
4086//----------------------------------------------------------
4087void Cmt::do_setup (const ArgParser& ap)
4088{
4089  Use& current_use = Use::current ();
4090
4091  do_check_configuration (ap);
4092
4093  switch (ap.mode)
4094    {
4095    case Xml :
4096      Cmt::print_xml_prolog ("setup");
4097      cout << "<setup>";
4098      break;
4099    }
4100  print (ap.mode);
4101  switch (ap.mode)
4102    {
4103    case Xml :
4104      cout << "</setup>" << endl;
4105      break;
4106    }
4107
4108  //
4109  //  Try a cleanup of the installation area
4110  //
4111  if (current_use.get_strategy ("InstallArea"))
4112    {
4113      CmtInstallAreaMgr& ia_mgr = CmtInstallAreaMgr::instance ();
4114
4115      ia_mgr.setup ();
4116    }
4117}
4118
4119//----------------------------------------------------------
4120void Cmt::do_show_action (const ArgParser& ap)
4121{
4122  cmt_string target;
4123
4124  if (ap.arguments.size () > 0) target = ap.arguments[0];
4125
4126  Symbol* symbol;
4127
4128  set_standard_macros ();
4129
4130  symbol = Symbol::find (target);
4131
4132  if (symbol == 0) 
4133    {
4134      cmt_string t (target);
4135      t += " is not defined";
4136
4137      CmtError::set (CmtError::symbol_not_found, t);
4138
4139      return;
4140    }
4141  else
4142    {
4143      cmt_string t = target;
4144      t += " is a ";
4145
4146      if ((Me.m_action == action_show_action) ||
4147          (Me.m_action == action_show_action_value))
4148        {
4149          if (symbol->type != Symbol::SymbolAction)
4150            {
4151              if (symbol->type == Symbol::SymbolMacro)
4152                {
4153                  t += "macro";
4154                }
4155              else if (symbol->type == Symbol::SymbolSet)
4156                {
4157                  t += "set";
4158                }
4159              else if (symbol->type == Symbol::SymbolPath)
4160                {
4161                  t += "path";
4162                }
4163              else if (symbol->type == Symbol::SymbolAlias)
4164                {
4165                  t += "alias";
4166                }
4167
4168              CmtError::set (CmtError::wrong_symbol_type, t);
4169            }
4170        }
4171    }
4172
4173  if (symbol->value_lists.size () < 1) return;
4174
4175  symbol->show_macro (ap.mode);
4176}
4177
4178//----------------------------------------------------------
4179void Cmt::do_show_action_names (const ArgParser& ap)
4180{
4181  if (ap.arguments.size () > 0)
4182    {
4183      const cmt_string& pattern = ap.arguments[0];
4184      print_symbol_names (ap.mode, pattern);
4185    }
4186  else
4187    {
4188      print_symbol_names (ap.mode);
4189    }
4190}
4191
4192//----------------------------------------------------------
4193void Cmt::do_show_action_value (const ArgParser& ap)
4194{
4195  do_show_macro (ap);
4196}
4197
4198//----------------------------------------------------------
4199void Cmt::do_show_actions (const ArgParser& ap)
4200{
4201  if (ap.arguments.size () > 0)
4202    {
4203      const cmt_string& pattern = ap.arguments[0];
4204      print_macros (ap.mode, pattern);
4205    }
4206  else
4207    {
4208      print_macros (ap.mode);
4209    }
4210}
4211
4212//----------------------------------------------------------
4213void Cmt::do_show_all_tags (const ArgParser& /*ap*/)
4214{
4215  Tag::TagPtrVector tags = Tag::tags ();
4216  int index;
4217
4218  set_standard_macros ();
4219
4220  for (index = 0; index < tags.size (); index++)
4221    {
4222      const Tag* tag = tags[index];
4223      if (tag != 0)
4224        {
4225          tag->show_definition (true);
4226        }
4227    }
4228}
4229
4230//----------------------------------------------------------
4231void Cmt::do_show_applied_patterns (const ArgParser& /*ap*/)
4232{
4233  Pattern::show_all_applied_patterns ();
4234}
4235
4236//----------------------------------------------------------
4237void Cmt::do_show_author (const ArgParser& /*ap*/)
4238{
4239  Use& use = Use::current();
4240
4241  cout << use.author << endl;
4242 
4243}
4244//----------------------------------------------------------
4245void Cmt::do_show_project_author (const ArgParser& /*ap*/)
4246{
4247  cmt_string cmtpath;
4248  cmt_string offset;
4249
4250  Use& use = Use::current();   
4251  use.get_cmtpath_and_offset (cmtpath, offset);
4252
4253  Project* p = Project::find_by_cmtpath (cmtpath);
4254
4255  cout << p->get_author () << endl;
4256}
4257
4258//----------------------------------------------------------
4259void Cmt::do_show_branches (const ArgParser& ap)
4260{
4261  Branch::print_all (ap.mode);
4262}
4263
4264//----------------------------------------------------------
4265void Cmt::do_show_clients (const ArgParser& ap)
4266{
4267  cmt_string package   = "";
4268  cmt_string version   = "";
4269  cmt_string path_name = "";
4270
4271  if (ap.arguments.size () >= 1) package  = ap.arguments[0];
4272  if (ap.arguments.size () >= 2) version  = ap.arguments[1];
4273  if (ap.arguments.size () >= 3) path_name = ap.arguments[2];
4274
4275  PathScanner scanner;
4276  ClientCollector collector (package, version);
4277
4278  clear     ();
4279  //  configure ();
4280  configure (ap);
4281
4282  cout << "# ----------- Clients of " << package
4283       << (version != "" ? " " + version : "")
4284       << (path_name != "" ? " (" + path_name + ")": "")
4285    //    " " << version <<
4286    //    " " << path_name <<
4287       << endl;
4288
4289  if (path_name == "")
4290    {
4291      Project::scan_paths (scanner, collector);
4292    }
4293  else
4294    {
4295      scanner.scan_path (path_name, collector);
4296    }
4297
4298  cout << "# ----------- " << collector.count () << " clients found." << endl;
4299}
4300
4301//----------------------------------------------------------
4302void Cmt::do_show_cmtpath_patterns (const ArgParser& /*ap*/)
4303{
4304  set_standard_macros ();
4305  CmtPathPattern::show_all ();
4306}
4307
4308//----------------------------------------------------------
4309void Cmt::do_show_constituent (const ArgParser& ap)
4310{
4311  if (ap.arguments.size () > 0) 
4312    {
4313      set_standard_macros ();
4314
4315      Constituent* c = Constituent::find (ap.arguments[0]);
4316      if (c != 0)
4317        {
4318          c->show ();
4319        }
4320      else
4321        {
4322          cmt_string t (ap.arguments[0]);
4323          CmtError::set (CmtError::constituent_not_found, t);
4324        }
4325    }
4326}
4327
4328//----------------------------------------------------------
4329void Cmt::do_show_constituent_names (const ArgParser& /*ap*/)
4330{
4331  set_standard_macros ();
4332  Constituent::show_names ();
4333}
4334
4335//----------------------------------------------------------
4336void Cmt::do_show_constituents (const ArgParser& /*ap*/)
4337{
4338  set_standard_macros ();
4339  Constituent::show_all ();
4340}
4341
4342//----------------------------------------------------------
4343void Cmt::do_show_container (const ArgParser& ap)
4344{
4345  if (ap.arguments.size () == 0)
4346    {
4347      Project::show_container ();
4348    }
4349  else
4350    {
4351      for (int i = 0; i < ap.arguments.size (); i++)
4352        {
4353          const cmt_string& path_name = ap.arguments[i];
4354          if (CmtSystem::test_directory (path_name))
4355            {
4356              Project::show_container (path_name);
4357            }
4358          else
4359            {
4360              CmtMessage::warning ("No such directory: " + path_name);
4361              CmtError::set (CmtError::path_not_found, path_name);
4362            }
4363        }
4364    }
4365}
4366
4367//----------------------------------------------------------
4368void Cmt::do_show_cycles (const ArgParser& /*ap*/)
4369{
4370  set_standard_macros ();
4371  Use& use = Use::current();
4372
4373  use.show_cycles ();
4374}
4375
4376//----------------------------------------------------------
4377void Cmt::do_show_fragment (const ArgParser& ap)
4378{
4379  if (ap.arguments.size () > 0) Fragment::show (ap.arguments[0]);
4380}
4381
4382//----------------------------------------------------------
4383void Cmt::do_show_fragments (const ArgParser& /*ap*/)
4384{
4385  Fragment::show_all ();
4386}
4387
4388//----------------------------------------------------------
4389void Cmt::do_show_groups (const ArgParser& /*ap*/)
4390{
4391  Constituent::parse_all ();
4392  //  set_standard_macros ();
4393  Group::show_all ();
4394}
4395
4396//----------------------------------------------------------
4397void Cmt::do_show_include_dirs (const ArgParser& /*ap*/)
4398{
4399  cmt_string temp;
4400
4401  Use& use = Use::current();
4402
4403  set_standard_macros ();
4404
4405  if (use.include_path == "")
4406    {
4407      temp += "$(src) ";
4408    }
4409  else if (use.include_path != "none")
4410    {
4411      temp += use.include_path;
4412      temp += " ";
4413    }
4414
4415  for (int include_number = 0;
4416       include_number < use.includes.size ();
4417       include_number++)
4418    {
4419      Include& incl = use.includes[include_number];
4420
4421      if (incl.name == "") continue;
4422
4423      temp += incl.name;
4424      temp += " ";
4425    }
4426
4427  cout << temp << endl;
4428}
4429
4430//----------------------------------------------------------
4431void Cmt::do_show_language (const ArgParser& ap)
4432{
4433  if (ap.arguments.size () > 0) 
4434    {
4435      set_standard_macros ();
4436      Language::show (ap.arguments[0]);
4437    }
4438}
4439
4440//----------------------------------------------------------
4441void Cmt::do_show_languages (const ArgParser& /*ap*/)
4442{
4443  set_standard_macros ();
4444  Language::show_all ();
4445}
4446
4447//----------------------------------------------------------
4448static bool is_upperroot (const cmt_string& name)
4449{
4450  int r = name.find_last_of ("ROOT");
4451  if (r == cmt_string::npos ||
4452      (r + 4) != name.size () ||
4453      r == 0)
4454    return false;
4455  for (int i = 0; i < r; i++)
4456    if (islower (name[i])) return false;
4457  return true;
4458}
4459
4460//----------------------------------------------------------
4461void Cmt::do_show_macro (const ArgParser& ap)
4462{
4463  cmt_string target;
4464
4465  if (ap.arguments.size () > 0) target = ap.arguments[0];
4466
4467  Symbol* symbol;
4468
4469  set_standard_macros ();
4470
4471  symbol = Symbol::find (target);
4472
4473  if (symbol == 0) 
4474    {
4475      cmt_string t = " ";
4476      t += target;
4477      t += " is not defined ";
4478
4479      CmtError::set (CmtError::symbol_not_found, t);
4480
4481      return;
4482    }
4483  else
4484    {
4485      cmt_string t = target;
4486      t += " is a ";
4487
4488      if ((Me.m_action == action_show_macro) ||
4489          (Me.m_action == action_show_macro_value))
4490        {
4491          if (symbol->type != Symbol::SymbolMacro)
4492            {
4493              if (symbol->type == Symbol::SymbolAction)
4494                {
4495                  t += "action";
4496                }
4497              else if (symbol->type == Symbol::SymbolSet)
4498                {
4499                  t += "set";
4500                }
4501              else if (symbol->type == Symbol::SymbolPath)
4502                {
4503                  t += "path";
4504                }
4505              else if (symbol->type == Symbol::SymbolAlias)
4506                {
4507                  t += "alias";
4508                }
4509
4510              if (!is_upperroot (symbol->name))
4511                CmtError::set (CmtError::wrong_symbol_type, t);
4512            }
4513        }
4514      else if ((Me.m_action == action_show_set) ||
4515               (Me.m_action == action_show_set_value))
4516        {
4517          if ((symbol->type != Symbol::SymbolSet) &&
4518              (symbol->type != Symbol::SymbolPath) &&
4519              (symbol->type != Symbol::SymbolAction) &&
4520              (symbol->type != Symbol::SymbolAlias))
4521            {
4522              t += "macro";
4523
4524              if (!is_upperroot (symbol->name))
4525                CmtError::set (CmtError::wrong_symbol_type, t);
4526            }
4527        }
4528    }
4529
4530  if (symbol->value_lists.size () < 1) return;
4531
4532  symbol->show_macro (ap.mode);
4533}
4534
4535//----------------------------------------------------------
4536void Cmt::do_show_macro_names (const ArgParser& ap)
4537{
4538  if (ap.arguments.size () > 0)
4539    {
4540      const cmt_string& pattern = ap.arguments[0];
4541      print_symbol_names (ap.mode, pattern);
4542    }
4543  else
4544    {
4545      print_symbol_names (ap.mode);
4546    }
4547}
4548
4549//----------------------------------------------------------
4550void Cmt::do_show_macro_value (const ArgParser& ap)
4551{
4552  do_show_macro (ap);
4553}
4554
4555//----------------------------------------------------------
4556void Cmt::do_show_macros (const ArgParser& ap)
4557{
4558  if (ap.arguments.size () > 0)
4559    {
4560      const cmt_string& pattern = ap.arguments[0];
4561      print_macros (ap.mode, pattern);
4562    }
4563  else
4564    {
4565      print_macros (ap.mode);
4566    }
4567}
4568
4569//----------------------------------------------------------
4570void Cmt::do_show_manager (const ArgParser& /*ap*/)
4571{
4572  Use& use = Use::current();
4573
4574  cout << use.manager << endl;
4575}
4576
4577//----------------------------------------------------------
4578void Cmt::do_show_packages (const ArgParser& ap)
4579{
4580  PathScanner   scanner;
4581  PackageViewer viewer;
4582
4583  if (ap.arguments.size () == 0)
4584    {
4585      Project::scan_paths (scanner, viewer);
4586    }
4587  else
4588    {
4589      for (int i = 0; i < ap.arguments.size (); i++)
4590        {
4591          const cmt_string& path_name = ap.arguments[i];
4592          if (CmtSystem::test_directory (path_name))
4593            {
4594              scanner.scan_path (path_name, viewer);
4595            }
4596          else
4597            {
4598              CmtMessage::warning ("No such directory: " + path_name);
4599              CmtError::set (CmtError::path_not_found, path_name);
4600            }
4601        }
4602    }
4603}
4604
4605//----------------------------------------------------------
4606void Cmt::do_show_path (const ArgParser& ap, ostream& out)
4607{
4608  Project::show_paths (ap.arguments, out);
4609}
4610
4611//----------------------------------------------------------
4612void Cmt::do_show_pattern (const ArgParser& ap)
4613{
4614  cmt_string name;
4615  if (ap.arguments.size () > 0) name = ap.arguments[0];
4616  Pattern::show (name);
4617}
4618
4619//----------------------------------------------------------
4620void Cmt::do_show_pattern_names (const ArgParser& /*ap*/)
4621{
4622  Pattern::show_all_names ();
4623}
4624
4625//----------------------------------------------------------
4626void Cmt::do_show_patterns (const ArgParser& /*ap*/)
4627{
4628  Pattern::show_all ();
4629}
4630
4631//----------------------------------------------------------
4632void Cmt::do_show_projects (const ArgParser& ap)
4633//void Cmt::do_show_projects (const ArgParser& /*ap*/)
4634{
4635  Project::show_all (ap.mode);
4636}
4637
4638//----------------------------------------------------------
4639void Cmt::do_show_pwd (const ArgParser& /*ap*/)
4640{
4641  cout << Me.m_current_dir << endl;
4642}
4643
4644//----------------------------------------------------------
4645void Cmt::do_show_setup (const ArgParser& ap, ostream& out)
4646//void Cmt::do_show_setup (const ArgParser& ap)
4647{
4648  out << "----------> uses" << endl;
4649  do_show_uses (ap, out);
4650  // This is temporary to ensure that Make - which runs "cmt show setup" -
4651  // does not fail for packages with unresolved uses
4652  if (CmtError::get_last_error_code () == CmtError::package_not_found)
4653    CmtError::clear ();
4654
4655  out << "----------> tags" << endl;
4656  do_show_tags (ap, out);
4657
4658  out << "----------> CMTPATH" << endl;
4659  do_show_path (ap, out);
4660}
4661
4662//----------------------------------------------------------
4663void Cmt::do_show_set (const ArgParser& ap)
4664{
4665  do_show_macro (ap);
4666}
4667
4668//----------------------------------------------------------
4669void Cmt::do_show_set_names (const ArgParser& ap)
4670{
4671  if (ap.arguments.size () > 0)
4672    {
4673      const cmt_string& pattern = ap.arguments[0];
4674      print_symbol_names (ap.mode, pattern);
4675    }
4676  else
4677    {
4678      print_symbol_names (ap.mode);
4679    }
4680}
4681
4682//----------------------------------------------------------
4683void Cmt::do_show_set_value (const ArgParser& ap)
4684{
4685  do_show_macro (ap);
4686}
4687
4688//----------------------------------------------------------
4689void Cmt::do_show_sets (const ArgParser& ap)
4690{
4691  if (ap.arguments.size () > 0)
4692    {
4693      const cmt_string& pattern = ap.arguments[0];
4694      print_macros (ap.mode, pattern);
4695    }
4696  else
4697    {
4698      print_macros (ap.mode);
4699    }
4700}
4701
4702//----------------------------------------------------------
4703void Cmt::do_show_strategies (const ArgParser& /*ap*/)
4704{
4705  Use& current_use = Use::current ();
4706
4707  Project::show_specified_strategies_for_all ();
4708
4709  Project* p = Project::get_current ();
4710
4711  /*
4712    if (p != 0) p->show ();
4713    else cout << "No current project" << endl;
4714  */
4715
4716  cout << "Build strategy     : ";
4717
4718  if (current_use.get_strategy ("BuildPrototypes"))
4719    {
4720      cout << "prototypes";
4721    }
4722  else
4723    {
4724      cout << "no_prototypes";
4725    }
4726 
4727  if (current_use.get_strategy ("InstallArea"))
4728    {
4729      cout << " with_installarea";
4730    }
4731  else
4732    {
4733      cout << " without_installarea";
4734    }
4735 
4736  cout << endl;
4737
4738  cout << "Setup strategy     : ";
4739 
4740  if (current_use.get_strategy ("SetupConfig"))
4741    {
4742      cout << "config";
4743    }
4744  else
4745    {
4746      cout << "no_config";
4747    }
4748 
4749  if (current_use.get_strategy ("SetupRoot"))
4750    {
4751      cout << " root";
4752    }
4753  else
4754    {
4755      cout << " no_root";
4756    }
4757 
4758  if (current_use.get_strategy ("SetupCleanup"))
4759    {
4760      cout << " cleanup";
4761    }
4762  else
4763    {
4764      cout << " no_cleanup";
4765    }
4766
4767  if (current_use.get_strategy ("SetupScripts"))
4768    {
4769      cout << " scripts";
4770    }
4771  else
4772    {
4773      cout << " no_scripts";
4774    }
4775
4776  cout << endl;
4777 
4778  cout << "Structure strategy : ";
4779 
4780  if (current_use.get_strategy ("VersionDirectory"))
4781    {
4782      cout << "with_version_directory";
4783    }
4784  else
4785    {
4786      cout << "without_version_directory";
4787    }
4788 
4789  cout << endl;
4790
4791  if (Me.m_current_structuring_style != default_structuring_style)
4792    {
4793      cout << "Structuring style  : ";
4794
4795      switch (Me.m_current_structuring_style)
4796        {
4797        case without_version_directory:
4798          cout << "without_version_directory";
4799          break;
4800        case with_version_directory:
4801          cout << "with_version_directory";
4802          break;
4803        }
4804
4805      cout << endl;
4806    }
4807}
4808
4809//----------------------------------------------------------
4810void Cmt::do_show_tags (const ArgParser& /*ap*/, ostream& out)
4811{
4812  Tag::TagPtrVector tags = Tag::tags ();
4813  int index;
4814
4815  set_standard_macros ();
4816
4817  for (index = 0; index < tags.size (); index++)
4818    {
4819      const Tag* tag = tags[index];
4820      if (tag != 0)
4821        {
4822          tag->show (Me.m_quiet, out);
4823        }
4824    }
4825}
4826
4827//----------------------------------------------------------
4828void Cmt::do_show_use_paths (const ArgParser& ap)
4829{
4830  const cmt_string& to_name = ap.arguments[0];
4831
4832  Use* current = &(Use::current());
4833
4834  if (!current->get_all_clients (to_name))
4835    CmtError::set (CmtError::package_not_found, to_name);
4836}
4837
4838//----------------------------------------------------------
4839void Cmt::do_show_uses (const ArgParser& ap, ostream& out)
4840//void Cmt::do_show_uses (const ArgParser& /*ap*/, ostream& out)
4841{
4842  Use::show_all (false, out, ap.mode);
4843  //  Use::show_all (false, out);
4844}
4845
4846//----------------------------------------------------------
4847void Cmt::do_show_version (const ArgParser& /*ap*/)
4848{
4849  cout << Me.m_current_version << endl;
4850}
4851
4852//----------------------------------------------------------
4853void Cmt::do_show_versions (const ArgParser& ap)
4854{
4855  cmt_string package_name;
4856
4857  if (ap.arguments.size () > 0) package_name = ap.arguments[0];
4858
4859  PathScanner scanner;
4860
4861  Project::scan_paths_for_package (scanner, package_name);
4862}
4863
4864//----------------------------------------------------------
4865void Cmt::do_show_system (const ArgParser& /*ap*/)
4866{
4867  cout << CmtSystem::get_cmt_config () << endl;
4868}
4869
4870//----------------------------------------------------------
4871void Cmt::do_unlock (const ArgParser& /*ap*/)
4872{
4873  const cmt_string& package = Me.m_current_package;
4874  const cmt_string& version = Me.m_current_version;
4875  const cmt_string& path    = Me.m_current_path;
4876
4877  // (unused??) Use& use = Use::current();
4878
4879  CmtMessage::info ("try to unlock package " + package + " in " + CmtSystem::pwd ());
4880  //  cerr << "try to unlock package " << package << " in " << CmtSystem::pwd () << endl;
4881
4882  set_standard_macros ();
4883
4884  CmtLock::status status = CmtLock::unlock ();
4885}
4886
4887//----------------------------------------------------------
4888void Cmt::do_version (const ArgParser& /*ap*/)
4889{
4890  cout << CMTVERSION << endl;
4891}
4892
4893
4894
4895//----------------------------------------------------------
4896ActionType Cmt::get_action ()
4897{
4898  return (Me.m_action);
4899}
4900
4901/*
4902  const CmtSystem::cmt_string_vector& Cmt::get_cmt_path ()
4903  {
4904  return (Me.m_cmt_path);
4905  }
4906
4907  const CmtSystem::cmt_string_vector& Cmt::get_cmt_path_pwds ()
4908  {
4909  return (Me.m_cmt_path_pwds);
4910  }
4911
4912  const CmtSystem::cmt_string_vector& Cmt::get_cmt_path_sources ()
4913  {
4914  return (Me.m_cmt_path_sources);
4915  }
4916*/
4917
4918const cmt_string& Cmt::get_cmt_home ()
4919{
4920  return (Me.m_cmt_home);
4921}
4922
4923const cmt_string& Cmt::get_cmt_user_context ()
4924{
4925  return (Me.m_cmt_user_context);
4926}
4927
4928const cmt_string& Cmt::get_cmt_version ()
4929{
4930  return (Me.m_cmt_version);
4931}
4932
4933const cmt_string& Cmt::get_current_dir ()
4934{
4935  return (Me.m_current_dir);
4936}
4937
4938const cmt_string Cmt::get_current_dir_real ()
4939{
4940  cmt_string current_dir_real;
4941  if (CmtSystem::realpath_ (Me.m_current_dir, current_dir_real))
4942    return current_dir_real;
4943  return (Me.m_current_dir);
4944}
4945
4946const cmt_string& Cmt::get_current_package ()
4947{
4948  return (Me.m_current_package);
4949}
4950
4951const cmt_string& Cmt::get_current_path ()
4952{
4953  return (Me.m_current_path);
4954}
4955
4956const cmt_string& Cmt::get_current_cmtpath ()
4957{
4958  return (Me.m_current_cmtpath);
4959}
4960
4961const cmt_string& Cmt::get_current_offset ()
4962{
4963  return (Me.m_current_offset);
4964}
4965
4966AccessMode Cmt::get_current_access ()
4967{
4968  return (Me.m_current_access);
4969}
4970
4971CmtStructuringStyle Cmt::get_current_structuring_style ()
4972{
4973  return (Me.m_current_structuring_style);
4974}
4975
4976CmtDirStyle Cmt::get_current_style ()
4977{
4978  return (Me.m_current_style);
4979}
4980
4981const cmt_string& Cmt::get_current_version ()
4982{
4983  return (Me.m_current_version);
4984}
4985
4986const cmt_string& Cmt::get_current_target ()
4987{
4988  return (Me.m_current_target);
4989}
4990
4991bool Cmt::get_debug ()
4992{
4993  return (Me.m_debug);
4994}
4995
4996bool Cmt::get_quiet ()
4997{
4998  return (Me.m_quiet);
4999}
5000
5001bool Cmt::get_disable_warnings ()
5002{
5003  return (Me.m_disable_warnings);
5004}
5005
5006bool Cmt::get_warnings ()
5007{
5008  return (Me.m_warnings);
5009}
5010
5011bool Cmt::get_recursive ()
5012{
5013  return (Me.m_recursive);
5014}
5015
5016CmtScopeFilteringMode Cmt::get_scope_filtering_mode ()
5017{
5018  if (Me.m_scope_filtering_mode == default_filtering_mode)
5019    {
5020      return (block_private_uses);
5021    }
5022  else
5023    {
5024      return (Me.m_scope_filtering_mode);
5025    }
5026}
5027
5028//----------------------------------------------------------
5029bool Cmt::get_all_sets_done ()
5030{
5031  return (Me.m_all_sets_done);
5032}
5033
5034//----------------------------------------------------------
5035void Cmt::get_cmt_flags (CmtSystem::cmt_string_vector& flags)
5036{
5037  CmtSystem::split (CmtSystem::getenv ("CMTFLAGS"), " \t", flags);
5038}
5039
5040//----------------------------------------------------------
5041cmt_string Cmt::get_cmt_flags ()
5042{
5043  return CmtSystem::getenv ("CMTFLAGS");
5044}
5045
5046//----------------------------------------------------------
5047bool Cmt::set_cmt_flags (const cmt_string& cmtflags)
5048{
5049  return CmtSystem::putenv ("CMTFLAGS", cmtflags) ?
5050    true :
5051    (CmtMessage::error ("Cannot set `CMTFLAGS' to `" + cmtflags +
5052                        "' in the environment"),
5053     false) ;
5054}
5055
5056//----------------------------------------------------------
5057bool Cmt::add_cmt_flag (const cmt_string& flag)
5058{
5059  CmtSystem::cmt_string_vector flags;
5060  get_cmt_flags (flags);
5061  cmt_string cmtflags;
5062  for (int i = 0; i < flags.size (); i++)
5063    {
5064      if (flags[i] == flag) continue;
5065      if (cmtflags == "")
5066        cmtflags = flags[i]; 
5067      else
5068        cmtflags += " " + flags[i]; 
5069    }
5070
5071  if (cmtflags == "")
5072    cmtflags = flag; 
5073  else
5074    cmtflags += " " + flag; 
5075
5076  return set_cmt_flags (cmtflags);
5077}
5078
5079/**---------------------------------------------------------
5080   guess_current_project
5081
5082   if current directory is in one of the CMTPATHs, do nothing.
5083
5084   Otherwise, we want to guess the current project
5085
5086   move up in directories until cmt/project.cmt is found
5087
5088   for (;;)
5089   {
5090   if test -f ./cmt/project.cmt
5091   ok:
5092   this directory should become the first entry of the CMTPATH
5093   break
5094
5095   if (at top) break;
5096
5097   move up
5098   }
5099*/
5100void Cmt::guess_current_project ()
5101{
5102  Log;
5103
5104  log << "guess_current_project" << log_endl;
5105
5106  cmt_string here = CmtSystem::pwd ();
5107
5108  Use& use = Use::current();
5109  cmt_string current_path (Me.m_current_dir);
5110  if (use.located ())
5111    {
5112      current_path = use.real_path;
5113      CmtMessage::verbose ("Using current use real_path `" + current_path + "'");
5114    }
5115
5116  if (Project::find_in_cmt_paths (current_path) == "")
5117    //  if (Project::find_in_cmt_paths (Me.m_current_dir) == "")
5118    {
5119      if (!CmtSystem::cd (current_path))
5120        {
5121          CmtError::set (CmtError::file_access_error, current_path);
5122          CmtError::print ();
5123          return;
5124        }
5125      cmt_string project_file = "cmt";
5126      project_file += CmtSystem::file_separator ();
5127      project_file += Project::get_project_file_name ();
5128
5129      cmt_string pwd;
5130
5131      for (;;)
5132        {
5133          pwd = CmtSystem::pwd ();
5134          if (CmtSystem::test_file (project_file))
5135            {
5136              //this directory should become the first entry of the CMTPATH
5137
5138              IProjectFactory& factory = ProjectFactory::instance ();
5139              factory.create_project ("", pwd, "CurrentProject", 0);
5140
5141              break;
5142            }
5143
5144          log << "pwd=" << CmtSystem::pwd () << log_endl;
5145         
5146          if (!CmtSystem::cd (".."))
5147            {
5148              log << "Cannot cd up..." << log_endl;
5149              break;
5150            }
5151
5152          if (CmtSystem::pwd () == pwd)
5153            {
5154              log << "Looks the same pwd..." << log_endl;
5155              break;
5156            }
5157        }
5158
5159      CmtSystem::cd (Me.m_current_dir);
5160    }
5161
5162  //cmt_string buffer = "path CMTPATH \n";
5163  cmt_string buffer;
5164  Project::fill_cmtpaths (buffer);
5165  //cerr << "buffer = " << buffer << endl;
5166  //  Use& use = Use::current();
5167
5168  bool save_quiet = Me.m_quiet;
5169  Me.m_quiet = true;
5170
5171  SyntaxParser::parse_requirements_text (buffer, "", &use);
5172
5173  Me.m_quiet = save_quiet;
5174
5175  Me.m_autoconfigure_cmtpath = true;
5176
5177  CmtSystem::cd (here);
5178}
5179
5180//----------------------------------------------------------
5181const cmt_string& Cmt::filter_dir (const cmt_string& dir)
5182{
5183  static cmt_string newdir;
5184
5185  CmtSystem::compress_path (dir, newdir);
5186
5187  return (newdir);
5188}
5189
5190//----------------------------------------------------------
5191static void dos_script_prefix (FILE* f, 
5192                               const cmt_string& cmt_root, 
5193                               const cmt_string& package, 
5194                               const cmt_string& mangled_package, 
5195                               const cmt_string& version, 
5196                               const cmt_string& path, 
5197                               const cmt_string& action, 
5198                               const cmt_string& option = "")
5199{
5200  cmt_string no_device = path;
5201
5202  if (CmtSystem::absolute_path (path)) 
5203    {
5204      if (path[1] == ':')
5205        {
5206          no_device = path.substr (2);
5207        }
5208    }
5209
5210  if (package == "cmt_standalone")
5211    {
5212      no_device = "";
5213    }
5214  else
5215    {
5216      no_device = "..\\..\\..";
5217      if (Cmt::get_current_structuring_style () == without_version_directory)
5218        //      if (Cmt::get_current_style () == no_version_style)
5219        {
5220          no_device = "..\\..";
5221        }
5222    }
5223
5224
5225  fprintf (f, "@echo off\n");
5226  fprintf (f, "if NOT DEFINED CMTROOT set CMTROOT=%s& set PATH=%%CMTROOT%%\\%%CMTBIN%%;%%PATH%%& set CMTBIN=VisualC& if not defined CMTCONFIG set CMTCONFIG=%%CMTBIN%%\n", cmt_root.c_str ());
5227  fprintf (f, "\n");
5228  fprintf (f, "set cmt%stempfile=\"%%TEMP%%\\cmt%stempfile.bat\"\n", mangled_package.c_str (), mangled_package.c_str ());
5229  fprintf (f, "%%CMTROOT%%\\%%CMTBIN%%\\cmt.exe %s -bat "
5230           " -pack=%s -version=%s -path=\"%%~d0%%~p0%s\" "
5231           " %s "
5232           "%%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 %%9 >%%cmt%stempfile%%\n",
5233           action.c_str (),
5234           package.c_str (),
5235           version.c_str (),
5236           no_device.c_str (),
5237           option.c_str (), mangled_package.c_str ());
5238  fprintf (f, "if exist %%cmt%stempfile%% call %%cmt%stempfile%%\n", mangled_package.c_str (), mangled_package.c_str ());
5239  fprintf (f, "if exist %%cmt%stempfile%% del %%cmt%stempfile%%\n", mangled_package.c_str (), mangled_package.c_str ());
5240  fprintf (f, "set cmt%stempfile=\n", mangled_package.c_str ());
5241}
5242
5243//----------------------------------------------------------
5244//  Mangle text so that it consists solely of
5245//  letters, numbers, and underscores
5246//  (any other characters are replaced with X).
5247//  Then text, prefixed with a letter or underscore,
5248//  can be used as shell variable name
5249//----------------------------------------------------------
5250static cmt_string mangle (const cmt_string& text)
5251{
5252  cmt_string result;
5253  int allocated = text.size ();
5254  char* const buffer = (char*) malloc (allocated + 1);
5255  char* b (buffer);
5256  const char* t = text.c_str ();
5257  while (*t)
5258    {
5259      if (isalnum (*t) || *t == '_')
5260        *b++ = *t++;
5261      else
5262        {
5263          *b++ = 'X'; t++;
5264        }
5265    }
5266  *b = '\0';
5267  result = buffer;
5268  free (buffer);
5269  return result;
5270}
5271
5272//----------------------------------------------------------
5273void Cmt::install_cleanup_scripts ()
5274{
5275#ifdef WIN32
5276  static const int modes = 1;
5277  static const cmt_string suffix[1]   = {"bat"};
5278  static const PrintMode  mode[1]     = {Bat};
5279#else
5280  static const int modes = 2;
5281  static const cmt_string suffix[2]   = {"csh", "sh"};
5282  static const PrintMode  mode[2]     = {Csh, Sh};
5283#endif
5284
5285  Use& current_use = Use::current ();
5286
5287  if (!current_use.get_strategy ("SetupScripts"))
5288    {
5289      for (int i = 0; i < modes; i++)
5290        {
5291          cmt_string old_file_name = "cleanup";
5292          old_file_name += ".";
5293          old_file_name += suffix[i];
5294          if (CmtSystem::test_file (old_file_name))
5295            {
5296              if (CmtMessage::active (Error))
5297                cerr << "Removing cleanup script " + old_file_name << endl;
5298              CmtSystem::remove_file (old_file_name);
5299            }
5300        }
5301      return;
5302    }
5303
5304  if (CmtMessage::active (Error))
5305    cerr << "Creating cleanup scripts." << endl;
5306
5307  cmt_string temp;
5308  int i;
5309
5310  cmt_string version = Me.m_current_version;
5311  if (version == "v*") version = "";
5312  cmt_string mangled_package = mangle (Me.m_current_package);
5313
5314  for (i = 0; i < modes; i++)
5315    {
5316      cmt_string file_name = "cleanup";
5317      file_name += ".";
5318      file_name += suffix[i];
5319      file_name += ".";
5320      file_name += "new";
5321
5322      FILE* f = fopen (file_name.c_str (), "wb");
5323      if (f != NULL)
5324        {
5325          if (mode[i] == Csh)
5326            {
5327              fprintf (f, "if ( $?CMTROOT == 0 ) then\n");
5328              fprintf (f, "  setenv CMTROOT %s\n", Me.m_cmt_root.c_str ());
5329              fprintf (f, "endif\n");
5330              fprintf (f, "source ${CMTROOT}/mgr/setup.csh\n");
5331              fprintf (f, "set cmt%stempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n", mangled_package.c_str ());
5332              fprintf (f, "if $status != 0 then\n  set cmt%stempfile=/tmp/cmt.$$\nendif\n", mangled_package.c_str ());
5333              fprintf (f, "${CMTROOT}/mgr/cmt cleanup -%s "
5334                       "-pack=%s -version=%s -path=%s $* >${cmt%stempfile}\n",
5335                       suffix[i].c_str (),
5336                       Me.m_current_package.c_str (),
5337                       version.c_str (),
5338                       Me.m_current_path.c_str (), mangled_package.c_str ());
5339              fprintf (f,
5340                       "if ( $status != 0 ) then\n"
5341                       "  echo \"${CMTROOT}/mgr/cmt cleanup -%s "
5342                       "-pack=%s -version=%s -path=%s $* >${cmt%stempfile}\"\n"
5343                       "  set cmtcleanupstatus=2\n"
5344                       "  /bin/rm -f ${cmt%stempfile}\n"
5345                       "  unset cmt%stempfile\n"
5346                       "  exit $cmtcleanupstatus\n"
5347                       "endif\n"
5348                       "set cmtcleanupstatus=0\n"
5349                       "source ${cmt%stempfile}\n"
5350                       "if ( $status != 0 ) then\n"
5351                       "  set cmtcleanupstatus=2\n"
5352                       "endif\n"
5353                       "/bin/rm -f ${cmt%stempfile}\n"
5354                       "unset cmt%stempfile\n"
5355                       "exit $cmtcleanupstatus\n",
5356                       suffix[i].c_str (),
5357                       Me.m_current_package.c_str (),
5358                       version.c_str (),
5359                       Me.m_current_path.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str ());
5360            }
5361          else if (mode[i] == Sh)
5362            {
5363              fprintf (f, "if test \"${CMTROOT}\" = \"\"; then\n");
5364              fprintf (f, "  CMTROOT=%s; export CMTROOT\n", Me.m_cmt_root.c_str ());
5365              fprintf (f, "fi\n");
5366              fprintf (f, ". ${CMTROOT}/mgr/setup.sh\n");
5367              fprintf (f, "cmt%stempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n", mangled_package.c_str ());
5368              fprintf (f, "if test ! $? = 0 ; then cmt%stempfile=/tmp/cmt.$$; fi\n", mangled_package.c_str ());
5369              fprintf (f, "${CMTROOT}/mgr/cmt cleanup -%s "
5370                       "-pack=%s -version=%s -path=%s $* >${cmt%stempfile}\n",
5371                       suffix[i].c_str (),
5372                       Me.m_current_package.c_str (),
5373                       version.c_str (),
5374                       Me.m_current_path.c_str (), mangled_package.c_str ());
5375              fprintf (f,
5376                       "if test $? != 0 ; then\n"
5377                       "  echo >&2 \"${CMTROOT}/mgr/cmt cleanup -%s "
5378                       "-pack=%s -version=%s -path=%s $* >${cmt%stempfile}\"\n"
5379                       "  cmtcleanupstatus=2\n"
5380                       "  /bin/rm -f ${cmt%stempfile}\n"
5381                       "  unset cmt%stempfile\n"
5382                       "  return $cmtcleanupstatus\n"
5383                       "fi\n"
5384                       "cmtcleanupstatus=0\n"
5385                       ". ${cmt%stempfile}\n"
5386                       "if test $? != 0 ; then\n"
5387                       "  cmtcleanupstatus=2\n"
5388                       "fi\n"
5389                       "/bin/rm -f ${cmt%stempfile}\n"
5390                       "unset cmt%stempfile\n"
5391                       "return $cmtcleanupstatus\n",
5392                       suffix[i].c_str (),
5393                       Me.m_current_package.c_str (),
5394                       version.c_str (),
5395                       Me.m_current_path.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str ());
5396            }
5397          else if (mode[i] == Bat)
5398            {
5399              dos_script_prefix (f, Me.m_cmt_root, 
5400                                 Me.m_current_package, mangled_package, version, Me.m_current_path,
5401                                 "cleanup");
5402            }
5403
5404          fprintf (f, "\n");
5405
5406          CmtSystem::close_ostream (f, file_name);
5407          //          fclose (f);
5408
5409          cmt_string old_file_name = "cleanup";
5410          old_file_name += ".";
5411          old_file_name += suffix[i];
5412
5413          CmtSystem::compare_and_update_files (file_name, old_file_name);
5414        }
5415      else
5416        {
5417          CmtError::set (CmtError::file_access_error, file_name);
5418        }
5419    }
5420}
5421
5422//----------------------------------------------------------
5423void Cmt::install_setup_scripts ()
5424{
5425#ifdef WIN32
5426  static const int modes = 1;
5427  static const cmt_string suffix[1]   = {"bat"};
5428  static const PrintMode  mode[1]     = {Bat};
5429#else
5430  static const int modes = 2;
5431  static const cmt_string suffix[2]   = {"csh", "sh"};
5432  static const PrintMode  mode[2]     = {Csh, Sh};
5433#endif
5434
5435  Use& current_use = Use::current ();
5436
5437  if (!current_use.get_strategy ("SetupScripts"))
5438    {
5439      for (int i = 0; i < modes; i++)
5440        {
5441          cmt_string old_file_name = "setup";
5442          old_file_name += ".";
5443          old_file_name += suffix[i];
5444          if (CmtSystem::test_file (old_file_name))
5445            {
5446              if (CmtMessage::active (Error))
5447                cerr << "Removing setup script " + old_file_name << endl;
5448              CmtSystem::remove_file (old_file_name);
5449            }
5450        }
5451      return;
5452    }
5453
5454  if (CmtMessage::active (Error))
5455    cerr << "Creating setup scripts." << endl;
5456 
5457  cmt_string no_cleanup_opt;
5458
5459  if (current_use.get_strategy ("SetupCleanup"))
5460    {
5461      no_cleanup_opt = " -no_cleanup";
5462    }
5463
5464  cmt_string temp;
5465  int i;
5466
5467  cmt_string version = Me.m_current_version;
5468  if (version == "v*") version = "";
5469  cmt_string mangled_package = mangle (Me.m_current_package);
5470
5471  for (i = 0; i < modes; i++)
5472    {
5473      cmt_string file_name = "setup";
5474      file_name += ".";
5475      file_name += suffix[i];
5476      file_name += ".";
5477      file_name += "new";
5478
5479      FILE* f = fopen (file_name.c_str (), "wb");
5480      if (f != NULL)
5481        {
5482          if (mode[i] == Csh)
5483            {
5484              fprintf (f, "# echo \"Setting %s %s in %s\"\n",
5485                       Me.m_current_package.c_str (),
5486                       version.c_str (),
5487                       Me.m_current_path.c_str ());
5488              fprintf (f, "\n");
5489
5490              fprintf (f, "if ( $?CMTROOT == 0 ) then\n");
5491              fprintf (f, "  setenv CMTROOT %s\n", Me.m_cmt_root.c_str ());
5492              fprintf (f, "endif\n");
5493              fprintf (f, "source ${CMTROOT}/mgr/setup.csh\n");
5494              fprintf (f, "set cmt%stempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n", mangled_package.c_str ());
5495              fprintf (f, "if $status != 0 then\n  set cmt%stempfile=/tmp/cmt.$$\nendif\n", mangled_package.c_str ());
5496              fprintf (f, "${CMTROOT}/mgr/cmt setup -%s "
5497                       "-pack=%s -version=%s -path=%s %s $* >${cmt%stempfile}\n",
5498                       suffix[i].c_str (),
5499                       Me.m_current_package.c_str (),
5500                       version.c_str (),
5501                       Me.m_current_path.c_str (),
5502                       no_cleanup_opt.c_str (), mangled_package.c_str ());
5503              fprintf (f,
5504                       "if ( $status != 0 ) then\n"
5505                       "  echo \"${CMTROOT}/mgr/cmt setup -%s "
5506                       "-pack=%s -version=%s -path=%s %s $* >${cmt%stempfile}\"\n"
5507                       "  set cmtsetupstatus=2\n"
5508                       "  /bin/rm -f ${cmt%stempfile}\n"
5509                       "  unset cmt%stempfile\n"
5510                       "  exit $cmtsetupstatus\n"
5511                       "endif\n"
5512                       "set cmtsetupstatus=0\n"
5513                       "source ${cmt%stempfile}\n"
5514                       "if ( $status != 0 ) then\n"
5515                       "  set cmtsetupstatus=2\n"
5516                       "endif\n"
5517                       "/bin/rm -f ${cmt%stempfile}\n"
5518                       "unset cmt%stempfile\n"
5519                       "exit $cmtsetupstatus\n",
5520                       suffix[i].c_str (),
5521                       Me.m_current_package.c_str (),
5522                       version.c_str (),
5523                       Me.m_current_path.c_str (),
5524                       no_cleanup_opt.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str ());
5525            }
5526          else if (mode[i] == Sh)
5527            {
5528              fprintf (f, "# echo \"Setting %s %s in %s\"\n",
5529                       Me.m_current_package.c_str (),
5530                       version.c_str (),
5531                       Me.m_current_path.c_str ());
5532              fprintf (f, "\n");
5533
5534              fprintf (f, "if test \"${CMTROOT}\" = \"\"; then\n");
5535              fprintf (f, "  CMTROOT=%s; export CMTROOT\n", Me.m_cmt_root.c_str ());
5536              fprintf (f, "fi\n");
5537              fprintf (f, ". ${CMTROOT}/mgr/setup.sh\n");
5538              fprintf (f, "cmt%stempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n", mangled_package.c_str ());
5539              fprintf (f, "if test ! $? = 0 ; then cmt%stempfile=/tmp/cmt.$$; fi\n", mangled_package.c_str ());
5540              fprintf (f, "${CMTROOT}/mgr/cmt setup -%s "
5541                       "-pack=%s -version=%s -path=%s %s $* >${cmt%stempfile}\n",
5542                       suffix[i].c_str (),
5543                       Me.m_current_package.c_str (),
5544                       version.c_str (),
5545                       Me.m_current_path.c_str (),
5546                       no_cleanup_opt.c_str (), mangled_package.c_str ());
5547              fprintf (f,
5548                       "if test $? != 0 ; then\n"
5549                       "  echo >&2 \"${CMTROOT}/mgr/cmt setup -%s "
5550                       "-pack=%s -version=%s -path=%s %s $* >${cmt%stempfile}\"\n"
5551                       "  cmtsetupstatus=2\n"
5552                       "  /bin/rm -f ${cmt%stempfile}\n"
5553                       "  unset cmt%stempfile\n"
5554                       "  return $cmtsetupstatus\n"
5555                       "fi\n"
5556                       "cmtsetupstatus=0\n"
5557                       ". ${cmt%stempfile}\n"
5558                       "if test $? != 0 ; then\n"
5559                       "  cmtsetupstatus=2\n"
5560                       "fi\n"
5561                       "/bin/rm -f ${cmt%stempfile}\n"
5562                       "unset cmt%stempfile\n"
5563                       "return $cmtsetupstatus\n",
5564                       suffix[i].c_str (),
5565                       Me.m_current_package.c_str (),
5566                       version.c_str (),
5567                       Me.m_current_path.c_str (),
5568                       no_cleanup_opt.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str ());
5569            }
5570          else if (mode[i] == Bat)
5571            {
5572              fprintf (f, "rem Setting %s %s in %%~d0%%~p0\n",
5573                       Me.m_current_package.c_str (),
5574                       version.c_str ());
5575              dos_script_prefix (f, Me.m_cmt_root, 
5576                                 Me.m_current_package, mangled_package, version, Me.m_current_path,
5577                                 "setup", no_cleanup_opt);
5578            }
5579
5580          fprintf (f, "\n");
5581
5582          CmtSystem::close_ostream (f, file_name);
5583          //          fclose (f);
5584
5585          cmt_string old_file_name = "setup";
5586          old_file_name += ".";
5587          old_file_name += suffix[i];
5588
5589          CmtSystem::compare_and_update_files (file_name, old_file_name);
5590        }
5591      else
5592        {
5593          CmtError::set (CmtError::file_access_error, file_name);
5594        }
5595    }
5596}
5597
5598//----------------------------------------------------------
5599void Cmt::install_test_cleanup_scripts ()
5600{
5601#ifdef WIN32
5602  static const int modes = 1;
5603  static const cmt_string suffix[1]   = {"bat"};
5604  static const PrintMode  mode[1]     = {Bat};
5605#else
5606  static const int modes = 2;
5607  static const cmt_string suffix[2]   = {"csh", "sh"};
5608  static const PrintMode  mode[2]     = {Csh, Sh};
5609#endif
5610
5611  Use& current_use = Use::current ();
5612
5613  if (!current_use.get_strategy ("SetupScripts"))
5614    {
5615      for (int i = 0; i < modes; i++)
5616        {
5617          cmt_string old_file_name = "cleanup";
5618          old_file_name += ".";
5619          old_file_name += suffix[i];
5620          if (CmtSystem::test_file (old_file_name))
5621            {
5622              if (CmtMessage::active (Error))
5623                cerr << "Removing cleanup script " + old_file_name << endl;
5624              CmtSystem::remove_file (old_file_name);
5625            }
5626        }
5627      return;
5628    }
5629
5630  if (CmtMessage::active (Error))
5631    cerr << "Creating cleanup scripts." << endl;
5632
5633  cmt_string temp;
5634  int i;
5635
5636  cmt_string version = Me.m_current_version;
5637  if (version == "v*") version = "";
5638  cmt_string mangled_package = mangle (Me.m_current_package);
5639
5640  for (i = 0; i < modes; i++)
5641    {
5642      cmt_string file_name = "cleanup";
5643      file_name += ".";
5644      file_name += suffix[i];
5645      file_name += ".";
5646      file_name += "new";
5647
5648      FILE* f = fopen (file_name.c_str (), "wb");
5649      if (f != NULL)
5650        {
5651          if (mode[i] == Csh)
5652            {
5653              fprintf (f, "if ( $?CMTROOT == 0 ) then\n");
5654              fprintf (f, "  setenv CMTROOT %s\n", Me.m_cmt_root.c_str ());
5655              fprintf (f, "endif\n");
5656              fprintf (f, "source ${CMTROOT}/mgr/setup.csh\n");
5657              fprintf (f, "set cmt%stempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n", mangled_package.c_str ());
5658              fprintf (f, "if $status != 0 then\n  set cmt%stempfile=/tmp/cmt.$$\nendif\n", mangled_package.c_str ());
5659              fprintf (f, "${CMTROOT}/mgr/cmt cleanup -%s -pack=cmt_standalone -path=%s $* >${cmt%stempfile}\n",
5660                       suffix[i].c_str (),
5661                       Me.m_current_path.c_str (), mangled_package.c_str ());
5662              fprintf (f,
5663                       "if ( $status != 0 ) then\n"
5664                       "  echo \"${CMTROOT}/mgr/cmt cleanup -%s "
5665                       "-pack=cmt_standalone -path=%s $* >${cmt%stempfile}\"\n"
5666                       "  set cmtcleanupstatus=2\n"
5667                       "  /bin/rm -f ${cmt%stempfile}\n"
5668                       "  unset cmt%stempfile\n"
5669                       "  exit $cmtcleanupstatus\n"
5670                       "endif\n"
5671                       "set cmtcleanupstatus=0\n"
5672                       "source ${cmt%stempfile}\n"
5673                       "if ( $status != 0 ) then\n"
5674                       "  set cmtcleanupstatus=2\n"
5675                       "endif\n"
5676                       "/bin/rm -f ${cmt%stempfile}\n"
5677                       "unset cmt%stempfile\n"
5678                       "exit $cmtcleanupstatus\n",
5679                       suffix[i].c_str (),
5680                       Me.m_current_path.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str ());
5681            }
5682          else if (mode[i] == Sh)
5683            {
5684              fprintf (f, "if test \"${CMTROOT}\" = \"\"; then\n");
5685              fprintf (f, "  CMTROOT=%s; export CMTROOT\n", Me.m_cmt_root.c_str ());
5686              fprintf (f, "fi\n");
5687              fprintf (f, ". ${CMTROOT}/mgr/setup.sh\n");
5688              fprintf (f, "cmt%stempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n", mangled_package.c_str ());
5689              fprintf (f, "if test ! $? = 0 ; then cmt%stempfile=/tmp/cmt.$$; fi\n", mangled_package.c_str ());
5690              fprintf (f, "${CMTROOT}/mgr/cmt cleanup -%s -pack=cmt_standalone -path=%s $* >${cmt%stempfile}\n",
5691                       suffix[i].c_str (),
5692                       Me.m_current_path.c_str (), mangled_package.c_str ());
5693              fprintf (f,
5694                       "if test $? != 0 ; then\n"
5695                       "  echo >&2 \"${CMTROOT}/mgr/cmt cleanup -%s "
5696                       "-pack=cmt_standalone -path=%s $* >${cmt%stempfile}\"\n"
5697                       "  cmtcleanupstatus=2\n"
5698                       "  /bin/rm -f ${cmt%stempfile}\n"
5699                       "  unset cmt%stempfile\n"
5700                       "  return $cmtcleanupstatus\n"
5701                       "fi\n"
5702                       "cmtcleanupstatus=0\n"
5703                       ". ${cmt%stempfile}\n"
5704                       "if test $? != 0 ; then\n"
5705                       "  cmtcleanupstatus=2\n"
5706                       "fi\n"
5707                       "/bin/rm -f ${cmt%stempfile}\n"
5708                       "unset cmt%stempfile\n"
5709                       "return $cmtcleanupstatus\n",
5710                       suffix[i].c_str (),
5711                       Me.m_current_path.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str ());
5712            }
5713          else if (mode[i] == Bat)
5714            {
5715              dos_script_prefix (f, Me.m_cmt_root, 
5716                                 "cmt_standalone", "cmt_standalone", "", Me.m_current_path, 
5717                                 "cleanup");
5718            }
5719
5720          fprintf (f, "\n");
5721
5722          CmtSystem::close_ostream (f, file_name);
5723          //          fclose (f);
5724
5725          cmt_string old_file_name = "cleanup";
5726          old_file_name += ".";
5727          old_file_name += suffix[i];
5728
5729          CmtSystem::compare_and_update_files (file_name, old_file_name);
5730        }
5731      else
5732        {
5733          CmtError::set (CmtError::file_access_error, file_name);
5734        }
5735    }
5736}
5737
5738//----------------------------------------------------------
5739void Cmt::install_native_version_file ()
5740{   
5741  Use& current_use        = Use::current ();
5742  cmt_string package_name = current_use.get_package_name();
5743  cmt_string macro_name   = package_name + "_native_version";
5744  Symbol* macro           = Symbol::find (macro_name);
5745  if (macro != 0)
5746    {
5747      cmt_string value = macro->resolve_macro_value ();
5748      if (value != "")
5749        {
5750          if (CmtMessage::active (Info))
5751            cerr << "Creating native_version file." << endl;   
5752
5753          cmt_string file_name ("new.native_version.cmt");
5754          FILE* f = fopen (file_name.c_str (), "wb");
5755          if (f != NULL)
5756            {
5757              fprintf (f, "%s\n", value.c_str ());
5758              CmtSystem::close_ostream (f, file_name);
5759              //          fclose (f);
5760             
5761              CmtSystem::compare_and_update_files ("new.native_version.cmt", "native_version.cmt");
5762            }     
5763          else
5764            {
5765              CmtError::set (CmtError::file_access_error, file_name);
5766            }
5767        } 
5768    }   
5769}
5770//----------------------------------------------------------
5771void Cmt::install_test_setup_scripts ()
5772{
5773#ifdef WIN32
5774  static const int modes = 1;
5775  static const cmt_string suffix[1]   = {"bat"};
5776  static const PrintMode  mode[1]     = {Bat};
5777#else
5778  static const int modes = 2;
5779  static const cmt_string suffix[2]   = {"csh", "sh"};
5780  static const PrintMode  mode[2]     = {Csh, Sh};
5781#endif
5782
5783  Use& current_use = Use::current ();
5784
5785  if (!current_use.get_strategy ("SetupScripts"))
5786    {
5787      for (int i = 0; i < modes; i++)
5788        {
5789          cmt_string old_file_name = "setup";
5790          old_file_name += ".";
5791          old_file_name += suffix[i];
5792          if (CmtSystem::test_file (old_file_name))
5793            {
5794              if (CmtMessage::active (Error))
5795                cerr << "Removing setup script " + old_file_name << endl;
5796              CmtSystem::remove_file (old_file_name);
5797            }
5798        }
5799      return;
5800    }
5801
5802  if (CmtMessage::active (Error))
5803    cerr << "Creating setup scripts." << endl;
5804
5805  cmt_string no_cleanup_opt;
5806
5807  if (current_use.get_strategy ("SetupCleanup"))
5808    {
5809      no_cleanup_opt = " -no_cleanup";
5810    }
5811
5812  cmt_string temp;
5813  int i;
5814
5815  cmt_string version = Me.m_current_version;
5816  if (version == "v*") version = "";
5817  cmt_string mangled_package = mangle (Me.m_current_package);
5818
5819  for (i = 0; i < modes; i++)
5820    {
5821      cmt_string file_name = "setup";
5822      file_name += ".";
5823      file_name += suffix[i];
5824      file_name += ".";
5825      file_name += "new";
5826
5827      FILE* f = fopen (file_name.c_str (), "wb");
5828      if (f != NULL)
5829        {
5830          if (mode[i] == Csh)
5831            {
5832              fprintf (f, "# echo \"Setting standalone package\"\n");
5833              fprintf (f, "\n");
5834
5835              fprintf (f, "if ( $?CMTROOT == 0 ) then\n");
5836              fprintf (f, "  setenv CMTROOT %s\n", Me.m_cmt_root.c_str ());
5837              fprintf (f, "endif\n");
5838              fprintf (f, "source ${CMTROOT}/mgr/setup.csh\n");
5839              fprintf (f, "set cmt%stempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n", mangled_package.c_str ());
5840              fprintf (f, "if $status != 0 then\n  set cmt%stempfile=/tmp/cmt.$$\nendif\n", mangled_package.c_str ());
5841              fprintf (f, "${CMTROOT}/mgr/cmt setup -%s -pack=cmt_standalone -path=%s %s $* >${cmt%stempfile}\n",
5842                       suffix[i].c_str (),
5843                       Me.m_current_path.c_str (),
5844                       no_cleanup_opt.c_str (), mangled_package.c_str ());
5845              fprintf (f,
5846                       "if ( $status != 0 ) then\n"
5847                       "  echo \"${CMTROOT}/mgr/cmt setup -%s "
5848                       "-pack=cmt_standalone -path=%s %s $* >${cmt%stempfile}\"\n"
5849                       "  set cmtsetupstatus=2\n"
5850                       "  /bin/rm -f ${cmt%stempfile}\n"
5851                       "  unset cmt%stempfile\n"
5852                       "  exit $cmtsetupstatus\n"
5853                       "endif\n"
5854                       "set cmtsetupstatus=0\n"
5855                       "source ${cmt%stempfile}\n"
5856                       "if ( $status != 0 ) then\n"
5857                       "  set cmtsetupstatus=2\n"
5858                       "endif\n"
5859                       "/bin/rm -f ${cmt%stempfile}\n"
5860                       "unset cmt%stempfile\n"
5861                       "exit $cmtsetupstatus\n",
5862                       suffix[i].c_str (),
5863                       Me.m_current_path.c_str (),
5864                       no_cleanup_opt.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str ());
5865            }
5866          else if (mode[i] == Sh)
5867            {
5868              fprintf (f, "# echo \"Setting standalone package\"\n");
5869              fprintf (f, "\n");
5870
5871              fprintf (f, "if test \"${CMTROOT}\" = \"\"; then\n");
5872              fprintf (f, "  CMTROOT=%s; export CMTROOT\n", Me.m_cmt_root.c_str ());
5873              fprintf (f, "fi\n");
5874              fprintf (f, ". ${CMTROOT}/mgr/setup.sh\n");
5875              fprintf (f, "cmt%stempfile=`${CMTROOT}/mgr/cmt -quiet build temporary_name`\n", mangled_package.c_str ());
5876              fprintf (f, "if test ! $? = 0 ; then cmt%stempfile=/tmp/cmt.$$; fi\n", mangled_package.c_str ());
5877              fprintf (f, "${CMTROOT}/mgr/cmt setup -%s -pack=cmt_standalone -path=%s %s $* >${cmt%stempfile}\n",
5878                       suffix[i].c_str (),
5879                       Me.m_current_path.c_str (),
5880                       no_cleanup_opt.c_str (), mangled_package.c_str ());
5881              fprintf (f,
5882                       "if test $? != 0 ; then\n"
5883                       "  echo >&2 \"${CMTROOT}/mgr/cmt setup -%s "
5884                       "-pack=cmt_standalone -path=%s %s $* >${cmt%stempfile}\"\n"
5885                       "  cmtsetupstatus=2\n"
5886                       "  /bin/rm -f ${cmt%stempfile}\n"
5887                       "  unset cmt%stempfile\n"
5888                       "  return $cmtsetupstatus\n"
5889                       "fi\n"
5890                       "cmtsetupstatus=0\n"
5891                       ". ${cmt%stempfile}\n"
5892                       "if test $? != 0 ; then\n"
5893                       "  cmtsetupstatus=2\n"
5894                       "fi\n"
5895                       "/bin/rm -f ${cmt%stempfile}\n"
5896                       "unset cmt%stempfile\n"
5897                       "return $cmtsetupstatus\n",
5898                       suffix[i].c_str (),
5899                       Me.m_current_path.c_str (),
5900                       no_cleanup_opt.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str (), mangled_package.c_str ());
5901            }
5902          else if (mode[i] == Bat)
5903            {
5904              fprintf (f, "rem Setting standalone package\n");
5905              dos_script_prefix (f, Me.m_cmt_root, 
5906                                 "cmt_standalone", "cmt_standalone", "", Me.m_current_path, 
5907                                 "setup", no_cleanup_opt);
5908            }
5909
5910          fprintf (f, "\n");
5911
5912          CmtSystem::close_ostream (f, file_name);
5913          //          fclose (f);
5914
5915          cmt_string old_file_name = "setup";
5916          old_file_name += ".";
5917          old_file_name += suffix[i];
5918
5919          CmtSystem::compare_and_update_files (file_name, old_file_name);
5920        }
5921      else
5922        {
5923          CmtError::set (CmtError::file_access_error, file_name);
5924        }
5925    }
5926}
5927
5928/**
5929 *    load is only called from the Windows GUI which pretends to access directly
5930 *   the internal data model.
5931 *    This is considered to be rather unsafe, and should be replaced by query functions.
5932 */
5933//bool Cmt::load (const cmt_string& path,
5934bool Cmt::load (const ArgParser& ap,
5935                const cmt_string& path,
5936                const cmt_string& package,
5937                const cmt_string& version,
5938                const cmt_string& tag_name)
5939{
5940  clear ();
5941  //  configure ();
5942  configure (ap);
5943
5944  Me.m_action  = action_load;
5945  Me.m_recursive = true;
5946
5947  if (((package != "") && (version != "")) || (Me.m_current_package == ""))
5948    {
5949      //
5950      //  Here we want to connect to a new package, or to the current package
5951      //  but with another tag.
5952      //
5953      //   the 'package' argument may include a directory offset. Thus 'path'
5954      //  is only expected to hold the base directory.
5955      //
5956      cmt_string offset;
5957      cmt_string package_name;
5958     
5959      CmtSystem::dirname (package, offset);
5960      CmtSystem::basename (package, package_name);
5961     
5962      if (offset != "")
5963        {
5964          Me.m_current_path = path;
5965          Me.m_current_path += CmtSystem::file_separator ();
5966          Me.m_current_path += offset;
5967        }
5968      else
5969        {
5970          Me.m_current_path = path;
5971        }
5972     
5973      Me.m_current_package = package_name;
5974      Me.m_current_version = version;
5975    }
5976
5977  if (tag_name != "")
5978    {
5979      Tag* tag;
5980
5981      Tag::unmark_all ();
5982      configure_version_tag ();
5983      configure_site_tag (0);
5984      configure_uname_tag ();
5985      configure_hosttype_tag ();
5986
5987      Me.m_current_tag = tag_name;
5988
5989      //if (!Me.m_quiet) cerr << "load1> current_tag=" << Me.m_current_tag << endl;
5990
5991      tag = Tag::add (tag_name, PriorityTag, "load", 0);
5992      tag->mark ();
5993    }
5994
5995  /*
5996    Set to developer mode if positioned into the package
5997    (which is detected since we were able to retreive the
5998    Version, Package and Path)
5999  */
6000
6001  if ((Me.m_current_path == "") ||
6002      (Me.m_current_package == "") ||
6003      (Me.m_current_version == ""))
6004    {
6005      Me.m_current_access = UserMode;
6006    }
6007  else
6008    {
6009      Me.m_current_access = DeveloperMode;
6010    }
6011
6012  use_cmt ();
6013
6014  cmt_string dir;
6015
6016  /*
6017    Try to access the package.
6018  */
6019
6020  if (Me.m_current_path != "")
6021    {
6022      dir = Me.m_current_path;
6023    }
6024  else
6025    {
6026      dir = Me.m_default_path;
6027    }
6028
6029  if (!CmtSystem::cd (Me.m_current_path))
6030    {
6031      CmtMessage::error ("Cannot reach the directory " + Me.m_current_path);
6032      /*
6033      if (!Me.m_quiet)
6034        {
6035          cerr << "#CMT> Cannot reach the directory " <<
6036            Me.m_current_path << endl;
6037        }
6038      */
6039      CmtError::set (CmtError::package_not_found, "Load> Cannot reach the path directory");
6040      CmtSystem::cd (Me.m_current_dir);
6041
6042      return (false);
6043    }
6044
6045  dir += CmtSystem::file_separator ();
6046  dir += Me.m_current_package;
6047
6048  if (!CmtSystem::cd (Me.m_current_package))
6049    {
6050      CmtMessage::error ("Cannot reach the package " + Me.m_current_package);
6051      /*
6052      if (!Me.m_quiet)
6053        {
6054          cerr << "#CMT::load> Cannot reach the package " <<
6055            Me.m_current_package << endl;
6056        }
6057      */
6058      CmtError::set (CmtError::package_not_found, "Load> Cannot reach the package directory");
6059      CmtSystem::cd (Me.m_current_dir);
6060
6061      return (false);
6062    }
6063
6064  dir += CmtSystem::file_separator ();
6065  dir += Me.m_current_version;
6066
6067  Me.m_current_style = none_style;
6068  Me.m_current_structuring_style = without_version_directory;
6069
6070  if (!CmtSystem::cd (Me.m_current_version))
6071    {
6072      if (!CmtSystem::test_directory ("cmt"))
6073        {
6074          CmtMessage::error ("Cannot reach the version " + Me.m_current_version);
6075          /*
6076          if (!Me.m_quiet)
6077            {
6078              cerr << "#CMT> Cannot reach the version " <<
6079                Me.m_current_version << endl;
6080            }
6081          */
6082          CmtError::set (CmtError::package_not_found, "Load> Cannot reach the version directory");
6083          CmtSystem::cd (Me.m_current_dir);
6084
6085          return (false);
6086        }
6087      else
6088        {
6089          Me.m_current_style = cmt_style;
6090          Me.m_current_structuring_style = without_version_directory;
6091          //          Me.m_current_style = no_version_style;
6092        }
6093    }
6094
6095  if (CmtSystem::cd ("cmt"))
6096    {
6097      dir += CmtSystem::file_separator ();
6098      dir += "cmt";
6099      if (Me.m_current_style == none_style)
6100        {
6101          Me.m_current_style = cmt_style;
6102          Me.m_current_structuring_style = with_version_directory;
6103        }
6104    }
6105  else
6106    {
6107      /*
6108        if (!Me.m_quiet)
6109        {
6110        cerr << "Cannot reach the cmt branch" << endl;
6111        }
6112      */
6113
6114      if (CmtSystem::cd ("mgr"))
6115        {
6116          dir += CmtSystem::file_separator ();
6117          dir += "mgr";
6118          if (Me.m_current_style == none_style)
6119            {
6120              Me.m_current_style = mgr_style;
6121              Me.m_current_structuring_style = with_version_directory;
6122            }
6123        }
6124      else
6125        {
6126          CmtMessage::error ("Cannot reach the mgr branch");
6127          /*
6128          if (!Me.m_quiet)
6129            {
6130              cerr << "#CMT> Cannot reach the mgr branch" << endl;
6131            }
6132          */
6133
6134          CmtError::set (CmtError::package_not_found,
6135                         "Load> Cannot reach the mgr/cmt directory");
6136          CmtSystem::cd (Me.m_current_dir);
6137
6138          return (false);
6139        }
6140    }
6141
6142  /*
6143    Check Tag is always set up
6144  */
6145
6146  if (Me.m_current_tag == "")
6147    {
6148      char* env;
6149
6150      env = getenv (Me.m_current_config.c_str ());
6151      if (env != 0)
6152        {
6153          Tag* tag;
6154
6155          tag = Tag::add (env, PriorityConfig, "load", 0);
6156          tag->mark ();
6157          Me.m_current_tag = env;
6158
6159          //if (!Me.m_quiet) cerr << "load2> current_tag=" << Me.m_current_tag << endl;
6160
6161        }
6162      else
6163        {
6164          Me.m_current_tag = Me.m_cmt_config;
6165
6166          //if (!Me.m_quiet) cerr << "load3> current_tag=" << Me.m_current_tag << endl;
6167
6168        }
6169    }
6170
6171  if (Me.m_debug)
6172    {
6173      cout << "pwd = " << CmtSystem::pwd () << endl;
6174    }
6175
6176  configure_current_dir ();
6177  build_prefix (Me.m_current_package, Me.m_current_prefix);
6178  build_config (Me.m_current_prefix, Me.m_current_config);
6179
6180  Use* use = &(Use::current());
6181  use->path    = Me.m_current_path;
6182  use->set_package_name (Me.m_current_package);
6183  use->version = Me.m_current_version;
6184  use->prefix  = Me.m_current_prefix;
6185  use->done    = false;
6186  use->style   = Me.m_current_style;
6187  use->structuring_style  = Me.m_current_structuring_style;
6188
6189  /*
6190    Work on the requirements file.
6191  */
6192
6193  dir += CmtSystem::file_separator ();
6194  dir += "requirements";
6195  SyntaxParser::parse_requirements (dir, use);
6196
6197  if (CmtError::has_pending_error ()) return (false);
6198
6199  /**
6200   * See reach_current_package for an explanation of this call
6201   */
6202  Pattern::apply_all_globals ();
6203
6204  /*
6205    Select all possible tags
6206  */
6207
6208  Tag::restore_tree ();
6209
6210  return (true);
6211}
6212
6213//----------------------------------------------------------
6214bool Cmt::need_prototypes ()
6215{
6216  Use& current_use = Use::current ();
6217
6218  if (current_use.get_strategy ("BuildPrototypes")) return (true);
6219  else return (false);
6220}
6221
6222//----------------------------------------------------------
6223void Cmt::parse_arguments (ArgParser& ap)
6224{
6225  /*
6226    Decoding arguments.
6227
6228    While decoding all arguments, no requirements analysis should
6229    occur. Every new option, or parameter should be saved and
6230    used later at actual analysis time.
6231  */
6232
6233  Me.m_action = action_none;
6234
6235  restore_all_tags (0);
6236
6237#ifdef WIN32
6238  Me.m_build_nmake = true;
6239#endif
6240
6241  ap.parse ();
6242}
6243
6244//----------------------------------------------------------
6245int Cmt::parser (const cmt_string& command_line)
6246{
6247  CmtSystem::cmt_string_vector v;
6248
6249  CmtSystem::split (command_line, " \t", v);
6250
6251  int argc = v.size ();
6252
6253  char** argv = (char**) malloc ((argc + 1) * sizeof (char*));
6254
6255  int i;
6256  for (i = 0; i < argc; i++)
6257    {
6258      argv[i] = (char*) v[i].c_str ();
6259    }
6260  argv[argc] = 0;
6261
6262  int status = parser (argc, argv);
6263
6264  free (argv);
6265
6266  return (status);
6267}
6268
6269//----------------------------------------------------------
6270int Cmt::parser (int argc, char* argv[])
6271{
6272  ArgParser ap (Me);
6273 
6274  ap.argc = argc;
6275  ap.argv = argv;
6276
6277  if (argc <= 1)
6278    {
6279      do_help (ap);
6280      return 2;
6281      //      exit (0);
6282    }
6283
6284  // save CMTFLAGS
6285  cmt_string cmtflags (get_cmt_flags ());
6286
6287  clear ();
6288
6289  //  configure ();
6290  configure (ap);
6291
6292  CmtError::clear ();
6293
6294  /*
6295    Set private if positioned inside the package
6296    (which is detected since we were able to retreive the
6297    Version, Package and Path)
6298  */
6299
6300  if ((Me.m_current_path.size () == 0) ||
6301      (Me.m_current_package.size () == 0) ||
6302      (Me.m_current_version.size () == 0))
6303    {
6304      Me.m_current_access = UserMode;
6305    }
6306  else
6307    {
6308      Me.m_current_access = DeveloperMode;
6309    }
6310 
6311  parse_arguments (ap);
6312 
6313  if (ap.help_action == action_help)
6314    {
6315      int code (0);
6316      if (CmtError::has_pending_error ())
6317        {
6318          code = CmtError::get_last_error_code ();
6319          CmtError::print ();
6320        }
6321      do_help (ap);
6322      return code;
6323    }
6324
6325  if (Me.m_debug)
6326    {
6327      cout << "After parse_argument> pack=" << Me.m_current_package
6328           << " Me.m_current_tag=" << Me.m_current_tag
6329           << " cwd=" << CmtSystem::pwd () 
6330           << " mode=" << Me.m_current_access
6331           << endl;
6332    }
6333
6334  if (Me.m_configure_error != "")
6335    {
6336      CmtMessage::error (Me.m_configure_error);
6337      //      if (!Me.m_quiet) cerr << "#CMT> Error: " << Me.m_configure_error << endl;
6338      return (CmtError::execution_error);
6339    }
6340
6341  if (CmtError::has_pending_error ())
6342    {
6343      int code = CmtError::get_last_error_code ();
6344      CmtError::print ();
6345      //      if (!Me.m_quiet) CmtError::print ();
6346      clear ();
6347
6348      return (code);
6349    }
6350
6351  /*
6352    Now actual requirements analysis can take place.
6353
6354    Extra lines or files are analysed first.
6355  */
6356
6357  //  if (strlen (ap.extra_file.c_str ()) > 0) SyntaxParser::parse_requirements (ap.extra_file, (Use*) 0);
6358  if (strlen (ap.extra_line.c_str ()) > 0) SyntaxParser::parse_requirements_line (ap.extra_line, (Use*) 0);
6359
6360  //
6361  //  For some of the actions, the CMT package must be automatically
6362  //  included
6363  //
6364
6365  if (Me.m_debug) cout << "parser1> current_tag=" << Me.m_current_tag << endl;
6366
6367//   if (ap.help_action == action_help)
6368//     {
6369//       do_help (ap);
6370//       int code (0);
6371//       if (CmtError::has_pending_error ())
6372//      {
6373//        code = CmtError::get_last_error_code ();
6374//        CmtError::print ();
6375//      }
6376//       return code;
6377//       //return (0);
6378//     }
6379
6380  switch (Me.m_action)
6381    {
6382      // case action_none :
6383    case action_awk :
6384    case action_broadcast :
6385    case action_build_constituent_makefile :
6386    case action_build_constituent_config :
6387    case action_build_constituents_makefile :
6388    case action_build_constituents_config :
6389    case action_build_dependencies :
6390    case action_build_library_links :
6391    case action_build_make_setup :
6392    case action_build_msdev :
6393    case action_build_CMT_pacman :
6394    case action_build_vsnet :     
6395    case action_build_os9_makefile :
6396      // case action_build_prototype :
6397    case action_build_readme :
6398    case action_build_tag_makefile :
6399      // case action_build_temporary_name :
6400    case action_build_triggers :
6401    case action_build_windefs :
6402    case action_check_configuration :
6403      // case action_check_files :
6404      // case action_check_version :
6405    case action_checkout :
6406    case action_cleanup :
6407    case action_config :
6408    case action_create :
6409      // case action_create_project :
6410    case action_cvsbranches :
6411    case action_cvssubpackages :
6412    case action_cvssubprojects :
6413    case action_cvstags :
6414    case action_do :
6415    case action_expand_model :
6416    case action_filter :
6417      // case action_help :
6418    case action_load :
6419    case action_lock :
6420    case action_relocate:
6421    case action_remove :
6422    case action_remove_library_links :
6423    case action_run :
6424    case action_run_sequence :
6425    case action_set_version :
6426    case action_set_versions :
6427    case action_setup :
6428    case action_show_action :
6429    case action_show_action_names :
6430    case action_show_action_value :
6431    case action_show_actions :
6432    case action_show_all_tags :
6433    case action_show_applied_patterns :
6434      // case action_show_author :
6435      // case action_show_branches :
6436      // case action_show_clients :
6437    case action_show_cmtpath_patterns :
6438    case action_show_constituent :
6439    case action_show_constituent_names :
6440    case action_show_constituents :
6441    case action_show_container :
6442    case action_show_cycles :
6443    case action_show_fragment :
6444    case action_show_fragments :
6445    case action_show_groups :
6446    case action_show_include_dirs :
6447    case action_show_language :
6448    case action_show_languages :
6449    case action_show_macro :
6450    case action_show_macro_names :
6451    case action_show_macro_value :
6452    case action_show_macros :
6453      // case action_show_manager :
6454    case action_show_packages :
6455    case action_show_path :
6456    case action_show_pattern :
6457    case action_show_pattern_names :
6458    case action_show_patterns :
6459    case action_show_projects :
6460      // case action_show_pwd :
6461    case action_show_setup :
6462    case action_show_set :
6463    case action_show_set_names :
6464    case action_show_set_value :
6465    case action_show_sets :
6466    case action_show_strategies :
6467    case action_show_tags :
6468    case action_show_use_paths :
6469    case action_show_uses :
6470    case action_show_version :
6471      // case action_show_versions :
6472      // case action_system :
6473    case action_unlock :
6474    case action_version :
6475      if (Me.m_use_cmt)
6476        {
6477      use_cmt ();
6478        }
6479      //
6480      // Now parse the requirements file stored in ${CMTHOME}
6481      //
6482     
6483      use_home_requirements ();
6484
6485      configure_devenv_tag ();
6486
6487      break;
6488    default:
6489      break;
6490    }
6491
6492  if (Me.m_debug) cout << "parser2> current_tag=" << Me.m_current_tag << endl;
6493
6494  //
6495  // Setting up recursive actions
6496  //
6497
6498  switch (Me.m_action)
6499    {
6500      // case action_none :
6501    case action_awk :
6502    case action_broadcast :
6503    case action_build_constituent_makefile :
6504    case action_build_constituent_config :
6505    case action_build_constituents_makefile :
6506    case action_build_constituents_config :
6507    case action_build_dependencies :
6508    case action_build_library_links :
6509    case action_build_make_setup :
6510    case action_build_msdev :
6511    case action_build_CMT_pacman :
6512    case action_build_vsnet :     
6513    case action_build_os9_makefile :
6514      // case action_build_prototype :
6515    case action_build_readme :
6516    case action_build_tag_makefile :
6517      // case action_build_temporary_name :
6518    case action_build_triggers :
6519    case action_build_windefs :
6520    case action_check_configuration :
6521      // case action_check_files :
6522      // case action_check_version :
6523      // case action_checkout :
6524    case action_cleanup :
6525    case action_config :
6526      // case action_create :
6527      // case action_create_project :
6528      // case action_cvsbranches :
6529      // case action_cvssubpackages :
6530      // case action_cvssubprojects :
6531      // case action_cvstags :
6532    case action_do :
6533    case action_expand_model :
6534    case action_filter :
6535      // case action_help :
6536    case action_load :
6537      // case action_lock :
6538      // case action_remove :
6539    case action_relocate:
6540    case action_remove_library_links :
6541    case action_run :
6542    case action_run_sequence :
6543      // case action_set_version :
6544    case action_set_versions :
6545    case action_setup :
6546    case action_show_action :
6547    case action_show_action_names :
6548    case action_show_action_value :
6549    case action_show_actions :
6550    case action_show_all_tags :
6551    case action_show_applied_patterns :
6552      // case action_show_author :
6553      // case action_show_branches :
6554      // case action_show_clients :
6555    case action_show_cmtpath_patterns :
6556    case action_show_constituent :
6557    case action_show_constituent_names :
6558    case action_show_constituents :
6559    case action_show_container :
6560    case action_show_cycles :
6561    case action_show_fragment :
6562    case action_show_fragments :
6563    case action_show_groups :
6564    case action_show_include_dirs :
6565    case action_show_language :
6566    case action_show_languages :
6567    case action_show_macro :
6568    case action_show_macro_names :
6569    case action_show_macro_value :
6570    case action_show_macros :
6571      // case action_show_manager :
6572    case action_show_packages :
6573    case action_show_path :
6574    case action_show_pattern :
6575    case action_show_pattern_names :
6576    case action_show_patterns :
6577    case action_show_projects :
6578      // case action_show_pwd :
6579    case action_show_setup :
6580    case action_show_set :
6581    case action_show_set_names :
6582    case action_show_set_value :
6583    case action_show_sets :
6584    case action_show_strategies :
6585    case action_show_tags :
6586    case action_show_use_paths :
6587    case action_show_uses :
6588      // case action_show_version :
6589      // case action_show_versions :
6590      // case action_system :
6591      // case action_unlock :
6592      // case action_version :
6593      Me.m_recursive = true;
6594      break;
6595    default:
6596      Me.m_recursive = false;
6597      break;
6598    }
6599
6600  //
6601  //  Actions for which the context of the package is checked,
6602  //  and the requirements file is analysed.
6603  //
6604
6605  switch (Me.m_action)
6606    {
6607    case action_none :
6608    case action_awk :
6609    case action_broadcast :
6610    case action_build_constituent_makefile :
6611    case action_build_constituent_config :
6612    case action_build_constituents_makefile :
6613    case action_build_constituents_config :
6614    case action_build_dependencies :
6615    case action_build_library_links :
6616    case action_build_make_setup :
6617    case action_build_msdev :
6618    case action_build_CMT_pacman :
6619    case action_build_vsnet :     
6620    case action_build_os9_makefile :
6621      // case action_build_prototype :
6622    case action_build_readme :
6623    case action_build_tag_makefile :
6624      // case action_build_temporary_name :
6625    case action_build_triggers :
6626    case action_build_windefs :
6627    case action_check_configuration :
6628      // case action_check_files :
6629      // case action_check_version :
6630      // case action_checkout :
6631    case action_cleanup :
6632    case action_config :
6633      // case action_create :
6634      // case action_create_project :
6635      // case action_cvsbranches :
6636      // case action_cvssubpackages :
6637      // case action_cvssubprojects :
6638      // case action_cvstags :
6639    case action_do :
6640    case action_expand_model :
6641    case action_filter :
6642      // case action_help :
6643    case action_load :
6644    case action_lock :
6645      // case action_remove :
6646    case action_relocate :
6647    case action_remove_library_links :
6648    case action_run :
6649      // case action_run_sequence :
6650      // case action_set_version :
6651    case action_set_versions :
6652    case action_setup :
6653    case action_show_action :
6654    case action_show_action_names :
6655    case action_show_action_value :
6656    case action_show_actions :
6657    case action_show_all_tags :
6658    case action_show_applied_patterns :
6659    case action_show_author :
6660    case action_show_branches :
6661      // case action_show_clients :
6662    case action_show_cmtpath_patterns :
6663    case action_show_constituent :
6664    case action_show_constituent_names :
6665    case action_show_constituents :
6666    case action_show_container :
6667    case action_show_cycles :
6668    case action_show_fragment :
6669    case action_show_fragments :
6670    case action_show_groups :
6671    case action_show_include_dirs :
6672    case action_show_language :
6673    case action_show_languages :
6674    case action_show_macro :
6675    case action_show_macro_names :
6676    case action_show_macro_value :
6677    case action_show_macros :
6678    case action_show_manager :
6679    case action_show_packages :
6680    case action_show_path :
6681    case action_show_pattern :
6682    case action_show_pattern_names :
6683    case action_show_patterns :
6684    case action_show_projects :
6685    case action_show_project_author :
6686    case action_show_pwd :
6687    case action_show_setup :
6688    case action_show_set :
6689    case action_show_set_names :
6690    case action_show_set_value :
6691    case action_show_sets :
6692    case action_show_strategies :
6693    case action_show_tags :
6694    case action_show_use_paths :
6695    case action_show_uses :
6696    case action_show_version :
6697      // case action_show_versions :
6698      // case action_system :
6699    case action_unlock :
6700      // case action_version :
6701      {
6702        bool w = Me.m_warnings;
6703        Me.m_warnings = false;
6704        if (strlen (ap.extra_file.c_str ()) == 0)
6705          reach_current_package ();
6706        else
6707          use_extra_file (ap.extra_file);
6708        use_user_context_requirements ();
6709        Me.m_warnings = w;
6710      }
6711      break;
6712    default:
6713      break;
6714    }
6715
6716  if (Me.m_debug) cout << "parser3> current_tag=" << Me.m_current_tag << endl;
6717
6718  //
6719  // Perform some actions even if there is an error
6720  //
6721
6722  if (CmtError::has_pending_error ())
6723    {
6724      int code = CmtError::get_last_error_code ();
6725      if (!Me.m_quiet) CmtError::print ();
6726
6727      switch (Me.m_action)
6728        {
6729          // case action_none :
6730          // case action_awk :
6731          // case action_broadcast :
6732        case action_build_constituent_makefile :
6733        case action_build_constituent_config :
6734        case action_build_constituents_makefile :
6735        case action_build_constituents_config :
6736        case action_build_dependencies :
6737        case action_build_library_links :
6738        case action_build_make_setup :
6739        case action_build_msdev :
6740        case action_build_CMT_pacman :
6741        case action_build_vsnet :     
6742        case action_build_os9_makefile :
6743        case action_build_prototype :
6744        case action_build_readme :
6745        case action_build_tag_makefile :
6746          // case action_build_temporary_name :
6747        case action_build_triggers :
6748        case action_build_windefs :
6749        case action_check_configuration :
6750          // case action_check_files :
6751          // case action_check_version :
6752          // case action_checkout :
6753        case action_cleanup :
6754          // case action_config :
6755          // case action_create :
6756          // case action_create_project :
6757          // case action_cvsbranches :
6758          // case action_cvssubpackages :
6759          // case action_cvssubprojects :
6760          // case action_cvstags :
6761          // case action_do :
6762          // case action_expand_model :
6763          // case action_filter :
6764          // case action_help :
6765        case action_load :
6766        case action_lock :
6767        case action_relocate :
6768        case action_remove :
6769        case action_remove_library_links :
6770          // case action_run :
6771        case action_run_sequence :
6772          // case action_set_version :
6773          // case action_set_versions :
6774        case action_setup :
6775          // case action_show_action :
6776          // case action_show_action_names :
6777          // case action_show_action_value :
6778          // case action_show_actions :
6779          // case action_show_all_tags :
6780          // case action_show_applied_patterns :
6781          // case action_show_author :
6782          // case action_show_branches :
6783          // case action_show_clients :
6784          // case action_show_cmtpath_patterns :
6785          // case action_show_constituent :
6786          // case action_show_container :
6787          // case action_show_constituent_names :
6788          // case action_show_constituents :
6789          // case action_show_cycles :
6790          // case action_show_fragment :
6791          // case action_show_fragments :
6792          // case action_show_groups :
6793          // case action_show_include_dirs :
6794          // case action_show_language :
6795          // case action_show_languages :
6796          // case action_show_macro :
6797          // case action_show_macro_names :
6798          // case action_show_macro_value :
6799          // case action_show_macros :
6800          // case action_show_manager :
6801          // case action_show_packages :
6802          // case action_show_path :
6803          // case action_show_pattern :
6804          // case action_show_pattern_names :
6805          // case action_show_patterns :
6806          // case action_show_projects :
6807          // case action_show_pwd :
6808          // case action_show_setup :
6809          // case action_show_set :
6810          // case action_show_set_names :
6811          // case action_show_set_value :
6812          // case action_show_sets :
6813          // case action_show_strategies :
6814          // case action_show_tags :
6815          // case action_show_use_paths :
6816          // case action_show_uses :
6817          // case action_show_version :
6818          // case action_show_versions :
6819          // case action_system :
6820        case action_unlock :
6821          // case action_version :
6822          clear ();
6823          return (code);
6824        default:
6825          CmtError::clear ();
6826          break;
6827        }
6828    }
6829
6830  //
6831  // Perform actions
6832  //
6833
6834  if (!Me.m_simulation)
6835    {
6836      switch (Me.m_action)
6837        {
6838        case action_none :
6839          //CmtError::set (CmtError::syntax_error, "ParseArguments> ");
6840          break;
6841        case action_awk :
6842          do_awk (ap);
6843          break;
6844        case action_broadcast :
6845          do_broadcast (ap);
6846          break;
6847        case action_build_constituent_makefile :
6848          do_build_constituent_makefile (ap);
6849          break;
6850        case action_build_constituent_config :
6851          do_build_constituent_config (ap);
6852          break;
6853        case action_build_constituents_makefile :
6854          do_build_constituents_makefile (ap);
6855          break;
6856        case action_build_constituents_config :
6857          do_build_constituents_config (ap);
6858          break;
6859        case action_build_dependencies :
6860          do_build_dependencies (ap, argc, argv);
6861          break;
6862        case action_build_library_links :
6863          do_build_library_links (ap);
6864          break;
6865        case action_build_make_setup :
6866          do_build_make_setup (ap);
6867          break;
6868        case action_build_msdev :
6869          do_build_msdev (ap);
6870          break;
6871        case action_build_CMT_pacman :
6872          do_build_CMT_pacman (ap);
6873          break;
6874        case action_build_vsnet :     
6875          do_build_vsnet (ap);
6876          break;
6877        case action_build_os9_makefile :
6878          do_build_os9_makefile (ap);
6879          break;
6880        case action_build_prototype :
6881          do_build_prototype (ap);
6882          break;
6883        case action_build_readme :
6884          do_build_readme (ap);
6885          break;
6886        case action_build_tag_makefile :
6887          do_build_tag_makefile (ap);
6888          break;
6889        case action_build_temporary_name :
6890          do_build_temporary_name (ap);
6891          break;
6892        case action_build_triggers :
6893          do_build_triggers (ap);
6894          break;
6895        case action_build_windefs :
6896          do_build_windefs (ap);
6897          break;
6898        case action_check_configuration :
6899          do_check_configuration (ap);
6900          break;
6901        case action_check_files :
6902          do_check_files (ap);
6903          break;
6904        case action_check_version :
6905          do_check_version (ap);
6906          break;
6907        case action_checkout :
6908          do_checkout (ap);
6909          break;
6910        case action_cleanup :
6911          do_cleanup (ap);
6912          break;
6913        case action_config :
6914          do_config (ap);
6915          break;
6916        case action_create :
6917          do_create (ap);
6918          break;
6919        case action_create_project :
6920          do_create_project (ap);
6921          break;
6922        case action_cvsbranches :
6923          do_cvsbranches (ap);
6924          break;
6925        case action_cvssubpackages :
6926          do_cvssubpackages (ap);
6927          break;
6928        case action_cvssubprojects :
6929          do_cvssubprojects (ap);
6930          break;
6931        case action_cvstags :
6932          do_cvstags (ap);
6933          break;
6934        case action_do :
6935          do_do (ap);
6936          break;
6937        case action_expand_model :
6938          do_expand_model (ap);
6939          break;
6940        case action_filter :
6941          do_filter (ap);
6942          break;
6943        case action_help :
6944          do_help (ap);
6945          break;
6946        case action_load :
6947          CmtMessage::error ("action not implemented");
6948          //      cerr << "#CMT> action not implemented" << endl;
6949          break;
6950        case action_lock :
6951          do_lock (ap);
6952          break;
6953        case action_relocate :
6954          do_relocate (ap);
6955          break;
6956        case action_remove :
6957          do_remove (ap);
6958          break;
6959        case action_remove_library_links :
6960          do_remove_library_links (ap);
6961          break;
6962        case action_run :
6963          do_run (ap);
6964          break;
6965        case action_run_sequence :
6966          do_run_sequence (ap);
6967          break;
6968        case action_set_version :
6969          do_set_version (ap);
6970          break;
6971        case action_set_versions :
6972          do_set_versions (ap);
6973          break;
6974        case action_setup :
6975          do_setup (ap);
6976          break;
6977        case action_show_action :
6978          do_show_action (ap);
6979          break;
6980        case action_show_action_names :
6981          do_show_action_names (ap);
6982          break;
6983        case action_show_action_value :
6984          do_show_action_value (ap);
6985          break;
6986        case action_show_actions :
6987          do_show_actions (ap);
6988          break;
6989        case action_show_all_tags :
6990          do_show_all_tags (ap);
6991          break;
6992        case action_show_applied_patterns :
6993          do_show_applied_patterns (ap);
6994          break;
6995        case action_show_author :
6996          do_show_author (ap);
6997          break;
6998        case action_show_branches :
6999          do_show_branches (ap);
7000          break;
7001        case action_show_container :
7002          do_show_container (ap);
7003          break;
7004        case action_show_clients :
7005          do_show_clients (ap);
7006          break;
7007        case action_show_cmtpath_patterns :
7008          do_show_cmtpath_patterns (ap);
7009          break;
7010        case action_show_constituent :
7011          do_show_constituent (ap);
7012          break;
7013        case action_show_constituent_names :
7014          do_show_constituent_names (ap);
7015          break;
7016        case action_show_constituents :
7017          do_show_constituents (ap);
7018          break;
7019        case action_show_cycles :
7020          do_show_cycles (ap);
7021          break;
7022        case action_show_fragment :
7023          do_show_fragment (ap);
7024          break;
7025        case action_show_fragments :
7026          do_show_fragments (ap);
7027          break;
7028        case action_show_groups :
7029          do_show_groups (ap);
7030          break;
7031        case action_show_include_dirs :
7032          do_show_include_dirs (ap);
7033          break;
7034        case action_show_language :
7035          do_show_language (ap);
7036          break;
7037        case action_show_languages :
7038          do_show_languages (ap);
7039          break;
7040        case action_show_macro :
7041          do_show_macro (ap);
7042          break;
7043        case action_show_macro_names :
7044          do_show_macro_names (ap);
7045          break;
7046        case action_show_macro_value :
7047          do_show_macro_value (ap);
7048          break;
7049        case action_show_macros :
7050          do_show_macros (ap);
7051          break;
7052        case action_show_manager :
7053          do_show_manager (ap);
7054          break;
7055        case action_show_packages :
7056          do_show_packages (ap);
7057          break;
7058        case action_show_path :
7059          do_show_path (ap);
7060          break;
7061        case action_show_pattern :
7062          do_show_pattern (ap);
7063          break;
7064        case action_show_pattern_names :
7065          do_show_pattern_names (ap);
7066          break;
7067        case action_show_patterns :
7068          do_show_patterns (ap);
7069          break;
7070        case action_show_projects :
7071          do_show_projects (ap);
7072          break;
7073        case action_show_project_author :
7074          do_show_project_author (ap);
7075          break;
7076        case action_show_pwd :
7077          do_show_pwd (ap);
7078          break;
7079        case action_show_setup :
7080          do_show_setup (ap);
7081          break;
7082        case action_show_set :
7083          do_show_set (ap);
7084          break;
7085        case action_show_set_names :
7086          do_show_set_names (ap);
7087          break;
7088        case action_show_set_value :
7089          do_show_set_value (ap);
7090          break;
7091        case action_show_sets :
7092          do_show_sets (ap);
7093          break;
7094        case action_show_strategies :
7095          do_show_strategies (ap);
7096          break;
7097        case action_show_tags :
7098          do_show_tags (ap);
7099          break;
7100        case action_show_use_paths :
7101          do_show_use_paths (ap);
7102          break;
7103        case action_show_uses :
7104          do_show_uses (ap);
7105          break;
7106        case action_show_version :
7107          do_show_version (ap);
7108          break;
7109        case action_show_versions :
7110          do_show_versions (ap);
7111          break;
7112        case action_system :
7113          do_show_system (ap);
7114          break;
7115        case action_unlock :
7116          do_unlock (ap);
7117          break;
7118        case action_version :
7119          do_version (ap);
7120          break;
7121        default:
7122          CmtError::set (CmtError::syntax_error, "ParseArguments>");
7123          break;
7124        }
7125    }
7126
7127  // restore CMTFLAGS
7128  set_cmt_flags (cmtflags);
7129
7130  if (CmtError::has_pending_error ())
7131    {
7132      int code = CmtError::get_last_error_code ();     
7133      if (code == CmtError::execution_failed)
7134        {
7135          code = CmtError::get_last_execution_error();
7136        }   
7137     
7138      CmtError::print ();
7139      //      if (!Me.m_quiet) CmtError::print ();
7140      clear ();
7141      return (code);
7142    }
7143  else
7144    {
7145      clear ();
7146      return (0);
7147    }
7148}
7149
7150
7151/**
7152 * Format as one single line a set of 'setenv' statements
7153 * joined with semi-colons to form one shell command.
7154 */
7155void Cmt::print (PrintMode mode)
7156{
7157  Use::UsePtrVector& Uses = Use::get_ordered_uses ();
7158  Use& current_use = Use::current ();
7159
7160  cmt_string tag;
7161
7162  set_standard_macros ();
7163
7164  //cerr << "# current_tag=" << Me.m_current_tag << endl;
7165  //cerr << "# current_config=" << Me.m_current_config << endl;
7166
7167  if (Me.m_current_tag == "")
7168    {
7169      if (mode == Bat) tag = "%CMTCONFIG%";
7170      else tag = "${CMTCONFIG}";
7171    }
7172  else
7173    {
7174      tag = Me.m_current_tag;
7175    }
7176
7177  //
7178  //  Now check if all extra tags are still valid. Some of them
7179  //  may be discarded du to some conflict with highest priority
7180  //  tags, or with exclude statements
7181  //
7182
7183  {
7184    CmtSystem::cmt_string_vector words;
7185     
7186    cmt_string tags;
7187
7188    tags = Me.m_extra_tags;
7189     
7190    CmtSystem::split (tags, " \t,", words);
7191
7192    Me.m_extra_tags = ",";
7193     
7194    for (int i = 0; i < words.size (); i++)
7195      {
7196        Tag* tag;
7197        const cmt_string& a = words[i];
7198
7199        tag = Tag::find (a);
7200
7201        if ((tag != 0) && (tag->is_selected ()))
7202          {
7203            Me.m_extra_tags += a;
7204            Me.m_extra_tags += ",";
7205          }
7206      }
7207  }
7208
7209  if (mode == Requirements)
7210    {
7211      //do_show_tags (ap, cout);
7212      Tag::TagPtrVector tags = Tag::tags ();
7213      for (int index = 0; index < tags.size (); index++)
7214        {
7215          const Tag* tag = tags[index];
7216          if (tag != 0)
7217            {
7218              tag->show (true, cout, "apply_tag ");
7219            }
7220        }
7221    }
7222
7223  if (Me.m_debug)
7224    {
7225      cout << "Before all print contexts" << endl;
7226    }
7227
7228  if (Uses.size () > 0)
7229    {
7230      int number;
7231
7232      for (number = 0; number < Uses.size (); number++)
7233        {
7234          Use& use = *(Uses[number]);
7235
7236          if (use.discarded) continue;
7237          if (use.m_hidden) continue;
7238         
7239          print_context (use, mode, tag);
7240        }
7241    }
7242
7243  print_context (Use::current (), mode, tag);
7244
7245  if (Me.m_debug)
7246    {
7247      cout << "After all print contexts" << endl;
7248    }
7249
7250  Symbol::all_print (mode);
7251  // Script::all_print (mode);
7252
7253  if (Me.m_debug)
7254    {
7255      cout << "After all print" << endl;
7256    }
7257
7258  switch (mode)
7259    {
7260    case Xml :
7261      break;
7262    default :
7263  cout << endl;
7264      break;
7265    }
7266}
7267
7268
7269/**
7270 * Format as one single line a set of 'unsetenv' statements
7271 * joined with semi-colons to form one shell command.
7272 */
7273void Cmt::print_clean (PrintMode mode)
7274{
7275  Use::UsePtrVector& Uses = Use::get_ordered_uses ();
7276
7277  set_standard_macros ();
7278
7279  Script::all_print_clean (mode);
7280  Symbol::all_print_clean (mode);
7281
7282  switch (mode)
7283    {
7284    case Csh :
7285      if (Me.m_current_package != "CMT")
7286        {
7287          cout << "unsetenv " << Me.m_current_prefix << "ROOT" << endl;
7288          cout << "unsetenv " << Me.m_current_prefix << "CONFIG" << endl;
7289        }
7290      break;
7291    case Sh :
7292      if (Me.m_current_package != "CMT")
7293        {
7294          cout << "unset " << Me.m_current_prefix << "ROOT" << endl;
7295          cout << "unset " << Me.m_current_prefix << "CONFIG" << endl;
7296        }
7297      break;
7298    case Bat :
7299      if (Me.m_current_package != "CMT")
7300        {
7301          cout << "set " << Me.m_current_prefix << "ROOT=" << endl;
7302          cout << "set " << Me.m_current_prefix << "CONFIG=" << endl;
7303        }
7304      break;
7305    }
7306
7307  if (Uses.size () > 0)
7308    {
7309      int number;
7310
7311      for (number = 0; number < Uses.size (); number++)
7312        {
7313          Use* use = Uses[number];
7314
7315          if (use->discarded) continue;
7316          if (use->m_hidden) continue;
7317
7318          Package* p = use->get_package ();
7319          if (p->is_cmt ()) continue;
7320
7321
7322          switch (mode)
7323            {
7324            case Csh :
7325              cout << "unsetenv " << use->prefix << "ROOT" << endl;
7326              cout << "unsetenv " << use->prefix << "CONFIG" << endl;
7327              break;
7328            case Sh :
7329              cout << "unset " << use->prefix << "ROOT" << endl;
7330              cout << "unset " << use->prefix << "CONFIG" << endl;
7331              break;
7332            case Bat :
7333              cout << "set " << use->prefix << "ROOT=" << endl;
7334              cout << "set " << use->prefix << "CONFIG=" << endl;
7335              break;
7336            }
7337        }
7338    }
7339
7340  switch (mode)
7341    {
7342    case Csh :
7343      cout << "unsetenv CMTEXTRATAGS" << endl;
7344      break;
7345    case Sh :
7346      cout << "unset CMTEXTRATAGS" << endl;
7347      break;
7348    case Bat :
7349      cout << "set CMTEXTRATAGS=" << endl;
7350      break;
7351    }
7352
7353  cout << endl;
7354}
7355
7356//----------------------------------------------------------
7357void Cmt::print_context (Use& use, PrintMode mode, const cmt_string& tag)
7358{
7359  if (use.get_package_name () == "cmt_standalone") return;
7360
7361  cmt_string fs = CmtSystem::file_separator ();
7362
7363  use.real_path.replace_all (CmtSystem::file_separator (), fs);
7364
7365  cmt_string system = CmtSystem::get_cmt_config ();
7366
7367  bool do_config = use.get_strategy ("SetupConfig");
7368  bool do_root   = use.get_strategy ("SetupRoot");
7369   
7370 
7371  switch (mode)
7372    {
7373    case Csh :
7374      if (do_root)
7375        {
7376          cout << "setenv " << use.prefix << "ROOT \"" <<
7377            use.get_full_path () << "\"" << endl;
7378        }
7379
7380      if (use.get_package_name () == "CMT")
7381        {
7382          cout << "setenv CMTCONFIG " << system << endl;
7383        }
7384      else
7385        {
7386          if (do_config)
7387            {
7388              cout << "setenv " << use.prefix << "CONFIG \"" << tag << "\"" << endl;
7389            }
7390        }
7391       
7392      break;
7393    case Sh :
7394      if (do_root)
7395        {
7396          cout << use.prefix << "ROOT=\"" <<
7397            use.get_full_path () << "\"; export " <<
7398            use.prefix << "ROOT" << endl;
7399        }
7400
7401      if (use.get_package_name () == "CMT")
7402        {
7403          cout << "CMTCONFIG=" << system << "; export CMTCONFIG" << endl;
7404        }
7405      else
7406        {
7407          if (do_config)
7408            {
7409              cout << use.prefix << "CONFIG=\"" <<
7410                tag << "\"; export " <<
7411                use.prefix << "CONFIG" << endl;
7412            }
7413        }
7414       
7415      break;
7416    case Bat :
7417      if (do_root)
7418        {
7419          cout << "set " << use.prefix << "ROOT=" <<
7420            use.get_full_path () << endl;
7421        }
7422
7423      if (use.get_package_name () == "CMT")
7424        {
7425          cout << "set CMTCONFIG=" << system << endl;
7426        }
7427      else
7428        {
7429          if (do_config)
7430            {
7431              cout << "set " << use.prefix << "CONFIG=" << tag << endl;
7432            }
7433        }
7434       
7435      break;
7436    case Xml :
7437      if (do_root)
7438        {
7439          cout << "<variable><name>" << use.prefix << "ROOT</name>"
7440               << "<value>" << use.get_full_path () << "</value></variable>";
7441        }
7442
7443      if (use.get_package_name () == "CMT")
7444        {
7445          cout << "<variable><name>CMTCONFIG</name>"
7446               << "<value>" << system << "</value></variable>";
7447        }
7448      else
7449        {
7450          if (do_config)
7451            {
7452              cout << "<variable><name>" << use.prefix << "CONFIG</name>"
7453                   << "<value>" << tag << "</value></variable>";
7454            }
7455        }
7456       
7457      break;
7458    case Requirements :
7459      if (do_root &&
7460          use.get_package_name () != "CMT")
7461        {
7462          cout << "set " << use.prefix << "ROOT \"" <<
7463            use.get_full_path () << "\"" << endl;
7464
7465          cmt_string name (use.prefix + "ROOT");
7466          Symbol* symbol = Symbol::find (name);
7467          if (symbol != 0) 
7468            {
7469              symbol->printed = true;
7470            }
7471          if (symbol == 0 || symbol->type != Symbol::SymbolMacro)
7472            {
7473              CmtMessage::warning
7474                (CmtError::get_error_name (CmtError::symbol_not_found)
7475                 + ": macro " + name);
7476            }
7477        }
7478
7479      if (use.get_package_name () == "CMT")
7480        {
7481          cout << "set CMTCONFIG \"" << system << "\"" << endl;
7482        }
7483      else
7484        {
7485          if (do_config)
7486            {
7487              cout << "set " << use.prefix << "CONFIG \"" << tag << "\"" << endl;
7488            }
7489        }
7490       
7491      break;
7492    }
7493}
7494
7495/**
7496 *  Format a set of make macro definitions (one per line)
7497 * Each macro value is provided enclosed in single quotes
7498 *
7499 *  Take the macro values from the macro statements found
7500 * in recursively read requirements files.
7501 */
7502void Cmt::print_symbol_names (PrintMode mode, const cmt_string& pattern)
7503{
7504  int number;
7505
7506  set_standard_macros ();
7507
7508  cmt_regexp expression (pattern);
7509
7510  bool has_pattern = (pattern != "");
7511
7512  for (number = 0; number < Symbol::symbol_number (); number++)
7513    {
7514      Symbol& symbol = Symbol::symbol (number);
7515
7516      if (has_pattern)
7517        {
7518          if (!expression.match (symbol.name)) continue;
7519        }
7520
7521      if (Me.m_action == action_show_macro_names)
7522        {
7523          // Only keep macros.
7524          if ((symbol.type == Symbol::SymbolSet) ||
7525              (symbol.type == Symbol::SymbolAlias) ||
7526              (symbol.type == Symbol::SymbolPath) ||
7527              (symbol.type == Symbol::SymbolAction)) continue;
7528        }
7529      else if (Me.m_action == action_show_set_names)
7530        {
7531          // Exclude macros.
7532          if ((symbol.type == Symbol::SymbolMacro) ||
7533              (symbol.type == Symbol::SymbolAction)) continue;
7534        }
7535      else if (Me.m_action == action_show_action_names)
7536        {
7537          // Exclude macros.
7538          if (symbol.type != Symbol::SymbolAction) continue;
7539        }
7540
7541      cout << symbol.name << endl;
7542    }
7543}
7544
7545/**
7546 *  Format a set of make macro definitions (one per line)
7547 * Each macro value is provided enclosed in single quotes
7548 *
7549 *  Take the macro values from the macro statements found
7550 * in recursively read requirements files.
7551 */
7552void Cmt::print_macros (PrintMode mode, const cmt_string& pattern, ostream& out)
7553//void Cmt::print_macros (PrintMode mode, const cmt_string& pattern)
7554{
7555  int number;
7556
7557  set_standard_macros ();
7558
7559  cmt_regexp expression (pattern);
7560
7561  bool has_pattern = (pattern != "");
7562
7563  for (number = 0; number < Symbol::symbol_number (); number++)
7564    {
7565      Symbol& symbol = Symbol::symbol (number);
7566
7567      if (has_pattern)
7568        {
7569          if (!expression.match (symbol.name)) continue;
7570        }
7571
7572      if (Me.m_action == action_show_macros)
7573        {
7574          // Only keep macros.
7575          if ((symbol.type == Symbol::SymbolSet) ||
7576              (symbol.type == Symbol::SymbolAlias) ||
7577              (symbol.type == Symbol::SymbolPath) ||
7578              (symbol.type == Symbol::SymbolAction)) continue;
7579        }
7580      else if (Me.m_action == action_show_sets)
7581        {
7582          // Exclude macros.
7583          if ((symbol.type == Symbol::SymbolMacro) ||
7584              (symbol.type == Symbol::SymbolAction)) continue;
7585        }
7586      else if ((Me.m_action == action_build_tag_makefile) ||
7587               (Me.m_action == action_build_constituents_config) ||
7588               (Me.m_action == action_build_constituent_config))
7589        {
7590          // Exclude scripts and actions
7591          if ((symbol.type == Symbol::SymbolSetupScript) ||
7592              (symbol.type == Symbol::SymbolCleanupScript) ||
7593              (symbol.type == Symbol::SymbolAction)) continue;
7594        }
7595      else if (Me.m_action == action_show_actions)
7596        {
7597          if (symbol.type != Symbol::SymbolAction) continue;
7598        }
7599
7600      if (symbol.value_lists.size () < 1) continue;
7601
7602      symbol.show_macro (mode, out);
7603      //      symbol.show_macro (mode);
7604    }
7605}
7606
7607//----------------------------------------------------------
7608void Cmt::print_tabs (int tabs)
7609{
7610  while (tabs > 0)
7611    {
7612      cout << "  ";
7613      tabs--;
7614    }
7615}
7616
7617//----------------------------------------------------------
7618void Cmt::print_xml_prolog (const cmt_string& root, ostream& out)
7619{
7620  out << "<?xml version=\"1.0\" standalone=\"no\"?>" << endl;
7621  cmt_string dtd_file (Me.m_cmt_root);
7622  dtd_file += CmtSystem::file_separator ();
7623  dtd_file += "mgr";
7624  dtd_file += CmtSystem::file_separator ();
7625  dtd_file += "CMT.dtd";
7626  out << "<!DOCTYPE " + root + " SYSTEM \"" + dtd_file + "\">" << endl;
7627}
7628
7629//----------------------------------------------------------
7630int Cmt::reach_current_package ()
7631{
7632  Use& use = Use::current ();
7633  cmt_string dir;
7634
7635  if (Me.m_debug)
7636    {
7637      cout << "Cmt::reach_current_package> pwd = " 
7638           << CmtSystem::pwd () 
7639           << " path=" << Me.m_current_path
7640           << " package=" << Me.m_current_package
7641           << endl;
7642    }
7643
7644  /*
7645    Try to access the package.
7646  */
7647
7648  if (Me.m_current_package == "cmt_standalone")
7649    {
7650      if ((Me.m_current_path != "") && (Me.m_current_path != CmtSystem::pwd ()))
7651        {
7652          if (!CmtSystem::cd (Me.m_current_path))
7653            {
7654              CmtError::set (CmtError::package_not_found,
7655                             "ReachCurrentPackage> Cannot reach the path directory");
7656              return (0);
7657            }
7658        }
7659
7660      if (!CmtSystem::test_file ("requirements"))
7661        {
7662          return (0);
7663        }
7664
7665      Me.m_current_structuring_style = without_version_directory;
7666    }
7667  else if (Me.m_current_package != "")
7668    {
7669      if (!use.move_to () && !use.move_to ("", true))
7670        //      if (!use.move_to ())
7671        {
7672          CmtError::set (CmtError::package_not_found,
7673                         "ReachCurrentPackage> Cannot reach the path directory");
7674          return (0);
7675        }
7676
7677      Me.m_current_path = use.real_path;
7678    }
7679  else
7680    {
7681      //
7682      // The cmt command has been given without explicit search for
7683      // a package. Thus it is expected that we are in the context of a
7684      // true package.
7685      //
7686      //  This means that there should be a requirements file visible.
7687      //
7688      //  If this is not true, we'll make a try into ../cmt and then
7689      // a last try into ../mgr
7690      //
7691
7692      if (!CmtSystem::test_file ("requirements"))
7693        {
7694          if (CmtSystem::cd ("../cmt") && 
7695              CmtSystem::test_file ("requirements"))
7696            {
7697              Me.m_current_style = cmt_style;
7698            }
7699          else if (CmtSystem::cd ("../mgr") && 
7700                   CmtSystem::test_file ("requirements"))
7701            {
7702              Me.m_current_style = mgr_style;
7703            }
7704          else
7705            {
7706              CmtMessage::error ("Cannot reach the mgr branch");
7707              /*
7708              if (!Me.m_quiet)
7709                {
7710                  cerr << "#CMT> Cannot reach the mgr branch" << endl;
7711                }
7712              */
7713             
7714              CmtError::set (CmtError::package_not_found,
7715                             "ReachCurrentPackage> Cannot reach the mgr/cmt directory");
7716              return (0);
7717            }
7718        }
7719      else
7720        Me.m_current_style = none_style;
7721
7722      dir = CmtSystem::pwd ();
7723
7724      CmtSystem::dirname (dir, Me.m_current_path);
7725      CmtSystem::basename (Me.m_current_path, Me.m_current_version);
7726
7727      if (CmtSystem::is_version_directory (Me.m_current_version))
7728        {
7729          CmtSystem::dirname (Me.m_current_path, Me.m_current_path);
7730          CmtSystem::basename (Me.m_current_path, Me.m_current_package);
7731          CmtSystem::dirname (Me.m_current_path, Me.m_current_path);
7732          Me.m_current_structuring_style = with_version_directory;
7733        }
7734      else
7735        {
7736          Me.m_current_package = Me.m_current_version;
7737          Me.m_current_version = "";
7738          CmtSystem::dirname (Me.m_current_path, Me.m_current_path);
7739          Me.m_current_structuring_style = without_version_directory;
7740          //          Me.m_current_style = no_version_style;
7741        }
7742
7743      use.set_package_name (Me.m_current_package);
7744      use.version = Me.m_current_version;
7745      use.path    = Me.m_current_path;
7746      use.style   = Me.m_current_style;
7747      use.structuring_style = Me.m_current_structuring_style;
7748    }
7749
7750  configure_current_dir ();
7751  build_prefix (Me.m_current_package, Me.m_current_prefix);
7752  build_config (Me.m_current_prefix, Me.m_current_config);
7753
7754  /*
7755    Check Tag is always set up
7756  */
7757
7758  if (Me.m_debug) cout << "reach_current_package0> current_tag=" << Me.m_current_tag << endl;
7759
7760  if (Me.m_current_tag == "")
7761    {
7762      cmt_string env;
7763
7764      env = CmtSystem::getenv (Me.m_current_config);
7765      if (env != "")
7766        {
7767          Tag* tag;
7768
7769          tag = Tag::add (env, PriorityConfig, "reach current package", 0);
7770          tag->mark ();
7771          //Me.m_current_tag = env;
7772
7773          //if (!Me.m_quiet) cerr << "reach_current_package1> current_tag=" << Me.m_current_tag << endl;
7774
7775        }
7776    }
7777
7778  if (Me.m_debug)
7779    {
7780      cout << "pwd = " << CmtSystem::pwd () << endl;
7781    }
7782
7783  /*
7784    Work on the requirements file.
7785  */
7786
7787  if (dir != "") dir += CmtSystem::file_separator ();
7788  dir += "requirements";
7789  SyntaxParser::parse_requirements (dir, &use);
7790
7791  if (Me.m_debug) cout << "reach_current_package2> current_tag=" << Me.m_current_tag << endl;
7792
7793  /**
7794   *   It would be useful to change this mechanism. Instead of
7795   *  applying all global patterns at once to all use contexts, it
7796   *  would be much better to apply it at the end of each
7797   *  requirements file parsing, and only in the context the
7798   *  appropriate Use.
7799   *
7800   *   This would avoid the current flaw which is that when a global
7801   *  pattern specifies a "private" definition, it is actually
7802   *  applied in the scope context of the Current Use and not in
7803   *  each individual Use. Therefore the private is lost.
7804   *
7805   *   However, this induces problems since some pattern definitions
7806   *  are done AFTER the use statements, which will NOT receive the
7807   *  pattern aplications.
7808   *
7809   *   Therefore it is decided to leave this "bad" mechanism until
7810   *  everybody is aware of this constraint.
7811   *
7812   *
7813   */
7814  Pattern::apply_all_globals ();
7815
7816  /*
7817    Select all possible tags
7818  */
7819
7820  Tag::restore_tree ();
7821
7822  return (1);
7823}
7824
7825//----------------------------------------------------------
7826int Cmt::use_extra_file (const cmt_string& file)
7827{
7828  if (!CmtSystem::test_file (file))
7829    {
7830      CmtError::set (CmtError::path_not_found, file);
7831      return 0;
7832    }
7833
7834  // (almost) Cmt::reach_current_package ()
7835  Use& use = Use::current ();
7836  /*
7837    Try to access the package.
7838  */
7839
7840  if (Me.m_current_package == "cmt_standalone")
7841    {
7842      if ((Me.m_current_path != "") && (Me.m_current_path != CmtSystem::pwd ()))
7843        {
7844          if (!CmtSystem::cd (Me.m_current_path))
7845            {
7846              CmtError::set (CmtError::package_not_found,
7847                             "ReachCurrentPackage> Cannot reach the path directory");
7848              return (0);
7849            }
7850        }
7851      /*
7852      if (!CmtSystem::test_file ("requirements"))
7853        {
7854          return (0);
7855        }
7856      */
7857      Me.m_current_structuring_style = without_version_directory;
7858    }
7859  else if (Me.m_current_package != "")
7860    {
7861      if (!use.move_to () && !use.move_to ("", true))
7862        //      if (!use.move_to ())
7863        {
7864          CmtError::set (CmtError::package_not_found,
7865                         "ReachCurrentPackage> Cannot reach the path directory");
7866          //          return -1;
7867          return (0);
7868        }
7869
7870      Me.m_current_path = use.real_path;
7871    }
7872  else
7873    {
7874      //
7875      // The cmt command has been given without explicit search for
7876      // a package. Thus it is expected that we are in the context of a
7877      // true package.
7878      //
7879      //  This means that there should be a requirements file visible.
7880      //
7881      //  If this is not true, we'll make a try into ../cmt and then
7882      // a last try into ../mgr
7883      //
7884
7885      //...
7886    }
7887
7888  configure_current_dir ();
7889  build_prefix (Me.m_current_package, Me.m_current_prefix);
7890  build_config (Me.m_current_prefix, Me.m_current_config);
7891
7892  /*
7893    Check Tag is always set up
7894  */
7895
7896  if (Me.m_debug) cout << "use_extra_file0> current_tag=" << Me.m_current_tag << endl;
7897
7898  if (Me.m_current_tag == "")
7899    {
7900      cmt_string env;
7901
7902      env = CmtSystem::getenv (Me.m_current_config);
7903      if (env != "")
7904        {
7905          Tag* tag;
7906
7907          tag = Tag::add (env, PriorityConfig, "reach current package", 0);
7908          tag->mark ();
7909          //Me.m_current_tag = env;
7910
7911          //if (!Me.m_quiet) cerr << "use_extra_file1> current_tag=" << Me.m_current_tag << endl;
7912
7913        }
7914    }
7915
7916  if (Me.m_debug)
7917    {
7918      cout << "pwd = " << CmtSystem::pwd () << endl;
7919    }
7920
7921  /*
7922    Work on the requirements file.
7923  */
7924
7925  SyntaxParser::parse_requirements (file, &use);
7926
7927  if (Me.m_debug) cout << "use_extra_file2> current_tag=" << Me.m_current_tag << endl;
7928
7929  /**
7930   *   It would be useful to change this mechanism. Instead of
7931   *  applying all global patterns at once to all use contexts, it
7932   *  would be much better to apply it at the end of each
7933   *  requirements file parsing, and only in the context the
7934   *  appropriate Use.
7935   *
7936   *   This would avoid the current flaw which is that when a global
7937   *  pattern specifies a "private" definition, it is actually
7938   *  applied in the scope context of the Current Use and not in
7939   *  each individual Use. Therefore the private is lost.
7940   *
7941   *   However, this induces problems since some pattern definitions
7942   *  are done AFTER the use statements, which will NOT receive the
7943   *  pattern aplications.
7944   *
7945   *   Therefore it is decided to leave this "bad" mechanism until
7946   *  everybody is aware of this constraint.
7947   *
7948   *
7949   */
7950  Pattern::apply_all_globals ();
7951
7952  /*
7953    Select all possible tags
7954  */
7955
7956  Tag::restore_tree ();
7957
7958  return (1);
7959}
7960
7961//----------------------------------------------------------
7962static cmt_string get_best_form (const CmtSystem::cmt_string_vector& pwd,
7963                                 const cmt_string& path)
7964{
7965  static cmt_string fs = CmtSystem::file_separator ();
7966  cmt_string result;
7967
7968  /*
7969  //if (CmtSystem::getenv ("CMTTESTPREFIX") != "")
7970  {
7971  */
7972
7973  //
7974  //  If there is a common prefix between
7975  //  use->real_path and pwd
7976  //  we have
7977  //  use->real_path = /<prefix>/aaa
7978  //  pwd            = /<prefix>/bbb
7979  //
7980  //  Then use->real_path may be expressed as:
7981  //  ../..../../aaa
7982  //   where ../..../../ moves up to /<prefix>
7983  //
7984  //   Then we try to find the shortest between
7985  //
7986  //     /<prefix> and ../..../..
7987  //
7988  cmt_string a = path;
7989 
7990  CmtSystem::cmt_string_vector va;
7991 
7992  va.clear ();
7993 
7994  CmtSystem::split (a, fs, va);
7995 
7996  int m = va.size ();
7997  if (pwd.size () < m) m = pwd.size ();
7998 
7999  int i;
8000 
8001  for (i = 0; i < m; i++)
8002    {
8003      const cmt_string& fa = va[i];
8004      const cmt_string& fb = pwd[i];
8005     
8006      if (fa != fb) break;
8007    }
8008 
8009  cmt_string ups = "";
8010 
8011  if (i > 0)
8012    {
8013      // We have the prefix.
8014      // if we count what remains from pwd, then
8015      // we have the number of ../ required to
8016      // move to /<prefix>
8017      int j;
8018     
8019      for (j = i; j < pwd.size (); j++)
8020        {
8021          if (j > i) ups += fs;
8022          ups += "..";
8023        }
8024
8025      for (j = i; j < va.size (); j++)
8026        {
8027          ups += fs;
8028          ups += va[j];
8029        }
8030    }
8031 
8032  //
8033  // Here ups contains the ../..../../aaa form
8034  // for the use->real_path or is empty when there
8035  // were no common prefix.
8036  //
8037 
8038  //if (ups != "")
8039  if ((ups != "") &&
8040      (ups.size () < path.size ()))
8041    {
8042      result = ups;
8043    }
8044  else
8045    {
8046      result = path;
8047    }
8048
8049  return (result);
8050}
8051
8052/**
8053 *   This completely local class holds primitive actions for building
8054 *   standard macros.
8055 */
8056class StandardMacroBuilder
8057{
8058public:
8059
8060  /**
8061   *  CMTVERSION
8062   */
8063  void fill_for_CMTVERSION ()
8064  {
8065    buffer = "macro CMTVERSION \"";
8066    buffer += CMTVERSION;
8067    buffer += "\"";
8068    apply ();
8069  }
8070
8071  StandardMacroBuilder (const cmt_string& tag,
8072                        const cmt_string& package,
8073                        const cmt_string& version,
8074                        const cmt_string& prefix,
8075                        CmtDirStyle style)
8076  {
8077    fs = CmtSystem::file_separator ();
8078    buffer = "";
8079    pwd = CmtSystem::pwd ();
8080    CmtSystem::split (pwd, fs, vb);
8081    current_use = &(Use::current ());
8082    current_tag = tag;
8083    current_package = package;
8084    current_version = version;
8085    current_prefix = prefix;
8086    current_style = style;
8087  }
8088
8089  void apply ()
8090  {
8091    SyntaxParser::parse_requirements_line (buffer, current_use);
8092    buffer = "";
8093  }
8094
8095  /**
8096   *   tag
8097   */
8098  void fill_for_tag ()
8099  {
8100    static bool tag_debug = CmtSystem::testenv ("TAGDEBUG");
8101
8102    if (!Symbol::is_selected ("tag"))
8103      {
8104        if (tag_debug) cerr << "set_standard_macro2.1> current_tag=" << current_tag << endl;
8105
8106        if (current_tag == "")
8107          {
8108            buffer = "macro tag \"$(CMTCONFIG)\"";
8109          }
8110        else
8111          {
8112            buffer = "macro tag \"";
8113            buffer += current_tag;
8114            buffer += "\"";
8115          }
8116       
8117        if (tag_debug) cerr << " define tag: " << buffer << endl;
8118       
8119        apply ();
8120      }
8121  }
8122
8123  /**
8124   *   PACKAGE_ROOT
8125   */
8126  void fill_for_package (const cmt_string& current_dir)
8127  {
8128    buffer = "macro package \"";
8129    buffer += current_package;
8130    buffer += "\"";
8131    apply ();
8132
8133    buffer = "macro version \"";
8134    buffer += current_version;
8135    buffer += "\"";
8136    apply ();
8137
8138    if (!Symbol::is_selected ("PACKAGE_ROOT"))
8139      {
8140        buffer = "macro PACKAGE_ROOT \"$(";
8141        buffer += current_prefix;
8142        buffer += "ROOT";
8143        buffer += ")\"";
8144
8145        apply ();
8146      }
8147  }
8148
8149  /**
8150   *   srcdir
8151   *   src       =$(srcdir)/
8152   *   inc
8153   *   mgrdir
8154   *   mgr       =../$(mgrdir)/
8155   *   bin
8156   *   javabin
8157   *   doc
8158   *   version
8159   *   package
8160   *
8161   *   <package>_project
8162   *   <package>_cmtpath
8163   *   <package>_offset
8164   *   package_cmtpath
8165   *   package_offset
8166   *   project
8167   *
8168   */
8169  void fill_for_branches ()
8170  {
8171    /**
8172     *    Basic macros  (src, mgr, ...)
8173     */
8174   
8175    if (current_style == none_style)
8176      {
8177        if (!Symbol::is_selected ("srcdir"))
8178          {
8179            buffer = "macro srcdir \".";
8180            buffer += "\"";
8181            apply ();
8182          }
8183
8184        if (!Symbol::is_selected ("src"))
8185          {
8186            buffer = "macro src \".";
8187            buffer += fs;
8188            buffer += "\"";
8189            apply ();
8190          }
8191
8192        if (!Symbol::is_selected ("inc"))
8193          {
8194            buffer = "macro inc \".";
8195            buffer += fs;
8196            buffer += "\"";
8197            apply ();
8198          }
8199
8200        if (!Symbol::is_selected ("doc"))
8201          {
8202            buffer = "macro doc \".";
8203            buffer += fs;
8204            buffer += "\"";
8205            apply ();
8206          }
8207
8208        if (!Symbol::is_selected ("bin"))
8209          {
8210            buffer = "macro bin \".";
8211            buffer += fs;
8212            buffer += "\"";
8213            apply ();
8214          }
8215
8216        if (!Symbol::is_selected ("BIN"))
8217          {
8218            /*
8219            buffer = "macro BIN \"";
8220            buffer += pwd;
8221            buffer += fs;
8222            buffer += "\"";
8223            apply ();
8224            */
8225            cmt_string temp = "$(bin)";
8226            Symbol::expand (temp);
8227            int len (temp.size());
8228            cmt_string atemp;
8229            if (CmtSystem::absolute_path (temp))
8230              {
8231                CmtSystem::compress_path (temp, atemp);
8232              }
8233            else
8234              {
8235                atemp = pwd + fs + temp;
8236                CmtSystem::compress_path (atemp);
8237              }
8238            buffer = "macro BIN \"";
8239            buffer += atemp;
8240            buffer += "\"";
8241            apply ();
8242            if (0 != len && fs != temp[len - 1])
8243              {
8244                buffer = "macro_append bin \"";
8245                buffer += fs;
8246                buffer += "\"";
8247                apply ();
8248
8249                buffer = "macro_append BIN \"";
8250                buffer += fs;
8251                buffer += "\"";
8252                apply ();
8253              }
8254          }
8255
8256        if (!Symbol::is_selected ("javabin"))
8257          {
8258            buffer = "macro javabin \".";
8259            buffer += fs;
8260            buffer += "\"";
8261            apply ();
8262          }
8263
8264        if (!Symbol::is_selected ("mgr"))
8265          {
8266            buffer = "macro mgr \".";
8267            buffer += fs;
8268            buffer += "\"";
8269            apply ();
8270          }
8271      }
8272    else
8273      {
8274        if (!Symbol::is_selected ("srcdir"))
8275          {
8276            buffer = "macro srcdir \"..";
8277            buffer += fs;
8278            buffer += "src";
8279            buffer += "\"";
8280            apply ();
8281          }
8282       
8283        if (!Symbol::is_selected ("src"))
8284          {
8285            buffer = "macro src \"..";
8286            buffer += fs;
8287            buffer += "src";
8288            buffer += fs;
8289            buffer += "\"";
8290            apply ();
8291          }
8292       
8293        if (!Symbol::is_selected ("inc"))
8294          {
8295            buffer = "macro inc \"..";
8296            buffer += fs;
8297            buffer += "src";
8298            buffer += fs;
8299            buffer += "\"";
8300            apply ();
8301          }
8302       
8303        if (!Symbol::is_selected ("doc"))
8304          {
8305            buffer = "macro doc \"..";
8306            buffer += fs;
8307            buffer += "doc";
8308            buffer += fs;
8309            buffer += "\"";
8310            apply ();
8311          }
8312       
8313        if (!Symbol::is_selected ("bin"))
8314          {
8315            cmt_string package_tag = current_package;
8316            package_tag += "_tag";
8317
8318            buffer = "macro bin \"..";
8319            buffer += fs;
8320            buffer += "$(";
8321            buffer += package_tag;
8322            buffer += ")";
8323            buffer += fs;
8324            buffer += "\"";
8325            apply ();
8326          }
8327
8328        if (!Symbol::is_selected ("BIN"))
8329          {
8330            /*
8331            cmt_string pardir;
8332            CmtSystem::dirname (pwd, pardir);
8333            //            buffer = "macro BIN \"$(PACKAGE_ROOT)";
8334            buffer = "macro BIN \"";
8335            buffer += pardir;
8336            buffer += fs;
8337            buffer += "$(";
8338            buffer += package_tag;
8339            buffer += ")";
8340            buffer += fs;
8341            buffer += "\"";
8342            apply ();
8343            */
8344            cmt_string temp = "$(bin)";
8345            Symbol::expand (temp);
8346            int len (temp.size());
8347            cmt_string atemp;
8348            if (CmtSystem::absolute_path (temp))
8349              {
8350                CmtSystem::compress_path (temp, atemp);
8351              }
8352            else
8353              {
8354                atemp = pwd + fs + temp;
8355                CmtSystem::compress_path (atemp);
8356              }
8357            buffer = "macro BIN \"";
8358            buffer += atemp;
8359            buffer += "\"";
8360            apply ();
8361            if (0 != len && fs != temp[len - 1])
8362              {
8363                buffer = "macro_append bin \"";
8364                buffer += fs;
8365                buffer += "\"";
8366                apply ();
8367
8368                buffer = "macro_append BIN \"";
8369                buffer += fs;
8370                buffer += "\"";
8371                apply ();
8372              }
8373          }
8374
8375        if (!Symbol::is_selected ("javabin"))
8376          {
8377            buffer = "macro javabin \"..";
8378            buffer += fs;
8379            buffer += "classes";
8380            buffer += fs;
8381            buffer += "\"";
8382            apply ();
8383          }
8384       
8385        if (current_style == mgr_style)
8386          {
8387            buffer = "macro mgrdir \"mgr\"";
8388            apply ();
8389
8390            buffer = "macro mgr \"..";
8391            buffer += fs;
8392            buffer += "mgr";
8393            buffer += fs;
8394            buffer += "\"";
8395            apply ();
8396          }
8397        else
8398          {
8399            buffer = "macro mgrdir \"cmt\"";
8400            apply ();
8401
8402            buffer = "macro mgr \"..";
8403            buffer += fs;
8404            buffer += "cmt";
8405            buffer += fs;
8406            buffer += "\"";
8407            apply ();
8408          }
8409
8410        Cmt::configure_current_cmtpath ();
8411      }
8412  }
8413
8414  /**
8415   *   project
8416   */
8417  void fill_for_project ()
8418  {
8419    if (current_style == none_style) return;
8420
8421    cmt_string project_name;
8422    Project* project = Project::get_current ();
8423    if (project != 0)
8424      {
8425        project_name = project->get_name ();
8426      }
8427
8428    buffer = "macro project \"";
8429    buffer += project_name;
8430    buffer += "\"";
8431    apply ();
8432  }
8433
8434  /**
8435   *   use_requirements
8436   */
8437  void fill_for_use_requirements ()
8438  {
8439    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8440
8441    if (Uses.size () == 0) return;
8442
8443    if (!Symbol::is_selected ("use_requirements"))
8444      {
8445        buffer  = "macro use_requirements \"";
8446        buffer += "requirements ";
8447       
8448        for (int number = 0; number < Uses.size (); number++)
8449          {
8450            Use* use = Uses[number];
8451           
8452            if (use->discarded) continue;
8453            if (use->m_hidden) continue;
8454           
8455            if (use->located ())
8456              {
8457                buffer += "$(";
8458                buffer += use->get_package_name ();
8459                buffer += "_root)";
8460//                 buffer += use->prefix;
8461//                 buffer += "ROOT)";
8462               
8463                if (use->style == cmt_style)
8464                  {
8465                    buffer += fs;
8466                    buffer += "cmt";
8467                  }
8468                else if (use->style == mgr_style)
8469                  {
8470                    buffer += fs;
8471                    buffer += "mgr";
8472                  }
8473                //                else buffer += "cmt";
8474               
8475                buffer += fs;
8476                buffer += "requirements ";
8477              }
8478          }
8479       
8480        buffer += "\"";
8481       
8482        apply ();
8483      }
8484  }
8485
8486  /**
8487   *   use_includes
8488   */
8489  void fill_for_use_includes ()
8490  {
8491    Include::parse_all (&(Use::current()));
8492
8493    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8494
8495    if (Uses.size () == 0) return;
8496
8497    if (!Symbol::is_selected ("use_includes"))
8498      {
8499        buffer = "macro_append use_includes \' ";
8500       
8501        for (int number = 0; number < Uses.size (); number++)
8502          {
8503            Use* use = Uses[number];
8504           
8505            if (use->discarded) continue;
8506            if (use->m_hidden) continue;
8507
8508            Package* p = use->get_package ();
8509            if (p->is_cmt ()) continue;
8510
8511            if (Cmt::get_debug ())
8512              {
8513                cout << "fill use_includes for " << use->get_package_name () 
8514                     << " discarded=" << use->discarded
8515                     << " auto_imports=" << use->auto_imports << endl;
8516              }
8517           
8518            if (use->auto_imports == Off) continue;
8519           
8520            Include::parse_all (use);
8521
8522            use->fill_includes_macro (buffer);
8523          }
8524       
8525        buffer += "\'";
8526       
8527        apply ();
8528      }
8529  }
8530
8531  /**
8532   *   use_fincludes
8533   */
8534  void fill_for_use_fincludes ()
8535  {
8536    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8537
8538    if (Uses.size () == 0) return;
8539
8540    if (!Symbol::is_selected ("use_fincludes"))
8541      {
8542        buffer = "macro_append use_fincludes \" $(use_includes)\"";
8543        apply ();
8544      }
8545  }
8546
8547  /**
8548   *   use_stamps
8549   */
8550  void fill_for_use_stamps ()
8551  {
8552    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8553
8554    if (Uses.size () == 0) return;
8555
8556    if (!Symbol::is_selected ("use_stamps"))
8557      {
8558        /*
8559        buffer = "macro use_stamps \"";
8560        (Use::current()).fill_macro (buffer, "stamps");
8561       
8562        for (int number = 0; number < Uses.size (); number++)
8563          {
8564            Use* use = Uses[number];
8565           
8566            if (use->discarded) continue;
8567            if (use->m_hidden) continue;
8568
8569            Package* p = use->get_package ();
8570            if (p->is_cmt ()) continue;
8571           
8572            use->fill_macro (buffer, "stamps");
8573          }
8574       
8575        buffer += "\"";
8576        */
8577        Use::fill_macro_all (buffer, "stamps");
8578        apply ();
8579      }
8580  }
8581
8582  /**
8583   *   use_cflags
8584   */
8585  void fill_for_use_cflags ()
8586  {
8587    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8588
8589    if (Uses.size () == 0) return;
8590
8591    if (!Symbol::is_selected ("use_cflags"))
8592      {
8593        Use::fill_macro_all (buffer, "cflags");
8594        apply ();
8595      }
8596  }
8597
8598  /**
8599   *   use_pp_cflags
8600   */
8601  void fill_for_use_pp_cflags ()
8602  {
8603    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8604
8605    if (Uses.size () == 0) return;
8606
8607    if (!Symbol::is_selected ("use_pp_cflags"))
8608      {
8609        Use::fill_macro_all (buffer, "pp_cflags");
8610        apply ();
8611      }
8612  }
8613
8614  /**
8615   *   use_cppflags
8616   */
8617  void fill_for_use_cppflags ()
8618  {
8619    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8620
8621    if (Uses.size () == 0) return;
8622
8623    if (!Symbol::is_selected ("use_cppflags"))
8624      {
8625        Use::fill_macro_all (buffer, "cppflags");
8626        apply ();
8627      }
8628  }
8629
8630  /**
8631   *   use_pp_cppflags
8632   */
8633  void fill_for_use_pp_cppflags ()
8634  {
8635    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8636
8637    if (Uses.size () == 0) return;
8638
8639    if (!Symbol::is_selected ("use_pp_cppflags"))
8640      {
8641        Use::fill_macro_all (buffer, "pp_cppflags");
8642        apply ();
8643      }
8644  }
8645
8646  /**
8647   *   use_fflags
8648   */
8649  void fill_for_use_fflags ()
8650  {
8651    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8652
8653    if (Uses.size () == 0) return;
8654
8655    if (!Symbol::is_selected ("use_fflags"))
8656      {
8657        Use::fill_macro_all (buffer, "fflags");
8658        apply ();
8659      }
8660  }
8661
8662  /**
8663   *   use_pp_fflags
8664   */
8665  void fill_for_use_pp_fflags ()
8666  {
8667    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8668
8669    if (Uses.size () == 0) return;
8670
8671    if (!Symbol::is_selected ("use_pp_fflags"))
8672      {
8673        Use::fill_macro_all (buffer, "pp_fflags");
8674        apply ();
8675      }
8676  }
8677
8678  /**
8679   *   use_linkopts
8680   */
8681  void fill_for_use_linkopts ()
8682  {
8683    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8684
8685    if (Uses.size () == 0) return;
8686
8687    if (!Symbol::is_selected ("use_linkopts"))
8688      {
8689        Use::fill_macro_all (buffer, "linkopts");
8690        apply ();
8691      }
8692  }
8693
8694  /**
8695   *   use_libraries
8696   */
8697  void fill_for_use_libraries ()
8698  {
8699    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8700
8701    if (Uses.size () == 0) return;
8702
8703    if (!Symbol::is_selected ("use_libraries"))
8704      {
8705        buffer  = "macro use_libraries \"";
8706
8707        for (int number = 0; number < Uses.size (); number++)
8708          {
8709            Use* use = Uses[number];
8710           
8711            if (use->discarded) continue;
8712            if (use->m_hidden) continue;
8713           
8714            Package* p = use->get_package ();
8715            if (p->is_cmt ()) continue;
8716
8717            use->fill_macro (buffer, "libraries");
8718          }
8719       
8720        buffer += "\"";
8721
8722        apply ();
8723      }
8724  }
8725
8726  /**
8727   *   includes
8728   */
8729  void fill_for_includes ()
8730  {
8731    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8732
8733    if (Uses.size () == 0) return;
8734
8735    if (!Symbol::is_selected ("includes"))
8736      {
8737        buffer = "macro_append includes \' ";
8738
8739        Use& use = Use::current();
8740
8741        if (use.include_path == "")
8742          {
8743            buffer += "$(ppcmd)\"$(srcdir)\" ";
8744          }
8745        else if (use.include_path != "none")
8746          {
8747            buffer += "$(ppcmd)\"";
8748            buffer += use.include_path;
8749            buffer += "\" ";
8750          }
8751
8752        for (int include_number = 0;
8753             include_number < use.includes.size ();
8754             include_number++)
8755          {
8756            Include& incl = use.includes[include_number];
8757
8758            if (incl.name == "") continue;
8759
8760            buffer += "$(ppcmd)\"";
8761            buffer += incl.name;
8762            buffer += "\" ";
8763          }
8764       
8765        buffer += "$(use_includes)\'";
8766       
8767        apply ();
8768      }
8769  }
8770
8771  /**
8772   *   fincludes
8773   */
8774  void fill_for_fincludes ()
8775  {
8776    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8777
8778    if (Uses.size () == 0) return;
8779
8780    if (!Symbol::is_selected ("fincludes"))
8781      {
8782        buffer = "macro_append fincludes \" $(includes)\"";
8783        apply ();
8784      }
8785  }
8786
8787  /**
8788   *  Macros specific to constituents.
8789   *  This includes the compiler flags
8790   *  and fills in these macros from uses packages. This takes care
8791   *  of -no_auto_imports and -import= directives
8792   *
8793   *    <constituent>_use_linkopts
8794   *    <prefix>_<constituent>_cflags
8795   *    <prefix>_<constituent>_pp_cflags
8796   *    <prefix>_<constituent>_cppflags
8797   *    <prefix>_<constituent>_pp_cppflags
8798   *    <prefix>_<constituent>_fflags
8799   *    <prefix>_<constituent>_pp_fflags
8800   *    <constituent>linkopts
8801   *    <constituent>_GUID
8802   *    <constituent>_stamps
8803   *
8804   */
8805  void fill_for_all_constituents ()
8806  {
8807    /// First, finish the parsing of constituent parameters.
8808    if (Cmt::get_debug ())
8809      {
8810        cout << "fill_for_all_constituents>" << endl;
8811      }
8812
8813    Constituent::parse_all ();
8814
8815    Use::UsePtrVector& Uses = Use::get_ordered_uses ();
8816   
8817    const Constituent::ConstituentVector& constituents =
8818      Constituent::constituents ();
8819
8820 
8821    // Prepare the auto_imports states in a vector
8822    cmt_vector<bool> base_auto_imports_states;
8823
8824    base_auto_imports_states.resize (Uses.size ());
8825
8826    int number;
8827   
8828    for (number = 0; number < Uses.size (); number++)
8829      {
8830        Use* use = Uses[number];
8831        base_auto_imports_states[number] = (use->auto_imports != Off);
8832      }
8833   
8834    // Now scan all constituents
8835       
8836    for (number = 0; number < constituents.size (); number++)
8837      {
8838        const Constituent& constituent = constituents[number];
8839       
8840        //==== GLAST addition for vs.net ==========
8841        buffer = "macro ";
8842        buffer += constituent.name;
8843        buffer += "_GUID \"{88BF15AB-5A2D-4bea-B64F-02752C2A1F4F}\" ";
8844        apply ();
8845       
8846        if (Cmt::get_debug ())
8847          {
8848            cout << "Checking " << constituent.name  << " against imports requests" << endl;
8849          }
8850        Use::UsePtrVector imports;
8851        int i;
8852       
8853        /**
8854         *  Problem for imports in constituents.
8855         *
8856         *     1) use_xxx has holes due to the corresponding
8857         *         -no_auto_imports options attached to some
8858         *        use statements (including the transitive ones)
8859         *
8860         *     2) the -import=yyy options provided to a given constituent
8861         *        should restore the appropriate holes as well as
8862         *        all transitive ones.
8863         *
8864         *     3) for use_linkopts, missing pieces must be filled at
8865         *        the right position. (for others, order is not relevant
8866         *        while transitive access is required for all)
8867         *
8868         */
8869       
8870        if (constituent.type == Document)
8871          { 
8872            continue;
8873          }
8874       
8875        if (constituent.imports.size () == 0) 
8876          {
8877            buffer = "macro_append ";
8878            buffer += constituent.name;
8879            buffer += "_use_linkopts ";
8880            buffer += " \" ";
8881           
8882            current_use->fill_macro (buffer, "linkopts");
8883           
8884            for (i = 0; i < Uses.size (); i++)
8885              {
8886                if (base_auto_imports_states[i])
8887                  {
8888                    Use* u = Uses[i];
8889                   
8890                    if (u->discarded) continue;
8891                    if (u->m_hidden) continue;
8892                   
8893                    Package* p = u->get_package ();
8894                    if (p->is_cmt ()) continue;
8895                   
8896                    u->fill_macro (buffer, "linkopts");
8897                  }
8898              }
8899            buffer += "\"";
8900            apply ();
8901           
8902            /*
8903              buffer = "macro_append ";
8904              buffer += constituent.name;
8905              buffer += "_use_linkopts ";
8906             
8907              buffer += " \" $(use_linkopts)\"";
8908              apply ();
8909            */
8910           
8911            continue;
8912          }
8913       
8914        // From here on, the constituent HAS import options.
8915       
8916        /**
8917         * Create a private copy of the state vector. This private copy
8918         * will be updated according to -import=xxx modifiers for the
8919         * current constituent.
8920         */
8921        cmt_vector<bool> auto_imports_states (base_auto_imports_states);
8922       
8923        for (i = 0; i < constituent.imports.size (); i++)
8924          {
8925            const cmt_string& import = constituent.imports[i];
8926           
8927            //
8928            // Resolve the imported uses
8929            //
8930           
8931            int use_index = Use::find_index (import, "", "");
8932           
8933            if (use_index >= 0)
8934              {
8935                Use* u = Uses[use_index];
8936               
8937                if (u->discarded) continue;
8938                if (u->m_hidden) continue;
8939               
8940                if (Cmt::get_debug ())
8941                  {
8942                    cout << constituent.name  << " needs imports " << import << " "
8943                         << use_index << " " 
8944                         << u->get_package()->get_name() << " "
8945                         << " u->auto_imports=" << u->auto_imports
8946                         << " Off=" << Off
8947                         << " On=" << On
8948                         << endl;
8949                  }
8950               
8951                Package* p = u->get_package ();
8952                if (p->is_cmt ()) continue;
8953               
8954                if (u->auto_imports != Off) continue;
8955               
8956                Use::set_auto_imports_state (use_index, auto_imports_states);
8957              }
8958            else
8959              {
8960                if (Me.m_action == action_build_constituents_makefile ||
8961                    Me.m_action == action_build_constituents_config)
8962                  CmtMessage::warning ("package " + import +
8963                                       " cannot be imported (requested for " +
8964                                       constituent.name +")");
8965              }
8966          }
8967       
8968        if (Cmt::get_debug ())
8969          {
8970            cout << constituent.name  << " has imports " << endl;
8971          }
8972       
8973       
8974        /**
8975         *   Find all newly exposed packages and precompute this list inside
8976         *   a vector.
8977         */
8978        for (i = 0; i < base_auto_imports_states.size (); i++)
8979          {
8980            //      if (base_auto_imports_states[i]== On && auto_imports_states[i] == Off) continue;
8981            if (base_auto_imports_states[i] == On || auto_imports_states[i] == Off) continue;
8982            /**
8983             *  Consider the package (i) for import only if it is:
8984             *   o not imported yet (i.e., base_auto_imports_states[i] != On)
8985             *   o requested for import according to -import=xxx modifiers for
8986             *     the current constituent (i.e., auto_imports_states[i] != Off)
8987             */
8988            Use* u = Uses[i];
8989           
8990            if (u->discarded) continue;
8991            if (u->m_hidden)  continue;
8992           
8993            Package* p = u->get_package ();
8994            if (p->is_cmt ()) continue;
8995           
8996            if (Cmt::get_debug ())
8997              {
8998                cout << constituent.name  << " has import " << p->get_name () << endl;
8999              }
9000           
9001            imports.push_back (u);
9002          }
9003       
9004        /**
9005         *  Only for linkopts we take care of the order. This means
9006         *  that ${CONSTITUENT}_use_linkopts should be used in place of use_linkopts.
9007         *
9008         *  (see the application fragments)
9009         *  that ${CONSTITUENT}_use_linkopts will be used in place of use_linkopts.
9010         */
9011        buffer = "macro_append ";
9012        buffer += constituent.name;
9013        buffer += "_use_linkopts ";
9014        buffer += " \" ";
9015       
9016        current_use->fill_macro (buffer, "linkopts");
9017       
9018        for (i = 0; i < Uses.size (); i++)
9019          {
9020            if (auto_imports_states[i])
9021              {
9022                Use* u = Uses[i];
9023               
9024                if (u->discarded) continue;
9025                if (u->m_hidden) continue;
9026               
9027                Package* p = u->get_package ();
9028                if (p->is_cmt ()) continue;
9029               
9030                u->fill_macro (buffer, "linkopts");
9031              }
9032          }
9033        buffer += "\"";
9034        apply ();
9035       
9036        if (imports.size () == 0) continue;
9037       
9038        cmt_string prefix;
9039       
9040        //
9041        // Documents are not considered
9042        //
9043        switch (constituent.type)
9044          {
9045          case Application:
9046            prefix = "app_";
9047            break;
9048          case Library:
9049            prefix = "lib_";
9050            break;
9051          }
9052       
9053        buffer = "macro_append ";
9054        buffer += prefix;
9055        buffer += constituent.name;
9056        buffer += "_cflags ";
9057        buffer += " \' ";
9058        for (i = 0; i < imports.size (); i++)
9059          {
9060            Use* u = imports[i];
9061           
9062            u->fill_includes_macro (buffer);
9063            u->fill_macro (buffer, "cflags");
9064          }
9065        buffer += "\'";
9066        apply ();
9067       
9068        buffer = "macro_append ";
9069        buffer += prefix;
9070        buffer += constituent.name;
9071        buffer += "_pp_cflags ";
9072        buffer += " \" ";
9073        for (i = 0; i < imports.size (); i++)
9074          {
9075            Use* u = imports[i];
9076           
9077            u->fill_macro (buffer, "pp_cflags");
9078          }
9079        buffer += "\"";
9080        apply ();
9081       
9082        buffer = "macro_append ";
9083        buffer += prefix;
9084        buffer += constituent.name;
9085        buffer += "_cppflags ";
9086        buffer += " \' ";
9087        for (i = 0; i < imports.size (); i++)
9088          {
9089            Use* u = imports[i];
9090           
9091            u->fill_includes_macro (buffer);
9092            u->fill_macro (buffer, "cppflags");
9093          }
9094        buffer += "\'";
9095        apply ();
9096       
9097        buffer = "macro_append ";
9098        buffer += prefix;
9099        buffer += constituent.name;
9100        buffer += "_pp_cppflags ";
9101        buffer += " \" ";
9102        for (i = 0; i < imports.size (); i++)
9103          {
9104            Use* u = imports[i];
9105           
9106            u->fill_macro (buffer, "pp_cppflags");
9107          }
9108        buffer += "\"";
9109        apply ();
9110       
9111        buffer = "macro_append ";
9112        buffer += prefix;
9113        buffer += constituent.name;
9114        buffer += "_fflags ";
9115        buffer += " \' ";
9116        for (i = 0; i < imports.size (); i++)
9117          {
9118            Use* u = imports[i];
9119           
9120            u->fill_includes_macro (buffer);
9121            u->fill_macro (buffer, "fflags");
9122          }
9123        buffer += "\'";
9124        apply ();
9125       
9126        buffer = "macro_append ";
9127        buffer += prefix;
9128        buffer += constituent.name;
9129        buffer += "_pp_fflags ";
9130        buffer += " \" ";
9131        for (i = 0; i < imports.size (); i++)
9132          {
9133            Use* u = imports[i];
9134           
9135            u->fill_macro (buffer, "pp_fflags");
9136          }
9137        buffer += "\"";
9138        apply ();
9139
9140        buffer = "macro_append ";
9141        buffer += constituent.name;
9142        buffer += "_stamps ";
9143        buffer += " \" ";
9144        for (i = 0; i < imports.size (); i++)
9145          {
9146            Use* u = imports[i];
9147           
9148            u->fill_macro (buffer, "stamps");
9149          }
9150        buffer += "\"";
9151        apply ();
9152
9153        /**
9154         *  Setting ${CONSTITUENT}linkopts is a temporary solution
9155         *  until the backward compatibility solution for a proper
9156         *  replacement of use_linkopts by ${CONSTITUENT}_use_linkopts
9157         *  is acheived.
9158         *
9159         */
9160        /**
9161        buffer = "macro_append ";
9162        buffer += constituent.name;
9163        buffer += "linkopts ";
9164        buffer += " \" ";
9165        for (i = 0; i < imports.size (); i++)
9166          {
9167            Use* u = imports[i];
9168           
9169            u->fill_macro (buffer, "linkopts");
9170          }
9171        buffer += "\"";
9172        apply ();
9173        */
9174      }
9175  }
9176 
9177  /**
9178   *   Macros implied or required to manage constituents.
9179   */
9180  void fill_for_constituent_macros ()
9181  {
9182    int number;
9183    cmt_string temp;
9184
9185    const Constituent::ConstituentVector& constituents = Constituent::constituents ();
9186 
9187    if (!Symbol::is_selected ("constituents"))
9188      {
9189        temp = "macro_append constituents \" ";
9190       
9191        for (number = 0; number < constituents.size (); number++)
9192          {
9193            const Constituent& constituent = constituents[number];
9194           
9195            if (constituent.group == 0)
9196              {
9197                temp += constituent.name;
9198                temp += " ";
9199              }
9200          }
9201       
9202        temp += "\"";
9203       
9204        SyntaxParser::parse_requirements_line (temp, current_use);
9205      }
9206   
9207    SyntaxParser::parse_requirements_line ("macro_append all_constituents \" $(constituents)\"", 
9208                                           current_use);
9209   
9210    if (!Symbol::is_selected ("constituentsclean"))
9211      {
9212        temp = "macro_append constituentsclean \" ";
9213       
9214        for (number = constituents.size () - 1; number >= 0 ; number--)
9215          {
9216            const Constituent& constituent = constituents[number];
9217           
9218            if (constituent.group == 0)
9219              {
9220                temp += constituent.name;
9221                temp += "clean ";
9222              }
9223          }
9224       
9225        temp += "\"";
9226       
9227        SyntaxParser::parse_requirements_line (temp, current_use);
9228      }
9229   
9230    SyntaxParser::parse_requirements_line ("macro_append all_constituentsclean \" $(constituentsclean)\"", 
9231                                           current_use);
9232   
9233    const Group::GroupVector& groups = Group::groups ();
9234   
9235    for (number = 0; number < groups.size (); number++)
9236      {
9237        const Group& group = groups[number];
9238       
9239        temp = "macro_append ";
9240        temp += group.name ();
9241        temp += "_constituents \" ";
9242       
9243        int i;
9244       
9245        for (i = 0; i < constituents.size (); i++)
9246          {
9247            const Constituent& constituent = constituents[i];
9248           
9249            if ((constituent.group != 0) && 
9250                (group.name () == constituent.group->name ()))
9251              {
9252                temp += constituent.name;
9253                temp += " ";
9254              }
9255          }
9256       
9257        temp += "\"";
9258       
9259        SyntaxParser::parse_requirements_line (temp, current_use);
9260       
9261        temp = "macro_append ";
9262        temp += group.name ();
9263        temp += "_constituentsclean \" ";
9264       
9265        for (i = constituents.size () - 1; i >= 0 ; i--)
9266          {
9267            const Constituent& constituent = constituents[i];
9268           
9269            if ((constituent.group != 0) && 
9270                (group.name () == constituent.group->name ()))
9271              {
9272                temp += constituent.name;
9273                temp += "clean ";
9274              }
9275          }
9276       
9277        temp += "\"";
9278       
9279        SyntaxParser::parse_requirements_line (temp, current_use);
9280      }
9281  }
9282
9283  /**
9284   *  Definitions for installation area mechanisms. Apply all cmtpath patterns
9285   */
9286  void fill_for_install_area ()
9287  {
9288    CmtPathPattern::apply_all ();
9289
9290    const Use& current_use = Use::current ();
9291
9292    if (current_use.get_strategy ("InstallArea"))
9293      {
9294        CmtInstallAreaMgr& ia_mgr = CmtInstallAreaMgr::instance ();
9295       
9296        ia_mgr.setup ();
9297      }
9298  }
9299
9300  /**
9301   * Macros to be defined once current_package is known
9302   * and even before reading its requirements file.
9303   */
9304  void fill_for_current_package (const cmt_string& current_dir)
9305  {
9306    fill_for_tag ();
9307    fill_for_package (current_dir);
9308  }
9309
9310private:
9311  cmt_string fs;
9312  cmt_string buffer;
9313  CmtSystem::cmt_string_vector vb;
9314  cmt_string pwd;
9315  Use* current_use;
9316  cmt_string current_tag; 
9317  cmt_string current_package; 
9318  cmt_string current_version; 
9319  cmt_string current_prefix; 
9320  CmtDirStyle current_style;
9321};
9322
9323//----------------------------------------------------------
9324void Cmt::set_current_access (AccessMode mode)
9325{
9326  Me.m_current_access = mode;
9327}
9328
9329//----------------------------------------------------------
9330void Cmt::set_recursive (bool mode)
9331{
9332  Me.m_recursive = mode;
9333}
9334
9335//----------------------------------------------------------
9336void Cmt::set_scope_filtering_mode (CmtScopeFilteringMode mode)
9337{
9338  Me.m_scope_filtering_mode = mode;
9339}
9340
9341//----------------------------------------------------------
9342void Cmt::set_standard_macros ()
9343{
9344  if (Me.m_standard_macros_done) return;
9345
9346  Me.m_standard_macros_done = true;
9347
9348  //  Use::UsePtrVector& Uses = Use::get_ordered_uses ();
9349  Use& current_use = Use::current ();
9350
9351  cmt_string fs = CmtSystem::file_separator ();
9352
9353  cmt_string pwd = CmtSystem::pwd ();
9354
9355  /**
9356   * This is a check for package names
9357   */
9358  if (CmtMessage::active (Verbose))
9359    {
9360      Use::UsePtrVector uses (Use::get_ordered_uses ());
9361      uses.push_back (&current_use);
9362      for (int i = uses.size () - 1; i >= 0; i--)
9363        {
9364          Use* use = uses[i];
9365          if (use->discarded) continue;
9366          if (use->m_hidden) continue;
9367          if (!use->located ()) continue;
9368          for (int j = i - 1; j >= 0; j--)
9369            {
9370              Use* use2 = uses[j];
9371              if (use2->discarded) continue;
9372              if (use2->m_hidden) continue;
9373              if (!use2->located ()) continue;
9374              if (use->prefix == use2->prefix)
9375                {
9376                  CmtMessage::warning (use->prefix + "ROOT, "
9377                                       + use->prefix + "VERSION ill-defined: "
9378                                       + use->get_package_name () + " and "
9379                                       + use2->get_package_name ()
9380                                       + " package name conflict");
9381                }
9382            }
9383        }
9384    }
9385  /**
9386   * This is already done in
9387   void Cmt::configure_current_package ()
9388
9389  if (CmtSystem::test_file ("../cmt/requirements")) Me.m_current_style = cmt_style;
9390  else if (CmtSystem::test_file ("../mgr/requirements")) Me.m_current_style = mgr_style;
9391  else Me.m_current_style = none_style;
9392
9393  {
9394    cmt_string v;
9395    CmtSystem::dirname (pwd, v);
9396    CmtSystem::basename (v, v);
9397    if (!CmtSystem::is_version_directory (v))
9398      {
9399        Me.m_current_style = no_version_style;
9400      }
9401  }
9402  */
9403
9404  // Prepare computation of the best form for relative path from current directory
9405  // to package directories.
9406  CmtSystem::cmt_string_vector vb;
9407  CmtSystem::split (pwd, fs, vb);
9408
9409
9410  /**
9411   *    TAG management
9412   */
9413
9414  bool tag_debug = CmtSystem::testenv ("TAGDEBUG");
9415
9416  if (tag_debug) cerr << "set_standard_macro0> current_tag=" << Me.m_current_tag << endl;
9417
9418  if (Me.m_current_tag != "")
9419    {
9420      // this is when some -tag= argument was used.
9421      if (tag_debug) cerr << "set_standard_macro0.1> current_tag=" << Me.m_current_tag << endl;
9422    }
9423  else if (Symbol::is_selected ("CMTCONFIG"))
9424    {
9425      // This is when CMTCONFIG has been set from some requirements file
9426      Symbol* macro = Symbol::find ("CMTCONFIG");
9427      if (macro != 0)
9428        {
9429          Me.m_current_tag = macro->build_macro_value ();
9430          if (tag_debug) cerr << "set_standard_macro1> current_tag=" << Me.m_current_tag << endl;
9431        }
9432    }
9433  else
9434    {
9435      // this is when no -tag= argument was used.
9436      if (tag_debug) cerr << "set_standard_macro(before2)> current_tag=" << Me.m_current_tag << endl;
9437      if (current_use.get_package_name () == "CMT")
9438        {
9439          Me.m_current_tag = CmtSystem::getenv ("CMTBIN");
9440        }
9441      else
9442        {
9443          Me.m_current_tag = CmtSystem::getenv ("CMTCONFIG");
9444        }
9445
9446      if (tag_debug) cerr << "set_standard_macro2> current_tag=" << Me.m_current_tag << endl;
9447    }
9448
9449  if (Me.m_debug)
9450    {
9451      cout << "set_standard_macro3>" << endl;
9452    }
9453
9454  StandardMacroBuilder builder (Me.m_current_tag,
9455                                Me.m_current_package,
9456                                Me.m_current_version,
9457                                Me.m_current_prefix,
9458                                Me.m_current_style);
9459
9460
9461  builder.fill_for_current_package (Me.m_current_dir);
9462
9463  builder.fill_for_branches ();
9464  builder.fill_for_project ();
9465
9466  builder.fill_for_install_area ();
9467
9468  builder.fill_for_use_requirements ();
9469  builder.fill_for_use_includes ();
9470  builder.fill_for_use_fincludes ();
9471  builder.fill_for_use_stamps ();
9472  builder.fill_for_use_cflags ();
9473  builder.fill_for_use_pp_cflags ();
9474  builder.fill_for_use_cppflags ();
9475  builder.fill_for_use_pp_cppflags ();
9476  builder.fill_for_use_fflags ();
9477  builder.fill_for_use_pp_fflags ();
9478  builder.fill_for_use_linkopts ();
9479  builder.fill_for_use_libraries ();
9480  builder.fill_for_includes ();
9481  builder.fill_for_fincludes ();
9482  builder.fill_for_all_constituents ();
9483  builder.fill_for_constituent_macros ();
9484}
9485
9486void Cmt::set_all_sets_done ()
9487{
9488  Me.m_all_sets_done = true;
9489}
9490
9491void Cmt::reset_all_sets_done ()
9492{
9493  Me.m_all_sets_done = false;
9494}
9495
9496//----------------------------------------------------------
9497void Cmt::use_cmt ()
9498{
9499  UseRef use;
9500  bool recursive_copy = Me.m_recursive;
9501  bool debug_copy = Me.m_debug;
9502
9503  if (Me.m_default_path.size () <= 0) return;
9504  if (Me.m_current_package == "CMT") return;
9505
9506  Me.m_recursive = true;
9507  //Me.m_debug = false;
9508  use = Use::add (Me.m_default_path, "CMT", Me.m_cmt_version, "", "", "", 0);
9509  Me.m_recursive = recursive_copy;
9510  Me.m_debug = debug_copy;
9511}
9512
9513//----------------------------------------------------------
9514void Cmt::use_home_requirements ()
9515{
9516  use_special_requirements (Me.m_cmt_home, 
9517                            CmtSystem::get_home_package (), 
9518                            "requirements");
9519}
9520
9521//----------------------------------------------------------
9522void Cmt::use_user_context_requirements ()
9523{
9524  use_special_requirements (Me.m_cmt_user_context, 
9525                            CmtSystem::get_user_context_package (), 
9526                            "requirements");
9527}
9528
9529//----------------------------------------------------------
9530void Cmt::use_special_requirements (const cmt_string& path, 
9531                                    const cmt_string& name, 
9532                                    const cmt_string& file_name)
9533{
9534  if (path == "") 
9535    {
9536      return;
9537    }
9538
9539  UseRef use;
9540  bool recursive_copy = Me.m_recursive;
9541
9542  if (Me.m_default_path.size () <= 0) return;
9543  if (Me.m_current_package == "CMT") return;
9544
9545  Me.m_recursive = true;
9546
9547  use = Use::add (path, name, "v0", "", "", "", 0);
9548
9549  cmt_string f = path;
9550  f += CmtSystem::file_separator ();
9551  f += file_name;
9552  if (CmtSystem::test_file (f))
9553    {
9554      use->m_located = true;
9555    }
9556  SyntaxParser::parse_requirements (f, use);
9557
9558  Me.m_recursive = recursive_copy;
9559}
9560
9561//-------------------------------------------------
9562void Cmt::vector_to_string (const CmtSystem::cmt_string_vector& v,
9563                            const cmt_string& separator,
9564                            cmt_string& result)
9565{
9566  result.erase (0);
9567
9568  for (int i = 0; i < v.size (); i++)
9569    {
9570      const cmt_string& s = v[i];
9571      if (s == "") continue;
9572
9573      if (i > 0) result += separator;
9574      result += v[i];
9575    }
9576}
9577
9578//-------------------------------------------------
9579cmt_string Cmt::vector_to_string (const CmtSystem::cmt_string_vector& v)
9580{
9581  cmt_string result;
9582
9583  vector_to_string (v, " ", result);
9584
9585  return (result);
9586}
Note: See TracBrowser for help on using the repository browser.