]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/tools/ecostest/common/eCosTestPlatform.cpp
Initial revision
[karo-tx-redboot.git] / tools / src / tools / ecostest / common / eCosTestPlatform.cpp
1 //####COPYRIGHTBEGIN####
2 //                                                                          
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 // Copyright (C) 2003 John Dallaway
6 //
7 // This program is part of the eCos host tools.
8 //
9 // This program is free software; you can redistribute it and/or modify it 
10 // under the terms of the GNU General Public License as published by the Free 
11 // Software Foundation; either version 2 of the License, or (at your option) 
12 // any later version.
13 // 
14 // This program is distributed in the hope that it will be useful, but WITHOUT 
15 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
16 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
17 // more details.
18 // 
19 // You should have received a copy of the GNU General Public License along with
20 // this program; if not, write to the Free Software Foundation, Inc., 
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 //
23 // ----------------------------------------------------------------------------
24 //                                                                          
25 //####COPYRIGHTEND####
26 //=================================================================
27 //
28 //        eCosTestPlatform.cpp
29 //
30 //        platform information implementation
31 //
32 //=================================================================
33 #include "eCosTestPlatform.h"
34 #include "eCosTestUtils.h"
35 #include "eCosTrace.h"
36
37 std::vector<CeCosTestPlatform> CeCosTestPlatform::arPlatforms;
38
39 const CeCosTestPlatform *CeCosTestPlatform::Get(LPCTSTR psz) 
40
41   for(int i=0;i<(signed)arPlatforms.size();i++){
42     const CeCosTestPlatform &t=arPlatforms[i];
43     if(0==_tcsicmp(t.Name(),psz)){
44       return &t;
45     }
46   }
47   return NULL;
48 }
49
50 CeCosTestPlatform::CeCosTestPlatformProperties::CeCosTestPlatformProperties(CeCosTestPlatform *pti)
51 {
52   Add(_T("platform"),pti->m_strName);
53   Add(_T("prefix"),  pti->m_strPrefix);
54   Add(_T("commands"),pti->m_strCommands);
55   Add(_T("inferior"),pti->m_strInferior);
56   Add(_T("prompt"),  pti->m_strPrompt);
57   Add(_T("ServerSideGdb"),pti->m_nServerSideGdb);
58 }
59
60 bool CeCosTestPlatform::LoadFromDir(LPCTSTR pszDir)
61 {
62   bool rc=true;
63   TRACE(_T("CeCosTestPlatform::LoadFromDir %s\n"),pszDir);
64   // Find all the files in directory pszDir and load from each of them
65   TCHAR szOrigDir[256];
66   _tgetcwd(szOrigDir,sizeof szOrigDir-1);
67   if(0==_tchdir(pszDir)){
68     String strFile;
69     void *pHandle;
70     for(bool b=CeCosTestUtils::StartSearch(pHandle,strFile);b;b=CeCosTestUtils::NextFile(pHandle,strFile)){
71       if(CeCosTestUtils::IsFile(strFile)){
72         CeCosTestPlatform t;
73         t.m_strName=strFile;
74         CeCosTestPlatformProperties prop(&t);
75         if(prop.LoadFromFile(strFile)){
76           Add(t);
77         } else {
78           ERROR(_T("Illegal platform specification in %s%c%s\n"),pszDir,cPathsep,(LPCTSTR)strFile);
79           rc=false;
80         }
81       }
82     }
83     CeCosTestUtils::EndSearch(pHandle);
84   } else {
85     ERROR(_T("Failed to change to %s from %s\n"),pszDir,szOrigDir);
86   }
87   _tchdir(szOrigDir);
88
89   return rc;
90 }
91
92 #ifdef _WIN32
93 bool CeCosTestPlatform::SaveToRegistry(HKEY hTopKey,LPCTSTR pszKey)
94 {
95   // save target info to the registry
96
97   CProperties::CreateKey(pszKey,hTopKey);
98   HKEY hKey;
99   bool rc=ERROR_SUCCESS==RegOpenKeyEx (hTopKey, pszKey, 0L, KEY_ALL_ACCESS, &hKey);
100   if(rc){
101     for(int i=0;i<(signed)arPlatforms.size();i++){
102       HKEY hKey2;
103       DWORD dwDisp;
104       const CeCosTestPlatform &ti=arPlatforms[i];
105       rc&=(ERROR_SUCCESS==RegCreateKeyEx(hKey,ti.Name(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey2, &dwDisp));
106       if(rc){
107         LPCTSTR pszPrefix=ti.Prefix();
108         LPCTSTR pszGdb   =ti.GdbCmds();
109         LPCTSTR pszInferior=ti.Inferior();
110         LPCTSTR pszPrompt=ti.Prompt();
111         DWORD dwServerSideGdb=ti.ServerSideGdb()?1l:0l;
112         rc&=(ERROR_SUCCESS==RegSetValueEx(hKey2,_T("Prefix"),0,REG_SZ,   (CONST BYTE *)pszPrefix,(1+_tcslen(pszPrefix))*sizeof (TCHAR))) &&
113             (ERROR_SUCCESS==RegSetValueEx(hKey2,_T("Commands"),0,REG_SZ,   (CONST BYTE *)pszGdb,(1+_tcslen(pszGdb))*sizeof (TCHAR))) &&
114             (ERROR_SUCCESS==RegSetValueEx(hKey2,_T("Inferior"),0,REG_SZ,   (CONST BYTE *)pszInferior,(1+_tcslen(pszInferior))*sizeof (TCHAR))) &&
115             (ERROR_SUCCESS==RegSetValueEx(hKey2,_T("Prompt"),0,REG_SZ,   (CONST BYTE *)pszPrompt,(1+_tcslen(pszPrompt))*sizeof (TCHAR))) &&
116             (ERROR_SUCCESS==RegSetValueEx(hKey2,_T("ServerSideGdb"),0,REG_DWORD,   (CONST BYTE *)&dwServerSideGdb,sizeof (DWORD)));
117       }
118       RegCloseKey(hKey2);
119     }
120     RegCloseKey(hKey);
121   }
122   return rc;
123 }
124
125 const String CeCosTestPlatform::GetGreatestSubkey (LPCTSTR pszKey)
126 {
127   String strSubkey;
128   HKEY hKey;
129   
130   if (ERROR_SUCCESS == RegOpenKeyEx (HKEY_LOCAL_MACHINE, pszKey, 0L, KEY_READ, &hKey)) {
131     DWORD dwIndex = 0;
132     TCHAR pszBuffer [MAX_PATH + 1];
133     
134     while (ERROR_SUCCESS == RegEnumKey (hKey, dwIndex++, (LPTSTR) pszBuffer, sizeof (pszBuffer))) {
135       if (strSubkey.compare (pszBuffer) < 0) {
136         strSubkey = pszBuffer;
137       }
138     }
139     
140     RegCloseKey (hKey);
141   }
142   
143   TRACE (_T("CeCosTestPlatform::GetGreatestSubkey(\"%s\"): %s\n"), pszKey, (LPCTSTR)strSubkey);
144   return strSubkey;
145 }
146 #endif
147
148 bool CeCosTestPlatform::Load()
149 {
150   TRACE(_T("CeCosTestPlatform::Load\n"));
151   srand( (unsigned)time( NULL ) );
152   
153 #ifdef _WIN32
154
155   // get target info from the registry
156   String strPlatformsKey = _T("Software\\eCos Configuration Tool\\Platforms");
157 //  strPlatformsKey += GetGreatestSubkey (_T("Software\\eCos"));
158 //  strPlatformsKey += _T("\\Platforms");
159
160   HKEY hKey;
161   bool rc=ERROR_SUCCESS==RegOpenKeyEx (HKEY_CURRENT_USER, strPlatformsKey, 0L, KEY_READ, &hKey);
162   DWORD dwSubKeys=0;
163   if(rc){
164     // Found the given key.
165     // Subkeys' names are the target image names:
166     // Subkeys's values are:
167     //      Prefix  String
168     //      Type    String 
169     //      GdbCmd  String [optional]
170     FILETIME ftLastWriteTime;
171     DWORD dwMaxSubKeyLen;
172     if(ERROR_SUCCESS==RegQueryInfoKey(hKey,NULL,NULL,NULL,&dwSubKeys,&dwMaxSubKeyLen,NULL,NULL,NULL,NULL,NULL,NULL)){
173       TCHAR *szName=new TCHAR[1+dwMaxSubKeyLen];
174       DWORD dwSizeName=sizeof(TCHAR)*(1+dwMaxSubKeyLen);
175       for(DWORD dwIndex=0;ERROR_SUCCESS==RegEnumKeyEx(hKey, dwIndex, szName, &dwSizeName, NULL, NULL, NULL, &ftLastWriteTime); dwIndex++){
176         CeCosTestPlatform t;
177         if(t.LoadFromRegistry(hKey,szName)){
178           t.m_strName=szName;
179           CeCosTestPlatform::Add(t);
180         }
181         dwSizeName=sizeof(TCHAR)*(1+dwMaxSubKeyLen);
182       }
183       delete [] szName;
184     }
185     RegCloseKey(hKey);
186   }
187 #endif
188   const String strDir(CeCosTestUtils::HomeFile(_T(".eCosPlatforms")));
189 #ifdef _WIN32
190   if(!CeCosTestUtils::Exists(strDir)){
191     return true;
192   }
193 #endif
194   LoadFromDir(strDir);
195   if(0==Count()){
196     ERROR(_T("Failed to initialize any targets\n"));
197   }
198   return true;
199 }
200
201 int CeCosTestPlatform::Add(const CeCosTestPlatform &t)
202 {
203   for(std::vector<CeCosTestPlatform>::iterator it=arPlatforms.begin();it!=arPlatforms.end();){
204     if(0==_tcsicmp(it->Name(),t.Name())){
205       // Careful - there's already something here with this name:
206       ERROR(_T("Warning: duplicate target info %s\n"),it->Name());
207       it=arPlatforms.erase(it);
208     } else {
209       it++;
210     }
211   }
212   arPlatforms.push_back(t);
213   return arPlatforms.size()-1;
214 }
215
216 void CeCosTestPlatform::RemoveAllPlatforms()
217 {
218   arPlatforms.clear();
219 }
220
221 bool CeCosTestPlatform::LoadFromCommandString(LPCTSTR psz)
222 {
223   CeCosTestPlatformProperties prop(this);
224   return prop.LoadFromCommandString(psz);
225 }
226
227 #ifdef _WIN32
228 bool CeCosTestPlatform::LoadFromRegistry(HKEY hKey,LPCTSTR pszKey)
229 {
230   CeCosTestPlatformProperties prop(this);
231   return prop.LoadFromRegistry(hKey,pszKey);
232 }
233 #endif
234
235 bool CeCosTestPlatform::Save()
236 {
237   const String strDir(CeCosTestUtils::HomeFile(_T(".eCosPlatforms")));
238 #ifdef _WIN32
239   if(!CeCosTestUtils::Exists(strDir)){
240     String strPlatformsKey = _T("Software\\eCos Configuration Tool\\Platforms");
241 //    strPlatformsKey += GetGreatestSubkey (_T("Software\\eCos"));
242 //    strPlatformsKey += _T("\\Platforms");
243
244     return SaveToRegistry(HKEY_CURRENT_USER,strPlatformsKey);
245   }
246 #endif
247   return SaveToDir(strDir);
248 }
249
250 bool CeCosTestPlatform::SaveToDir (LPCTSTR pszDir)
251 {
252   bool rc=false;
253   void *pHandle;
254   TCHAR szOrigDir[256];
255   _tgetcwd(szOrigDir,sizeof szOrigDir-1);
256   if(0==_tchdir(pszDir)){
257     // Delete all the files under directory "pszDir"
258     String strFile;
259     for(bool b=CeCosTestUtils::StartSearch(pHandle,strFile);b;b=CeCosTestUtils::NextFile(pHandle,strFile)){
260       if(CeCosTestUtils::IsFile(strFile)){
261         _tunlink(strFile);
262       }
263     }
264     CeCosTestUtils::EndSearch(pHandle);
265     rc=true;
266     // Rewrite the files
267     for(int i=0;i<(signed)arPlatforms.size();i++){
268       CeCosTestPlatform &t=arPlatforms[i];
269       CeCosTestPlatformProperties prop(&t);
270       rc&=prop.SaveToFile(t.Name());
271     }
272   } else {
273     ERROR(_T("Failed to change to %s from %s\n"),pszDir,szOrigDir);
274   }
275   
276   return rc;
277 }
278
279