From 59298b27f1f5b25df81a39f9a7f44ef8dc09ce06 Mon Sep 17 00:00:00 2001 From: Adrian Pardini Date: Sun, 1 Jan 2012 11:16:53 -0300 Subject: [PATCH 2/2] Action to filter the pastebuffer with an external program. This patch adds the "Filter(command)" and "! command" actions. It runs "sh -c command" piping the contents of the pastebuffer to stdin. If the return code is zero it replaces the buffer with the output of the command. Both the input and output of the command are saved to temporary files, if pcb is compiled with debug enabled they are kept, otherwise deleted. --- src/action.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 139 insertions(+), 0 deletions(-) diff --git a/src/action.c b/src/action.c index affefba..79eb499 100644 --- a/src/action.c +++ b/src/action.c @@ -120,6 +120,7 @@ typedef enum F_ElementConnections, F_ElementToBuffer, F_Escape, + F_Filter, F_Find, F_FlipElement, F_FoundPins, @@ -326,6 +327,7 @@ static bool mid_stroke = false; static BoxType StrokeBox; #endif static FunctionType Functions[] = { + {"!", F_Filter}, {"AddSelected", F_AddSelected}, {"All", F_All}, {"AllConnections", F_AllConnections}, @@ -354,6 +356,7 @@ static FunctionType Functions[] = { {"ElementConnections", F_ElementConnections}, {"ElementToBuffer", F_ElementToBuffer}, {"Escape", F_Escape}, + {"Filter", F_Filter}, {"Find", F_Find}, {"FlipElement", F_FlipElement}, {"FoundPins", F_FoundPins}, @@ -7975,9 +7978,142 @@ ActionAttributes (int argc, char **argv, Coord x, Coord y) return 0; } +/* ------------------------------------------------------------ */ + +static const char filter_syntax[] = +"Filter(command)\n" +"! command"; + +static const char filter_help[] = +"Filters the contents of the paste buffer through the command."; + +/* %start-doc actions Filter + +Runs the command passing the contents of the paste buffer via stdin and replaces +it with the command output if successful. + +%end-doc */ + +static int +ActionFilter (int argc, char **argv, Coord x, Coord y) +{ + int idx; + int ret = 0; + pid_t child; + char *myargv[] = {"sh", "-c", NULL, NULL}; + static DynamicStringType command; + FILE *fp = NULL; + char *input, *output; + +#ifdef DEBUG + printf("ActionFilter argc: %i . args: \n", argc); + for (idx=0; idx