Make 'arguments' to file association properly optional
[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 #include <limits.h> /* for PATH_MAX */
6 #include <sys/types.h>
7 #include <sys/stat.h>
8
9 #define __USE_GNU /* for strcasestr */
10 #include <string.h> /* for making ftw.h happy */
11
12 #define _XOPEN_SOURCE 500
13 #define __USE_XOPEN_EXTENDED
14 #define _GNU_SOURCE
15 #include <ftw.h> /* for nftw, tree walker */
16
17 #include "pnd_container.h"
18 #include "pnd_pxml.h"
19 #include "pnd_discovery.h"
20 #include "pnd_pathiter.h"
21 #include "pnd_apps.h"
22 #include "pnd_pndfiles.h"
23 #include "pnd_logger.h"
24 #include "pnd_conf.h"
25
26 // need these 'globals' due to the way nftw and ftw work :/
27 static pnd_box_handle disco_box;
28 static char *disco_overrides = NULL;
29
30 void pnd_disco_destroy ( pnd_disco_t *p ) {
31   if ( p -> package_id ) {     free ( p -> package_id);   }
32   if ( p -> title_en ) {       free ( p -> title_en );    }
33   if ( p -> desc_en ) {        free ( p -> desc_en );    }
34   if ( p -> unique_id ) {      free ( p -> unique_id );   }
35   if ( p -> appdata_dirname ) { free ( p -> appdata_dirname );   }
36   if ( p -> icon )     {       free ( p -> icon );        }
37   if ( p -> exec )     {       free ( p -> exec );        }
38   if ( p -> execargs ) {       free ( p -> execargs );    }
39   if ( p -> clockspeed ) {     free ( p -> clockspeed );  }
40   if ( p -> startdir ) {       free ( p -> startdir );    }
41   if ( p -> option_no_x11 ) {  free ( p -> option_no_x11 );  }
42   if ( p -> main_category ) {  free ( p -> main_category );  }
43   if ( p -> main_category1 ) { free ( p -> main_category1 ); }
44   if ( p -> main_category2 ) { free ( p -> main_category2 ); }
45   if ( p -> alt_category ) {   free ( p -> alt_category );   }
46   if ( p -> alt_category1 ) {  free ( p -> alt_category1 );  }
47   if ( p -> alt_category2 ) {  free ( p -> alt_category2 );  }
48   if ( p -> object_filename ) { free ( p -> object_filename ); }
49   if ( p -> object_path )     { free ( p -> object_path ); }
50   if ( p -> mkdir_sp )      {  free ( p -> mkdir_sp );       }
51   if ( p -> info_name )     {  free ( p -> info_name );       }
52   if ( p -> info_type )     {  free ( p -> info_type );       }
53   if ( p -> info_filename ) {  free ( p -> info_filename );       }
54   if ( p -> preview_pic1 )  {  free ( p -> preview_pic1 );     }
55   if ( p -> preview_pic2 )  {  free ( p -> preview_pic2 );     }
56   if ( p -> version_major ) {  free ( p -> version_major );    }
57   if ( p -> version_minor ) {  free ( p -> version_minor );    }
58   if ( p -> version_release ) {free ( p -> version_release );  }
59   if ( p -> version_build ) {  free ( p -> version_build );    }
60   if ( p -> package_version_major ) { free ( p -> package_version_major ); }
61   if ( p -> package_version_minor ) { free ( p -> package_version_minor ); }
62   if ( p -> package_version_release ) { free ( p -> package_version_release ); }
63   if ( p -> package_version_build ) { free ( p -> package_version_build ); }
64   if ( p -> associationitem1_name ) { free ( p -> associationitem1_name ); }
65   if ( p -> associationitem1_filetype ) { free ( p -> associationitem1_filetype ); }
66   if ( p -> associationitem1_command ) { free ( p -> associationitem1_command ); }
67   if ( p -> associationitem1_args ) { free ( p -> associationitem1_args ); }
68   if ( p -> associationitem2_name ) { free ( p -> associationitem2_name ); }
69   if ( p -> associationitem2_filetype ) { free ( p -> associationitem2_filetype ); }
70   if ( p -> associationitem2_command ) { free ( p -> associationitem2_command ); }
71   if ( p -> associationitem2_args ) { free ( p -> associationitem2_args ); }
72   if ( p -> associationitem3_name ) { free ( p -> associationitem3_name ); }
73   if ( p -> associationitem3_filetype ) { free ( p -> associationitem3_filetype ); }
74   if ( p -> associationitem3_command ) { free ( p -> associationitem3_command ); }
75   if ( p -> associationitem3_args ) { free ( p -> associationitem3_args ); }
76
77   return;
78 }
79
80 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
81                                 int typeflag, struct FTW *ftwbuf )
82 {
83   unsigned char valid = pnd_object_type_unknown;
84   pnd_pxml_handle pxmlh = 0;
85   pnd_pxml_handle *pxmlapps = NULL;
86   pnd_pxml_handle *pxmlappiter;
87   unsigned int pxml_close_pos = 0;
88   unsigned char logit = pnd_log_do_buried_logging();
89
90   if ( logit ) {
91     pnd_log ( PND_LOG_DEFAULT, "disco callback encountered '%s'\n", fpath );
92   }
93
94   // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
95   if ( typeflag & FTW_D ) {
96     if ( logit ) {
97       pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
98     }
99     if ( ftwbuf -> level >= pathiter_depthlimit ) {
100       return ( FTW_SKIP_SUBTREE );
101     }
102     return ( FTW_CONTINUE ); // skip directories and other non-regular files
103   }
104
105   // PND/PNZ file and others may be valid as well .. but lets leave that for now
106   //   printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
107   if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
108     valid = pnd_object_type_directory;
109   } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
110     valid = pnd_object_type_pnd;
111   }
112
113   // if not a file of interest, just keep looking until we run out
114   if ( ! valid ) {
115     if ( logit ) {
116       pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
117     }
118     return ( FTW_CONTINUE );
119   }
120
121   // potentially a valid application
122   if ( valid == pnd_object_type_directory ) {
123     // Plaintext PXML file
124     //printf ( "PXML: disco callback encountered '%s'\n", fpath );
125
126     // pick up the PXML if we can
127     pxmlapps = pnd_pxml_fetch ( (char*) fpath );
128
129   } else if ( valid == pnd_object_type_pnd ) {
130     // PND ... ??
131     FILE *f;
132     char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
133
134     //printf ( "PND: disco callback encountered '%s'\n", fpath );
135
136     // is this a valid .pnd file? The filename is a candidate already, but verify..
137     // .. presence of PXML appended, or at least contained within?
138     // .. presence of an icon appended after PXML?
139
140     // open it up..
141     f = fopen ( fpath, "r" );
142
143     // try to locate the PXML portion
144     if ( ! pnd_pnd_seek_pxml ( f ) ) {
145       fclose ( f );
146       return ( FTW_CONTINUE ); // pnd or not, but not to spec. Pwn'd the pnd?
147     }
148
149     // accrue it into a buffer
150     if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
151       fclose ( f );
152       return ( FTW_CONTINUE );
153     }
154
155     //printf ( "buffer is %s\n", pxmlbuf );
156     //fflush ( stdout );
157
158 #if 1 // icon
159     // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
160     {
161       char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
162       pngbuffer [ 0 ] = 137;      pngbuffer [ 1 ] = 80;      pngbuffer [ 2 ] = 78;      pngbuffer [ 3 ] = 71;
163       pngbuffer [ 4 ] = 13;       pngbuffer [ 5 ] = 10;       pngbuffer [ 6 ] = 26;      pngbuffer [ 7 ] = 10;
164
165       unsigned char padtests = 20;
166       unsigned int padstart = ftell ( f );
167
168       // seek back 10 (should be back into the /PXML> part) to catch any appending-icon-no-line-endings funny business
169       fseek ( f, -10, SEEK_CUR );
170
171       while ( padtests ) {
172
173         if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
174           if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
175             pxml_close_pos = ftell ( f ) - 8;
176             break;
177           } // if
178           fseek ( f, -7, SEEK_CUR ); // seek back 7 (so we're 1 further than we started, since PNG header is 8b)
179         } // if read
180
181         padtests --;
182       } // while
183
184       if ( ! padtests ) {
185         // no icon found, so back to where we started looking
186         fseek ( f, padstart, SEEK_SET );
187       }
188
189     } // icon
190 #endif
191
192     // by now, we have <PXML> .. </PXML>, try to parse..
193     pxmlapps = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
194
195     // done with file
196     fclose ( f );
197
198   }
199
200   // pxmlh is useful?
201   if ( ! pxmlapps ) {
202     return ( FTW_CONTINUE ); // continue tree walk
203   }
204
205   // for ovr-file
206   char ovrfile [ PATH_MAX ];
207   pnd_box_handle ovrh = 0; // 0 didn't try, -1 tried and failed, >0 tried and got
208
209   // iterate across apps in the PXML
210   pxmlappiter = pxmlapps;
211   while ( 1 ) {
212     pxmlh = *pxmlappiter;
213     pxmlappiter++;
214
215     if ( ! pxmlh ) {
216       break; // all done
217     }
218
219     // check for validity and add to resultset if it looks executable
220     if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
221       pnd_disco_t *p;
222       char *fixpxml;
223       char *z;
224
225       //pnd_log ( PND_LOG_DEFAULT, "Setting up discovered app %u\n", ((pnd_pxml_t*) pxmlh) -> subapp_number );
226
227       p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
228
229       // base paths
230       p -> object_path = strdup ( fpath );
231
232       if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
233         *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
234       } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
235         *(fixpxml+1) = '\0'; // for pnd, lop off to last /
236       }
237
238       if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
239         p -> object_filename = strdup ( fixpxml + 1 );
240       }
241
242       // subapp-number
243       p -> subapp_number = ((pnd_pxml_t*) pxmlh) -> subapp_number;
244
245       // png icon path
246       p -> pnd_icon_pos = pxml_close_pos;
247
248       // type
249       p -> object_type = valid;
250
251       // PXML fields
252       if ( pnd_pxml_get_package_id ( pxmlh ) ) {
253         p -> package_id = strdup ( pnd_pxml_get_package_id ( pxmlh ) );
254       }
255       char *name_en = pnd_pxml_get_app_name_en ( pxmlh );
256       if (name_en) {
257         p -> title_en = name_en; /* already strdupped */
258       }
259       char *desc_en = pnd_pxml_get_description_en ( pxmlh );
260       if ( desc_en ) {
261         p -> desc_en = desc_en; /* already strdupped */
262       }
263       if ( pnd_pxml_get_icon ( pxmlh ) ) {
264         p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
265       }
266       if ( pnd_pxml_get_exec ( pxmlh ) ) {
267         p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
268       }
269       if ( pnd_pxml_get_execargs ( pxmlh ) ) {
270         p -> execargs = strdup ( pnd_pxml_get_execargs ( pxmlh ) );
271       }
272       if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
273         p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
274       }
275       if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
276         p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
277       }
278       if ( pnd_pxml_get_appdata_dirname ( pxmlh ) ) {
279         p -> appdata_dirname = strdup ( pnd_pxml_get_appdata_dirname ( pxmlh ) );
280       }
281       if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
282         p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) );
283       }
284       if ( pnd_pxml_get_startdir ( pxmlh ) ) {
285         p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) );
286       }
287       // category kruft
288       if ( pnd_pxml_get_main_category ( pxmlh ) ) {
289         p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
290       }
291       if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
292         p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
293       }
294       if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
295         p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
296       }
297       if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
298         p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
299       }
300       if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
301         p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
302       }
303       if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
304         p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
305       }
306       // preview pics
307       if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
308         p -> preview_pic1 = strdup ( z );
309       }
310       if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
311         p -> preview_pic2 = strdup ( z );
312       }
313       // mkdirs
314       if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
315         p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
316       }
317       // info
318       if ( pnd_pxml_get_info_src ( pxmlh ) ) {
319         p -> info_filename = strdup ( pnd_pxml_get_info_src ( pxmlh ) );
320       }
321       if ( pnd_pxml_get_info_name ( pxmlh ) ) {
322         p -> info_name = strdup ( pnd_pxml_get_info_name ( pxmlh ) );
323       }
324       if ( pnd_pxml_get_info_type ( pxmlh ) ) {
325         p -> info_type = strdup ( pnd_pxml_get_info_type ( pxmlh ) );
326       }
327       if ( pnd_pxml_get_version_major ( pxmlh ) ) {
328    p -> version_major = strdup ( pnd_pxml_get_version_major ( pxmlh ) );
329       }
330       if ( pnd_pxml_get_version_minor ( pxmlh ) ) {
331    p -> version_minor = strdup ( pnd_pxml_get_version_minor ( pxmlh ) );
332       }
333       if ( pnd_pxml_get_version_release ( pxmlh ) ) {
334    p -> version_release = strdup ( pnd_pxml_get_version_release ( pxmlh ) );
335       }
336       if ( pnd_pxml_get_version_build ( pxmlh ) ) {
337    p -> version_build = strdup ( pnd_pxml_get_version_build ( pxmlh ) );
338       }
339       if ( pnd_pxml_get_package_version_major ( pxmlh ) ) {
340         p -> package_version_major = strdup ( pnd_pxml_get_package_version_major ( pxmlh ) );
341       }
342       if ( pnd_pxml_get_package_version_minor ( pxmlh ) ) {
343    p -> package_version_minor = strdup ( pnd_pxml_get_package_version_minor ( pxmlh ) );
344       }
345       if ( pnd_pxml_get_package_version_release ( pxmlh ) ) {
346    p -> package_version_release = strdup ( pnd_pxml_get_package_version_release ( pxmlh ) );
347       }
348       if ( pnd_pxml_get_package_version_build ( pxmlh ) ) {
349    p -> package_version_build = strdup ( pnd_pxml_get_package_version_build ( pxmlh ) );
350       }
351 #if 1
352       // file associations
353       if ( pnd_pxml_get_associationitem1_name ( pxmlh ) ) {
354         p -> associationitem1_name = strdup ( pnd_pxml_get_associationitem1_name ( pxmlh ) );
355         p -> associationitem1_filetype = strdup ( pnd_pxml_get_associationitem1_filetype ( pxmlh ) );
356         p -> associationitem1_command = strdup ( pnd_pxml_get_associationitem1_command ( pxmlh ) );
357         p -> associationitem1_args = strdup ( pnd_pxml_get_associationitem1_args ( pxmlh ) );
358         //pnd_log ( PND_LOG_DEFAULT, "  Disco: Found file association request in PXML (%s)\n", p -> title_en );
359       }
360       if ( pnd_pxml_get_associationitem2_name ( pxmlh ) ) {
361         p -> associationitem2_name = strdup ( pnd_pxml_get_associationitem2_name ( pxmlh ) );
362         p -> associationitem2_filetype = strdup ( pnd_pxml_get_associationitem2_filetype ( pxmlh ) );
363         p -> associationitem2_command = strdup ( pnd_pxml_get_associationitem2_command ( pxmlh ) );
364         p -> associationitem2_args = strdup ( pnd_pxml_get_associationitem2_args ( pxmlh ) );
365       }
366       if ( pnd_pxml_get_associationitem3_name ( pxmlh ) ) {
367         p -> associationitem3_name = strdup ( pnd_pxml_get_associationitem3_name ( pxmlh ) );
368         p -> associationitem3_filetype = strdup ( pnd_pxml_get_associationitem3_filetype ( pxmlh ) );
369         p -> associationitem3_command = strdup ( pnd_pxml_get_associationitem3_command ( pxmlh ) );
370         p -> associationitem3_args = strdup ( pnd_pxml_get_associationitem3_args ( pxmlh ) );
371       }
372 #endif
373
374       // look for any PXML overrides, if requested
375       if ( disco_overrides ) {
376         pnd_pxml_merge_override ( pxmlh, disco_overrides );
377       }
378
379       // handle ovr overrides
380       // try to load a same-path-as-pnd override file
381       if ( ovrh == 0 ) {
382         sprintf ( ovrfile, "%s/%s", p -> object_path, p -> object_filename );
383         fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
384         if ( fixpxml ) {
385           strcpy ( fixpxml, PXML_SAMEPATH_OVERRIDE_FILEEXT );
386           struct stat statbuf;
387           if ( stat ( ovrfile, &statbuf ) == 0 ) {
388             ovrh = pnd_conf_fetch_by_path ( ovrfile );
389
390             if ( ! ovrh ) {
391               // couldn't pull conf out of file, so don't try again
392               ovrh = (void*)(-1);
393             }
394
395           } else {
396             ovrh = (void*)(-1); // not found, don't try again
397           } // stat
398         } // can find .pnd
399       } // tried ovr yet?
400
401       // is ovr file open?
402       if ( ovrh != 0 && ovrh != (void*)(-1) ) {
403         // pull in appropriate values
404         char key [ 100 ];
405         char *v;
406
407         // set the flag regardless, so its for all subapps
408         p -> object_flags |= PND_DISCO_FLAG_OVR;
409
410         // title
411         snprintf ( key, 100, "Application-%u.title", p -> subapp_number );
412         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
413           if ( p -> title_en ) {
414             free ( p -> title_en );
415           }
416           p -> title_en = strdup ( v );
417         }
418
419         // clockspeed
420         snprintf ( key, 100, "Application-%u.clockspeed", p -> subapp_number );
421         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
422           if ( p -> clockspeed ) {
423             free ( p -> clockspeed );
424           }
425           p -> clockspeed = strdup ( v );
426         }
427
428         // appdata dirname
429         snprintf ( key, 100, "Application-%u.appdata", p -> subapp_number );
430         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
431           if ( p -> appdata_dirname ) {
432             free ( p -> appdata_dirname );
433           }
434           p -> appdata_dirname = strdup ( v );
435         }
436
437         // categories
438         snprintf ( key, 100, "Application-%u.maincategory", p -> subapp_number );
439         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
440           if ( p -> main_category ) {
441             free ( p -> main_category );
442           }
443           p -> main_category = strdup ( v );
444         }
445         snprintf ( key, 100, "Application-%u.maincategorysub1", p -> subapp_number );
446         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
447           if ( p -> main_category1 ) {
448             free ( p -> main_category1 );
449             p -> main_category1 = NULL;
450           }
451           if ( strcasecmp ( v, "NoSubcategory" ) != 0 ) {
452             p -> main_category1 = strdup ( v );
453           }
454         }
455
456       } // got ovr conf loaded?
457
458     } else {
459       //printf ( "Invalid PXML; skipping.\n" );
460     }
461
462     // ditch pxml
463     pnd_pxml_delete ( pxmlh );
464
465   } // while pxmlh is good
466
467   // free up ovr
468   if ( ovrh != 0 && ovrh != (void*)(-1) ) {
469     pnd_box_delete ( ovrh );
470   }
471
472   // free up the applist
473   free ( pxmlapps );
474
475   return ( FTW_CONTINUE ); // continue the tree walk
476 }
477
478 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
479
480   //printf ( "Searchpath to discover: '%s'\n", searchpath );
481
482   // alloc a container for the result set
483   disco_box = pnd_box_new ( "discovery" );
484   disco_overrides = overridespath;
485
486   /* iterate across the paths within the searchpath, attempting to locate applications
487    */
488
489   SEARCHPATH_PRE
490   {
491
492     // invoke the dir walking function; thankfully Linux includes a pretty good one
493     nftw ( buffer,               // path to descend
494            pnd_disco_callback,   // callback to do processing
495            10,                   // no more than X open fd's at once
496            FTW_PHYS | FTW_ACTIONRETVAL );   // do not follow symlinks
497
498   }
499   SEARCHPATH_POST
500
501   // return whatever we found, or NULL if nada
502   if ( ! pnd_box_get_head ( disco_box ) ) {
503     pnd_box_delete ( disco_box );
504     disco_box = NULL;
505   }
506
507   return ( disco_box );
508 }
509
510 pnd_box_handle pnd_disco_file ( char *path, char *filename ) {
511   struct stat statbuf;
512
513   // set up container
514   disco_overrides = NULL;
515   disco_box = pnd_box_new ( "discovery" );
516
517   // path
518   char fullpath [ PATH_MAX ];
519   sprintf ( fullpath, "%s/%s", path, filename );
520
521   // fake it
522   if ( stat ( fullpath, &statbuf ) < 0 ) {
523     return ( 0 );
524   }
525
526   struct FTW ftw;
527   ftw.base = strlen ( path );
528   ftw.level = 0;
529
530   pnd_disco_callback ( fullpath, &statbuf, FTW_F, &ftw );
531
532   // return whatever we found, or NULL if nada
533   if ( ! pnd_box_get_head ( disco_box ) ) {
534     pnd_box_delete ( disco_box );
535     disco_box = NULL;
536   }
537
538   return ( disco_box );
539 }