aboutsummaryrefslogtreecommitdiff
path: root/src/cmd.h
blob: b47b1b2f472c16e6f34c48775061dea1ac1b3ea0 (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
/*

    casm - Simple, portable assembler

    Copyright (C) 2003-2015  Ian Cowburn (ianc@noddybox.demon.co.uk)

    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 3 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, see <http://www.gnu.org/licenses/>.

    -------------------------------------------------------------------------

    Command callback definitions

*/

#ifndef CASM_CMD_H
#define CASM_CMD_H

/* ---------------------------------------- TYPES
*/


typedef enum
{
    CMD_OK,
    CMD_OK_WARNING,
    CMD_FAILED,
    CMD_NOT_KNOWN
} CommandStatus;

/* Used to implement a command.
*/
typedef CommandStatus (*Command)(const char *label, int argc, char *argv[],
                                 int quoted[], char *err, size_t errsize);


/* ---------------------------------------- HELPER MACROS
*/


/* Check for the precesence of a label.  Expects a variable called 'label'

   Args:
        NONE

   Expects:

        argv    - The argv passed to the command handler
        label   - The label passed to the command handler
        err     - The error location passed to the command handler
        errsize - The size of the error location passed to the command handler
*/
#define CMD_LABEL_CHECK                                                 \
do                                                                      \
{                                                                       \
    if (!label)                                                         \
    {                                                                   \
        snprintf(err, errsize, "%s: missing label", argv[0]);           \
        return CMD_FAILED;                                              \
    }                                                                   \
} while(0)



/* Check for the arg count being at least a specified number.

   Args:
        min     - The minimum number of arguments to support

   Expects:

        argc    - The argc passed to the command handler
        argv    - The argv passed to the command handler
        err     - The error location passed to the command handler
        errsize - The size of the error location passed to the command handler
*/
#define CMD_ARGC_CHECK(min)                                             \
do                                                                      \
{                                                                       \
    if (argc < min)                                                     \
    {                                                                   \
        snprintf(err, errsize, "%s: missing argument", argv[0]);        \
        return CMD_FAILED;                                              \
    }                                                                   \
} while(0)




/* Evaluate an expression.

   Args:
        expr    - The expression to evaluate
        result  - The variable to hold the result

   Expects:

        argc    - The argc passed to the command handler
        argv    - The argv passed to the command handler
        err     - The error location passed to the command handler
        errsize - The size of the error location passed to the command handler
*/
#define CMD_EXPR(expr, result)                                          \
do                                                                      \
{                                                                       \
    if (!ExprEval(expr, &result))                                       \
    {                                                                   \
        snprintf(err, errsize, "%s: expression error:  %s",             \
                                        argv[0], ExprError());          \
        return CMD_FAILED;                                              \
    }                                                                   \
} while(0)



/* Parse a table using ParseTable()

   Args:
        str     - The string to look for
        table   - The table to parse
        result  - Variable to hold the return from ParseTable()

   Expects:

        argv    - The argv passed to the command handler
        err     - The error location passed to the command handler
        errsize - The size of the error location passed to the command handler
*/
#define CMD_TABLE(str, table, result)                                   \
do                                                                      \
{                                                                       \
    if (!(result = ParseTable(str, table)))                             \
    {                                                                   \
        snprintf(err, errsize, "unknown value: \"%s\"", str);           \
        return CMD_FAILED;                                              \
    }                                                                   \
} while(0)



#endif

/*
vim: ai sw=4 ts=8 expandtab
*/