-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnyados.cpp
executable file
·295 lines (273 loc) · 6.99 KB
/
nyados.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
#include <assert.h>
#include <io.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#if defined(HAVE_NOT_OLD_NEW_H) || defined(_MSC_VER)
# include <new>
#else
# include <new.h>
#endif
#include "config.h"
#include "nnstring.h"
#include "getline.h"
#include "ishell.h"
#include "writer.h"
#define VER "2.31_3"
#ifdef _MSC_VER
int nya_new_handler(size_t size)
#else
void nya_new_handler()
#endif
{
fputs("memory allocation error.\n",stderr);
abort();
#ifdef _MSC_VER
return 0;
#endif
}
/* _nya ファイルのパス */
static NnString rcfname;
/* rcファイルを探す.
* rcfname - 見つかった場合、rcファイル名が入る
* argv0 - exeファイルの名前.
* return
* 0 - 見つかった , 1 - 見つからなかった
*/
static int seekrc( NnString &rcfname , const char *argv0 )
{
const static char *rcfnames[] = RUN_COMMANDS ;
NnString rcfn;
for( const char **p =rcfnames ; *p != NULL ; ++p ){
/* カレントディレクトリの _nya を試みる */
if( access( *p ,0 ) == 0 ){
rcfname = *p ;
return 0;
}
/* HOME ディレクトリの _nya を試みる */
const char *home=getEnv("HOME");
if( home == NULL )
home = getEnv("USERPROFILE");
if( home != NULL ){
rcfn = home;
rcfn.trim();
rcfn << '\\' << *p;
if( access( rcfn.chars() , 0 )==0 ){
rcfname = rcfn;
return 0;
}
}
rcfn = "\\";
rcfn += *p;
if( access(rcfn.chars(),0) == 0 ){
rcfname = rcfn;
return 0;
}
/* EXE ファイルと同じディレクトリの _nya を試みる */
int lastroot=NnDir::lastRoot( argv0 );
if( lastroot != -1 ){
rcfn.assign( argv0 , lastroot );
rcfn << '\\' << *p;
if( access( rcfn.chars(),0) == 0 ){
rcfname = rcfn;
return 0;
}
}
}
return 1;
}
/* -d デバッグオプション
* argc , argv : main の引数
* i : 現在読み取り中の argv の添字
* return
* 常に 0 (継続)
*/
static int opt_d( int , char **argv , int &i )
{
if( argv[i][2] == '\0' ){
properties.put("debug",new NnString() );
}else{
size_t len=strcspn(argv[i]+2,"=");
NnString var(argv[i]+2,len);
if( argv[i][2+len] == '\0' ){
properties.put( var , new NnString() );
}else{
properties.put( var , new NnString(argv[i]+2+len+1));
}
}
return 0;
}
/* -r _nya 指定オプション処理
* argc , argv : main の引数
* i : 現在読み取り中の argv の添字
* return
* 0 : 継続
* not 0 : NYA*S を終了させる(mainの戻り値+1を返す)
*/
static int opt_r( int argc , char **argv, int &i )
{
if( i+1 >= argc ){
conErr << "-r : needs a file name.\n";
return 2;
}
if( access( argv[i+1] , 0 ) != 0 ){
conErr << argv[++i] << ": no such file.\n";
return 2;
}
rcfname = argv[++i];
return 0;
}
/* -e 1行コマンド用オプション処理
* argc , argv : main の引数
* i : 現在読み取り中の argv の添字
* return
* 0 : 継続
* not 0 : NYA*S を終了させる(mainの戻り値+1を返す)
*/
static int opt_e( int argc , char **argv , int &i )
{
if( i+1 >= argc ){
conErr << "-e needs commandline.\n";
return 2;
}
OneLineShell sh( argv[++i] );
sh.mainloop();
return 1;
}
/* -f スクリプト実行用オプション処理
* argc , argv : main の引数
* i : 現在読み取り中の argv の添字
* return
* 0 : 継続
* not 0 : NYA*S を終了させる(mainの戻り値+1を返す)
*/
static int opt_f( int argc , char **argv , int &i )
{
if( i+1 >= argc ){
conErr << argv[0] << " -f : no script filename.\n";
return 2;
}
if( access(argv[i+1],0 ) != 0 ){
conErr << argv[i+1] << ": no such file or command.\n";
return 2;
}
NnString path(argv[i+1]);
ScriptShell scrShell( path.chars() );
for( int j=i+1 ; j < argc ; j++ )
scrShell.addArgv( *new NnString(argv[j]) );
int rc=1;
if( argv[i][2] == 'x' ){
NnString line;
while( (rc=scrShell.readline(line)) >= 0
&& (strstr(line.chars(),"#!") == NULL
|| strstr(line.chars(),"nya") == NULL ) ){
// no operation.
}
}
if( rc >= 0 )
scrShell.mainloop();
return 1;
}
/* -a デフォルトエイリアスを有効にする
* argc , argv : main の引数
* i : 現在読み取り中の argv の添字
* return
* 0 : 継続
* not 0 : NYA*S を終了させる(mainの戻り値+1を返す)
*/
static int opt_a( int , char ** , int & )
{
extern NnHash aliases;
aliases.put("ls" , new NnString("list") );
aliases.put("mv" , new NnString("move /-Y $@") );
aliases.put("cp" , new NnString("copy /-Y /B /V $@") );
aliases.put("rmdir" , new NnString("rmdir $@") );
aliases.put("rm" , new NnString("del $@") );
return 0;
}
int main( int argc, char **argv )
{
// set_new_handler(nya_new_handler);
properties.put("nyatype",new NnString(SHELL_NAME) );
NnVector nnargv;
for(int i=1;i<argc;i++){
if( argv[i][0] == '-' ){
int rv;
switch( argv[i][1] ){
case 'D': rv=opt_d(argc,argv,i); break;
case 'r': rv=opt_r(argc,argv,i); break;
case 'f': rv=opt_f(argc,argv,i); break;
case 'e': rv=opt_e(argc,argv,i); break;
case 'a': rv=opt_a(argc,argv,i); break;
default:
rv = 2;
conErr << argv[0] << " : -"
<< (char)argv[1][1] << ": No such option\n";
break;
}
if( rv != 0 )
return rv-1;
}else{
nnargv.append( new NnString(argv[i]) );
}
}
conOut <<
#ifdef ESCAPE_SEQUENCE_OK
"\x1B[2J" <<
#endif
#ifdef NYADOS
"Nihongo Yet Another DOS Shell "
#elif defined(NYACUS)
"Nihongo Yet Another CUI Shell "
#else
"Nihongo Yet Another OS/2 Shell "
#endif
VER" (C) 2001-2010 by NYAOS.ORG\n";
NnDir::set_default_special_folder();
/* _nyados を実行する */
if( !rcfname.empty() || seekrc(rcfname,argv[0])==0 ){
rcfname.slash2yen();
ScriptShell scrShell( rcfname.chars() );
scrShell.addArgv( rcfname );
for( int i=0 ; i < nnargv.size() ; i++ ){
if( nnargv.const_at(i) != NULL )
scrShell.addArgv( *(const NnString *)nnargv.const_at(i) );
}
scrShell.mainloop();
}
signal( SIGINT , SIG_IGN );
/* DOS窓からの入力に従って実行する */
InteractiveShell intShell;
/* ヒストリをロードする */
NnString *histfn=(NnString*)properties.get("savehist");
if( histfn != NULL ){
FileReader fr( histfn->chars() );
if( fr.eof() ){
perror( histfn->chars() );
}else{
intShell.getHistoryObject()->read( fr );
}
}
intShell.mainloop();
/* ヒストリを保存する */
if( (histfn=(NnString*)properties.get("savehist")) != NULL ){
int histfrom=0;
NnString *histsizestr=(NnString*)properties.get("histfilesize");
History *hisObj=intShell.getHistoryObject();
if( histsizestr != NULL ){
int histsize=atoi(histsizestr->chars());
if( histsize !=0 && (histfrom=hisObj->size() - histsize)<0 )
histfrom = 0;
}
FileWriter fw( histfn->chars() , "w" );
if( fw.ok() ){
NnString his1;
for(int i=histfrom ; i<hisObj->size() ; i++ ){
if( hisObj->get(i,his1) == 0 )
fw << his1 << '\n';
}
}
}
conOut << "\nGood bye!\n";
return 0;
}