Generalized io_error
This commit is contained in:
parent
ed3edbdf56
commit
8c17dcf7e5
19
src/cmd.c
19
src/cmd.c
@ -23,16 +23,16 @@
|
|||||||
#include "cmd.h"
|
#include "cmd.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
#define GET_LONG_OPT "--get"
|
#define GET_LONG_OPT "--get"
|
||||||
#define GET_SHORT_OPT "-g"
|
#define GET_SHORT_OPT "-g"
|
||||||
#define FADE_LONG_OPT "--fade"
|
#define FADE_LONG_OPT "--fade"
|
||||||
#define FADE_SHORT_OPT "-f"
|
#define FADE_SHORT_OPT "-f"
|
||||||
#define DEVICE_LONG_OPT "--device"
|
#define DEVICE_LONG_OPT "--device"
|
||||||
#define DEVICE_SHORT_OPT "-d"
|
#define DEVICE_SHORT_OPT "-d"
|
||||||
#define LIST_LONG_OPT "--list"
|
#define LIST_LONG_OPT "--list"
|
||||||
#define LIST_SHORT_OPT "-l"
|
#define LIST_SHORT_OPT "-l"
|
||||||
#define HELP_LONG_OPT "--help"
|
#define HELP_LONG_OPT "--help"
|
||||||
#define HELP_SHORT_OPT "-h"
|
#define HELP_SHORT_OPT "-h"
|
||||||
|
|
||||||
|
|
||||||
#define CHECK_OPTION(opt, arg) (!strcmp(opt##_LONG_OPT, arg) || !strcmp(opt##_SHORT_OPT, arg))
|
#define CHECK_OPTION(opt, arg) (!strcmp(opt##_LONG_OPT, arg) || !strcmp(opt##_SHORT_OPT, arg))
|
||||||
@ -46,6 +46,7 @@ struct cmd_arg rexbacklight_args[] = {
|
|||||||
};
|
};
|
||||||
int rexbacklight_args_length = sizeof(rexbacklight_args) / sizeof(rexbacklight_args[0]);
|
int rexbacklight_args_length = sizeof(rexbacklight_args) / sizeof(rexbacklight_args[0]);
|
||||||
|
|
||||||
|
//Clean up a cmd_arg struct
|
||||||
void free_cmd_args(struct arg_values* a){
|
void free_cmd_args(struct arg_values* a){
|
||||||
if(!a->next)
|
if(!a->next)
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -29,12 +29,26 @@ static const char* backlight_dir = "/sys/class/backlight/";
|
|||||||
static const char* backlight_file = "brightness";
|
static const char* backlight_file = "brightness";
|
||||||
static const char* max_backlight_file = "max_brightness";
|
static const char* max_backlight_file = "max_brightness";
|
||||||
|
|
||||||
|
//Used to store a dynamic array of dynamic strings and size
|
||||||
struct string_array{
|
struct string_array{
|
||||||
char** list;
|
char** list;
|
||||||
int size;
|
int size;
|
||||||
};
|
};
|
||||||
|
|
||||||
//Delete the list of backlight devices
|
#define IO_ERROR_DIR "directory"
|
||||||
|
#define IO_ERROR_FILE "file"
|
||||||
|
#define IO_ERROR_OPEN "open"
|
||||||
|
#define IO_ERROR_READ "read"
|
||||||
|
#define IO_ERROR_WRITE "write to"
|
||||||
|
|
||||||
|
void io_error(const char* type, const char* error, const char* name){
|
||||||
|
fprintf(stderr, "Unable to %s %s '%s'!\n", error, type, name);
|
||||||
|
}
|
||||||
|
void io_error_2(const char* type, const char* error, const char* name, const char* name2){
|
||||||
|
fprintf(stderr, "Unable to %s %s '%s%s'!\n", error, type, name, name2);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Clean up a string array
|
||||||
void free_string_array(struct string_array* s){
|
void free_string_array(struct string_array* s){
|
||||||
for(int i = 0;i < s->size;i++)
|
for(int i = 0;i < s->size;i++)
|
||||||
free(s->list[i]);
|
free(s->list[i]);
|
||||||
@ -50,7 +64,7 @@ struct string_array get_backlight_sources(void){
|
|||||||
fd = opendir(backlight_dir);
|
fd = opendir(backlight_dir);
|
||||||
//If the dir can't be opened, return no backlight devices
|
//If the dir can't be opened, return no backlight devices
|
||||||
if(!fd){
|
if(!fd){
|
||||||
fprintf(stderr, "Unable to open '%s' for reading!\n", backlight_dir);
|
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, backlight_dir);
|
||||||
return (struct string_array){.list = NULL, .size = 0};
|
return (struct string_array){.list = NULL, .size = 0};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,15 +101,14 @@ struct string_array get_backlight_sources(void){
|
|||||||
|
|
||||||
//Read a float from a file
|
//Read a float from a file
|
||||||
float get_brightness(const char* file){
|
float get_brightness(const char* file){
|
||||||
//Static buffer because it shouldn't be able to exceed 127 chars long
|
|
||||||
char buff[127];
|
char buff[127];
|
||||||
FILE* bright = fopen(file, "r");
|
FILE* bright = fopen(file, "r");
|
||||||
if(!bright){
|
if(!bright){
|
||||||
fprintf(stderr, "Unable to open brightness file \"%s\"!\n", file);
|
io_error(IO_ERROR_OPEN, IO_ERROR_FILE, file);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(!fgets(buff, sizeof(buff), bright)){
|
if(!fgets(buff, sizeof(buff), bright)){
|
||||||
fprintf(stderr, "Unable to read file \"%s\"!\n", file);
|
io_error(IO_ERROR_READ, IO_ERROR_FILE, file);
|
||||||
fclose(bright);
|
fclose(bright);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -108,24 +121,29 @@ void do_assignment(struct arg_values* arg){
|
|||||||
int out = process_op(arg, 0, get_brightness(backlight_file), get_brightness(max_backlight_file));
|
int out = process_op(arg, 0, get_brightness(backlight_file), get_brightness(max_backlight_file));
|
||||||
FILE* bright = fopen(backlight_file, "w+");
|
FILE* bright = fopen(backlight_file, "w+");
|
||||||
if(!bright){
|
if(!bright){
|
||||||
fprintf(stderr, "Unable to open brightness file \"%s%s\" for writing!\n", backlight_dir, backlight_file);
|
io_error_2(IO_ERROR_OPEN, IO_ERROR_FILE, backlight_dir, backlight_file);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
fprintf(bright, "%d", out);
|
fprintf(bright, "%d", out);
|
||||||
fclose(bright);
|
fclose(bright);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Run get operation
|
||||||
void do_get(const char* device){
|
void do_get(const char* device){
|
||||||
fprintf(stdout, "%s: %f\n", device, get_brightness(backlight_file));
|
fprintf(stdout, "%s: %.2f\n", device, get_brightness(backlight_file) / get_brightness(max_backlight_file) * 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Run list operation
|
||||||
void do_list(struct string_array* names){
|
void do_list(struct string_array* names){
|
||||||
for(int i = 0;i < names->size;i++)
|
for(int i = 0;i < names->size;i++)
|
||||||
fprintf(stdout, "%s\n", names->list[i]);
|
fprintf(stdout, "%s\n", names->list[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//If devices were specified, this function will run
|
||||||
void individual_device_loop(struct arg_values* a){
|
void individual_device_loop(struct arg_values* a){
|
||||||
for(struct arg_values* curr = a->next;curr;curr = curr->next){
|
for(struct arg_values* curr = a->next;curr;curr = curr->next){
|
||||||
if(chdir(curr->device)){
|
if(chdir(curr->device)){
|
||||||
fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, curr->device);
|
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, backlight_dir, curr->device);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
switch(curr->operation){
|
switch(curr->operation){
|
||||||
@ -140,6 +158,8 @@ void individual_device_loop(struct arg_values* a){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//If no devices were specified, this function will run
|
||||||
void all_device_loop(struct string_array* backlight_names, struct arg_values* args){
|
void all_device_loop(struct string_array* backlight_names, struct arg_values* args){
|
||||||
switch(args->operation){
|
switch(args->operation){
|
||||||
case OP_SET:
|
case OP_SET:
|
||||||
@ -147,7 +167,7 @@ void all_device_loop(struct string_array* backlight_names, struct arg_values* ar
|
|||||||
case OP_DEC:
|
case OP_DEC:
|
||||||
for(int i = 0;i < backlight_names->size;i++){
|
for(int i = 0;i < backlight_names->size;i++){
|
||||||
if(chdir(backlight_names->list[i])){
|
if(chdir(backlight_names->list[i])){
|
||||||
fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, backlight_names->list[i]);
|
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, backlight_dir, backlight_names->list[i]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
do_assignment(args);
|
do_assignment(args);
|
||||||
@ -159,10 +179,10 @@ void all_device_loop(struct string_array* backlight_names, struct arg_values* ar
|
|||||||
case OP_GET:
|
case OP_GET:
|
||||||
for(int i = 0;i < backlight_names->size;i++){
|
for(int i = 0;i < backlight_names->size;i++){
|
||||||
if(chdir(backlight_names->list[i])){
|
if(chdir(backlight_names->list[i])){
|
||||||
fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, backlight_names->list[i]);
|
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, backlight_dir, backlight_names->list[i]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
fprintf(stdout, "%s: %f\n", backlight_names->list[i], get_brightness(backlight_file));
|
do_get(backlight_names->list[i]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -189,7 +209,6 @@ int main(int argc, char** argv){
|
|||||||
return RETVAL_INVALID_DEVICE;
|
return RETVAL_INVALID_DEVICE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Make sure all explicit devices actually exist
|
//Make sure all explicit devices actually exist
|
||||||
for(struct arg_values* curr = args.next;curr;curr = curr->next){
|
for(struct arg_values* curr = args.next;curr;curr = curr->next){
|
||||||
for(int i = 0;i < backlight_names.size;i++){
|
for(int i = 0;i < backlight_names.size;i++){
|
||||||
@ -203,24 +222,19 @@ int main(int argc, char** argv){
|
|||||||
continue_outer:;
|
continue_outer:;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//save our starting directory so we can change to each backlight directory (makes logic easier)
|
|
||||||
char* starting_dir = getcwd(NULL, 0);
|
char* starting_dir = getcwd(NULL, 0);
|
||||||
|
|
||||||
|
|
||||||
//Redefine CLEANUP to include freeing of the starting_dir string
|
|
||||||
#undef CLEANUP
|
#undef CLEANUP
|
||||||
#define CLEANUP() do{free_string_array(&backlight_names);free_cmd_args(&args);free(starting_dir);}while(0)
|
#define CLEANUP() do{free_string_array(&backlight_names);free_cmd_args(&args);free(starting_dir);}while(0)
|
||||||
|
|
||||||
|
|
||||||
//Change to the base directory for all sysfs backlights
|
//Change to the base directory for all sysfs backlights
|
||||||
if(chdir(backlight_dir)){
|
if(chdir(backlight_dir)){
|
||||||
fprintf(stderr, "Unable to read backlight sysfs directory!\n");
|
io_error(IO_ERROR_READ, IO_ERROR_DIR, backlight_dir);
|
||||||
CLEANUP();
|
CLEANUP();
|
||||||
return RETVAL_INVALID_DIR;
|
return RETVAL_INVALID_DIR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Run selected operation
|
//Run selected operation
|
||||||
if(args.next){
|
if(args.next){
|
||||||
individual_device_loop(&args);
|
individual_device_loop(&args);
|
||||||
@ -228,7 +242,6 @@ int main(int argc, char** argv){
|
|||||||
all_device_loop(&backlight_names, &args);
|
all_device_loop(&backlight_names, &args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Return to start directory
|
//Return to start directory
|
||||||
if(chdir(starting_dir)){
|
if(chdir(starting_dir)){
|
||||||
CLEANUP();
|
CLEANUP();
|
||||||
@ -239,6 +252,7 @@ int main(int argc, char** argv){
|
|||||||
return RETVAL_INVALID_DIR;
|
return RETVAL_INVALID_DIR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CLEANUP();
|
CLEANUP();
|
||||||
return RETVAL_SUCCESS;
|
return RETVAL_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user