OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [argtable2/] [arg_rex.c] - Blame information for rev 771

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jeremybenn
/*********************************************************************
2
This file is part of the argtable2 library.
3
Copyright (C) 1998-2001,2003-2008 Stewart Heitmann
4
sheitmann@users.sourceforge.net
5
 
6
The argtable2 library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public License as
8
published by the Free Software Foundation; either version 2 of the
9
License, or (at your option) any later version.
10
 
11
This software is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
Library General Public License for more details.
15
 
16
You should have received a copy of the GNU Library General Public
17
License along with this library; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19
USA.
20
**********************************************************************/
21
 
22
/* config.h must be included before anything else */
23
#ifdef HAVE_CONFIG_H
24
#include "config.h"
25
#endif
26
 
27
#ifdef HAVE_STDLIB_H
28
#include <stdlib.h>
29
#endif
30
 
31
#include "argtable2.h"
32
#include <sys/types.h>
33
#include <regex.h>
34
 
35
 
36
/* local error codes (these must not conflict with reg_error codes) */
37
enum {EMINCOUNT=200,EMAXCOUNT=201};
38
 
39
struct privhdr
40
    {
41
    const char *pattern;
42
    int flags;
43
    regex_t regex;
44
    };
45
 
46
 
47
static void resetfn(struct arg_rex *parent)
48
    {
49
    struct privhdr *priv = (struct privhdr*)(parent->hdr.priv);
50
 
51
    /*printf("%s:resetfn(%p)\n",__FILE__,parent);*/
52
    parent->count=0;
53
 
54
    /* construct the regex representation of the given pattern string. */
55
    /* Dont bother checking for errors as we already did that earlier (in the constructor) */
56
    regcomp(&(priv->regex), priv->pattern, priv->flags);
57
    }
58
 
59
static int scanfn(struct arg_rex *parent, const char *argval)
60
    {
61
    int errorcode = 0;
62
 
63
    if (parent->count == parent->hdr.maxcount )
64
        {
65
        /* maximum number of arguments exceeded */
66
        errorcode = EMAXCOUNT;
67
        }
68
    else if (!argval)
69
        {
70
        /* a valid argument with no argument value was given. */
71
        /* This happens when an optional argument value was invoked. */
72
        /* leave parent arguiment value unaltered but still count the argument. */
73
        parent->count++;
74
        }
75
    else
76
        {
77
        struct privhdr *priv = (struct privhdr*)parent->hdr.priv;
78
 
79
       /* test the current argument value for a match with the regular expression */
80
        /* if a match is detected, record the argument value in the arg_rex struct */
81
        errorcode = regexec(&(priv->regex), argval, 0, NULL, 0);
82
        if (errorcode==0)
83
            parent->sval[parent->count++] = argval;
84
        }
85
 
86
    /*printf("%s:scanfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
87
    return errorcode;
88
    }
89
 
90
static int checkfn(struct arg_rex *parent)
91
    {
92
    int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
93
    struct privhdr *priv = (struct privhdr*)parent->hdr.priv;
94
 
95
    /* free the regex "program" we constructed in resetfn */
96
    regfree(&(priv->regex));
97
 
98
    /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
99
    return errorcode;
100
    }
101
 
102
static void errorfn(struct arg_rex *parent, FILE *fp, int errorcode, const char *argval, const char *progname)
103
    {
104
    const char *shortopts = parent->hdr.shortopts;
105
    const char *longopts  = parent->hdr.longopts;
106
    const char *datatype  = parent->hdr.datatype;
107
 
108
    /* make argval NULL safe */
109
    argval = argval ? argval : "";
110
 
111
    fprintf(fp,"%s: ",progname);
112
    switch(errorcode)
113
        {
114
        case EMINCOUNT:
115
            fputs("missing option ",fp);
116
            arg_print_option(fp,shortopts,longopts,datatype,"\n");
117
            break;
118
 
119
        case EMAXCOUNT:
120
            fputs("excess option ",fp);
121
            arg_print_option(fp,shortopts,longopts,argval,"\n");
122
            break;
123
 
124
        case REG_NOMATCH:
125
            fputs("illegal value  ",fp);
126
            arg_print_option(fp,shortopts,longopts,argval,"\n");
127
            break;
128
 
129
        default:
130
            {
131
            char errbuff[256];
132
            regerror(errorcode, NULL, errbuff, sizeof(errbuff));
133
            printf("%s\n", errbuff);
134
            }
135
            break;
136
        }
137
    }
138
 
139
 
140
struct arg_rex* arg_rex0(const char* shortopts,
141
                         const char* longopts,
142
                         const char* pattern,
143
                         const char *datatype,
144
                         int flags,
145
                         const char *glossary)
146
    {
147
    return arg_rexn(shortopts,longopts,pattern,datatype,0,1,flags,glossary);
148
    }
149
 
150
struct arg_rex* arg_rex1(const char* shortopts,
151
                         const char* longopts,
152
                         const char* pattern,
153
                         const char *datatype,
154
                         int flags,
155
                         const char *glossary)
156
    {
157
    return arg_rexn(shortopts,longopts,pattern,datatype,1,1,flags,glossary);
158
    }
159
 
160
 
161
struct arg_rex* arg_rexn(const char* shortopts,
162
                         const char* longopts,
163
                         const char* pattern,
164
                         const char *datatype,
165
                         int mincount,
166
                         int maxcount,
167
                         int flags,
168
                         const char *glossary)
169
    {
170
    size_t nbytes;
171
    struct arg_rex *result;
172
    struct privhdr *priv;
173
 
174
    if (!pattern)
175
        {
176
        printf("argtable: ERROR - illegal regular expression pattern \"(NULL)\"\n");
177
        printf("argtable: Bad argument table.\n");
178
        return NULL;
179
        }
180
 
181
        /* foolproof things by ensuring maxcount is not less than mincount */
182
        maxcount = (maxcount<mincount) ? mincount : maxcount;
183
 
184
    nbytes = sizeof(struct arg_rex)       /* storage for struct arg_rex */
185
           + sizeof(struct privhdr)       /* storage for private arg_rex data */
186
           + maxcount * sizeof(char*);    /* storage for sval[maxcount] array */
187
 
188
    result = (struct arg_rex*)malloc(nbytes);
189
    if (result)
190
        {
191
        int errorcode, i;
192
 
193
        /* init the arg_hdr struct */
194
        result->hdr.flag      = ARG_HASVALUE;
195
        result->hdr.shortopts = shortopts;
196
        result->hdr.longopts  = longopts;
197
        result->hdr.datatype  = datatype ? datatype : pattern;
198
        result->hdr.glossary  = glossary;
199
        result->hdr.mincount  = mincount;
200
        result->hdr.maxcount  = maxcount;
201
        result->hdr.parent    = result;
202
        result->hdr.resetfn   = (arg_resetfn*)resetfn;
203
        result->hdr.scanfn    = (arg_scanfn*)scanfn;
204
        result->hdr.checkfn   = (arg_checkfn*)checkfn;
205
        result->hdr.errorfn   = (arg_errorfn*)errorfn;
206
 
207
        /* store the arg_rex_priv struct immediately after the arg_rex struct */
208
        result->hdr.priv  = (const char**)(result+1);
209
        priv = (struct privhdr*)(result->hdr.priv);
210
        priv->pattern = pattern;
211
        priv->flags = flags | REG_NOSUB;
212
 
213
        /* store the sval[maxcount] array immediately after the arg_rex_priv struct */
214
        result->sval  = (const char**)(priv+1);
215
        result->count = 0;
216
 
217
        /* foolproof the string pointers by initialising them to reference empty strings */
218
        for (i=0; i<maxcount; i++)
219
            { result->sval[i] = ""; }
220
 
221
        /* here we construct and destroy a regex representation of the regular expression
222
           for no other reason than to force any regex errors to be trapped now rather
223
           than later. If we dont, then errors may go undetected until an argument is
224
           actually parsed. */
225
        errorcode = regcomp(&(priv->regex), priv->pattern, priv->flags);
226
        if (errorcode)
227
            {
228
            char errbuff[256];
229
            regerror(errorcode, &(priv->regex), errbuff, sizeof(errbuff));
230
            printf("argtable: %s \"%s\"\n", errbuff, priv->pattern);
231
            printf("argtable: Bad argument table.\n");
232
            }
233
        else
234
            regfree(&(priv->regex));
235
        }
236
 
237
    /*printf("arg_rexn() returns %p\n",result);*/
238
    return result;
239
    }

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.