Skip to content

mapec - change the exit code of a program via a simple rule specification

[Home | sourcehut]

Overview

The mapec tool runs the specified program, waits for it to exit, examines its exit code, and then itself exits with that code mapped according to some rules. The map pattern is supplied as the first argument to mapec; the program and any arguments follow.

The mapec tool was originally written by Matt S Trout; his homepage describes it thus:

A quick C app that allows you to change the exit code of a program via a simple rule specification. Useful for generating specific exit codes for different conditions e.g. in a qmail-qfilter script. Currently just a single C source file.

After his passing, Peter Pentchev took the mapec tool's C source code, polished it up a bit, and wrote a simple test suite and a trivial Rust implementation.

Documentation from the original source code

/* mapec is a simple program designed to be used in Bernstein chains,       */
/* usualy as part of a supervise run script for a qmail-style service. It   */
/* is invoked as follows -                                                  */
/*                                                                          */
/*   mapec <map> <command> <arg> <arg> ...                                  */
/*                                                                          */
/* mapec forks, invokes the command with the specified arguments and waits  */
/* for the child process to exit. Once it does, mapec exits as specified by */
/* the map entry for the exit code of the child. The map consists of colon  */
/* separated terms, each of which is a list of codes (or nothing to set the */
/* default exit code), an equals sign, then the exit code to use if this    */
/* term matches. List of codes are comma separated, and may use two numbers */
/* with a hyphen between them to specify ranges. For example -              */
/*                                                                          */
/*   3-7,12=43:15=44:=45                                                    */
/*                                                                          */
/* would cause mapec to exit 43 if the child exits 12 or from 3 to 7, 44 if */
/* the child exists 15 and 45 otherwise.                                    */

Example

Run rsync, remap most of its runtime errors to code 2 and its timeout errors to 30:

mapec 3-29-2:30-126=30 rsync -az ...

Same as above, but remap any other exit code to 127:

mapec 3-29-2:30-126=30:=127 rsync -az ...

Contact

The mapec tool was originally written by Matt S Trout and is now maintained by Peter Pentchev. It is developed in a sourcehut repository. This documentation is hosted at Ringlet.