File: edit.c

package info (click to toggle)
nodau 0.3.8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 176 kB
  • ctags: 88
  • sloc: ansic: 1,153; makefile: 47
file content (343 lines) | stat: -rw-r--r-- 7,162 bytes parent folder | download | duplicates (3)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/************************************************************************
* edit.c
* nodau console note taker
* Copyright (C) Lisa Milne 2010-2013 <lisa@ltmnet.com>
*
* edit.c 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.
*
* edit.c 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 this program.  If not, see <http://www.gnu.org/licenses/>
*
* In addition, as a special exception, the copyright holder (Lisa Milne)
* gives permission to link the code of this release of nodau with the
* OpenSSL project's "OpenSSL" library (or with modified versions of it
* that use the same license as the "OpenSSL" library), and distribute
* the linked executables. You must obey the GNU General Public License
* in all respects for all of the code used other than "OpenSSL". If you
* modify this file, you may extend this exception to your version of the
* file, but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version.
************************************************************************/

/* so that asprintf works */
#define _GNU_SOURCE
#include <unistd.h>
#include <ncurses.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>

#include "nodau.h"

/* local storage for note name and date */
static char* bname;
static char* bdate;

/* draw to the screen */
static void draw(char* data)
{
	/* clear the screen */
	clear();
	/* print the name and date in bold */
	attron(A_BOLD);
	printw("%s (%s):\n",bname,bdate);
	attroff(A_BOLD);
	/* print the note body */
	printw("%s",data);
	/* refresh the screen */
	refresh();
}

/* edit a note with the builtin editor */
static int edit_builtin(char* name, char* date, char* data)
{
	char buffer[256];
	int bl;
	/* still editing? */
	int quit = 0;
	/* character storage */
	int plch = 0;
	int lch = 0;
	int ch = 0;

	/* set the local data */
	bname = name;
	bdate = date;
	/* find the buffer length */
	bl = strlen(data);
	/* create the buffer */
	/* fill the buffer with 0's */
	memset(&buffer,0,256);

	/* if the note is too long, shorten it */
	if (bl > 255) {
		data[255] = 0;
		bl = 255;
	}

	/* put the note into the buffer */
	sprintf(buffer, "%s", data);

	/* init ncurses */
	initscr();
	/* no line buffering */
	cbreak();
	/* get all the keys */
	keypad(stdscr, TRUE);
	/* don't echo keypresses */
	noecho();

	/* while we are editing */
	while (!quit) {
		/* draw the screen */
		draw(buffer);
		/* set previous last char to last char */
		plch = lch;
		/* set last char to char */
		lch = ch;
		/* get char */
		ch = getch();
		/* if it's printable or newline */
		if (isprint(ch) || ch == '\n') {
			bl++;
			/* if the note is under 255 chars, add the char */
			if (bl < 255) {
				buffer[bl-1] = ch;
				buffer[bl] = 0;
			}
		/* backspace means delete a char */
		}else if (ch == 127 || ch == KEY_BACKSPACE) {
			/* if we've got one to delete */
			if (bl > 0) {
				bl--;
				buffer[bl] = 0;
			}
		}

		/* check for newline dot exit */
		if (plch == '\n' && lch == '.' && ch == '\n') {
			/* don't include the dot in the note */
			bl -= 3;
			buffer[bl] = 0;
			quit = 1;
		/* check for escape exit */
		}else if (ch == 27) {
			quit = 1;
		}
	}

	/* exit curses */
	endwin();

	/* save the note */
	if (!db_update(name,buffer))
		return 1;

	/* let the user know */
	printf("%s saved\n",name);

	return 0;
}

/* edit with an external editor */
static int edit_ext(char* editor, char* name, char* date, char* data)
{
	int fd;
	int st;
	int sz;
	char* b;
	char* l;
	char buff[512];
	pid_t pid;

	strcpy(buff,"/tmp/nodau.XXXXXX");
	fd = mkstemp(buff);

	if (fd < 0)
		return 1;

	pid = fork();

	if (pid < 0) {
		return 1;
	}else if (pid) {
		close(fd);
		waitpid(pid,&st,0);
		if (!st) {
			if ((fd = open(buff,O_RDONLY)) < 0)
				return 1;
			/* find the file length */
			sz = lseek(fd,0,SEEK_END);
			lseek(fd,0,SEEK_SET);
			if (sz) {
				/* load the note into memory */
				b = alloca(sz+1);
				if (sz != read(fd,b,sz))
					return 1;
				close(fd);
				/* delete the file */
				remove(buff);
				b[sz] = 0;
				/* find the note data */
				l = strstr(b,"-----");
				if (l) {
					/* save the note */
					l += 6;
					if (db_update(name,l))
						return 1;

					/* let the user know */
					printf("%s saved\n",name);
				}
			}
		}
		return st;
	}

	sz = strlen(name)+strlen(date)+strlen(data)+50;
	b = alloca(sz);

	/* insert data into file */
	sz = sprintf(
		b,
		"%s (%s)\nText above this line is ignored\n-----\n%s",
		name,
		date,
		data
	);
	if (write(fd,b,sz) != sz) {
		exit(1);
	}
	fsync(fd);
	close(fd);

	st = execl(editor,editor,buff,(char*)NULL);

	/* we should only ever get here if something goes wrong with exec */
	exit(st);

	/* and we shouldn't ever get here, but it stops the compiler complaining */
	return 1;
}

/* edit a note using data piped from stdin */
int edit_stdin(char* name, char* date, char* data, int append)
{
	char buff[1024];
	int l;
	int s;
	int r;
	char* d;
	char* b;

	/* get the initial buffer size */
	l = strlen(data);
	if (l < 512) {
		s = 512;
	}else{
		s = l*2;
	}

	d = malloc(s);
	if (!d)
		return 1;

	/* for append mode copy the old data to the start of the buffer */
	if (append && strcmp(data,"new entry")) {
		strcpy(d,data);
	}else{
		l = 0;
	}

	/* read it in */
	while ((r = read(STDIN_FILENO,buff,1024)) > 0) {
		/* extend the buffer as necessary */
		if (l+r+1 > s) {
			s = l+r+512;
			b = realloc(d,s);
			if (!b)
				return 1;
			d = b;
		}
		memcpy(d+l,buff,r);
		l += r;
	}

	/* make sure there's room for the nul byte */
	if (l+1 > s) {
		s = l+1;
		b = realloc(d,s);
		if (!b)
			return 1;
		d = b;
	}

	d[l] = 0;

	/* done */
	return db_update(name,d);
}

/* edit a note */
int edit(char* name, char* date, char* data)
{
	char* ed;
	char* pt;
	char* editor;
	char* p = NULL;
	struct stat st;

	if (!isatty(STDIN_FILENO))
		return edit_stdin(name,date,data,0);

	pt = getenv("PATH");

	ed = config_read("external_editor",NULL);
	if (!ed)
		ed = getenv("EDITOR");

	/* no editor or no path, use builtin */
	if (config_read("force_builtin_editor","true") || !ed || (ed[0] != '/' && !pt))
		return edit_builtin(name,date,data);

	/* find the executable */
	if (ed[0] == '/') {
		stat(ed,&st);
		/* check it exists */
		if (S_ISREG(st.st_mode)) {
			p = ed;
			editor = strdup(ed);
		}
	}else{
		p = strtok(pt,":");
		while (p) {
			p = strtok(NULL,":");

			if (asprintf(&editor,"%s/%s",p,ed) < 0)
				continue;

			stat(editor,&st);
			/* check it exists */
			if (S_ISREG(st.st_mode))
				break;

			free(editor);
		}
	}

	/* no executable, or fails to run, use builtin */
	if (!p || edit_ext(editor,name,date,data))
		return edit_builtin(name,date,data);

	return 0;
}