@@ -1369,6 +1369,49 @@ static size_t command_calc_nary(command_result_t *result, command_file_ctx_t *cc
13691369 RETURN_OK (slen );
13701370}
13711371
1372+ /** Perform casting
1373+ *
1374+ */
1375+ static size_t command_cast (command_result_t * result , command_file_ctx_t * cc ,
1376+ char * data , UNUSED size_t data_used , char * in , size_t inlen )
1377+ {
1378+ fr_value_box_t * a , * out ;
1379+ size_t match_len ;
1380+ fr_type_t type ;
1381+ char const * p , * value , * end ;
1382+ size_t slen ;
1383+
1384+ a = talloc_zero (cc -> tmp_ctx , fr_value_box_t );
1385+
1386+ p = in ;
1387+ end = in + inlen ;
1388+
1389+ match_len = parse_typed_value (result , a , & value , p , end - p );
1390+ if (match_len == 0 ) return 0 ; /* errors have already been updated */
1391+
1392+ p += match_len ;
1393+ fr_skip_whitespace (p );
1394+
1395+ out = talloc_zero (cc -> tmp_ctx , fr_value_box_t );
1396+
1397+ if (strncmp (p , "->" , 2 ) != 0 ) RETURN_PARSE_ERROR (0 );
1398+ p += 2 ;
1399+ fr_skip_whitespace (p );
1400+
1401+ type = fr_table_value_by_longest_prefix (& match_len , fr_type_table , p , end - p , FR_TYPE_MAX );
1402+ if (type == FR_TYPE_MAX ) RETURN_PARSE_ERROR (0 );
1403+ fr_value_box_init (out , type , NULL , false);
1404+
1405+ if (fr_value_box_cast (out , out , type , NULL , a ) < 0 ) {
1406+ RETURN_OK_WITH_ERROR ();
1407+ }
1408+
1409+ slen = fr_value_box_print (& FR_SBUFF_OUT (data , COMMAND_OUTPUT_MAX ), out , NULL );
1410+ if (slen <= 0 ) RETURN_OK_WITH_ERROR ();
1411+
1412+ RETURN_OK (slen );
1413+ }
1414+
13721415/** Change the working directory
13731416 *
13741417 */
@@ -3042,6 +3085,11 @@ static fr_table_ptr_sorted_t commands[] = {
30423085 .usage = "calc_nary op <type1> <value1> <type2> <value2> ... -> <output-type>" ,
30433086 .description = "Perform calculations on value boxes" ,
30443087 }},
3088+ { L ("cast " ), & (command_entry_t ){
3089+ .func = command_cast ,
3090+ .usage = "cast (type) <value> -> <output-type>" ,
3091+ .description = "Perform calculations on value boxes" ,
3092+ }},
30453093 { L ("cd " ), & (command_entry_t ){
30463094 .func = command_cd ,
30473095 .usage = "cd <path>" ,
0 commit comments