aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
blob: 3b7ac274765fa6fe7c608f2c27bea02c348d3dc3 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* Copyright (c) 2021 nytpu <alex@nytpu.com>
 * SPDX-License-Identifier: GPL-3.0-only
 * For more license details, see LICENSE or <https://www.gnu.org/licenses/gpl-3.0.html>.
 */

#include "buffer.h"
#include "command.h"
#include "common.h"
#include "io.h"
#include "linenoise.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <sys/ioctl.h>
#include <unistd.h>

struct Editor E;

static int
get_width(void)
{
	struct winsize ws;
	int err = ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
	if (err < 0 || ws.ws_col == 0) return DEFAULT_WIDTH;
	return ws.ws_col;
}

static void
usage(const char *cmd)
{
	fprintf(stderr,
		"Usage: %s [-hlsvV] [-p string] [file]\n\n"
		"Flags:\n"
		"\t-h           Display this usage prompt.\n"
		"\t-l           Use a \"loose exit status\"; i.e. exit 0 even "
	        "if a command fails.\n"
		"\t-s           Suppress diagnostics (if enabled), byte counts "
	        "when reading\n"
		"\t             and writing, and '!' prompt.\n"
		"\t-v           Be verbose, equivalent to the 'H' command.\n"
		"\t-V           Display version and copyright information\n"
		"\t-p 'string'  Use 'string' as the interactive prompt string "
	        "in command\n"
		"\t             mode. Also enables prompt mode. (default '*')\n"
		"\nFor more help, see ed(1), ed(1p), and "
	        "<https://git.nytpu.com/ed>\n",
		cmd);
}

int
main(int argc, char *argv[])
{
	editor_init();

	int c;
	while ((c = getopt(argc, argv, "hlp:svV")) != -1) {
		switch (c) {
		case 'h':
			usage(argv[0]);
			return EXIT_SUCCESS;
		case 'l':
			E.loose = true;
			break;
		case 'p':
			E.prompt_enabled = true;
			E.prompt = optarg;
			break;
		case 's':
			E.quiet = true;
			break;
		case 'v':
			E.verbose = true;
			break;
		case 'V':
			printf("nytpu's ed v%s\n\n"
			       "Copyright (c) 2021 nytpu <alex@nytpu.com>\n"
			       "SPDX-License-Identifier: GPL-3.0-only\n"
			       "For more license details, see LICENSE in the "
			       "source repo <https://git.nytpu.com/ed>\n",
				VERSION);
			return EXIT_SUCCESS;
		default:
			usage(argv[0]);
			return EXIT_FAILURE;
		}
	}

	if (optind < argc) { open_file(argv[optind], true); }

	char *line = NULL;
	while ((line = linenoise(E.prompt_enabled ? E.prompt : "")) != NULL) {
		struct command cmd;
		int ret = parse_command(&cmd, line);
		if (ret < 0) continue;

		switch (cmd.command) {
		case 'a':
			insert_mode(cmd.end + 1);
			break;
		case 'c':
			if (cmd.start == 0) cmd.start = 1;
			if (cmd.end == 0) cmd.end = 1;
			delete_rows(cmd.start, cmd.end);
			insert_mode(cmd.start);
			break;
		case 'd':
			delete_rows(cmd.start, cmd.end);
			break;
		case 'h':
			fprintf(stderr, "%s\n", E.last_err);
			break;
		case 'H':
			E.verbose = !E.verbose;
			if (E.verbose) fprintf(stderr, "%s\n", E.last_err);
			break;
		case 'i':
			if (cmd.end == 0) cmd.end = 1;
			insert_mode(cmd.end);
			break;
		case 'l':
			print_rows_escaped(cmd.start, cmd.end);
			break;
		case 'n':
			print_rows_numbered(cmd.start, cmd.end);
			break;
		case 'p':
			print_rows(cmd.start, cmd.end);
			break;
		case 'P':
			E.prompt_enabled = !E.prompt_enabled;
			break;
		case 'q':
			if (E.dirty) {
				error("Buffer modified");
				break;
			}
			// fallthrough
		case 'Q':
			free(line);
			goto quit;
		case '=':
			printf("%ld\n", cmd.end);
			break;
		case '\0':
			print_rows(cmd.end, cmd.end);
			break;
		default:
			error("Unknown command");
			break;
		}

		linenoiseHistoryAdd(line);
		free(line);
	}

quit:
	editor_deinit();
	return EXIT_SUCCESS;
}

noreturn void
die(const char *m)
{
	editor_deinit();
	perror(m);
	exit(EXIT_FAILURE);
}

void
editor_init(void)
{
	// initialize editor struct
	E.prompt = "*";
	E.scols = get_width();
	E.last_err = "";
	// all other fields are automatically initialized to zero, false, or
	// NULL

	// linenoise setup
	linenoiseHistorySetMaxLen(50);
}

void
editor_deinit(void)
{
	for (int i = 0; i < E.numrows; ++i) { free(E.rows[i].chars); }
	free(E.rows);
	free(E.filename);
}

void
error(const char *msg)
{
	printf("?\n");
	if (E.verbose) fprintf(stderr, "%s\n", msg);
	E.last_err = msg;
}