home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1952 < prev    next >
Internet Message Format  |  1990-12-28  |  7KB

  1. From: jpr@jpradley.uucp (Jean-Pierre Radley)
  2. Newsgroups: alt.sources
  3. Subject: [comp.lang.c] Re: Converting long variable names to names <8 characters
  4. Message-ID: <1990Oct14.202826.13577@math.lsa.umich.edu>
  5. Date: 14 Oct 90 20:28:26 GMT
  6.  
  7. Archive-name: unique-nms/13-Oct-90
  8. Original-posting-by: jpr@jpradley.uucp (Jean-Pierre Radley)
  9. Original-subject: Re: Converting long variable names to names <8 characters
  10. Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
  11.  
  12. [Reposted from comp.lang.c.
  13. Comments on this service to emv@math.lsa.umich.edu (Edward Vielmetti).]
  14.  
  15. In article <1091@ria.ccs.uwo.ca> peter@ria.ccs.uwo.ca (Peter Marshall) writes:
  16. >
  17. >Most code these days uses variables that have more than 8 significant
  18. >characters.  I recently purchased a system (a convergent CG1000
  19. >Miniframe -- a Motorola 68010 based system) that has a compiler that
  20. >has the original 8 character limit on variable names.  I haven't been
  21. >able to find a conversion routine that would systematically convert
  22. >long names to shorter ones.  Is there such a beast?
  23. >
  24. >One of the primary candidates for conversion would be the gcc
  25. >compiler.  Once it is running, I suspect that file name problems would
  26. >be either eliminated or at least reduced (I have not yet determined
  27. >the limitations of the linker).  Is there either a version of gcc that
  28. >has been hand converted to 8 character names or a binary that would be
  29. >compatible with my machine available?
  30.  
  31.  
  32. By our late friend Fred Buck:
  33.  
  34.  
  35. :
  36. # Name: unique_nms
  37. #
  38. # Usage: unique_nms [ -y ] [ -v ] [ tagfilename ]
  39. #
  40. # Description: from the set of C source files in the current directory
  41. # (i.e., *.c and *.h), detects function names that aren't unique within
  42. # their first seven characters, and rewrites each reference to such
  43. # functions (including their definitions) with a unique prefix.
  44. #
  45. # If a tagfilename is specified, then the specified tagfilename is
  46. # taken to be a file created by 'ctags', representing the contents of
  47. # the C source files in the current directory, and up to date.  If no
  48. # tagfilename is specified, "tags" is assumed to be the tagfilename.
  49. # The script prompts for whether to restrict the edit to the files
  50. # specifically mentioned in the tagfile, regardless of whether a
  51. # tagfilename is specified on the command line.
  52. #
  53. # If the "-y" (for "yes" or "affirmative") switch is present, no
  54. # prompting will occur; the responses to all ordinary prompts will be
  55. # assumed to be "yes".
  56. #
  57. # If the "-v" (for "void") switch is present, then if a tags file must
  58. # be built, it will be built from copies of the specified source files
  59. # with references to the "void" type removed.  This is for versions of
  60. # 'ctags' that don't know about the "void" data type.  The implementation
  61. # is crude, requiring a physical copy of each source file so as not to
  62. # disturb the original.  The Tandy 6000 requires this switch for any
  63. # source fileset that contains "void" functions.
  64. #
  65. # Notes: this script generates a 'sed' scriptfile called "sedfile$$"
  66. # where "$$" is the current process number.  This file can be referred
  67. # to to keep track of changes made in the original source files; it's
  68. # pretty easy to reverse the changes made by this script with simple
  69. # massaging of the sedfile and re-running 'sed'.
  70. #
  71. # Copyright: this script was written by Fred Buck, who hereby releases
  72. # it into the public domain and expressly waives all copyright or
  73. # similar rights with respect to it.  This script is public property.
  74. # However, liability is disclaimed for any consequences of use of
  75. # this script.
  76.  
  77. trap "rm -f TEMP$$ *@ tags$$ /tmp/tag$$; exit 1" 1 2 15
  78.  
  79. # affirmative mode?
  80. if [ $# -gt 0 -a "$1" = "-y" ]
  81. then
  82.     AFFIRM="yes"
  83.     shift
  84. else
  85.     AFFIRM="no"
  86. fi
  87.  
  88. # anti-void mode?
  89. if [ $# -gt 0 -a "$1" = "-v" ]
  90. then
  91.     VOID="yes"
  92.     shift
  93. else
  94.     VOID="no"
  95. fi
  96.  
  97.  
  98. # usage section
  99. case $# in
  100. 0)
  101.     TAGFILE="tags"                ;;
  102. 1)    if [ ! -r $1 ]
  103.     then
  104.         echo "$0: can't open $1"
  105.         exit 1
  106.     else
  107.         TAGFILE="$1"
  108.     fi                    ;;
  109. *)    echo "usage: $0 [ -y ] [ -v ] [ tagfilename ]"
  110.     exit 1                    ;;
  111. esac
  112.  
  113. # check for readability of specified tags file
  114. if [ ! -r "$TAGFILE" ]
  115. then
  116.     if [ "$AFFIRM" = "no" ]
  117.     then
  118.         echo -n "'tags' file doesn't exist; generate it? "
  119.         read yesno
  120.         case $yesno in
  121.             [Yy]*)                    ;;
  122.             *)    echo "can't work without a tags file"
  123.             exit 1                    ;;
  124.         esac
  125.     fi
  126.     echo "calling 'ctags'....."
  127.     if [ "$VOID" = "no" ]
  128.     then
  129.         ctags *.c *.h
  130.     else
  131.         for SFILE in *.c *.h
  132.         do
  133.             0<$SFILE sed \
  134.                 -e 's/^void//'\
  135.                 -e 's/^static void/static/'\
  136.             1>$SFILE@
  137.         done
  138.         ctags *.c@ *.h@
  139.         mv tags tags$$
  140.         TAGFILE=/tmp/tag$$
  141.         0<tags$$ sed 's/@//' 1>$TAGFILE
  142.         rm tags$$
  143.     fi
  144. else
  145.     echo "The tags file is stamped as follows:"
  146.     echo
  147.     ls -l ./$TAGFILE
  148.     echo
  149.     if [ "$AFFIRM" = "no" ]
  150.     then
  151.         echo -n "Use it? "
  152.         read yesno
  153.         case $yesno in
  154.         [Yy]*)        echo "OK."                ;;
  155.         *)        echo "can't work without a tags file"
  156.                 exit 1                    ;;
  157.         esac
  158.     fi
  159. fi
  160.  
  161. # get to work
  162.  
  163.                 # first, find names ambiguous in 1st 7 chars;
  164.                 # and build a sed scriptfile to disambiguate 'em
  165.                 # (scriptfile will be sedfile$$).  This logic
  166.                 # will accommodate up to about seventeen
  167.                 # thousand ambiguous function names, with
  168.                 # gradually increasing probability that
  169.                 # some "[A-Z][A-Z][A-Z]_*" names may become
  170.                 # ambiguous.
  171. echo "Building edit scriptfile for ambiguous function names....."
  172. 0<$TAGFILE awk '
  173.     BEGIN {
  174.         a = "ABCDEFGFHIJKLMNOPQRSTUVWXYZ"
  175.         i1 = 1
  176.         i2 = 0
  177.         i3 = 0
  178.         l1 = 1
  179.         l2 = 0
  180.         l3 = 0
  181.     }
  182.     { if (substr($1,1,7) == prevsub) {
  183.         prefix = substr(a,i3,l3) substr(a,i2,l2) substr(a,i1,l1)
  184.         printf "s/^%s(/%s_&/\n", $1, prefix
  185.         printf "s/\\([^_]\\)\\(%s(\\)/\\1%s_\\2/g\n", $1, prefix
  186.         printf "s/\\([, \t]\\)\\(%s[^A-Za-z0-9_]\\)/\\1%s_\\2/g\n", \
  187.             $1, prefix
  188.         ++i1
  189.         if (i1 > 26) {
  190.             i1 = 1
  191.             ++i2
  192.             l2 = 1
  193.         }
  194.         if (i2 > 26) {
  195.             i2 = 1
  196.             ++i3
  197.             l3 = 1
  198.         }
  199.         if (i3 > 26) exit(1)
  200.       }
  201.     }
  202.     { prevsub = substr($1,1,7)
  203.     }'                1>sedfile$$
  204.  
  205.     if [ $? -gt 0 ]
  206.     then
  207.         echo "Fatal error: 'awk' failure" 1>&2
  208.         exit 1
  209.     fi
  210.  
  211.                 # if the sedfile is empty, bail out
  212. if [ ! -s sedfile$$ ]
  213. then
  214.     echo "All names in '$TAGFILE' are unique within the first 7 characters."
  215.     rm -f sedfile$$ /tmp/tag$$
  216.     exit 0
  217. fi
  218.  
  219.                 # let the user look over the sedfile first
  220. if [ "$AFFIRM" = "no" ]
  221. then
  222.     echo "Sed command file contains:"
  223.     /usr/bin/less sedfile$$
  224.     echo -n " -- go ahead with edit? "
  225.     read yesno
  226.     case $yesno in
  227.         [Yy]*) echo ;;
  228.         *)     exit 1 ;;
  229.     esac
  230.     echo -n "Restrict edit to files mentioned in '$TAGFILE'? "
  231.     read yesno
  232.     case $yesno in
  233.         [Yy]*) TARGETS=`0<$TAGFILE awk '{ print $2 }' | sort -u` ;;
  234.         *)     TARGETS=`echo *.c *.h`                 ;;
  235.     esac
  236. else
  237.     echo "Restricting edit to files mentioned in '$TAGFILE'."
  238.     TARGETS=`0<$TAGFILE awk '{ print $2 }' | sort -u`
  239. fi
  240. echo "About to edit: $TARGETS" | tr '\012' ' '
  241. echo
  242.  
  243.                 # finally, run the 'sed' commandfile on
  244.                 # the files mentioned in $TAGFILE
  245. for FILE in $TARGETS
  246. do
  247.     echo "Editing $FILE:"
  248.     0<$FILE sed -f sedfile$$ 1>TEMP$$
  249.     if [ $? -eq 0 ]
  250.     then
  251.         mv TEMP$$ $FILE
  252.         echo "    $FILE successfully edited"
  253.     else
  254.         echo "  Can't edit $FILE!"
  255.     fi
  256. done
  257. rm -f TEMP$$
  258. -- 
  259.  Jean-Pierre Radley          HIGH-Q         jpr@jpradley    CIS: 72160,1341
  260.