DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / vimscript / vimextras.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <readline/readline.h>
5 #include <sys/time.h>
6
7 /*
8 * Vim interface for the readline(3) function.
9 *
10 * Prints 'prompt' and reads a line from the input. If EOF is encountered,
11 * returns the string "E"; otherwise, returns the string "S<line>" where <line>
12 * is the line read from input.
13 *
14 * This function is not thread-safe.
15 */
16 char* vimreadline(char* prompt) {
17 static char buf[1024];
18 char* res = readline(prompt);
19 if (res) {
20 buf[0] = 'S';
21 strncpy(buf + 1, res, sizeof(buf) - 1);
22 free(res);
23 } else {
24 buf[0] = 'E';
25 buf[1] = '\0';
26 }
27 return buf;
28 }
29
30 #define UNIXTIME_BASE 1451606400 /* = Unix time of 2016-01-01 */
31
32 /*
33 * Returns the number of milliseconds since 2016-01-01 00:00:00 UTC.
34 *
35 * This date is chosen (instead of the standard 1970 epoch) so the number of
36 * milliseconds will not exceed a 32-bit integer, which is the limit for Vim
37 * number variables.
38 */
39 int vimtimems(int dummy) {
40 struct timeval tv;
41 (void) dummy; /* unused */
42 gettimeofday(&tv, NULL);
43 return (tv.tv_sec - UNIXTIME_BASE) * 1000 + (tv.tv_usec / 1000);
44 }