home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / inject / tear < prev   
Text File  |  1994-09-20  |  782b  |  43 lines

  1. #! /bin/sh
  2. # tear prefix [file...] - tear RFC822 header and body apart
  3. #    output files are $1hdr and $1body
  4. PATH=/bin:/usr/bin; export PATH
  5.  
  6. case $# in
  7. 0)
  8.     echo "usage: tear prefix [file...]" >&2
  9.     exit 1
  10.     ;;
  11. esac
  12.  
  13. hdr="$1hdr"
  14. body="$1body"
  15. shift
  16.  
  17. >>$hdr                    # create files just in case
  18. >>$body
  19. case $# in
  20. 0)    args="-" ;;    # awk needs a filename due to cmd. line assignments
  21. *)    args="$@" ;;
  22. esac
  23. # thanks to Charles Lindsey for the slightly better checking here
  24. exec awk 'BEGIN {
  25.     inbody = 0
  26.     status = 0
  27.     hdr = "'"$hdr"'"
  28.     body = "'"$body"'"
  29. }
  30. /^$/ { inbody = 1 }
  31. inbody == 0 && ($0 ~ /^[^ \t]*:/ || ($0 ~ /^[ \t]/ && NR > 1)) {
  32.     print >hdr
  33.     next
  34. }
  35. inbody == 0 {
  36.     print "tear: invalid header \"" $0 "\"" | "cat >&2"
  37.     status = 1
  38. }
  39. inbody == 1 {
  40.     print >body
  41. }
  42. END { exit status }' $args
  43.