1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
6 // This program is part of the eCos host tools.
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)
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
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.
22 // ----------------------------------------------------------------------------
24 //####COPYRIGHTEND####
26 //===========================================================================
27 //===========================================================================
28 //#####DESCRIPTIONBEGIN####
35 // Description: This is a collection of utility functions.
36 // Modified by julians for wxWindows (originally CTUtils.h)
43 //####DESCRIPTIONEND####
45 //===========================================================================
48 #pragma implementation "ecutils.h"
51 // Includes other headers for precompiled compilation
59 #include "wx/listctrl.h"
60 #include "wx/stream.h"
62 #include <float.h> // for DBL_DIG macro
63 #include <sys/types.h>
76 #define INCLUDEFILE <string>
77 #include "IncludeSTL.h"
81 // Chop str into pieces, using cSep as separator.
82 // " and \ have usual semantics
83 // Return value is array of pieces found.
85 int ecUtils::Chop(const wxString& psz, wxArrayString &ar, wxChar cSep,
86 bool bObserveStrings/*=false*/,bool bBackslashQuotes/*=false*/)
91 return Chop(psz, ar, wxT("\t\n\v\f\r "), bObserveStrings, bBackslashQuotes);
93 wxASSERT(wxT('\0')!=cSep);
94 wxChar c[2]={cSep,wxT('\0')};
95 return Chop(psz,ar,wxString(c),bObserveStrings,bBackslashQuotes);
99 int ecUtils::Chop(const wxString& str, wxArrayString &ar, const wxString& sep,
100 bool bObserveStrings/*=false*/,bool bBackslashQuotes/*=false*/)
104 const wxChar* pszSep = (const wxChar*) sep;
105 const wxChar* psz = (const wxChar*) str;
109 // Skip multiple separators
110 while(*psz && wxStrchr(pszSep,*psz)){
118 bool bInString=FALSE;
120 if(*psz == wxT('\\') && bBackslashQuotes && psz[1]){
123 } else if(*psz == wxT('"')){
125 } else if (!bInString && *psz && NULL != wxStrchr(pszSep,*psz)) {
132 const wxChar* pszStart=psz;
135 } while (*psz && ! wxStrchr(pszSep,*psz));
136 strTok=wxString(pszStart,psz-pszStart);
141 return ar.GetCount();
146 // vararg-style message box formatter
147 int ecUtils::MessageBoxF (LPCTSTR pszFormat, ...)
151 va_start(args, pszFormat);
152 rc=ecUtils::vMessageBox(MB_OK, pszFormat,args);
157 // As above, but with type as first parameter.
158 int ecUtils::MessageBoxFT (UINT nType, LPCTSTR pszFormat, ...)
162 va_start(args, pszFormat);
163 rc=ecUtils::vMessageBox(nType, pszFormat,args);
168 int ecUtils::vMessageBox(UINT nType, LPCTSTR pszFormat, va_list marker)
171 for(int nLength=100;nLength;) {
172 TCHAR *buf=new TCHAR[1+nLength];
173 int n=_vsntprintf(buf, nLength, pszFormat, marker );
175 nLength*=2; // NT behavior
176 } else if (n<nLength){
177 rc=AfxMessageBox(buf,nType);
178 nLength=0; // trigger exit from loop
180 nLength=n+1; // UNIX behavior generally, or NT behavior when buffer size exactly matches required length
189 bool ecUtils::StrToItemIntegerType(const wxString & str, long &d)
194 bool bHex=(str.Len() > 2 && str[0]==wxT('0') && (str[1]==wxT('x')||str[1]==wxT('X')));
195 //d=_tcstol(str,&pEnd,bHex?16:10);
196 d=wxStrtol(str,&pEnd,bHex?16:10);
197 rc=(0==errno && (*pEnd==wxT('\0')));
201 const wxString ecUtils::IntToStr(long d,bool bHex)
204 s.Printf(bHex?wxT("0x%08x"):wxT("%d"),d);
208 const wxString ecUtils::DoubleToStr (double dValue)
211 s.Printf(wxT("%.*e"), DBL_DIG, dValue);
215 bool ecUtils::StrToDouble (const wxString & strValue, double &dValue)
219 //dValue = _tcstod (strValue, &pEnd);
220 dValue = wxStrtod(strValue, &pEnd);
221 return (0 == errno) && (*pEnd == wxT('\0'));
225 const wxString ecUtils::Explanation(CFileException & exc)
229 case CFileException::none: strMsg=wxT("No error occurred.");break;
230 case CFileException::generic: strMsg=wxT(" An unspecified error occurred.");break;
231 case CFileException::fileNotFound: strMsg=wxT(" The file could not be located.");break;
232 case CFileException::badPath: strMsg=wxT(" All or part of the path is invalid.");break;
233 case CFileException::tooManyOpenFiles: strMsg=wxT(" The permitted number of open files was exceeded.");break;
234 case CFileException::accessDenied: strMsg=wxT(" The file could not be accessed.");break;
235 case CFileException::invalidFile: strMsg=wxT(" There was an attempt to use an invalid file handle.");break;
236 case CFileException::removeCurrentDir: strMsg=wxT(" The current working directory cannot be removed.");break;
237 case CFileException::directoryFull: strMsg=wxT(" There are no more directory entries.");break;
238 case CFileException::badSeek: strMsg=wxT(" There was an error trying to set the file pointer.");break;
239 case CFileException::hardIO: strMsg=wxT(" There was a hardware error.");break;
240 case CFileException::sharingViolation: strMsg=wxT(" SHARE.EXE was not loaded, or a shared region was locked.");break;
241 case CFileException::lockViolation: strMsg=wxT(" There was an attempt to lock a region that was already locked.");break;
242 case CFileException::diskFull: strMsg=wxT(" The disk is full.");break;
243 case CFileException::endOfFile: strMsg=wxT(" The end of file was reached. ");break;
245 strMsg=wxT(" Unknown cause");
252 const wxString ecUtils::LoadString(UINT id)
260 bool ecUtils::AddToPath(const ecFileName & strFolder, bool bAtFront)
262 wxString strPath,strOldPath;
264 if (wxGetEnv(wxT("PATH"), & strOldPath))
266 // Place the user tools folders at the head or tail of the new path
269 strPath.Printf(wxT("%s;%s"), (const wxChar*) strFolder.ShortName(), (const wxChar*) strOldPath);
272 strPath.Printf(wxT("%s;%s"), (const wxChar*) strOldPath, (const wxChar*) strFolder.ShortName());
279 return (TRUE == wxSetEnv(wxT("PATH"),strPath));
283 wxString ecUtils::GetLastErrorMessageString()
288 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
291 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
297 // Display the string.
306 bool ecUtils::Launch(const ecFileName &strFileName, const ecFileName &strViewer)
310 if(!strViewer.IsEmpty())//use custom editor
312 wxString strCmdline(strViewer);
314 PTCHAR pszCmdLine=strCmdline.GetBuffer(strCmdline.GetLength());
315 GetShortPathName(pszCmdLine,pszCmdLine,strCmdline.GetLength());
316 strCmdline.ReleaseBuffer();
318 strCmdline+=_TCHAR(' ');
319 strCmdline+=strFileName;
320 PROCESS_INFORMATION pi;
323 si.cb = sizeof(STARTUPINFO);
324 si.lpReserved = NULL;
325 si.lpReserved2 = NULL;
333 //strCmdline.GetBuffer(strCmdline.GetLength()), // command line
334 strCmdline.GetBuffer(strCmdline.GetLength()), // command line
335 NULL, // process security
336 NULL, // thread security
337 TRUE, // inherit handles
343 CloseHandle(pi.hProcess);
344 CloseHandle(pi.hThread);
347 MessageBoxF(wxT("Failed to invoke %s.\n"),strCmdline);
349 strCmdline.ReleaseBuffer();
350 } else {// Use association
351 TCHAR szExe[MAX_PATH];
352 HINSTANCE h=FindExecutable(strFileName,wxT("."),szExe);
356 case 0: str=wxT("The system is out of memory or resources.");break;
357 case 31: str=wxT("There is no association for the specified file type.");break;
358 case ERROR_FILE_NOT_FOUND: str=wxT("The specified file was not found.");break;
359 case ERROR_PATH_NOT_FOUND: str=wxT("The specified path was not found.");break;
360 case ERROR_BAD_FORMAT: str=wxT("The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).");break;
363 MessageBoxF(wxT("Failed to open document %s.\r\n%s"),strFileName,str);
366 SHELLEXECUTEINFO sei = {sizeof(sei), 0, AfxGetMainWnd()->GetSafeHwnd(), wxT("open"),
367 strFileName, NULL, NULL, SW_SHOWNORMAL, AfxGetInstanceHandle( )};
370 HINSTANCE hInst=ShellExecute(AfxGetMainWnd()->GetSafeHwnd(),wxT("open"), strFileName, NULL, wxT("."), 0)/*ShellExecuteEx(&sei)*/;
371 if(int(hInst)<=32/*sei.hInstApp==0*/)
376 case 0 : str=wxT("The operating system is out of memory or resources. ");break;
377 case ERROR_FILE_NOT_FOUND : str=wxT("The specified file was not found. ");break;
378 case ERROR_PATH_NOT_FOUND : str=wxT("The specified path was not found. ");break;
379 case ERROR_BAD_FORMAT : str=wxT("The .EXE file is invalid (non-Win32 .EXE or error in .EXE image). ");break;
380 case SE_ERR_ACCESSDENIED : str=wxT("The operating system denied access to the specified file. ");break;
381 case SE_ERR_ASSOCINCOMPLETE : str=wxT("The filename association is incomplete or invalid. ");break;
382 case SE_ERR_DDEBUSY : str=wxT("The DDE transaction could not be completed because other DDE transactions were being processed. ");break;
383 case SE_ERR_DDEFAIL : str=wxT("The DDE transaction failed. ");break;
384 case SE_ERR_DDETIMEOUT : str=wxT("The DDE transaction could not be completed because the request timed out. ");break;
385 case SE_ERR_DLLNOTFOUND : str=wxT("The specified dynamic-link library was not found. ");break;
386 //case SE_ERR_FNF : str=wxT("The specified file was not found. ");break;
387 case SE_ERR_NOASSOC : str=wxT("There is no application associated with the given filename extension. ");break;
388 case SE_ERR_OOM : str=wxT("There was not enough memory to complete the operation. ");break;
389 //case SE_ERR_PNF : str=wxT("The specified path was not found. ");break;
390 case SE_ERR_SHARE : str=wxT("A sharing violation occurred. ");break;
391 default: str=wxT("An unexpected error occurred");break;
393 MessageBoxF(wxT("Failed to open document %s using %s.\r\n%s"),strFileName,szExe,str);
404 void ecUtils::UnicodeToCStr(const wxChar* str,char *&psz)
406 int nLength=1 + wxStrlen(str);
407 psz=new char[nLength];
409 WideCharToMultiByte(CP_ACP, 0, str, -1, psz, nLength, NULL, NULL);
415 std::string ecUtils::UnicodeToStdStr(const wxChar* str)
419 UnicodeToCStr(str,psz);
420 stdstr=std::string(psz);
425 // ecUtils::StripExtraWhitespace() returns a modified version of
426 // a string in which each sequence of whitespace characters is
427 // replaced by a single space
429 static bool ecIsSpace(wxChar ch)
431 return (ch == wxT(' ') || ch == wxT('\r') || ch == wxT('\n') || ch == wxT('\t'));
434 wxString ecUtils::StripExtraWhitespace (const wxString & strInput)
437 wxChar* o=strOutput.GetWriteBuf(1+strInput.Len());
438 for(const wxChar* c=strInput.GetData();*c;c++){
441 if (ecIsSpace(c[1])){
442 for(c=c+2; ecIsSpace(*c);c++);
450 strOutput.UngetWriteBuf();
451 strOutput.Trim(TRUE);
452 strOutput.Trim(FALSE);
456 LPTSTR o=strOutput.GetBuffer(1+strInput.GetLength());
457 for(LPCTSTR c=strInput;*c;c++){
460 if (_istspace(c[1])){
461 for(c=c+2;_istspace(*c);c++);
469 strOutput.ReleaseBuffer();
470 strOutput.TrimLeft();
471 strOutput.TrimRight();
477 ecFileName ecUtils::WPath(const std::string &str)
479 // Convert a path as read from cdl into host format
480 // Change / to \ throughout
482 strPath(str.c_str());
483 strPath.Replace (_TCHAR('/'), _TCHAR('\\'));
487 // Copy file helper function.
488 // This makes sure the destination file is only touched as necessary.
489 // It is written using Posix calls lest it should be more broadly applicable.
491 bool ecUtils::CopyFile(LPCTSTR pszSource,LPCTSTR pszDest)
493 // Compare the files. First set rc to the result of the comparison (true if the same)
497 if(-1!=_tstat(pszSource,&s1) && -1!=_tstat(pszDest,&s2) && s1.st_size==s2.st_size){
498 // Files both exist and are of equal size
499 FILE *f1=_tfopen(pszSource,wxT("rb"));
501 FILE *f2=_tfopen(pszDest,wxT("rb"));
506 char buf1[4096],buf2[4096];
507 nSize1=fread(buf1,1,sizeof buf1,f1);
508 nSize2=fread(buf2,1,sizeof buf2,f2);
509 if(nSize1!=nSize2 || 0!=memcmp(buf1,buf2,nSize1)){
521 // Files are identical
523 rc=TRUE==::CopyFile(pszSource,pszDest,FALSE);
526 MessageBoxF(wxT("Failed to copy '%s' to '%s' - %s"),pszSource,pszDest,GetLastErrorMessageString());
536 * wxStringToStringMap
538 * Stores string values keyed by strings
541 void wxStringToStringMap::Set(const wxString& key, const wxString& value)
544 if (Find(key, oldValue))
546 m_hashTable.Put(key, (wxObject*) new wxString(value));
549 bool wxStringToStringMap::Remove(const wxString& key)
551 wxString* str = (wxString*) m_hashTable.Delete(key);
561 bool wxStringToStringMap::Find(const wxString& key, wxString& value)
563 wxString* str = (wxString*) m_hashTable.Get(key);
573 void wxStringToStringMap::Clear()
575 m_hashTable.BeginFind();
577 while ((node = m_hashTable.Next()))
579 wxString* str = (wxString*) node->Data();
584 void wxStringToStringMap::BeginFind()
586 m_hashTable.BeginFind();
589 bool wxStringToStringMap::Next(wxString& key, wxString& value)
591 wxNode* node = m_hashTable.Next();
594 value = * (wxString*) node->Data();
601 // Is str a member of arr?
602 bool wxArrayStringIsMember(const wxArrayString& arr, const wxString& str)
605 for (i = (size_t) 0; i < arr.GetCount(); i++)
612 // Eliminate .. and .
613 wxString wxGetRealPath(const wxString& path)
615 wxChar* p = new wxChar[path.Len() + 1];
616 wxStrcpy(p, (const wxChar*) path);
624 // A version of the above but prepending 'cwd' (current path) first
625 // if 'path' is relative
626 wxString wxGetRealPath(const wxString& cwd, const wxString& path)
628 wxString path1(path);
630 if (!wxIsAbsolutePath(path))
633 if (path1.Last() != wxFILE_SEP_PATH)
634 path1 += wxFILE_SEP_PATH;
638 return wxGetRealPath(path1);
641 // Find the absolute path where this application has been run from.
642 // argv0 is wxTheApp->argv[0]
643 // cwd is the current working directory (at startup)
644 // appVariableName is the name of a variable containing the directory for this app, e.g.
645 // MYAPPDIR. This is checked first.
647 wxString wxFindAppPath(const wxString& argv0, const wxString& cwd, const wxString& appVariableName)
651 // Try appVariableName
652 if (!appVariableName.IsEmpty())
654 str = wxGetenv(appVariableName);
659 if (wxIsAbsolutePath(argv0))
660 return wxPathOnly(argv0);
663 // Is it a relative path?
664 wxString currentDir(cwd);
665 if (currentDir.Last() != wxFILE_SEP_PATH)
666 currentDir += wxFILE_SEP_PATH;
668 str = currentDir + argv0;
669 if (wxFileExists(str))
670 return wxPathOnly(str);
673 // OK, it's neither an absolute path nor a relative path.
677 pathList.AddEnvList(wxT("PATH"));
678 str = pathList.FindAbsoluteValidPath(argv0);
680 return wxPathOnly(str);
683 return wxEmptyString;
686 // Make a path name with no separators, out of a full pathname,
687 // e.g. opt_ecos_ecos-1.4.5 out of /opt/ecos/ecos-1.4.5
688 wxString ecMakeNameFromPath(const wxString& path)
691 p.Replace(wxT("/"), wxT("_"));
692 p.Replace(wxT("\\"), wxT("_"));
693 p.Replace(wxT(":"), wxT("_"));
698 // Find the text of the list control item at the given column
699 wxString wxListCtrlGetItemTextColumn(wxListCtrl& listCtrl, long item, int col)
702 listItem.m_mask = wxLIST_MASK_TEXT;
703 listItem.m_itemId = item;
704 listItem.m_col = col;
706 if (listCtrl.GetItem(listItem))
707 return listItem.m_text;
709 return wxEmptyString;
712 // Select the given item
713 void wxListCtrlSelectItem(wxListCtrl& listCtrl, long sel, bool deselectOthers)
715 long n = listCtrl.GetItemCount();
719 for (i = 0; i < n; i++)
721 if (listCtrl.GetItemState(i, wxLIST_STATE_SELECTED) & wxLIST_STATE_SELECTED)
723 listCtrl.SetItemState(i, wxLIST_STATE_SELECTED, 0);
727 listCtrl.SetItemState(sel, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
730 // Find the selection
731 long wxListCtrlGetSelection(wxListCtrl& listCtrl)
733 long n = listCtrl.GetItemCount();
735 for (i = 0; i < n; i++)
737 if (listCtrl.GetItemState(i, wxLIST_STATE_SELECTED) & wxLIST_STATE_SELECTED)
745 // Find which column the cursor is on
746 int wxListCtrlFindColumn(wxListCtrl& listCtrl, int noCols, int x)
750 // Find which column we're on
753 for (i = 0; i < noCols; i++)
755 width += listCtrl.GetColumnWidth(i);
766 void wxRefreshControls(wxWindow* win)
768 wxNode *node = win->GetChildren().First();
771 wxWindow* win = (wxWindow*) node->Data();
777 wxOutputStream& operator <<(wxOutputStream& stream, const wxString& s)
779 stream.Write(s, s.Length());
783 wxOutputStream& operator <<(wxOutputStream& stream, long l)
786 str.Printf("%ld", l);
787 return stream << str;
790 wxOutputStream& operator <<(wxOutputStream& stream, const char c)
794 return stream << str;
799 * Supports features we want to have for all dialogs in the application.
800 * So far, this just allows dialogs to be resizeable under MSW by
801 * refreshing the controls in OnSize (otherwise there's a mess)
804 IMPLEMENT_CLASS(ecDialog, wxDialog)
806 BEGIN_EVENT_TABLE(ecDialog, wxDialog)
808 EVT_SIZE(ecDialog::OnSize)
812 void ecDialog::OnSize(wxSizeEvent& event)
814 wxDialog::OnSize(event);
816 wxRefreshControls(this);
820 * Implements saving/loading of window settings - fonts only for now
823 wxWindowSettingsObject* wxWindowSettings::FindSettings(const wxString& windowName) const
825 wxNode* node = m_settings.First();
828 wxWindowSettingsObject* obj = (wxWindowSettingsObject*) node->Data();
829 if (obj->m_windowName.CmpNoCase(windowName) == 0)
836 bool wxWindowSettings::LoadConfig(wxConfigBase& config)
839 for (i = 0; i < GetCount(); i++)
841 wxWindowSettingsObject* obj = GetNth(i);
843 wxString name(obj->m_windowName);
844 name.Replace(wxT(" "), wxT(""));
846 LoadFont(config, name, obj->m_font);
852 bool wxWindowSettings::SaveConfig(wxConfigBase& config)
855 for (i = 0; i < GetCount(); i++)
857 wxWindowSettingsObject* obj = GetNth(i);
859 wxString name(obj->m_windowName);
860 name.Replace(wxT(" "), wxT(""));
862 SaveFont(config, name, obj->m_font);
868 // Load and save font descriptions
869 bool wxWindowSettings::LoadFont(wxConfigBase& config, const wxString& windowName, wxFont& font)
871 wxString pathBase(wxT("/Fonts/"));
872 pathBase += windowName;
873 pathBase += wxT("/");
875 int pointSize, family, style, weight;
876 bool underlined = FALSE;
879 if (!config.Read(pathBase + wxT("PointSize"), & pointSize))
882 if (!config.Read(pathBase + wxT("Family"), & family))
885 if (!config.Read(pathBase + wxT("Style"), & style))
888 if (!config.Read(pathBase + wxT("Weight"), & weight))
891 config.Read(pathBase + wxT("Underlined"), (bool*) & underlined);
892 config.Read(pathBase + wxT("FaceName"), & faceName);
894 wxFont font1(pointSize, family, style, weight, underlined, faceName);
900 bool wxWindowSettings::SaveFont(wxConfigBase& config, const wxString& windowName, const wxFont& font)
905 wxString pathBase(wxT("/Fonts/"));
906 pathBase += windowName;
907 pathBase += wxT("/");
909 config.Write(pathBase + wxT("PointSize"), (long) font.GetPointSize());
910 config.Write(pathBase + wxT("Family"), (long) font.GetFamily());
911 config.Write(pathBase + wxT("Style"), (long) font.GetStyle());
912 config.Write(pathBase + wxT("Weight"), (long) font.GetWeight());
913 config.Write(pathBase + wxT("Underlined"), (long) font.GetUnderlined());
914 config.Write(pathBase + wxT("FaceName"), font.GetFaceName());
919 wxFont wxWindowSettings::GetFont(const wxString& name) const
921 wxWindowSettingsObject* obj = FindSettings(name);
928 void wxWindowSettings::SetFont(const wxString& name, const wxFont& font)
930 wxWindowSettingsObject* obj = FindSettings(name);
933 obj = new wxWindowSettingsObject(name, NULL) ;
935 m_settings.Append(obj);
940 wxWindow* wxWindowSettings::GetWindow(const wxString& name) const
942 wxWindowSettingsObject* obj = FindSettings(name);
945 if (obj->m_arrWindow.GetCount() > 0)
946 return (wxWindow*) obj->m_arrWindow[0];
951 void wxWindowSettings::SetWindow(const wxString& name, wxWindow* win)
953 wxWindowSettingsObject* obj = FindSettings(name);
956 obj = new wxWindowSettingsObject(name, win) ;
957 m_settings.Append(obj);
959 obj->m_arrWindow.Clear();
962 obj->m_arrWindow.Add(win);
965 wxArrayPtrVoid* wxWindowSettings::GetWindows(const wxString& name) const
967 wxWindowSettingsObject* obj = FindSettings(name);
970 return & obj->m_arrWindow ;
973 void wxWindowSettings::SetWindows(const wxString& name, wxArrayPtrVoid& arr)
975 wxWindowSettingsObject* obj = FindSettings(name);
978 obj = new wxWindowSettingsObject(name, NULL) ;
979 m_settings.Append(obj);
981 obj->m_arrWindow.Clear() ;
982 obj->m_arrWindow = arr;
985 bool wxWindowSettings::ApplyFontsToWindows()
991 for (i = 0; i < GetCount(); i++)
993 wxWindowSettingsObject* obj = GetNth(i);
996 for (j = 0; j < obj->m_arrWindow.GetCount(); j++)
998 wxWindow* win = (wxWindow*) obj->m_arrWindow[j];
999 win->SetFont(obj->m_font);
1007 // This will be obsolete when we switch to using the version included
1008 // in wxWindows (from wxWin 2.3.1 onwards)
1011 ecKILL_OK, // no error
1012 ecKILL_BAD_SIGNAL, // no such signal
1013 ecKILL_ACCESS_DENIED, // permission denied
1014 ecKILL_NO_PROCESS, // no such process
1015 ecKILL_ERROR // another, unspecified error
1019 // ----------------------------------------------------------------------------
1020 // process management
1021 // ----------------------------------------------------------------------------
1025 // structure used to pass parameters from wxKill() to wxEnumFindByPidProc()
1026 struct wxNewFindByPidParams
1028 wxNewFindByPidParams() { hwnd = 0; pid = 0; }
1030 // the HWND used to return the result
1033 // the PID we're looking from
1037 // wxKill helper: EnumWindows() callback which is used to find the first (top
1038 // level) window belonging to the given process
1039 static BOOL CALLBACK wxEnumFindByPidProc(HWND hwnd, LPARAM lParam)
1042 (void)::GetWindowThreadProcessId(hwnd, &pid);
1044 wxNewFindByPidParams *params = (wxNewFindByPidParams *)lParam;
1045 if ( pid == params->pid )
1047 // remember the window we found
1048 params->hwnd = hwnd;
1050 // return FALSE to stop the enumeration
1054 // continue enumeration
1058 // This will be obsolete when we switch to using the version included
1059 // in wxWindows (from wxWin 2.3.1 onwards)
1060 int wxNewKill(long pid, wxSignal sig, ecKillError *krc = NULL)
1063 // get the process handle to operate on
1064 HANDLE hProcess = ::OpenProcess(SYNCHRONIZE |
1066 PROCESS_QUERY_INFORMATION,
1067 FALSE, // not inheritable
1069 if ( hProcess == NULL )
1073 if ( ::GetLastError() == ERROR_ACCESS_DENIED )
1075 *krc = ecKILL_ACCESS_DENIED;
1079 *krc = ecKILL_NO_PROCESS;
1090 // kill the process forcefully returning -1 as error code
1091 if ( !::TerminateProcess(hProcess, (UINT)-1) )
1093 wxLogSysError(_("Failed to kill process %d"), pid);
1097 // this is not supposed to happen if we could open the
1099 *krc = ecKILL_ERROR;
1107 // do nothing, we just want to test for process existence
1111 // any other signal means "terminate"
1113 wxNewFindByPidParams params;
1114 params.pid = (DWORD)pid;
1116 // EnumWindows() has nice semantics: it returns 0 if it found
1117 // something or if an error occured and non zero if it
1118 // enumerated all the window
1119 if ( !::EnumWindows(wxEnumFindByPidProc, (LPARAM)¶ms) )
1121 // did we find any window?
1124 // tell the app to close
1126 // NB: this is the harshest way, the app won't have
1127 // opportunity to save any files, for example, but
1128 // this is probably what we want here. If not we
1129 // can also use SendMesageTimeout(WM_CLOSE)
1130 if ( !::PostMessage(params.hwnd, WM_QUIT, 0, 0) )
1132 wxLogLastError(_T("PostMessage(WM_QUIT)"));
1135 else // it was an error then
1137 wxLogLastError(_T("EnumWindows"));
1142 else // no windows for this PID
1146 *krc = ecKILL_ERROR;
1159 // as we wait for a short time, we can use just WaitForSingleObject()
1160 // and not MsgWaitForMultipleObjects()
1161 switch ( ::WaitForSingleObject(hProcess, 500 /* msec */) )
1164 // process terminated
1165 if ( !::GetExitCodeProcess(hProcess, &rc) )
1167 wxLogLastError(_T("GetExitCodeProcess"));
1172 wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") );
1176 wxLogLastError(_T("WaitForSingleObject"));
1182 *krc = ecKILL_ERROR;
1191 // just to suppress the warnings about uninitialized variable
1195 ::CloseHandle(hProcess);
1197 // the return code is the same as from Unix kill(): 0 if killed
1198 // successfully or -1 on error
1199 if ( sig == wxSIGNONE )
1201 if ( ok && rc == STILL_ACTIVE )
1203 // there is such process => success
1209 if ( ok && rc != STILL_ACTIVE )
1211 // killed => success
1216 wxFAIL_MSG( _T("not implemented") );
1217 #endif // Win32/Win16
1224 int ecKill(long pid, wxSignal sig)
1226 #if defined(__UNIX__) && !defined(__CYGWIN__)
1227 return wxKill(pid, sig);
1228 #elif defined(__WXMSW__)
1229 return wxNewKill(pid, sig);
1236 #include <tlhelp32.h>
1238 WXHINSTANCE wxProcessKiller::hInstLib1 = VER_PLATFORM_WIN32_NT==wxProcessKiller::GetPlatform()? (WXHINSTANCE) LoadLibrary(_T("PSAPI.DLL")) : (WXHINSTANCE) LoadLibrary(_T("Kernel32.DLL")) ;
1239 WXHINSTANCE wxProcessKiller::hInstLib2 = VER_PLATFORM_WIN32_NT==wxProcessKiller::GetPlatform()? (WXHINSTANCE) LoadLibrary(_T("NTDLL.DLL")): (WXHINSTANCE) NULL;
1243 const unsigned int wxProcessKiller::PROCESS_KILL_EXIT_CODE=0xCCFFCCFF;
1245 wxProcessKiller::wxProcessKiller(int pid):
1251 m_hProcess = (WXHANDLE) ::OpenProcess(SYNCHRONIZE |
1253 PROCESS_QUERY_INFORMATION,
1254 FALSE, // not inheritable
1255 (DWORD) m_idProcess);
1259 wxProcessKiller::~wxProcessKiller()
1264 ::CloseHandle((HANDLE) m_hProcess);
1269 bool wxProcessKiller::Kill(bool bRecurse)
1271 wxPInfoArray arPinfo;
1273 if(m_idProcess && -1!=m_idProcess){
1274 // Start of with the easy one:
1276 // Need to gather this information before we orphan our grandchildren:
1283 rc=(TRUE==::TerminateProcess((HANDLE) m_hProcess,PROCESS_KILL_EXIT_CODE));
1284 // dtor's (or subsequent Run's) responsibility to close the handle
1288 rc=(0==kill(m_idProcess,SIGTERM));
1290 waitpid(m_idProcess,&status,WNOHANG);
1294 // kill process *and* its children
1295 // FIXME: needs to be top-down
1296 for(int i=0;i<(signed)arPinfo.size();i++){
1297 if(arPinfo[i].IsChildOf(m_idProcess)){
1302 const wxString strName(Name(arPinfo[i].PID));
1303 if(_tcsstr(strName,_T("eCosTest")) || _tcsstr(strName,_T("cmd.EXE")) || _tcsstr(strName,_T("CMD.EXE")) || arPinfo[i].PID==(signed)GetCurrentProcessId()){
1307 HANDLE hProcess=::OpenProcess(PROCESS_TERMINATE,false,arPinfo[i].PID);
1309 rc&=(TRUE==::TerminateProcess(hProcess,PROCESS_KILL_EXIT_CODE));
1310 CloseHandle(hProcess);
1315 rc&=(0==kill(arPinfo[i].PID,SIGTERM));
1317 waitpid(arPinfo[i].PID,&status,WNOHANG);
1327 bool wxProcessKiller::PSExtract(wxProcessKiller::wxPInfoArray &arPinfo)
1332 switch(GetPlatform()) {
1333 case VER_PLATFORM_WIN32_NT:
1336 // Get procedure addresses.
1337 static BOOL (WINAPI *lpfEnumProcesses)( DWORD *, DWORD cb, DWORD * ) = (BOOL(WINAPI *)(DWORD *,DWORD,DWORD*))GetProcAddress( (HINSTANCE) hInstLib1, "EnumProcesses" ) ;
1338 if( lpfEnumProcesses) {
1342 static DWORD (WINAPI *lpfNtQueryInformationProcess)( HANDLE, int, void *, DWORD, LPDWORD ) =
1343 (DWORD(WINAPI *)(HANDLE, int, void *, DWORD, LPDWORD)) GetProcAddress( (HINSTANCE) hInstLib2,"NtQueryInformationProcess" ) ;
1345 if(lpfNtQueryInformationProcess){
1346 DWORD dwMaxPids=256;
1348 DWORD *arPids = NULL ;
1351 arPids=new DWORD[dwMaxPids];
1352 } while(lpfEnumProcesses(arPids, dwMaxPids, &dwPidSize) && dwPidSize/sizeof(DWORD)==dwMaxPids) ;
1354 if(dwPidSize/sizeof(DWORD)<dwMaxPids){
1356 for( DWORD dwIndex = 0 ; (signed)dwIndex < dwPidSize/sizeof(DWORD); dwIndex++ ) {
1357 // Regardless of OpenProcess success or failure, we
1358 // still call the enum func with the ProcID.
1359 DWORD pid=arPids[dwIndex];
1360 HANDLE hProcess=::OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, pid );
1363 DWORD ExitStatus; // receives process termination status
1364 DWORD PebBaseAddress; // receives process environment block address
1365 DWORD AffinityMask; // receives process affinity mask
1366 DWORD BasePriority; // receives process priority class
1367 ULONG UniqueProcessId; // receives process identifier
1368 ULONG InheritedFromUniqueProcessId; // receives parent process identifier
1370 memset( &pbi, 0, sizeof(pbi));
1372 __int64 ftCreation,ftExit,ftKernel,ftUser;
1373 if(lpfNtQueryInformationProcess(hProcess, 0 /*ProcessBasicInformation*/, &pbi, sizeof(pbi), &retLen)>=0 &&
1374 TRUE==::GetProcessTimes (hProcess,(FILETIME *)&ftCreation,(FILETIME *)&ftExit,(FILETIME *)&ftKernel,(FILETIME *)&ftUser)){
1375 // The second test is important. It excludes orphaned processes who appear to have been adopted by virtue of a new
1376 // process having been created with the same ID as their original parent.
1379 p.PPID=pbi.InheritedFromUniqueProcessId;
1380 p.tCreation=ftCreation;
1381 p.tCpu=Time((ftKernel+ftUser)/10000);
1382 arPinfo.push_back(p);
1385 CloseHandle(hProcess);
1396 case VER_PLATFORM_WIN32_WINDOWS:
1400 static HANDLE (WINAPI *lpfCreateToolhelp32Snapshot)(DWORD,DWORD)=
1401 (HANDLE(WINAPI *)(DWORD,DWORD))GetProcAddress( (HINSTANCE) hInstLib1,"CreateToolhelp32Snapshot" ) ;
1402 static BOOL (WINAPI *lpfProcess32First)(HANDLE,LPPROCESSENTRY32)=
1403 (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))GetProcAddress( (HINSTANCE) hInstLib1, "Process32First" ) ;
1404 static BOOL (WINAPI *lpfProcess32Next)(HANDLE,LPPROCESSENTRY32)=
1405 (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))GetProcAddress( (HINSTANCE) hInstLib1, "Process32Next" ) ;
1406 if( lpfProcess32Next && lpfProcess32First && lpfCreateToolhelp32Snapshot) {
1408 // Get a handle to a Toolhelp snapshot of the systems
1410 HANDLE hSnapShot = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) ;
1411 if(INVALID_HANDLE_VALUE != hSnapShot) {
1412 // Get the first process' information.
1413 PROCESSENTRY32 procentry;
1414 procentry.dwSize = sizeof(PROCESSENTRY32) ;
1415 if(lpfProcess32First( hSnapShot, &procentry )){
1419 p.PID=procentry.th32ProcessID;
1420 p.PPID=procentry.th32ParentProcessID;
1421 arPinfo.push_back(p);
1422 } while(lpfProcess32Next( hSnapShot, &procentry ));
1424 CloseHandle(hSnapShot);
1433 SetParents(arPinfo);
1436 wxLogError(_T("Couldn't get process information!\n"));
1443 bool wxProcessKiller::PSExtract(wxProcessKiller::wxPInfoArray &arPinfo)
1447 FILE *f=popen("ps -l",_T("r") MODE_TEXT);
1450 while(fgets(buf,sizeof(buf)-1,f)){
1453 // Output is in the form
1454 // F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
1455 //100 S 490 877 876 0 70 0 - 368 wait4 pts/0 00:00:00 bash
1456 int F,UID,C,PRI,NI,SZ,HH,MM,SS;
1457 bool rc=(15==_stscanf(buf,_T("%d %s %d %d %d %d %d %d %s %d %s %s %d:%d:%d"),&F,discard,&UID,&p.PID,&p.PPID,&C,&PRI,&NI,discard,&SZ,discard,discard,&HH,&MM,&SS));
1459 p.tCpu=1000*(SS+60*(60*HH+MM));
1460 arPinfo.push_back(p);
1464 for(i=0;i<(signed)arPinfo.size();i++){
1465 int pid=arPinfo[i].PPID;
1466 arPinfo[i].pParent=0;
1467 for(int j=0;j<(signed)arPinfo.size();j++){
1468 if(i!=j && arPinfo[j].PID==pid){
1469 arPinfo[i].pParent=&arPinfo[j];
1475 wxLogError(_T("Failed to run ps -l\n"));
1477 return true; //FIXME
1482 void wxProcessKiller::SetParents(wxProcessKiller::wxPInfoArray &arPinfo)
1485 for(i=0;i<(signed)arPinfo.size();i++){
1486 wxPInfo &p=arPinfo[i];
1488 for(int j=0;j<(signed)arPinfo.size();j++){
1489 if(arPinfo[j].PID==p.PPID
1491 && arPinfo[j].tCreation<p.tCreation
1495 arPinfo[i].pParent=&arPinfo[j];
1501 // Check for circularity
1502 bool bCircularity=false;
1503 for(i=0;i<(signed)arPinfo.size();i++){
1504 wxPInfo *p=&arPinfo[i];
1505 for(int j=0;j<(signed)arPinfo.size() && p;j++){
1508 // If all is well, p should be NULL here. Otherwise we have a loop.
1510 // Make sure it can't foul things up:
1511 arPinfo[i].pParent=0;
1517 wxLogError(_T("!!! Circularly linked process list at index %d\n"),i);
1518 for(int k=0;k<(signed)arPinfo.size();k++){
1519 const wxPInfo &p=arPinfo[k];
1520 wxLogError(_T("%d: %s ppid=%4d\n"),k,(LPCTSTR)Name(p.PID),p.PPID);
1525 bool wxProcessKiller::wxPInfo::IsChildOf(int pid) const
1527 for(wxPInfo *p=pParent;p && p!=this;p=p->pParent) { // guard against circular linkage
1535 const wxString wxProcessKiller::Name(int pid)
1538 str.Printf(_T("id=%d"),pid);
1541 if(VER_PLATFORM_WIN32_NT==GetPlatform() && hInstLib1){
1542 static BOOL (WINAPI *lpfEnumProcessModules)( HANDLE, HMODULE *, DWORD, LPDWORD ) =
1543 (BOOL(WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD)) GetProcAddress( (HINSTANCE) hInstLib1,"EnumProcessModules" ) ;
1544 static DWORD (WINAPI *lpfGetModuleFileNameEx)( HANDLE, HMODULE, LPTSTR, DWORD )=
1545 (DWORD (WINAPI *)(HANDLE, HMODULE,LPTSTR, DWORD )) GetProcAddress( (HINSTANCE) hInstLib1,"GetModuleFileNameExA" ) ;
1546 if( lpfEnumProcessModules && lpfGetModuleFileNameEx ) {
1547 HANDLE hProcess=::OpenProcess(PROCESS_ALL_ACCESS,false,pid);
1551 if(lpfEnumProcessModules( hProcess, &hMod, sizeof(HMODULE), &dwSize ) ){
1552 // Get Full pathname:
1553 TCHAR buf[1+MAX_PATH];
1554 lpfGetModuleFileNameEx( hProcess, hMod, buf, MAX_PATH);
1558 CloseHandle(hProcess);
1568 long wxProcessKiller::GetPlatform()
1570 OSVERSIONINFO osver;
1571 osver.dwOSVersionInfoSize = sizeof( osver ) ;
1572 return GetVersionEx( &osver ) ? (long) osver.dwPlatformId : (long)-1;
1576 const wxString wxProcessKiller::ErrorString() const
1581 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
1584 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
1591 return strerror(errno);