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

allow to pass filename in autocmd parameter #219

Open
wants to merge 2 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
6 changes: 5 additions & 1 deletion doc/man.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion doc/man6/cap32.6
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ If the mapping file is not defined or not found, Caprice32 will default to a sta
.SH OPTIONS
.PP
.TP
\fB\-a\fR, \fB\-\-autocmd\fR=\fICOMMAND\fR
\fB\-a\fR, \fB\-\-autocmd\fR=\fICOMMAND\fR | \fI@FILENAME\fR
pass command to execute to the emulator. The option can be repeated to pass multiple commands. For example: cap32 -a 'print "Hello"' -a 'print "World"'
.RS
with prefix "@", a filename is specified. All commands in the file are passed. For example: cap32 -a '@/tmp/cmd.txt'
.RE
.TP
\fB\-c\fR, \fB\-\-cfg_file\fR=\fIFILE\fR
use FILE as the emulator configuration file.
Expand Down
30 changes: 28 additions & 2 deletions src/argparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ std::string replaceCap32Keys(std::string command)
return command;
}

void inlineString(CapriceArgs& args, char* data)
{
args.autocmd += replaceCap32Keys(data);
args.autocmd += "\n";
}

void parseArguments(int argc, char **argv, std::vector<std::string>& slot_list, CapriceArgs& args)
{
int option_index = 0;
Expand All @@ -111,9 +117,29 @@ void parseArguments(int argc, char **argv, std::vector<std::string>& slot_list,
switch (c)
{
case 'a':
if (optarg[0] == '@')
{
/* Well, this is a file content we want to inject */
std::ifstream inlineStream(&optarg[1], std::ifstream::in);
if(!inlineStream.is_open())
{
/* The filename is not correct, it may be a string to inject */
LOG_VERBOSE("Append to autocmd: " << optarg);
inlineString(args, optarg);
break;
}
/* Inject all lines from the file */
LOG_VERBOSE("Append file content to autocmd: " << &optarg[1]);
while(inlineStream.good())
{
char chLine[256];
inlineStream.getline(chLine, 256);
inlineString(args, chLine);
}
break;
}
LOG_VERBOSE("Append to autocmd: " << optarg);
args.autocmd += replaceCap32Keys(optarg);
args.autocmd += "\n";
inlineString(args, optarg);
break;

case 'c':
Expand Down