-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_input_regx.c
61 lines (51 loc) · 1.29 KB
/
user_input_regx.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <pcre.h>
int guessletter(char *word) {
/*
this function uses PCRE regex to allow input
only of letter for the hangman
pcre_example.c was used for regex usage example
*/
char *regex;
pcre *reCompiled;
const char *pcreErrorStr;
int pcreErrorOffset;
int subStrVec[30];
int pcreExecRet;
char letter[2];
// compile regex
regex = "[a-z]";
reCompiled = pcre_compile(regex, 0, &pcreErrorStr, &pcreErrorOffset, NULL);
if (reCompiled == NULL) {
printf("ERROR: Could not compile '%s': %s\n", regex, pcreErrorStr);
exit(1);
}
/* use system call to make terminal send all keystrokes directly to stdin */
system ("/bin/stty raw");
do
{
//printf("\033[2J"); //clear screen
printf("\rGuess a letter: \b");
fgets(letter, 2, stdin);
pcreExecRet = pcre_exec(reCompiled,
NULL, // no regex optimizations
letter,
2, // length of string
0, // Start looking at this point
0, // OPTIONS
subStrVec,
30); // Length of subStrVec
} while (pcreExecRet <= 0);
system ("/bin/stty cooked");
pcre_free(reCompiled);
printf("\nDebug: %c\n", letter[0]);
return 0;
}
void main() {
char *word;
guessletter(word);
}