summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPeter Mikkelsen <petermikkelsen10@gmail.com>2026-04-01 22:24:22 +0200
committerPeter Mikkelsen <petermikkelsen10@gmail.com>2026-04-04 19:51:35 +0200
commita809bc0f49ee4c6e777f39d1f80ee57862a0fcab (patch)
tree5187dbd9b83205aac62d1f48252025b6e2d158a9 /lib
Initial import of the code into gitv0.1
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am15
-rw-r--r--lib/aplwc.h34
-rw-r--r--lib/aplwc.pc.in12
-rw-r--r--lib/aplwc_internal.h31
-rw-r--r--lib/autocomplete.c94
-rw-r--r--lib/lookup_syscmd.c32
-rw-r--r--lib/new.c36
-rw-r--r--lib/register_syscmd.c39
8 files changed, 293 insertions, 0 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
new file mode 100644
index 0000000..ac8555b
--- /dev/null
+++ b/lib/Makefile.am
@@ -0,0 +1,15 @@
+lib_LTLIBRARIES = libaplwc.la
+
+noinst_HEADERS = aplwc_internal.h
+
+libaplwc_la_SOURCES = \
+ autocomplete.c \
+ lookup_syscmd.c \
+ new.c \
+ register_syscmd.c
+
+libaplwc_la_LDFLAGS = -version-info 0:0:0
+
+include_HEADERS = aplwc.h
+
+pkgconfig_DATA = aplwc.pc
diff --git a/lib/aplwc.h b/lib/aplwc.h
new file mode 100644
index 0000000..aef2495
--- /dev/null
+++ b/lib/aplwc.h
@@ -0,0 +1,34 @@
+/* Aplwc - A Programming Language With Constraints
+ *
+ * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com>
+ *
+ * This file is part of aplwc.
+ *
+ * Aplwc 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.
+ *
+ * Aplwc 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 aplwc. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef APLWC_H
+#define APLWC_H
+
+struct aplwc;
+
+struct aplwc_syscmd {
+ const char *name;
+};
+
+struct aplwc *aplwc_new(void);
+char **aplwc_autocomplete(struct aplwc *, const char *, int, int);
+void aplwc_register_syscmd(struct aplwc *, struct aplwc_syscmd *);
+
+#endif /* APLWC_H */
diff --git a/lib/aplwc.pc.in b/lib/aplwc.pc.in
new file mode 100644
index 0000000..808e117
--- /dev/null
+++ b/lib/aplwc.pc.in
@@ -0,0 +1,12 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: aplwc
+Description: Library interface to aplwc
+Version: @PACKAGE_VERSION@
+
+Libs: -L${libdir} -laplwc
+Cflags: -I${includedir}
+
diff --git a/lib/aplwc_internal.h b/lib/aplwc_internal.h
new file mode 100644
index 0000000..99c16ee
--- /dev/null
+++ b/lib/aplwc_internal.h
@@ -0,0 +1,31 @@
+/* Aplwc - A Programming Language With Constraints
+ *
+ * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com>
+ *
+ * This file is part of aplwc.
+ *
+ * Aplwc 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.
+ *
+ * Aplwc 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 aplwc. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef APLWC_INTERNAL_H
+#define APLWC_INTERNAL_H
+
+struct aplwc {
+ struct aplwc_syscmd **syscmds;
+ int n_syscmds;
+};
+
+struct aplwc_syscmd *aplwc_lookup_syscmd(struct aplwc *, const char *);
+
+#endif /* APLWC_INTERNAL_H */
diff --git a/lib/autocomplete.c b/lib/autocomplete.c
new file mode 100644
index 0000000..12a8cdc
--- /dev/null
+++ b/lib/autocomplete.c
@@ -0,0 +1,94 @@
+/* Aplwc - A Programming Language With Constraints
+ *
+ * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com>
+ *
+ * This file is part of aplwc.
+ *
+ * Aplwc 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.
+ *
+ * Aplwc 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 aplwc. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <aplwc.h>
+#include "aplwc_internal.h"
+
+struct completion_state {
+ const struct aplwc *aplwc;
+ int num;
+ const char *word;
+ const int wordlen;
+};
+
+static char **generate(struct aplwc *, const char *, char *(*)(struct completion_state *));
+static char *completions_syscmd(struct completion_state *);
+
+char **
+aplwc_autocomplete(struct aplwc *aplwc, const char *line, int start, int end)
+{
+ int len = end-start;
+ char *word = malloc(sizeof(char) * len+1);
+ char **completions = NULL;
+
+ memcpy(word, line+start, len);
+ word[len] = 0;
+
+ if(start == 0 && word[0] == ')')
+ completions = generate(aplwc, word, completions_syscmd);
+ free(word);
+
+ return completions;
+}
+
+static char **
+generate(struct aplwc *aplwc, const char *word, char *(*fn)(struct completion_state *))
+{
+ char **completions = NULL;
+ struct completion_state state = {
+ .aplwc = aplwc,
+ .num = 0,
+ .word = word,
+ .wordlen = strlen(word),
+ };
+
+ char *c;
+ int index = 0;
+ while(c = fn(&state)){
+ index++;
+ completions = realloc(completions, sizeof(*completions) * (index + 1));
+ completions[index-1] = c;
+ completions[index] = NULL;
+ }
+
+ return completions;
+}
+
+static char *
+completions_syscmd(struct completion_state *state)
+{
+ struct aplwc_syscmd *syscmd;
+
+ while(syscmd = state->aplwc->syscmds[state->num++]){
+ const char *name = syscmd->name;
+ if(strncmp(name, state->word+1, state->wordlen-1) == 0){
+ int len = strlen(name);
+ char *result = malloc(sizeof(char) * (len + 2));
+ result[0] = ')';
+ memcpy(result+1, name, sizeof(char) * (len+1));
+ return result;
+ }
+ }
+
+ return NULL;
+}
diff --git a/lib/lookup_syscmd.c b/lib/lookup_syscmd.c
new file mode 100644
index 0000000..40aa19d
--- /dev/null
+++ b/lib/lookup_syscmd.c
@@ -0,0 +1,32 @@
+/* Aplwc - A Programming Language With Constraints
+ *
+ * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com>
+ *
+ * This file is part of aplwc.
+ *
+ * Aplwc 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.
+ *
+ * Aplwc 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 aplwc. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+#include <aplwc.h>
+#include "aplwc_internal.h"
+
+struct aplwc_syscmd *
+aplwc_lookup_syscmd(struct aplwc *aplwc, const char *name)
+{
+ for(struct aplwc_syscmd **syscmd = aplwc->syscmds; *syscmd; syscmd++)
+ if(strcmp((*syscmd)->name, name) == 0)
+ return *syscmd;
+ return NULL;
+}
diff --git a/lib/new.c b/lib/new.c
new file mode 100644
index 0000000..4454f47
--- /dev/null
+++ b/lib/new.c
@@ -0,0 +1,36 @@
+/* Aplwc - A Programming Language With Constraints
+ *
+ * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com>
+ *
+ * This file is part of aplwc.
+ *
+ * Aplwc 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.
+ *
+ * Aplwc 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 aplwc. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <aplwc.h>
+#include "aplwc_internal.h"
+
+struct aplwc *
+aplwc_new(void)
+{
+ struct aplwc *aplwc = malloc(sizeof(*aplwc));
+ memset(aplwc, 0, sizeof(*aplwc));
+
+ aplwc->syscmds = malloc(sizeof(aplwc->syscmds[0]));
+ aplwc->syscmds[0] = NULL;
+
+ return aplwc;
+}
diff --git a/lib/register_syscmd.c b/lib/register_syscmd.c
new file mode 100644
index 0000000..dd08a6a
--- /dev/null
+++ b/lib/register_syscmd.c
@@ -0,0 +1,39 @@
+/* Aplwc - A Programming Language With Constraints
+ *
+ * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com>
+ *
+ * This file is part of aplwc.
+ *
+ * Aplwc 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.
+ *
+ * Aplwc 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 aplwc. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <aplwc.h>
+#include "aplwc_internal.h"
+
+void
+aplwc_register_syscmd(struct aplwc *aplwc, struct aplwc_syscmd *syscmd)
+{
+ if(aplwc_lookup_syscmd(aplwc, syscmd->name))
+ return;
+
+ int n = 0;
+ for(struct aplwc_syscmd **tmp = aplwc->syscmds; *tmp; tmp++)
+ n++;
+
+ aplwc->syscmds = realloc(aplwc->syscmds, sizeof(aplwc->syscmds[0]) * (n+2));
+ aplwc->syscmds[n] = syscmd;
+ aplwc->syscmds[n+1] = NULL;
+}