File: options.c

package info (click to toggle)
jpnevulator 1.3.1-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 224 kB
  • sloc: ansic: 1,200; makefile: 94
file content (314 lines) | stat: -rw-r--r-- 9,510 bytes parent folder | download
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* jpnevulator - serial reader/writer
 * Copyright (C) 2006-2012 Freddy Spierenburg
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>

#include "options.h"
#include "jpnevulator.h"
#include "io.h"
#include "crc16.h"
#include "crc8.h"
#include "list.h"
#include "tty.h"

static void usage(void) {
	printf(
		"Usage: %s [--version] [--help] [--checksum] [--crc16 poly] [--crc8 poly]\n"
		"         [--fuck-up] [--file file] [--no-send]\n"
		"         [--delay-line microseconds] [--delay-byte microseconds]\n"
		"         [--print] [--size size] [--tty tty] [--width] [--pass] [--read]\n"
		"         [--write] [--timing-print] [--timing-delta microseconds]\n"
		"         [--ascii] [--alias-separator separator] [--byte-count] <file>\n",
		PROGRAM_NAME
	);
}

static void optionsDefault(void) {
	/* Don't add a checksum by default. */
	_jpnevulatorOptions.checksum=checksumTypeNone;

	/* Use the crc16 x^16+x^15+x^2+1 polynomial by default. */
	_jpnevulatorOptions.crc16Poly=0xA001;
	crc16TableCreate(0,_jpnevulatorOptions.crc16Poly);

	/* Use the crc8 x^8+x^2+x+1 polynomial by default. */
	_jpnevulatorOptions.crc8Poly=0x07;
	crc8PolyInit(_jpnevulatorOptions.crc8Poly);

	/* Don't fuck up the checksum(wrongly named crc) by default. */
	boolReset(_jpnevulatorOptions.checksumFuckup);

	/* Read/Write standard input/output by default, but use some black magic to find out if a user selects
	 * multiple I/O sources. Which we of course neglect anyway. */
	sprintf(_jpnevulatorOptions.io,"%.*s",(int)sizeof(_jpnevulatorOptions.io)-1,ioMAGIC);

	/* Initialize our list where we store our tty devices. We do not yet add the
	 * default /dev/ttyS0 device. We postpone that up untill we know for sure the
	 * user did not gave us a tty device. */
	ttyInitialize();

	/* By default we expect to send/receive Cham2 messages. */
	_jpnevulatorOptions.size=22;

	/* By default we send our messages on the serial port... */
	boolSet(_jpnevulatorOptions.send);

	/* ...but we probably don't want to see the messages on the console. Or do we? :-) */
	boolReset(_jpnevulatorOptions.print);

	/* Do not delay between messages by default. */
	_jpnevulatorOptions.delayLine=0L;

	/* Do not delay between bytes by default. */
	_jpnevulatorOptions.delayByte=0L;

	/* Action type is mandatory (not by getopts, but by our own mechanism). */
	_jpnevulatorOptions.action=actionTypeNone;

	/* By default we display 16 bytes on one line. */
	_jpnevulatorOptions.width=16;

	/* By default we do not display timing information. */
	boolReset(_jpnevulatorOptions.timingPrint);

	/* By default we couple bytes that arrive within 100 miliseconds. */
	_jpnevulatorOptions.timingDelta=100000UL;

	/* By default we do not display ascii data in read mode. */
	boolReset(_jpnevulatorOptions.ascii);

	/* By default the alias separator is the ':' character. */
	_jpnevulatorOptions.aliasSeparator=":";

	/* Do not show the byte count by default. */
	boolReset(_jpnevulatorOptions.byteCountDisplay);

	/* Do not pass bytes between tty's by default. */
	boolReset(_jpnevulatorOptions.pass);
}

static void optionsIOWrite(char *file) {
	int charactersPrinted;
	charactersPrinted=sprintf(_jpnevulatorOptions.io,"%.*s",(int)sizeof(_jpnevulatorOptions.io)-1,file);
	if(charactersPrinted!=strlen(file)) {
		fprintf(stderr,"%s: Filename truncated to %d chars\n",PROGRAM_NAME,charactersPrinted);
	}
}

enum optionsRtrn optionsParse(int argc,char **argv) {
	int finished;
	optionsDefault();
	for(finished=0;!finished;) {
		int option,option_index;
		static struct option long_options[]={
			{"ascii",no_argument,NULL,'a'},
			{"byte-count",no_argument,NULL,'b'},
			{"checksum",no_argument,NULL,'c'},
			{"delay-line",required_argument,NULL,'d'},
			{"timing-delta",required_argument,NULL,'e'},
			{"file",required_argument,NULL,'f'},
			{"timing-print",no_argument,NULL,'g'},
			{"help",no_argument,NULL,'h'},
			{"width",required_argument,NULL,'i'},
			{"fuck-up",no_argument,NULL,'j'},
			{"delay-byte",required_argument,NULL,'k'},
			{"alias-separator",required_argument,NULL,'l'},
			{"no-send",no_argument,NULL,'n'},
			{"pass",no_argument,NULL,'P'},
			{"print",no_argument,NULL,'p'},
			{"read",no_argument,NULL,'r'},
			{"size",required_argument,NULL,'s'},
			{"tty",required_argument,NULL,'t'},
			{"version",no_argument,NULL,'v'},
			{"write",no_argument,NULL,'w'},
			{"crc16",optional_argument,NULL,'y'},
			{"crc8",optional_argument,NULL,'z'},
			{NULL,no_argument,NULL,0}
		};
		option=getopt_long(argc,argv,"acd:e:f:ghi:jl:npPrs:t:vwy:z:",long_options,&option_index);
		switch(option) {
			case -1: {
				finished=!finished;
				break;
			}
			case 'a': {
				boolSet(_jpnevulatorOptions.ascii);
				break;
			}
			case 'b': {
				boolSet(_jpnevulatorOptions.byteCountDisplay);
				break;
			}
			case 'c': {
				_jpnevulatorOptions.checksum=checksumTypeChecksum;
				break;
			}
			case 'd': {
				unsigned long delay;
				delay=atol(optarg);
				if(delay>0) {
					_jpnevulatorOptions.delayLine=delay;
				} else {
					fprintf(stderr,"%s: Discarding line delay. It should be bigger than zero.\n",PROGRAM_NAME);
				}
				break;
			}
			case 'e': {
				_jpnevulatorOptions.timingDelta=atol(optarg);
				break;
			}
			case 'f': {
				optionsIOWrite(optarg);
				break;
			}
			case 'g': {
				boolSet(_jpnevulatorOptions.timingPrint);
				break;
			}
			case 'h': {
				usage();
				return(optionsRtrnUsage);
			}
			case 'i': {
				int width;
				width=atoi(optarg);
				if(optarg>0) {
					_jpnevulatorOptions.width=width;
				} else {
					fprintf(stderr,"%s: Discarding width. It should be bigger than zero.\n",PROGRAM_NAME);
				}
				break;
			}
			case 'j': {
				boolSet(_jpnevulatorOptions.checksumFuckup);
				break;
			}
			case 'k': {
				unsigned long delay;
				delay=atol(optarg);
				if(delay>0) {
					_jpnevulatorOptions.delayByte=delay;
				} else {
					fprintf(stderr,"%s: Discarding byte delay. It should be bigger than zero.\n",PROGRAM_NAME);
				}
				break;
			}
			case 'l': {
				_jpnevulatorOptions.aliasSeparator=optarg;
				break;
			}
			case 'n': {
				boolReset(_jpnevulatorOptions.send);
				break;
			}
			case 'p': {
				boolSet(_jpnevulatorOptions.print);
				break;
			}
			case 'P': {
				boolSet(_jpnevulatorOptions.pass);
				break;
			}
			case 'r': {
				if(_jpnevulatorOptions.action!=actionTypeNone) {
					fprintf(stderr,"%s: Use --read or --write, but not both. Performing a read this time.\n",PROGRAM_NAME);
				}
				_jpnevulatorOptions.action=actionTypeRead;
				break;
			}
			case 's': {
				int size;
				size=atoi(optarg);
				if(size>0) {
					_jpnevulatorOptions.size=size;
				} else {
					fprintf(stderr,"%s: Discarding size. It should be bigger than zero.\n",PROGRAM_NAME);
				}
				break;
			}
			case 't': {
				ttyAdd(optarg);
				break;
			}
			case 'v': {
				printf(
					"%s version %s\n"
					"Copyright (C) 2006-2012 Freddy Spierenburg <freddy@snarl.nl>\n"
					"This is free software.  You may redistribute copies of it under the terms of\n"
					"the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"
					"There is NO WARRANTY, to the extent permitted by law.\n",
					PROGRAM_NAME,PROGRAM_VERSION
				);
				return(optionsRtrnVersion);
			}
			case 'w': {
				if(_jpnevulatorOptions.action!=actionTypeNone) {
					fprintf(stderr,"%s: Use --read or --write, but not both. Performing a write this time.\n",PROGRAM_NAME);
				}
				_jpnevulatorOptions.action=actionTypeWrite;
				break;
			}
			case 'y': {
				_jpnevulatorOptions.checksum=checksumTypeCrc16;
				if(optarg) {
					printf("[%s]\n",optarg);
					_jpnevulatorOptions.crc16Poly=strtol(optarg,NULL,16);
					crc16TableCreate(0,_jpnevulatorOptions.crc16Poly);
				}
				break;
			}
			case 'z': {
				_jpnevulatorOptions.checksum=checksumTypeCrc8;
				if(optarg) {
					_jpnevulatorOptions.crc8Poly=strtol(optarg,NULL,16);
					crc8PolyInit(_jpnevulatorOptions.crc8Poly);
				}
				break;
			}
		}
	}

	/* As mentioned more early, we read/write standard input/output by default and warn the user if he
	 * or she tries to use multiple sources. */
	if(optind<argc) {
		if(strcmp(_jpnevulatorOptions.io,ioMAGIC)!=0) {
			fprintf(stderr,"%s: Overriding io file given before...\n",PROGRAM_NAME);
		}
		optionsIOWrite(argv[optind]);
	} else {
		if(strcmp(_jpnevulatorOptions.io,ioMAGIC)==0) {
			optionsIOWrite("-");
		}
	}

	if(_jpnevulatorOptions.action==actionTypeNone) {
		fprintf(stderr,"%s: Use of --read or --write mandatory. Defaulting to write this time.\n",PROGRAM_NAME);
		_jpnevulatorOptions.action=actionTypeWrite;
	}

	/* If the user did not mentioned any tty devices we will by default
	 * open the /dev/ttyS0 device. */
	if(listElements(&_jpnevulatorOptions.tty)==0) {
		ttyAdd("/dev/ttyS0");
	}

	return(optionsRtrnOk);
}