summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am18
-rw-r--r--src/main.c92
-rw-r--r--src/nls.h36
-rw-r--r--src/readline.c56
-rw-r--r--src/readline.h31
-rw-r--r--src/syscmd.c36
-rw-r--r--src/syscmd.h28
7 files changed, 297 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..b9463d4
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,18 @@
+bin_PROGRAMS = aplwc
+dist_man_MANS = aplwc.1
+
+aplwc_SOURCES = \
+ main.c \
+ nls.h \
+ readline.c \
+ readline.h \
+ syscmd.c \
+ syscmd.h
+
+aplwc_CPPFLAGS = $(READLINE_CFLAGS) -DLOCALEDIR=\"$(localedir)\" -I$(top_srcdir)/lib -I$(top_srcdir)/gnu -I$(top_builddir)/gnu
+aplwc_LDADD = $(LTLIBINTL) ../lib/libaplwc.la ../gnu/libgnu.la $(READLINE_LIBS)
+
+CLEANFILES = aplwc.1
+
+aplwc.1: aplwc
+ $(AM_V_GEN)help2man --output=$@ $(top_builddir)/src/aplwc$(EXEEXT)
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..f163558
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,92 @@
+/* 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 <config.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <aplwc.h>
+#include "nls.h"
+#include "syscmd.h"
+#include "readline.h"
+
+static struct option long_options[] = {
+ {"help", no_argument, 0, 'h'},
+ {"version", no_argument, 0, 'v'},
+ {NULL, 0, NULL, 0}
+};
+
+static void version(void);
+static void help(int);
+
+int
+main(int argc, char *argv[])
+{
+ init_nls();
+
+ int opt;
+ while((opt = getopt_long(argc, argv, "hv", long_options, NULL)) != -1){
+ switch(opt){
+ case 'h':
+ help(EXIT_SUCCESS);
+ break;
+ case 'v':
+ version();
+ break;
+ default:
+ help(EXIT_FAILURE);
+ break;
+ }
+ }
+
+ struct aplwc *aplwc = aplwc_new();
+ init_syscmds(aplwc);
+ init_readline(aplwc);
+
+ char *line;
+ while(line = readline(" ")){
+ if(strlen(line) == 0)
+ continue;
+ add_history(line);
+ printf(_("You typed: %s\n"), line);
+ free(line);
+ }
+ printf(_("bye\n"));
+}
+
+static void
+version(void)
+{
+ printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
+ printf(_("Copyright (C) 2026 Peter Mikkelsen\n"));
+ printf(_("License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.\n"
+ "This is free software: you are free to change and redistribute it.\n"
+ "There is NO WARRANTY, to the extent permitted by law.\n"));
+ exit(EXIT_SUCCESS);
+}
+
+static void
+help(int status)
+{
+ printf(_("Usage: %s [OPTION...]\n\n"), PACKAGE_NAME);
+ printf(_(" -h, --help display this help and exit\n"));
+ printf(_(" -v, --version display version information and exit\n"));
+ exit(status);
+}
diff --git a/src/nls.h b/src/nls.h
new file mode 100644
index 0000000..363fa0a
--- /dev/null
+++ b/src/nls.h
@@ -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/>.
+ */
+
+/* This file must be included after <config.h> */
+
+#if ENABLE_NLS
+#include <locale.h>
+#include <libintl.h>
+#define _(S) dgettext(PACKAGE, S)
+#define init_nls() \
+ do{ \
+ setlocale(LC_ALL, ""); \
+ bindtextdomain(PACKAGE, LOCALEDIR); \
+ textdomain(PACKAGE); \
+ }while(0)
+#else
+#define _(S) S
+#define init_nls() (void)0
+#endif
diff --git a/src/readline.c b/src/readline.c
new file mode 100644
index 0000000..bda4df9
--- /dev/null
+++ b/src/readline.c
@@ -0,0 +1,56 @@
+/* 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 <config.h>
+#include <stdlib.h>
+#include "readline.h"
+
+static struct aplwc *aplwc;
+static char **completions;
+static int completion_index;
+
+static char **complete(const char *, int, int);
+static char *get_completion(const char *, int);
+
+void
+init_readline(struct aplwc *_aplwc)
+{
+ aplwc = _aplwc;
+ rl_readline_name = "aplwc";
+ rl_attempted_completion_function = complete;
+}
+
+static char **
+complete(const char *text, int start, int end)
+{
+ rl_attempted_completion_over = 1;
+ completions = aplwc_autocomplete(aplwc, rl_line_buffer, start, end);
+ char **result = NULL;
+ if(completions)
+ result = rl_completion_matches(text, get_completion);
+ free(completions);
+ return result;
+}
+
+static char *
+get_completion(const char *text, int state)
+{
+ return completions[state];
+}
diff --git a/src/readline.h b/src/readline.h
new file mode 100644
index 0000000..92045d6
--- /dev/null
+++ b/src/readline.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 READLINE_H
+#define READLINE_H
+
+#include <stdio.h>
+#include <readline/readline.h>
+#include <readline/history.h>
+#include <aplwc.h>
+
+void init_readline(struct aplwc *);
+
+#endif /* READLINE_H */
diff --git a/src/syscmd.c b/src/syscmd.c
new file mode 100644
index 0000000..6c3d2e5
--- /dev/null
+++ b/src/syscmd.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 <config.h>
+#include "syscmd.h"
+
+static struct aplwc_syscmd syscmds[] = {
+ {.name = "help"},
+ {.name = "version"},
+ {.name = "reset"},
+ {.name = "exit"},
+};
+
+void
+init_syscmds(struct aplwc *aplwc)
+{
+ for(int i = 0; i < (sizeof(syscmds)/sizeof(*syscmds)); i++)
+ aplwc_register_syscmd(aplwc, &syscmds[i]);
+}
diff --git a/src/syscmd.h b/src/syscmd.h
new file mode 100644
index 0000000..db4a874
--- /dev/null
+++ b/src/syscmd.h
@@ -0,0 +1,28 @@
+/* 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 SYSCMD_H
+#define SYSCMD_H
+
+#include <aplwc.h>
+
+void init_syscmds(struct aplwc *);
+
+#endif /* SYSCMD_H */