RENAME - Variable renamer for BASIC Kelvin Smith, Financial Computer Systems, Inc. When I joined this company, I found many of the programs had been written in a dialect of Basic which allowed only one-letter or one-letter plus one-digit variable names, a horror story only too familiar to old-timers in the Basic world. Giving the variables more meaningful names was a top priority in order to be able to maintain and enhance the programs. However, finding and changing single-character names is a tedious process very prone to error; since one program alone (in several subprograms, but with common variable names) ran to over 120 pages of code, I was very uncomfortable with the idea of making the changes manually. Being an old TECO hacker ("hacker" used in the positive sense), it occurred to me that the problem might be amenable to a TECO solution. The program which follows will properly distinguish even single-character variable names and rename them to whatever you desire. While this program was designed to work with BASIC-PLUS-2 programs, using TECO V40 for RSTS/E, it should work properly for most BASIC and TECO combination on PDP-11s and VAXes. It may be usable in other language environments as well, but obviously the tightness of fit will depend on the particular language's syntax. RENAME depends on punctuation to separate the symbols it looks at: quoted strings (with either single or double quotation marks) are skipped; exclamation points mark the beginning and end of comments (by default, nothing in comments is changed); dollar and percent signs indicate a string or integer suffix; an open parenthesis marks an array (or function) reference; underscores are acceptable in the middle of a variable name; all other punctuation is considered a breakpoint. Note that this means that legal "no extend" Basic-Plus code, which did not require spaces to separate variables and keywords, will not be processed correctly. Fixing that code is quite another matter. Since I was not inclined to completely emulate the syntax checking done by the BASIC compiler, this program does have a few restrictions. It works by essentially breaking out every different symbol reference, whether a variable name or a valid BASIC construct. (A statement like PRINT, a built-in function like EDIT$, or a variable like FOO% will each get picked out.) It then checks to see if the symbol found is in its list of variables to change; a match generates a replacement. This means that RENAME can annihilate the meaning of a program (try changing IF to LET, for example); however, this could also changing "declining features" like CVT$$(). be useful for •RENAME - Variable renamer Page 2 Additional restrictions: 1) While RENAME's input routine kicks out gross errors in variable names, there is nonetheless a large class of character sequences which will pass RENAME but bomb on a compile attempt. If you find, however, that you have given an illegal name, you can simply change that again. 2) A few BASIC statements refer to arrays without using parentheses. RENAME handles the CHANGE statement correctly as long as the string variable has a "$" suffix. It does not handle MAT, essentially because I don't use those functions. You can always add your own code. 3) Similarly, RENAME will not change function references inside a multi-line DEF, where the function name is referred to without parentheses. The workaround for this is to enter two renaming lines, one with and one without the terminating parenthesis: e.g., FNA%(/FNNEW.NAME%( and FNA%/FNNEW.NAME%. A performance note: Since each symbol is chopped out and checked, it is much better from a performance standpoint to run RENAME once with a long list of variables to change than to run it several times with short lists. Like any TECO program, RENAME is a system hog while running (and on long programs may take 5 minutes or more of CPU time); you may want to run it in batch mode at reduced priority. Instructions for use RENAME can be RUN like any other program; you need not enter TECO first, and the user need not even know that the program is written in TECO. (Note: This may be different on non-RSTS systems.) It is highly recommended that RENAME be SQUished (using the SQU macro available on some distribution tapes from DEC) before running, since the comments slow down execution enormously. When run, RENAME responds with its header and prompts: File to fix <.B2S>? Enter the name of the program to modify; the default filetype is .B2S. The file will be opened in backup mode; when complete, RSTS gives the old version of the file the filetype .BAK. (Other systems may handle backup mode differently; see the TECO User's Guide for details.) It is recommended that you NOT specify /B2, if you still use ampersand continuation, since RENAME gets just one line at a time, and thus would eliminate all the ampersands. (If you wish to switch from ampersands and backslashes to the BASIC Version 2 format, with implicit statement continuation, see •RENAME - Variable renamer Page 3 my STRIP macro elsewhere on this tape.) If you want RENAME to change any matches in comments, add /C to the file name. Note that this will slow execution. Enter old and new variable name, separated by "/". Enter "@filnam" to use list of names in a file. Press <CR> to end list. Old name/new name? Enter one by one your list of variables to rename with their new names. The format of input is "OLDNAME/NEWNAME". Any $ or % suffix must be included. If the variable is an array (of any number of dimensions), it must be followed by an open parenthesis "(". Several examples are shown at the end of this documentation. You can put a list of variable names in another file and read those in by entering "@filnam" (e.g., "@FOO.LST"). (This is particularly useful for a number of subprograms with common variable names.) RENAME will do the same error checking that it does on normal input, listing out each line as it reads and processes that line. After RENAME is finished with the file, it will return to the "Old name/new name" prompt. If you want RENAME to assume that all renamings are correct which would normally require confirmation (see below), add /N to the indirect file name ("No confirm"). Note that any illegal input is still rejected. When all names have been entered, press <CR> response to the prompt and the changing will begin. in Typing CTRL/C at any time during the operation will cause an abort with the original file left intact. When finished, RENAME will print out the total number of references changed and exit. Error checking RENAME does a limited amount of checking the input. The following errors cause the input line to be rejected: 1) 2) 3) Only one name has a "(" indicating an array. One of the names does not begin with a letter. The "old" name has already been used. 4) 5) No "/" is found separating the two names. One of the names ends with "$" and the other ends with "%". •RENAME - Variable renamer Page 4 The following situations cause RENAME to ask for confirmation of the validity of the input, unless the /N qualifier was used when the file name holding a variable list was entered (see above): 1) One name has a $ or %, but the other does not (this may be correct when changing from floating point variables to integers or when changing implicitly defined variables to DECLAREd variables). 2) The "new" name has already been used (you may want to combine two old variables into one new one, particularly a dummy). Input examples A%/FOO% A%(/SORT%( Q0/TEST.VAR% FIRST.VAR$/SECOND.VAR T%/R$ Q0/NEW.TEST% B%/FOO% A normal change. An array change. Confirm: is the "%" correct? Confirm: is the "$" correct? Illegal: "%" and "$" conflict. Illegal: Q0 was already used. Same new name as A% above; confirm if this is correct. As always with TECO, experimentation is encouraged. If you have any questions, or come up with ideas for enhancements, feel free to contact me. Kelvin Smith Financial Computer Systems, Inc. 1 Strawberry Hill Ct. Stamford, CT 06902 (203) 357-0504