Adding argument support to PXML; pnd_run.sh already had it, but PXML did not.
[pandora-libraries.git] / lib / pnd_discovery.c
1
2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #include <unistd.h> /* for unlink */
5
6 #define __USE_GNU /* for strcasestr */
7 #include <string.h> /* for making ftw.h happy */
8
9 #define _XOPEN_SOURCE 500
10 #define __USE_XOPEN_EXTENDED
11 #include <ftw.h> /* for nftw, tree walker */
12
13 #include "pnd_container.h"
14 #include "pnd_pxml.h"
15 #include "pnd_discovery.h"
16 #include "pnd_pathiter.h"
17 #include "pnd_apps.h"
18 #include "pnd_pndfiles.h"
19 #include "pnd_logger.h"
20
21 // need these 'globals' due to the way nftw and ftw work :/
22 static pnd_box_handle disco_box;
23 static char *disco_overrides = NULL;
24
25 void pnd_disco_destroy ( pnd_disco_t *p ) {
26
27   if ( p -> title_en ) {       free ( p -> title_en );    }
28   if ( p -> unique_id ) {      free ( p -> unique_id );   }
29   if ( p -> icon )     {       free ( p -> icon );        }
30   if ( p -> exec )     {       free ( p -> exec );        }
31   if ( p -> execargs ) {       free ( p -> execargs );    }
32   if ( p -> clockspeed ) {     free ( p -> clockspeed );  }
33   if ( p -> startdir ) {       free ( p -> startdir );    }
34   if ( p -> option_no_x11 ) {  free ( p -> option_no_x11 );  }
35   if ( p -> main_category ) {  free ( p -> main_category );  }
36   if ( p -> main_category1 ) { free ( p -> main_category1 ); }
37   if ( p -> main_category2 ) { free ( p -> main_category2 ); }
38   if ( p -> alt_category ) {   free ( p -> alt_category );   }
39   if ( p -> alt_category1 ) {  free ( p -> alt_category1 );  }
40   if ( p -> alt_category2 ) {  free ( p -> alt_category2 );  }
41   if ( p -> mkdir_sp )      {  free ( p -> mkdir_sp );       }
42
43   return;
44 }
45
46 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
47                                 int typeflag, struct FTW *ftwbuf )
48 {
49   unsigned char valid = pnd_object_type_unknown;
50   pnd_pxml_handle pxmlh = 0;
51   pnd_pxml_handle *pxmlapps = NULL;
52   pnd_pxml_handle *pxmlappiter;
53   unsigned int pxml_close_pos = 0;
54   unsigned char logit = pnd_log_do_buried_logging();
55
56   if ( logit ) {
57     pnd_log ( PND_LOG_DEFAULT, "disco callback encountered '%s'\n", fpath );
58   }
59
60   // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
61   if ( typeflag & FTW_D ) {
62     if ( logit ) {
63       pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
64     }
65     return ( 0 ); // skip directories and other non-regular files
66   }
67
68   // PND/PNZ file and others may be valid as well .. but lets leave that for now
69   //   printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
70   if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
71     valid = pnd_object_type_directory;
72   } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
73     valid = pnd_object_type_pnd;
74   }
75
76   // if not a file of interest, just keep looking until we run out
77   if ( ! valid ) {
78     if ( logit ) {
79       pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
80     }
81     return ( 0 );
82   }
83
84   // potentially a valid application
85   if ( valid == pnd_object_type_directory ) {
86     // Plaintext PXML file
87     //printf ( "PXML: disco callback encountered '%s'\n", fpath );
88
89     // pick up the PXML if we can
90     pxmlapps = pnd_pxml_fetch ( (char*) fpath );
91
92   } else if ( valid == pnd_object_type_pnd ) {
93     // PND ... ??
94     FILE *f;
95     char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
96
97     //printf ( "PND: disco callback encountered '%s'\n", fpath );
98
99     // is this a valid .pnd file? The filename is a candidate already, but verify..
100     // .. presence of PXML appended, or at least contained within?
101     // .. presence of an icon appended after PXML?
102
103     // open it up..
104     f = fopen ( fpath, "r" );
105
106     // try to locate the PXML portion
107     if ( ! pnd_pnd_seek_pxml ( f ) ) {
108       fclose ( f );
109       return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
110     }
111
112     // accrue it into a buffer
113     if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
114       fclose ( f );
115       return ( 0 );
116     }
117
118     //printf ( "buffer is %s\n", pxmlbuf );
119     //fflush ( stdout );
120
121 #if 1 // icon
122     // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
123     {
124       unsigned int pos = ftell ( f );
125       char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
126       pngbuffer [ 0 ] = 137;      pngbuffer [ 1 ] = 80;      pngbuffer [ 2 ] = 78;      pngbuffer [ 3 ] = 71;
127       pngbuffer [ 4 ] = 13;       pngbuffer [ 5 ] = 10;       pngbuffer [ 6 ] = 26;      pngbuffer [ 7 ] = 10;
128
129       unsigned char padtests = 10;
130       unsigned int padstart = ftell ( f );
131       while ( padtests ) {
132
133         if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
134           if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
135             pxml_close_pos = pos;
136             break;
137           } // if
138           fseek ( f, -7, SEEK_CUR ); // seek back 7 (so we're 1 further than we started, since PNG header is 8b)
139         } // if read
140
141         padtests --;
142       } // while
143
144       if ( ! padtests ) {
145         // no icon found, so back to where we started looking
146         fseek ( f, padstart, SEEK_SET );
147       }
148
149     } // icon
150 #endif
151
152     // by now, we have <PXML> .. </PXML>, try to parse..
153     pxmlapps = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
154
155     // done with file
156     fclose ( f );
157
158   }
159
160   // pxmlh is useful?
161   if ( ! pxmlapps ) {
162     return ( 0 ); // continue tree walk
163   }
164
165   // iterate across apps in the PXML
166   pxmlappiter = pxmlapps;
167   while ( 1 ) {
168     pxmlh = *pxmlappiter;
169     pxmlappiter++;
170
171     if ( ! pxmlh ) {
172       break; // all done
173     }
174
175     // look for any overrides, if requested
176     pnd_pxml_merge_override ( pxmlh, disco_overrides );
177
178     // check for validity and add to resultset if it looks executable
179     if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
180       pnd_disco_t *p;
181       char *fixpxml;
182       char *z;
183
184       pnd_log ( PND_LOG_DEFAULT, "Setting up discovered app %u\n", ((pnd_pxml_t*) pxmlh) -> subapp_number );
185
186       p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
187
188       // base paths
189       p -> object_path = strdup ( fpath );
190
191       if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
192         *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
193       } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
194         *(fixpxml+1) = '\0'; // for pnd, lop off to last /
195       }
196
197       if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
198         p -> object_filename = strdup ( fixpxml + 1 );
199       }
200
201       // subapp-number
202       p -> subapp_number = ((pnd_pxml_t*) pxmlh) -> subapp_number;
203
204       // png icon path
205       p -> pnd_icon_pos = pxml_close_pos;
206
207       // type
208       p -> object_type = valid;
209
210       // PXML fields
211       if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
212         p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
213       }
214       if ( pnd_pxml_get_description_en ( pxmlh ) ) {
215         p -> desc_en = strdup ( pnd_pxml_get_description_en ( pxmlh ) );
216       }
217       if ( pnd_pxml_get_icon ( pxmlh ) ) {
218         p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
219       }
220       if ( pnd_pxml_get_exec ( pxmlh ) ) {
221         p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
222       }
223       if ( pnd_pxml_get_execargs ( pxmlh ) ) {
224         p -> execargs = strdup ( pnd_pxml_get_execargs ( pxmlh ) );
225       }
226       if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
227         p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
228       }
229       if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
230         p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
231       }
232       if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
233         p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) ); 
234       }
235       if ( pnd_pxml_get_startdir ( pxmlh ) ) {
236         p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) ); 
237       }
238       // category kruft
239       if ( pnd_pxml_get_main_category ( pxmlh ) ) {
240         p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
241       }
242       if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
243         p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
244       }
245       if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
246         p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
247       }
248       if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
249         p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
250       }
251       if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
252         p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
253       }
254       if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
255         p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
256       }
257       // preview pics
258       if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
259         p -> preview_pic1 = strdup ( z );
260       }
261       if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
262         p -> preview_pic2 = strdup ( z );
263       }
264       // mkdirs
265       if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
266         p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
267       }
268
269     } else {
270       //printf ( "Invalid PXML; skipping.\n" );
271     }
272
273     // ditch pxml
274     pnd_pxml_delete ( pxmlh );
275
276   } // while pxmlh is good
277
278   // free up the applist
279   free ( pxmlapps );
280
281   return ( 0 ); // continue the tree walk
282 }
283
284 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
285
286   //printf ( "Searchpath to discover: '%s'\n", searchpath );
287
288   // alloc a container for the result set
289   disco_box = pnd_box_new ( "discovery" );
290   disco_overrides = overridespath;
291
292   /* iterate across the paths within the searchpath, attempting to locate applications
293    */
294
295   SEARCHPATH_PRE
296   {
297
298     // invoke the dir walking function; thankfully Linux includes a pretty good one
299     nftw ( buffer,               // path to descend
300            pnd_disco_callback,   // callback to do processing
301            10,                   // no more than X open fd's at once
302            FTW_PHYS );           // do not follow symlinks
303
304   }
305   SEARCHPATH_POST
306
307   // return whatever we found, or NULL if nada
308   if ( ! pnd_box_get_head ( disco_box ) ) {
309     pnd_box_delete ( disco_box );
310     disco_box = NULL;
311   }
312
313   return ( disco_box );
314 }