[pacman-dev] [PATCH] Support reading package args from stdin
Only occurs if no arguments were provided directly. Arguments can be separated by any amount of valid whitespace. This allows for piping into pacman from other programs or from itself, e.g.: pacman -Qdtq | pacman -Rs Signed-off-by: Dave Reisner <d@falconindy.com> --- src/pacman/pacman.c | 24 ++++++++++++++++++++++++ 1 files changed, 24 insertions(+), 0 deletions(-) diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index 049bc40..6d9d954 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -26,6 +26,7 @@ #define PACKAGE_VERSION GIT_VERSION #endif +#include <ctype.h> /* isspace */ #include <stdlib.h> /* atoi */ #include <stdio.h> #include <limits.h> @@ -1172,6 +1173,29 @@ int main(int argc, char *argv[]) cleanup(ret); } + /* read package arguments from stdin if we have none yet */ + if(!pm_targets && !isatty(0)) { + char line[BUFSIZ]; + int i = 0; + while((line[i] = fgetc(stdin)) != EOF) { + if(isspace(line[i])) { + line[i] = 0; + /* avoid adding zero length arg, if multiple spaces separate args */ + if(i > 0) { + pm_targets = alpm_list_add(pm_targets, strdup(line)); + i = 0; + } + continue; + } + ++i; + } + /* end of stream -- check for data still in line buffer */ + if(i > 0) { + pm_targets = alpm_list_add(pm_targets, strdup(line)); + } + freopen(ctermid(NULL), "r", stdin); + } + /* parse the config file */ ret = parseconfig(config->configfile); if(ret != 0) { -- 1.7.2
On Fri, Jul 23, 2010 at 10:26 PM, Dave Reisner <d@falconindy.com> wrote:
Only occurs if no arguments were provided directly. Arguments can be separated by any amount of valid whitespace. This allows for piping into pacman from other programs or from itself, e.g.:
pacman -Qdtq | pacman -Rs
What's wrong with xargs? -Dan
On Sat, Jul 24, 2010 at 10:21, Dan McGee <dpmcgee@gmail.com> wrote:
On Fri, Jul 23, 2010 at 10:26 PM, Dave Reisner <d@falconindy.com> wrote:
Only occurs if no arguments were provided directly. Arguments can be separated by any amount of valid whitespace. This allows for piping into pacman from other programs or from itself, e.g.:
pacman -Qdtq | pacman -Rs
What's wrong with xargs? You can also just do pacman -Rs $(pacman -Qqdt)
On Sat, Jul 24, 2010 at 10:30:45AM -0400, Daenyth Blank wrote:
On Sat, Jul 24, 2010 at 10:21, Dan McGee <dpmcgee@gmail.com> wrote:
On Fri, Jul 23, 2010 at 10:26 PM, Dave Reisner <d@falconindy.com> wrote:
Only occurs if no arguments were provided directly. Arguments can be separated by any amount of valid whitespace. This allows for piping into pacman from other programs or from itself, e.g.:
pacman -Qdtq | pacman -Rs
What's wrong with xargs? You can also just do pacman -Rs $(pacman -Qqdt)
I hadn't considered xargs, but 'pacman -Xnn $()' always felt awkward to me compared to pipes. I suppose this doesn't provide any _extra_ functionality, but it seemed like an easy enough addition for something that's a seemingly ubiquitous feature in *nix userspace. I understand if you consider this to be redundant. d
On 25/07/10 01:09, Dave Reisner wrote:
On Sat, Jul 24, 2010 at 10:30:45AM -0400, Daenyth Blank wrote:
On Sat, Jul 24, 2010 at 10:21, Dan McGee<dpmcgee@gmail.com> wrote:
On Fri, Jul 23, 2010 at 10:26 PM, Dave Reisner<d@falconindy.com> wrote:
Only occurs if no arguments were provided directly. Arguments can be separated by any amount of valid whitespace. This allows for piping into pacman from other programs or from itself, e.g.:
pacman -Qdtq | pacman -Rs
What's wrong with xargs? You can also just do pacman -Rs $(pacman -Qqdt)
I hadn't considered xargs, but 'pacman -Xnn $()' always felt awkward to me compared to pipes. I suppose this doesn't provide any _extra_ functionality, but it seemed like an easy enough addition for something that's a seemingly ubiquitous feature in *nix userspace. I understand if you consider this to be redundant.
I actually quite like this idea, despite there being multiple other ways to achieve the same thing. The code is nicely self contained and is moderately useful so I do not see the downside. I give it a +0.25. Allan
participants (4)
-
Allan McRae
-
Daenyth Blank
-
Dan McGee
-
Dave Reisner