Merge branch 'player' into edge
[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
cc63083e 19
0325af12
AW
20 // Act depending on command
21 switch( check_sum ){
cc63083e
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;
25 case play_command_checksum : this->play_command(get_arguments(possible_command)); break;
0325af12
AW
26 }
27}
28
0325af12
AW
29// Convert a path indication ( absolute or relative ) into a path ( absolute )
30string SimpleShell::absolute_from_relative( string path ){
31 if( path[0] == '/' ){ return path; }
32 if( path[0] == '.' ){ return this->current_path; }
33 return this->current_path + path;
34}
35
36// Act upon an ls command
37// Convert the first parameter into an absolute path, then list the files in that path
38void SimpleShell::ls_command( string parameters ){
39 string folder = this->absolute_from_relative( parameters );
40 DIR* d;
41 struct dirent* p;
42 d = opendir(folder.c_str());
43 if(d != NULL) {
44 while((p = readdir(d)) != NULL) { this->kernel->serial->printf("%s\r\n", lc(string(p->d_name)).c_str()); }
45 } else {
46 this->kernel->serial->printf("Could not open directory %s \r\n", folder.c_str());
47 }
48}
49
50// Change current absolute path to provided path
51void SimpleShell::cd_command( string parameters ){
52 string folder = this->absolute_from_relative( parameters );
53 if( folder[folder.length()-1] != '/' ){ folder += "/"; }
54 DIR *d;
55 struct dirent *p;
56 d = opendir(folder.c_str());
57 if(d == NULL) {
58 this->kernel->serial->printf("Could not open directory %s \r\n", folder.c_str() );
59 }else{
60 this->current_path = folder;
61 }
62}
63
64// Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
65void SimpleShell::cat_command( string parameters ){
66
67 // Get parameters ( filename and line limit )
68 string filename = this->absolute_from_relative(shift_parameter( parameters ));
69 string limit_paramater = shift_parameter( parameters );
70 int limit = -1;
71 if( limit_paramater != "" ){ limit = int(atof(limit_paramater.c_str())); }
72
73 // Open file
74 FILE *lp = fopen(filename.c_str(), "r");
75 string buffer;
76 int c;
77 int newlines = 0;
78
79 // Print each line of the file
80 while ((c = fgetc (lp)) != EOF){
81 if( char(c) == '\n' ){ newlines++; }
82 this->kernel->serial->putc(c);
83 if( newlines == limit ){ break; }
84 };
85 fclose(lp);
86
87}
88
cc63083e
AW
89// Play a gcode file by considering each line as if it was received on the serial console
90void SimpleShell::play_command( string parameters ){
0325af12 91
cc63083e
AW
92 // Get filename
93 string filename = this->absolute_from_relative(shift_parameter( parameters ));
94
95 // Open file
96 FILE *lp = fopen(filename.c_str(), "r");
97 string buffer;
98 int c;
99
100 // Print each line of the file
101 while ((c = fgetc (lp)) != EOF){
102 if (c == '\n'){
103 this->kernel->serial->printf("%s\n", buffer.c_str());
104 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &buffer);
105 buffer.clear();
106 }else{
107 buffer += c;
108 }
109 };
110 fclose(lp);
0325af12 111
cc63083e
AW
112
113
114}