1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include <precomp.h>
23 #include "cmd_run.hxx"
24 
25 
26 // NOT FULLY DEFINED SERVICES
27 #include <cosv/file.hxx>
28 #include <cosv/x.hxx>
29 #include <ary/ary.hxx>
30 #include <ary/cpp/c_gate.hxx>
31 #include <ary/idl/i_ce.hxx>
32 #include <ary/idl/i_gate.hxx>
33 #include <ary/idl/i_module.hxx>
34 #include <ary/idl/ip_ce.hxx>
35 #include <autodoc/filecoli.hxx>
36 #include <autodoc/parsing.hxx>
37 #include <autodoc/prs_code.hxx>
38 #include <autodoc/prs_docu.hxx>
39 #include <parser/unoidl.hxx>
40 #include <adc_cl.hxx>
41 #include "adc_cmd_parse.hxx"
42 #include "adc_cmds.hxx"
43 
44 namespace autodoc
45 {
46 namespace command
47 {
48 namespace run
49 {
50 
Parser(const Parse & i_command)51 Parser::Parser( const Parse & i_command )
52     :   rCommand(i_command),
53         pCppParser(),
54         pCppDocuInterpreter(),
55         pIdlParser()
56 {
57 }
58 
~Parser()59 Parser::~Parser()
60 {
61 }
62 
63 bool
Perform()64 Parser::Perform()
65 {
66     Cout() << "Parsing the repository "
67               << rCommand.ReposyName()
68               << " ..."
69               << Endl();
70   try
71   {
72     ::ary::Repository &
73         rAry = CommandLine::Get_().TheRepository();
74     rAry.Set_Title(rCommand.ReposyName());
75 
76     Dyn< FileCollector_Ifc >
77         pFiles( ParseToolsFactory().Create_FileCollector(6000) );
78 
79     bool bIDL = false;
80     bool bCpp = false;
81 
82     command::Parse::ProjectIterator
83         itEnd = rCommand.ProjectsEnd();
84     for ( command::Parse::ProjectIterator it = rCommand.ProjectsBegin();
85           it != itEnd;
86           ++it )
87     {
88         uintt nCount = GatherFiles( *pFiles, *(*it) );
89         Cout() << nCount
90              << " files found to parse in project "
91              << (*it)->Name()
92              << "."
93              << Endl();
94 
95         switch ( (*it)->Language().eLanguage )
96         {
97             case command::S_LanguageInfo::idl:
98             {
99                 Get_IdlParser().Run(*pFiles);
100                 bIDL = true;
101             }   break;
102             case command::S_LanguageInfo::cpp:
103             {
104                 Get_CppParser().Run( *pFiles );
105                 bCpp = true;
106             }   break;
107             default:
108                 Cerr() << "Project in yet unimplemented language skipped."
109                        << Endl();
110         }
111     }	// end for
112 
113     if (bCpp)
114     {
115 	    rAry.Gate_Cpp().Calculate_AllSecondaryInformation();
116     }
117     if (bIDL)
118     {
119 	    rAry.Gate_Idl().Calculate_AllSecondaryInformation(
120 	                        rCommand.DevelopersManual_RefFilePath() );
121 
122 //        ::ary::idl::SecondariesPilot &
123 //            rIdl2sPilot = rAry.Gate_Idl().Secondaries();
124 //
125 //        rIdl2sPilot.CheckAllInterfaceBases( rAry.Gate_Idl() );
126 //        rIdl2sPilot.Connect_Types2Ces();
127 //        rIdl2sPilot.Gather_CrossReferences();
128 //
129 //        if (NOT rCommand.DevelopersManual_RefFilePath().empty())
130 //        {
131 //            csv::File
132 //                aFile(rCommand.DevelopersManual_RefFilePath(), csv::CFM_READ);
133 //            if ( aFile.open() )
134 //            {
135 //                rIdl2sPilot.Read_Links2DevManual(aFile);
136 //     	        aFile.close();
137 //            }
138 //        }
139     }   // endif (bIDL)
140 
141     return true;
142 
143   }   // end try
144   catch (csv::Exception & xx)
145   {
146     xx.GetInfo(Cerr());
147     Cerr() << " program will exit." << Endl();
148 
149     return false;
150   }
151 }
152 
153 CodeParser_Ifc &
Get_CppParser()154 Parser::Get_CppParser()
155 {
156     if ( NOT pCppParser )
157         Create_CppParser();
158     return *pCppParser;
159 }
160 
161 IdlParser &
Get_IdlParser()162 Parser::Get_IdlParser()
163 {
164     if ( NOT pIdlParser )
165         Create_IdlParser();
166     return *pIdlParser;
167 }
168 
169 void
Create_CppParser()170 Parser::Create_CppParser()
171 {
172     pCppParser          = ParseToolsFactory().Create_Parser_Cplusplus();
173     pCppDocuInterpreter = ParseToolsFactory().Create_DocuParser_AutodocStyle();
174 
175     pCppParser->Setup( CommandLine::Get_().TheRepository(),
176                        *pCppDocuInterpreter,
177                        CommandLine::Get_().IgnoreDefines() );
178 }
179 
180 void
Create_IdlParser()181 Parser::Create_IdlParser()
182 {
183     pIdlParser = new IdlParser(CommandLine::Get_().TheRepository());
184 }
185 
186 const ParseToolsFactory_Ifc &
ParseToolsFactory()187 Parser::ParseToolsFactory()
188 {
189     return ParseToolsFactory_Ifc::GetIt_();
190 }
191 
192 uintt
GatherFiles(FileCollector_Ifc & o_rFiles,const S_ProjectData & i_rProject)193 Parser::GatherFiles( FileCollector_Ifc &    o_rFiles,
194                      const S_ProjectData &  i_rProject )
195 {
196     uintt ret = 0;
197     o_rFiles.EraseAll();
198 
199     typedef StringVector                StrVector;
200     typedef StrVector::const_iterator   StrIterator;
201     const S_Sources &
202         rSources = i_rProject.Sources();
203     const StrVector &
204         rExtensions = i_rProject.Language().aExtensions;
205 
206     StrIterator     it;
207     StrIterator     itTreesEnd  = rSources.aTrees.end();
208     StrIterator     itDirsEnd   = rSources.aDirectories.end();
209     StrIterator     itFilesEnd  = rSources.aFiles.end();
210     StrIterator     itExt;
211     StrIterator     itExtEnd    = rExtensions.end();
212 
213     csv::StreamStr aDir(500);
214     i_rProject.RootDirectory().Get( aDir );
215 
216     uintt nProjectDir_AddPosition =
217             ( strcmp(aDir.c_str(),".\\") == 0 OR strcmp(aDir.c_str(),"./") == 0 )
218                 ?   0
219                 :   uintt( aDir.tellp() );
220 
221     for ( it = rSources.aDirectories.begin();
222           it != itDirsEnd;
223           ++it )
224     {
225         aDir.seekp( nProjectDir_AddPosition );
226         aDir << *it;
227 
228         for ( itExt = rExtensions.begin();
229               itExt != itExtEnd;
230               ++itExt )
231         {
232             ret += o_rFiles.AddFilesFrom( aDir.c_str(),
233                                           *itExt,
234                                           FileCollector_Ifc::flat );
235         }   // end for itExt
236     }   // end for it
237     for ( it = rSources.aTrees.begin();
238           it != itTreesEnd;
239           ++it )
240     {
241         aDir.seekp( nProjectDir_AddPosition );
242         aDir << *it;
243 
244         for ( itExt = rExtensions.begin();
245               itExt != itExtEnd;
246               ++itExt )
247         {
248             ret += o_rFiles.AddFilesFrom( aDir.c_str(),
249                                           *itExt,
250                                           FileCollector_Ifc::recursive );
251         }   // end for itExt
252     }   // end for it
253     for ( it = rSources.aFiles.begin();
254           it != itFilesEnd;
255           ++it )
256     {
257         aDir.seekp( nProjectDir_AddPosition );
258         aDir << *it;
259 
260         o_rFiles.AddFile( aDir.c_str() );
261     }   // end for it
262     ret += rSources.aFiles.size();
263 
264     return ret;
265 }
266 
267 
268 }   // namespace run
269 }   // namespace command
270 
271 
272 #if 0
273 inline const ParseToolsFactory_Ifc &
274 CommandRunner::ParseToolsFactory()
275     { return ParseToolsFactory_Ifc::GetIt_(); }
276 
277 
278 inline const command::S_LanguageInfo &
279 CommandRunner::Get_ProjectLanguage( const command::Parse &          i_rCommand,
280                                     const command::S_ProjectData &  i_rProject )
281 {
282     if ( i_rProject.pLanguage )
283         return *i_rProject.pLanguage;
284     return *i_rCommand.GlobalLanguageInfo();
285 }
286 
287 inline bool
288 CommandRunner::HasParsedCpp() const
289     { return pCppParser; }
290 inline bool
291 CommandRunner::HasParsedIdl() const
292     { return pIdlParser; }
293 
294 
295 
296 
297 
298 CommandRunner::CommandRunner()
299     :   pCommandLine(0),
300         pReposy(0),
301         pNewReposy(0),
302         nResultCode(0)
303 {
304     Cout() << "\nAutodoc version 2.2.1"
305            << "\n-------------------"
306            << "\n" << Endl();
307 }
308 
309 CommandRunner::~CommandRunner()
310 {
311     ary::Repository::Destroy_();
312     Cout() << "\n" << Endl();
313 }
314 
315 void
316 CommandRunner::Run( const CommandLine & i_rCL )
317 {
318     ary::Repository::Destroy_();
319 //  ary::Repository::Destroy_();
320     pReposy = 0;
321     pNewReposy = 0;
322     nResultCode = 0;
323     pCommandLine = &i_rCL;
324 
325     pCommandLine->Run();
326 }
327 
328 void
329 CommandRunner::Parse()
330 {
331     try
332     {
333 
334     csv_assert( pCommandLine->Cmd_Parse() != 0 );
335     const command::Parse &
336         rCmd = *pCommandLine->Cmd_Parse();
337 
338     Cout() << "Parsing the repository "
339               << rCmd.ReposyName()
340               << " ..."
341               << Endl();
342 
343     if ( pReposy == 0 )
344         pReposy = & ary::Repository::Create_( rCmd.ReposyName(), 0 );
345     if ( pNewReposy == 0 )
346         pNewReposy = & ary::Repository::Create_( rCmd.ReposyName() );
347 
348     Dyn< FileCollector_Ifc > pFiles;
349     pFiles      = ParseToolsFactory().Create_FileCollector(6000);
350 
351     bool bCpp = false;
352     bool bIDL = false;
353 
354     command::Parse::ProjectIterator itEnd = rCmd.ProjectsEnd();
355     for ( command::Parse::ProjectIterator it = rCmd.ProjectsBegin();
356           it != itEnd;
357           ++it )
358     {
359 
360         uintt nCount = GatherFiles( *pFiles, rCmd, *(*it) );
361         Cout() << nCount
362              << " files found to parse in project "
363              << (*it)->Name()
364              << "."
365              << Endl();
366 
367 
368         switch ( Get_ProjectLanguage(rCmd, *(*it)).eLanguage )
369         {
370             case command::S_LanguageInfo::cpp:
371             {
372                 Get_CppParser().Run( (*it)->Name(),
373                                      (*it)->RootDirectory(),
374                                      *pFiles );
375                 bCpp = true;
376             }   break;
377             case command::S_LanguageInfo::idl:
378             {
379                 Get_IdlParser().Run(*pFiles);
380                 bIDL = true;
381             }   break;
382             default:
383                 Cerr() << "Project in yet unimplemented language skipped."
384                        << Endl();
385         }
386     }	// end for
387 
388     if (bCpp)
389 	    pReposy->RwGate_Cpp().Connect_AllTypes_2_TheirRelated_CodeEntites();
390     if (bIDL)
391     {
392         pNewReposy->Gate_Idl().Secondaries().Connect_Types2Ces();
393         pNewReposy->Gate_Idl().Secondaries().Gather_CrossReferences();
394     }
395 
396     }   // end try
397     catch (csv::Exception & xx)
398     {
399         xx.GetInfo(Cerr());
400         Cerr() << " program will exit." << Endl();
401         nResultCode = 1;
402     }
403     catch (...)
404     {
405         Cerr() << "Unknown exception -   program will exit." << Endl();
406         nResultCode = 1;
407     }
408 }
409 
410 void
411 CommandRunner::Load()
412 {
413     Cout() << "This would load the repository from the directory "
414               << pCommandLine->Cmd_Load()->ReposyDir()
415               << "."
416               << Endl();
417 }
418 
419 
420 void
421 CommandRunner::Save()
422 {
423     Cout() << "This would save the repository into the directory "
424               << pCommandLine->Cmd_Save()->ReposyDir()
425               << "."
426               << Endl();
427 }
428 
429 
430 void
431 CommandRunner::CreateHtml()
432 {
433     Cout() << "Creating HTML-output into the directory "
434               << pCommandLine->Cmd_CreateHtml()->OutputDir()
435               << "."
436               << Endl();
437 
438     if ( HasParsedCpp() )
439         CreateHtml_NewStyle();
440     if ( HasParsedIdl() )
441         CreateHtml_OldIdlStyle();
442 }
443 
444 
445 
446 void
447 CommandRunner::CreateXml()
448 {
449     Cout() << "This would create the XML-output into the directory "
450               << pCommandLine->Cmd_CreateXml()->OutputDir()
451               << "."
452               << Endl();
453 }
454 
455 CodeParser_Ifc &
456 CommandRunner::Get_CppParser()
457 {
458     if ( NOT pCppParser )
459         Create_CppParser();
460     return *pCppParser;
461 }
462 
463 IdlParser &
464 CommandRunner::Get_IdlParser()
465 {
466     if ( NOT pIdlParser )
467         Create_IdlParser();
468     return *pIdlParser;
469 }
470 
471 void
472 CommandRunner::Create_CppParser()
473 {
474     pCppParser          = ParseToolsFactory().Create_Parser_Cplusplus();
475     pCppDocuInterpreter = ParseToolsFactory().Create_DocuParser_AutodocStyle();
476 
477     pCppParser->Setup( *pReposy,
478                        *pCppDocuInterpreter );
479 }
480 
481 void
482 CommandRunner::Create_IdlParser()
483 {
484     pIdlParser = new IdlParser(*pNewReposy);
485 }
486 
487 uintt
488 CommandRunner::GatherFiles( FileCollector_Ifc &            o_rFiles,
489                             const command::Parse &         i_rCommand,
490                             const command::S_ProjectData & i_rProject )
491 {
492     uintt ret = 0;
493     o_rFiles.EraseAll();
494 
495     typedef StringVector                StrVector;
496     typedef StrVector::const_iterator   StrIterator;
497     const command::S_Sources &
498         rSources = i_rProject.aFiles;
499     const StrVector &
500         rExtensions = Get_ProjectLanguage(i_rCommand,i_rProject).aExtensions;
501 
502     StrIterator     it;
503     StrIterator     itDirsEnd   = rSources.aDirectories.end();
504     StrIterator     itTreesEnd  = i_rProject.aFiles.aTrees.end();
505     StrIterator     itFilesEnd  = i_rProject.aFiles.aFiles.end();
506     StrIterator     itExt;
507     StrIterator     itExtEnd    = rExtensions.end();
508 
509     csv::StreamStr aDir(500);
510     i_rProject.aRootDirectory.Get( aDir );
511 
512     uintt nProjectDir_AddPosition =
513             ( strcmp(aDir.c_str(),".\\") == 0 OR strcmp(aDir.c_str(),"./") == 0 )
514                 ?   0
515                 :   uintt( aDir.tellp() );
516 
517     for ( it = rSources.aDirectories.begin();
518           it != itDirsEnd;
519           ++it )
520     {
521         aDir.seekp( nProjectDir_AddPosition );
522         aDir << *it;
523 
524         for ( itExt = rExtensions.begin();
525               itExt != itExtEnd;
526               ++itExt )
527         {
528             ret += o_rFiles.AddFilesFrom( aDir.c_str(),
529                                           *itExt,
530                                           FileCollector_Ifc::flat );
531         }   // end for itExt
532     }   // end for it
533     for ( it = rSources.aTrees.begin();
534           it != itTreesEnd;
535           ++it )
536     {
537         aDir.seekp( nProjectDir_AddPosition );
538         aDir << *it;
539 
540         for ( itExt = rExtensions.begin();
541               itExt != itExtEnd;
542               ++itExt )
543         {
544             ret += o_rFiles.AddFilesFrom( aDir.c_str(),
545                                           *itExt,
546                                           FileCollector_Ifc::recursive );
547         }   // end for itExt
548     }   // end for it
549     for ( it = rSources.aFiles.begin();
550           it != itFilesEnd;
551           ++it )
552     {
553         aDir.seekp( nProjectDir_AddPosition );
554         aDir << *it;
555 
556         o_rFiles.AddFile( aDir.c_str() );
557     }   // end for it
558     ret += rSources.aFiles.size();
559 
560     return ret;
561 }
562 
563 void
564 CommandRunner::CreateHtml_NewStyle()
565 {
566     const ary::cpp::DisplayGate &
567             rGate = pReposy->DisplayGate_Cpp();
568 
569     Dyn< autodoc::HtmlDisplay_UdkStd > pHtmlDisplay;
570             pHtmlDisplay = DisplayToolsFactory_Ifc::GetIt_()
571                                 .Create_HtmlDisplay_UdkStd();
572 
573     pHtmlDisplay->Run( pCommandLine->Cmd_CreateHtml()->OutputDir(),
574                        rGate,
575                        DisplayToolsFactory_Ifc::GetIt_().Create_StdFrame() );
576 }
577 
578 void
579 CommandRunner::CreateHtml_OldIdlStyle()
580 {
581     ary::idl::Gate &
582         rAryGate            = pNewReposy->Gate_Idl();
583 
584     // Read DevManualLinkFile:
585     // KORR_FUTURE
586     csv::File
587         aFile("devmanref.txt", csv::CFM_READ);
588     if ( aFile.open() )
589     {
590         rAryGate.Secondaries().Read_Links2DevManual(aFile);
591      	aFile.close();
592     }
593 
594     // New Style Output
595     Dyn<autodoc::HtmlDisplay_Idl_Ifc> pNewDisplay;
596         pNewDisplay         = DisplayToolsFactory_Ifc::GetIt_()
597                                 .Create_HtmlDisplay_Idl();
598     pNewDisplay->Run( pCommandLine->Cmd_CreateHtml()->OutputDir(),
599                       rAryGate,
600                       DisplayToolsFactory_Ifc::GetIt_().Create_StdFrame() );
601 }
602 #endif // 0
603 
604 }   // namespace autodoc
605 
606 
607 
608 
609