-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterminal.c
453 lines (411 loc) · 12.5 KB
/
terminal.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* @file
* Terminal interface for sudoku solver.
*/
/***********************************************************************************
* Author: Laurent Farhi *
* Name: terminal.c *
* Language: C *
* Copyright (C) 2009, All rights reserved. *
* *
* LICENSE: *
* 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 2 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, write to the Free Software *
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
***********************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include "solve.h"
#include "terminal.h"
/// Display mode.
static display sudokuDisplay;
/// Flag indicating that a user input is required before proceeding.
static int askAgain = 1;
/// Event handler to display the sudoku grid.
/// @param [in] id Grid identifier
/// @param [in] evt_args Event to be processed
static void
grid_print (uintptr_t id, sudoku_grid_event_args evt_args)
{
static int nbCells;
int ok[GRID_SIZE][GRID_SIZE];
for (int l = 0; l < GRID_SIZE; l++)
for (int c = 0; c < GRID_SIZE; c++)
{
ok[l][c] = 0;
for (int v = 0; v < GRID_SIZE; v++)
if (evt_args.grid[l][c][v])
{
if (!ok[l][c])
ok[l][c] = evt_args.grid[l][c][v];
else
{
ok[l][c] = 0;
break;
}
}
}
display d = 0;
if (sudokuDisplay == RULES)
d = RULES;
else if (nbCells && (evt_args.nbCells != GRID_SIZE * GRID_SIZE) && (sudokuDisplay & VERBOSE))
d = VERBOSE;
else if (!nbCells || (evt_args.nbCells == GRID_SIZE * GRID_SIZE)
|| ((sudokuDisplay & NORMAL) && (evt_args.nbCells != nbCells)))
d = NORMAL;
if (d == VERBOSE)
{
printf ("Grid #%u:\n", (unsigned int) id);
printf ("[%3i]", evt_args.nbCells);
for (int i = 0; i < GRID_SIZE; i++)
{
printf (" ");
for (int j = 0; j < (SQUARE_SIZE - 1) / 2; j++)
printf (" ");
printf ("%c", sudoku_grid_referential.column_name[i]);
for (int j = 0; j < SQUARE_SIZE - (SQUARE_SIZE - 1) / 2 - 1; j++)
printf (" ");
}
printf ("\n");
for (int i = 0; i < GRID_SIZE * GRID_SIZE * GRID_SIZE; i++)
{
int l = (i / (GRID_SIZE * SQUARE_SIZE)) / SQUARE_SIZE;
int c = (i % (GRID_SIZE * SQUARE_SIZE)) / SQUARE_SIZE;
int v = i % SQUARE_SIZE + ((i / (GRID_SIZE * SQUARE_SIZE)) % SQUARE_SIZE) * SQUARE_SIZE + 1;
if (i % (GRID_SIZE * SQUARE_SIZE * SQUARE_SIZE * SQUARE_SIZE) == 0)
{
printf (" +");
for (int i = 0; i < GRID_SIZE; i++)
{
for (int j = 0; j < SQUARE_SIZE; j++)
printf ("-");
printf ("+");
}
printf ("\n");
}
else if (i % (GRID_SIZE * SQUARE_SIZE * SQUARE_SIZE) == 0)
{
printf (" +");
for (int i = 0; i < GRID_SIZE; i++)
{
for (int j = 0; j < SQUARE_SIZE; j++)
printf (".");
printf ("+");
}
printf ("\n");
}
if ((i + (SQUARE_SIZE - 1) * GRID_SIZE * SQUARE_SIZE) % (SQUARE_SIZE * SQUARE_SIZE * SQUARE_SIZE * SQUARE_SIZE) ==
0)
printf (" %c |", sudoku_grid_referential.row_name[i / (GRID_SIZE * GRID_SIZE)]);
else if (i % (SQUARE_SIZE * SQUARE_SIZE * SQUARE_SIZE) == 0)
printf (" |");
if (!ok[l][c]) // Display all candidates
{
if (evt_args.grid[l][c][v - 1])
printf ("%c", sudoku_grid_referential.value_name[evt_args.grid[l][c][v - 1] - 1]);
else
printf (" ");
}
else // Display one single candidate
{
if (v == (GRID_SIZE + 1) / 2 - (GRID_SIZE % 2 ? 0 : SQUARE_SIZE / 2))
printf ("%c", sudoku_grid_referential.value_name[ok[l][c] - 1]);
else
printf (" ");
}
if ((i + 1) % (GRID_SIZE * SQUARE_SIZE) == 0)
printf ("|\n");
else if ((i + 1) % (SQUARE_SIZE * SQUARE_SIZE) == 0)
printf ("|");
else if ((i + 1) % SQUARE_SIZE == 0)
printf (":");
}
printf (" +");
for (int i = 0; i < GRID_SIZE; i++)
{
for (int j = 0; j < SQUARE_SIZE; j++)
printf ("-");
printf ("+");
}
printf ("\n");
printf ("\n");
askAgain = 1;
}
else if (d == NORMAL)
{
nbCells = evt_args.nbCells;
printf ("Grid #%u:\n", (unsigned int) id);
printf ("[%3i]", evt_args.nbCells);
for (int i = 0; i < GRID_SIZE; i++)
printf (" %c", sudoku_grid_referential.column_name[i]);
printf ("\n");
for (int l = 0; l < GRID_SIZE; l++)
{
if (l % SQUARE_SIZE == 0)
{
printf (" +");
for (int i = 0; i < SQUARE_SIZE; i++)
{
for (int j = 0; j < 2 * SQUARE_SIZE - 1; j++)
printf ("-");
printf ("+");
}
printf ("\n");
}
printf (" %c |", sudoku_grid_referential.row_name[l]);
for (int c = 1; c <= GRID_SIZE; c++)
{
int val = 0;
for (int v = 1; v <= GRID_SIZE; v++)
{
if (evt_args.grid[l][c - 1][v - 1])
{
if (val)
{
v = GRID_SIZE;
val = 0;
}
else
val = evt_args.grid[l][c - 1][v - 1];
}
}
if (val)
printf ("%c", sudoku_grid_referential.value_name[val - 1]);
else
printf (".");
if (c % SQUARE_SIZE == 0)
printf ("|");
else
printf (" ");
}
printf ("\n");
}
printf (" +");
for (int i = 0; i < SQUARE_SIZE; i++)
{
for (int j = 0; j < 2 * SQUARE_SIZE - 1; j++)
printf ("-");
printf ("+");
}
printf ("\n");
printf ("\n");
askAgain = 1;
}
else if (d == RULES)
{
nbCells = evt_args.nbCells;
printf ("Grid #%u: [%2i] ", (unsigned int) id, evt_args.nbCells);
for (int l = 0; l < GRID_SIZE; l++)
{
for (int c = 0; c < GRID_SIZE; c++)
{
int val = 0;
for (int v = 1; v <= GRID_SIZE; v++)
{
if (evt_args.grid[l][c][v - 1])
{
if (val)
{
v = GRID_SIZE;
val = 0;
}
else
val = evt_args.grid[l][c][v - 1];
}
}
printf ("%c", val ? sudoku_grid_referential.value_name[val - 1] : '.');
}
}
printf ("\n");
askAgain = 1;
}
}
/// Event handler for interactive mode.
/// @param [in] id Grid identifier
/// @param [in] evt_args Event to be processed
static void
ask (uintptr_t id, sudoku_grid_event_args evt_args)
{
if (!askAgain)
return;
else
askAgain = 0;
printf ("====================================================================\n");
printf ("g[o]/n[ext]/v[erbosity]/r[ules]/q[uit]?[n]");
while (1 == 1)
{
int c = tolower (getchar ());
if (c == 'g')
{
printf ("\nConfirm (y[es]/n[o])?[n]");
if (tolower (getchar ()) == 'y')
{
sudoku_grid_event_handler_remove (ON_CHANGE, ask);
terminal_unset ();
}
printf ("\n");
return;
}
else if (c == 'r')
{
printf ("\n");
if (terminal_display_get () & RULES)
terminal_display_set (terminal_display_get () & ~RULES);
else
terminal_display_set (terminal_display_get () | RULES);
}
else if (c == 'v')
{
printf ("\n");
if (terminal_display_get () & NORMAL)
terminal_display_set ((terminal_display_get () & ~NORMAL) | VERBOSE);
else if (terminal_display_get () & VERBOSE)
terminal_display_set (terminal_display_get () & ~(NORMAL | VERBOSE));
else
terminal_display_set (terminal_display_get () | NORMAL);
}
else if (c == '\n' || c == 'n')
{
printf ("\n");
return;
}
else if (c == 'q')
{
printf ("\nConfirm (y[es]/n[o])?[n]");
if (tolower (getchar ()) == 'y')
{
printf ("\n");
exit (0);
}
else
{
printf ("\n");
return;
}
}
}
}
/// Message handler to display a message.
/// @param [in] id Grid identifier
/// @param [in] msg_args Message to be processed
static void
print_message (uintptr_t id, sudoku_message_args msg_args)
{
if ((sudokuDisplay & VERBOSE) || ((sudokuDisplay & RULES) && (msg_args.verbosity <= 2)) || msg_args.verbosity <= 0)
{
if (id)
printf ("Grid #%u:\n", (unsigned int) id);
printf ("%s", msg_args.rule);
askAgain = 1;
}
}
/**************************************************/
/* Terminal manager */
/**************************************************/
/// Previous terminal setting to restore them at the end of execution.
static struct termios old_tio;
/// Flag indicating the terminal has been configured.
static int termios_init = 0;
/// User terminal (standard input stream, that is keyboard) initialization.
static void
terminal_init (void)
{
if (termios_init)
return;
signal (SIGTERM, SIG_IGN);
signal (SIGQUIT, SIG_IGN);
signal (SIGHUP, SIG_IGN);
/* unbuffered_input */
struct termios new_tio;
/* get the terminal settings for stdin */
if (tcgetattr (STDIN_FILENO, &old_tio))
return;
termios_init = 1;
/* we want to keep the old setting to restore them at the end */
new_tio = old_tio;
/* disable canonical mode (buffered i/o) and local echo */
new_tio.c_lflag &= (~ICANON & ~ECHO);
/* set the new settings immediately */
tcsetattr (STDIN_FILENO, TCSANOW, &new_tio);
}
/// Restore terminal to its initial state.
static void
terminal_end (void)
{
if (!termios_init)
return;
/* restore the former settings */
tcsetattr (STDIN_FILENO, TCSANOW, &old_tio);
termios_init = 0;
}
/// Set terminal (standard input and standard output streams, that are keyboard and terminal screen) for interactive mode.
/// @param[in] iflag 1 for interactive mode
void
terminal_set (int iflag)
{
// set handlers
sudoku_grid_event_handler_add (ON_INIT | ON_SOLVED, grid_print);
sudoku_message_handler_add (print_message);
if (iflag)
{
struct termios st;
if (tcgetattr (STDIN_FILENO, &st) < 0)
fprintf (stderr, "No standard input available. Interactive mode disabled.\n");
else if (tcgetattr (STDOUT_FILENO, &st) < 0)
fprintf (stderr, "No standard output available. Interactive mode disabled.\n");
else
{
terminal_init ();
sudoku_grid_event_handler_add (ON_CHANGE, ask);
}
}
else
{
terminal_end ();
sudoku_grid_event_handler_remove (ON_CHANGE, ask);
}
}
/// Reset terminal.
void
terminal_unset (void)
{
terminal_end ();
}
/// Get display mode.
/// @return Display mode
display
terminal_display_get (void)
{
return (sudokuDisplay);
}
/// Set display mode.
/// @param [in] d mode to set.
/// @return Previous display mode.
display
terminal_display_set (display d)
{
sudoku_message_handler_remove (print_message);
sudoku_grid_event_handler_remove (ON_CHANGE, grid_print);
sudoku_message_handler_add (print_message);
if ((d & NORMAL) || (d & VERBOSE))
sudoku_grid_event_handler_add (ON_CHANGE, grid_print);
printf ("Display mode :%s%s%s%s.\n", (d ? "" : " NONE"), (d & NORMAL ? " GRIDS" : ""),
(d & VERBOSE ? " CANDIDATES" : ""), (d & RULES ? " RULES" : ""));
display old = sudokuDisplay;
sudokuDisplay = d;
return (old);
}