DEV Community

project-laguardia
project-laguardia

Posted on

LuCI on MGMT - Day 05

Hurdle 4: Identifying Where uci Interop Actually Occurs

Previously:

Task 1: Code-Search

I believe uci interop occurs in the following modules:

  • luci.model.uci

Like we've discussed before, LuCI uses an older version of Lua that allows the module function to be used. This allows LuCI to name modules consistently regradless of whether they are Lua or C modules.

After searching for "luci.model.uci", we discover that it is defined in Lua:

The API doc for luci.model.uci is available at:

One can see how uci may be interacted with by using regex search (?<!l>)uci in code search. Unfortunately, Github code search does not support regex, so we will have to do this locally. Here is a PowerShell script that does that for us:

function Find-InSource {
    param(
        [string] $Pattern,
        [string] $Repository = ".\luci",
        [string[]] $Extensions = @(
            ".lua",
            ".c",
            ".js",
            ".mjs"
        ),
        [switch] $Verbose
    )

    $gitRoot = (git rev-parse --show-toplevel 2>$null)

    $output = [ordered]@{}

    If( $Verbose ) {
        Write-Host "Aggregating source files with extensions: $($Extensions -join ', ')" -ForegroundColor Yellow
        Write-Host "- Wait a moment, this may take a bit..." -ForegroundColor DarkGray
    }

    $files = Get-ChildItem -Path $Repository -File -Recurse | Where-Object {
        $_.Extension -in $Extensions
    }

    if ($files.Count -eq 0) {
        Write-Host "No files found with the specified extensions." -ForegroundColor Yellow
        return $output
    } elseif ( $Verbose ) {
        Write-Host "Searching in $($files.Count) files with extensions: $($Extensions -join ', ')" -ForegroundColor Black -BackgroundColor DarkYellow
    }

    $files | ForEach-Object {
        $filename = $_.FullName.Replace($gitRoot, '').TrimStart('\/')
        $fileContent = Get-Content $_.FullName

        $hits = for ($ln = 0; $ln -lt $fileContent.Count; $ln++) {
            $line = $fileContent | Select-Object -Index $ln
            if ($line -match $pattern) {
                @{
                    Line = $ln + 1
                    Content = $line.Trim()
                }
            }
        }

        if ($hits.Count -gt 0) {
            $output."$filename" = [ordered]@{}

            $hits | ForEach-Object {
                $output."$filename"."$($_.Line)" = $_.Content
            }

            If( $Verbose ){
                Write-Host "$filename`:" -ForegroundColor Cyan

                $hits | ForEach-Object {
                    Write-Host "L$($_.Line)" -NoNewline -ForegroundColor Gray
                    Write-Host ": $($_.Content)"
                }
                Write-Host ""
            }
        }
    }

    If( $Verbose ) {
        $total_files = $output.Keys.Count
        $total_hits = ($output.Values | ForEach-Object { $_.Count }) -as [int[]] | Measure-Object -Sum | Select-Object -ExpandProperty Sum

        Write-Host "Found $total_hits hits in $total_files files." -ForegroundColor Green
    }

    return $output
}

& { # Search for `uci` in the source code
    $hits = Find-InSource -Pattern '(?<!l)uci' -Verbose
    $hits | ConvertTo-Json -Depth 5 | Out-File "porting/searches/uci/hits.json" -Encoding UTF8
    $hits.Keys | Out-File "porting/searches/uci/hits.txt" -Encoding UTF8
}
Enter fullscreen mode Exit fullscreen mode

I will be making this script available in the porting directory of the repository, so that it can be used for future searches.

  • see the README for usage instructions

After running our first search, I've aggregated the following results:

Task 2: Reviewing the Results

For this we are going scope it down to just libs, and modules. Applications are third-party. We may want to port them later, but for now we will focus on the core LuCI codebase. Contrib was matched by mistake. The referenced C file is a html special character parser for ucode. The search script matched the &ucirc; which is html for û, which is not what we are looking for.

Libs:

Top comments (0)