xref: /aoo4110/main/tools/bootstrp/rscdep.cxx (revision b1cdbd2c)
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 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_tools.hxx"
26 #if defined(UNX) || defined(OS2)
27 #include <unistd.h>
28 #endif
29 
30 #include <sys/stat.h>
31 #include <stdio.h>
32 #include <string.h>
33 
34 #include "bootstrp/prj.hxx"
35 #include "sal/main.h"
36 
37 #include <tools/string.hxx>
38 #include <tools/list.hxx>
39 #include <tools/fsys.hxx>
40 #include <tools/stream.hxx>
41 
42 #include "cppdep.hxx"
43 
44 class RscHrcDep : public CppDep
45 {
46 public:
47 					RscHrcDep();
48 	virtual			~RscHrcDep();
49 
50 	virtual void 	Execute();
51 };
52 
RscHrcDep()53 RscHrcDep::RscHrcDep()	:
54 	CppDep()
55 {
56 }
57 
~RscHrcDep()58 RscHrcDep::~RscHrcDep()
59 {
60 }
61 
Execute()62 void RscHrcDep::Execute()
63 {
64 	CppDep::Execute();
65 }
66 
67 //static String aDelim;
68 
main(int argc,char ** argv)69 int main( int argc, char** argv )
70 {
71 	char aBuf[255];
72 	char pFileNamePrefix[255];
73 	char pOutputFileName[255];
74 	char pSrsFileName[255];
75 	String aSrsBaseName;
76 	sal_Bool bSource = sal_False;
77 	ByteString aRespArg;
78 //  who needs anything but '/' ?
79 //	String aDelim = String(DirEntry::GetAccessDelimiter());
80 	String aDelim = '/';
81 	RscHrcDep *pDep = new RscHrcDep;
82 
83     // When the options are processed, the non-option arguments are
84     // collected at the head of the argv array.
85     // nLastNonOption points to the last of them.
86     int nLastNonOption (-1);
87 
88     pOutputFileName[0] = 0;
89     pSrsFileName[0] = 0;
90 
91 	for ( int i=1; i<argc; i++)
92 	{
93 		strcpy( aBuf, (const char *)argv[i] );
94         const sal_Int32 nLength (strlen(aBuf));
95 
96 #ifdef DEBUG
97         printf("option %d is [%s] and has length %d\n", i, aBuf, (int)nLength);
98 #endif
99 
100         if (nLength == 0)
101         {
102             // Is this even possible?
103             continue;
104         }
105         if (aBuf[0] == '-' && nLength > 0)
106         {
107             bool bIsKnownOption (true);
108             // Make a switch on the first character after the - for a
109             // preselection of the option.
110             // This is faster then multiple ifs and improves readability.
111             switch (aBuf[1])
112             {
113                 case 'p':
114                     if (nLength>1 && aBuf[2] == '=' )
115                         strcpy(pFileNamePrefix, &aBuf[3]);
116                     else
117                         bIsKnownOption = false;
118                     break;
119 
120                 case 'f':
121                     if (nLength>2 && aBuf[2] == 'o' && aBuf[3] == '=' )
122                     {
123                         strcpy(pOutputFileName, &aBuf[4]);
124                     }
125                     else if (nLength>2 && aBuf[2] == 'p' && aBuf[3] == '=' )
126                     {
127                         strcpy(pSrsFileName, &aBuf[4]);
128                         String aName( pSrsFileName, gsl_getSystemTextEncoding());
129                         DirEntry aDest( aName );
130                         aSrsBaseName = aDest.GetBase();
131                     }
132                     else
133                         bIsKnownOption = false;
134                     break;
135 
136                 case 'i':
137                 case 'I':
138 #ifdef DEBUG_VERBOSE
139                     printf("Include : %s\n", &aBuf[2] );
140 #endif
141                     pDep->AddSearchPath( &aBuf[2] );
142                     break;
143 
144                 case 'h' :
145                 case 'H' :
146                 case '?' :
147                     printf("RscDep 1.0\n");
148                     break;
149 
150                 case 'a' :
151 #ifdef DEBUG_VERBOSE
152                     printf("option a\n");
153 #endif
154                     break;
155 
156                 case 'l' :
157 #ifdef DEBUG_VERBOSE
158                     printf("option l with Value %s\n", &aBuf[2] );
159 #endif
160                     pDep->AddSource(&aBuf[2]);
161                     break;
162 
163                 default:
164                     bIsKnownOption = false;
165                     break;
166             }
167 #ifdef DEBUG_VERBOSE
168             if ( ! bIsKnownOption)
169 				printf("Unknown option error [%s]\n", aBuf);
170 #endif
171         }
172 		else if (aBuf[0] == '@' )
173 		{
174 			ByteString aToken;
175 			String aRespName( &aBuf[1], gsl_getSystemTextEncoding());
176 			SimpleConfig aConfig( aRespName );
177 			while ( (aToken = aConfig.GetNext()) != "")
178 			{
179 				char aBuf2[255];
180 				(void) strcpy( aBuf2, aToken.GetBuffer());
181 				if ( aBuf[0] == '-' && aBuf[1] == 'p' && aBuf[2] == '=' )
182 				{
183 					strcpy(pFileNamePrefix, &aBuf[3]);
184 					//break;
185 				}
186 				if ( aBuf2[0] == '-' && aBuf2[1] == 'f' && aBuf2[2] == 'o' )
187 				{
188 					strcpy(pOutputFileName, &aBuf2[3]);
189 					//break;
190 				}
191 				if ( aBuf2[0] == '-' && aBuf2[1] == 'f' && aBuf2[2] == 'p' )
192 				{
193 					strcpy(pSrsFileName, &aBuf2[3]);
194 					String aName( pSrsFileName, gsl_getSystemTextEncoding());
195 					DirEntry aDest( aName );
196 					aSrsBaseName = aDest.GetBase();
197 					//break;
198 				}
199 				if (aBuf2[0] == '-' &&  aBuf2[1] == 'i' )
200 				{
201 					//printf("Include : %s\n", &aBuf[2] );
202 					pDep->AddSearchPath( &aBuf2[2] );
203 				}
204 				if (aBuf2[0] == '-' &&  aBuf2[1] == 'I' )
205 				{
206 					//printf("Include : %s\n", &aBuf[2] );
207 					pDep->AddSearchPath( &aBuf2[2] );
208 				}
209 				if (( aBuf2[0] != '-' ) && ( aBuf2[0] != '@' ))
210 				{
211 					pDep->AddSource( &aBuf2[0] );
212 					aRespArg += " ";
213 					aRespArg += &aBuf2[0];
214 					bSource = sal_True;
215 				}
216 			}
217 		}
218         else
219         {
220             // Collect all non-options at the head of argv.
221             if (++nLastNonOption < i)
222                 argv[nLastNonOption] = argv[i];
223         }
224 	}
225 
226 	DirEntry aEntry(".");
227 	aEntry.ToAbs();
228 //	String aCwd = aEntry.GetName();
229 	String aCwd(pFileNamePrefix, gsl_getSystemTextEncoding());
230 /*	sal_uInt16 nPos;
231 #ifndef UNX
232 	while ( (nPos = aCwd.Search('\\') != STRING_NOTFOUND  ))
233 #else
234 	while ( (nPos = aCwd.Search('/') != STRING_NOTFOUND  ))
235 #endif
236 	{
237 		String attt = aCwd.Copy( 0, nPos );
238 		aCwd.Erase( 0, nPos );
239 	} */
240 	SvFileStream aOutStream;
241 	String aOutputFileName( pOutputFileName, gsl_getSystemTextEncoding());
242 	DirEntry aOutEntry( aOutputFileName );
243 	String aOutPath = aOutEntry.GetPath().GetFull();
244 
245 	String aFileName( aOutPath );
246 	aFileName += aDelim;
247 	aFileName += aCwd;
248 	aFileName += String(".", gsl_getSystemTextEncoding());
249 	aFileName += aSrsBaseName;
250 	aFileName += String(".dprr", gsl_getSystemTextEncoding());
251 	//fprintf( stderr, "OutFileName : %s \n",aFileName.GetStr());
252 	aOutStream.Open( aFileName, STREAM_WRITE );
253 
254     // Process the yet unhandled non-options.  These are supposed to
255     // be names of files on which the target depends.
256 	ByteString aString;
257     if (nLastNonOption >= 0)
258 	{
259 #ifdef DEBUG_VERBOSE
260 		printf("further arguments : ");
261 #endif
262 		aString = ByteString( pSrsFileName );
263 		aString.SearchAndReplaceAll('\\', ByteString( aDelim,  RTL_TEXTENCODING_ASCII_US ));
264 		aString += ByteString(" : " );
265 
266         for (sal_Int32 nIndex=0; nIndex<=nLastNonOption; ++nIndex)
267 		{
268 
269 #ifdef DEBUG
270             printf("option at %d is [%s]\n", (int)nIndex, argv[nIndex]);
271 #endif
272 			if (!bSource )
273 			{
274 				aString += ByteString("	" );
275 				aString += ByteString( argv[nIndex]);
276 				pDep->AddSource( argv[nIndex]);
277 			}
278 		}
279 	}
280 	aString += aRespArg;
281 	pDep->Execute();
282 	ByteStringList *pLst = pDep->GetDepList();
283 	sal_uIntPtr nCount = pLst->Count();
284 	if ( nCount == 0 )
285 	{
286 		aOutStream.WriteLine( aString );
287 	}
288 	else
289 	{
290 		aString += ByteString( "\\" );
291 		aOutStream.WriteLine( aString );
292 	}
293 
294 	for ( sal_uIntPtr j=0; j<nCount; j++ )
295 	{
296 		ByteString *pStr = pLst->GetObject(j);
297 		pStr->SearchAndReplaceAll('\\', ByteString( aDelim,  RTL_TEXTENCODING_ASCII_US ));
298 		if ( j != (nCount-1) )
299 			*pStr += ByteString( "\\" );
300 		aOutStream.WriteLine( *pStr );
301 	}
302 	delete pDep;
303 	aOutStream.Close();
304 
305 	return 0;
306 }
307 
308