comparison host/tools/ecostest/common/eCosTestUtils.cpp @ 82:6736c52df507 ecos-sw-2000-04-14

Merge from eCos master repository on 2000-04-14-13:35:46-BST
author jlarmour
date Tue, 18 Apr 2000 21:51:55 +0000
parents 435cced73e2f
children
comparison
equal deleted inserted replaced
81:89fef2181d7d 82:6736c52df507
39 // Usage: 39 // Usage:
40 // 40 //
41 //####DESCRIPTIONEND#### 41 //####DESCRIPTIONEND####
42 42
43 #include "eCosStd.h" 43 #include "eCosStd.h"
44 #include "eCosSocket.h"
44 #include "eCosTestUtils.h" 45 #include "eCosTestUtils.h"
45 #include "eCosThreadUtils.h" 46 #include "eCosThreadUtils.h"
46 #include "eCosTrace.h" 47 #include "eCosTrace.h"
48 #include "TestResource.h"
47 49
48 LPCTSTR const CeCosTestUtils::Tail(LPCTSTR const pszFile) 50 LPCTSTR const CeCosTestUtils::Tail(LPCTSTR const pszFile)
49 { 51 {
50 LPCTSTR pszTail=_tcsrchr(pszFile,_TCHAR('/')); 52 LPCTSTR pszTail=_tcsrchr(pszFile,_TCHAR('/'));
51 if(0==pszTail){ 53 if(0==pszTail){
52 pszTail=_tcsrchr(pszFile,_TCHAR('\\')); 54 pszTail=_tcsrchr(pszFile,_TCHAR('\\'));
53 } 55 }
54 return (0==pszTail)?pszFile:pszTail+1; 56 return (0==pszTail)?pszFile:pszTail+1;
55 } 57 }
56
57 LPCTSTR CeCosTestUtils::HostName()
58 {
59 static bool bFirstTime=true;
60 static String str;
61 static int nErr;
62 if(bFirstTime){
63 char szMyname[256];
64 nErr=gethostname(szMyname,sizeof szMyname);
65 bFirstTime=false;
66 if(0==nErr){
67 str=String::CStrToUnicodeStr(szMyname);
68 } else {
69 str=_T("");
70 }
71 }
72 return str;
73 }
74
75 LPCTSTR CeCosTestUtils::SimpleHostName()
76 {
77 static bool bFirstTime=true;
78 static String str;
79 if(bFirstTime){
80 str=HostName();
81 // Remove all after a '.'
82 LPCTSTR c=_tcschr(str,_TCHAR('.'));
83 if(c){
84 str.SetLength(c-(LPCTSTR )str);
85 }
86 bFirstTime=false;
87 }
88 return str;
89 }
90
91
92 58
93 // File iterator. Gets next file in directory, avoiding _T(".") and _T("..") 59 // File iterator. Gets next file in directory, avoiding _T(".") and _T("..")
94 bool CeCosTestUtils::NextFile (void *&pHandle,String &str) 60 bool CeCosTestUtils::NextFile (void *&pHandle,String &str)
95 { 61 {
96 #ifdef _WIN32 62 #ifdef _WIN32
145 #endif 111 #endif
146 } 112 }
147 113
148 114
149 // deal with common command-line actions 115 // deal with common command-line actions
150 bool CeCosTestUtils::CommandLine(int &argc,TCHAR **argv) 116 bool CeCosTestUtils::CommandLine(int &argc,TCHAR **argv,bool bRequireResourceServer)
151 { 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
152 for(int i=1;i<argc;i++){ 124 for(int i=1;i<argc;i++){
153 if(_TCHAR('-')==*argv[i]){ 125 if(_TCHAR('-')==*argv[i]){
154 if(0==_tcscmp(argv[i],_T("-v"))){ 126 if(0==_tcscmp(argv[i],_T("-v"))){
155 CeCosTrace ::EnableTracing(true); 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));
156 // Shuffle the command line down to remove that which we have just seen: 133 // Shuffle the command line down to remove that which we have just seen:
157 for(TCHAR **a=argv+i;(a[0]=a[1]);a++); 134 for(TCHAR **a=argv+i;(a[0]=a[1]);a++);
158 argc--;i--; // counteract the increment 135 argc--;i--; // counteract the increment
159 } else if(0==_tcscmp(argv[i],_T("-o"))){ 136 } else if(0==_tcscmp(argv[i],_T("-o"))){
160 if(i+1<argc){ 137 if(i+1<argc){
161 if(!CeCosTrace::SetOutput(argv[i+1])){ 138 if(!CeCosTrace::SetOutput(argv[i+1])){
162 ERROR(_T("Failed to direct output to %s\n"),argv[i+1]); 139 _ftprintf(stderr,_T("Failed to direct output to %s\n"),argv[i+1]);
163 } 140 }
164 // Shuffle the command line down to remove that which we have just seen: 141 // Shuffle the command line down to remove that which we have just seen:
165 for(TCHAR **a=argv+i;(a[0]=a[2]);a++); 142 for(TCHAR **a=argv+i;(a[0]=a[2]);a++);
166 argc-=2;i-=2; // counteract the increment 143 argc-=2;i-=2; // counteract the increment
167 } else { 144 } else {
168 return false; 145 return false;
169 } 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"))){ 170 } else if(0==_tcscmp(argv[i],_T("-version"))){
171 const TCHAR *pszTail=_tcsrchr(argv[0],_TCHAR('/')); 171 const TCHAR *pszTail=_tcsrchr(argv[0],_TCHAR('/'));
172 if(0==pszTail){ 172 if(0==pszTail){
173 pszTail=_tcsrchr(argv[0],_TCHAR('\\')); 173 pszTail=_tcsrchr(argv[0],_TCHAR('\\'));
174 } 174 }
175 _tprintf (_T("%s %s (%s %s)\n"), (0==pszTail)?argv[0]:pszTail+1,ECOS_VERSION, __DATE__, __TIME__); 175 _tprintf (_T("%s %s (%s %s)\n"), (0==pszTail)?argv[0]:pszTail+1,ECOS_VERSION, __DATE__, __TIME__);
176 exit(0); 176 exit(0);
177 } 177 }
178 } 178 }
179 } 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
180 return true; 210 return true;
181 } 211 }
182 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