summaryrefslogtreecommitdiff
path: root/lib/interface.c
blob: 3a3c6fcbf8cd9b0f3789976234a987286357e62c (plain)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "config.h"
#include "interface.h"
#include "terminal.h"
#include "exceptions.h"
#include "parser.h"
#include "pile.h"

int quit;

char * ancien = NULL;

/* Ces deux fonctions nous servent à insérer ou supprimer un caractère dans une chaîne. Utile pour l'utilisation des
touches backspace, suppr et insert */

void supprime(char * ch) {
	for (; *ch; ch++) {
		*ch = *(ch + 1);
	}
}

void inserer(char * ch, char c) {
	int i;
	
	ch[strlen(ch) + 1] = '\0';
	
	for (i = strlen(ch) - 1; i != -1; i--) {
		*(ch + i + 1) = *(ch + i);
	}
	*ch = c;
}

/* La boucle de l'interface. Même si elle a l'air longue, elle ne fait que tester les différents cas
en fonction des touches enfoncées. */

void ifloop(void) {
	int cread, i, insert = 0;
	int gotesc = 0;
	char buffer[BUFSIZ] = "";
	int position = 0;
	
	printf("> ");

	quit = 0;
	while (!quit) {
		cread = fgetc(input);
		
		if (gotesc) {
			switch (cread) {
			case 91:
				cread = fgetc(input);
				switch (cread) {
				case 49: /* Home */
					position = 0;
					break;
				case 50: /* Insert */
					insert ^= 1;
					break;
				case 52: /* End */
					printf("%s", &(buffer[position]));
					position = strlen(buffer);
					break;
				case 67: /* Droite */
					if (buffer[position]) {
						printf("%c", buffer[position++]);
					}
					break;
				case 68: /* Gauche */
					if (position) {
						printf("\010");
						position--;
					}
					break;
				default:
					gotesc = 0;
					break;
				}
				break;
			case 79:
				cread = fgetc(input);
				switch (cread) {
				case 82: /* F3 */
					for (i = 0; i < position; i++) {
						printf("\010");
					}
					strcpy(buffer, ancien);
					position = strlen(buffer);
					printf("%s", buffer);
					break;
				default:
					gotesc = 0;
					break;

				}
				break;
			default:
				gotesc = 0;
				break;
			}
			if (gotesc) {
				cread = fgetc(input);
				gotesc = 0;
			}
		}
		switch(cread) {
		case 3: /* CTRL-C */
			printf(_("*CTRL-C*\n"));
			quit = 1;
			break;
		case 8: /* backspace */
			if (position) {
				supprime(&(buffer[--position]));
				printf("\010%s ", &(buffer[position]));
				for (i = 0; i <= strlen(&(buffer[position])); i++) {
					printf("\010");
				}
			}
			break;
		case 10: /* Entrée */
			printf("\n");
			clearterm();
			if (ancien) free(ancien);
			ancien = Estrdup(buffer);
			parse_line(buffer);
			if (quit) break;
			initterm();
			position = 0;
			buffer[0] = 0;
			printf("\n");
			while (has_resultat()) {
				printf(" .   %s\n", pop_resultat());
			}
			printf("\n> ");
			global_error = 0;
			break;
		case 27: /* ESC */
			gotesc = 1;
			break;
		case 126: /* ~ */
			break;
		case 127: /* Suppr */
			if (buffer[position]) {
				supprime(&(buffer[position]));
			}
			printf("%s ", &(buffer[position]));
			for (i = 0; i <= strlen(&(buffer[position])); i++) {
				printf("\010");
			}
			break;
		default: /* Tout caractère autre, on le rajoute à la position en cours */
			if (insert) {
				inserer(&(buffer[position]), cread);
				printf("%s", &(buffer[position]));
				position++;
				for (i = 0; i < strlen(&(buffer[position])); i++) {
					printf("\010");
				}
			} else {
				printf("%c", cread);
				if (buffer[position]) {
					buffer[position++] = cread;
				} else {
					buffer[position++] = cread;
					buffer[position] = 0;
				}
			}
			break;
		}
	}
}