Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
soveran committed Mar 11, 2018
0 parents commit 4cc5405
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 0 deletions.
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2018, Michel Martens <[email protected]>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# map

Map lines from stdin to commands.

Description
-----------

Map lets you process each line from stdin with a command of your
choice. For example:

```shell
$ ls
LICENSE README.md makefile map.1 map.c
```

```shell
$ ls | map f 'echo $f $f'
LICENSE LICENSE
README.md README.md
makefile makefile
map.1 map.1
map.c map.c
```

Note that the command must be wrapped in single quotes to prevent
the variable from being expanded by the shell.

Installation
------------

Install map into `/usr/local/bin` with the following command:

$ make install

You can use `make PREFIX=/some/other/directory install` if you wish
to use a different destination. If you want to remove map from
your system, use `make uninstall`.

Motivation
----------

There are many ways to accomplish what you can do with `map`,
including shell builtins, `find`, `awk`, etc. The approach taken
by `map` is extremely pragmatic and allows me to express concisely
what I want. Given the fact that it's designed as a filter, it can
operate on any kind of list, not only lists of files.

Contributing
------------

If you find a bug, please create an issue detailing the ways to
reproduce it. If you have a suggestion, create an issue detailing
the use case.
36 changes: 36 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
PREFIX?=/usr/local
MANPREFIX?=${PREFIX}/share/man
STRIP?=strip

default: map

map: map.c
$(CC) -Wall -Os -o map map.c

clean:
@echo cleaning
@rm -f map

install: map
@echo stripping executable
@${STRIP} map
@echo installing executable to ${PREFIX}/bin
@mkdir -p ${PREFIX}/bin
@cp -f map ${PREFIX}/bin/map
@chmod 755 ${PREFIX}/bin/map

@echo installing manual pages to ${MANPREFIX}/man1
@mkdir -p ${MANPREFIX}/man1
@cp -f map.1 ${MANPREFIX}/man1/map.1
@chmod 644 ${MANPREFIX}/man1/map.1

uninstall:
@echo removing executable from ${PREFIX}/bin
@rm ${PREFIX}/bin/map
@echo removing manual pages from ${MANPREFIX}/man1
@rm ${MANPREFIX}/man1/map.1

test:
@sh test/tests.sh

.PHONY: clean install uninstall test
39 changes: 39 additions & 0 deletions map.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
.Dd March 11, 2018
.Dt MAP 1
.
.Sh NAME
.
.Nm map
.Nd Map lines from stdin to commands.
.
.Sh SYNOPSIS
.
.Nm
.Ar variable
.Ar command
.
.Sh DESCRIPTION
.
.Nm
lets you process each line from stdin with a command of your
choice.
.
.Sh EXAMPLES
.
Here's an example:
.Pp
.Dl $ ls
.Dl Sy LICENSE README.md makefile map.1 map.c
.Pp
.Dl $ ls | map f 'echo $f $f'
.Dl Sy LICENSE LICENSE
.Dl Sy README.md README.md
.Dl Sy makefile makefile
.Dl Sy map.1 map.1
.Dl Sy map.c map.c
.Pp
Note that the command must be wrapped in single quotes to prevent
the variable from being expanded by the shell.
.
.Sh AUTHOR
.An Michel Martens Aq [email protected]
51 changes: 51 additions & 0 deletions map.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2018, Michel Martens <mail at soveran dot com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {

if (argc != 3) {
printf("usage: %s <variable> <command>\n", argv[0]);
exit(1);
}

char *line = NULL;
size_t lcap = 0;
ssize_t llen;

while ((llen = getline(&line, &lcap, stdin)) > 0) {
setenv(argv[1], line, 1);
system(argv[2]);
}

return 0;
}
3 changes: 3 additions & 0 deletions test/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo
bar
baz
8 changes: 8 additions & 0 deletions test/tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

assert_equal () {
test "$1" = "$2" || printf "F: \"%s\" != \"%s\"\n" $1 $2
}

assert_equal "foofoo" `printf foo | ./map f 'printf $f$f'`
assert_equal "foobarbaz" `cat ./test/input | ./map f 'printf $f'`

0 comments on commit 4cc5405

Please sign in to comment.