xref: /aoo42x/main/tools/bootstrp/rscdep.cxx (revision 0619ec5b)
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 
53 RscHrcDep::RscHrcDep()	:
54 	CppDep()
55 {
56 }
57 
58 RscHrcDep::~RscHrcDep()
59 {
60 }
61 
62 void RscHrcDep::Execute()
63 {
64 	CppDep::Execute();
65 }
66 
67 //static String aDelim;
68 
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         printf("option %d is [%s] and has length %d\n", i, aBuf, (int)nLength);
97 
98         if (nLength == 0)
99         {
100             // Is this even possible?
101             continue;
102         }
103         if (aBuf[0] == '-' && nLength > 0)
104         {
105             bool bIsKnownOption (true);
106             // Make a switch on the first character after the - for a
107             // preselection of the option.
108             // This is faster then multiple ifs and improves readability.
109             switch (aBuf[1])
110             {
111                 case 'p':
112                     if (nLength>1 && aBuf[2] == '=' )
113                         strcpy(pFileNamePrefix, &aBuf[3]);
114                     else
115                         bIsKnownOption = false;
116                     break;
117 
118                 case 'f':
119                     if (nLength>2 && aBuf[2] == 'o' && aBuf[3] == '=' )
120                     {
121                         strcpy(pOutputFileName, &aBuf[4]);
122                     }
123                     else if (nLength>2 && aBuf[2] == 'p' && aBuf[3] == '=' )
124                     {
125                         strcpy(pSrsFileName, &aBuf[4]);
126                         String aName( pSrsFileName, gsl_getSystemTextEncoding());
127                         DirEntry aDest( aName );
128                         aSrsBaseName = aDest.GetBase();
129                     }
130                     else
131                         bIsKnownOption = false;
132                     break;
133 
134                 case 'i':
135                 case 'I':
136 #ifdef DEBUG_VERBOSE
137                     printf("Include : %s\n", &aBuf[2] );
138 #endif
139                     pDep->AddSearchPath( &aBuf[2] );
140                     break;
141 
142                 case 'h' :
143                 case 'H' :
144                 case '?' :
145                     printf("RscDep 1.0\n");
146                     break;
147 
148                 case 'a' :
149 #ifdef DEBUG_VERBOSE
150                     printf("option a\n");
151 #endif
152                     break;
153 
154                 case 'l' :
155 #ifdef DEBUG_VERBOSE
156                     printf("option l with Value %s\n", &aBuf[2] );
157 #endif
158                     pDep->AddSource(&aBuf[2]);
159                     break;
160 
161                 default:
162                     bIsKnownOption = false;
163                     break;
164             }
165 #ifdef DEBUG_VERBOSE
166             if ( ! bIsKnownOption)
167 				printf("Unknown option error [%s]\n", aBuf);
168 #endif
169         }
170 		else if (aBuf[0] == '@' )
171 		{
172 			ByteString aToken;
173 			String aRespName( &aBuf[1], gsl_getSystemTextEncoding());
174 			SimpleConfig aConfig( aRespName );
175 			while ( (aToken = aConfig.GetNext()) != "")
176 			{
177 				char aBuf2[255];
178 				(void) strcpy( aBuf2, aToken.GetBuffer());
179 				if ( aBuf[0] == '-' && aBuf[1] == 'p' && aBuf[2] == '=' )
180 				{
181 					strcpy(pFileNamePrefix, &aBuf[3]);
182 					//break;
183 				}
184 				if ( aBuf2[0] == '-' && aBuf2[1] == 'f' && aBuf2[2] == 'o' )
185 				{
186 					strcpy(pOutputFileName, &aBuf2[3]);
187 					//break;
188 				}
189 				if ( aBuf2[0] == '-' && aBuf2[1] == 'f' && aBuf2[2] == 'p' )
190 				{
191 					strcpy(pSrsFileName, &aBuf2[3]);
192 					String aName( pSrsFileName, gsl_getSystemTextEncoding());
193 					DirEntry aDest( aName );
194 					aSrsBaseName = aDest.GetBase();
195 					//break;
196 				}
197 				if (aBuf2[0] == '-' &&  aBuf2[1] == 'i' )
198 				{
199 					//printf("Include : %s\n", &aBuf[2] );
200 					pDep->AddSearchPath( &aBuf2[2] );
201 				}
202 				if (aBuf2[0] == '-' &&  aBuf2[1] == 'I' )
203 				{
204 					//printf("Include : %s\n", &aBuf[2] );
205 					pDep->AddSearchPath( &aBuf2[2] );
206 				}
207 				if (( aBuf2[0] != '-' ) && ( aBuf2[0] != '@' ))
208 				{
209 					pDep->AddSource( &aBuf2[0] );
210 					aRespArg += " ";
211 					aRespArg += &aBuf2[0];
212 					bSource = sal_True;
213 				}
214 			}
215 		}
216         else
217         {
218             // Collect all non-options at the head of argv.
219             if (++nLastNonOption < i)
220                 argv[nLastNonOption] = argv[i];
221         }
222 	}
223 
224 	DirEntry aEntry(".");
225 	aEntry.ToAbs();
226 //	String aCwd = aEntry.GetName();
227 	String aCwd(pFileNamePrefix, gsl_getSystemTextEncoding());
228 /*	sal_uInt16 nPos;
229 #ifndef UNX
230 	while ( (nPos = aCwd.Search('\\') != STRING_NOTFOUND  ))
231 #else
232 	while ( (nPos = aCwd.Search('/') != STRING_NOTFOUND  ))
233 #endif
234 	{
235 		String attt = aCwd.Copy( 0, nPos );
236 		aCwd.Erase( 0, nPos );
237 	} */
238 	SvFileStream aOutStream;
239 	String aOutputFileName( pOutputFileName, gsl_getSystemTextEncoding());
240 	DirEntry aOutEntry( aOutputFileName );
241 	String aOutPath = aOutEntry.GetPath().GetFull();
242 
243 	String aFileName( aOutPath );
244 	aFileName += aDelim;
245 	aFileName += aCwd;
246 	aFileName += String(".", gsl_getSystemTextEncoding());
247 	aFileName += aSrsBaseName;
248 	aFileName += String(".dprr", gsl_getSystemTextEncoding());
249 	//fprintf( stderr, "OutFileName : %s \n",aFileName.GetStr());
250 	aOutStream.Open( aFileName, STREAM_WRITE );
251 
252     // Process the yet unhandled non-options.  These are supposed to
253     // be names of files on which the target depends.
254 	ByteString aString;
255     if (nLastNonOption >= 0)
256 	{
257 #ifdef DEBUG_VERBOSE
258 		printf("further arguments : ");
259 #endif
260 		aString = ByteString( pSrsFileName );
261 		aString.SearchAndReplaceAll('\\', ByteString( aDelim,  RTL_TEXTENCODING_ASCII_US ));
262 		aString += ByteString(" : " );
263 
264         for (sal_Int32 nIndex=0; nIndex<=nLastNonOption; ++nIndex)
265 		{
266             printf("option at %d is [%s]\n", (int)nIndex, argv[nIndex]);
267 			if (!bSource )
268 			{
269 				aString += ByteString("	" );
270 				aString += ByteString( argv[nIndex]);
271 				pDep->AddSource( argv[nIndex]);
272 			}
273 		}
274 	}
275 	aString += aRespArg;
276 	pDep->Execute();
277 	ByteStringList *pLst = pDep->GetDepList();
278 	sal_uIntPtr nCount = pLst->Count();
279 	if ( nCount == 0 )
280 	{
281 		aOutStream.WriteLine( aString );
282 	}
283 	else
284 	{
285 		aString += ByteString( "\\" );
286 		aOutStream.WriteLine( aString );
287 	}
288 
289 	for ( sal_uIntPtr j=0; j<nCount; j++ )
290 	{
291 		ByteString *pStr = pLst->GetObject(j);
292 		pStr->SearchAndReplaceAll('\\', ByteString( aDelim,  RTL_TEXTENCODING_ASCII_US ));
293 		if ( j != (nCount-1) )
294 			*pStr += ByteString( "\\" );
295 		aOutStream.WriteLine( *pStr );
296 	}
297 	delete pDep;
298 	aOutStream.Close();
299 
300 	return 0;
301 }
302 
303