-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdlnopts.c
More file actions
152 lines (147 loc) · 6.67 KB
/
cmdlnopts.c
File metadata and controls
152 lines (147 loc) · 6.67 KB
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
/* geany_encoding=koi8-r
* cmdlnopts.c - the only function that parse cmdln args and returns glob parameters
*
* Copyright 2013 Edward V. Emelianoff <eddy@sao.ru>
*
* 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 Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <math.h>
#include "cmdlnopts.h"
#include "usefull_macros.h"
/*
* here are global parameters initialisation
*/
int help;
glob_pars G;
#define DEFAULT_COMDEV "/dev/ttyUSB0"
// DEFAULTS
// default global parameters
glob_pars const Gdefault = {
.terminal = 0,
.heater = HEATER_LEAVE,
.device = DEFAULT_COMDEV,
.rest_pars_num = 0,
.splist = 0,
.newspeed = 0,
.rest_pars = NULL,
.shutter_cmd = NULL,
.subframe = NULL,
.speed = 0,
.exptime = 1e-4,
.binning = 0,
.takeimg = 0,
.imtype = "l",
.imformat = NULL,
.imstoretype = NULL,
.outpfname = "output.fits",
.hostname = NULL,
.port = "4444",
.once = 0,
.timestamp = 0,
.dark_interval = 1800.,
.min_dark_exp = 30.,
.max_exptime = -1.,
.htrperiod = 0
};
/*
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
myoption cmdlnopts[] = {
// common options
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
// only standalone options
#if !defined DAEMON && !defined CLIENT
{"imtype", NEED_ARG, NULL, 'T', arg_string, APTR(&G.imtype), _("image type: light (l, L), autodark (a, A), dark (d, D); default: light")},
{"terminal",NO_ARGS, NULL, 't', arg_int, APTR(&G.terminal), _("run as terminal")},
{"start-exp",NO_ARGS, NULL, 'X', arg_int, APTR(&G.takeimg), _("start exposition")},
#endif
// not daemon options
#ifndef DAEMON
{"storetype",NEED_ARG, NULL, 'S', arg_string, APTR(&G.imstoretype),_("'overwrite'/'rewrite' to rewrite existing image, 'enumerate'/'numerate' to use given filename as base for series")},
{"output", NEED_ARG, NULL, 'o', arg_string, APTR(&G.outpfname), _("output file name (default: output.fits)")},
{"imformat",NEED_ARG, NULL, 'f', arg_string, APTR(&G.imformat), _("image format: FITS (f), TIFF (t), raw dump with histogram storage (r,d), may be OR'ed; default: FITS or based on output image name")},
#endif
// not client options
#ifndef CLIENT
{"exptime-max",NEED_ARG,NULL, 0, arg_double, APTR(&G.max_exptime),_("maximal exposition time (seconds)")},
{"heaterperiod", NEED_ARG,NULL, 0, arg_int, APTR(&G.htrperiod), _("change heater ON time (0..3599 seconds)")},
{"device", NEED_ARG, NULL, 'i', arg_string, APTR(&G.device), _("serial device name (default: " DEFAULT_COMDEV ")")},
{"heater-on",NO_ARGS, APTR(&G.heater),HEATER_ON, arg_none, NULL, _("turn heater on")},
{"heater-off",NO_ARGS, APTR(&G.heater),HEATER_OFF, arg_none, NULL, _("turn heater off")},
{"spd-list",NO_ARGS, NULL, 'l', arg_int, APTR(&G.splist), _("list speeds available")},
{"baudrate",NEED_ARG, NULL, 'b', arg_int, APTR(&G.speed), _("connect at given baudrate without autocheck")},
{"spd-set", NEED_ARG, NULL, 's', arg_int, APTR(&G.newspeed), _("set terminal speed")},
{"shutter", NEED_ARG, NULL, 0, arg_string, APTR(&G.shutter_cmd),_("shutter command: 'o' for open, 'c' for close, 'k' for de-energize")},
{"subframe",NEED_ARG, NULL, 0, arg_string, APTR(&G.subframe), _("select subframe: x,y,size")},
{"exptime", NEED_ARG, NULL, 'x', arg_double, APTR(&G.exptime), _("exposition time in seconds (default: 1s)")},
{"binning", NEED_ARG, NULL, 'B', arg_int, APTR(&G.binning), _("binning (default 0: full size)")},
#endif
// not standalone options
#if defined DAEMON || defined CLIENT
#ifndef DAEMON
{"hostname",NEED_ARG, NULL, 'H', arg_string, APTR(&G.hostname), _("hostname to connect (default: localhost)")},
{"once", NO_ARGS, NULL, '1', arg_int, APTR(&G.once), _("run client just once")},
{"timestamp",NO_ARGS, NULL, 't', arg_int, APTR(&G.timestamp), _("add timestamp to filename")},
#endif
{"port", NEED_ARG, NULL, 'p', arg_string, APTR(&G.port), _("port to connect (default: 4444)")},
#endif
// only daemon options
#ifdef DAEMON
{"dark-interval",NEED_ARG,NULL, 'D', arg_double, APTR(&G.dark_interval),_("time interval (in seconds) between dark images taken (default: 1800)")},
{"min-dark-exp",NEED_ARG,NULL, 'E', arg_double, APTR(&G.min_dark_exp),_("minimal exposition (in seconds) at which darks would be taken (default: 30)")},
#endif
end_option
};
/**
* Parse command line options and return dynamically allocated structure
* to global parameters
* @param argc - copy of argc from main
* @param argv - copy of argv from main
* @return allocated structure with global parameters
*/
glob_pars *parse_args(int argc, char **argv){
int i;
void *ptr;
ptr = memcpy(&G, &Gdefault, sizeof(G)); assert(ptr);
size_t hlen = 1024;
char helpstring[1024], *hptr = helpstring;
#ifndef LIBCFITSIO
int l0 = snprintf(hptr, hlen, "\t%s\n", _("Warning! Compiled without FITS support!!!"));
if(l0 > 0){ hptr += l0; hlen -= l0;}
#endif // LIBCFITSIO
#ifndef LIBTIFF
int l1 = snprintf(hptr, hlen, "\t%s\n", _("Warning! Compiled without TIFF support!!!"));
if(l1 > 0){ hptr += l1; hlen -= l1;}
#endif // LIBTIFF
snprintf(hptr, hlen, "Usage: %%s [args]\n\n\tWhere args are:\n");
// format of help: "Usage: progname [args]\n"
change_helpstring(helpstring);
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(argc > 0){
G.rest_pars_num = argc;
G.rest_pars = calloc(argc, sizeof(char*));
for (i = 0; i < argc; i++)
G.rest_pars[i] = strdup(argv[i]);
}
return &G;
}