-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgiac_history.c
101 lines (80 loc) · 2.84 KB
/
giac_history.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
/*
* giac_history.c
* Copyright (C) Harald Milz <hm@seneca.muc.de> 2024
*
* Command line history extension for giac, the XCAS cli
*
* compile with
* gcc -shared -fPIC -o giac_history.so giac_history.c
* and use like
* LD_PRELOAD=/path/to/giac_history.so giac
*
* This program 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.
*
* This program 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/>.
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define MAXLEN 256
char histfile[MAXLEN];
int histfilesize;
typedef void (*original_add_history_ptr)();
typedef void (*original_using_history_ptr)();
// void add_history (const char *string)
void add_history (const char *string) {
// Get a pointer to the original function
original_add_history_ptr original_add_history = (original_add_history_ptr)dlsym(RTLD_NEXT, "add_history");
if (original_add_history) {
// Call the original function
original_add_history(string);
} else {
fprintf(stderr, "Failed to get pointer to original_add_history()\n");
}
append_history(1, histfile);
history_truncate_file (histfile, histfilesize);
}
// void using_history (void)
void using_history (void) {
char histfile[MAXLEN];
int fd;
// Get a pointer to the original function
original_using_history_ptr original_using_history = (original_using_history_ptr)dlsym(RTLD_NEXT, "using_history");
if (original_using_history) {
// Call the original function
original_using_history();
} else {
fprintf(stderr, "Failed to get pointer to original_using_history()\n");
}
if (getenv("GIAC_HISTFILE")) {
strncpy (histfile, getenv("GIAC_HISTFILE"), MAXLEN-1);
} else {
snprintf (histfile, MAXLEN-1, "%s/.giac_history", getenv("HOME")); // default
}
if (getenv("GIAC_HISTFILESIZE")) {
histfilesize = atoi(getenv("GIAC_HISTFILESIZE"));
} else {
histfilesize = 100; // default
}
// create a histfile if none exists
fd = open(histfile, O_CREAT | O_RDONLY, 0644);
close (fd);
read_history (histfile);
}