summaryrefslogtreecommitdiff
path: root/door.c
blob: 4c6fc06317b421d1819d0f72868c4336ed6ddf02 (plain) (blame)
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
#include "door.h"

#ifdef CONFIG_DOORGAMES



//just for full fill libc example
static int dir_door_game_selector (const struct dirent *unused)
{
  return 1;
}


int bbs_door_start( term_screen *ts, const char *cmd )
{
	int ret=0;

	int restore_mode=1;

	term_clr_scr( ts );

	tcsetattr( ts->ifd, TCSAFLUSH, &ts->orig_i );

	if ( system( cmd ) == -1 )
	{
		bbs_log( "DOOR", "couldnt execute doorgame");
	}

	if ( restore_mode )
	{
		tcsetattr( ts->ifd, TCSAFLUSH, &ts->raw_i );
	}


	return ret;
}

int bbs_door( term_screen *ts, const char *dir_name )
{
	int ret=0;

	int quit_loop=0;
	int menu_input = 0;
	char menu_cmd = 0;
	const char *door_game_dir=NULL;

	if ( ts == NULL )
		return ret;


	if ( dir_name == NULL )
	{
		door_game_dir = CONFIG_DOOR_DEFAULT_DIR;
	} else
	{
		door_game_dir = dir_name;
	}

	//list dir
	struct stat path_node;
	struct dirent **eps;
	int n;

	//get all articles names in list and use list every time needed
	List *dir_list = llist_new();

	bbs_log_article_list( NULL );

	if ( door_game_dir != NULL )
	{
		//should have some bugs be carefull
		n = scandir( door_game_dir, &eps, dir_door_game_selector, alphasort );
		if ( n >= 0)
		{
			int cnt;
			for ( cnt=0; cnt<n; cnt++)
			{
				sds d_name = sds_new( eps[cnt]->d_name );
				sds pathname  = sds_new( door_game_dir );
				pathname = sds_cat( pathname, d_name );
				sds_free( d_name );

				//!!!possible memleak unfreed d_name?

				//check if its file
				term_cur_set_c( ts, 0 );
				if ( stat( pathname, &path_node ) == 0 )
				{
					if ( (path_node.st_mode & S_IFREG) && (path_node.st_mode & 0111) )
					{
						llist_push( dir_list, pathname );
					} else
					{
						sds_free( pathname );
					}
				} else 
				{
					sds_free( pathname );
				}
				free( eps[cnt] );
			}
			free( eps );
		} else
		{
			term_printf( ts, "Err\n");
			ERROR("Cannot open article directory\n");
		}
		ret = 0;
	}

	while( (quit_loop == 0) )
	{
		term_clr_scr( ts );
		term_cur_set_c( ts, 0 );
		term_printf( ts, "(L)ist games, (P)lay game, (Q)uit");


		term_cur_set_c( ts, 0 );
		term_cur_set_r( ts, term_get_maxrow( ts ) );
		term_printf( ts, ":");

		menu_input = term_getc( ts );
		//if something whent wrong dont know why, need to get some test case
		if ( menu_input == -1 )
		{
			term_cur_set_c( ts, 0 );
			ERROR("Cannot read char");
			sleep(1);
			continue;
		}
		menu_cmd = (char)menu_input;

		switch( menu_cmd )
		{
			case 'q':
			case 'Q':
				quit_loop = 1;
				break;

			case 'l':
			case 'L':
				{
					term_cur_set_c( ts, 0 );
					term_cur_set_r( ts, 2 );
					//print list of executables
					struct ListNode *iter=dir_list->first;
					int cnt = 1;
					while (iter != NULL)
					{
						term_cur_set_c( ts, 0 );
						printf( "[%02d] %s\n", cnt, (char *)iter->val);
						cnt += 1;
						iter = iter->next;
					}
					term_cur_set_c( ts, 0 );
					term_print( ts, "Press ANYKEY\n", 13);
					term_getc( ts );
				}
				break;
			case 'p':
			case 'P':
				{
					term_cur_set_c( ts, 0 );
					term_printf( ts, "Input game number 1-9:");
					int game_input = term_getc( ts );
					//if something whent wrong dont know why, need to get some test case
					char game_number = (char)game_input-'0';

					if ( game_number > 0 && game_number < 10)
					{
						struct ListNode *iter = dir_list->first;
						int cnt = 1;
						while ( iter != NULL )
						{
							if ( cnt == game_number )
							{
								bbs_door_start( ts, iter->val );
								break;
							}
							cnt += 1;
							iter = iter->next;
						}
					}
				}
				break;
			default:
				term_printf( ts, "Try more\n");
		}
	}

	//needed special care to free sds strings
	//dangerous stuff as list contains sds strings then we need to call sds_free
	//not just free, maybe try to use llist_manager it should handle that
	{
		struct ListNode *iter=dir_list->first;
		while (iter != NULL)
		{
			if ( iter->val != NULL )
			{
				sds_free( iter->val );
				iter->val = NULL;
			}
			iter = iter->next;
		}

	}
	llist_free( dir_list );

	return ret;
}

#endif