3
3
#include <string.h>
4
4
#include <strings.h>
5
5
#include <unistd.h>
6
- #include <errno.h>
7
6
#include "sway/commands.h"
8
7
#include "sway/config.h"
9
- #include "sway/swaynag.h"
10
8
#include "log.h"
11
9
#include "stringop.h"
12
10
@@ -42,14 +40,14 @@ struct cmd_results *output_cmd_background(int argc, char **argv) {
42
40
}
43
41
44
42
struct output_config * output = config -> handler_context .output_config ;
45
-
43
+ char * src = NULL ;
46
44
if (strcasecmp (argv [1 ], "solid_color" ) == 0 ) {
47
45
if (!validate_color (argv [0 ])) {
48
46
return cmd_results_new (CMD_INVALID ,
49
47
"Colors should be of the form #RRGGBB" );
50
48
}
51
- output -> background = strdup (argv [0 ]);
52
- output -> background_option = strdup ("solid_color" );
49
+ if (!( output -> background = strdup (argv [0 ]))) goto cleanup ;
50
+ if (!( output -> background_option = strdup ("solid_color" ))) goto cleanup ;
53
51
output -> background_fallback = NULL ;
54
52
argc -= 2 ; argv += 2 ;
55
53
} else {
@@ -77,37 +75,25 @@ struct cmd_results *output_cmd_background(int argc, char **argv) {
77
75
return cmd_results_new (CMD_INVALID , "Missing background file" );
78
76
}
79
77
80
- char * src = join_args (argv , j );
78
+ if (!( src = join_args (argv , j ))) goto cleanup ;
81
79
if (!expand_path (& src )) {
82
80
struct cmd_results * cmd_res = cmd_results_new (CMD_INVALID ,
83
81
"Invalid syntax (%s)" , src );
84
82
free (src );
85
83
return cmd_res ;
86
84
}
87
- if (!src ) {
88
- sway_log (SWAY_ERROR , "Failed to allocate expanded path" );
89
- return cmd_results_new (CMD_FAILURE , "Unable to allocate resource" );
90
- }
91
85
92
86
if (config -> reading && * src != '/' ) {
93
87
// src file is inside configuration dir
94
88
95
89
char * conf = strdup (config -> current_config_path );
96
- if (!conf ) {
97
- sway_log (SWAY_ERROR , "Failed to duplicate string" );
98
- free (src );
99
- return cmd_results_new (CMD_FAILURE ,
100
- "Unable to allocate resources" );
101
- }
90
+ if (!conf ) goto cleanup ;
102
91
103
92
char * conf_path = dirname (conf );
104
93
char * real_src = malloc (strlen (conf_path ) + strlen (src ) + 2 );
105
94
if (!real_src ) {
106
- free (src );
107
95
free (conf );
108
- sway_log (SWAY_ERROR , "Unable to allocate memory" );
109
- return cmd_results_new (CMD_FAILURE ,
110
- "Unable to allocate resources" );
96
+ goto cleanup ;
111
97
}
112
98
113
99
snprintf (real_src , strlen (conf_path ) + strlen (src ) + 2 , "%s/%s" , conf_path , src );
@@ -117,40 +103,48 @@ struct cmd_results *output_cmd_background(int argc, char **argv) {
117
103
}
118
104
119
105
bool can_access = access (src , F_OK ) != -1 ;
120
- if (!can_access ) {
121
- sway_log_errno (SWAY_ERROR , "Unable to access background file '%s'" ,
122
- src );
123
- config_add_swaynag_warning ("Unable to access background file '%s'" ,
124
- src );
125
- struct cmd_results * result = cmd_results_new (CMD_FAILURE ,
126
- "unable to access background file '%s'" , src );
127
- free (src );
128
- return result ;
129
- } else {
130
- output -> background = src ;
131
- output -> background_option = strdup (mode );
132
- }
133
106
argc -= j + 1 ; argv += j + 1 ;
107
+ free (output -> background_option );
108
+ free (output -> background_fallback );
109
+ free (output -> background );
110
+ output -> background = output -> background_option = output -> background_fallback = NULL ;
111
+ char * fallback = NULL ;
134
112
135
- output -> background_fallback = NULL ;
136
113
if (argc && * argv [0 ] == '#' ) {
137
- if (!validate_color (argv [0 ])) {
138
- return cmd_results_new (CMD_INVALID ,
139
- "fallback color should be of the form #RRGGBB" );
114
+ if (validate_color (argv [0 ])) {
115
+ if (!(fallback = strdup (argv [0 ]))) goto cleanup ;
116
+ output -> background_fallback = fallback ;
117
+ } else {
118
+ sway_log (SWAY_ERROR , "fallback '%s' should be of the form #RRGGBB" , argv [0 ]);
119
+ config_add_swaynag_warning ("fallback '%s' should be of the form #RRGGBB\n" , argv [0 ]);
140
120
}
141
-
142
- output -> background_fallback = strdup (argv [0 ]);
143
121
argc -- ; argv ++ ;
122
+ }
144
123
145
- if (!can_access ) {
146
- output -> background = output -> background_fallback ;
147
- output -> background_option = strdup ("solid_color" );
148
- output -> background_fallback = NULL ;
124
+ if (!can_access ) {
125
+ if (!fallback ) {
126
+ sway_log (SWAY_ERROR , "Unable to access background file '%s' "
127
+ "and no valid fallback provided" , src );
128
+ struct cmd_results * res = cmd_results_new (CMD_FAILURE , "Unable to access "
129
+ "background file '%s' and no valid fallback provided" , src );
130
+ free (src );
131
+ return res ;
149
132
}
133
+ sway_log (SWAY_DEBUG , "Cannot access file '%s', using fallback '%s'" , src , fallback );
134
+ output -> background = fallback ;
135
+ if (!(output -> background_option = strdup ("solid_color" ))) goto cleanup ;
136
+ output -> background_fallback = NULL ;
137
+ } else {
138
+ output -> background = src ;
139
+ if (!(output -> background_option = strdup (mode ))) goto cleanup ;
150
140
}
151
141
}
152
-
153
142
config -> handler_context .leftovers .argc = argc ;
154
143
config -> handler_context .leftovers .argv = argv ;
155
144
return NULL ;
145
+
146
+ cleanup :
147
+ free (src );
148
+ sway_log (SWAY_ERROR , "Failed to allocate resources" );
149
+ return cmd_results_new (CMD_FAILURE , "Unable to allocate resources" );
156
150
}
0 commit comments