1 | //####COPYRIGHTBEGIN#### |
---|
2 | // |
---|
3 | // ---------------------------------------------------------------------------- |
---|
4 | // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
---|
5 | // |
---|
6 | // This program is part of the eCos host tools. |
---|
7 | // |
---|
8 | // This program is free software; you can redistribute it and/or modify it |
---|
9 | // under the terms of the GNU General Public License as published by the Free |
---|
10 | // Software Foundation; either version 2 of the License, or (at your option) |
---|
11 | // any later version. |
---|
12 | // |
---|
13 | // This program is distributed in the hope that it will be useful, but WITHOUT |
---|
14 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
15 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
16 | // more details. |
---|
17 | // |
---|
18 | // You should have received a copy of the GNU General Public License along with |
---|
19 | // this program; if not, write to the Free Software Foundation, Inc., |
---|
20 | // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
21 | // |
---|
22 | // ---------------------------------------------------------------------------- |
---|
23 | // |
---|
24 | //####COPYRIGHTEND#### |
---|
25 | //================================================================= |
---|
26 | // |
---|
27 | // eCosTestUtils.cpp |
---|
28 | // |
---|
29 | // Utility functions |
---|
30 | // |
---|
31 | //================================================================= |
---|
32 | //================================================================= |
---|
33 | //#####DESCRIPTIONBEGIN#### |
---|
34 | // |
---|
35 | // Author(s): sdf |
---|
36 | // Contributors: sdf |
---|
37 | // Date: 1999-04-01 |
---|
38 | // Description: This class contains utility functions for use in the testing infrastructure |
---|
39 | // Usage: |
---|
40 | // |
---|
41 | //####DESCRIPTIONEND#### |
---|
42 | |
---|
43 | #include "eCosStd.h" |
---|
44 | #include "eCosSocket.h" |
---|
45 | #include "eCosTestUtils.h" |
---|
46 | #include "eCosThreadUtils.h" |
---|
47 | #include "eCosTrace.h" |
---|
48 | #include "TestResource.h" |
---|
49 | |
---|
50 | LPCTSTR const CeCosTestUtils::Tail(LPCTSTR const pszFile) |
---|
51 | { |
---|
52 | LPCTSTR pszTail=_tcsrchr(pszFile,_TCHAR('/')); |
---|
53 | if(0==pszTail){ |
---|
54 | pszTail=_tcsrchr(pszFile,_TCHAR('\\')); |
---|
55 | } |
---|
56 | return (0==pszTail)?pszFile:pszTail+1; |
---|
57 | } |
---|
58 | |
---|
59 | // File iterator. Gets next file in directory, avoiding _T(".") and _T("..") |
---|
60 | bool CeCosTestUtils::NextFile (void *&pHandle,String &str) |
---|
61 | { |
---|
62 | #ifdef _WIN32 |
---|
63 | WIN32_FIND_DATA fd; |
---|
64 | while(FindNextFile((HANDLE)pHandle,&fd)){ |
---|
65 | LPCTSTR pszName=fd.cFileName; |
---|
66 | #else // UNIX |
---|
67 | struct dirent *d; |
---|
68 | while((d=readdir((DIR *)pHandle))){ |
---|
69 | LPCTSTR pszName=d->d_name; |
---|
70 | #endif |
---|
71 | if(pszName[0]!='.'){ |
---|
72 | str=pszName; |
---|
73 | return true; |
---|
74 | } |
---|
75 | } |
---|
76 | return false; |
---|
77 | } |
---|
78 | |
---|
79 | // Start file iteration and return first file. |
---|
80 | bool CeCosTestUtils::StartSearch (void *&pHandle,String &str) |
---|
81 | { |
---|
82 | #ifdef _WIN32 |
---|
83 | WIN32_FIND_DATA fd; |
---|
84 | pHandle=(void *)FindFirstFile (_T("*.*"), &fd); |
---|
85 | if(INVALID_HANDLE_VALUE==(HANDLE)pHandle){ |
---|
86 | ERROR(_T("Failed to open dir\n")); |
---|
87 | return false; |
---|
88 | } else if (fd.cFileName[0]=='.') { |
---|
89 | return NextFile((HANDLE)pHandle,str); |
---|
90 | } else { |
---|
91 | str=String(fd.cFileName); |
---|
92 | return true; |
---|
93 | } |
---|
94 | #else // UNIX |
---|
95 | pHandle=(void *)opendir(_T(".")); |
---|
96 | if(0==pHandle){ |
---|
97 | ERROR(_T("Failed to open dir\n")); |
---|
98 | return false; |
---|
99 | } |
---|
100 | return NextFile(pHandle,str); |
---|
101 | #endif |
---|
102 | } |
---|
103 | |
---|
104 | // End file iteration |
---|
105 | void CeCosTestUtils::EndSearch (void *&pHandle) |
---|
106 | { |
---|
107 | #ifdef _WIN32 |
---|
108 | FindClose((HANDLE)pHandle); |
---|
109 | #else // UNIX |
---|
110 | closedir((DIR *)pHandle); |
---|
111 | #endif |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | // deal with common command-line actions |
---|
116 | bool CeCosTestUtils::CommandLine(int &argc,TCHAR **argv,bool bRequireResourceServer) |
---|
117 | { |
---|
118 | LPCTSTR psz=_tgetenv(_T("RESOURCESERVER")); |
---|
119 | if(psz && !CTestResource::SetResourceServer(psz)){ |
---|
120 | _ftprintf(stderr,_T("Illegal host:port '%s' defined in RESOURCESERVER environment variable\n"),psz); |
---|
121 | return false; |
---|
122 | } |
---|
123 | |
---|
124 | for(int i=1;i<argc;i++){ |
---|
125 | if(_TCHAR('-')==*argv[i]){ |
---|
126 | if(0==_tcscmp(argv[i],_T("-v"))){ |
---|
127 | CeCosTrace::EnableTracing((CeCosTrace::TraceLevel)MAX(CeCosTrace::TracingEnabled(),CeCosTrace::TRACE_LEVEL_TRACE)); |
---|
128 | // Shuffle the command line down to remove that which we have just seen: |
---|
129 | for(TCHAR **a=argv+i;(a[0]=a[1]);a++); |
---|
130 | argc--;i--; // counteract the increment |
---|
131 | } else if(0==_tcscmp(argv[i],_T("-V"))){ |
---|
132 | CeCosTrace::EnableTracing((CeCosTrace::TraceLevel)MAX(CeCosTrace::TracingEnabled(),CeCosTrace::TRACE_LEVEL_VTRACE)); |
---|
133 | // Shuffle the command line down to remove that which we have just seen: |
---|
134 | for(TCHAR **a=argv+i;(a[0]=a[1]);a++); |
---|
135 | argc--;i--; // counteract the increment |
---|
136 | } else if(0==_tcscmp(argv[i],_T("-o"))){ |
---|
137 | if(i+1<argc){ |
---|
138 | if(!CeCosTrace::SetOutput(argv[i+1])){ |
---|
139 | _ftprintf(stderr,_T("Failed to direct output to %s\n"),argv[i+1]); |
---|
140 | } |
---|
141 | // Shuffle the command line down to remove that which we have just seen: |
---|
142 | for(TCHAR **a=argv+i;(a[0]=a[2]);a++); |
---|
143 | argc-=2;i-=2; // counteract the increment |
---|
144 | } else { |
---|
145 | return false; |
---|
146 | } |
---|
147 | } else if(0==_tcscmp(argv[i],_T("-O"))){ |
---|
148 | if(i+1<argc){ |
---|
149 | if(!CeCosTrace::SetError(argv[i+1])){ |
---|
150 | _ftprintf(stderr,_T("Failed to direct error to %s\n"),argv[i+1]); |
---|
151 | } |
---|
152 | // Shuffle the command line down to remove that which we have just seen: |
---|
153 | for(TCHAR **a=argv+i;(a[0]=a[2]);a++); |
---|
154 | argc-=2;i-=2; // counteract the increment |
---|
155 | } else { |
---|
156 | return false; |
---|
157 | } |
---|
158 | } else if(0==_tcscmp(argv[i],_T("-r"))){ |
---|
159 | if(i+1<argc){ |
---|
160 | if(!CTestResource::SetResourceServer(argv[i+1])){ |
---|
161 | _ftprintf(stderr,_T("Illegal host:port '%s'\n"),argv[i+1]); |
---|
162 | return false; |
---|
163 | } |
---|
164 | // Shuffle the command line down to remove that which we have just seen: |
---|
165 | for(TCHAR **a=argv+i;(a[0]=a[2]);a++); |
---|
166 | argc-=2;i-=2; // counteract the increment |
---|
167 | } else { |
---|
168 | return false; |
---|
169 | } |
---|
170 | } else if(0==_tcscmp(argv[i],_T("-version"))){ |
---|
171 | const TCHAR *pszTail=_tcsrchr(argv[0],_TCHAR('/')); |
---|
172 | if(0==pszTail){ |
---|
173 | pszTail=_tcsrchr(argv[0],_TCHAR('\\')); |
---|
174 | } |
---|
175 | _tprintf (_T("%s %s (%s %s)\n"), (0==pszTail)?argv[0]:pszTail+1,ECOS_VERSION, __DATE__, __TIME__); |
---|
176 | exit(0); |
---|
177 | } |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | if(!CeCosSocket::Init() || !CeCosTestPlatform::Load()){ |
---|
182 | return false; |
---|
183 | } |
---|
184 | |
---|
185 | #ifndef _WIN32 |
---|
186 | sigset_t mask; |
---|
187 | |
---|
188 | // Clean out all the signals |
---|
189 | sigemptyset(&mask); |
---|
190 | |
---|
191 | // Add our sigpipe |
---|
192 | sigaddset(&mask, SIGPIPE); |
---|
193 | |
---|
194 | sigprocmask(SIG_SETMASK, &mask, NULL); |
---|
195 | |
---|
196 | #endif |
---|
197 | |
---|
198 | if(CTestResource::ResourceServerSet()){ |
---|
199 | if(CTestResource::Load()){ |
---|
200 | _ftprintf(stderr,_T("Connected to resource server %s\n"),(LPCTSTR)CTestResource::GetResourceServer()); |
---|
201 | } else { |
---|
202 | _ftprintf(stderr,_T("Can't load from resource server %s\n"),(LPCTSTR)CTestResource::GetResourceServer()); |
---|
203 | return false; |
---|
204 | } |
---|
205 | } else if (bRequireResourceServer) { |
---|
206 | _ftprintf(stderr,_T("You must specify a resource server using either the -r switch or by setting the RESOURCESERVER environment variable\n")); |
---|
207 | return false; |
---|
208 | } |
---|
209 | |
---|
210 | return true; |
---|
211 | } |
---|
212 | |
---|
213 | void CeCosTestUtils::UsageMessage(bool bRequireResourceServer) |
---|
214 | { |
---|
215 | _ftprintf(stderr, |
---|
216 | _T(" -o file : send standard output to named file\n") |
---|
217 | _T(" -O file : send standard error to named file\n")); |
---|
218 | if(bRequireResourceServer){ |
---|
219 | _ftprintf(stderr, |
---|
220 | _T(" -r host:port : use this host:port as resourceserver [or set the RESOURCESERVER environment variable]\n") |
---|
221 | ); |
---|
222 | } |
---|
223 | _ftprintf(stderr, |
---|
224 | _T(" -v : vebose mode - trace to stderr\n") |
---|
225 | _T(" -V : very verbose mode - trace to stderr\n") |
---|
226 | _T(" -version : print a version string\n") |
---|
227 | ); |
---|
228 | } |
---|
229 | |
---|
230 | const String CeCosTestUtils::HomeFile (LPCTSTR pszFile) |
---|
231 | { |
---|
232 | String strFile; |
---|
233 | #ifdef _WIN32 |
---|
234 | LPCTSTR psz=_tgetenv(_T("HOMEDRIVE")); |
---|
235 | if(psz){ |
---|
236 | strFile=psz; |
---|
237 | psz=_tgetenv(_T("HOMEPATH")); |
---|
238 | if(psz){ |
---|
239 | strFile+=psz; |
---|
240 | } |
---|
241 | if(_TCHAR('\\')!=strFile[strFile.size()-1]){ |
---|
242 | strFile+=_TCHAR('\\'); |
---|
243 | } |
---|
244 | strFile+=pszFile; |
---|
245 | } |
---|
246 | #else // UNIX |
---|
247 | LPCTSTR psz=_tgetenv(_T("HOME")); |
---|
248 | if(psz){ |
---|
249 | strFile=psz; |
---|
250 | strFile+=_TCHAR('/'); |
---|
251 | strFile+=pszFile; |
---|
252 | } |
---|
253 | #endif |
---|
254 | return strFile; |
---|
255 | } |
---|
256 | |
---|
257 | bool CeCosTestUtils::Exists(LPCTSTR pszFile) |
---|
258 | { |
---|
259 | struct _stat buf; |
---|
260 | return (0==_tstat(pszFile,&buf)); |
---|
261 | } |
---|
262 | |
---|
263 | bool CeCosTestUtils::IsFile(LPCTSTR pszFile) |
---|
264 | { |
---|
265 | struct _stat buf; |
---|
266 | return 0==_tstat(pszFile,&buf) && 0==(S_IFDIR&buf.st_mode); |
---|
267 | } |
---|
268 | |
---|