2023-03-03 00:29:00 +01:00
|
|
|
//---------------------------------------------------------------------------//
|
|
|
|
// ==>/[_]\ fxlink: A community communication tool for CASIO calculators. //
|
|
|
|
// |:::| Made by Lephe' as part of the fxSDK. //
|
|
|
|
// \___/ License: MIT <https://opensource.org/licenses/MIT> //
|
|
|
|
//---------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
#include <fxlink/filter.h>
|
|
|
|
#include <fxlink/logging.h>
|
|
|
|
#include <stdlib.h>
|
2021-04-03 11:58:30 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
//---
|
2023-03-03 00:29:00 +01:00
|
|
|
// Filter parser
|
2021-04-03 11:58:30 +02:00
|
|
|
//---
|
|
|
|
|
2023-03-03 00:29:00 +01:00
|
|
|
/* Identify property separating characters to be skipped */
|
|
|
|
static bool issep(int c)
|
2021-04-03 11:58:30 +02:00
|
|
|
{
|
2023-03-03 00:29:00 +01:00
|
|
|
return (c == ' ' || c == '\t' || c == '\n' || c == ',');
|
2021-04-03 11:58:30 +02:00
|
|
|
}
|
2023-03-03 00:29:00 +01:00
|
|
|
/* Identify valid word characters for the filter */
|
|
|
|
static bool isword(int c)
|
2021-04-03 11:58:30 +02:00
|
|
|
{
|
2023-03-03 00:29:00 +01:00
|
|
|
return c && !strchr(" \t\n,=", c);
|
2021-04-03 11:58:30 +02:00
|
|
|
}
|
2023-03-03 00:29:00 +01:00
|
|
|
/* Copy the next word in the string, assumes word is non-empty */
|
|
|
|
static char *read_word(char const **input)
|
2021-04-03 11:58:30 +02:00
|
|
|
{
|
|
|
|
char const *str = *input;
|
|
|
|
while(**input && isword(**input)) (*input)++;
|
|
|
|
return strndup(str, *input - str);
|
|
|
|
}
|
|
|
|
|
2023-03-03 00:29:00 +01:00
|
|
|
/* Reads a property from the input source. Advances *input and sets *name and
|
|
|
|
*value to newly-allocated copies of the name and (optional) value of the
|
|
|
|
property (T_PROP). Both should be free()'d after use. At the end of the
|
|
|
|
input, returns false and sets *name = *value = NULL. */
|
|
|
|
static bool read_property(char const **input, char **name, char **value)
|
2021-04-03 11:58:30 +02:00
|
|
|
{
|
|
|
|
*name = *value = NULL;
|
|
|
|
|
2023-03-03 00:29:00 +01:00
|
|
|
while(issep(**input))
|
2021-04-03 11:58:30 +02:00
|
|
|
(*input)++;
|
2023-03-03 00:29:00 +01:00
|
|
|
if(!**input)
|
|
|
|
return false;
|
2021-04-03 11:58:30 +02:00
|
|
|
|
|
|
|
if(!isword(**input)) {
|
2023-03-03 00:29:00 +01:00
|
|
|
elog("expected property name in filter, found '%c'; stopping\n",
|
|
|
|
**input);
|
|
|
|
return false;
|
2021-04-03 11:58:30 +02:00
|
|
|
}
|
|
|
|
*name = read_word(input);
|
2023-03-03 00:29:00 +01:00
|
|
|
|
|
|
|
if(**input == '=') {
|
|
|
|
(*input)++;
|
2021-04-03 11:58:30 +02:00
|
|
|
*value = read_word(input);
|
2023-03-03 00:29:00 +01:00
|
|
|
}
|
|
|
|
return true;
|
2021-04-03 11:58:30 +02:00
|
|
|
}
|
|
|
|
|
2023-03-03 00:29:00 +01:00
|
|
|
struct fxlink_filter *fxlink_filter_parse(char const *input)
|
2021-04-03 11:58:30 +02:00
|
|
|
{
|
|
|
|
char *name=NULL, *value=NULL;
|
2023-03-03 00:29:00 +01:00
|
|
|
struct fxlink_filter *filter = calloc(1, sizeof *filter);
|
|
|
|
if(!filter)
|
2021-04-03 11:58:30 +02:00
|
|
|
return NULL;
|
|
|
|
|
2023-03-03 00:29:00 +01:00
|
|
|
while(read_property(&input, &name, &value)) {
|
2021-04-03 11:58:30 +02:00
|
|
|
/* Add a new property to the current option */
|
|
|
|
if(!strcmp(name, "p7") && !value)
|
2023-03-03 00:29:00 +01:00
|
|
|
filter->p7 = true;
|
2021-04-03 11:58:30 +02:00
|
|
|
else if(!strcmp(name, "mass_storage") && !value)
|
2023-03-03 00:29:00 +01:00
|
|
|
filter->mass_storage = true;
|
2021-04-03 11:58:30 +02:00
|
|
|
else if(!strcmp(name, "series_cg") && !value)
|
2023-03-03 00:29:00 +01:00
|
|
|
filter->series_cg = true;
|
2021-04-03 11:58:30 +02:00
|
|
|
else if(!strcmp(name, "series_g3") && !value)
|
2023-03-03 00:29:00 +01:00
|
|
|
filter->series_g3 = true;
|
|
|
|
else if(!strcmp(name, "intf_fxlink") && !value)
|
|
|
|
filter->intf_fxlink = true;
|
|
|
|
else if(!strcmp(name, "intf_cesg502") && !value)
|
|
|
|
filter->intf_cesg502 = true;
|
|
|
|
else if(!strcmp(name, "serial") && value)
|
|
|
|
filter->serial = strdup(value);
|
|
|
|
else if(!strcmp(name, "serial_number") && value) // Old name
|
|
|
|
filter->serial = strdup(value);
|
|
|
|
else wlog("ignoring invalid filter property: '%s' (%s value)\n",
|
|
|
|
name, value ? "with" : "without");
|
2021-04-03 11:58:30 +02:00
|
|
|
|
|
|
|
free(name);
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
return filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---
|
|
|
|
// Filtering API
|
|
|
|
//---
|
|
|
|
|
2023-03-03 00:29:00 +01:00
|
|
|
void fxlink_filter_clean_libusb(struct fxlink_filter *filter)
|
2021-04-03 11:58:30 +02:00
|
|
|
{
|
2023-03-03 00:29:00 +01:00
|
|
|
if(!filter)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Suppress series_cg and series_g3, which are based off the SCSI metadata
|
|
|
|
provided only to UDisks2 */
|
|
|
|
if(filter->series_cg) {
|
|
|
|
wlog("ignoring series_cg in libusb filter (cannot be detected)\n");
|
|
|
|
filter->series_cg = false;
|
|
|
|
}
|
|
|
|
if(filter->series_g3) {
|
|
|
|
wlog("ignoring series_g3 in libusb filter (cannot be detected)\n");
|
|
|
|
filter->series_g3 = false;
|
2021-04-03 11:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 00:29:00 +01:00
|
|
|
void fxlink_filter_clean_udisks2(struct fxlink_filter *filter)
|
2021-04-03 11:58:30 +02:00
|
|
|
{
|
|
|
|
/* Every property can be used */
|
|
|
|
(void)filter;
|
|
|
|
}
|
|
|
|
|
2023-03-03 00:29:00 +01:00
|
|
|
bool fxlink_filter_match(
|
|
|
|
struct fxlink_filter const *props,
|
|
|
|
struct fxlink_filter const *filter)
|
2021-04-03 11:58:30 +02:00
|
|
|
{
|
|
|
|
/* No filter is a pass-through */
|
2023-03-03 00:29:00 +01:00
|
|
|
if(!filter)
|
2021-04-03 11:58:30 +02:00
|
|
|
return true;
|
|
|
|
|
2023-03-03 00:29:00 +01:00
|
|
|
if(filter->p7 && !props->p7)
|
|
|
|
return false;
|
|
|
|
if(filter->mass_storage && !props->mass_storage)
|
|
|
|
return false;
|
|
|
|
if(filter->intf_fxlink && !props->intf_fxlink)
|
|
|
|
return false;
|
|
|
|
if(filter->intf_cesg502 && !props->intf_cesg502)
|
|
|
|
return false;
|
|
|
|
if(filter->series_cg && !props->series_cg)
|
|
|
|
return false;
|
|
|
|
if(filter->series_g3 && !props->series_g3)
|
|
|
|
return false;
|
|
|
|
if(filter->serial &&
|
|
|
|
(!props->serial || strcmp(filter->serial, props->serial)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2021-04-03 11:58:30 +02:00
|
|
|
}
|
|
|
|
|
2023-03-03 00:29:00 +01:00
|
|
|
void fxlink_filter_print(FILE *fp, struct fxlink_filter const *filter)
|
2021-04-03 11:58:30 +02:00
|
|
|
{
|
|
|
|
#define output(...) { \
|
|
|
|
if(sep) fprintf(fp, ", "); \
|
|
|
|
fprintf(fp, __VA_ARGS__); \
|
|
|
|
sep = true; \
|
|
|
|
}
|
|
|
|
|
2023-03-03 00:29:00 +01:00
|
|
|
bool sep = false;
|
|
|
|
if(filter->p7)
|
|
|
|
output("p7");
|
|
|
|
if(filter->mass_storage)
|
|
|
|
output("mass_storage");
|
|
|
|
if(filter->intf_fxlink)
|
|
|
|
output("intf_fxlink");
|
|
|
|
if(filter->intf_cesg502)
|
|
|
|
output("intf_cesg502");
|
|
|
|
if(filter->series_cg)
|
|
|
|
output("series_cg");
|
|
|
|
if(filter->series_g3)
|
|
|
|
output("series_g3");
|
|
|
|
if(filter->serial)
|
|
|
|
output("serial=%s", filter->serial);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fxlink_filter_free(struct fxlink_filter *filter)
|
|
|
|
{
|
|
|
|
if(filter)
|
|
|
|
free(filter->serial);
|
|
|
|
free(filter);
|
2021-04-03 11:58:30 +02:00
|
|
|
}
|