-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtine.c
209 lines (186 loc) · 4.39 KB
/
tine.c
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
200
201
202
203
204
205
206
207
208
209
#include <errno.h>
#include <ctype.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include "structs.h"
#include "editor.h"
#include "util.h"
static EDITOR *editor;
static WINDOW *cmdwin;
static bool reversed = false;
static int
quit(const char *m, int rc)
{
if (editor)
closeeditor(editor);
if (cmdwin)
endwin();
if (m)
fputs(m, stderr);
exit(rc);
}
static int
setupcommand(WINDOW *w, int i)
{
(void)i;
cmdwin = w;
if (!reversed)
wbkgdset(w, A_REVERSE);
werase(w);
return OK;
}
static void
initializescreen(int i)
{
ripoffline(i, setupcommand);
if (!initscr())
quit("could not initialize screen", EXIT_FAILURE);
raw();
noecho();
nonl();
if (reversed)
wbkgdset(stdscr, A_REVERSE);
keypad(stdscr, TRUE);
keypad(cmdwin, TRUE);
intrflush(stdscr, FALSE);
}
static void
loadfile(const char *fn)
{
wchar_t *s = stows(fn, strlen(fn));
if (!s)
quit("Out of memory\n", EXIT_FAILURE);
ARG a = {.t = ARG_STRING, .s1 = s, .n1 = wcslen(s)};
errno = 0;
cmd_if(editor, &editor->docview, &a);
free(s);
}
static void
runfile(const char *fn)
{
wchar_t *s = stows(fn, strlen(fn));
if (!s)
quit("out of memory\n", EXIT_FAILURE);
ARG a = {.t = ARG_STRING, .s1 = s, .n1 = wcslen(s)};
cmd_rf(editor, &editor->docview, &a);
free(s);
}
static bool
tryrun(const char *p, const char *n)
{
char fn[FILENAME_MAX + 1] = {0};
snprintf(fn, FILENAME_MAX, "%s/%s", p, n);
if (access(fn, R_OK) == 0){
runfile(fn);
return true;
}
return false;
}
static const char *
getdef(const char *env, const char *def)
{
if (getenv(env))
return getenv(env);
return def;
}
static void
runstartup(const char *ext)
{
char def[FILENAME_MAX + 1] = {0};
char path[FILENAME_MAX + 1] = {0};
char *s = NULL;
snprintf(def, FILENAME_MAX, "%s/.config", getdef("HOME", "/"));
snprintf(path, FILENAME_MAX, "tine/%s", ext);
if (tryrun(getdef("XDG_CONFIG_HOME", def), path))
return;
if ((s = strdup(getdef("XDG_CONFIG_DIRS", "/etc/xdg")))){
for (char *n = strtok(s, ":"); n; n = strtok(NULL, ":")){
if (tryrun(n, path))
break;
}
free(s);
return;
}
}
static void
runstartupfiles(const char *fn)
{
char path[FILENAME_MAX + 1] = {0};
snprintf(path, FILENAME_MAX, "tinerc.%s", strrchr(fn, '.')? strrchr(fn, '.') + 1 : fn);
runstartup("tinerc");
runstartup(path);
}
static void
fixloc(void)
{
int y, x;
getyx(editor->focusview->w, y, x);
wmove(editor->focusview->w, y, x);
wrefresh(editor->focusview->w);
}
static void
run(void)
{
if (editor->focusview->statuscb)
editor->focusview->statuscb(editor, editor->focusview);
redisplay(editor->focusview);
while (editor->running){
fixloc();
KEYSTROKE k = getkeystroke(editor, true);
if (k.o == ERR)
continue;
dispatch(editor, editor->focusview, k);
if (editor->focusview->statuscb)
editor->focusview->statuscb(editor, editor->focusview);
redisplay(editor->focusview);
}
}
#define USAGE "usage: tine [-rtn] FILE [CMDFILE|+LINE]...\n"
int
main(int argc, char **argv)
{
setlocale(LC_ALL, "");
int o = 0, toporbot = -1;
bool runrc = true;
while ((o = getopt(argc, argv, "rtn")) != -1){
switch (o){
case 'n':
runrc = false;
break;
case 't':
toporbot = 1;
break;
case 'r':
reversed = true;
break;
default:
quit(USAGE, EXIT_FAILURE);
break;
}
}
argc -= optind;
argv += optind;
if (argc < 1)
quit(USAGE, EXIT_FAILURE);
initializescreen(toporbot);
if ((editor = openeditor(argv[0], stdscr, cmdwin)) == NULL)
return fputs("out of memory", stderr), EXIT_FAILURE;
loadfile(argv[0]);
if (runrc)
runstartupfiles(argv[0]);
enableundo(editor->docview.b);
editor->docview.b->dirty = false;
for (int i = 1; i < argc; i++){
if (argv[i][0] == '+' && isdigit(argv[i][1])){
ARG a = {.t = ARG_NUMBER, .n1 = (size_t)atol(argv[i] + 1)};
cmd_m(editor, &editor->docview, &a);
} else
runfile(argv[i]);
}
run();
return quit(NULL, EXIT_SUCCESS);
}