Skip to content

Commit

Permalink
add rpmexpand, rpmexpandnumeric to retrieve rpm macro value
Browse files Browse the repository at this point in the history
add rpmdefine to set rpm macro value
  • Loading branch information
remicollet committed Dec 19, 2024
1 parent deb0dd4 commit 8997cc9
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 5 deletions.
7 changes: 4 additions & 3 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ Documentation: https://www.php.net/rpminfo
</lead>
<date>2024-09-03</date>
<version>
<release>1.1.2dev</release>
<api>1.1.0</api>
<release>1.2.0dev</release>
<api>1.2.0</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="https://www.php.net/license/3_01.txt" filesource="LICENSE">PHP-3.01</license>
<notes><![CDATA[
-
- add rpmexpand, rpmexpandnumeric to retrieve rpm macro value
- add rpmdefine to set rpm macro value
]]></notes>
<contents>
<dir name="/">
Expand Down
2 changes: 1 addition & 1 deletion php_rpminfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
extern zend_module_entry rpminfo_module_entry;
#define phpext_rpminfo_ptr &rpminfo_module_entry

#define PHP_RPMINFO_VERSION "1.1.2-dev"
#define PHP_RPMINFO_VERSION "1.2.0-dev"
#define PHP_RPMINFO_AUTHOR "Remi Collet"
#define PHP_RPMINFO_LICENSE "PHP-3.01"

Expand Down
59 changes: 59 additions & 0 deletions rpminfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <rpm/rpmio.h>
#include <rpm/rpmlib.h>
#include <rpm/rpmts.h>
#include <rpm/rpmmacro.h>

#include "php_rpminfo.h"

Expand Down Expand Up @@ -867,6 +868,64 @@ PHP_FUNCTION(rpmgetsymlink)
}
/* }}} */

/* {{{ proto array rpmexpand(string $text): string
Expand macro in text */
PHP_FUNCTION(rpmexpand)
{
char *text, *result;
size_t len;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &text, &len) == FAILURE) {
RETURN_THROWS();
}

(void)rpminfo_getts(); // read config files

result = rpmExpand(text, NULL);
RETVAL_STRING(result);
free(result);
}
/* }}} */

/* {{{ proto array rpmexpandnumeric(string $text): int
Expand macro in text */
PHP_FUNCTION(rpmexpandnumeric)
{
char *text;
size_t len;
int result;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &text, &len) == FAILURE) {
RETURN_THROWS();
}

(void)rpminfo_getts(); // read config files

result = rpmExpandNumeric(text);

RETURN_LONG(result);
}
/* }}} */

/* {{{ proto array rpmdefine(string $macro): bool
Define a new macro */
PHP_FUNCTION(rpmdefine)
{
char *macro;
size_t len;
int result;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &macro, &len) == FAILURE) {
RETURN_THROWS();
}

(void)rpminfo_getts(); // read config files

result = rpmDefineMacro(NULL, macro, RMIL_GLOBAL);

RETURN_BOOL(result == 0);
}
/* }}} */

/* {{{ PHP_MINIT_FUNCTION
*/
Expand Down
7 changes: 7 additions & 0 deletions rpminfo.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ function rpminfo(string $path, bool $full = false, ?string &$error = null): Arra
function rpmvercmp(string $evr1, string $evr2, ?string $operator = null): int|bool {}

function rpmgetsymlink(string $path, string $name): string|null {}

function rpmexpand(string $text): string {}

function rpmexpandnumeric(string $text): int {}

function rpmdefine(string $macro): bool {}

20 changes: 19 additions & 1 deletion rpminfo_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 6b129e53b21eb21027683874775c2cdb7d1d485d */
* Stub hash: dc980a56084190700162f5a8c70b54fcdbc30ced */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_rpmaddtag, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, rpmtag, IS_LONG, 0)
Expand Down Expand Up @@ -34,13 +34,28 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_rpmgetsymlink, 0, 2, IS_STRING,
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_rpmexpand, 0, 1, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, text, IS_STRING, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_rpmexpandnumeric, 0, 1, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, text, IS_STRING, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_rpmdefine, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, macro, IS_STRING, 0)
ZEND_END_ARG_INFO()


ZEND_FUNCTION(rpmaddtag);
ZEND_FUNCTION(rpmdbinfo);
ZEND_FUNCTION(rpmdbsearch);
ZEND_FUNCTION(rpminfo);
ZEND_FUNCTION(rpmvercmp);
ZEND_FUNCTION(rpmgetsymlink);
ZEND_FUNCTION(rpmexpand);
ZEND_FUNCTION(rpmexpandnumeric);
ZEND_FUNCTION(rpmdefine);


static const zend_function_entry ext_functions[] = {
Expand All @@ -50,5 +65,8 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(rpminfo, arginfo_rpminfo)
ZEND_FE(rpmvercmp, arginfo_rpmvercmp)
ZEND_FE(rpmgetsymlink, arginfo_rpmgetsymlink)
ZEND_FE(rpmexpand, arginfo_rpmexpand)
ZEND_FE(rpmexpandnumeric, arginfo_rpmexpandnumeric)
ZEND_FE(rpmdefine, arginfo_rpmdefine)
ZEND_FE_END
};
25 changes: 25 additions & 0 deletions tests/015-rpmmacro.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Check for rpmdefine, rpmexpand, rpmexpandnumeric
--SKIPIF--
<?php if (!extension_loaded("rpminfo")) print "skip"; ?>
--FILE--
<?php
var_dump(is_dir(rpmexpand("%{_dbpath}")));

var_dump(rpmexpandnumeric("%__isa_bits") === PHP_INT_SIZE * 8);
var_dump(rpmexpandnumeric("0%{?fedora}%{?rhel}") > 0);

$name = "_my_test_macro_for_rpminfo_";
$val = __FILE__;
var_dump(rpmexpand("%$name") === "%$name" );
var_dump(rpmdefine("$name $val"));
var_dump(rpmexpand("%$name") === __FILE__);
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

0 comments on commit 8997cc9

Please sign in to comment.