Commit 0845145f authored by Isabella Skořepová's avatar Isabella Skořepová
Browse files

Add autowrite command (interactive-only)

Remove dollar-counting message
parent 1aaae15a
......@@ -102,7 +102,7 @@ VALA_AUX := $(DIRECTORY).vala-aux/
VALAC := valac
# Add any additional flags to pass to the vala compiler.
VALAFLAGS := --pkg readline --pkg gio-2.0 -g
VALAFLAGS := --pkg readline --pkg gio-2.0 --pkg posix -g
# Set the C compiler.
CC := gcc
......@@ -144,7 +144,7 @@ BUILDMSG := Generating executable...
# Fast option
ifneq ($(INCREMENTAL), true)
$(BUILD_DIR)$(TARGET): src/*.vala src/commands/*.vala Makefile
@echo ' $(BUILDMSG) ';mkdir -p $(BUILD_DIR) && $(VALAC) $(wildcard src/*.vala) $(wildcard src/*/*.vala) -X -w -o $(BUILD_DIR)$(TARGET) --pkg gio-2.0 --pkg readline --pkg posix -X -lreadline -g
@echo ' $(BUILDMSG) ';mkdir -p $(BUILD_DIR) && $(VALAC) $(wildcard src/*.vala) $(wildcard src/*/*.vala) -X -w -o $(BUILD_DIR)$(TARGET) $(VALAFLAGS) -X -lreadline -g
@echo " Done"
# At first slower but then faster option, sometimes buggy
else
......
/*
* Copyright 2015 Jakub Skořepa <jakub@skorepa.info>
*
* This file is part of dserial
*
* dserial is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* dserial is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with dserial.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
public class AutowriteCommand : Object, Command
{
public int execute(string[] args, ApplicationState state)
{
if(args.length == 0) {
return error(Err.NO_AUTOWRITE_STATE_SPECIFIED);
}
if(args[0].down() == "on") {
state.autowrite = true;
} else if(args[0].down() == "off") {
state.autowrite = false;
} else {
return error(Err.WRONG_AUTOWRITE_STATE_SPECIFIED);
}
return 0;
}
public string name()
{
return "autowrite";
}
public string[]? args(string[] args, ApplicationState state)
{
if(args.length == 0) {
return {"on", "off"};
} else {
return {};
}
}
}
......@@ -24,6 +24,7 @@ public class ApplicationState: Object {
public bool running = true;
public Array<Command> command_list;
public bool interactive = false;
public bool autowrite = false;
public static bool is_not_initialized = true;
public ApplicationState() {
......
......@@ -44,6 +44,7 @@ public class HelpCommand : Object, Command
stdout.printf("autodetect\t\t\tAutodetects ports\n");
stdout.printf("bind <port>\t\t\tBinds port for other commands (interactive only)\n");
stdout.printf("unbind\t\t\t\tRemoves binding (interactive only)\n");
stdout.printf("autowrite on|off\t\tSet whether every line should start with write\n");
stdout.printf("exit\t\t\t\tExits interactive session (interactive only)\n");
}else if(args[0] == "interactive") {
stdout.printf("Enters interactive session.\n");
......@@ -130,6 +131,10 @@ public class HelpCommand : Object, Command
stdout.printf("Interactive only\n");
stdout.printf("Other commands requiring port automatically get this value\n");
stdout.printf("It also makes them faster due to preexisting connection\n");
}else if(args[0] == "autowrite") {
stdout.printf("Synopsis: autowrite on|off\n");
stdout.printf("Turns automatic write command preparation on/off\n");
stdout.printf("Try \"autowrite on\" to check it out!\n");
}else if(args[0] == "unbind") {
stdout.printf("Synopsis: unbind\n");
stdout.printf("Interactive only\n");
......
......@@ -21,7 +21,7 @@
public class InteractiveCommand : Object, Command
{
static const string[] cmds = { "interactive", "help", "list", "add", "info", "get", "set", "values", "open", "close", "write", "destroy", "read", "autodetect" };
static const string[] cmds = { "interactive", "help", "list", "add", "info", "get", "set", "values", "open", "close", "write", "destroy", "read", "autodetect", "autowrite" };
static int list_index = 0;
static int len = 0;
......@@ -59,42 +59,67 @@ public class InteractiveCommand : Object, Command
return (matches);
}
private static bool line_end = false;
private static string? static_line;
private static void readline_linehandler(string? line)
{
line_end = true;
static_line = line;
}
public int execute(string[] args, ApplicationState state)
{
state.interactive = true;
stdout.printf("dserial: Serial communication program\n");
stdout.printf("License: GPL3+\n");
stdout.printf("Version: 0.2\n");
stdout.printf("Version: 0.3\n");
stdout.printf("Write help for help\n");
Readline.History.read(Environment.get_variable("HOME")+"/.dserial-history");
Readline.attempted_completion_function = readline_completion;
Readline.callback_handler_install("$ ", readline_linehandler);
while(state.running) {
var line = Readline.readline("$ ");
if (line == null) {
stdout.printf("\n");
run_func(state,{"exit"});
break;
}
Readline.History.add(line);
try {
string[] cmd;
Shell.parse_argv(line,out cmd);
int ret = run_func(state, cmd);
if(ret != 0 && ret != 100) {
stdout.printf("Error code: %d\n",ret);
}
} catch (ShellError e) {
if(e is ShellError.EMPTY_STRING) {
continue;
} else if(e is ShellError.BAD_QUOTING) {
stdout.printf("Bad quoting\n");
}else {
stdout.printf("Encountered error while parsing command\n");
}
}
line_end = false;
Readline.set_prompt("$ ");
if(state.autowrite) {
Readline.insert_text("write ");
}
Readline.redisplay();
while(!line_end)
Readline.callback_read_char();
Readline.set_prompt("");
Readline.redisplay();
string? line = static_line;
if (line == null) {
stdout.printf("\n");
run_func(state,{"exit"});
return 0;
}
Readline.History.add(line);
try {
string[] cmd;
Shell.parse_argv(line,out cmd);
int ret = run_func(state, cmd);
if(ret != 0 && ret != 100) {
stdout.printf("Error code: %d\n",ret);
}
} catch (ShellError e) {
if(e is ShellError.EMPTY_STRING) {
continue;
} else if(e is ShellError.BAD_QUOTING) {
stdout.printf("Bad quoting\n");
}else {
stdout.printf("Encountered error while parsing command\n");
}
}
}
Readline.History.write(Environment.get_variable("HOME")+"/.dserial-history");
......
......@@ -32,7 +32,7 @@ public class WriteCommand : Object, Command
return ch-LETTER_BIG_A+10;
if(ch >= LETTER_SMALL_A && ch <= LETTER_SMALL_A+5)
return ch-LETTER_SMALL_A+10;
return -1;
return 255;
}
public int execute(string[] args, ApplicationState state)
......@@ -79,8 +79,6 @@ public class WriteCommand : Object, Command
dollar_positions.append_val(i);
}
stdout.printf("Dollar_count = %u\n",dollar_positions.length);
// Check dollar count
if(dollar_positions.length%2 == 1) {
return error(Err.NOT_MATCHING_DOLLAR_COUNT);
......@@ -92,7 +90,7 @@ public class WriteCommand : Object, Command
var number = new BigInt();
if (dollar_positions.index(i+1) == DOLLAR) {
number.add_uint8(DOLLAR);
}else{
} else {
// detect base
uint8 n_base = 10;
int base_pos = dollar_positions.index(i)+1;
......
......@@ -30,6 +30,8 @@ enum Err {
WRONG_VALUE,
PORT_NOT_SPECIFIED_VALUES,
NO_MESSAGE,
NO_AUTOWRITE_STATE_SPECIFIED,
WRONG_AUTOWRITE_STATE_SPECIFIED,
NEGATIVE_LENGTH,
NOT_ASCII_FORMAT_STRING,
INVALID_FORMAT_CHARACTER,
......@@ -74,6 +76,12 @@ int error(Err error) {
case Err.NO_MESSAGE:
stdout.printf("No message specified\n");
return 10;
case Err.NO_AUTOWRITE_STATE_SPECIFIED:
stdout.printf("No autowrite state specified\nautowrite ON|OFF\n");
return 11;
case Err.WRONG_AUTOWRITE_STATE_SPECIFIED:
stdout.printf("Wrong autowrite state specified\nautowrite ON|OFF\n");
return 12;
case Err.NEGATIVE_LENGTH:
stdout.printf("Length must be positive\n");
return 13;
......
......@@ -35,7 +35,7 @@ int run_func(ApplicationState state, string[] args) {
return state.command_list.index(i).execute(other_args, state);
}
}
stdout.printf(@"Unknown function $f\n");
stdout.printf("Unknown function %s\n",f);
if(!state.interactive)
stdout.printf("Execute \"dserial help\" for help\n");
else
......@@ -76,6 +76,8 @@ void init_list(ApplicationState state) {
state.command_list.append_val(c);
c = new WriteCommand();
state.command_list.append_val(c);
c = new AutowriteCommand();
state.command_list.append_val(c);
}
/*=====================================
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment