@@ -98,7 +98,7 @@ include an option for suppressing superfluous commands.
9898#include " interp_internal.hh" // interpreter private definitions
9999#include " interp_queue.hh"
100100#include " rs274ngc_interp.hh"
101-
101+ # include < wordexp.h >
102102#include " units.h"
103103
104104#include < unordered_set>
@@ -2651,55 +2651,96 @@ int Interp::on_abort(int reason, const char *message)
26512651
26522652// spun out from interp_o_word so we can use it to test ngc file accessibility during
26532653// config file parsing (REMAP... ngc=<basename>)
2654+ // Will expand ~ to user's home path
2655+ // searchs this sequence until it finds a match:
2656+ // 1) checks if path is already the full path
2657+ // 2) tries adding the INI defined program prefix to path
2658+ // 3) tries adding the INI defined subroutine prefix to path
2659+ // 4) tries adding the INI defined whizard prefix to path
26542660FILE *Interp::find_ngc_file (setup_pointer settings,const char *basename, char *foundhere )
26552661{
26562662 FILE *newFP = NULL ;
26572663 char tmpFileName[PATH_MAX+1 ];
26582664 char newFileName[PATH_MAX+1 ];
26592665 char foundPlace[PATH_MAX+1 ];
26602666 int dct;
2667+ wordexp_t exp_result;
2668+
2669+ // #1 check if this is the full path already
26612670
2662- // look for a new file
2663- snprintf (tmpFileName, sizeof (tmpFileName), " %s.ngc" , basename);
2671+ // expand user path
2672+ wordexp (basename, &exp_result, 0 );
2673+ // add .ngc to expanded path
2674+ snprintf (tmpFileName, sizeof (tmpFileName), " %s.ngc" , exp_result.we_wordv [0 ]);
26642675
2665- // find subroutine by search: program_prefix, subroutines, wizard_root
2666- // use first file found
2676+ // copy to newFileName - in case this is the one...
2677+ size_t chk = snprintf (newFileName, sizeof (newFileName), " %s " , tmpFileName);
26672678
2668- // first look in the program_prefix place
2669- size_t chk = snprintf (newFileName, sizeof (newFileName), " %s/%s" , settings->program_prefix , tmpFileName);
2679+ // found a file we can open?
26702680 if (chk < sizeof (newFileName)){
26712681 newFP = fopen (newFileName, " r" );
26722682 }
26732683
2674- // then look in the subroutines place
2684+ // #2 then look in the program_prefix place
26752685 if (!newFP) {
2676- for (dct = 0 ; dct < MAX_SUB_DIRS; dct++) {
2677- if (!settings->subroutines [dct])
2678- continue ;
2679- chk = snprintf (newFileName, sizeof (newFileName), " %s/%s" , settings->subroutines [dct], tmpFileName);
2680- if (chk < sizeof (newFileName)){
2686+
2687+ // expand '~' into user path
2688+ wordexp (settings->program_prefix , &exp_result, 0 );
2689+ chk = snprintf (newFileName, sizeof (newFileName), " %s/%s" , exp_result.we_wordv [0 ], tmpFileName);
2690+
2691+ // found a file we can open?
2692+ if (chk < sizeof (newFileName)){
26812693 newFP = fopen (newFileName, " r" );
2682- if (newFP) {
2683- // logOword("fopen: |%s|", newFileName);
2684- break ; // use first occurrence in dir search
2694+ }
2695+ }
2696+
2697+ // #3 then look in the list of subroutines prefixes
2698+ if (!newFP) {
2699+ for (dct = 0 ; dct < MAX_SUB_DIRS; dct++) {
2700+ if (!settings->subroutines [dct])
2701+ continue ;
2702+
2703+ // expand '~' into user path
2704+ wordexp (settings->subroutines [dct], &exp_result, 0 );
2705+ chk = snprintf (newFileName, sizeof (newFileName), " %s/%s" , exp_result.we_wordv [0 ], tmpFileName);
2706+
2707+ // found a file we can open?
2708+ if (chk < sizeof (newFileName)){
2709+ newFP = fopen (newFileName, " r" );
2710+ if (newFP) {
2711+ // logOword("fopen: |%s|", newFileName);
2712+ break ; // use first occurrence in dir search
2713+ }
26852714 }
2686- }
2687- }
2715+ }
26882716 }
2689- // if not found, search the wizard tree
2717+
2718+ // #4 if still not found, search the wizard tree
2719+ // Wiz directory already expands the '~' to user path
26902720 if (!newFP) {
2691- int ret;
2692- ret = findFile (settings->wizard_root , tmpFileName, foundPlace);
2693-
2694- if (INTERP_OK == ret) {
2695- // create the long name
2696- chk = snprintf (newFileName, sizeof (newFileName), " %s/%s" ,
2697- foundPlace, tmpFileName);
2698- if (chk < sizeof (newFileName)) newFP = fopen (newFileName, " r" );
2699- }
2721+ int ret;
2722+
2723+ // walks the directory hierarchy ?
2724+ ret = findFile (settings->wizard_root , tmpFileName, foundPlace);
2725+
2726+ if (INTERP_OK == ret) {
2727+ // create the long name
2728+ chk = snprintf (newFileName, sizeof (newFileName), " %s/%s" ,
2729+ foundPlace, tmpFileName);
2730+
2731+ // found a file we can open?
2732+ if (chk < sizeof (newFileName)){
2733+ newFP = fopen (newFileName, " r" );
2734+ }
2735+ }
27002736 }
2737+
2738+ // pass what we found
27012739 if (foundhere && (newFP != NULL ))
2702- strcpy (foundhere, newFileName);
2740+ strcpy (foundhere, newFileName);
2741+
2742+ // Not sure this is needed but the internet told me
2743+ wordfree (&exp_result);
27032744 return newFP;
27042745}
27052746
0 commit comments