home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March B / SCO_CASTOR4RRT.iso / uccs / root.14 / udk / usr / ccs / bin / ctc < prev    next >
Text File  |  1998-08-19  |  3KB  |  157 lines

  1. #!/sbin/sh
  2.  
  3. #ident    "@(#)ctrace:common/ctcr    1.6"
  4. # ctc, ctcr - ctrace, compile, and optionally run a C program
  5. #
  6.  
  7. if test $# = 0
  8. then    cat <<!
  9. Usage:    ctc  [ ctrace options ] file ... [ makefile target ] [ make macros ]
  10.     ctcr [ ctrace options ] file ... [ makefile target ] [ make macros ]
  11. !
  12.     exit 1
  13. fi
  14.  
  15. # get any initial parameter values:
  16.  
  17. if test -f .ctcr_init
  18. then    . .ctcr_init
  19. fi
  20.  
  21. # default parameter values:
  22.  
  23. MAKE=${MAKE-make}
  24.  
  25. # get the arguments:
  26.  
  27. for i
  28. do    case $i in
  29.     *.[cly])    if test "$lastarg" != -r
  30.             then    firstfile=$i
  31.                 break
  32.             fi
  33.             ;;
  34.     -r)        CTRACEFLAGS="$CTRACEFLAGS $i '$2'"
  35.             shift 2
  36.             ;;
  37.     *)        CTRACEFLAGS="$CTRACEFLAGS '$i'"
  38.             shift
  39.     esac
  40.     lastarg=$i
  41. done
  42.  
  43. for i
  44. do    case $i in
  45.     *.[cly])    files="$files $i"
  46.             ;;
  47.             # note: $i may contain embedded blanks.
  48.     -*)        MAKEARGS="$MAKEARGS $i"
  49.             ;;
  50.     *)        if expr "$i" : '[^=]*=' >/dev/null
  51.             then    MAKEFLAGS="$MAKEFLAGS $i" # don't quote make args
  52.             else    TARGET=$i
  53.             fi
  54.     esac
  55. done
  56.  
  57. if [ $firstfile ]
  58. then    :
  59. else    echo missing file parameter
  60.     exit 1
  61. fi
  62.  
  63. # check for a makefile:
  64.  
  65. for i in [Mm]akefile s.[Mm]akefile *.mk
  66. do    case $i in
  67.     ctcr_temp.mk)    ;;
  68.     Makefile|makefile|s.Makefile|s.makefile|[!*]*.mk)    makefile=$i ;;
  69.     esac
  70. done
  71.  
  72. # make sure that the makefile is for the traced files:
  73.  
  74. prefix=`expr $firstfile : '\(.*\)\..$'`
  75.  
  76. if [ $makefile ] && egrep "^$prefix.o[^a-z]|[^a-z]$prefix.o[^a-z]|[^a-z]$prefix.o$" $makefile >/dev/null
  77. then    # change the TARGET name in a copy of the makefile:
  78.     pwd=`pwd`
  79.     TARGET=${TARGET-`basename $pwd`}
  80.     patterns="
  81. s/^$TARGET\([^a-z.]\)/$TARGET.t\1/g
  82. s/\([^a-z]\)$TARGET\([^a-z.]\)/\1$TARGET.t\2/g
  83. s/\([^a-z]\)$TARGET$/\1$TARGET.t/g
  84. s/^$TARGET\.b\([^a-z]\)/$TARGET.t.b\1/g
  85. s/\([^a-z]\)$TARGET.b\([^a-z]\)/\1$TARGET.t.b\2/g
  86. s/\([^a-z]\)$TARGET.b$/\1$TARGET.t.b/g"
  87.  
  88.     # change a copy of the makefile to trace each file:
  89.  
  90.     for i in $files
  91.     do    prefix=`expr $i : '\(.*\)\..$'`
  92.         patterns="$patterns
  93. s/^$prefix\.o\([^a-z]\)/$prefix\.t.o\1/g
  94. s/\([^a-z]\)$prefix\.o\([^a-z]\)/\1$prefix\.t.o\2/g
  95. s/\([^a-z]\)$prefix\.o$/\1$prefix\.t.o/g"
  96.     done
  97.  
  98.     sed "$patterns" $makefile >ctcr_temp.mk
  99.     
  100.     # change a copy of any 3bldp spec file to trace each file:
  101.  
  102.     if test -f $TARGET.b
  103.     then    sed "$patterns" $TARGET.b >$TARGET.t.b
  104.     fi
  105.  
  106.     # force relinking because the traced file's .t.o file may be up-to-date.
  107.  
  108.     rm -f $TARGET.t
  109.  
  110. else    # create a makefile:
  111.     TARGET=${TARGET-a.out}
  112.     cat >ctcr_temp.mk <<!
  113. $TARGET.t: $prefix.t.o
  114.     cc $LDFLAGS $prefix.t.o -o $TARGET.t
  115. !
  116. fi
  117.  
  118. # Add the transformation rules for tracing C, lex, and yacc files:
  119. # Note: Can't use $*.t.c in place of temp.c because editing $*.c will not remake $*.t.c.
  120. cat >>ctcr_temp.mk <<!
  121. .SUFFIXES: .t.o
  122. .c.t.o:
  123.     ctrace $CTRACEFLAGS $< >ctcr_temp.c
  124.     $(CC) $(CFLAGS) -c ctcr_temp.c
  125.     rm ctcr_temp.c
  126.     mv ctcr_temp.o \$@
  127. .l.t.o:
  128.     ltrace $(LTRACEFLAGS) $< >ctcr_temp.l
  129.     $(LEX) $(LFLAGS) ctcr_temp.l
  130.     rm ctcr_temp.l
  131.     $(CC) $(CFLAGS) -c lex.yy.c
  132.     rm lex.yy.c
  133.     mv lex.yy.o \$@
  134. .y.t.o:
  135.     ltrace $(LTRACEFLAGS) $< | yaccer $(YACCERFLAGS) >ctcr_temp.y
  136.     $(YACC) $(YFLAGS) ctcr_temp.y
  137.     rm ctcr_temp.y
  138.     $(CC) $(CFLAGS) -c y.tab.c
  139.     rm y.tab.c
  140.     mv y.tab.o \$@
  141. !
  142. # trace and compile the file:
  143.  
  144. command="$MAKE -f ctcr_temp.mk ${MAKEFILAGS:+-}$MAKEFLAGS $MAKEARGS $TARGET.t"
  145. echo $command
  146. $command
  147.  
  148. # Run target file if no errors and this script was not called as ctc.
  149.  
  150. if [ $? -eq 0 ]
  151. then    rm -f ctcr_temp.* $TARGET.t.b
  152.     if [ `basename $0` != ctc ]
  153.     then    set -x
  154.         $TARGET.t $RUNFLAGS
  155.     fi
  156. fi
  157.