-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshellstr.cpp
138 lines (130 loc) · 3.22 KB
/
shellstr.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
#include <stdlib.h>
#include "nnstring.h"
#include "shell.h"
/** buf の中に空白があれば、外側を引用符で囲む。
* buf - 元文字列
* dst - 変換後文字列
*/
void NyadosShell::enquote( const char *buf , NnString &dst )
{
if( strchr(buf,' ') != NULL ){
dst << '"' << buf << '"';
}else{
dst << buf ;
}
}
/** 文字列 p から、引用符を除く。ただし、"" は " に変換する。
* p - 元文字列
* result - 変換後文字列
*/
void NyadosShell::dequote( const char *p , NnString &result )
{
result.erase();
int quotecount=0;
for( ; *p != '\0' ; ++p ){
if( *p == '"' ){
if( ++quotecount % 2 == 0 )
result << '"';
}else if( *p == '\n' ){
result << "$T";
quotecount = 0;
}else{
result << (char)*p;
quotecount = 0;
}
}
}
void NyadosShell::dequote( NnString &result )
{
NnString result_;
dequote( result.chars() , result_ );
result = result_ ;
}
/* 文字列を読み取る。
* 引用符なども認識する。
* sp - 文字列先頭ポインタ
* →実行後は、末尾の次の文字位置(空白,\0 位置へ)
* token - 文字列を入れる先
* return
* 0 - 普通
* '<', '>','|'
* EOF - 末尾
*/
int NyadosShell::readWord( const char *&sp , NnString &token )
{
int quote=0;
token.erase();
for(;;){
if( *sp == '\0' )
return EOF;
if( isSpace(*sp) && quote==0 )
return 0;
if( quote==0 && (*sp=='<' || *sp=='>' || *sp=='|' ) )
return *sp;
if( quote==0 && *sp=='%' ){
NnString envname;
for(;;){
++sp;
if( *sp == '\0' ){
token << '%' << envname;
return EOF;
}
if( *sp == '<' || *sp == '>' || *sp == '|' ){
token << '%' << envname;
return *sp;
}
if( *sp == '"' ){
quote = !quote;
token << '%' << envname;
break;
}
if( *sp == '%' ){
const char *envval=getEnv( envname.chars() , NULL );
if( envval == NULL ){
token << '%' << envname << '%';
}else{
token << envval;
}
break;
}
envname << *sp;
}
}else{
if( *sp == '"' )
quote = !quote;
else
token << *sp;
}
++sp;
}
}
/* スペース文字をスキップする
* sp : 変換前後の文字列
* return
* 0 : 成功
* -1 : スペース以外の文字が見つからなかった。
*/
int NyadosShell::skipSpc( const char *&sp )
{
for(;;){
if( *sp == '\0' )
return -1;
if( !isSpace(*sp) )
return 0;
++sp;
}
}
/* リダイレクト先を読み取る。
* sp - '>' などの文字の次を指しているポインタ
* → 実行後は末尾の次の文字位置へ(空白,\0)
* fn - ファイル名
*/
void NyadosShell::readNextWord( const char *&sp , NnString &fn )
{
if( skipSpc( sp ) == -1 ){
fn.erase();
return ;
}
readWord( sp , fn );
fn.slash2yen();
}