Changeset 5674

Show
Ignore:
Timestamp:
02/24/10 13:02:56 (3 years ago)
Author:
blogic
Message:

add command line options, integrate lucic, add unix socket

Location:
luci2/cbi2
Files:
3 added
6 modified

Legend:

Unmodified
Added
Removed
  • luci2/cbi2/cbi.c

    r5673 r5674  
    1212#include <libgen.h> 
    1313 
     14#include "lucic.h" 
    1415#include "list.h" 
    1516#include "blob.h" 
     
    589590        for(i = 0; i < gl.gl_pathc; i++) 
    590591        { 
    591             char buf[256]; 
    592592            char *out = strdup(gl.gl_pathv[i]); 
    593593            out[strlen(out) - 1] = 'o'; 
    594             snprintf(buf, 255, "./lucic %s %s", gl.gl_pathv[i], out); 
    595             buf[255] = '\0'; 
    596594            printf("compiling %s -> %s\n", gl.gl_pathv[i], out); 
    597             if(!system(buf)) 
     595            if(!lucic_main(gl.gl_pathv[i], out)) 
    598596            { 
    599597                printf("deleting %s\n", gl.gl_pathv[i]); 
  • luci2/cbi2/luci.c

    r5664 r5674  
    77#include <unistd.h> 
    88#include <signal.h> 
     9#include <libgen.h> 
    910#include <sys/types.h> 
    1011#include <sys/stat.h> 
    1112#include <sys/mman.h> 
    1213 
     14#define _GNU_SOURCE 
     15#include <getopt.h> 
     16 
    1317#include "cbi.h" 
     18#include "u_sock.h" 
     19#include "lucic.h" 
    1420 
    1521static int loop = 1; 
     
    3137    sigaction(SIGTERM, &s2, NULL); 
    3238} 
     39 
     40int print_help(void) 
     41{ 
     42    printf("luci2 - cbi daemon\n"); 
     43    printf("  -h,-?\t\t- print this help\n"); 
     44    printf("  -s\t\t- open /tmp/luci2-socket for json testing\n"); 
     45    printf("  -d\t\t- daemonize\n"); 
     46    printf("  -v\t\t- use current folder as virtual root\n"); 
     47    printf("  -b in.luco\t- dump the content of a blob\n"); 
     48    printf("  -c in.luci\t- compile luci file\n"); 
     49    printf("  -u in.uvl\t- compile uvl file\n"); 
     50    printf("  -o out.luco\t- write compiled output to file\n"); 
     51    return 0; 
     52} 
     53 
    3354int main(int argc, char **argv) 
    3455{ 
    35     struct cbi_ctx *ctx = cbi_load(); 
     56    int opt; 
     57    struct cbi_ctx *ctx; 
     58    int compile = 0; 
     59    char *c_file = 0; 
     60    int output = 0; 
     61    char *o_file = 0; 
     62    int daemonize = 0; 
     63    int sock = 0; 
     64    int blob; 
     65    char *b_file; 
     66    int uvl = 0; 
     67    char *u_file = 0; 
     68    int vroot = 0; 
     69    const char *vpath = 0; 
     70 
     71    while((opt = getopt(argc, argv, "h?sdvc:o:u:b:")) != -1) 
     72    { 
     73        switch(opt) 
     74        { 
     75        case '?': 
     76        case 'h': 
     77            return print_help(); 
     78        case 's': 
     79            sock = 1; 
     80            break; 
     81        case 'd': 
     82            daemonize = 1; 
     83            break; 
     84        case 'c': 
     85            compile = 1; 
     86            c_file = optarg; 
     87            break; 
     88        case 'b': 
     89            blob = 1; 
     90            b_file = optarg; 
     91            break; 
     92        case 'o': 
     93            output = 1; 
     94            o_file = optarg; 
     95            break; 
     96        case 'u': 
     97            uvl = 1; 
     98            u_file = optarg; 
     99            break; 
     100        case 'v': 
     101            vroot = 1; 
     102            break; 
     103        default: 
     104            printf("unhandled parameter %c\n", opt); 
     105            break; 
     106        } 
     107    } 
     108 
     109    if(compile) 
     110        return lucic_main(c_file, o_file); 
     111 
     112    if(blob) 
     113//      return cbi_dump_blob(b_file); 
     114        return 0; 
     115 
     116    if(uvl) 
     117    { 
     118        printf("todo when lua is added\n"); 
     119        return 1; 
     120    } 
     121 
     122    if(vroot) 
     123    { 
     124        vpath = dirname(*argv); 
     125        printf("using %s as virtual root\n", vpath); 
     126    } 
     127 
     128    if(daemonize) 
     129        daemon(0,0); 
     130 
     131    ctx = cbi_load(); 
    36132 
    37133    cbi_dump(ctx); 
    38134    setup_signals(); 
     135    if(sock) 
     136        u_sock_init(); 
    39137    while(loop) 
    40138    { 
    41         sleep(1); 
     139        if(sock) 
     140            u_sock_poll(); 
     141        else 
     142            sleep(1); 
    42143        cbi_reindex(ctx); 
    43144    } 
  • luci2/cbi2/lucic.c

    r5664 r5674  
    99 
    1010#include "lucip.h" 
    11  
    12 void lucip_blob_dump(const char *path, struct blob_buf *bbuf) 
    13 { 
    14     if(path) 
    15     { 
    16         int fd = open(path, O_CREAT|O_WRONLY, 0644); 
    17         if (fd < 0) 
    18             perror("Cannot open output file"); 
    19         write(fd, bbuf->buf, blob_pad_len(bbuf->buf)); 
    20         close(fd); 
    21     } 
    22 } 
     11#include "lucic.h" 
    2312 
    2413static bool 
     
    3423}; 
    3524 
    36 void print_usage(void) 
     25int lucic_main(const char *in, const char *out) 
    3726{ 
    38     printf("lucic <infile> <outfile>\n"); 
    39     exit(-1); 
    40 } 
    41  
    42 int main(int argc, char **argv) 
    43 { 
    44     if(argc != 3) 
    45         print_usage(); 
    46     if(!lucip_load(argv[1], &bbuf)) 
    47         lucip_blob_dump(argv[2], &bbuf); 
    48     else 
    49         return printf("error opening %s\n",  argv[1]); 
     27    if(!lucip_load(in, &bbuf)) 
     28    { 
     29        printf("Ok!\n"); 
     30        if(out) 
     31            lucip_blob_dump(out, &bbuf); 
     32        else 
     33            printf("use -o to save luco file\n"); 
     34    } else { 
     35        printf("error opening/compiling %s\n", in); 
     36        return 1; 
     37    } 
    5038    return 0; 
    5139} 
  • luci2/cbi2/lucip.c

    r5672 r5674  
    196196            if(*l->last) 
    197197            { 
    198                 //struct lucip_tag *t; 
    199198                if(!l->stackcount) 
    200199                    return lucip_fail(l, OUTSIDE_TAG); 
     
    350349} 
    351350 
     351void lucip_blob_dump(const char *path, struct blob_buf *bbuf) 
     352{ 
     353    if(path) 
     354    { 
     355        int fd = open(path, O_CREAT|O_WRONLY, 0644); 
     356        if (fd < 0) 
     357            perror("Cannot open output file"); 
     358        write(fd, bbuf->buf, blob_pad_len(bbuf->buf)); 
     359        close(fd); 
     360    } 
     361} 
  • luci2/cbi2/lucip.h

    r5664 r5674  
    6060 
    6161int lucip_load(const char *path, struct blob_buf *bbuf); 
     62void lucip_blob_dump(const char *path, struct blob_buf *bbuf); 
    6263 
    6364#endif 
  • luci2/cbi2/Makefile

    r5673 r5674  
    44LDFLAGS?= 
    55LIBS=-llua5.1 
    6 BINARY=lucic luci uvl uvlc liblmo.a lmo_po2lmo 
     6BINARY=luci uvl uvlc liblmo.a lmo_po2lmo 
    77 
    88all: $(BINARY) 
    99 
    10 lucic: lucic.c lucip.c blob.c  
    11     $(CC) $(CFLAGS) -o $@ $^ 
    12  
    13 luci: luci.c cbi.c validate.c uvl.c blob.c widget.c validator/boolean.c widgets/page.c widgets/form.c widgets/section.c widgets/option.c widgets/field.c widgets/foreach.c widgets/file.c widgets/tsection.c  
     10luci: luci.c lucic.c lucip.c cbi.c u_sock.c validate.c uvl.c blob.c widget.c validator/boolean.c widgets/page.c widgets/form.c widgets/section.c widgets/option.c widgets/field.c widgets/foreach.c widgets/file.c widgets/tsection.c  
    1411    $(CC) $(CFLAGS) -o $@ $^ 
    1512