@@ -2647,10 +2647,7 @@ main(int argc, char *argv[])
26472647 bool size_is_valid = false;
26482648 int ioctl_result ;
26492649
2650- char pspg_conf_path [MAXPATHLEN ];
2651- char pspg_history_path [MAXPATHLEN ];
2652- const char * XDG_CONFIG_HOME ;
2653- const char * XDG_STATE_HOME ;
2650+ FILE * cf = NULL ;
26542651
26552652#if NCURSES_MOUSE_VERSION > 1
26562653
@@ -2743,42 +2740,91 @@ main(int argc, char *argv[])
27432740 PSPG_CONF = getenv ("PSPG_CONF" );
27442741 if (PSPG_CONF )
27452742 {
2746- snprintf (pspg_conf_path , MAXPATHLEN , "%s" , PSPG_CONF );
2743+ tilde (state .pspg_conf_path , PSPG_CONF );
2744+
2745+ errno = 0 ;
2746+ cf = fopen (state .pspg_conf_path , "r" );
2747+ if (cf == NULL )
2748+ fprintf (stderr , "cannot to open from config file \"%s\" specified by PSPG_CONF (%s), ignored\n" ,
2749+ PSPG_CONF ,
2750+ strerror (errno ));
27472751 }
27482752 else
27492753 {
2750- XDG_CONFIG_HOME = getenv ("XDG_CONFIG_HOME" );
2751- if (XDG_CONFIG_HOME )
2752- {
2753- snprintf (pspg_conf_path , MAXPATHLEN , "%s/pspg.conf" , XDG_CONFIG_HOME );
2754- }
2755- else
2754+ tilde (state .pspg_conf_path , "~/.pspgconf" );
2755+
2756+ errno = 0 ;
2757+ cf = fopen (state .pspg_conf_path , "r" );
2758+
2759+ /*
2760+ * when config is not on old path, try to use XDG specification,
2761+ * but only when oldconfig doesn't exists.
2762+ */
2763+ if (cf == NULL && errno == ENOENT )
27562764 {
2757- snprintf (pspg_conf_path , MAXPATHLEN , "%s" , "~/.pspgconf" );
2765+ const char * XDG_CONFIG_HOME ;
2766+ const char * untilded ;
2767+ struct stat sb ;
2768+
2769+ XDG_CONFIG_HOME = getenv ("XDG_CONFIG_HOME" );
2770+ if (!XDG_CONFIG_HOME )
2771+ XDG_CONFIG_HOME = "~/.config" ;
2772+
2773+ untilded = tilde (NULL , XDG_CONFIG_HOME );
2774+
2775+ /* untilded must be a directory, else use old config path */
2776+ if (stat (untilded , & sb ) == 0 && S_ISDIR (sb .st_mode ))
2777+ {
2778+ snprintf (state .pspg_conf_path , MAXPATHLEN , "%s/pspg.conf" , untilded );
2779+
2780+ cf = fopen (state .pspg_conf_path , "r" );
2781+
2782+ }
27582783 }
27592784 }
27602785
2761- load_config (tilde (NULL , pspg_conf_path ), & opts );
2762- if (PSPG_CONF && errno )
2763- fprintf (stderr , "cannot to read from config file \"%s\" specified by PSPG_CONF (%s), ignored\n" ,
2764- PSPG_CONF ,
2765- strerror (errno ));
2786+ if (cf )
2787+ {
2788+ /*
2789+ * in this moment, the log file is not initialized,
2790+ * use strerr instead.
2791+ */
2792+ logfile = stderr ;
2793+
2794+ if (!load_config (cf , & opts ))
2795+ fprintf (stderr , "warning: found errors while reading of config file" );
2796+
2797+ logfile = NULL ;
2798+
2799+ fclose (cf );
2800+ cf = NULL ;
2801+ }
27662802
27672803 PSPG_HISTORY = getenv ("PSPG_HISTORY" );
27682804 if (PSPG_HISTORY )
27692805 {
2770- snprintf ( pspg_history_path , MAXPATHLEN , "%s" , PSPG_HISTORY );
2806+ tilde ( state . pspg_hist_path , PSPG_HISTORY );
27712807 }
27722808 else
27732809 {
2774- XDG_STATE_HOME = getenv ( "XDG_STATE_HOME" ) ;
2775- if ( XDG_STATE_HOME )
2776- {
2777- snprintf ( pspg_history_path , MAXPATHLEN , "%s/ pspg_history", XDG_STATE_HOME );
2778- }
2779- else
2810+ struct stat sb ;
2811+ const char * untilded ;
2812+
2813+ tilde ( state . pspg_hist_path , "~/. pspg_history" );
2814+
2815+ if ( stat ( state . pspg_hist_path , & sb ) != 0 || ! S_ISREG ( sb . st_mode ))
27802816 {
2781- snprintf (pspg_history_path , MAXPATHLEN , "%s" , "~/.pspg_history" );
2817+ const char * XDG_STATE_HOME ;
2818+
2819+ XDG_STATE_HOME = getenv ("XDG_STATE_HOME" );
2820+ if (!XDG_STATE_HOME )
2821+ XDG_STATE_HOME = "~/.local/state/" ;
2822+
2823+ /* the directory should to exists, or use old path */
2824+ untilded = tilde (NULL , XDG_STATE_HOME );
2825+
2826+ if (stat (untilded , & sb ) == 0 && S_ISDIR (sb .st_mode ))
2827+ snprintf (state .pspg_hist_path , MAXPATHLEN , "%s/pspg_history" , untilded );
27822828 }
27832829 }
27842830
@@ -3389,7 +3435,7 @@ main(int argc, char *argv[])
33893435 last_nullstr [0 ] = '\0' ;
33903436
33913437 /* initialize readline if it is active */
3392- pspg_init_readline (pspg_history_path );
3438+ pspg_init_readline (state . pspg_hist_path );
33933439
33943440#ifdef COMPILE_MENU
33953441
@@ -4295,23 +4341,23 @@ main(int argc, char *argv[])
42954341 break ;
42964342
42974343 case cmd_SaveSetup :
4298- if (!save_config (tilde ( NULL , pspg_conf_path ) , & opts ))
4344+ if (!save_config (state . pspg_conf_path , & opts ))
42994345 {
43004346 if (errno != 0 )
43014347 {
4302- char buffer1 [1000 ];
4348+ char buffer1 [1000 ];
43034349
43044350 snprintf (buffer1 , 1000 , "Cannot write to \"%.800s\" (%s)" ,
4305- pspg_conf_path , strerror (errno ));
4351+ state . pspg_conf_path , strerror (errno ));
43064352
43074353 show_info_wait (buffer1 , strerror (errno ), true, true, false, true);
43084354 }
43094355 else
4310- show_info_wait (" Cannot write to \"%s\"" , pspg_conf_path ,
4356+ show_info_wait (" Cannot write to \"%s\"" , state . pspg_conf_path ,
43114357 true, true, false, true);
43124358 }
43134359 else
4314- show_info_wait (" Setup saved to \"%s\"" , pspg_conf_path ,
4360+ show_info_wait (" Setup saved to \"%s\"" , state . pspg_conf_path ,
43154361 true, true, true, false);
43164362 break ;
43174363
@@ -7063,7 +7109,7 @@ main(int argc, char *argv[])
70637109
70647110 close_tty_stream ();
70657111
7066- pspg_save_history (pspg_history_path , & opts );
7112+ pspg_save_history (state . pspg_hist_path , & opts );
70677113
70687114 /*
70697115 * Try to release all allocated memory, although this has not
0 commit comments