- rename src/global/urjtag.c to src/global/log-error.c
- introduce data structure for key/value pairs - have an API call initbus() that uses key/value pairs to pass in options - have API calls urj_tap_cable_parport_connect(), urj_tap_cable_usb_connect(), urj_tap_cable_other_connect(); have a tagged union in the cable driver that offers driver-specific connect() calls. git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@1636 b68d4a1b-bc3d-0410-92ed-d4ac073336b7master
parent
24224efa33
commit
ebb0861971
@ -0,0 +1,126 @@
|
||||
/*
|
||||
* $Id: params.h 1606 2009-05-19 15:06:20Z rfhh $
|
||||
*
|
||||
* Parameter list, in the vein of X parameter passing
|
||||
* Copyright (C) 2009 VU Amsterdam
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the ETC s.r.o. nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* Written by Rutger Hofman <rutger at cs dot vu dot nl>, 2009
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef URJ_PARAMS_H
|
||||
#define URJ_PARAMS_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
/**
|
||||
* Supported parameter types
|
||||
*/
|
||||
typedef enum URJ_PARAM_TYPE_T
|
||||
{
|
||||
URJ_PARAM_TYPE_LU,
|
||||
URJ_PARAM_TYPE_STRING,
|
||||
URJ_PARAM_TYPE_BOOL,
|
||||
}
|
||||
urj_param_type_t;
|
||||
|
||||
/**
|
||||
* Parameter as assembled for passing to parameter-taking functions
|
||||
*/
|
||||
typedef struct URJ_PARAM
|
||||
{
|
||||
urj_param_type_t type;
|
||||
int key;
|
||||
union {
|
||||
long unsigned lu;
|
||||
const char *string;
|
||||
int enabled; /**< bool */
|
||||
} value;
|
||||
}
|
||||
urj_param_t;
|
||||
|
||||
/**
|
||||
* Description of a parameter, as recognized by a module
|
||||
*/
|
||||
typedef struct URJ_PARAM_DESCR
|
||||
{
|
||||
int key; /**< key */
|
||||
urj_param_type_t type; /**< type */
|
||||
const char *string; /**< human-friendly form */
|
||||
}
|
||||
urj_param_descr_t;
|
||||
|
||||
/**
|
||||
* Type for a module to export its recognized parameters
|
||||
*/
|
||||
typedef struct URJ_PARAM_LIST
|
||||
{
|
||||
urj_param_descr_t *list;
|
||||
size_t n;
|
||||
}
|
||||
urj_param_list_t;
|
||||
|
||||
/** Initialise a parameter assembly line */
|
||||
int urj_param_init (const urj_param_t ***bp);
|
||||
/** Clear the parameter assembly line */
|
||||
int urj_param_clear (const urj_param_t ***bp);
|
||||
|
||||
/**
|
||||
* toString function for #urj_param_t. Is not reentrant. May return a
|
||||
* pointer to a static string.
|
||||
*/
|
||||
const char *urj_param_string(const urj_param_list_t *params,
|
||||
const urj_param_t *p);
|
||||
|
||||
/**
|
||||
* Append a string-type argument to the current #urj_param_t assembly line.
|
||||
* Copies the pointer value, does no strdup(val).
|
||||
*/
|
||||
int urj_param_push_string (const urj_param_t ***bp, int key, const char *val);
|
||||
/**
|
||||
* Append a ulong-type argument to the current #urj_param_t assembly line.
|
||||
*/
|
||||
int urj_param_push_lu (const urj_param_t ***bp, int key, long unsigned val);
|
||||
/**
|
||||
* Append a bool-type argument to the current #urj_param_t assembly line.
|
||||
*/
|
||||
int urj_param_push_bool (const urj_param_t ***bp, int key, int val);
|
||||
/**
|
||||
* Transform a string representation of a param "key=value" into a #urj_param_t
|
||||
* representation, and append it to the current #urj_param_t assembly line.
|
||||
*/
|
||||
int urj_param_push (const urj_param_list_t *params, const urj_param_t ***bp,
|
||||
const char *p);
|
||||
/**
|
||||
* Utility function to count the number of items in a #urj_param_t assembly
|
||||
* line
|
||||
*/
|
||||
size_t urj_param_num (const urj_param_t *params[]);
|
||||
|
||||
#endif /* URJ_BUS_DRIVER_BRUX_BUS_H */
|
@ -0,0 +1,337 @@
|
||||
/*
|
||||
* $Id: buses.c 1606 2009-05-19 15:06:20Z rfhh $
|
||||
*
|
||||
* Copyright (C) 2003 ETC s.r.o.
|
||||
*
|
||||
* 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., 59 Temple Place - Suite 330, Boston, MA
|
||||
* 02111-1307, USA.
|
||||
*
|
||||
* Written by Marcel Telka <marcel@telka.sk>, 2003.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <sysdep.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <urjtag/error.h>
|
||||
#include <urjtag/params.h>
|
||||
|
||||
static const char *
|
||||
urj_param_key_string(const urj_param_list_t *params, int key)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < params->n; i++)
|
||||
if (key == params->list[i].key)
|
||||
return params->list[i].string;
|
||||
|
||||
return "<no such bus parameter key>";
|
||||
}
|
||||
|
||||
static int
|
||||
urj_param_parse_key(const urj_param_list_t *params, const char *p)
|
||||
{
|
||||
int i;
|
||||
const char *eq;
|
||||
|
||||
eq = strchr(p, '=');
|
||||
if (eq == NULL)
|
||||
eq = p + strlen(p);
|
||||
|
||||
for (i = 0; i < params->n; i++)
|
||||
if (strncasecmp(params->list[i].string, p, eq - p) == 0)
|
||||
return params->list[i].key;
|
||||
|
||||
urj_error_set (URJ_ERROR_SYNTAX, "unrecognized param key '%s'", p);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static urj_param_type_t
|
||||
urj_param_type_of(const urj_param_list_t *params, int key)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < params->n; i++)
|
||||
if (params->list[i].key == key)
|
||||
return params->list[i].type;
|
||||
|
||||
urj_error_set (URJ_ERROR_INVALID, "unknown key %d", key);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static urj_param_t *
|
||||
urj_param_increase (const urj_param_t ***bp)
|
||||
{
|
||||
size_t n;
|
||||
const urj_param_t **scan;
|
||||
urj_param_t *new_p;
|
||||
|
||||
scan = *bp;
|
||||
for (n = 0; scan[n] != NULL; n++)
|
||||
{
|
||||
// count
|
||||
}
|
||||
|
||||
scan = realloc (*bp, (n + 2) * sizeof *scan);
|
||||
if (scan == NULL)
|
||||
{
|
||||
urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "realloc(%s,%zd) fails",
|
||||
"*bp", (n + 2) * sizeof *scan);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*bp = scan;
|
||||
|
||||
new_p = malloc (sizeof *new_p);
|
||||
if (new_p == NULL)
|
||||
{
|
||||
urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails",
|
||||
sizeof *new_p);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
(*bp)[n] = new_p;
|
||||
(*bp)[n + 1] = NULL;
|
||||
|
||||
return new_p;
|
||||
}
|
||||
|
||||
static int
|
||||
urj_param_decrease (const urj_param_t ***bp)
|
||||
{
|
||||
size_t n;
|
||||
const urj_param_t **scan;
|
||||
|
||||
scan = *bp;
|
||||
for (n = 0; scan[n] != NULL; n++)
|
||||
{
|
||||
// count
|
||||
}
|
||||
|
||||
if (n > 0)
|
||||
{
|
||||
--n;
|
||||
free ((void *) (*bp)[n]); // Yes, I know why I cast
|
||||
(*bp)[n] = NULL;
|
||||
}
|
||||
|
||||
return URJ_STATUS_OK;
|
||||
}
|
||||
|
||||
int
|
||||
urj_param_push_string (const urj_param_t ***bp, int key, const char *val)
|
||||
{
|
||||
urj_param_t *new_p = urj_param_increase (bp);
|
||||
|
||||
if (new_p == NULL)
|
||||
return URJ_STATUS_FAIL;
|
||||
|
||||
new_p->type = URJ_PARAM_TYPE_STRING;
|
||||
new_p->key = key;
|
||||
new_p->value.string = val;
|
||||
|
||||
return URJ_STATUS_OK;
|
||||
}
|
||||
|
||||
int
|
||||
urj_param_push_lu (const urj_param_t ***bp, int key, long unsigned val)
|
||||
{
|
||||
urj_param_t *new_p = urj_param_increase (bp);
|
||||
|
||||
if (new_p == NULL)
|
||||
return URJ_STATUS_FAIL;
|
||||
|
||||
new_p->type = URJ_PARAM_TYPE_LU;
|
||||
new_p->key = key;
|
||||
new_p->value.lu = val;
|
||||
|
||||
return URJ_STATUS_OK;
|
||||
}
|
||||
|
||||
int
|
||||
urj_param_push_bool (const urj_param_t ***bp, int key, int val)
|
||||
{
|
||||
urj_param_t *new_p = urj_param_increase (bp);
|
||||
|
||||
if (new_p == NULL)
|
||||
return URJ_STATUS_FAIL;
|
||||
|
||||
new_p->type = URJ_PARAM_TYPE_BOOL;
|
||||
new_p->key = key;
|
||||
new_p->value.enabled = val;
|
||||
|
||||
return URJ_STATUS_OK;
|
||||
}
|
||||
|
||||
static int
|
||||
parse_param_lu(const char *eq, long unsigned *lu)
|
||||
{
|
||||
if (sscanf(eq + 1, "%lu", lu) == 1)
|
||||
return URJ_STATUS_OK;
|
||||
|
||||
urj_error_set (URJ_ERROR_SYNTAX, "need unsigned int, not '%s'", eq + 1);
|
||||
return URJ_STATUS_FAIL;
|
||||
}
|
||||
|
||||
static const char *
|
||||
parse_param_string(const char *eq)
|
||||
{
|
||||
return eq + 1;
|
||||
}
|
||||
|
||||
static int
|
||||
parse_param_bool(const char *eq, int *enabled)
|
||||
{
|
||||
if (eq == NULL)
|
||||
{
|
||||
*enabled = 1;
|
||||
return URJ_STATUS_OK;
|
||||
}
|
||||
|
||||
if (sscanf(eq, "%d", enabled) == 1 && (*enabled == 0 || *enabled == 1))
|
||||
return URJ_STATUS_OK;
|
||||
|
||||
urj_error_set (URJ_ERROR_SYNTAX, "need unsigned int, not '%s'", eq + 1);
|
||||
return URJ_STATUS_FAIL;
|
||||
}
|
||||
|
||||
int
|
||||
urj_param_push (const urj_param_list_t *params, const urj_param_t ***bp,
|
||||
const char *p)
|
||||
{
|
||||
int key;
|
||||
urj_param_t *new_p;
|
||||
int r = URJ_STATUS_OK;
|
||||
const char *eq;
|
||||
urj_param_type_t type;
|
||||
|
||||
key = urj_param_parse_key(params, p);
|
||||
if (key == -1)
|
||||
return URJ_STATUS_FAIL;
|
||||
|
||||
type = urj_param_type_of(params, key);
|
||||
if (type == -1)
|
||||
return URJ_STATUS_FAIL;
|
||||
|
||||
eq = strchr(p, '=');
|
||||
if (type != URJ_PARAM_TYPE_BOOL && eq == NULL)
|
||||
{
|
||||
urj_error_set (URJ_ERROR_SYNTAX,
|
||||
"param should be of the form 'key=value', not '%s'", p);
|
||||
return URJ_STATUS_FAIL;
|
||||
}
|
||||
|
||||
new_p = urj_param_increase (bp);
|
||||
if (new_p == NULL)
|
||||
return URJ_STATUS_FAIL;
|
||||
|
||||
new_p->type = type;
|
||||
new_p->key = key;
|
||||
switch (new_p->type)
|
||||
{
|
||||
case URJ_PARAM_TYPE_LU:
|
||||
r = parse_param_lu(eq, &new_p->value.lu);
|
||||
break;
|
||||
case URJ_PARAM_TYPE_STRING:
|
||||
new_p->value.string = parse_param_string(eq);
|
||||
if (new_p->value.string == NULL)
|
||||
r = URJ_STATUS_FAIL;
|
||||
break;
|
||||
case URJ_PARAM_TYPE_BOOL:
|
||||
r = parse_param_bool(eq, &new_p->value.enabled);
|
||||
break;
|
||||
}
|
||||
|
||||
if (r == URJ_STATUS_FAIL)
|
||||
urj_param_decrease (bp);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
const char *
|
||||
urj_param_string(const urj_param_list_t *params, const urj_param_t *p)
|
||||
{
|
||||
#define PARAM_BUF_SIZE 256
|
||||
static char buf[PARAM_BUF_SIZE];
|
||||
size_t size;
|
||||
|
||||
snprintf(buf, sizeof buf, "%s=", urj_param_key_string(params, p->key));
|
||||
size = strlen(buf);
|
||||
|
||||
switch (p->type)
|
||||
{
|
||||
case URJ_PARAM_TYPE_LU:
|
||||
snprintf(buf + size, sizeof buf - size, "%lu", p->value.lu);
|
||||
break;
|
||||
case URJ_PARAM_TYPE_STRING:
|
||||
snprintf(buf + size, sizeof buf - size, "%s", p->value.string);
|
||||
break;
|
||||
case URJ_PARAM_TYPE_BOOL:
|
||||
snprintf(buf + size, sizeof buf - size, "%s",
|
||||
p->value.enabled ? "on" : "off");
|
||||
break;
|
||||
default:
|
||||
return "urj_param_string(): <unimplemented>";
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
int
|
||||
urj_param_init (const urj_param_t ***bp)
|
||||
{
|
||||
*bp = calloc (1, sizeof **bp);
|
||||
if (*bp == NULL)
|
||||
{
|
||||
urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails",
|
||||
(size_t) 1, sizeof **bp);
|
||||
return URJ_STATUS_FAIL;
|
||||
}
|
||||
|
||||
*bp[0] = NULL;
|
||||
|
||||
return URJ_STATUS_OK;
|
||||
}
|
||||
|
||||
int
|
||||
urj_param_clear (const urj_param_t ***bp)
|
||||
{
|
||||
const urj_param_t **scan;
|
||||
|
||||
for (scan = *bp; *scan != NULL; scan++)
|
||||
free ((void *) *scan);
|
||||
|
||||
free (*bp);
|
||||
|
||||
return URJ_STATUS_OK;
|
||||
}
|
||||
|
||||
size_t
|
||||
urj_param_num (const urj_param_t *p[])
|
||||
{
|
||||
size_t n;
|
||||
|
||||
if (p == NULL)
|
||||
return 0;
|
||||
|
||||
for (n = 0; p[n] != NULL; n++)
|
||||
{
|
||||
/* advance n */
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
Loading…
Reference in New Issue