home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / GNU / recode-3.4-MIHS / src / mergelex.awk < prev    next >
Encoding:
AWK Script  |  1994-10-27  |  4.6 KB  |  195 lines

  1. # Conversion of files between different charsets and usages.
  2. # Copyright (C) 1990, 1993, 1994 Free Software Foundation, Inc.
  3. # Francois Pinard <pinard@iro.umontreal.ca>, 1990.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2, or (at your option)
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. # This awk script merges several lex sources intended for GNU recode.
  20.  
  21. # At beginning, one temporary file is initialized for each section.
  22.  
  23. BEGIN {
  24.   SECTION1 = "merged1.tmp"
  25.   SECTION2 = "merged2.tmp"
  26.   SECTION3 = "merged3.tmp"
  27.  
  28.   print "/* This file is generated automatically by mergelex.awk.  */"
  29.  
  30.   print ""                        >SECTION1
  31.   print "%{"                        >SECTION1
  32.   print "#include \"recode.h\""                >SECTION1
  33.   print "#ifdef USE_FPUTC"                >SECTION1
  34.   print "#define output(ch) fputc (ch, yyout)"        >SECTION1
  35.   print "#else"                        >SECTION1
  36.   print "#define output(ch) putc (ch, yyout)"        >SECTION1
  37.   print "#endif"                    >SECTION1
  38.   print "%}"                        >SECTION1
  39.  
  40.   print "%%"                        >SECTION2
  41.   print "<<EOF>>            { return 1; }"    >SECTION2
  42.  
  43.   print "%%"                        >SECTION3
  44.   print ""                        >SECTION3
  45.   print "#ifndef yywrap"                >SECTION3
  46.   print "int"                        >SECTION3
  47.   print "yywrap (void)"                    >SECTION3
  48.   print "{"                        >SECTION3
  49.   print "  return 1;"                    >SECTION3
  50.   print "}"                        >SECTION3
  51.   print "#endif /* not yywrap */"            >SECTION3
  52. }
  53.  
  54. # A `/* Step name: NAME.  */' line indicates the start of a new file.
  55. # Generate an interface routine.  The step name is saved for broketed
  56. # prefixes in rules.
  57.  
  58. $1 == "/*" && $2 == "Step" && $3 == "name:" && $5 == "*/" {
  59.   section = 1
  60.   step_name = $4
  61.   sub (".$", "", step_name)
  62.   print ""                        >SECTION3
  63.   print "static int"                    >SECTION3
  64.   print "file_" step_name " (const STEP *step, " \
  65.     "FILE *input_file, FILE *output_file)"         >SECTION3
  66.   print "{"                        >SECTION3
  67.   print "  yy_init = 1;"                >SECTION3
  68.   print "  yyin = input_file;"                >SECTION3
  69.   print "  yyout = output_file;"            >SECTION3
  70.   print "  BEGIN " step_name ";"            >SECTION3
  71.   print "  return yylex ();"                >SECTION3
  72.   print "}"                        >SECTION3
  73.   next
  74. }
  75.  
  76. # Remove block C comments in section 1, except the very first.  It is
  77. # assumed that, when a /* comment starts in column 1, there is no code
  78. # following the closing */ on its line.  Also, remove all white lines.
  79.  
  80. /^\/\*/ && section == 1 {
  81.   while (!match ($0, /\*\//)) {
  82.     if (!comment_done) {
  83.       print
  84.     }
  85.     getline
  86.   }
  87.   if (!comment_done) {
  88.     print
  89.   }
  90.   comment_done = 1
  91.   next
  92. }
  93.  
  94. /^[     ]*$/ && section == 1 {
  95.   next
  96. }
  97.  
  98. # A %% in column 1 signals the beginning of lex rules.
  99.  
  100. /^%%/ && section == 1 {
  101.   print "%x " step_name                    >SECTION1
  102.   section = 2
  103.   print ""                        >SECTION2
  104.   next
  105. }
  106.  
  107. # A %{ comment in column 1 signals the start of a C code section ended
  108. # by a %} line.
  109.  
  110. /^%\{/ {
  111.   c_code = 1
  112.   print ""                        >SECTION1
  113.   print                            >SECTION1
  114.   next
  115. }
  116.  
  117. /^%\}/ {
  118.   print                            >SECTION1
  119.   print ""                        >SECTION1
  120.   c_code = 0
  121.   next
  122. }
  123.  
  124. c_code {
  125.   print                            >SECTION1
  126.   next
  127. }
  128.  
  129. # Section 1 declarations are studied and copied, once each.
  130. # Conflicting declaractions are reported at beginning of output.
  131.  
  132. /^[^     ]/ && section == 1 {
  133.   if ($1 in rules) {
  134.     if ($0 != rules[$1]) {
  135.       print "** Conflicting definition: " $0
  136.     }
  137.   }
  138.   else {
  139.     rules[$1] = $0
  140.     print                        >SECTION1
  141.   }
  142.   next
  143. }
  144.  
  145. # In section 2, every non-empty line which does not start with white
  146. # space has to be a lex rule, which is then prefixed by a start name
  147. # derived from the step name.  However, a %% in column 1 starts
  148. # section 3, which contains pure C code, which is copied verbatim.
  149.  
  150. /^$/ && section == 2 {
  151.   print                            >SECTION2
  152.   next
  153. }
  154.  
  155. /^[     ]/ && section == 2 {
  156.   print                            >SECTION2
  157.   next
  158. }
  159.  
  160. /^%%/ && section == 2 {
  161.   section = 3
  162.   next
  163. }
  164.  
  165. section == 2 {
  166.   print "<" step_name ">" $0                >SECTION2
  167.   next
  168. }
  169.  
  170. section == 3 {
  171.   print                            >SECTION3
  172.   next
  173. }
  174.  
  175. # At end, all three temporary files are reread and output, followed by
  176. # the generated interfaces: one routine for each step name.
  177.  
  178. END {
  179.   close (SECTION1)
  180.   while (getline <SECTION1)
  181.     print
  182.   close (SECTION1)
  183.  
  184.   close (SECTION2)
  185.   while (getline <SECTION2)
  186.     print
  187.   close (SECTION2)
  188.  
  189.   close (SECTION3)
  190.   while (getline <SECTION3)
  191.     print
  192.   close (SECTION3)
  193. }
  194.  
  195.