This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
/
validator.c
202 lines (160 loc) · 5.1 KB
/
validator.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
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
/*
validator.c - Grbl G-code validation
Part of Grbl Simulator
Copyright (c) 2012 Adam Shelly
2020 - modified for grblHAL by Terje Io
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "platform.h"
#include "grbl/hal.h"
#include "grbl/report.h"
#include "grbl/protocol.h"
#include "grbl/protocol.h"
#include "grbl/nvs_buffer.h"
typedef struct arg_vars {
// Output file handles
FILE *input_file;
FILE *output_file;
uint8_t echo;
uint8_t silent;
} arg_vars_t;
arg_vars_t args;
const char* progname;
uint8_t exit_code = 0;
int usage (const char* badarg)
{
if (badarg)
printf("Unrecognized option %s\n", badarg);
printf("Usage: \n"
"%s <Options> [input_file]\n"
" Options:\n"
" -o <output file> : use output file instead of stdout\n"
" -e : echo input to output\n"
" -s : silent, no output only return code \n"
"\n Parses gcode from stdin or input line, prints grbl's expected response"
"\n Returns 0 on successs, or line number of error",
progname);
return -1;
}
status_code_t validator_report_status_message (status_code_t status_code)
{
report_status_message(status_code);
if (status_code && !exit_code) {
printf("EXITING %d\n",status_code);
exit_code = status_code;
sys.abort = 1;
}
return status_code;
}
// Read fom input
int16_t serial_read()
{
int16_t data = fgetc(args.input_file);
if (data == PLATFORM_EXTRA_CR)
return(0);
if (args.echo)
fputc(data, args.output_file);
plan_reset();
if (sys.abort || feof(args.input_file) || data == 0x06 || data == -1) {
sys.abort = 1;
return SERIAL_NO_DATA;
}
return data;
}
// Write to output
void serial_write (const char *data)
{
if (!args.silent) {
char c, *ptr = (char *)data;
while((c = *ptr++) != '\0')
fputc(c, args.output_file);
}
}
int main(int argc, char *argv[])
{
int positional_args=0;
//defaults
args.input_file = stdin;
args.output_file = stdout;
args.echo = 0;
args.silent = 0;
progname = argv[0];
while (argc > 1) {
argv++; argc--;
if (argv[0][0] == '-') {
switch(argv[0][1]) {
case 'e': //echo mode
args.echo = 1;
break;
case 's': //silent
args.silent = 1;
break;
case 'o': //output file
argv++; argc--;
args.output_file = fopen(*argv,"w");
if (!args.output_file) {
perror("fopen");
printf("Error opening : %s\n",*argv);
return(usage(0));
}
break;
case 'h':
return usage(NULL);
default:
return usage(*argv);
}
} else { //handle positional arguments
positional_args++;
switch(positional_args) {
case 1: //input file
args.input_file = fopen(*argv,"r");
if (!args.input_file) {
perror("fopen");
printf("Error opening : %s\n",*argv);
return(usage(0));
}
break;
default:
return usage(*argv);
}
}
}
// Clear all and set some core function pointers
memset(&grbl, 0, sizeof(grbl_t));
grbl.on_execute_realtime = protocol_execute_noop;
grbl.protocol_enqueue_gcode = protocol_enqueue_gcode;
// Clear all and set some HAL function pointers
memset(&hal, 0, sizeof(grbl_hal_t));
hal.version = HAL_VERSION; // Update when signatures and/or contract is changed - driver_init() should fail
hal.driver_reset = dummy_handler;
hal.irq_enable = dummy_handler;
hal.irq_disable = dummy_handler;
hal.nvs.size = GRBL_NVS_SIZE;
if(!driver_init())
return -1;
// TODO: read settings from EEPROM.dat if exists?
nvs_buffer_init();
settings_init();
report_init_fns();
grbl.report.status_message = validator_report_status_message;
grbl.report.feedback_message = report_feedback_message;
hal.stream.read = serial_read;
hal.stream.write = serial_write;
hal.stream.write_all = serial_write;
protocol_main_loop();
return exit_code;
}