annotate lwbasic/input.c @ 38:7e92484cfbc3

Caused expressions used in setdp and conditionals to be reduced on pass 1
author lost@l-w.ca
date Sat, 02 Apr 2011 02:19:02 -0600
parents 87590f43e76d
children
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>
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 23
diff changeset
31 #include <lw_error.h>
23
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
32
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
33 #define __input_c_seen__
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
34 #include "lwbasic.h"
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
35
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
36 struct input_state
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
37 {
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
38 FILE *fp;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
39 int error;
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
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
42 static void input_init(cstate *state)
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
43 {
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
44 struct input_state *sp;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
45
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
46 sp = lw_alloc(sizeof(struct input_state));
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
47 sp -> error = 0;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
48
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
49 if (!(state -> input_file) || strcmp(state -> input_file, "-"))
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
50 {
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
51 sp -> fp = stdin;
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
52 }
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
53 else
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
54 {
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
55 sp -> fp = fopen(state -> input_file, "rb");
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
56 if (!(sp -> fp))
25a4aef9c5ed lwbasic: Added basic character input framework
lost@l-w.ca
parents:
diff changeset
57 {
25
87590f43e76d Started lwbasic parser; checkpoint
lost@l-w.ca
parents: 23
diff changeset
58 lwb_error("Cannot open input file\n");
23
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 }