Skip to content

Commit

Permalink
Implement sampov2#18: Implements a basic console interface that allow…
Browse files Browse the repository at this point in the history
…s listing and loading presets.

+ Add help and license messages as cli options.
  • Loading branch information
jofemodo committed Oct 24, 2018
1 parent b99d4a3 commit 72726ba
Showing 1 changed file with 105 additions and 14 deletions.
119 changes: 105 additions & 14 deletions src/main-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ ADVISEDOF THE POSSIBILITY OF SUCH DAMAGE.

#include <signal.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>

#include <yc20-jack.h>
#include <foo-yc20-os.h>
Expand All @@ -52,20 +54,44 @@ void exit_cli(int sig)
std::cerr << "Exiting" << std::endl;
}

int main(int argc, char **argv)
{
std::string get_version() {
std::string version(VERSION_STR);
std::string svn_revision("$Revision: 106 $");

if (version == "") {
version = svn_revision.substr(11, svn_revision.find(" $", 11));
version = "svn-" + version;
}


#define L << std::endl <<
std::cerr << "Foo-YC20 (CLI) " << version
L "Copyright 2010-2011 Sampo Savolainen ([email protected]). All rights reserved."
return version;
}

void print_usage() {
#define L << std::endl <<
std::cerr << "Usage: foo-yc20-cli [OPTION] | [PRESET_FILE_PATH]"
L ""
L "Run foo-yc20 combo-organ emulator in headless mode. The following options are accepted:"
L ""
L " -h Display this help and exit"
L " -l Display license info and exit"
L "" << std::endl;
#undef L
}

void print_command_help() {
#define L << std::endl <<
std::cerr << "Available Commands:"
L " + get_presets : Print preset list"
L " + set_preset [PRESET_FILE_PATH] : Load preset from file"
L " + help : Print command help"
L " + exit : Exit"
L "" << std::endl;
#undef L
}

void print_license() {
#define L << std::endl <<
std::cerr << "Copyright 2010-2011 Sampo Savolainen ([email protected]). All rights reserved."
L "Redistribution and use in source and binary forms, with or without modification,"
L "are permitted provided that the following conditions are met:"
L " 1. Redistributions of source code must retain the above copyright notice,"
Expand All @@ -85,8 +111,64 @@ int main(int argc, char **argv)
L "PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF"
L "LIABILITY, WHETHERIN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE"
L "OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF"
L "ADVISEDOF THE POSSIBILITY OF SUCH DAMAGE." << std::endl;
#undef L
L "ADVISEDOF THE POSSIBILITY OF SUCH DAMAGE."
L "" << std::endl;
#undef L
}

void print_presets() {
DIR *dir;
struct dirent *ent;
std::string dirName(DEFAULT_CONFIG_DIR);
if ((dir = opendir (dirName.c_str())) != NULL) {
while ((ent = readdir (dir)) != NULL) {
if (ent->d_name[0]=='.') continue;
std::cout << ent->d_name << std::endl;
}
closedir (dir);
} else {
std::cerr << "Could not open presets directory!" << std::endl;
}
}

void process_command(YC20Jack &processor, char *command) {
char arg1[128];

if (strcmp(command, "get_presets\n") == 0) {
print_presets();
}
else if (sscanf(command, "set_preset %[a-zA-Z0-9_:/-.#]\n", arg1) == 1) {
std::string dirName(DEFAULT_CONFIG_DIR);
std::string fname(arg1);
std::string fpath=dirName + "/" + fname;
std::cerr << "Loading preset <" << fpath << ">" << std::endl;
processor.loadConfiguration(fpath);
}
else if (strcmp(command, "help\n") == 0) {
print_command_help();
}
else if (strcmp(command, "exit\n") == 0) {
exit_cli(0);
}
}

int main(int argc, char **argv)
{
std::cerr << "Foo-YC20 (CLI) " << get_version() << " (c)Sampo Savolainen 2010" << std::endl << std::endl;

std::string config_fpath;
if (argc > 1) {
std::string arg1(argv[1]);
if (arg1=="-h") {
print_usage();
return 0;
} else if (arg1=="-l") {
print_license();
return 0;
} else {
config_fpath=arg1;
}
}

YC20Jack processor;
processor.connect();
Expand All @@ -98,10 +180,9 @@ int main(int argc, char **argv)
processor.setDSP(yc20);
getUserData(yc20)->osc = osc;

if (argc > 1) {
std::string conf(argv[1]);
std::cerr << "using configuration file '" << conf << "'" << std::endl;
processor.loadConfiguration(conf);
if (!config_fpath.empty()) {
std::cerr << "Using configuration file '" << config_fpath << "'" << std::endl;
processor.loadConfiguration(config_fpath);
} else {
processor.loadConfiguration();
}
Expand All @@ -110,10 +191,20 @@ int main(int argc, char **argv)
run = true;
signal(SIGINT, exit_cli);

while(run) {
sleep(100); // ctrl-C interrupts sleep and runs exit_cli() which sets run to false
while (run) {
char line[128];
std::cout << "> ";
if (fgets(line, sizeof(line), stdin)) {
process_command(processor, line);
} else {
break;
}
}

//while(run) {
// sleep(100); // ctrl-C interrupts sleep and runs exit_cli() which sets run to false
//}

processor.deactivate();

processor.saveConfiguration();
Expand Down

0 comments on commit 72726ba

Please sign in to comment.