Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a 'paste' command to write a local file to the terminal #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,38 @@ static int cmd_break(int argc, char *argv[])
return MICROCOM_CMD_START;
}

static int cmd_paste(int argc, char *argv[])
{
int fd = -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to initialize.

int i = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leave uninitialized and see below.

int ret;
unsigned char buf[BUFSIZE];

if (argc < 2)
return MICROCOM_CMD_USAGE;

fd = open(argv[1], O_RDONLY);
if (fd == -1) {
ret = errno;
fprintf(stderr, "unable to open '%s': %s\n", argv[1], strerror(ret));
return -ret;
}

while (i > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move check after initializing i with proper value.

i = read(fd, buf, BUFSIZE);
if (i < 0) {
ret = errno;
fprintf(stderr, "%s\n", strerror(ret));
return -ret;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaks fd.

}

ios->write(ios, buf, i);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You call write with i=0 here on last iteration. Is that intended? Otherwise move check between read and write. also consider early exit here on failure.

}

close(fd);
return MICROCOM_CMD_START;
}

static int cmd_quit(int argc, char *argv[])
{
microcom_exit(0);
Expand Down Expand Up @@ -223,6 +255,11 @@ static struct cmd cmds[] = {
.name = "break",
.fn = cmd_break,
.info = "send break",
}, {
.name = "paste",
.fn = cmd_paste,
.info = "write contents of file to the terminal",
.help = "paste <file>",
}, {
.name = "quit",
.fn = cmd_quit,
Expand All @@ -234,7 +271,7 @@ static struct cmd cmds[] = {
}, {
.name = "x",
.fn = cmd_execute,
.info = "execute a script",
.info = "execute a script containing microcom commands",
.help = "x <scriptfile>",
}, {
.name = "log",
Expand Down
2 changes: 2 additions & 0 deletions microcom.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ struct ios_ops {
int fd;
};

#define BUFSIZE 1024

int mux_loop(struct ios_ops *); /* mux.c */
void init_terminal(void);
void restore_terminal(void);
Expand Down
2 changes: 0 additions & 2 deletions mux.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#include <stdarg.h>
#include <stdbool.h>

#define BUFSIZE 1024

/* This is called with buf[-2:0] being IAC SB COM_PORT_OPTION */
static int do_com_port_option(struct ios_ops *ios, unsigned char *buf, int len)
{
Expand Down