contrib/lar:
[project/luci.git] / contrib / lar / cli.c
1 #include "lar.h"
2
3 int do_print_member( lar_archive *ar, const char *name )
4 {
5         lar_member *member;
6
7         if( (member = lar_open_member(ar, name)) != NULL )
8         {
9                 write(fileno(stdout), member->data, member->length);
10                 lar_close_member(member);
11         }
12         else
13                 LAR_DIE("Unable to locate archive member");
14
15         return 0;
16 }
17
18 int do_print_index( lar_archive *ar )
19 {
20         lar_index *index = ar->index;
21         LAR_FNAME(filename);
22
23         while(index)
24         {
25                 lar_get_filename(ar, index, filename);
26                 printf("%s\n", filename);
27                 index = index->next;
28         }
29
30         return 0;
31 }
32
33 int do_require( const char *package )
34 {
35         int stat = 1;
36         lar_archive *ar;
37         lar_member *mb;
38
39         if( (ar = lar_find_archive(package)) != NULL )
40         {
41                 if( (mb = lar_find_member(ar, package)) != NULL )
42                 {
43                         write(fileno(stdout), mb->data, mb->length);
44                         lar_close_member(mb);
45                         stat = 0;
46                 }
47
48                 lar_close(ar);
49         }
50
51         return stat;
52 }
53
54 int main( int argc, const char* argv[] )
55 {
56         lar_archive *ar;
57         int stat = 0;
58
59         if( argv[1] != NULL )
60         {
61                 switch(argv[1][0])
62                 {
63                         case 's':
64                                 if( (ar = lar_open(argv[2])) != NULL )
65                                 {
66                                         if( argv[3] != NULL )
67                                                 stat = do_print_member(ar, argv[3]);
68                                         else
69                                                 stat = do_print_index(ar);
70
71                                         lar_close(ar);
72                                 }
73                                 else
74                                 {
75                                         LAR_DIE("Failed to open archive");
76                                 }
77
78                                 break;
79
80                         case 'r':
81                                 stat = do_require(argv[2]);
82                                 break;
83                 }
84
85                 return stat;
86         }
87         else
88         {
89                 printf("Usage:\n");
90                 printf("\tlar show <archive> [<member>]\n");
91                 printf("\tlar require <package>\n");
92
93                 return 1;
94         }
95
96         return 0;
97 }