home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuawk.zip / awklib / extract.awk < prev    next >
Text File  |  1995-12-27  |  2KB  |  88 lines

  1. # extract.awk --- extract files and run programs
  2. #                 from texinfo files
  3. # Arnold Robbins, arnold@gnu.ai.mit.edu, Public Domain
  4. # May 1993
  5.  
  6. BEGIN    { IGNORECASE = 1 }
  7.  
  8. /^@c(omment)?[ \t]+system/    \
  9. {
  10.     if (NF < 3) {
  11.         e = (FILENAME ":" FNR)
  12.         e = (e  ": badly formed `system' line")
  13.         print e > "/dev/stderr"
  14.         next
  15.     }
  16.     $1 = ""
  17.     $2 = ""
  18.     stat = system($0)
  19.     if (stat != 0) {
  20.         e = (FILENAME ":" FNR)
  21.         e = (e ": warning: system returned " stat)
  22.         print e > "/dev/stderr"
  23.     }
  24. }
  25. /^@c(omment)?[ \t]+file/    \
  26. {
  27.     if (NF != 3) {
  28.         e = (FILENAME ":" FNR ": badly formed `file' line")
  29.         print e > "/dev/stderr"
  30.         next
  31.     }
  32.     if ($3 != curfile) {
  33.         if (curfile != "")
  34.             close(curfile)
  35.         curfile = $3
  36.     }
  37.  
  38.     for (;;) {
  39.         if ((getline line) <= 0)
  40.             unexpected_eof()
  41.         if (line ~ /^@c(omment)?[ \t]+endfile/)
  42.             break
  43.         else if (line ~ /^@(end[ \t]+)?group/)
  44.             continue
  45.         if (index(line, "@") == 0) {
  46.             print line > curfile
  47.             continue
  48.         }
  49.         n = split(line, a, "@")
  50.         # if a[1] == "", means leading @,
  51.         # don't add one back in.
  52.         for (i = 2; i <= n; i++) {
  53.             if (a[i] == "") { # was an @@
  54.                 a[i] = "@"
  55.                 if (a[i+1] == "")
  56.                     i++
  57.             }
  58.         }
  59.         print join(a, 1, n, SUBSEP) > curfile
  60.     }
  61. }
  62. function unexpected_eof()
  63. {
  64.     printf("%s:%d: unexpected EOF or error\n", \
  65.         FILENAME, FNR) > "/dev/stderr"
  66.     exit 1
  67. }
  68.  
  69. END {
  70.     if (curfile)
  71.         close(curfile)
  72. }
  73. # join.awk --- join an array into a string
  74. # Arnold Robbins, arnold@gnu.ai.mit.edu, Public Domain
  75. # May 1993
  76.  
  77. function join(array, start, end, sep,    result, i)
  78. {
  79.     if (sep == "")
  80.        sep = " "
  81.     else if (sep == SUBSEP) # magic value
  82.        sep = ""
  83.     result = array[start]
  84.     for (i = start + 1; i <= end; i++)
  85.         result = result sep array[i]
  86.     return result
  87. }
  88.