xref: /trunk/main/idlc/source/idlcmain.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_idlc.hxx"
30 
31 #include "idlc/idlc.hxx"
32 #include "sal/main.h"
33 
34 #include <string.h>
35 
36 using namespace ::rtl;
37 
38 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
39 {
40     std::vector< std::string > args;
41     for (int i = 1; i < argc; i++)
42     {
43         if (!Options::checkArgument (args, argv[i], strlen(argv[i])))
44             return (1);
45     }
46 
47     Options options(argv[0]);
48     try
49     {
50         if (!options.initOptions(args))
51            return (0);
52     }
53     catch( IllegalArgument& e)
54     {
55         fprintf(stderr, "Illegal argument: %s\n%s",
56             e.m_message.getStr(),
57             options.prepareVersion().getStr());
58         return (99);
59     }
60 
61     setIdlc(&options);
62 
63     sal_Int32 nErrors = 0;
64     if (options.readStdin()) {
65         if ( !options.quiet() )
66             fprintf(
67                 stdout, "%s: Compiling stdin\n",
68                 options.getProgramName().getStr());
69         nErrors = compileFile(0);
70         if ( ( idlc()->getWarningCount() > 0 ) && !options.quiet() ) {
71             fprintf(
72                 stdout, "%s: detected %lu warnings compiling stdin\n",
73                 options.getProgramName().getStr(),
74                 sal::static_int_cast< unsigned long >(
75                     idlc()->getWarningCount()));
76         }
77         OString outputUrl;
78         if (options.isValid("-O")) {
79             outputUrl = convertToFileUrl(options.getOption("-O"));
80             if (outputUrl[outputUrl.getLength() - 1] != '/') {
81                 outputUrl += "/";
82             }
83             outputUrl += "stdin.urd";
84         } else {
85             outputUrl = convertToFileUrl("stdin.urd");
86         }
87         if (nErrors > 0) {
88             removeIfExists(outputUrl);
89         } else {
90             nErrors = produceFile(outputUrl);
91         }
92         idlc()->reset();
93     }
94     StringVector const & files = options.getInputFiles();
95     if ( options.verbose() )
96     {
97         fprintf( stdout, "%s: compiling %i source files ... \n",
98             options.getProgramName().getStr(), (int)files.size() );
99         fflush( stdout );
100     }
101     for (StringVector::const_iterator i(files.begin());
102          i != files.end() && nErrors == 0; ++i)
103     {
104         OString sysFileName( convertToAbsoluteSystemPath(*i) );
105 
106         if ( !options.quiet() )
107             fprintf(stdout, "Compiling: %s\n",
108                 (*i).getStr());
109         nErrors = compileFile(&sysFileName);
110 
111         if ( idlc()->getWarningCount() && !options.quiet() )
112             fprintf(stdout, "%s: detected %lu warnings compiling file '%s'\n",
113                     options.getProgramName().getStr(),
114                     sal::static_int_cast< unsigned long >(
115                         idlc()->getWarningCount()),
116                     (*i).getStr());
117 
118         // prepare output file name
119         OString outputFileUrl;
120         if ( options.isValid("-O") )
121         {
122             OString strippedFileName(sysFileName.copy(sysFileName.lastIndexOf(SEPARATOR) + 1));
123             outputFileUrl = convertToFileUrl(options.getOption("-O"));
124             sal_Char c = outputFileUrl.getStr()[outputFileUrl.getLength()-1];
125 
126             if ( c != '/' )
127                 outputFileUrl += OString::valueOf('/');
128 
129             outputFileUrl += strippedFileName.replaceAt(strippedFileName.getLength() -3 , 3, "urd");
130         } else
131         {
132             outputFileUrl = convertToFileUrl(sysFileName.replaceAt(sysFileName.getLength() -3 , 3, "urd"));
133         }
134 
135         if ( nErrors )
136             removeIfExists(outputFileUrl);
137         else
138             nErrors = produceFile(outputFileUrl);
139 
140         idlc()->reset();
141     }
142 
143     if ( nErrors > 0 )
144     {
145         fprintf(stderr, "%s: detected %ld errors%s",
146             options.getProgramName().getStr(),
147             sal::static_int_cast< long >(nErrors),
148             options.prepareVersion().getStr());
149     } else
150     {
151         if ( options.verbose() )
152             fprintf(stdout, "%s: returned successful%s",
153                 options.getProgramName().getStr(),
154                 options.prepareVersion().getStr());
155     }
156     return nErrors;
157 }
158