2016-06-24 1 views
1

:UCI C의 API - 목록 옵션과 함께 작동하는 방법 우리는 다음과 같은 명령을 사용하여 전체 목록을 검색 할 수 있습니다 UCI를 사용

config system 
    option hostname 'OpenWrt' 
    option timezone 'UTC' 

config timeserver 'ntp' 
    list server '0.openwrt.pool.ntp.org' 
    list server '1.openwrt.pool.ntp.org' 
    list server '2.openwrt.pool.ntp.org' 
    list server '3.openwrt.pool.ntp.org' 
    option enabled '1' 
    option enable_server '0' 

반환 : 이것은 아래 구성의 종류를 읽을 것

$ uci get system.ntp.server 

하나의 긴 문자열에서 모든 ntp 서버가 정상적으로 작동합니다.

0.openwrt.pool.ntp.org 1.openwrt.pool.ntp.org 2.openwrt.pool.ntp.org 3.openwrt.pool.ntp.org 

나는 C api를 사용하여 동일 (또는 동급)을 달성하고자합니다. 이것은 단지 출력 문자열에서 쓰레기를 반환 실행

#include <uci.h> 
#include <string.h> 
void main() 
{ 
    //char path[] = "system.ntp.enabled"; 
    char path[] = "system.ntp.server"; 
    char buffer[80]; 
    get_config_entry(path, &buffer); 
    printf("%s\n", buffer); 

} 

int get_config_entry (char *path, char *buffer) 
{ 
    struct uci_context *c; 
    struct uci_ptr ptr; 

    c = uci_alloc_context(); 
    if (uci_lookup_ptr (c, &ptr, path, true) != UCI_OK) 
    { 
     uci_perror (c, "XXX"); 
     return 1; 
    } 

    strcpy(buffer, ptr.o->v.string); 
    uci_free_context (c); 
    return 0; 
} 

:

다음 코드를 함께 넣어.

UCI C API를 사용하여 목록 콘텐츠를 어떻게 처리해야합니까?

+0

'main' 전에'get_config_entry'를위한 프로토 타입을 넣어야합니다 (또는'main' 전에 전체 함수를 올리면됩니다). 경고를 사용하여 컴파일했다면 (예 :'gcc -Wall ...') 컴파일러는 유용하게도 실수를 지적했을 것입니다. –

+0

"통합 구성 인터페이스"가 아닌 "범용 체스 인터페이스"에 관한 UCI 태그를 제거했습니다. –

+0

나는 다음과 같이 호출해야한다고 생각한다 : get_config_entry (path, buffer); 참고 "버퍼"앞에 앰퍼샌드가 없습니다. – TonyB

답변

1

목록 요소가 요청되면이 요소는 v.string이 아닌 v.list에 저장됩니다.

uci_show_value 함수가 많은 도움이 된 uci cli 코드에서 발견되었습니다. 필자는 목록 옵션을 사용하여 다음 코드를 잘 처리 할 수있었습니다.

관련 문제