Skip to content

Latest commit

 

History

History
executable file
·
271 lines (213 loc) · 6.47 KB

libymp.rst

File metadata and controls

executable file
·
271 lines (213 loc) · 6.47 KB

Create application with libymp

libymp is part of ymp. We can call ymp operation from libymp.

How to compile

You can compile application with gcc like this:

# single command
gcc main.c -o main `pkg-config --libs --cflags ymp`
# alternative way
gcc -c main.c `pkg-config --cflags ymp`
gcc -o main main.o `pkg-config --libs ymp`

Simple application

You can run libymp functions like this:

#include <ymp.h>
int main(int argc, char* argv[]){
    ymp_init(argv, argc);
    char* args[] = {"Hello", "World"};
    echo_main(args,2);
}

ymp_init create ymp operation manager object to run ymp functions. all libymp operations must require char* args[] and int argc arguments. You should send main function arguments to ymp_init function.

Ymp operation manager

Example hello world program with ymp operation manager like this:

#include <ymp.h>
int main(int argc, char* argv[]){
    ymp_init(argv, argc);
    // script add
    add_script("echo Hello World");
    // process add
    char* args[] = {"Hello", "World"};
    add_process("echo", args, 2);
    return ymp_run();
}

add_script function add ymp shell script. add_process function add ymp process. ymp_run call ymp operations and return exit status.

For all ymp function please see /usr/include/ymp.h header.

Using libymp with vala

You can use ymp with vala using vapi. For example:

int main(string[] argv){
    ymp_init(argv);
    string[] args = {"Hello","World"};
    add_process("echo",args);
    ymp_run();
    return 0;
}

You can build code with this command:

valac main.vala --pkg ymp

An alternative method you can use C based library on vala.

extern void ymp_init(string[] args);
extern int echo_main(string[] args);
int main(string[] argv){
    ymp_init(argv);
    string[] args={"Hello"};
    echo_main(args);
    return 0;
}

And compile program with this command.

valac main.vala -X -lymp

Note: This method is a bad idea but working :)

libymp API

Package struct

libymp has package struct for package operations. We can define package like this:

#include <ymp.h>
int main(int argc, char** argv){
    ymp_init(argv, argc);
    char** deps = {"example", "test"};
    package *p = package_new();
    p->name = "test";
    p->dependencies = deps;
    return 0;
}

Also we can get package struct from repository or file or installed.

#include <ymp.h>
int main(int argc, char** argv){
    ymp_init(argv, argc);
    // for automatic
    package *p0 = get_package("test");
    // for installed package
    package *p1 = get_installed_package("test");
    // for repository (with your emerge option)
    package *p2 = get_from_repository("test");
    // for binary package from repository
    package *p3 = get_package_from_file("test");
    // for source package from repository
    package *p4 = get_source_from_repository("test");
    // for package file
    package *p5 = get_package_from_file("./test.ymp");
    return 0;
}

We can get information about package struct with this functions:

#include <ymp.h>
int main(int argc, char** argv){
    ymp_init(argv, argc);
    int len;
    package *p = get_instaled_package("test");
    char** files = package_list_files(p, &len)
    char** links = package_list_links(p, &len)
    char* name = package_get(p, "name");
    char** deps = package_gets(p, "depends");
    char* uri = package_get_uri(p);
    gboolean ii = package_is_installed(p);
    return 0;
}

Repository struct

libymp has repository structure. We can define like this:

#include <ymp.h>
int main(int argc, char** argv){
    ymp_init(argv, argc);
    repository *r = repository_new();
    r->name = "main";
    r->address = "https://example.org/repo/$uri";
    return 0;
}

Also we can list available repositories:

#include <ymp.h>
#include <glib/gprintf.h>
int main(int argc, char** argv){
    ymp_init(argv, argc);
    int repo_len;
    repository** repos = get_repos(&repo_len);
    for(int i=0;i<repo_len;i++){
        g_printf("%s\n",repos[i]->name);
        g_printf("%d\n", repos[i]->address);
    }
    return 0;
}

We can get package struct from repository functions.

#include <ymp.h>
int main(int argc, char** argv){
    ymp_init(argv, argc);
    int repo_len;
    repository* r;
    // load repository from name
    repository_load(r,"main");
    // for binary package
    package p0 = repository_get_package(r, "test");
    // for source package
    package p1 = repository_get_source(r, "test");
    // check binary package is available
    bool b0 = repository_has_package(r,"test");
    // check source package is available
    bool b1 = repository_has_source(r,"test");
    // fetch binary package list
    char** l0 = repository_list_packages(r);
    // fetch source package list
    char** l0 = repository_list_sources(r);
    return 01
}

We can list installed package names:

#include <ymp.h>
int main(int argc, char** argv){
    ymp_init(argv, argc);
    int len;
    // list instaled packages
    char** l0 = list_installed_packages(len);
    return 0;
}

Usefull library parts of libymp

libymp provide some usefull functions.

The array library of libymp

An example array library usage in here:

#include <ymp.h>
#include <glib/gprintf.h>

int main(int argc, char* argv[]){
    ymp_init(argv, argc);
    array *a = array_new();
    array_add(a,"hello");
    array_add(a,"world");
    gint len=0;
    for(int i=0;i<array_length(a);i++){
        g_printf("%s ",array_get(a,&len)[i]);
        g_printf("%d\n",len);
    }
    return 0;
}

Note: Array library uses glib types. You can use standard types but it is not recommended.

Yaml parser

An example yaml parser usage in here:

#include <ymp.h>
#include <glib/gprintf.h>

int main(int argc, char* argv[]){
    ymp_init(argv, argc);
    yamlfile *y = yamlfile_new();
    yamlfile_load(y, "/etc/ymp.yaml");
    gchar* data = yamlfile_get(y, "ymp");
    g_printf("%s\n", yamlfile_get_value(y, data, "compress"));
    return 0;
}