Skip to content

Commit

Permalink
Export rpmGlobPath() to Lua as rpm.glob()
Browse files Browse the repository at this point in the history
Lua doesn't ship with batteries, or glob functionality. It's not
unthinkable that packagers and macro writers may find globbing useful
in this space.
  • Loading branch information
pmatilai committed May 30, 2023
1 parent 636eadd commit 9f568c4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/manual/lua.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ rpm.expand('/usr%{__lib}/mypath')
rpm.expand('%{_libdir}')
```

#### glob(pattern, [flags])

Return pathnames matching pattern in a table.
If flags contains "c", return the original pattern in case of no matches.

```
for i, p in ipairs(rpm.glob('*')) do
print(p)
end
```

#### interactive()

Launch interactive session for testing and debugging.
Expand Down
25 changes: 25 additions & 0 deletions rpmio/rpmlua.c
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,30 @@ static int rpm_unsplitargs(lua_State *L)
return 1;
}

static int rpm_glob(lua_State *L)
{
const char *pat = luaL_checkstring(L, 1);
rpmglobFlags flags = RPMGLOB_NONE;
int argc = 0;
ARGV_t argv = NULL;

if (luaL_optstring(L, 2, "c"))
flags |= RPMGLOB_NOCHECK;

if (rpmGlobPath(pat, flags, &argc, &argv) == 0) {
lua_createtable(L, 0, argc);
for (int i = 0; i < argc; i++) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i + 1);
}
argvFree(argv);
} else {
luaL_error(L, "glob %s failed: %s", pat, strerror(errno));
}

return 1;
}

static int newinstance(lua_State *L, const char *name, void *p)
{
if (p != NULL) {
Expand Down Expand Up @@ -1271,6 +1295,7 @@ static const luaL_Reg rpmlib[] = {
{"open", rpm_open},
{"splitargs", rpm_splitargs},
{"unsplitargs", rpm_unsplitargs},
{"glob", rpm_glob},
{NULL, NULL}
};

Expand Down
18 changes: 18 additions & 0 deletions tests/rpmmacro.at
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,24 @@ runroot rpm --eval '%{lua:print(5*5)}'
])
AT_CLEANUP

AT_SETUP([lua glob])
AT_CHECK([
RPMDB_INIT
runroot_other mkdir -p aaa/{123,223,323,322,321}
runroot rpm --eval "%{lua:for i,p in ipairs(rpm.glob('aaa/3*')) do print(p..'\\n') end}"
runroot rpm --eval "%{lua:for i,p in ipairs(rpm.glob('aaa/b*', 'c')) do print(p..'\\n') end}"
],
[0],
[aaa/321
aaa/322
aaa/323

aaa/b*

],
[])
AT_CLEANUP

AT_SETUP([lua macro arguments])
AT_KEYWORDS([macros lua])
AT_CHECK([[
Expand Down

0 comments on commit 9f568c4

Please sign in to comment.