igawk 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #! /bin/sh
  2. # igawk --- like gawk but do @include processing
  3. #
  4. # Arnold Robbins, arnold@skeeve.com, Public Domain
  5. # July 1993
  6. # December 2010, minor edits
  7. if [ "$1" = debug ]
  8. then
  9. set -x
  10. shift
  11. fi
  12. # A literal newline, so that program text is formatted correctly
  13. n='
  14. '
  15. # Initialize variables to empty
  16. program=
  17. opts=
  18. while [ $# -ne 0 ] # loop over arguments
  19. do
  20. case $1 in
  21. --) shift
  22. break ;;
  23. -W) shift
  24. # The ${x?'message here'} construct prints a
  25. # diagnostic if $x is the null string
  26. set -- -W"${@?'missing operand'}"
  27. continue ;;
  28. -[vF]) opts="$opts $1 '${2?'missing operand'}'"
  29. shift ;;
  30. -[vF]*) opts="$opts '$1'" ;;
  31. -f) program="$program$n@include ${2?'missing operand'}"
  32. shift ;;
  33. -f*) f=$(expr "$1" : '-f\(.*\)')
  34. program="$program$n@include $f" ;;
  35. -[W-]file=*)
  36. f=$(expr "$1" : '-.file=\(.*\)')
  37. program="$program$n@include $f" ;;
  38. -[W-]file)
  39. program="$program$n@include ${2?'missing operand'}"
  40. shift ;;
  41. -[W-]source=*)
  42. t=$(expr "$1" : '-.source=\(.*\)')
  43. program="$program$n$t" ;;
  44. -[W-]source)
  45. program="$program$n${2?'missing operand'}"
  46. shift ;;
  47. -[W-]version)
  48. echo igawk: version 3.0 1>&2
  49. gawk --version
  50. exit 0 ;;
  51. -[W-]*) opts="$opts '$1'" ;;
  52. *) break ;;
  53. esac
  54. shift
  55. done
  56. if [ -z "$program" ]
  57. then
  58. program=${1?'missing program'}
  59. shift
  60. fi
  61. # At this point, `program' has the program.
  62. expand_prog='
  63. function pathto(file, i, t, junk)
  64. {
  65. if (index(file, "/") != 0)
  66. return file
  67. if (file == "-")
  68. return file
  69. for (i = 1; i <= ndirs; i++) {
  70. t = (pathlist[i] "/" file)
  71. if ((getline junk < t) > 0) {
  72. # found it
  73. close(t)
  74. return t
  75. }
  76. }
  77. return ""
  78. }
  79. BEGIN {
  80. path = ENVIRON["AWKPATH"]
  81. ndirs = split(path, pathlist, ":")
  82. for (i = 1; i <= ndirs; i++) {
  83. if (pathlist[i] == "")
  84. pathlist[i] = "."
  85. }
  86. stackptr = 0
  87. input[stackptr] = ARGV[1] # ARGV[1] is first file
  88. for (; stackptr >= 0; stackptr--) {
  89. while ((getline < input[stackptr]) > 0) {
  90. if (tolower($1) != "@include") {
  91. print
  92. continue
  93. }
  94. fpath = pathto($2)
  95. if (fpath == "") {
  96. printf("igawk: %s:%d: cannot find %s\n",
  97. input[stackptr], FNR, $2) > "/dev/stderr"
  98. continue
  99. }
  100. if (! (fpath in processed)) {
  101. processed[fpath] = input[stackptr]
  102. input[++stackptr] = fpath # push onto stack
  103. } else
  104. print $2, "included in", input[stackptr],
  105. "already included in",
  106. processed[fpath] > "/dev/stderr"
  107. }
  108. close(input[stackptr])
  109. }
  110. }' # close quote ends `expand_prog' variable
  111. processed_program=$(gawk -- "$expand_prog" /dev/stdin << EOF
  112. $program
  113. EOF
  114. )
  115. eval gawk $opts -- '"$processed_program"' '"$@"'