X-Git-Url: https://ruderich.org/simon/gitweb/?p=config%2Fdotfiles.git;a=blobdiff_plain;f=lib.sh;h=474f638c42c8fc858418656ca59ad73e9adab8da;hp=7b76a7cab66b08cad1d639f0c059fae2dc637798;hb=0d6b1deba13390ed289d4c933e0a05da7d031e5e;hpb=f2ae0a2fdd20981164c1a10b0be4c354234610b0 diff --git a/lib.sh b/lib.sh index 7b76a7c..474f638 100644 --- a/lib.sh +++ b/lib.sh @@ -50,27 +50,71 @@ installed_path() { ) } -# Usage: sed_i ... +# Usage: cmd_i ... # -# Uses .tmp as temporary file. sed -i is not compatible due to different -# implementations. -sed_i() { +# Run with all arguments (including the last file) and write the result +# to the temporary file .tmp and then renamed that file to . This +# can't be done in-place (e.g. cmd file) because it truncates the file. +cmd_i() { # Get last argument. last= for x; do last="$x" done - sed "$@" >"$last".tmp + "$@" >"$last".tmp mv "$last".tmp "$last" } +# Usage: sed_i ... +# +# sed -i is not compatible due to different implementations. See cmd_i. +sed_i() { + cmd_i sed "$@" +} +grep_i() { + cmd_i grep "$@" +} + +# Usage: perl_line_filter ... +# +# Run the perl command cmd on each line before printing it. +perl_line_filter() { + cmd="$1" + shift + + # Can't use -pe because it uses <> which treats the arguments as files. + perl -e "use strict; use warnings; while () { $cmd; print; }" "$@" +} + +# Usage: simple_cpp .. -- ... +# +# Replaces each FIRST (on word boundaries) with like a +# simple cpp replacement. +simple_cpp() { + cmd='my $i = 0;' + + for x; do + shift + + if test x"$x" = x--; then + break + fi + + cmd="$cmd s/\b$x\b/\$ARGV[\$i]/g; \$i++;" + done + + perl_line_filter "$cmd" -- "$@" +} + # Print the current OS. The following OS are supported at the moment: +# # - Debian (debian) # - Gentoo (gentoo) # - Mac OS X (darwin) # - Solaris/OpenSolaris (sun) # - FreeBSD (freebsd) +# # If an unsupported OS is used an error is printed. os() { if test -f /etc/debian_version; then @@ -130,67 +174,45 @@ link() { cd "$pwd" } -# Write a warning to $1 to make clear it should not be modified. $2 is the -# source for the generated file. Also print a message to stdout that the file -# $1 was generated from $2 using the command $3 with options $4. -warning() { - echo "###################################" > $1 - echo "# WARNING! DO NOT EDIT THIS FILE! #" >> $1 - echo "###################################" >> $1 - echo >> $1 - echo "# It was generated from $2 on `date`." >> $1 - echo >> $1 - - # Display given options if there were any (Zsh has a problem with $options - # as variable name). - option= - if test -n "$4"; then - option=" with options '$4'" - fi - # Write message to stdout. - echo "$3: generating '$1' from '$2'$option" - - unset option -} - -# Generate a file using several methods. A warning not to edit it is -# automatically added to the created file and a message printed to stdout -# through warning(). +# Generate a file from a source file using a given command. A warning not to +# edit it is automatically added to the created file. # -# The following commands are possible; the file extension for the source file -# in brackets. +# Usage: generated() # -# - m4 (.m4): pipe $2.m4 through m4 then write it to $2 -# - awk (.in): pipe $2.in through awk then write it to $2 -# - perl (.in): pipe $2.in through perl then write it to $2 -# - cat ($3): copy $2$3 to $2 +# If an empty extension is provided, the file is modified in-place (through a +# temporary file). generate() { + local file >/dev/null 2>&1 || true + local file_tmp >/dev/null 2>&1 || true + local extension >/dev/null 2>&1 || true + # Get command and target file. - command="$1" - file="$2" - # Remove arguments from list. + file="$1" + extension="$2" shift shift - # Set extension for the used commands. When cat is used $3 is used as - # extension. - if test x"$command" = xm4; then - extension=.m4 - elif test x"$command" = xawk -o x"$command" = xperl; then - extension=.in - elif test x"$command" = xcat; then - extension="$1" # is $3 in reality, $1 because of shifting - shift - # Print a warning and exit if an unsupported command is used. + if test -z "$extension"; then + file_tmp="$file.tmp" else - echo "generate(): command '$command' not supported!" >&2 - exit 1 + # We only need this message if we generate a new file. + printf "%s: generating from '%s' (%s)\n" \ + "$file" "$file$extension" "$1" + + echo '###################################' >"$file" + echo '# WARNING! DO NOT EDIT THIS FILE! #' >>"$file" + echo '###################################' >>"$file" + echo >>"$file" + echo "# It was generated from $file$extension on `date`." >>"$file" + echo >>"$file" + + file_tmp="$file" fi - # Add warning to file and write a message to stdout. - warning "$file" "$file$extension" $command "$*" # Generate $file from $file$extension using the given command. - cat "$file$extension" | $command "$@" >> "$file" + "$@" <"$file$extension" >>"$file_tmp" - unset command file + if test -z "$extension"; then + mv "$file_tmp" "$file" + fi }