@@ -122,7 +122,7 @@ def completer(text, state):
122
122
except IndexError :
123
123
return None
124
124
125
- def call_daemon (shell , req ):
125
+ def call_daemon (shell , req , interactive ):
126
126
try :
127
127
sock = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
128
128
except socket .error as err :
@@ -140,9 +140,23 @@ def call_daemon(shell, req):
140
140
"then run '#targetcli --disable-daemon'" , 'red' ))
141
141
sys .exit (1 )
142
142
143
+ # Two cases where we want to get pwd:
144
+ # 1. Before starting shell in interactive mode, needed for setting terminal
145
+ # 2. And only in Interactive mode, having command 'cd'
146
+ get_pwd = False
147
+ if interactive :
148
+ if not req :
149
+ req = "pwd"
150
+ get_pwd = True
151
+ elif "cd " in req :
152
+ req += "%pwd"
153
+ get_pwd = True
154
+ else :
155
+ req = "cd /%" + req # Non-interactive modes always consider start at '/'
156
+
143
157
try :
144
158
# send request
145
- sock .sendall (req )
159
+ sock .sendall (req . encode () )
146
160
except socket .error as err :
147
161
shell .con .display (shell .con .render_text (err , 'red' ))
148
162
sys .exit (1 )
@@ -153,15 +167,30 @@ def call_daemon(shell, req):
153
167
amount_received = 0
154
168
155
169
# get the actual data in chunks
170
+ output = ""
171
+ path = ""
156
172
while amount_received < amount_expected :
157
173
data = sock .recv (1024 )
158
174
data = data .decode ()
159
175
amount_received += len (data )
160
- print (data , end = "" )
176
+ output += data
177
+
178
+ if get_pwd :
179
+ output_split = output .splitlines ()
180
+ lines = len (output_split )
181
+ for i in range (0 , lines ):
182
+ if i == lines - 1 :
183
+ path = str (output_split [i ])
184
+ else :
185
+ print (str (output_split [i ]), end = "\n " )
186
+ else :
187
+ print (output , end = "" )
161
188
162
189
sock .send (b'-END@OF@DATA-' )
163
190
sock .close ()
164
191
192
+ return path
193
+
165
194
def switch_to_daemon (shell , interactive ):
166
195
readline .set_completer (completer )
167
196
readline .set_completer_delims ('' )
@@ -173,7 +202,7 @@ def switch_to_daemon(shell, interactive):
173
202
174
203
if len (sys .argv ) > 1 :
175
204
command = " " .join (sys .argv [1 :])
176
- call_daemon (shell , command . encode () )
205
+ call_daemon (shell , command , False )
177
206
sys .exit (0 )
178
207
179
208
if interactive :
@@ -188,10 +217,14 @@ def switch_to_daemon(shell, interactive):
188
217
"type 'exit' to run them all in one go.\n "
189
218
% targetcli_version )
190
219
220
+ prompt_path = "/"
221
+ if interactive :
222
+ prompt_path = call_daemon (shell , None , interactive ) # get the initial path
223
+
191
224
inputs = []
192
225
real_exit = False
193
226
while True :
194
- shell .con .raw_write ("/ > " )
227
+ shell .con .raw_write ("%s > " % prompt_path )
195
228
pos = shell .con .get_cursor_xy ()
196
229
shell .con .set_cursor_xy (pos [0 ], pos [1 ])
197
230
command = six .moves .input ()
@@ -203,12 +236,17 @@ def switch_to_daemon(shell, interactive):
203
236
inputs .append (command )
204
237
if real_exit :
205
238
command = '%' .join (inputs ) # delimit multiple commands with '%'
206
- call_daemon (shell , command . encode () )
239
+ call_daemon (shell , command , interactive )
207
240
break
208
241
else :
209
242
if real_exit :
210
243
break
211
- call_daemon (shell , command .encode ())
244
+ path = call_daemon (shell , command , interactive )
245
+ if path :
246
+ if path [0 ] == "/" :
247
+ prompt_path = path
248
+ else :
249
+ print (path ) # Error No Path ...
212
250
213
251
sys .exit (0 )
214
252
0 commit comments