File: console.c

package info (click to toggle)
gniall 0.7.1-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 580 kB
  • sloc: ansic: 1,195; sh: 329; makefile: 22
file content (156 lines) | stat: -rw-r--r-- 3,367 bytes parent folder | download | duplicates (6)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "niall.h"

#define BUFFER_SIZE	256

#define FALSE		0
#define TRUE		1

#define ACT_ERROR	0
#define ACT_NONE	1
#define ACT_EXIT	2
#define ACT_LIST	3
#define ACT_NEW		4
#define ACT_LOAD	5
#define ACT_SAVE	6
#define ACT_CORRECT	7
#define ACT_HELP	8

void Niall_Print( char *fmt, ... )
{
	va_list ap;
	va_start( ap, fmt );
	vfprintf( stderr, fmt, ap );
	va_end( ap );
}

void Niall_Warning( char *fmt, ... )
{
	va_list ap;

	fprintf( stderr, "%s: ", "Warning" );
	va_start( ap, fmt );
	vfprintf( stderr, fmt, ap );
	va_end( ap );
	fprintf( stderr,"\n");
}

void Niall_Error( char *fmt, ... )
{
	va_list ap;

	fprintf( stderr, "%s: ", "Error" );
	va_start( ap, fmt );
	vfprintf( stderr, fmt, ap );
	va_end( ap );
	fprintf( stderr,"\n");
	exit(EXIT_FAILURE);
}

int Action(char *Buffer)
{
	int i;

	if(feof(stdin))
	{
		printf("#exit\n");
		return(ACT_EXIT);
	}
	if(Buffer[0]!='#')	        return(ACT_NONE);
	for(i=0;i<strlen(Buffer);i++)	if(isspace(Buffer[i])) Buffer[i]=0;

	if(!strcmp(&Buffer[1],"exit"))		return(ACT_EXIT);
	if(!strcmp(&Buffer[1],"list"))		return(ACT_LIST);
	if(!strcmp(&Buffer[1],"new"))		return(ACT_NEW);
	if(!strcmp(&Buffer[1],"load"))		return(ACT_LOAD);
	if(!strcmp(&Buffer[1],"save"))		return(ACT_SAVE);
	if(!strcmp(&Buffer[1],"correct")) 	return(ACT_CORRECT);
	if(!strcmp(&Buffer[1],"help"))	 	return(ACT_HELP);

	return(ACT_ERROR);
}

int main(void)
{
	char Buffer[BUFFER_SIZE];
	char Buffer2[BUFFER_SIZE];
	char exit_flag=FALSE;

	Niall_Init();

	do
	{
		printf("User:  ");
		fflush(stdout);
		fgets(Buffer,BUFFER_SIZE,stdin);
		if(strlen(Buffer)<1) return;

		Buffer[strlen(Buffer)-1]=0;
		switch(Action(Buffer))
		{
		    case ACT_NONE:
			Niall_Learn(Buffer);
			Niall_Reply(Buffer,BUFFER_SIZE);
			printf("Niall: %s\n",Buffer);
			break;
		    case ACT_EXIT:
			printf("Niall: Bye...\n");
			exit_flag=TRUE;
			break;
		    case ACT_LIST:
			Niall_ListDictionary();
			break;
		    case ACT_NEW:
			Niall_NewDictionary();
			break;
		    case ACT_LOAD:
			printf(" Filename to LOAD: ");
			fflush(stdout);
			fgets(Buffer,BUFFER_SIZE,stdin);
			if(strlen(Buffer)<1) return;
			Buffer[strlen(Buffer)-1]=0;
			Niall_LoadDictionary(Buffer);
			break;
		    case ACT_SAVE:
			printf(" Filename to SAVE: ");
			fflush(stdout);
			fgets(Buffer,BUFFER_SIZE,stdin);
			if(strlen(Buffer)<1) return;
			Buffer[strlen(Buffer)-1]=0;
			Niall_SaveDictionary(Buffer);
			break;
		    case ACT_CORRECT:
			printf(" Enter incorrect word: ");
			fflush(stdout);
			fgets(Buffer,BUFFER_SIZE,stdin);
			if(strlen(Buffer)<1) return;
			Buffer[strlen(Buffer)-1]=0;
			printf(" Enter correct spelling: ");
			fflush(stdout);
			fgets(Buffer2,BUFFER_SIZE,stdin);
			if(strlen(Buffer2)<1) return;
			Buffer2[strlen(Buffer2)-1]=0;
			Niall_CorrectSpelling(Buffer,Buffer2);
			break;
		    case ACT_HELP:
			printf("\n\t#new     - Clear the dictionary.");
			printf("\n\t#load    - Load a dictionary.");
			printf("\n\t#save    - Save the dictionary.");
			printf("\n\t#list    - View the dictionary.");
			printf("\n\t#correct - Correct a spelling.");
			printf("\n\t#help    - This help.");
			printf("\n\t#exit    - Quit Niall.");
			printf("\n\n");
			break;
		    default:
			printf("  Unknown command - type #help for help\n");
		}
	}
	while(!exit_flag);

	Niall_Free();
}