From a809bc0f49ee4c6e777f39d1f80ee57862a0fcab Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Wed, 1 Apr 2026 22:24:22 +0200 Subject: Initial import of the code into git --- src/Makefile.am | 18 +++++++++++ src/main.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/nls.h | 36 ++++++++++++++++++++++ src/readline.c | 56 +++++++++++++++++++++++++++++++++++ src/readline.h | 31 +++++++++++++++++++ src/syscmd.c | 36 ++++++++++++++++++++++ src/syscmd.h | 28 ++++++++++++++++++ 7 files changed, 297 insertions(+) create mode 100644 src/Makefile.am create mode 100644 src/main.c create mode 100644 src/nls.h create mode 100644 src/readline.c create mode 100644 src/readline.h create mode 100644 src/syscmd.c create mode 100644 src/syscmd.h (limited to 'src') 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 + * + * 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 . + */ + +#include +#include +#include +#include +#include +#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 .\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 + * + * 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 . + */ + +/* This file must be included after */ + +#if ENABLE_NLS +#include +#include +#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 + * + * 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 . + */ + +#include +#include +#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 + * + * 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 . + */ + +#ifndef READLINE_H +#define READLINE_H + +#include +#include +#include +#include + +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 + * + * 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 . + */ + +#include +#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 + * + * 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 . + */ + +#ifndef SYSCMD_H +#define SYSCMD_H + +#include + +void init_syscmds(struct aplwc *); + +#endif /* SYSCMD_H */ -- cgit v1.2.3