source: CMT/v1r21/source/cmt_parser.cxx

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

See C.L. 428

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