Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/ui/param.c
blob: eb10a9dcc1c40e927eb52a9217c146969f13042a (plain)
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
/* 
 *     XaoS, a fast portable realtime fractal zoomer 
 *                  Copyright (C) 1996,1997 by
 *
 *      Jan Hubicka          (hubicka@paru.cas.cz)
 *      Thomas Marsh         (tmarsh@austin.ibm.com)
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#ifdef _plan9_
#include <u.h>
#include <libc.h>
#else
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#endif
#include <config.h>
#include <param.h>
#include <xmenu.h>
#include <version.h>
#include <xerror.h>
#define MAXPARAMS 40
static CONST struct params *params[40];
int nparams;

int params_parser(int argc, char **argv)
{
    int i, p = 0, d;
    int ie = 0;
    int is;
    CONST struct params *par = NULL;
    int error = 0;
    int found;
    for (i = 1; i < argc && !error; i++) {
	found = 0;
#ifdef MACOSX
	if (strncmp("-psn", argv[i], 4) == 0)
	    continue;
#endif
	if (!strcmp("-help", argv[i])) {
	    error = 1;
	    break;
	}
	for (d = 0; d < nparams; d++) {
	    par = params[d];
	    for (p = 0; par[p].name != NULL && !error; p++) {
		if (!strcmp(par[p].name, argv[i])) {
		    found = 1;
		    is = i;
		    switch (par[p].type) {
		    case P_SWITCH:
			*((int *) par[p].value) = 1;
			break;
		    case P_NUMBER:
			{
			    int n;
			    if (i == argc - 1) {
				x_error
				    ("parameter %s requires numeric value.",
				     argv[i]);
				error = 1;
				break;
			    }
			    if (sscanf(argv[i + 1], "%i", &n) != 1) {
				x_error("parameter for %s is not number.",
					argv[i]);
				error = 1;
				break;
			    }
			    *((int *) par[p].value) = n;
			    i++;
			}
			break;
		    case P_FLOAT:
			{
			    float n;
			    if (i == argc - 1) {
				x_error
				    ("parameter %s requires floating point numeric value.",
				     argv[i]);
				error = 1;
				break;
			    }
			    if (sscanf(argv[i + 1], "%f", &n) != 1) {
				x_error
				    ("parameter for %s is not floating point number.",
				     argv[i]);
				error = 1;
				break;
			    }
			    *((float *) par[p].value) = n;
			    i++;
			}
			break;
		    case P_STRING:
			{
			    if (i == argc - 1) {
				x_error
				    ("parameter %s requires string value.",
				     argv[i]);
				error = 1;
				break;
			    }
			    i++;
			    *((char **) par[p].value) = *(argv + i);
			}
		    }
		    ie = i;
		    i = is;
		}
	    }
	}
	if (d == nparams && !found) {
	    i = menu_processargs(i, argc, argv);
	    if (i < 0) {
		error = 1;
		break;
	    } else
		i++;
	} else
	    i = ie;
    }
    if (error) {
	const char *name[] = {
	    "",
	    "number",
	    "string",
	    "f.point"
	};
#ifndef _plan9_
	printf("                 XaoS" XaoS_VERSION " help text\n");
	printf
	    (" (This help is genereated automagically. I am sorry for all inconvencies)\n\n");
#endif
	printf("option string   param   description\n\n");
	for (d = 0; d < nparams; d++) {
	    par = params[d];
	    for (p = 0; par[p].name != NULL; p++) {
		if (par[p].type == P_HELP)
		    printf("\n%s\n\n", par[p].help);
		else if (!par[p].type)
		    printf(" %-14s   %s\n", par[p].name, par[p].help);
		else
		    printf(" %-14s  %s\n%14s    %s\n", par[p].name,
			   name[par[p].type], "", par[p].help);
	    }
	    if (p == 0)
		printf(" No options avaiable for now\n");
	}
	menu_printhelp();
	return 0;
    }
    return (1);
}

void params_register(CONST struct params *par)
{
    params[nparams++] = par;
}