Compare commits

...

13 commits
v1.3 ... master

Author SHA1 Message Date
zawz
199390d9fa update for newer ztd version 2021-08-12 18:37:44 +02:00
zawz
9efd8cf9af Output to mim format + bugfixes 2020-05-22 16:15:48 +02:00
zawz
9e984f6d38 Update to new ztd 2020-05-03 17:03:45 +02:00
zawz
df123ef150 Better help gen 2020-03-03 13:23:48 +01:00
zawz
f10a6900c6 Move to new zfd format 2020-03-03 10:25:14 +01:00
zawz
22d3462f09 Fix command count 2020-02-18 16:50:47 +01:00
zawz
92cf2c603c Fix stdin mim 2020-02-18 16:50:18 +01:00
zawz
6ff844b772 Changed stdin method 2020-02-18 16:17:54 +01:00
zawz
2b8da6aa30 Enable optimisations 2020-02-13 23:18:51 +01:00
zawz
99f98b165a strval cleanup 2020-02-13 23:18:33 +01:00
zawz
0d7213d41a Code cleanup 2020-01-31 15:46:17 +01:00
zawz
8c944a49af add gitignore 2019-11-01 07:42:17 +01:00
zawz
b742fa5ae5 README dependency fix 2019-09-26 11:51:08 +02:00
20 changed files with 230 additions and 161 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
zmidimap
obj
Zmakefile

View file

@ -11,6 +11,8 @@ CC=g++
CXXFLAGS= -I$(IDIR) -Wall -pedantic -std=c++17
ifeq ($(DEBUG),true)
CXXFLAGS += -g
else
CXXFLAGS += -O2
endif
ifeq ($(STATIC),true)
LDFLAGS += -l:libztd.a

View file

@ -2,7 +2,7 @@
Map midi signals coming from ALSA midi devices to shell commands
Dependencies: alsa-utils , [ztd](https://github.com/zawwz/ztd)
Dependencies: alsa-utils
## Installing
@ -14,10 +14,12 @@ Download the .deb package then run: `sudo dpkg -i zmidimap.deb ; sudo apt -f ins
### Standalone
Download ``zmidimap-static.tar.gz`` for a standalone executable without library dependency
Download ``zmidimap.tar.gz`` for a standalone executable
### From source
Depends on: [ztd](https://github.com/zawwz/ztd)
``make -j6`` for a shared build
``STATIC=true make -j6`` for a static build

View file

@ -1,49 +1,43 @@
Device "Launch Control"
//display value 0:127 for knobs 21-28
Command controller
id=21:28
shell=echo "Knob #$id ch$channel:$value"
shell='echo "Knob #$id ch$channel:$value"'
//display value 0:127 for knob 41 from any channel
//display value -100:100 for knob 41 on channel 0
Command controller
id=41
channel=*
shell=echo "Knob #$id ch$channel:$value"
//display value -100:100 for knob 42 on channel 0
Command controller
id=42
channel=0
remap=-100:100
shell=echo "Knob #$id ch0:$value r:$rawvalue"
shell='echo "Knob #$id ch0:$value r:$rawvalue"'
//display value 0:1:0 for knob 42 on channel 1 (first half)
//display value 0:1:0 for knob 41 on channel 1 (first half)
Command controller
id=42
channel=1
range=0:63
remap=0:1
float=true
shell=echo "Knob #$id ch1:$value r:$rawvalue"
shell='echo "Knob #$id ch1:$value r:$rawvalue"'
//display value 0:1:0 for knob 42 on channel 1 (second half)
//display value 0:1:0 for knob 41 on channel 1 (second half)
Command controller
id=42
channel=1
range=64:127
remap=1:0
float=true
shell=echo "Knob #$id ch1:$value r:$rawvalue"
shell='echo "Knob #$id ch1:$value r:$rawvalue"'
Device "Launchpad S"
//display any note pressed and its velocity
Command note
shell=echo "Note $id on velocity:$velocity"
shell=echo Note $id on velocity:$velocity
//display any note off
Command note
trigger=0
shell=echo "Note $id off"
shell=echo Note $id off

View file

@ -10,46 +10,38 @@
//displays value 0:127 for knobs 21-28
type=controller
id=21:28
shell=echo "Knob #$id ch$channel:$value"
},
// KNOBS LOW
{
//KNOB L1
//displays value 0:127 for knob 41 from any channel
type=controller
id=41
shell=echo "Knob #$id ch$channel:$value"
shell='echo "Knob #$id ch$channel:$value"'
},
{
//KNOB L2
//displays value -100:100 for knob 42 on channel 0
//displays value -100:100 for knob 41 on channel 0
type=controller
id=42
channel=0
remap=-100:100
shell=echo "Knob #$id ch0:$value r:$rawvalue"
shell='echo "Knob #$id ch0:$value r:$rawvalue"'
},
{
//KNOB L3 H1
//displays value 0:1:0 for knob 42 on channel 1 (first half)
//displays value 0:1:0 for knob 41 on channel 1 (first half)
type=controller
id=42
channel=1
range=0:63
remap=0:1
float=true
shell=echo "Knob #$id ch1:$value r:$rawvalue"
shell='echo "Knob #$id ch1:$value r:$rawvalue"'
},
{
//KNOB L3 H2
//displays value 0:1:0 for knob 42 on channel 1 (second half)
//displays value 0:1:0 for knob 41 on channel 1 (second half)
type=controller
id=42
channel=1
range=64:127
remap=1:0
float=true
shell=echo "Knob #$id ch1:$value r:$rawvalue"
shell='echo "Knob #$id ch1:$value r:$rawvalue"'
}
]
}
@ -61,13 +53,13 @@
{
// ANY NOTE ON
type=note
shell=echo "Note $id on velocity:$velocity"
shell=echo Note $id on velocity:$velocity
},
{
// ANY NOTE OFF
type=note
trigger=0
shell=echo "Note $id off"
shell=echo Note $id off
}
]
}

View file

@ -1,11 +1,38 @@
FORMAT_FOLDER=help_format
IDIR=include
cp $FORMAT_FOLDER/help_template_head $IDIR/help.h
SLASHSCRIPT='s|\\|\\\\|g;s|\"|\\\"|g'
NEWLINESCRIPT=':a;N;$!ba;s/\n/\\n/g;'
echo "#define ZFD_FORMAT \"zmidimap$(sed -n -e 'H;${x;s/\n/\\n/g;s/^,//;p;}' $FORMAT_FOLDER/zfd-format)\"" >> $IDIR/help.h
echo "#define MIM_FORMAT \"zmidimap$(sed -n -e 'H;${x;s/\n/\\n/g;s/^,//;p;}' $FORMAT_FOLDER/mim-format)\"" >> $IDIR/help.h
echo "#define SHELL_FORMAT \"zmidimap$(sed -n -e 'H;${x;s/\n/\\n/g;s/^,//;p;}' $FORMAT_FOLDER/shell-format)\"" >> $IDIR/help.h
echo "#define COMMAND_TAGS \"zmidimap$(sed -n -e 'H;${x;s/\n/\\n/g;s/^,//;p;}' $FORMAT_FOLDER/command-tags)\"" >> $IDIR/help.h
filetocstr()
{
sed -e $SLASHSCRIPT "$1" | sed $NEWLINESCRIPT
}
cat $FORMAT_FOLDER/help_template_tail >> $IDIR/help.h
gen_line()
{
name="$(basename "$1")"
echo "#define $(echo "$name" | tr '[:lower:]' '[:upper:]') \"$(filetocstr "$1")\""
}
help_header()
{
echo '#ifndef HELP_H
#define HELP_
'
}
help_footer()
{
echo '
#endif //HELP_H'
}
help_header > "$IDIR/help.h"
for I in help_format/*
do
gen_line "$I" >> "$IDIR/help.h"
done
help_footer >> "$IDIR/help.h"

View file

@ -1,2 +0,0 @@
#ifndef HELP_H
#define HELP_H

View file

@ -1 +0,0 @@
#endif //HELP_H

View file

@ -20,6 +20,7 @@ Device <name>
*shell: shell command to be executed
> mandatory
Shell command can be concatenated with \"\" or ''
Comments are written by starting a line with //
see --command-tags for optional command tags
Shell commands can be concatenated with "" or ''
Comments are written with // or # and end at end of line
Multiple tags can be put on the same line by separating them with ;
See --command-tags for optional command tags

View file

@ -1,5 +1,7 @@
-- [ZFD FILE FORMAT] --
ZFD format: http://zawz.net/doc/ztd/zfd.html
[<device>,<device>]
-- <device> format --
@ -24,5 +26,5 @@
*shell: shell command to be executed
> mandatory
Shell command can be concatenated with \"\" or ''
Shell commands can be concatenated with "" or ''
see --command-tags for optional command tags

View file

@ -17,7 +17,7 @@ public:
bool start_loop();
void run_signal(char* buff);
bool import_chunk(ztd::chunkdat const& ch);
bool import_chunk(const ztd::chunkdat& ch);
ztd::chunkdat export_chunk();
std::string name;

View file

@ -3,7 +3,9 @@
#include <ztd/filedat.hpp>
ztd::chunkdat mimtochk(const std::string& mim);
ztd::chunkdat mimtochk(std::string mim);
std::string chktomim(ztd::chunkdat const& chk, std::string const& aligner);
std::string file_strimport(const std::string& path);

View file

@ -1,7 +1,9 @@
#ifndef HELP_H
#define HELP_H
#define ZFD_FORMAT "zmidimap\n-- [ZFD FILE FORMAT] --\n\n[<device>,<device>]\n\n-- <device> format --\n {\n name=<name>\n commands=[<command>,<command>]\n }\n--\n*name: string referring to client name of the device\n\n<command> format\n {\n type=<type>\n shell=<shell command>\n <tag>=<value>\n <tag>=<value>\n ...\n }\n--\n*type: type of the signal: note/controller/pitch/system/connect/disconnect\n > mandatory\n*shell: shell command to be executed\n > mandatory\n\nShell command can be concatenated with \"\" or ''\nsee --command-tags for optional command tags"
#define MIM_FORMAT "zmidimap\n-- [MIM FILE FORMAT] --\n\nDevice <name>\n Command <type>\n shell=<shell command>\n <tag>=<value>\n <tag>=<value>\n ...\n\nDevice <name>\n...\n\n--\n*name: string referring to client name of the device\n > mandatory\n*type: type of the signal: note/controller/pitch/system/connect/disconnect\n > mandatory\n*shell: shell command to be executed\n > mandatory\n\nShell command can be concatenated with \"\" or ''\nsee --command-tags for optional command tags"
#define SHELL_FORMAT "zmidimap\n-- [SHELL FORMAT] --\n\nShell command follows regular shell format\n\n-- [Environment] --\n\n[Note]\n $id: id of the note\n $channel: channel of the note\n $velocity: velocity of the note\n\n[Controller]\n $value: value of the controller (remapped)\n $id: id of the controller\n $channel: channel of the controller\n $rawvalue: original value of the controller\n\n[Pitch]\n $value: value of the bend (remapped)\n $rawvalue: original value of the bend\n\n[System]\n $code: hexadecimal code of the system signal"
#define COMMAND_TAGS "zmidimap\n-- [COMMAND TAGS] --\n\n[Note tags]\n id=<interval>\n channel=<x/*>\n trigger=<interval>\n--\n *id: note id from 0 to 127\n > optional, default 0:127\n *channel: value from 0 to 16 for channel, * for any channel\n > optional, default *\n *trigger: note velocity from 0 to 127 that triggers the command\n > optional, default 1:127\n\n[Controller tags]\n id=<interval>\n channel=<x/*>\n range=<interval>\n remap=<interval>\n float=<true/false>\n--\n *id: controller id from 0 to 127\n > optional, default 0:127\n *channel: value from 0 to 16 for channel, * for any channel\n > optional, default *\n *range: controller value from 0 to 127 that triggers command\n > optional, default 0:127\n *remap: remaps the range to given interval\n > optional, default same as range\n *float: boolean value defining if output is a floating point value\n > optional, default false\n\n[Pitch bend tags]\n range=<interval>\n remap=<interval>\n float=<true/false>\n--\n *range: controller value from -8192 to 8191 that triggers command\n > optional, default -8192:8191\n *remap: remaps the range to given interval\n > optional, default same as range\n *float: boolean value defining if output is a floating point value\n > optional, default false\n\n[Interval Format]\n x:y range from x to y\n x single value x\n * all possible values"
#define HELP_
#define COMMAND_TAGS "-- [COMMAND TAGS] --\n\n[Note tags]\n id=<interval>\n channel=<x/*>\n trigger=<interval>\n--\n *id: note id from 0 to 127\n > optional, default 0:127\n *channel: value from 0 to 16 for channel, * for any channel\n > optional, default *\n *trigger: note velocity from 0 to 127 that triggers the command\n > optional, default 1:127\n\n[Controller tags]\n id=<interval>\n channel=<x/*>\n range=<interval>\n remap=<interval>\n float=<true/false>\n--\n *id: controller id from 0 to 127\n > optional, default 0:127\n *channel: value from 0 to 16 for channel, * for any channel\n > optional, default *\n *range: controller value from 0 to 127 that triggers command\n > optional, default 0:127\n *remap: remaps the range to given interval\n > optional, default same as range\n *float: boolean value defining if output is a floating point value\n > optional, default false\n\n[Pitch bend tags]\n range=<interval>\n remap=<interval>\n float=<true/false>\n--\n *range: controller value from -8192 to 8191 that triggers command\n > optional, default -8192:8191\n *remap: remaps the range to given interval\n > optional, default same as range\n *float: boolean value defining if output is a floating point value\n > optional, default false\n\n[Interval Format]\n x:y range from x to y\n x single value x\n * all possible values"
#define MIM_FORMAT "-- [MIM FILE FORMAT] --\n\nDevice <name>\n Command <type>\n shell=<shell command>\n <tag>=<value>\n <tag>=<value>\n ...\n Command <type>\n ...\n\nDevice <name>\n...\n\n--\n*name: string referring to client name of the device\n > mandatory\n*type: type of the signal: note/controller/pitch/system/connect/disconnect\n > mandatory\n*shell: shell command to be executed\n > mandatory\n\nShell commands can be concatenated with \"\" or ''\nComments are written with // or # and end at end of line\nMultiple tags can be put on the same line by separating them with ;\nSee --command-tags for optional command tags"
#define SHELL_FORMAT "-- [SHELL FORMAT] --\n\nShell command follows regular shell format\n\n-- [Environment] --\n\n[Note]\n $id: id of the note\n $channel: channel of the note\n $velocity: velocity of the note\n\n[Controller]\n $value: value of the controller (remapped)\n $id: id of the controller\n $channel: channel of the controller\n $rawvalue: original value of the controller\n\n[Pitch]\n $value: value of the bend (remapped)\n $rawvalue: original value of the bend\n\n[System]\n $code: hexadecimal code of the system signal"
#define ZFD_FORMAT "-- [ZFD FILE FORMAT] --\n\nZFD format: http://zawz.net/doc/ztd/zfd.html\n\n[<device>,<device>]\n\n-- <device> format --\n {\n name=<name>\n commands=[<command>,<command>]\n }\n--\n*name: string referring to client name of the device\n\n<command> format\n {\n type=<type>\n shell=<shell command>\n <tag>=<value>\n <tag>=<value>\n ...\n }\n--\n*type: type of the signal: note/controller/pitch/system/connect/disconnect\n > mandatory\n*shell: shell command to be executed\n > mandatory\n\nShell commands can be concatenated with \"\" or ''\nsee --command-tags for optional command tags"
#endif //HELP_H

View file

@ -15,6 +15,6 @@ void device_check();
void announce_loop();
void filetime_loop(std::string filepath);
void filetime_loop(std::string const& filepath);
#endif //SYSTEM_HPP

View file

@ -36,7 +36,7 @@ static std::string dequote(const std::string& in)
return ret;
}
static bool _isNum(char a)
static bool _isNum(const char& a)
{
return (a>='0' && a<='9');
}
@ -63,12 +63,12 @@ bool Device::start_loop()
return true;
}
std::pair<int32_t,int32_t> importRange(ztd::chunkdat const& ch, std::string const& tag, int32_t low, int32_t high)
std::pair<int32_t,int32_t> importRange(const ztd::chunkdat& ch, const std::string& tag, int32_t low, int32_t high)
{
ztd::chunkdat* pch=ch.subChunkPtr(tag);
if(pch != nullptr)
{
std::string str=pch->strval();
std::string str=*pch;
auto tpos=str.find(':');
if (str=="*") //whole range
{
@ -76,25 +76,25 @@ std::pair<int32_t,int32_t> importRange(ztd::chunkdat const& ch, std::string cons
}
else if(tpos == std::string::npos) //single value
{
low=stoi(str);
low=std::stoi(str);
high=low;
}
else //range
{
low=stoi(str.substr(0,tpos));
low=std::stoi(str.substr(0,tpos));
tpos++;
high=stoi(str.substr(tpos, str.size()-tpos));
high=std::stoi(str.substr(tpos, str.size()-tpos));
}
}
return std::make_pair(low, high);
}
std::pair<float,float> importRangeFloat(ztd::chunkdat const& ch, std::string const& tag, float low, float high)
std::pair<float,float> importRangeFloat(const ztd::chunkdat& ch, const std::string& tag, float low, float high)
{
ztd::chunkdat* pch=ch.subChunkPtr(tag);
if(pch != nullptr)
{
std::string str=pch->strval();
std::string str=*pch;
auto tpos=str.find(':');
if(tpos == std::string::npos)
{
@ -113,12 +113,12 @@ std::pair<float,float> importRangeFloat(ztd::chunkdat const& ch, std::string con
return std::make_pair(low, high);
}
bool importBool(ztd::chunkdat const& ch, std::string const& tag, bool defbool)
bool importBool(const ztd::chunkdat& ch, const std::string& tag, const bool& defbool)
{
ztd::chunkdat* pch=ch.subChunkPtr(tag);
if(pch != nullptr)
{
std::string str=pch->strval();
std::string str=*pch;
if( str == "true" )
return true;
else if( str == "false" )
@ -127,30 +127,30 @@ bool importBool(ztd::chunkdat const& ch, std::string const& tag, bool defbool)
return defbool;
}
bool Device::import_chunk(ztd::chunkdat const& ch)
bool Device::import_chunk(const ztd::chunkdat& ch)
{
ztd::chunkdat& cch = ch["commands"];
this->name=dequote(ch["name"].strval());
this->name=dequote(ch["name"]);
for(int i=0 ; i<cch.listSize() ; i++)
{
ztd::chunkdat& tch=cch[i];
std::string tstr=tch["type"].strval();
std::string tstr=tch["type"];
if(tstr == "system") //type system
{
std::string shell;
shell=tch["shell"].strval();
shell=tch["shell"];
this->sysCommands.push_back(SystemCommand(shell));
}
else if (tstr == "connect")
{
std::string shell;
shell=tch["shell"].strval();
shell=tch["shell"];
this->connectCommands.push_back(ConnectCommand(shell));
}
else if (tstr == "disconnect")
{
std::string shell;
shell=tch["shell"].strval();
shell=tch["shell"];
this->disconnectCommands.push_back(DisconnectCommand(shell));
}
else
@ -162,13 +162,13 @@ bool Device::import_chunk(ztd::chunkdat const& ch)
std::pair<float,float> floatpair;
//channel
if(tch.subChunkPtr("channel") == nullptr || tch["channel"].strval()=="*")
if(tch.subChunkPtr("channel") == nullptr || tch["channel"] == "*")
channel=-1;
else
channel=stoi(tch["channel"].strval());
channel=std::stoi(tch["channel"]);
//shell
shell=tch["shell"].strval();
shell=tch["shell"];
//type
if(tstr == "note") //type note
@ -177,13 +177,11 @@ bool Device::import_chunk(ztd::chunkdat const& ch)
uint8_t id_high=127;
uint8_t low=1;
uint8_t high=127;
std::string tt;
//id
intpair=importRange(tch, "id", id_low, id_high);
id_low=intpair.first;
id_high=intpair.second;
// id=stoi(tch["id"].strval());
//trigger
intpair = importRange(tch, "trigger", low, high);
@ -194,7 +192,6 @@ bool Device::import_chunk(ztd::chunkdat const& ch)
{
this->noteCommands[i].push_back(NoteCommand(channel,low,high,shell));
}
this->nb_command++;
}
else if(tstr == "controller") //type controller
{
@ -228,7 +225,6 @@ bool Device::import_chunk(ztd::chunkdat const& ch)
{
this->ctrlCommands[i].push_back(ControllerCommand(channel,min,max,mapMin,mapMax,floating,shell));
}
this->nb_command++;
}
else if(tstr == "pitch") //type pitch bend
{
@ -252,7 +248,6 @@ bool Device::import_chunk(ztd::chunkdat const& ch)
floating = importBool(tch, "float", floating);
this->pitchCommands.push_back(PitchCommand(channel,min,max,mapMin,mapMax,floating,shell));
this->nb_command++;
}
else
{
@ -260,6 +255,7 @@ bool Device::import_chunk(ztd::chunkdat const& ch)
return false;
}
}
this->nb_command++;
}
return true;
}
@ -290,7 +286,7 @@ void Device::run_signal(char* buff)
{
std::string command="code=" + strval + ";";
command += dequote(it.shell);
std::thread(ztd::sh, command, true).detach();
std::thread(ztd::shr, command).detach();
}
}
else
@ -298,7 +294,7 @@ void Device::run_signal(char* buff)
int t;
char type;
int8_t channel;
int ctid;
int8_t ctid=-1;
int16_t value;
char* pos=NULL;
bool note_off=false;
@ -317,7 +313,7 @@ void Device::run_signal(char* buff)
else
{
// throw std::runtime_error("Unknown MIDI signal:\n" + std::string(buff));
printf("Unsupported signal, ignoring\n");
std::cerr << "Unsupported signal, ignoring\n" ;
return;
}
@ -367,7 +363,7 @@ void Device::run_signal(char* buff)
+ ";channel=" + std::to_string(channel)
+ ";velocity=" + std::to_string(value) + ";";
command += dequote(it.shell);
std::thread(ztd::sh, command, true).detach();
std::thread(ztd::shr, command).detach();
}
}
}
@ -394,7 +390,7 @@ void Device::run_signal(char* buff)
else
command += std::to_string((long int) result);
command += ";" + dequote(it.shell);
std::thread(ztd::sh, command, true).detach();
std::thread(ztd::shr, command).detach();
}
}
}
@ -420,7 +416,7 @@ void Device::run_signal(char* buff)
else
command += std::to_string((long int) result);
command += ";" + dequote(it.shell);
std::thread(ztd::sh, command, true).detach();
std::thread(ztd::shr, command).detach();
}
}
} // if type
@ -442,7 +438,7 @@ void Device::loop(Device* dev)
for( auto it : dev->connectCommands )
{
std::thread(ztd::sh, dequote(it.shell), true).detach();
std::thread(ztd::shr, dequote(it.shell)).detach();
}
while (getline(&buff, &buff_size, stream) > 0)
@ -452,7 +448,7 @@ void Device::loop(Device* dev)
for( auto it : dev->disconnectCommands )
{
std::thread(ztd::sh, dequote(it.shell), true).detach();
std::thread(ztd::shr, dequote(it.shell)).detach();
}
log("Device '" + dev->name + "' disconnected\n");

View file

@ -71,8 +71,9 @@ ztd::chunkdat mimtochk_commands(const std::string& mim)
return chk;
}
ztd::chunkdat mimtochk(const std::string& mim)
ztd::chunkdat mimtochk(std::string mim)
{
mim = ztd::filedat::removeComments(mim);
ztd::chunkdat chk;
unsigned int i=0,j=0;
_find_next(mim,i, "Device ");
@ -97,6 +98,29 @@ ztd::chunkdat mimtochk(const std::string& mim)
return chk;
}
std::string chktomim(ztd::chunkdat const& chk, std::string const& aligner)
{
std::string ret;
for(int i=0 ; i<chk.listSize() ; i++)
{
ztd::chunkdat& device=chk[i];
ret += "Device " + device.subChunkRef("name").str() + "\n\n";
for(int j=0 ; j < device["commands"].listSize() ; j++)
{
ztd::chunkdat command=device["commands"][j];
ret += aligner + "Command " + command["type"].str() + '\n';
command.erase("type");
for(auto it : command.getmap())
{
ret += aligner+aligner + it.first + '=' + it.second->str() + '\n';
}
ret += '\n';
// ret += command.str(2, aligner) + '\n';
}
}
return ret;
}
bool is_mim(const std::string& str)
{
unsigned int i=0;

View file

@ -14,7 +14,7 @@
#include <ztd/options.hpp>
#include <ztd/shell.hpp>
#define VERSION_STRING "v1.3"
#define VERSION_STRING "v1.3.1a"
ztd::option_set options;
@ -23,7 +23,6 @@ void help()
printf("zmidimap [options] <file>\n\nOptions:\n");
options.print_help(4, 25);
printf("\n");
printf("If piped, the map file will be read from standard input\n");
printf("See --mim-format --zfd-format --command-tags --shell-format options for details on map file format\n");
}
@ -73,17 +72,39 @@ void load_filedat(ztd::filedat& file, bool from_stdin, std::string const& path)
if(from_stdin)
{
log("Loading map from stdin\n");
file.import_stdin();
file.setFilePath(path);
std::string str=file_strimport(path);
if(options["zfd"])
{
file.data() = str;
}
else if(options["mim"])
{
file.data() = mimtochk(str);
}
else
{
if(is_mim(str))
{
file.data() = mimtochk(str);
}
else
{
file.data() = str;
}
}
}
else
{
file.setFilePath(path);
log("Loading map file '" + path + "'\n");
if(options.find("zfd")->activated)
if(options["zfd"])
{
file.import_file();
}
else if(options.find("mim")->activated)
else if(options["mim"])
{
file.data() = mimtochk(file_strimport(path));
}
@ -119,34 +140,31 @@ int main(int argc, char* argv[])
signal(SIGINT, inthandler);
signal(SIGCHLD, SIG_IGN); //not expecting returns from child processes
bool piped=false;
bool autoreload=true;
if (!isatty(fileno(stdin)))
piped = true;
bool from_stdin=false;
options.add(ztd::option("\r [Help]"));
options.add(ztd::option('h',"help", false, "Display this help message"));
options.add(ztd::option('v',"version", false, "Display version"));
options.add(ztd::option("mim-format", false, "Display mim file format help"));
options.add(ztd::option("zfd-format", false, "Display zfd file format help"));
options.add(ztd::option("command-tags", false, "Display for command tag help"));
options.add(ztd::option("shell-format", false, "Display for shell format help"));
options.add(ztd::option("\r [Format]"));
options.add(ztd::option("no-log", false, "Disable console logging"));
options.add(ztd::option("\r [Devices]"));
options.add(ztd::option('l',"list", false, "List detected devices"));
options.add(ztd::option('L',"full-list", false, "Print whole device list details"));
options.add(ztd::option('p',"port", true, "Connect to device and output to console", "device"));
options.add(ztd::option("\r [Map file]"));
options.add(ztd::option('m',"mim", false, "Read file in mim format"));
options.add(ztd::option('z',"zfd", false, "Read file in zfd format"));
options.add(ztd::option('o',"output", true, "Output the resulting zfd map to file. - for stdout", "file"));
options.add(ztd::option("aligner", true, "String to use for aligning output map format. Default \\t", "string"));
options.add(ztd::option("no-reload", false, "Disable auto reloading when file changes are detected"));
options.add(ztd::option("\rIf no file format is specified, the program will try to guess the format"));
// options.add(ztd::option('i',"interactive", false, "Start in interactive mode"));
options.add(
ztd::option("\r [Help]"),
ztd::option('h',"help", false, "Display this help message"),
ztd::option('v',"version", false, "Display version"),
ztd::option("mim-format", false, "Display mim file format help"),
ztd::option("zfd-format", false, "Display zfd file format help"),
ztd::option("command-tags", false, "Display for command tag help"),
ztd::option("shell-format", false, "Display for shell format help"),
ztd::option("\r [Format]"),
ztd::option("no-log", false, "Disable console logging"),
ztd::option("\r [Devices]"),
ztd::option('l',"list", false, "List detected devices"),
ztd::option('L',"full-list", false, "Print whole device list details"),
ztd::option('p',"port", true, "Connect to device and output to console", "device"),
ztd::option("\r [Map file]"),
ztd::option('m',"mim", false, "Read file in mim format"),
ztd::option('z',"zfd", false, "Read file in zfd format"),
ztd::option('o',"output", true, "Output the resulting map to file", "file"),
ztd::option("out-zfd", false, "Output in zfd format"),
ztd::option("aligner", true, "String to use for aligning output map format. Default \\t", "string"),
ztd::option("no-reload", false, "Disable auto reloading when file changes are detected")
);
std::vector<std::string> arg;
try
@ -156,132 +174,139 @@ int main(int argc, char* argv[])
catch(ztd::option_error& err)
{
printf("%s\n", err.what());
if(err.type() == ztd::option_error::unknown_option)
help();
stop(1);
}
ztd::option* op=nullptr;
//exit options
if( options.find('h')->activated )
if( options['h'] )
{
help();
stop(0);
}
if( options.find('v')->activated )
if( options['v'] )
{
version();
stop(0);
}
if( options.find("mim-format")->activated )
if( options["mim-format"] )
{
printf("%s\n", MIM_FORMAT);
stop(0);
}
if( options.find("zfd-format")->activated )
if( options["zfd-format"] )
{
printf("%s\n", ZFD_FORMAT);
stop(0);
}
if( options.find("command-tags")->activated )
if( options["command-tags"] )
{
printf("%s\n", COMMAND_TAGS);
stop(0);
}
if( options.find("shell-format")->activated )
if( options["shell-format"] )
{
printf("%s\n", SHELL_FORMAT);
stop(0);
}
if( options.find('L')->activated )
if( options['L'] )
{
ztd::sh("aseqdump -l", true);
ztd::shr("aseqdump -l");
stop(0);
}
if( options.find('l')->activated )
if( options['l'] )
{
ztd::sh(LIST_COMMAND, true);
ztd::shr(LIST_COMMAND);
stop(0);
}
op = options.find('p');
if( op->activated )
if( options['p'] )
{
option_p(op->argument);
option_p(options['p']);
stop(0);
}
if (options.find('o')->activated)
if (options['o'])
{
log_on=false;
}
//behavioral options
if( options.find("no-reload")->activated )
if( options["no-reload"] )
{
autoreload=false;
}
if( options.find("no-log")->activated )
if( options["no-log"] )
{
log_on=false;
}
std::string aligner="\t";
if(options.find("aligner")->activated)
if(options["aligner"])
{
aligner=options.find("aligner")->argument;
aligner=options["aligner"].argument;
}
//no argument: display help
ztd::filedat file;
bool no_arg=false;
std::string filepath;
if (arg.size() <= 0 || arg[0] == "")
{
no_arg=true;
if(!piped)
{
help();
stop(0);
}
help();
stop(0);
}
else
{
filepath=arg[0];
}
if(filepath == "-")
{
filepath = "/dev/stdin";
from_stdin = true;
}
//main processing
try
{
//load
load_filedat(file, no_arg, filepath);
load_filedat(file, from_stdin, filepath);
//output
if(options.find('o')->activated)
if(options['o'])
{
if(options.find('o')->argument == "-") {
std::cout << file.strval(aligner) << std::endl;
std::string ret;
if(options["out-zfd"])
ret=file.data().str(0, aligner);
else
ret=chktomim(file.data(), aligner);
if(options['o'].argument == "-") {
std::cout << ret << std::endl;
}
else {
file.setFilePath(options.find('o')->argument);
file.export_file();
std::ofstream output(options['o']);
if(!output)
{
std::cerr << "Cannot write to file '" + options['o'].argument + "'\n";
return 1;
}
output << ret << std::endl ;
}
return 0;
}
//create commands
load_commands(file.data());
autoreload = autoreload && !no_arg;
//main loop
log("Starting scan for devices\n");
if(autoreload)
std::thread(filetime_loop, filepath).detach();
announce_loop();
std::thread(filetime_loop, filepath).detach(); // start the killer thread
announce_loop(); // loop until killed
ztd::chunkdat bak_data = file.data();
while(autoreload)
{
log("Reloading file\n");
try
{
load_filedat(file, false, filepath);
load_filedat(file, from_stdin, filepath);
load_commands(file.data());
bak_data = file.data();
}
@ -297,7 +322,7 @@ int main(int argc, char* argv[])
log("Reloading old config\n");
load_commands(bak_data);
}
announce_loop();
announce_loop(); // loop until killed
}
}
catch (ztd::format_error& e)

View file

@ -102,7 +102,7 @@ void announce_loop()
free(buff);
}
void filetime_loop(std::string filepath)
void filetime_loop(std::string const& filepath)
{
struct stat attrib;
stat(filepath.c_str(), &attrib);