File: args.c

package info (click to toggle)
sim4 0.0.20121010-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,744 kB
  • ctags: 505
  • sloc: ansic: 5,807; makefile: 23; sh: 15
file content (99 lines) | stat: -rw-r--r-- 2,446 bytes parent folder | download | duplicates (5)
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
#include "libc.h"
#include "types.h"
#include "misc.h"
#include "args.h"

#ifndef __lint
static const char rcsid[] =
"$Id: args.c,v 1.3 2000/09/15 17:57:02 florea Exp $";
#endif

static int argc;
static char **argv;
char *argv0;

/* ckargs  --  check that only certain parameters are set on the command line */
void ckargs(const char *options, int argcx, char **argvx, int non_options)
{
	int i;

	argc = argcx;
	argv = argvx;
	argv0 = argv0 ? argv0 : argv[0];
	for (i = non_options+1; i < argc; ++i)
		if (argv[i][1] != '=')
			fatalf("Improper command option: '%s'.", argv[i]);
		else if (!strchr(options, argv[i][0]))
			fatalf("Available options: %s\n", options);
}

/* get_argval  --------------------- get the value of a command-line argument */
bool get_argval(int c, int *val_ptr)
{
	int i;

	ck_argc("get_argval");
	for (i = 0; i < argc; ++i)
		if (argv[i][0] == c && argv[i][1] == '=') {
			*val_ptr = atoi(argv[i]+2);
			return 1;
		}
	return 0;
}

/* get_fargval  --------------- get the float value of a command-line argument */
bool get_fargval(int c, double *val_ptr)
{
        int i;

        ck_argc("get_fargval");
        for (i = 0; i < argc; ++i)
                if (argv[i][0] == c && argv[i][1] == '=') {
                        *val_ptr = atof(argv[i]+2);
                        return 1;
                }
        return 0;
}

/* get_strargval  ---------- get the string value of a command-line argument */
bool get_strargval(int c, char **val_ptr)
{  
        int i;
   
        ck_argc("get_strargval");
        for (i = 0; i < argc; ++i)
                if (argv[i][0] == c && argv[i][1] == '=') {
                        *val_ptr = (char *) ckalloc(strlen(argv[i]+2)+1);
                        strcpy(*val_ptr, argv[i]+2);
                        return 1;
                }
        return 0;
}

bool get_cargval(int c, char **valp)
{
        int i;

        ck_argc("get_cargval");
        for (i = 0; i < argc; ++i)
                if (argv[i][0] == c && argv[i][1] == '=') {
                        *valp = argv[i]+2;
                        return 1;
                }
        return 0;
}

void fprintf_argv(FILE* fp)
{
        int i;
        fprintf(fp, "%s", argv0);
        for (i = 1; i < argc; ++i)
                (void)fprintf(fp, " %s", argv[i]);
}       

/* ck_argc - die if argc is unknown */
void ck_argc(const char *proc_name)
{
	if (argc == 0)
		fatalf("Call ckargs() before %s.\n", proc_name);
}