annotate lwbasic/input.c @ 24:421d7ceb4d86

Fixed missing return value
author lost@l-w.ca
date Mon, 24 Jan 2011 18:31:07 -0700
parents 25a4aef9c5ed
children 87590f43e76d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
1 /*
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
2 input.c
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
3
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
4 Copyright © 2011 William Astle
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
5
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
6 This file is part of LWTOOLS.
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
7
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
11 version.
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
12
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
16 more details.
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
17
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
20 */
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
21
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
22 /*
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
23 handle reading input for the rest of the system
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
24 */
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
25
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
26 #include <stdlib.h>
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
27 #include <stdio.h>
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
28 #include <string.h>
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
29
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
30 #include <lw_alloc.h>
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
31
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
32 #define __input_c_seen__
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
33 #include "lwbasic.h"
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
34
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
35 struct input_state
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
36 {
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
37 FILE *fp;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
38 int error;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
39 };
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
40
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
41 static void input_init(cstate *state)
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
42 {
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
43 struct input_state *sp;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
44
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
45 sp = lw_alloc(sizeof(struct input_state));
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
46 sp -> error = 0;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
47
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
48 if (!(state -> input_file) || strcmp(state -> input_file, "-"))
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
49 {
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
50 sp -> fp = stdin;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
51 }
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
52 else
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
53 {
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
54 sp -> fp = fopen(state -> input_file, "rb");
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
55 if (!(sp -> fp))
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
56 {
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
57 fprintf(stderr, "Cannot open input file\n");
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
58 exit(1);
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
59 }
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
60 }
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
61
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
62 state -> input_state = sp;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
63 }
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
64
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
65 int input_getchar(cstate *state)
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
66 {
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
67 int r;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
68 struct input_state *sp;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
69
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
70 if (!(state -> input_state))
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
71 input_init(state);
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
72 sp = state -> input_state;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
73
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
74
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
75 if (sp -> error)
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
76 return -2;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
77
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
78 if (feof(sp -> fp))
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
79 return -1;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
80
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
81 r = fgetc(sp -> fp);
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
82 if (r == EOF)
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
83 return -1;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
84 return r;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
85 }