2023-06-27

Pulling a sed script into a perl program

I need to shorten function names, so I identify them and produce a really long sed script that looks like this:

s/\breally_long_function_name1\b/A00128/g
s/\breally_long_function_name2\b/A00060/g
s/\breally_long_function_name3\b/A00035/g
s/\breally_long_function_name4\b/A00342/g
s/\breally_long_function_name5\b/A00203/g
...

and then call it like this:

`sed -i.bak -f $sedscript *`

The problem is that I can't depend on sed being able to handle those instances of \b and it seems messy. Instead of writing out that sed script, I want to put it into an array and then do something like this for each line of each file I need to process:

$targetline =~ $processing;

My problem is that using $processing like that won't work and the qoperators don't seem to do the job. How do I massage this to get the substitution in $processing processed and the result put in $targetline?

Note: I used Perl sed file inside script and the answer from @ron-bergin there to get this far.

The application for this is to convert modern-ish C code into a form that can be compiled by ancient C compilers. The existing script is at https://gitlab.com/DavidGriffith/frotz/-/blob/master/src/misc/snavig.pl where it's used to prepare source code for compilation by KCC, an early C compiler for PDP-10 mainframes. One of its quirks is that some symbols are limited to being 6 characters in length. Background on this is at https://github.com/PDP-10/panda/blob/master/files/kcc-6/kcc/user.doc#L519.

Here's a fragment from a source file before processing:

void reset_memory(void)
{
        if (story_fp != NULL)
                fclose(story_fp);
        story_fp = NULL;

        if (undo_diff) {
                free_undo(undo_count);
                zfree(undo_diff);
                zfree(prev_zmp);
        }

        undo_diff = NULL;
        undo_count = 0;
        prev_zmp = NULL;

        if (zmp)
                zfree(zmp);
        zmp = NULL;
} /* reset_memory */

After processing, it looks like this:

void A00156(void)
{
        if (A00144 != NULL)
                fclose(A00144);
        A00144 = NULL;

        if (undo_diff) {
                A00155(A00148);
                zfree(undo_diff);
                zfree(A00147);
        }

        undo_diff = NULL;
        A00148 = 0;
        A00147 = NULL;

        if (zmp)
                zfree(zmp);
        zmp = NULL;
} /* A00156 */

Once processed like this, it is proven to compile with KCC and run on TOPS20 on both emulated and real PDP-10 hardware.

With my attempts to not use an external sed, instead of doing anything, I get nothing and then a flood of this when pressing ^C:

Use of uninitialized value $targetline in pattern match (m//) at src/misc/snavig.pl line 181, <$targetfile> line 104.
Use of uninitialized value $targetline in pattern match (m//) at src/misc/snavig.pl line 181, <$targetfile> line 104.
Use of uninitialized value $targetline in pattern match (m//) at src/misc/snavig.pl line 181, <$targetfile> line 104.
Use of uninitialized value $targetline in pattern match (m//) at src/misc/snavig.pl line 181, <$targetfile> line 104.


No comments:

Post a Comment