pndnotifyd: fix some crashes
[pandora-libraries.git] / apps / pnd_run.c
1
2 #include <stdio.h> /* for printf, NULL */
3 #include <stdlib.h> /* for free */
4 #include <string.h> /* for strdup */
5 #include <ctype.h> /* for isdigit */
6
7 #include "pnd_conf.h"
8 #include "pnd_container.h"
9 #include "pnd_apps.h"
10 #include "pnd_discovery.h"
11 #include "pnd_locate.h"
12 #include "pnd_pndfiles.h"
13 #include "pnd_pxml.h"
14
15 static void usage ( char *argv[] ) {
16   printf ( "%s [-r runscript] [-n] [-X] path-to-pndfile\n", argv [ 0 ] );
17   printf ( "-r\tOptional. If not specified, will attempt to suss from configs.\n" );
18   printf ( "-X\tOptional. If present, run sub-app number 'X'; ex: -0 for first, -1 for second, etc.\n" );
19   printf ( "pndfile\tRequired. Full path to the pnd-file to execute.\n" );
20   return;
21 }
22
23 int main ( int argc, char *argv[] ) {
24   char *pnd_run = NULL;
25   char *pndfile = NULL;
26   unsigned char i;
27   unsigned char subapp = 0;
28
29   for ( i = 1; i < argc; i++ ) {
30
31     if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'r' ) {
32       pnd_run = argv [ i + 1 ];
33       i++;
34       if ( ! pnd_run ) {
35         printf ( "-r specified, but no argument provided.\n" );
36         exit ( 0 );
37       }
38     } else if ( argv [ i ][ 0 ] == '-' && isdigit(argv [ i ][ 1 ]) ) {
39       subapp = atoi (argv[i] + 1);
40     } else {
41
42       if ( argv [ i ][ 0 ] == '-' ) {
43         usage ( argv );
44         exit ( 0 );
45       } else if ( pndfile ) {
46         printf ( "Only one pndfile may be specified.\n" );
47       } else {
48         pndfile = argv [ i ];
49       }
50
51     }
52
53   } // for args
54
55   // if runscript was not specified on cmdline, attempt to pick it up from config
56   // ---> cribbed right out of discotest :/ copypaste ftw!
57   if ( ! pnd_run ) {
58     char *configpath;
59     char *overridespath;
60
61     // attempt to fetch a sensible default searchpath for configs
62     configpath = pnd_conf_query_searchpath();
63
64     // attempt to fetch the apps config. since it finds us the runscript
65     pnd_conf_handle apph;
66
67     apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
68
69     if ( apph ) {
70
71       overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );
72
73       if ( ! overridespath ) {
74         overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
75       }
76
77     } else {
78       // couldn't find a useful app search path so use the default
79       overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
80     }
81
82     // given app-config, try to locate a runscript
83     char *run_searchpath;
84     char *run_script;
85     char *pndrun;
86
87     if ( apph ) {
88       run_searchpath = pnd_conf_get_as_char ( apph, PND_PNDRUN_SEARCHPATH_KEY );
89       run_script = pnd_conf_get_as_char ( apph, PND_PNDRUN_KEY );
90       pndrun = NULL;
91
92       if ( ! run_searchpath ) {
93         run_searchpath = PND_APPS_SEARCHPATH;
94         run_script = PND_PNDRUN_FILENAME;
95       }
96
97     } else {
98       run_searchpath = NULL;
99       run_script = NULL;
100       pndrun = PND_PNDRUN_DEFAULT;
101     }
102
103     if ( ! pndrun ) {
104       pndrun = pnd_locate_filename ( run_searchpath, run_script );
105     }
106
107     // hand back to main proggy
108     pnd_run = pndrun; // lame, fix this
109
110   } // try to locate runscript
111
112   if ( ! pnd_run ) {
113     printf ( "Runscript could not be determined. Fail.\n" );
114     exit ( 0 );
115   }
116
117   if ( ! pndfile ) {
118     usage ( argv );
119     exit ( 0 );
120   }
121
122   // summary
123   printf ( "Runscript\t%s\n", pnd_run );
124   printf ( "Pndfile\t\t%s\n", pndfile );
125   printf ( "Subapp Number\t%u\n", subapp );
126
127   // figure out path and filename
128   char *path, *filename;
129
130   if ( strchr ( pndfile, '/' ) ) {
131     char *foo = rindex ( pndfile, '/' );
132     *foo = '\0';
133     path = pndfile;
134     filename = foo + 1;
135   } else {
136     path = "./";
137     filename = pndfile;
138   }
139
140   // run it
141   pnd_box_handle h = pnd_disco_file ( path, filename );
142
143   if ( h ) {
144     pnd_disco_t *d = pnd_box_get_head ( h );
145     while ( subapp && d ) {
146       if ( d -> title_en ) {
147         printf ( "Skipping: '%s'\n", d -> title_en );
148       }
149       d = pnd_box_get_next ( d );
150       subapp--;
151     }
152     if ( ! d ) {
153       printf ( "No more applications in pnd-file.\n" );
154       exit ( 0 );
155     }
156     if ( d -> title_en ) {
157       printf ( "Invoking: '%s'\n", d -> title_en );
158     }
159     printf ( "--\n" );
160     pnd_apps_exec_disco ( pnd_run, d, PND_EXEC_OPTION_BLOCK, NULL );
161   }
162
163   return ( 0 );
164 } // main