summaryrefslogtreecommitdiff
path: root/src/main.c
blob: f163558bd7e132983a56006d2dcc9d330e0f0635 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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);
}