summaryrefslogtreecommitdiffstats
path: root/tools/tools.h
blob: 9a16b6c477b668b37131f999ece8a560e67d42f7 (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
/*
 * Copyright (C) 2001 Sistina Software (UK) Limited.
 *
 * This file is released under the LGPL.
 */

#ifndef _LVM_TOOLS_H
#define _LVM_TOOLS_H

#define _GNU_SOURCE

#include "activate.h"
#include "archive.h"
#include "cache.h"
#include "config.h"
#include "dbg_malloc.h"
#include "dev-cache.h"
#include "device.h"
#include "display.h"
#include "errors.h"
#include "filter.h"
#include "filter-composite.h"
#include "filter-persistent.h"
#include "filter-regex.h"
#include "format-text.h"
#include "metadata.h"
#include "list.h"
#include "locking.h"
#include "log.h"
#include "lvm-file.h"
#include "lvm-string.h"
#include "pool.h"
#include "toolcontext.h"
#include "toollib.h"

#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

#define CMD_LEN 256
#define MAX_ARGS 64

/* command functions */
typedef int (*command_fn) (struct cmd_context * cmd, int argc, char **argv);

#define xx(a, b...) int a(struct cmd_context *cmd, int argc, char **argv);
#include "commands.h"
#undef xx

/* define the enums for the command line switches */
enum {
#define arg(a, b, c, d) a ,
#include "args.h"
#undef arg
};

typedef enum {
	SIGN_NONE = 0,
	SIGN_PLUS = 1,
	SIGN_MINUS = 2
} sign_t;

/* a global table of possible arguments */
struct arg {
	char short_arg;
	char *long_arg;
	int (*fn) (struct cmd_context * cmd, struct arg * a);

	int count;
	char *value;
	uint32_t i_value;
	uint64_t i64_value;
	sign_t sign;
	void *ptr;
};

/* a register of the lvm commands */
struct command {
	const char *name;
	const char *desc;
	const char *usage;
	command_fn fn;

	int num_args;
	int *valid_args;
};

void usage(const char *name);

/* the argument verify/normalise functions */
int yes_no_arg(struct cmd_context *cmd, struct arg *a);
int size_kb_arg(struct cmd_context *cmd, struct arg *a);
int size_mb_arg(struct cmd_context *cmd, struct arg *a);
int int_arg(struct cmd_context *cmd, struct arg *a);
int int_arg_with_sign(struct cmd_context *cmd, struct arg *a);
int minor_arg(struct cmd_context *cmd, struct arg *a);
int string_arg(struct cmd_context *cmd, struct arg *a);
int permission_arg(struct cmd_context *cmd, struct arg *a);
int metadatatype_arg(struct cmd_context *cmd, struct arg *a);

char yes_no_prompt(const char *prompt, ...);

/* we use the enums to access the switches */
static inline int arg_count(struct cmd_context *cmd, int a)
{
	return cmd->args[a].count;
}

static inline char *arg_value(struct cmd_context *cmd, int a)
{
	return cmd->args[a].value;
}

static inline char *arg_str_value(struct cmd_context *cmd, int a, char *def)
{
	return arg_count(cmd, a) ? cmd->args[a].value : def;
}

static inline uint32_t arg_int_value(struct cmd_context *cmd, int a,
				     uint32_t def)
{
	return arg_count(cmd, a) ? cmd->args[a].i_value : def;
}

static inline uint64_t arg_int64_value(struct cmd_context *cmd, int a,
				       uint64_t def)
{
	return arg_count(cmd, a) ? cmd->args[a].i64_value : def;
}

static inline void *arg_ptr_value(struct cmd_context *cmd, int a, void *def)
{
	return arg_count(cmd, a) ? cmd->args[a].ptr : def;
}

static inline sign_t arg_sign_value(struct cmd_context *cmd, int a, sign_t def)
{
	return arg_count(cmd, a) ? cmd->args[a].sign : def;
}

static inline int arg_count_increment(struct cmd_context *cmd, int a)
{
	return cmd->args[a].count++;
}

static inline const char *command_name(struct cmd_context *cmd)
{
	return cmd->command->name;
}

#endif