config-get and config-set now work
[clinton/Smoothieware.git] / src / modules / utils / simpleshell / SimpleShell.cpp
CommitLineData
0325af12
AW
1#include "mbed.h"
2#include "libs/Kernel.h"
3#include "SimpleShell.h"
4#include "libs/nuts_bolts.h"
5#include "libs/utils.h"
6
7
8void SimpleShell::on_module_loaded(){
9 this->current_path = "/";
10 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
11}
12
13// When a new line is received, check if it is a command, and if it is, act upon it
14void SimpleShell::on_console_line_received( void* argument ){
15 string possible_command = *static_cast<string*>(argument);
16
17 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
18 unsigned short check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
19
20 // Act depending on command
21 switch( check_sum ){
cebe90b6
AW
22 case ls_command_checksum : this->ls_command( get_arguments(possible_command)); break;
23 case cd_command_checksum : this->cd_command( get_arguments(possible_command)); break;
24 case cat_command_checksum : this->cat_command(get_arguments(possible_command)); break;
0325af12
AW
25 }
26}
27
0325af12
AW
28// Convert a path indication ( absolute or relative ) into a path ( absolute )
29string SimpleShell::absolute_from_relative( string path ){
30 if( path[0] == '/' ){ return path; }
31 if( path[0] == '.' ){ return this->current_path; }
32 return this->current_path + path;
33}
34
35// Act upon an ls command
36// Convert the first parameter into an absolute path, then list the files in that path
37void SimpleShell::ls_command( string parameters ){
38 string folder = this->absolute_from_relative( parameters );
39 DIR* d;
40 struct dirent* p;
41 d = opendir(folder.c_str());
42 if(d != NULL) {
43 while((p = readdir(d)) != NULL) { this->kernel->serial->printf("%s\r\n", lc(string(p->d_name)).c_str()); }
44 } else {
45 this->kernel->serial->printf("Could not open directory %s \r\n", folder.c_str());
46 }
47}
48
49// Change current absolute path to provided path
50void SimpleShell::cd_command( string parameters ){
51 string folder = this->absolute_from_relative( parameters );
52 if( folder[folder.length()-1] != '/' ){ folder += "/"; }
53 DIR *d;
54 struct dirent *p;
55 d = opendir(folder.c_str());
56 if(d == NULL) {
57 this->kernel->serial->printf("Could not open directory %s \r\n", folder.c_str() );
58 }else{
59 this->current_path = folder;
60 }
61}
62
63// Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
64void SimpleShell::cat_command( string parameters ){
65
66 // Get parameters ( filename and line limit )
67 string filename = this->absolute_from_relative(shift_parameter( parameters ));
68 string limit_paramater = shift_parameter( parameters );
69 int limit = -1;
70 if( limit_paramater != "" ){ limit = int(atof(limit_paramater.c_str())); }
71
72 // Open file
73 FILE *lp = fopen(filename.c_str(), "r");
74 string buffer;
75 int c;
76 int newlines = 0;
77
78 // Print each line of the file
79 while ((c = fgetc (lp)) != EOF){
80 if( char(c) == '\n' ){ newlines++; }
81 this->kernel->serial->putc(c);
82 if( newlines == limit ){ break; }
83 };
84 fclose(lp);
85
86}
87
88
89