use strtok to walk through the input string

vflag added for extra console output (toggle, set operations)
This commit is contained in:
onyx-and-iris 2024-07-02 10:13:37 +01:00
parent 12a55a52f2
commit 97fc9ca9ce
2 changed files with 54 additions and 45 deletions

View File

@ -14,23 +14,23 @@ enum kind
POTATOX64,
};
long login(T_VBVMR_INTERFACE *iVMR, int kind);
long logout(T_VBVMR_INTERFACE *iVMR);
long run_voicemeeter(T_VBVMR_INTERFACE *iVMR, int kind);
long type(T_VBVMR_INTERFACE *iVMR, long *type);
long version(T_VBVMR_INTERFACE *iVMR, long *version);
long login(T_VBVMR_INTERFACE *vmr, int kind);
long logout(T_VBVMR_INTERFACE *vmr);
long run_voicemeeter(T_VBVMR_INTERFACE *vmr, int kind);
long type(T_VBVMR_INTERFACE *vmr, long *type);
long version(T_VBVMR_INTERFACE *vmr, long *version);
bool is_pdirty(T_VBVMR_INTERFACE *iVMR);
long get_parameter_float(T_VBVMR_INTERFACE *iVMR, char *param, float *f);
long get_parameter_string(T_VBVMR_INTERFACE *iVMR, char *param, unsigned short *s);
long set_parameter_float(T_VBVMR_INTERFACE *iVMR, char *param, float val);
long set_parameter_string(T_VBVMR_INTERFACE *iVMR, char *param, char *s);
long set_parameters(T_VBVMR_INTERFACE *iVMR, char *command);
bool is_pdirty(T_VBVMR_INTERFACE *vmr);
long get_parameter_float(T_VBVMR_INTERFACE *vmr, char *param, float *f);
long get_parameter_string(T_VBVMR_INTERFACE *vmr, char *param, unsigned short *s);
long set_parameter_float(T_VBVMR_INTERFACE *vmr, char *param, float val);
long set_parameter_string(T_VBVMR_INTERFACE *vmr, char *param, char *s);
long set_parameters(T_VBVMR_INTERFACE *vmr, char *command);
bool is_mdirty(T_VBVMR_INTERFACE *iVMR);
long macrobutton_getstatus(T_VBVMR_INTERFACE *iVMR, long n, float *val, long mode);
long macrobutton_setstatus(T_VBVMR_INTERFACE *iVMR, long n, float val, long mode);
bool is_mdirty(T_VBVMR_INTERFACE *vmr);
long macrobutton_getstatus(T_VBVMR_INTERFACE *vmr, long n, float *val, long mode);
long macrobutton_setstatus(T_VBVMR_INTERFACE *vmr, long n, float val, long mode);
void clear_dirty(T_VBVMR_INTERFACE *iVMR);
void clear_dirty(T_VBVMR_INTERFACE *vmr);
#endif
#endif /* __VMR_H__ */

View File

@ -29,9 +29,12 @@ void help(void);
enum kind set_kind(char *kval);
int init_voicemeeter(T_VBVMR_INTERFACE *vmr, int kind);
void interactive(T_VBVMR_INTERFACE *vmr);
void parse_input(T_VBVMR_INTERFACE *vmr, char *input, int len);
void parse_command(T_VBVMR_INTERFACE *vmr, char *command);
void get(T_VBVMR_INTERFACE *vmr, char *command, struct result *res);
bool vflag = false;
int main(int argc, char *argv[])
{
bool iflag = false;
@ -48,7 +51,7 @@ int main(int argc, char *argv[])
log_set_level(LOG_WARN);
while ((opt = getopt(argc, argv, "k:ihD:")) != -1)
while ((opt = getopt(argc, argv, "k:ihD:v")) != -1)
{
switch (opt)
{
@ -75,6 +78,9 @@ int main(int argc, char *argv[])
"Log level will default to LOG_WARN (3).\n");
}
break;
case 'v':
vflag = true;
break;
default:
abort();
}
@ -98,7 +104,7 @@ int main(int argc, char *argv[])
{
for (int i = optind; i < argc; i++)
{
parse_command(vmr, argv[i]);
parse_input(vmr, argv[i], strlen(argv[i]));
}
}
@ -116,12 +122,13 @@ int main(int argc, char *argv[])
void help()
{
puts(
"Usage: ./vmrcli.exe [-h] [-i] [-k] [-D] <api commands>\n"
"Usage: ./vmrcli.exe [-h] [-i] [-k] [-D] [-v] <api commands>\n"
"Where: \n"
"\th: Prints the help dialogue\n"
"\ti: Enable interactive mode\n"
"\tk: The kind of Voicemeeter (basic, banana, potato)\n"
"\tD: Set log level 0=TRACE, 1=DEBUG, 2=INFO, 3=WARN, 4=ERROR, 5=FATAL");
"\tD: Set log level 0=TRACE, 1=DEBUG, 2=INFO, 3=WARN, 4=ERROR, 5=FATAL"
"\tv: Enable extra console output (toggle, set messages)\n");
}
/**
@ -188,9 +195,7 @@ int init_voicemeeter(T_VBVMR_INTERFACE *vmr, int kind)
void interactive(T_VBVMR_INTERFACE *vmr)
{
char input[MAX_LINE], command[MAX_LINE];
char *p = input;
int i;
char input[MAX_LINE];
size_t len;
printf(">> ");
@ -201,32 +206,26 @@ void interactive(T_VBVMR_INTERFACE *vmr)
if (len == 1 && toupper(input[0]) == 'Q')
break;
replace_multiple_space_with_one(input, len);
while (*p)
{
if (isspace(*p))
{
p++;
continue;
}
log_trace("commands still in buffer: %s", p);
parse_input(vmr, input, len);
i = 0;
while (*p && !isspace(*p))
command[i++] = *p++;
command[i] = '\0';
if (command[0] != '\0')
parse_command(vmr, command);
memset(command, '\0', MAX_LINE);
}
p = input; /* reset pointer */
memset(input, '\0', MAX_LINE); /* reset input buffer */
printf(">> ");
}
}
void parse_input(T_VBVMR_INTERFACE *vmr, char *input, int len)
{
char *token;
replace_multiple_space_with_one(input, len);
token = strtok(input, " ");
while (token != NULL)
{
parse_command(vmr, token);
token = strtok(NULL, " ");
}
}
void parse_command(T_VBVMR_INTERFACE *vmr, char *command)
{
log_debug("Parsing %s", command);
@ -240,7 +239,13 @@ void parse_command(T_VBVMR_INTERFACE *vmr, char *command)
if (res.type == FLOAT_T)
{
if (res.val.f == 1 || res.val.f == 0)
{
set_parameter_float(vmr, command, 1 - res.val.f);
if (vflag)
{
printf("Toggling %s\n", command);
}
}
else
log_warn("%s does not appear to be a boolean parameter", command);
}
@ -250,6 +255,10 @@ void parse_command(T_VBVMR_INTERFACE *vmr, char *command)
if (strchr(command, '=') != NULL) /* set */
{
set_parameters(vmr, command);
if (vflag)
{
printf("Setting %s\n", command);
}
}
else /* get */
{
@ -259,10 +268,10 @@ void parse_command(T_VBVMR_INTERFACE *vmr, char *command)
switch (res.type)
{
case FLOAT_T:
printf("%.1f\n", res.val.f);
printf("%s: %.1f\n", command, res.val.f);
break;
case STRING_T:
printf("%ls\n", res.val.s);
printf("%s: %ls\n", command, res.val.s);
break;
default:
break;