Added pnd_info.c since I'm an idiot
[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   if ( pnd_pnd_seek_pxml ( f ) ) {
82     if ( pnd_pnd_accrue_pxml ( f, pxmlbuf, pxmlbuflen ) ) {
83       h = pnd_pxml_fetch_buffer ( "pnd_run", pxmlbuf );
84     }
85   }
86
87   fclose ( f );
88
89   if ( ! h ) {
90     printf ( "ERROR: Couldn't pull PXML.xml from the pndfile.\n" );
91     exit ( 0 );
92   }
93
94   // display sections
95   for ( i = 0; i < sections; i++ ) {
96     char *t;
97
98     if ( strcasecmp ( section [ i ], "description" ) == 0 ) {
99
100       printf ( "Section: %s\n", section [ i ] );
101
102       if ( ( t = pnd_pxml_get_description_en ( h ) ) ) {
103         printf ( "%s\n", t );
104       } else {
105         printf ( "Not supplied by PXML.xml in the pnd-file\n" );
106       }
107
108     }
109
110   } // for
111
112   return ( 0 );
113 } // main