@@ -82,7 +82,7 @@ bool Commander::enablePipeModuleFunc( char* buffer, int bufferSize, commanderPip
82
82
return true ;
83
83
}
84
84
85
- bool Commander::executeCommand ( const char *cmd, void * parent ){
85
+ bool Commander::executeCommand ( const char *cmd ){
86
86
87
87
// The beginning of the argument list will be stored in this pointer
88
88
char *arg;
@@ -120,13 +120,13 @@ bool Commander::executeCommand( const char *cmd, void* parent ){
120
120
// Check if piping is requested, but disabled.
121
121
if ( pipeArgBuffer == NULL ){
122
122
123
- if ( response != NULL ){
124
- response -> print ( __CONST_TXT__ ( " The command contains piping but the pipe module is disabled!" ) );
125
- }
123
+ // if( caller -> channel != NULL ){
124
+ caller -> print ( __CONST_TXT__ ( " The command contains piping but the pipe module is disabled!" ) );
125
+ // }
126
126
return false ;
127
127
}
128
128
129
- // Terminate where pip is found.
129
+ // Terminate where pipe is found.
130
130
tempBuff[ pipePos ] = ' \0 ' ;
131
131
132
132
}
@@ -181,31 +181,31 @@ bool Commander::executeCommand( const char *cmd, void* parent ){
181
181
182
182
if ( memoryType == MEMORY_REGULAR ){
183
183
184
- if ( response != NULL ){
184
+ // if( caller -> channel != NULL ){
185
185
186
186
// Print the description text to the output channel.
187
- response -> print ( commandData_ptr -> name );
188
- response -> print ( ' :' );
189
- response -> print ( ' ' );
190
- response -> println ( commandData_ptr -> data.desc );
187
+ caller -> print ( commandData_ptr -> name );
188
+ caller -> print ( ' :' );
189
+ caller -> print ( ' ' );
190
+ caller -> println ( commandData_ptr -> data.desc );
191
191
192
- }
192
+ // }
193
193
194
194
}
195
195
196
196
#ifdef __AVR__
197
197
198
198
else if ( memoryType == MEMORY_PROGMEM ){
199
199
200
- if ( response != NULL ){
200
+ // if( response != NULL ){
201
201
202
202
// Print the description text to the output channel.
203
- response -> print ( commandData_ptr -> name_P );
204
- response -> print ( ' :' );
205
- response -> print ( ' ' );
206
- response -> println ( commandData_ptr -> data.desc_P );
203
+ caller -> print ( commandData_ptr -> name_P );
204
+ caller -> print ( ' :' );
205
+ caller -> print ( ' ' );
206
+ caller -> println ( commandData_ptr -> data.desc_P );
207
207
208
- }
208
+ // }
209
209
210
210
}
211
211
@@ -257,7 +257,10 @@ bool Commander::executeCommand( const char *cmd, void* parent ){
257
257
if ( ( pipeArgBuffer != NULL ) && ( pipePos > 0 ) ){
258
258
259
259
// Execute commands function and redirect the output to the pipe channel.
260
- executionStat = ( commandData_ptr -> data.func )( arg, pipeChannel, parent );
260
+ Stream* tmp = caller -> getChannel ();
261
+ caller -> setChannel ( pipeChannel );
262
+ executionStat = ( commandData_ptr -> data.func )( arg, caller );
263
+ caller -> setChannel ( tmp );
261
264
262
265
// If the actual command execution was successful, we have to increment the pipeCounter
263
266
if ( executionStat ){
@@ -271,7 +274,7 @@ bool Commander::executeCommand( const char *cmd, void* parent ){
271
274
else {
272
275
273
276
// Execute command function.
274
- executionStat = ( commandData_ptr -> data.func )( arg, response, parent );
277
+ executionStat = ( commandData_ptr -> data.func )( arg, caller );
275
278
276
279
}
277
280
@@ -369,21 +372,21 @@ bool Commander::executeCommand( const char *cmd, void* parent ){
369
372
// We have to check for single or described help function.
370
373
if ( strcmp ( arg, (const char *)" -d" ) == 0 ){
371
374
372
- printHelp ( response , true , formatting );
375
+ printHelp ( caller , true , formatting );
373
376
374
377
}
375
378
376
379
else {
377
380
378
- printHelp ( response , false , formatting );
381
+ printHelp ( caller , false , formatting );
379
382
380
383
}
381
384
382
385
}
383
386
384
387
else {
385
388
386
- if ( response != NULL ){
389
+ // if( caller -> channel != NULL ){
387
390
if ( pipePos > 0 ){
388
391
tempBuff[ pipePos ] = ' |' ;
389
392
printBrokenPipe ();
@@ -395,45 +398,34 @@ bool Commander::executeCommand( const char *cmd, void* parent ){
395
398
396
399
// If we went through the whole tree and we did not found the command in it,
397
400
// we have to notice the user abut the problem. Maybe a Type-O
398
- response -> print ( __CONST_TXT__ ( " Command \' " ) );
399
- response -> print ( tempBuff );
400
- response -> print ( __CONST_TXT__ ( " \' not found!" ) );
401
+ caller -> print ( __CONST_TXT__ ( " Command \' " ) );
402
+ caller -> print ( tempBuff );
403
+ caller -> print ( __CONST_TXT__ ( " \' not found!" ) );
401
404
402
- }
405
+ // }
403
406
return false ;
404
407
405
408
}
406
409
return true ;
407
410
}
408
411
409
- bool Commander::execute ( const char *cmd ){
410
-
411
- // Default execute handler, so the default response will be chosen.
412
- response = NULL ;
413
-
414
- // Reset the pipe position tracker.
415
- pipeCounter = 0 ;
412
+ bool Commander::execute ( const char *cmd, Stream* channel_p, CommandCaller* caller_p ){
416
413
417
- // Save the address of the original message.
418
- originalCommandData = cmd;
414
+ if ( channel_p == NULL ){
415
+ caller = &defaultCommandCaller;
416
+ caller -> clearChannel ();
417
+ }
419
418
420
- // If piping is enabled, we have to flush the pipe channel before command execution.
421
- if ( pipeArgBuffer != NULL ){
422
- while ( pipeChannel -> available () ){
423
- pipeChannel -> read ();
419
+ else {
420
+ if ( caller_p == NULL ){
421
+ caller = &defaultCommandCaller;
422
+ caller -> setChannel ( channel_p );
423
+ }
424
+ else {
425
+ caller = caller_p;
424
426
}
425
427
}
426
428
427
- // Execute the command.
428
- return ( executeCommand ( cmd ) );
429
-
430
- }
431
-
432
- bool Commander::execute ( const char *cmd, Stream *resp, void * parent ){
433
-
434
- // Redirect the response to the specified Stream.
435
- response = resp;
436
-
437
429
// Reset the pipe position tracker.
438
430
pipeCounter = 0 ;
439
431
@@ -448,7 +440,7 @@ bool Commander::execute( const char *cmd, Stream *resp, void* parent ){
448
440
}
449
441
450
442
// Execute the command.
451
- return ( executeCommand ( cmd, parent ) );
443
+ return ( executeCommand ( cmd ) );
452
444
453
445
}
454
446
@@ -891,51 +883,51 @@ void Commander::printBrokenPipe(){
891
883
892
884
if ( brokenPipePos >= 0 ){
893
885
894
- if ( response != NULL ){
886
+ // if( caller -> channel != NULL ){
895
887
896
888
// In case of broken pipe, inform the user about the problematic section.
897
889
if ( formatting ){
898
- response -> print ( __CONST_TXT__ ( " \033 [1;35m" ) );
890
+ caller -> print ( __CONST_TXT__ ( " \033 [1;35m" ) );
899
891
}
900
892
901
- response -> println ( __CONST_TXT__ ( " \r\n Broken pipe!" ) );
893
+ caller -> println ( __CONST_TXT__ ( " \r\n Broken pipe!" ) );
902
894
903
895
if ( formatting ){
904
- response -> print ( __CONST_TXT__ ( " \033 [0;37m" ) );
896
+ caller -> print ( __CONST_TXT__ ( " \033 [0;37m" ) );
905
897
}
906
898
907
- response -> println ( originalCommandData );
899
+ caller -> println ( originalCommandData );
908
900
909
901
for ( i = 0 ; i < brokenPipePos; i++ ){
910
- response -> print ( ' ' );
902
+ caller -> print ( ' ' );
911
903
}
912
904
913
905
if ( formatting ){
914
- response -> print ( __CONST_TXT__ ( " \033 [1;31m" ) );
906
+ caller -> print ( __CONST_TXT__ ( " \033 [1;31m" ) );
915
907
}
916
908
917
- response -> println ( " \u25B2 " );
909
+ caller -> println ( " \u25B2 " );
918
910
919
911
for ( i = 0 ; i < brokenPipePos; i++ ){
920
- response -> print ( ' ' );
912
+ caller -> print ( ' ' );
921
913
}
922
914
923
- response -> println ( " \u2514 The pipe broke here" );
915
+ caller -> println ( " \u2514 The pipe broke here" );
924
916
925
917
if ( formatting ){
926
- response -> print ( __CONST_TXT__ ( " \033 [0;37m" ) );
918
+ caller -> print ( __CONST_TXT__ ( " \033 [0;37m" ) );
927
919
}
928
920
929
921
if ( pipeChannel -> available () ){
930
- response -> print ( __CONST_TXT__ ( " Pipe data: " ) );
922
+ caller -> print ( __CONST_TXT__ ( " Pipe data: " ) );
931
923
while ( pipeChannel -> available () ){
932
- response -> print ( (char )pipeChannel -> read () );
924
+ caller -> print ( (char )pipeChannel -> read () );
933
925
}
934
- response -> println ();
926
+ caller -> println ();
935
927
936
928
}
937
929
938
- }
930
+ // }
939
931
940
932
}
941
933
0 commit comments