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

Last change on this file since 649 was 649, checked in by rybkin, 11 years ago

See C.L. 508

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