2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #include <unistd.h> /* for unlink */
5 #include <limits.h> /* for PATH_MAX */
9 #define __USE_GNU /* for strcasestr */
10 #include <string.h> /* for making ftw.h happy */
12 #define _XOPEN_SOURCE 500
13 #define __USE_XOPEN_EXTENDED
14 #include <ftw.h> /* for nftw, tree walker */
16 #include "pnd_container.h"
18 #include "pnd_discovery.h"
19 #include "pnd_pathiter.h"
21 #include "pnd_pndfiles.h"
22 #include "pnd_logger.h"
25 // need these 'globals' due to the way nftw and ftw work :/
26 static pnd_box_handle disco_box;
27 static char *disco_overrides = NULL;
29 void pnd_disco_destroy ( pnd_disco_t *p ) {
31 if ( p -> title_en ) { free ( p -> title_en ); }
32 if ( p -> unique_id ) { free ( p -> unique_id ); }
33 if ( p -> icon ) { free ( p -> icon ); }
34 if ( p -> exec ) { free ( p -> exec ); }
35 if ( p -> execargs ) { free ( p -> execargs ); }
36 if ( p -> clockspeed ) { free ( p -> clockspeed ); }
37 if ( p -> startdir ) { free ( p -> startdir ); }
38 if ( p -> option_no_x11 ) { free ( p -> option_no_x11 ); }
39 if ( p -> main_category ) { free ( p -> main_category ); }
40 if ( p -> main_category1 ) { free ( p -> main_category1 ); }
41 if ( p -> main_category2 ) { free ( p -> main_category2 ); }
42 if ( p -> alt_category ) { free ( p -> alt_category ); }
43 if ( p -> alt_category1 ) { free ( p -> alt_category1 ); }
44 if ( p -> alt_category2 ) { free ( p -> alt_category2 ); }
45 if ( p -> mkdir_sp ) { free ( p -> mkdir_sp ); }
46 if ( p -> info_name ) { free ( p -> info_name ); }
47 if ( p -> info_type ) { free ( p -> info_type ); }
48 if ( p -> info_filename ) { free ( p -> info_filename ); }
53 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
54 int typeflag, struct FTW *ftwbuf )
56 unsigned char valid = pnd_object_type_unknown;
57 pnd_pxml_handle pxmlh = 0;
58 pnd_pxml_handle *pxmlapps = NULL;
59 pnd_pxml_handle *pxmlappiter;
60 unsigned int pxml_close_pos = 0;
61 unsigned char logit = pnd_log_do_buried_logging();
64 pnd_log ( PND_LOG_DEFAULT, "disco callback encountered '%s'\n", fpath );
67 // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
68 if ( typeflag & FTW_D ) {
70 pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
72 return ( 0 ); // skip directories and other non-regular files
75 // PND/PNZ file and others may be valid as well .. but lets leave that for now
76 // printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
77 if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
78 valid = pnd_object_type_directory;
79 } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
80 valid = pnd_object_type_pnd;
83 // if not a file of interest, just keep looking until we run out
86 pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
91 // potentially a valid application
92 if ( valid == pnd_object_type_directory ) {
93 // Plaintext PXML file
94 //printf ( "PXML: disco callback encountered '%s'\n", fpath );
96 // pick up the PXML if we can
97 pxmlapps = pnd_pxml_fetch ( (char*) fpath );
99 } else if ( valid == pnd_object_type_pnd ) {
102 char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
104 //printf ( "PND: disco callback encountered '%s'\n", fpath );
106 // is this a valid .pnd file? The filename is a candidate already, but verify..
107 // .. presence of PXML appended, or at least contained within?
108 // .. presence of an icon appended after PXML?
111 f = fopen ( fpath, "r" );
113 // try to locate the PXML portion
114 if ( ! pnd_pnd_seek_pxml ( f ) ) {
116 return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
119 // accrue it into a buffer
120 if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
125 //printf ( "buffer is %s\n", pxmlbuf );
129 // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
131 unsigned int pos = ftell ( f );
132 char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
133 pngbuffer [ 0 ] = 137; pngbuffer [ 1 ] = 80; pngbuffer [ 2 ] = 78; pngbuffer [ 3 ] = 71;
134 pngbuffer [ 4 ] = 13; pngbuffer [ 5 ] = 10; pngbuffer [ 6 ] = 26; pngbuffer [ 7 ] = 10;
136 unsigned char padtests = 10;
137 unsigned int padstart = ftell ( f );
140 if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
141 if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
142 pxml_close_pos = pos;
145 fseek ( f, -7, SEEK_CUR ); // seek back 7 (so we're 1 further than we started, since PNG header is 8b)
152 // no icon found, so back to where we started looking
153 fseek ( f, padstart, SEEK_SET );
159 // by now, we have <PXML> .. </PXML>, try to parse..
160 pxmlapps = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
169 return ( 0 ); // continue tree walk
173 char ovrfile [ PATH_MAX ];
174 pnd_box_handle ovrh = 0; // 0 didn't try, -1 tried and failed, >0 tried and got
176 // iterate across apps in the PXML
177 pxmlappiter = pxmlapps;
179 pxmlh = *pxmlappiter;
186 // check for validity and add to resultset if it looks executable
187 if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
192 //pnd_log ( PND_LOG_DEFAULT, "Setting up discovered app %u\n", ((pnd_pxml_t*) pxmlh) -> subapp_number );
194 p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
197 p -> object_path = strdup ( fpath );
199 if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
200 *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
201 } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
202 *(fixpxml+1) = '\0'; // for pnd, lop off to last /
205 if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
206 p -> object_filename = strdup ( fixpxml + 1 );
210 p -> subapp_number = ((pnd_pxml_t*) pxmlh) -> subapp_number;
213 p -> pnd_icon_pos = pxml_close_pos;
216 p -> object_type = valid;
219 if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
220 p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
222 if ( pnd_pxml_get_description_en ( pxmlh ) ) {
223 p -> desc_en = strdup ( pnd_pxml_get_description_en ( pxmlh ) );
225 if ( pnd_pxml_get_icon ( pxmlh ) ) {
226 p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
228 if ( pnd_pxml_get_exec ( pxmlh ) ) {
229 p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
231 if ( pnd_pxml_get_execargs ( pxmlh ) ) {
232 p -> execargs = strdup ( pnd_pxml_get_execargs ( pxmlh ) );
234 if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
235 p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
237 if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
238 p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
240 if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
241 p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) );
243 if ( pnd_pxml_get_startdir ( pxmlh ) ) {
244 p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) );
247 if ( pnd_pxml_get_main_category ( pxmlh ) ) {
248 p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
250 if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
251 p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
253 if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
254 p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
256 if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
257 p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
259 if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
260 p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
262 if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
263 p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
266 if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
267 p -> preview_pic1 = strdup ( z );
269 if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
270 p -> preview_pic2 = strdup ( z );
273 if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
274 p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
277 if ( pnd_pxml_get_info_src ( pxmlh ) ) {
278 p -> info_filename = strdup ( pnd_pxml_get_info_src ( pxmlh ) );
280 if ( pnd_pxml_get_info_name ( pxmlh ) ) {
281 p -> info_name = strdup ( pnd_pxml_get_info_name ( pxmlh ) );
283 if ( pnd_pxml_get_info_type ( pxmlh ) ) {
284 p -> info_type = strdup ( pnd_pxml_get_info_type ( pxmlh ) );
287 // look for any PXML overrides, if requested
288 if ( disco_overrides ) {
289 pnd_pxml_merge_override ( pxmlh, disco_overrides );
292 // handle ovr overrides
293 // try to load a same-path-as-pnd override file
295 sprintf ( ovrfile, "%s/%s", p -> object_path, p -> object_filename );
296 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
298 strcpy ( fixpxml, PXML_SAMEPATH_OVERRIDE_FILEEXT );
300 if ( stat ( ovrfile, &statbuf ) == 0 ) {
301 ovrh = pnd_conf_fetch_by_path ( ovrfile );
304 // couldn't pull conf out of file, so don't try again
309 ovrh = (void*)(-1); // not found, don't try again
315 if ( ovrh != 0 && ovrh != (void*)(-1) ) {
316 // pull in appropriate values
320 // set the flag regardless, so its for all subapps
321 p -> object_flags |= PND_DISCO_FLAG_OVR;
324 snprintf ( key, 100, "Application-%u.title", p -> subapp_number );
325 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
326 if ( p -> title_en ) {
327 free ( p -> title_en );
329 p -> title_en = strdup ( v );
333 snprintf ( key, 100, "Application-%u.clockspeed", p -> subapp_number );
334 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
335 if ( p -> clockspeed ) {
336 free ( p -> clockspeed );
338 p -> clockspeed = strdup ( v );
342 snprintf ( key, 100, "Application-%u.maincategory", p -> subapp_number );
343 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
344 if ( p -> main_category ) {
345 free ( p -> main_category );
347 p -> main_category = strdup ( v );
349 snprintf ( key, 100, "Application-%u.maincategorysub1", p -> subapp_number );
350 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
351 if ( p -> main_category1 ) {
352 free ( p -> main_category1 );
354 p -> main_category1 = strdup ( v );
357 } // got ovr conf loaded?
360 //printf ( "Invalid PXML; skipping.\n" );
364 pnd_pxml_delete ( pxmlh );
366 } // while pxmlh is good
369 if ( ovrh != 0 && ovrh != (void*)(-1) ) {
370 pnd_box_delete ( ovrh );
373 // free up the applist
376 return ( 0 ); // continue the tree walk
379 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
381 //printf ( "Searchpath to discover: '%s'\n", searchpath );
383 // alloc a container for the result set
384 disco_box = pnd_box_new ( "discovery" );
385 disco_overrides = overridespath;
387 /* iterate across the paths within the searchpath, attempting to locate applications
393 // invoke the dir walking function; thankfully Linux includes a pretty good one
394 nftw ( buffer, // path to descend
395 pnd_disco_callback, // callback to do processing
396 10, // no more than X open fd's at once
397 FTW_PHYS ); // do not follow symlinks
402 // return whatever we found, or NULL if nada
403 if ( ! pnd_box_get_head ( disco_box ) ) {
404 pnd_box_delete ( disco_box );
408 return ( disco_box );