home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / bin / genclass next >
Text File  |  1996-10-12  |  10KB  |  453 lines

  1. #!/bin/sh
  2.  
  3. #   Copyright (C) 1989 Free Software Foundation, Inc.
  4. #   
  5. #   genclass program enhanced by Wendell C. Baker 
  6. #   (original by Doug Lea (dl@rocky.oswego.edu))
  7.  
  8. #This file is part of GNU libg++.
  9.  
  10. #GNU libg++ is free software; you can redistribute it and/or modify
  11. #it under the terms of the GNU General Public License as published by
  12. #the Free Software Foundation; either version 1, or (at your option)
  13. #any later version.
  14.  
  15. #GNU libg++ is distributed in the hope that it will be useful,
  16. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. #GNU General Public License for more details.
  19.  
  20. #You should have received a copy of the GNU General Public License
  21. #along with GNU libg++; see the file COPYING.  If not, write to
  22. #the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24.  
  25. #
  26. # genclass -list [proto ...]
  27. # genclass -catalog [proto ...]
  28. # genclass type1 {ref|val} proto [out_prefix]
  29. # genclass -2 type1 {ref|val} type2 {ref, val} proto [out_prefix]
  30. #
  31. # Generate classes from prototypes
  32. #
  33. name=genclass ;
  34. usage="
  35.     $name -list [proto ...]
  36.     $name -catalog [proto ...]
  37.     $name type1 {ref|val} proto [out_prefix]
  38.     $name -2 type1 {ref|val} type2 {ref|val} proto [out_prefix]" ;
  39.  
  40. case "$1" in
  41. -usage)
  42.     #
  43.     # -usage
  44.     #
  45.     echo "usage: $usage" 1>&2 ;
  46.     exit 0;
  47.     ;;
  48. -version)
  49.     #
  50.     # -version
  51.     #
  52.     # $(VERSION) is substituted by the build process.
  53.     # We currently use the libg++ version number (extracted from ../Makefile).
  54.     echo "$name: version $(VERSION)" ;
  55.     exit 0;
  56.     ;;
  57. -requires)
  58.     #
  59.     # -requires
  60.     #
  61.     # The following line should contain any nonstandard programs
  62.     # which must be in the users's path (i.e. not referenced by a
  63.     # fullpath);it allows one to check a script for dependencies
  64.     # without exhaustively testing its usages.
  65.     # ... in this case genclass depends on nothing else.
  66.     echo ;
  67.     exit 0;
  68.     ;;
  69. esac ;
  70.  
  71. # pull it in from the environment
  72. [ "$TRACE" = "" ] || set -xv 
  73.  
  74. # Search in standard g++ prototype directory and in the current directory
  75. # NOTE: this variable is edited by the install process
  76. PROTODIR=${PROTODIR-/ade/lib/g++-include/gen}
  77.  
  78. pwd=`pwd`
  79.  
  80. case "$1" in
  81. -catalog*|-list*)
  82.     #
  83.     # genclass -catalog [proto ...]
  84.     # genclass -list [proto ...]
  85.     #
  86.     option="$1" ;
  87.     shift ;
  88.  
  89.     case $# in
  90.     0)
  91.         #
  92.         # -catalog
  93.         # -list
  94.         #
  95.         select=all ;
  96.         select_pattern=p ;
  97.         ;;
  98.     *)
  99.         #
  100.         # -catalog proto ...
  101.         # -list proto ...
  102.         #
  103.         select="$@" ;
  104.         select_pattern= ;
  105.         for i in $@ ; do
  106.             select_pattern="\
  107. $select_pattern
  108. /.*$i\$/ p
  109. " ;
  110.         done ;
  111.  
  112.         ;;
  113.     esac ;
  114.  
  115.     #
  116.     # select_pattern is now a (possibly-vacuous) newline-
  117.     # separated list of patterns of the form:
  118.     #
  119.     #     /.*Proto1$/ p
  120.     #     /.*Proto2$/ p
  121.     #     /.*Proto3$/ p
  122.     #
  123.     # or select_pattern is simply ``p'' to select everything
  124.  
  125.     # Hmmm... not all systems have a fmt program; should we
  126.     # just go ahead and use ``nroff -Tcrt | cat -s'' here?
  127.     fmt='nroff -Tcrt | cat -s'
  128.     #fmt=fmt ;
  129.  
  130.     case "$option" in
  131.     -catalog*)
  132.         #
  133.         # -catalog [proto ...]
  134.         #
  135.         echo "\
  136. Catalog of ${name} class templates
  137. directories searched:
  138.     $PROTODIR
  139.     $pwd
  140. selecting: $select
  141. classes available:" ;
  142.         ;;
  143.     -list*)
  144.         #
  145.         # -list [proto ...]
  146.         #
  147.         # no need to do anything (the list is coming out next)
  148.         ;;
  149.     esac ;
  150.  
  151. # The sed script does the following:
  152. # - If it does not end in a .ccP or .hP then
  153. #   it's not a template and we are not intereseted.
  154. # - Get rid of pathname components [s;.*/;;]
  155. # - Just take the template names
  156. # - change quoting conventions and select off what we want to see
  157. # -if it did not pass the patterns, kill it
  158.  
  159.     ls $pwd $PROTODIR | sed -e '
  160. /\.ccP$/ !{
  161.    /\.hP$/ !{
  162.      d
  163.    }
  164. }
  165. s;.*/;;
  166. s/\.ccP$//
  167. s/\.hP$//
  168. ' -e "$select_pattern
  169. d
  170. " | sort -u | case "$option" in
  171.     -catalog*)
  172.         # The library catalog information preceded the list
  173.         # format the list, and tab in in a bit to make it readable.
  174.         # Re-evaluate $fmt because it might contain a shell command
  175.         eval $fmt | sed -e 's/.*/    &/' ;
  176.         ;;
  177.     -list*)
  178.         # nothing special, just let the sorted list dribble out
  179.         # we must use cat to receive (and reproduce) the incoming list
  180.         cat ;
  181.         ;;
  182.     esac ;
  183.     exit 0;
  184.     ;;
  185. -2)
  186.     #
  187.     # genclass -2 type1 {ref|val} type2 {ref|val} proto [out_prefix]
  188.     #
  189.     N=2 ;
  190.  
  191.     case $# in
  192.     6) # genclass -2 type1 {ref|val} type2 {ref|val} proto
  193.        ;;
  194.     7) # genclass -2 type1 {ref|val} type2 {ref|val} proto out_prefix
  195.        ;;
  196.     *)
  197.     echo "usage: $usage" 1>&2 ;
  198.     exit 1;
  199.     esac ;
  200.     shift ;
  201.     ;;
  202. *)
  203.     #
  204.     # genclass type1 {ref|val} proto [out_prefix]
  205.     #
  206.     N=1 ;
  207.  
  208.     case $# in
  209.     3) # genclass type1 {ref|val} proto
  210.        ;;
  211.     4) # genclass type1 {ref|val} proto out_prefix
  212.        ;;
  213.     *)
  214.     echo "usage: $usage" 1>&2 ;
  215.     exit 1;
  216.     esac ;
  217.     ;;
  218. esac
  219.  
  220. #
  221. # Args are now (the point being the leading ``-2'' is gone)
  222. #
  223. #     type1 {ref|val} proto [out_prefix]
  224. #     type1 {ref|val} type2 {ref|val} proto [out_prefix]
  225. #
  226.  
  227. #
  228. # Quote all of the $1 $2 etc references to guard against
  229. # dynamic syntax errors due to vacuous arguments (i.e. '')
  230. # as sometimes occurs when genclass is used from a Makefile
  231. #
  232.  
  233. T1="$1";
  234. T1NAME="$T1." ;
  235. T1SEDNAME="$T1" ;
  236.  
  237. case "$2" in
  238. ref)
  239.      T1ACC="\&" ;
  240.      ;;
  241. val)
  242.      T1ACC=" " ;
  243.      ;;
  244. *)
  245.     echo "${name}: Must specify type1 access as ref or val" 1>&2 ;
  246.     echo "usage: $usage" 1>&2 ;
  247.     exit 1;
  248.     ;;
  249. esac
  250.  
  251. # N is either 1 or 2
  252.  
  253. case $N in
  254. 1)
  255.     #
  256.     # type1 {ref|val} proto [out_prefix]
  257.     #
  258.     class="$3" ;
  259.  
  260.     T2="" ;
  261.     T2ACC="" ;
  262.     ;;
  263. 2)
  264.     #
  265.     # type1 {ref|val} type2 {ref|val} proto [out_prefix]
  266.     #
  267.     class="$5" ;
  268.  
  269.     T2="$3";
  270.     T2NAME="$T2." ;
  271.     T2SEDNAME="$T2" ;
  272.  
  273.     case "$4" in
  274.     ref)
  275.         T2ACC="\&" ;
  276.         ;;
  277.     val)
  278.         T2ACC=" " ;
  279.         ;;
  280.      *)
  281.         echo "${name}: Must specify type2 access: ref or val" 1>&2 ;
  282.     echo "usage: $usage" 1>&2 ;
  283.     exit 1;;
  284.     esac;
  285.     ;;
  286. esac
  287.  
  288. defaultprefix="$T1NAME$T2NAME" ;
  289.  
  290. case $# in
  291. 3)  # type1 {ref|val} proto
  292.     replaceprefix="N" ;
  293.     prefix="$defaultprefix" ;
  294.     ;;
  295. 5)  # type1 {ref|val} type2 {ref|val} proto
  296.     replaceprefix="N" ;
  297.     prefix="$defaultprefix" ;
  298.     ;;
  299. 4)  # type1 {ref|val} proto out_prefix
  300.     prefix="$4" ;
  301.     replaceprefix="Y" ;
  302.     ;;
  303. 6)  # type1 {ref|val} type2 {ref|val} proto out_prefix
  304.     prefix="$6" ;
  305.     replaceprefix="Y" ;
  306.     ;;
  307. *)
  308.     echo "${name}: too many arguments" 1>&2 ;
  309.     echo "usage: $usage" 1>&2 ;  
  310.     exit 1;
  311.     ;;
  312. esac ;
  313.  
  314. src_h=$class.hP
  315. src_cc=$class.ccP
  316. out_h=$prefix$class.h;
  317. out_cc=$prefix$class.cc ;
  318.  
  319. #
  320. # Note #1: The .h and .cc parts are done separately
  321. #     in case only a .h exists for the prototype
  322. #
  323. # Note #2: Bind the .h and .cc parts to the fullpath
  324. #     directories at the same time to ensure consistency.
  325. #
  326.  
  327. if [ -f $pwd/$src_h ] ; then
  328.     fullsrc_h=$pwd/$src_h ;
  329.     fullsrc_cc=$pwd/$src_cc ;
  330. elif [ -f $PROTODIR/$src_h ] ; then
  331.     fullsrc_h=$PROTODIR/$src_h ;
  332.     fullsrc_cc=$PROTODIR/$src_cc ;
  333. else
  334.     echo "${name}: there is no prototype for class $class - file $src_h" 1>&2 ;
  335.     $0 -list ;
  336.     exit 1;
  337. fi
  338.  
  339. CASES="$N$replaceprefix" ;
  340. # CASES is one of { 2Y 2N 1Y 1N }
  341.  
  342. #
  343. # WATCHOUT - we have no way of checking whether or not
  344. # the proper case type is being used with the prototype.
  345. #
  346. # For example, we have no way of ensuring that any of
  347. # Map variants are specified with the -2 argument set
  348. # Further, we have no way of ensuring that -2 is not
  349. # used with the prototypes which require only one.
  350. #
  351. # The second problem is not serious because it still
  352. # results in correctly-generated C++ code; the first
  353. # problem is serious because it results in C++ code that
  354. # still has ``<C>'' and ``<C&>'' syntax inside it.  Such
  355. # code of course will not compile.
  356. #
  357. # SO THE BEST WE CAN DO - is check for the presence of
  358. # <C> and <C&> AFTER the thing has been generated.
  359. #
  360.  
  361. case $CASES in
  362. 2Y) # Two output substitutions, change the prefix
  363.     sed < $fullsrc_h > $out_h -e "
  364. s/<T>/$T1/g
  365. s/<T&>/$T1$T1ACC/g
  366. s/<C>/$T2/g
  367. s/<C&>/$T2$T2ACC/g
  368. s/$T1SEDNAME\.$T2SEDNAME\./$prefix/g
  369. s/$T1SEDNAME\./$prefix/g
  370. s/$T2SEDNAME\./$prefix/g
  371. " ;
  372.     ;;
  373. 2N) # Two output substitutions, use the default prefix
  374.     sed < $fullsrc_h > $out_h -e "
  375. s/<T>/$T1/g
  376. s/<T&>/$T1$T1ACC/g
  377. s/<C>/$T2/g
  378. s/<C&>/$T2$T2ACC/g
  379. " ;
  380.     ;;
  381. 1Y) # One output substitution, change the prefix
  382.     sed < $fullsrc_h > $out_h -e "
  383. s/<T>/$T1/g
  384. s/<T&>/$T1$T1ACC/g
  385. s/$T1SEDNAME\./$prefix/g
  386. " ;
  387.     ;;
  388. 1N) # One output substitution, use the default prefix
  389.     sed < $fullsrc_h > $out_h -e "
  390. s/<T>/$T1/g
  391. s/<T&>/$T1$T1ACC/g
  392. " ;
  393.     ;;
  394. esac
  395.  
  396. if egrep '<C&?>' $out_h > /dev/null ; then
  397.     echo "${name}: the $class class requires the -2 syntax for the 2nd type" 1>&2 ;
  398.     echo "usage: $usage" 1>&2 ;
  399.     # the user does not get to see the mistakes (he might try to compile it)
  400.     rm $out_h ;
  401.     exit 1;
  402. fi ;
  403.  
  404. if [ ! -f $fullsrc_cc ] ; then
  405.     echo "${name}: warning, class has a .h but no .cc file" 1>&2 ;
  406.     exit 0;
  407. fi
  408.  
  409. case $CASES in
  410. 2Y) # Two output substitutions, change the prefix
  411.     sed < $fullsrc_cc > $out_cc -e "
  412. s/<T>/$T1/g
  413. s/<T&>/$T1$T1ACC/g
  414. s/<C>/$T2/g
  415. s/<C&>/$T2$T2ACC/g
  416. s/$T1SEDNAME\.$T2SEDNAME\./$prefix/g
  417. s/$T1SEDNAME\./$prefix/g
  418. s/$T2SEDNAME\./$prefix/g
  419. "
  420.     ;;
  421. 2N) # Two output substitutions, use the default prefix
  422.     sed < $fullsrc_cc > $out_cc -e "
  423. s/<T>/$T1/g
  424. s/<T&>/$T1$T1ACC/g
  425. s/<C>/$T2/g
  426. s/<C&>/$T2$T2ACC/g
  427. "
  428.     ;;
  429. 1Y) # One output substitution, change the prefix
  430.     sed < $fullsrc_cc > $out_cc -e "
  431. s/<T>/$T1/g
  432. s/<T&>/$T1$T1ACC/g
  433. s/$T1SEDNAME\./$prefix/g
  434. "
  435.     ;;
  436. 1N) # One output substitution, use the default prefix
  437.     sed < $fullsrc_cc > $out_cc -e "
  438. s/<T>/$T1/g
  439. s/<T&>/$T1$T1ACC/g
  440. "
  441.     ;;
  442. esac
  443.  
  444. if egrep '<C&?>' $out_h $out_cc > /dev/null ; then
  445.     echo "${name}: the $class class requires the -2 syntax for the 2nd type" 1>&2 ;
  446.     echo "usage: $usage" 1>&2 ;
  447.     # the user does not get to see the mistakes (he might try to compile it)
  448.     rm $out_h $out_cc ;
  449.     exit 1;
  450. fi ;
  451.  
  452. exit 0;
  453.