Fix even more leaks
[pandora-libraries.git] / apps / pnd_info.c
1
2 #include <stdio.h> /* for printf, NULL */
3 #include <stdlib.h> /* for free */
4 #include <string.h> /* for strdup */
5
6 #include "pnd_conf.h"
7 #include "pnd_container.h"
8 #include "pnd_apps.h"
9 #include "pnd_discovery.h"
10 #include "pnd_locate.h"
11 #include "pnd_pndfiles.h"
12 #include "pnd_pxml.h"
13
14 static void usage ( char *argv[] ) {
15   printf ( "%s\tObtain a description, install guide or other info about a pnd-file\n", argv [ 0 ] );
16   printf ( "\n" );
17   printf ( "%s path-to-pndfile [section] [section-2...]\n", argv [ 0 ] );
18   printf ( "\n" );
19   printf ( "section\tOptional. If not specified, general description is shown. (Section 'description')\n" );
20   printf ( "\tIf present, show the named section of the PXML -- such as to obtain install instructions etc.\n" );
21   printf ( "pndfile\tRequired. Full path to the pnd-file to execute.\n" );
22   return;
23 }
24
25 #define SECTIONMAX 100
26
27 int main ( int argc, char *argv[] ) {
28   char *pndfile = NULL;
29   char *section [ SECTIONMAX ];
30   unsigned char sections = 0;
31   int i;
32
33   for ( i = 1; i < argc; i++ ) {
34
35     if ( ! pndfile ) {
36       pndfile = argv [ i ];
37       continue;
38     }
39
40     section [ sections++ ] = argv [ i ];
41
42     if ( sections == SECTIONMAX ) {
43       break;
44     }
45
46   } // for args
47
48   if ( ! pndfile ) {
49     usage ( argv );
50     exit ( 0 );
51   }
52
53   if ( ! sections ) {
54     section [ sections++ ] = "description";
55   }
56
57   // summary
58   printf ( "Pndfile\t%s\n", pndfile );
59   printf ( "Sections to include:\n" );
60   for ( i = 0; i < sections; i++ ) {
61     printf ( "- %s\n", section [ i ] );
62   } // for
63   printf ( "\n" );
64
65   // pull PXML
66   unsigned int pxmlbuflen = 96 * 1024; // lame, need to calculate it
67   char *pxmlbuf = malloc ( pxmlbuflen );
68   if ( ! pxmlbuf ) {
69     printf ( "ERROR: RAM exhausted!\n" );
70     exit ( 0 );
71   }
72   memset ( pxmlbuf, '\0', pxmlbuflen );
73
74   FILE *f = fopen ( pndfile, "r" );
75   if ( ! f ) {
76     printf ( "ERROR: Couldn't open pndfile %s!\n", pndfile );
77     exit ( 0 );
78   }
79
80   pnd_pxml_handle h = NULL;
81   pnd_pxml_handle *apps = NULL;
82   if ( pnd_pnd_seek_pxml ( f ) ) {
83     if ( pnd_pnd_accrue_pxml ( f, pxmlbuf, pxmlbuflen ) ) {
84       apps = pnd_pxml_fetch_buffer ( "pnd_run", pxmlbuf );
85     }
86   }
87
88   fclose ( f );
89
90   if ( ! apps ) {
91     printf ( "ERROR: Couldn't pull PXML.xml from the pndfile.\n" );
92     exit ( 0 );
93   }
94
95   // iterate across apps
96   while ( *apps ) {
97     h = *apps;
98
99     // display sections
100     for ( i = 0; i < sections; i++ ) {
101       char *t;
102
103       if ( strcasecmp ( section [ i ], "description" ) == 0 ) {
104
105         printf ( "Section: %s\n", section [ i ] );
106
107         if ( ( t = pnd_pxml_get_description_en ( h ) ) ) {
108           printf ( "%s\n", t );
109         } else {
110           printf ( "Not supplied by PXML.xml in the pnd-file\n" );
111         }
112
113       }
114
115     } // for
116
117     // next
118     apps++;
119   } // while
120
121   return ( 0 );
122 } // main