-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapexec.cpp
1615 lines (1328 loc) · 35.5 KB
/
wrapexec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Wrapper of Execute
by opa
*/
#include <string>
#include <list>
#include <map>
#include <algorithm>
#include <cstdio>
#include <dir>
#include <io>
#include <windows.h>
#include <shlobj.h>
#include <lmcons.h>
#define PGM "wrapexec"
#define PGM_DEBUG PGM ": "
#define PGM_INFO PGM ": "
#define PGM_WARN PGM " warning: "
#define PGM_ERR PGM " error: "
#define VERSTR "1.04"
#define CREDIT2009 "Copyright (c) 2009,2011 by opa"
typedef signed char schar;
typedef unsigned char uchar;
typedef signed int sint;
typedef unsigned int uint;
typedef signed long slong;
typedef unsigned long ulong;
using namespace std;
char
credit[] = PGM " version " VERSTR " " CREDIT2009;
bool
error = false;
sint
rcode = 0;
////////////////////////////////////////////////////////////////////////
template <class BidirectionalIterator, class T>
BidirectionalIterator rfind(BidirectionalIterator first, BidirectionalIterator last, const T &value)
{
while(first != last){
--last;
if(*last == value)
break;
}
return last;
}
template <class BidirectionalIterator, class Predicate>
BidirectionalIterator rfind_if(BidirectionalIterator first, BidirectionalIterator last, Predicate pred)
{
while(first != last){
--last;
if(pred(*last))
break;
}
return last;
}
inline bool isnotwspace(wchar_t c)
{
return !iswspace(c);
}
inline bool isbackslash(wchar_t c)
{
return c == L'\\' || c == L'/';
}
bool file_is_exist(const wchar_t *filename)
{
_wffblk ff;
sint r;
r = _wfindfirst(filename, &ff, FA_NORMAL + FA_HIDDEN + FA_SYSTEM);
_wfindclose(&ff);
return r == 0;
}
inline bool file_is_exist(const wstring &filename)
{
return file_is_exist(filename.c_str());
}
bool file_is_readable(const wchar_t *filename)
{
FILE
*fp = _wfopen(filename, L"rb");
if(fp != NULL){
fclose(fp);
return true;
}
return false;
}
inline bool file_is_readable(const wstring &filename)
{
return file_is_readable(filename.c_str());
}
inline bool file_is_executable(const wstring &filename)
{
return file_is_readable(filename.c_str());
}
////////////////////////////////////////////////////////////////////////
class String : public wstring {
typedef String Self;
typedef wstring Super;
public:
String();
String(const Super &s);
String(const wchar_t *s);
String(const_iterator b, const_iterator e);
String(const char *s);
~String();
string to_ansi() const;
Self to_upper() const;
Self trim() const;
bool isdoublequote() const;
Self doublequote() const;
Self doublequote_del() const;
Self &operator=(const Self &s);
Self &operator=(const wchar_t *s);
Self &operator=(const char *s);
Self &operator+=(const Self &s) { append(s); return *this; }
Self &operator+=(const wchar_t *s) { append(s); return *this; }
Self &operator+=(const char *s);
Self &assign_from_ansi(const char *s);
Self &assign_from_ansi(const string &s) { return assign_from_ansi(s.c_str()); }
Self &assign_from_utf8(const char *s);
Self &assign_from_utf8(const string &s) { return assign_from_utf8(s.c_str()); }
Self &assign_from_env(const Self &name);
Self &printf(Self format, ...);
// filename operator
bool have_path() const;
bool have_ext() const;
bool isbackslash() const;
Self backslash() const;
Self subext(const Self &ext) const;
Self drivename() const;
Self dirname() const;
Self basename() const;
};
typedef list<String> Strings;
String::String() {}
String::String(const String::Super &s) : Super(s) {}
String::String(const wchar_t *s) : Super(s) {}
String::String(const_iterator b, const_iterator e) : Super(b, e) {}
String::String(const char *s) { assign_from_ansi(s); }
String::~String() {}
String &String::operator=(const String &s) { assign(s); return *this; }
String &String::operator=(const wchar_t *s) { assign(s); return *this; }
String &String::operator=(const char *s) { assign_from_ansi(s); return *this; }
String &String::operator+=(const char *s) { append(String(s)); return *this; }
String operator+(const String &s1, const char *s2) { return s1 + String(s2); }
String operator+(const char *s1, const String &s2) { return String(s1) + s2; }
bool operator==(const String &s1, const char *s2) { return s1 == String(s2); }
bool operator==(const char *s1, const String &s2) { return String(s1) == s2; }
string String::to_ansi() const
{
sint
siz = WideCharToMultiByte(CP_ACP, 0, c_str(), size(), NULL, 0, NULL, NULL);
char
*buf = new char[siz+1];
fill(buf, buf + siz+1, 0);
WideCharToMultiByte(CP_ACP, 0, c_str(), size(), buf, siz, NULL, NULL);
string
r(buf);
delete [] buf;
return r;
}
String String::to_upper() const
{
Self
r(*this);
for(iterator i = r.begin() ; i != r.end() ; ++i)
*i = towupper(*i);
return r;
}
String String::trim() const
{
const_iterator
b = begin(),
e = end();
b = ::find_if(b, e, isnotwspace);
e = ::rfind_if(b, e, isnotwspace);
if(e != end() && isnotwspace(*e))
++e;
return Self(b, e);
}
bool String::isdoublequote() const
{
// BUG:
// 単に先頭と最後の文字が " かどうかを判定しているだけなので、文字列の途中に " があった場合などを考慮していない。
if(size() >= 2 && *begin() == L'"' && *(end()-1) == L'"')
return true;
return false;
}
String String::doublequote() const
{
Self
r;
if(isdoublequote()){
r = *this;
}else{
r.reserve(size() + 2);
r.append(1, L'"');
r.append(*this);
r.append(1, L'"');
}
return r;
}
String String::doublequote_del() const
{
if(isdoublequote())
return String(begin()+1, end()-1);
else
return String(*this);
}
String &String::assign_from_ansi(const char *s)
{
sint
size = MultiByteToWideChar(CP_ACP, 0, s, -1, NULL, 0);
wchar_t
*buf = new wchar_t[size+1];
fill(buf, buf + size+1, 0);
MultiByteToWideChar(CP_ACP, 0, s, -1, buf, size);
assign(buf);
delete [] buf;
return *this;
}
String &String::assign_from_utf8(const char *s)
{
sint
size = MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0);
wchar_t
*buf = new wchar_t[size+1];
fill(buf, buf + size+1, 0);
MultiByteToWideChar(CP_UTF8, 0, s, -1, buf, size);
assign(buf);
delete [] buf;
return *this;
}
String &String::assign_from_env(const String &name)
{
wchar_t
*g = _wgetenv(name.c_str());
if(g)
assign(g);
else
clear();
return *this;
}
String &String::printf(String format, ...) // vaを使う必要上、Stringは値渡し
{
wchar_t
buf[1024+1];
va_list
va;
sint
size;
va_start(va, format);
size = wvsprintf(buf, format.c_str(), va);
va_end(va);
assign(buf, buf + size);
return *this;
}
bool String::have_path() const
{
return ::find(begin(), end(), L':') != end() || ::find_if(begin(), end(), ::isbackslash) != end();
}
bool String::have_ext() const
{
const_iterator
b = begin();
b = ::rfind_if(b, end(), ::isbackslash);
if(b != end() && ::isbackslash(*b))
++b;
b = ::find(b, end(), L'.');
if(b != end() && *b == L'.')
return true;
return false;
}
bool String::isbackslash() const
{
return size() > 0 && ::isbackslash(*(end()-1));
}
String String::backslash() const
{
String
r(*this);
if(size() > 0 && !isbackslash())
r.append(1, L'\\');
return r;
}
String String::subext(const String &ext) const
{
const_iterator
e = ::rfind(begin(), end(), L'.');
if(e != end() && *e != L'.')
e = end();
return Self(begin(), e).append(ext);
}
String String::drivename() const
{
if(size() >= 2 && iswalpha((*this)[0]) && (*this)[1] == L':')
return Self(begin(), begin() + 2);
return Self();
}
String String::dirname() const
{
const_iterator
e = ::rfind_if(begin(), end(), ::isbackslash);
if(e != end() && ::isbackslash(*e))
++e;
return Self(begin(), e);
}
String String::basename() const
{
const_iterator
b = begin(),
e = end();
b = ::rfind_if(b, e, ::isbackslash);
if(b != end() && ::isbackslash(*b))
++b;
e = ::rfind(b, e, L'.');
if(e != end() && *e != L'.')
e = end();
return Self(b, e);
}
////////////////////////////////////////////////////////////////////////
#if defined(__CONSOLE__)
void _putcxxx(const char *s, FILE *fp)
{
fputs(s, fp);
fputc('\n', fp);
}
#else // __CONSOLE__
String
putcxxx_string;
void _putcxxx(const char *s, FILE *)
{
if(putcxxx_string.size() > 0)
putcxxx_string += "\r\n";
putcxxx_string += s;
}
#endif // __CONSOLE__
inline void putcout(const char *s)
{
_putcxxx(s, stdout);
}
inline void putcerr(const char *s)
{
_putcxxx(s, stderr);
}
void _putcxxx(const String &s, FILE *fp)
{
_putcxxx(s.to_ansi().c_str(), fp);
}
inline void putcout(const String &s)
{
_putcxxx(s, stdout);
}
inline void putcerr(const String &s)
{
_putcxxx(s, stderr);
}
void _putcxxx(const char *s1, const String &s2, FILE *fp)
{
_putcxxx(String().assign_from_ansi(s1) + s2, fp);
}
inline void putcout(const char *s1, const String &s2)
{
_putcxxx(s1, s2, stdout);
}
inline void putcerr(const char *s1, const String &s2)
{
_putcxxx(s1, s2, stderr);
}
bool case_ignore_equal(const String &s1, const String &s2)
{
return s1.to_upper() == s2.to_upper();
}
////////////////////////////////////////////////////////////////////////
class IniFileStream {
typedef IniFileStream Self;
typedef ifstream Super;
private:
FILE
*_fp;
String
_filename;
bool skip_bom();
Self &get(sint &ch) { ch = fgetc(_fp); return *this; }
Self &unget(sint ch) { ungetc(ch, _fp); return *this; }
public:
IniFileStream() : _fp(NULL) {}
IniFileStream(const String &filename) : _fp(NULL) { open(filename); }
static String determine_filename(const String &exename);
const String &filename() const { return _filename; }
void open(const String &filename);
void close();
bool is_open() const { return _fp != NULL; }
bool good() const { return _fp != NULL && !feof(_fp); }
bool eof() const { return _fp == NULL || feof(_fp); }
bool getline(String &s);
static bool is_comment(const String &s);
static bool is_section(const String &s);
static bool is_section(const String &s, const String §ion_name);
static bool is_key(const String &s, const String &key_name);
static bool get_value_bool(const String &s);
static String get_value_String(const String &s);
};
bool IniFileStream::skip_bom()
{
// BUG:
// ef ff... のようなファイルの場合、二つ目のffしかunget()されない
sint
c;
if(!eof()){
get(c);
if(c == 0xef){
if(!eof()){
get(c);
if(c == 0xbb){
if(!eof()){
get(c);
if(c == 0xbf)
return true; // UTF8
}
}
}
#if 0 // UTF16には対応していない
}else if(c == 0xfe){
if(!eof()){
get(c);
if(c == 0xff)
return true; // UTF16BE
}
}else if(c == 0xff){
if(!eof()){
get(c);
if(c == 0xfe)
return true; // UTF16LE
}
#endif
}
unget(c);
}
return false;
}
String IniFileStream::determine_filename(const String &exename)
{
String
r;
if(file_is_readable(r = exename.subext(".ini")))
return r;
if(file_is_readable(r = exename.subext(".bat")))
return r;
return exename.subext(".ini"); /* not found, using default */
}
void IniFileStream::open(const String &fn)
{
close();
_fp = _wfopen(fn.c_str(), L"rt");
if(_fp != NULL){
_filename = fn;
skip_bom();
}
}
void IniFileStream::close()
{
if(_fp != NULL){
fclose(_fp);
_fp = NULL;
}
}
bool IniFileStream::is_comment(const String &s)
{
return s.size()==0 || s[0]==L'#' || s[0]==L';' || s[0]==L'@';
}
bool IniFileStream::is_section(const String &s)
{
if(s.size() >= 2 && *s.begin() == L'[' && *(s.end()-1) == L']')
return true;
return false;
}
bool IniFileStream::is_section(const String &s, const String §ion_name)
{
if(!is_section(s))
return false;
return String(s.begin()+1, s.end()-1).trim().to_upper() == section_name;
}
bool IniFileStream::is_key(const String &s, const String &key_name)
{
String::const_iterator
f = find(s.begin(), s.end(), L'=');
String
key_c(s.begin(), f);
return key_c.trim().trim().to_upper() == key_name;
}
bool IniFileStream::get_value_bool(const String &s)
{
String::const_iterator
f = find(s.begin(), s.end(), L'=');
if(f == s.end())
return true; // 「=」がない → キーのみで値の記述なし → trueを返す
String
value(f+1, s.end());
bool
r = false;
value = value.trim().to_upper();
if(value == "TRUE" || value == "YES" || value == "ON"){
r = true;
}else if(value == "FALSE" || value == "NO" || value == "OFF"){
r = false;
}else{
putcerr(PGM_ERR "invalid value: ", value);
error = true;
rcode = -1;
}
return r;
}
String IniFileStream::get_value_String(const String &s)
{
String::const_iterator
f = find(s.begin(), s.end(), L'=');
if(f == s.end())
return String(); // 「=」がない → 空文字列を返す
return String(f+1, s.end());
}
bool IniFileStream::getline(String &s)
{
s.clear();
if(!good())
return false;
string
tmp;
tmp.reserve(200);
for(int c ; (c = fgetc(_fp)) != EOF ; tmp += c)
if(c == '\n')
break;
s.assign_from_utf8(tmp);
s = s.trim();
return true;
}
////////////////////////////////////////////////////////////////////////
class WindowsAPI {
public:
static bool CreateProcess(const String &cmd, DWORD CreationFlags, const String &wd, LPSTARTUPINFO si, LPPROCESS_INFORMATION pi);
static String GetClipboardText();
static String GetCommandLine() { return ::GetCommandLine(); }
static String GetComputerName();
static String GetModuleFileName(HMODULE Module = 0);
static String GetTempPath();
static String GetUserName();
static String SHGetSpecialFolder(sint nFolder);
};
bool WindowsAPI::CreateProcess(const String &cmd, DWORD CreationFlags, const String &wd, LPSTARTUPINFO si, LPPROCESS_INFORMATION pi)
{
bool
r;
wchar_t
*cmd_c_str;
cmd_c_str = new wchar_t[cmd.size()+1];
copy(cmd.begin(), cmd.end(), cmd_c_str);
cmd_c_str[cmd.size()] = 0;
r = ::CreateProcess(NULL, cmd_c_str, NULL, NULL, TRUE, CreationFlags, NULL, (wd.size()>0 ? wd.c_str() : NULL), si, pi);
delete [] cmd_c_str;
return r;
}
String WindowsAPI::GetClipboardText()
{
String
r;
HANDLE
h;
if(::OpenClipboard(NULL) == 0)
return String();
if((h = ::GetClipboardData(CF_UNICODETEXT)) != NULL){
r.assign((wchar_t *)::GlobalLock(h));
::GlobalUnlock(h);
}
if(::CloseClipboard() == 0)
return String();
// 改行文字等はスペースに置換する
for(String::iterator i = r.begin() ; i != r.end() ; ++i)
if(iswspace(*i))
*i = L' ';
return r;
}
String WindowsAPI::GetComputerName()
{
wchar_t
buf[MAX_COMPUTERNAME_LENGTH+1];
DWORD
size = sizeof buf / sizeof(wchar_t);
if(::GetComputerName(buf, &size) == 0)
return String();
return String(String::iterator(buf), String::iterator(buf + size));
}
String WindowsAPI::GetModuleFileName(HMODULE Module)
{
wchar_t
buf[MAX_PATH+1];
DWORD
size = sizeof buf / sizeof(wchar_t);
size = ::GetModuleFileName(Module, buf, size);
if(size == 0)
return String();
return String(buf, buf + size);
}
String WindowsAPI::GetTempPath()
{
String
r;
DWORD
size = ::GetTempPath(0, NULL);
wchar_t
*buf = new wchar_t[size];
::GetTempPath(size, buf);
r.assign(buf);
delete [] buf;
return r;
}
String WindowsAPI::GetUserName()
{
wchar_t
buf[UNLEN+1];
DWORD
size = sizeof buf / sizeof(wchar_t);
if(::GetUserName(buf, &size) == 0)
return String();
return String(String::iterator(buf), String::iterator(buf + size - 1));
}
String WindowsAPI::SHGetSpecialFolder(sint nFolder)
{
wchar_t
buf[MAX_PATH+1];
memset(buf, 0, sizeof buf);
SHGetSpecialFolderPath(0, buf, nFolder, 0);
return String(buf);
}
String get_given_option(const String &cmd)
{
// コマンドライン文字列から、コマンド名を除いた残りの部分(コマンドに与えるパラメータの部分)を返す
bool
in_quote = false;
for(String::const_iterator i = cmd.begin() ; i != cmd.end() ; ++i){
if(!in_quote && iswspace(*i))
return String(i, cmd.end());
if(*i == L'"')
in_quote = in_quote ? false : true;
}
return String();
}
////////////////////////////////////////////////////////////////////////
class ExecuteInfo {
typedef ExecuteInfo Self;
private:
Strings
_exStrings,
_export_env;
String
_exename,
_ininame,
_target,
_cwd,
_arg,
_chdir;
bool
_verbose,
_wait,
_hide,
_maximize,
_minimize;
static String getenv(const String &name);
static sint putenv(const String &name, const String &val);
static String get_shell_name();
static String system_escape(const String &s);
static bool search_path(String &cmd, const String &selfname);
sint system(const String &cmd, const String &arg);
sint create_process(const String &cmd);
public:
ExecuteInfo();
const Strings &exs() const { return _exStrings; }
const Strings &export_env() const { return _export_env; }
const String &exename() const { return _exename; }
const String &ininame() const { return _ininame; }
const String &target() const { return _target; }
const String &cwd() const { return _cwd; }
const String &arg() const { return _arg; }
const String &chdir() const { return _chdir; }
bool verbose() const { return _verbose; }
bool wait() const { return _wait; }
bool hide() const { return _hide; }
bool maximize() const { return _maximize; }
bool minimize() const { return _minimize; }
void add_exs(const String &s);
void add_export_env(const String &s);
const String &exename(const String &s);
// const String &ininame(const String &s);
const String &target(const String &s);
const String &arg(const String &s);
const String &chdir(const String &s);
bool verbose(bool v) { return _verbose = v; }
bool wait(bool v) { return _wait = v; }
bool hide(bool v) { return _hide = v; }
bool maximize(bool v) { return _maximize = v; }
bool minimize(bool v) { return _minimize = v; }
sint execute();
String expand(const String &s) const;
String expand_value(const String &key) const;
void verbose_out(const String &s);
};
typedef list<ExecuteInfo>
ExecuteInfos;
ExecuteInfo::ExecuteInfo()
{
_arg = "${ARG}";
_chdir = "";
_verbose = false;
_wait = false;
_hide = false;
_maximize = false;
_minimize = false;
}
String ExecuteInfo::getenv(const String &name)
{
return String().assign_from_env(name);
}
sint ExecuteInfo::putenv(const String &name, const String &val)
{
return _wputenv((name + L'=' + val).c_str());
}
String ExecuteInfo::get_shell_name()
{
wchar_t
*g = _wgetenv(L"COMSPEC");
if(g)
return String(g);
else
return String("cmd.exe");
}
bool ExecuteInfo::search_path(String &cmd, const String &selfname)
{
String
path,
path1,
pathext,
pathext1,
fn;
String::iterator
b, e,
bb, ee;
bool
cmd_have_path = cmd.have_path(),
cmd_have_ext = cmd.have_ext();
path.assign_from_env("PATH");
pathext.assign_from_env("PATHEXT");
if(!cmd_have_path){
for(e = path.begin(), b = e ; e != path.end() ; b = e + 1){
e = find(b, path.end(), L';');
path1.assign(b, e);
path1 = path1.trim();
if(path1.size() > 0){
path1 = path1.backslash();
if(!cmd_have_ext){
if(file_is_exist(path1 + cmd + ".*")){
for(ee = pathext.begin(), bb = ee ; ee != pathext.end() ; bb = ee + 1){
ee = find(bb, pathext.end(), L';');
pathext1.assign(bb, ee);
pathext1 = pathext1.trim();
if(pathext1.size() > 0){
fn = path1 + cmd + pathext1;
if(fn.to_upper() != selfname && file_is_executable(fn)){
cmd = fn;
return true;
}
}
}
}
}else{
fn = path1 + cmd;
if(fn.to_upper() != selfname && file_is_executable(fn)){
cmd = fn;
return true;
}
}
}
}
}else{
if(!cmd_have_ext){
if(file_is_exist(cmd + ".*")){
for(ee = pathext.begin(), bb = ee ; ee != pathext.end() ; bb = ee + 1){
ee = find(bb, pathext.end(), L';');
pathext1.assign(bb, ee);
pathext1 = pathext1.trim();
if(pathext1.size() > 0){
fn = cmd + pathext1;
if(fn.to_upper() != selfname && file_is_executable(fn)){
cmd = fn;
return true;
}
}
}
}
}else{