summaryrefslogtreecommitdiff
path: root/src/command.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/command.c')
-rw-r--r--src/command.c185
1 files changed, 185 insertions, 0 deletions
diff --git a/src/command.c b/src/command.c
new file mode 100644
index 0000000..7ae36ae
--- /dev/null
+++ b/src/command.c
@@ -0,0 +1,185 @@
+/*
+ * command.c
+ *
+ * Copyright © 2019 Thomas White <taw@bitwiz.me.uk>
+ *
+ * This file is part of NanoLight.
+ *
+ * NanoLight is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+#include <libintl.h>
+#define _(x) gettext(x)
+
+#include "nanolight.h"
+
+enum token_type
+{
+ TK_FIXTURE,
+ TK_AT,
+ TK_DOT,
+ TK_TO,
+ TK_LEVEL,
+ TK_ATTRIBUTE,
+};
+
+
+struct token
+{
+ enum token_type type;
+ int fixture_index;
+ int val;
+};
+
+
+static int stop_char(char c)
+{
+ if ( c == '@' ) return 1;
+ if ( c == ' ' ) return 1;
+ if ( c == '\0' ) return 1;
+ if ( c == '.' ) return 1;
+ if ( c == '-' ) return 1;
+ return 0;
+}
+
+
+static int find_tokens(const char *cmd, struct token *tokens, struct nanolight *nl)
+{
+ int i;
+ int n = 0;
+ int pos = 0;
+ do {
+ int start;
+ char *word;
+ unsigned long val;
+ char *endptr;
+ int done = 0;
+
+ while ( isspace(cmd[pos]) ) pos++;
+ start = pos;
+
+ /* Is it an AT? */
+ if ( cmd[pos] == '@' ) {
+ tokens[n++].type = TK_AT;
+ pos++;
+ continue;
+ }
+
+ /* Is it a dot? */
+ if ( cmd[pos] == '.' ) {
+ tokens[n++].type = TK_DOT;
+ pos++;
+ continue;
+ }
+
+ /* Is it a dash? */
+ if ( cmd[pos] == '-' ) {
+ tokens[n++].type = TK_TO;
+ pos++;
+ continue;
+ }
+
+ while ( !stop_char(cmd[pos]) ) pos++;
+ word = strndup(cmd+start, pos-start);
+
+ /* Is is a fixture name? */
+ for ( i=0; i<nl->n_fixtures; i++ ) {
+ if ( strcasecmp(nl->fixtures[i].label, word) == 0 ) {
+ tokens[n].fixture_index = i;
+ tokens[n++].type = TK_FIXTURE;
+ done = 1;
+ break;
+ }
+ }
+
+ /* Is is an attribute name? */
+ for ( i=0; i<nl->n_fixtures; i++ ) {
+ if ( strcasecmp(nl->fixtures[i].label, word) == 0 ) {
+ tokens[n].fixture_index = i;
+ tokens[n++].type = TK_FIXTURE;
+ done = 1;
+ break;
+ }
+ }
+
+ /* Is it a number? */
+ val = strtoul(word, &endptr, 10);
+ if ( (word[0] != '\0') && (endptr[0] == '\0') ) {
+ tokens[n].val = val;
+ tokens[n++].type = TK_LEVEL;
+ done = 1;
+ }
+
+ free(word);
+
+ if ( !done ) return 0;
+
+ } while ( cmd[pos] != '\0' );
+ return n;
+}
+
+
+static void show_tokens(struct token *tokens, int n, struct nanolight *nl)
+{
+ int i;
+
+ for ( i=0; i<n; i++ ) {
+ switch ( tokens[i].type ) {
+
+ case TK_FIXTURE:
+ printf(" [fixture:%s]", nl->fixtures[tokens[i].fixture_index].label);
+ break;
+
+ case TK_AT:
+ printf(" [@]");
+ break;
+
+ case TK_DOT:
+ printf(" [.]");
+ break;
+
+ case TK_TO:
+ printf(" [-]");
+ break;
+
+ case TK_LEVEL:
+ printf(" [value:%i]", tokens[i].val);
+ break;
+
+ }
+ }
+
+ printf("\n");
+}
+
+
+int command_run(const char *cmd, struct nanolight *nl)
+{
+ struct token tokens[1024];
+ int n;
+
+ n = find_tokens(cmd, tokens, nl);
+ if ( n == 0 ) return 1;
+
+ show_tokens(tokens, n, nl);
+
+ return 0;
+}
+