Home | History | Annotate | Line # | Download | only in btdevctl
btdevctl.c revision 1.1
      1 /*	$NetBSD: btdevctl.c,v 1.1 2006/08/13 09:03:23 plunky Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 Itronix Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Iain Hibbert for Itronix Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of Itronix Inc. may not be used to endorse
     18  *    or promote products derived from this software without specific
     19  *    prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc.\n"
     36 	    "All rights reserved.\n");
     37 __RCSID("$NetBSD: btdevctl.c,v 1.1 2006/08/13 09:03:23 plunky Exp $");
     38 
     39 #include <prop/proplib.h>
     40 
     41 #include <err.h>
     42 #include <fcntl.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <unistd.h>
     47 
     48 #include "btdevctl.h"
     49 
     50 const char *config_file = "/var/db/btdev.xml";
     51 const char *control_file = NULL;
     52 
     53 struct command {
     54 	const char	*command;
     55 	int		(*handler)(int, char **);
     56 	const char	*description;
     57 } commands[] = {
     58 	{ "Attach",	dev_attach,	"attach device"		},
     59 	{ "Detach",	dev_detach,	"detach device"		},
     60 	{ "Parse",	hid_parse,	"parse HID descriptor"	},
     61 	{ "Print",	cfg_print,	"print config entry"	},
     62 	{ "Query",	cfg_query,	"make config entry"	},
     63 	{ "Remove",	cfg_remove,	"remove config entry"	},
     64 	{ NULL }
     65 };
     66 
     67 static void
     68 usage(void)
     69 {
     70 	struct command *cmd;
     71 
     72 	fprintf(stderr, "Usage: %s <btdev> <command> [parameters]\n"
     73 			"Commands:\n",
     74 			getprogname());
     75 
     76 	for (cmd = commands ; cmd->command != NULL ; cmd++)
     77 		fprintf(stderr, "\t%-13s%s\n", cmd->command, cmd->description);
     78 
     79 	exit(EXIT_FAILURE);
     80 }
     81 
     82 int
     83 main(int argc, char *argv[])
     84 {
     85 	struct command *cmd;
     86 
     87 	if (argc < 3)
     88 		usage();
     89 
     90 	control_file = argv[1];
     91 
     92 	for (cmd = commands ; cmd->command != NULL ; cmd++) {
     93 		if (strcasecmp(argv[2], cmd->command) == 0)
     94 			return (cmd->handler)(argc - 2, argv + 2);
     95 	}
     96 
     97 	errx(EXIT_FAILURE, "%s: unknown command", argv[2]);
     98 }
     99 
    100 prop_dictionary_t
    101 read_config(void)
    102 {
    103 	prop_dictionary_t dict;
    104 	char *xml;
    105 	off_t len;
    106 	int fd;
    107 
    108 	fd = open(config_file, O_RDONLY, 0);
    109 	if (fd < 0)
    110 		return NULL;
    111 
    112 	len = lseek(fd, 0, SEEK_END);
    113 	if (len == 0) {
    114 		close(fd);
    115 		return NULL;
    116 	}
    117 
    118 	xml = malloc(len);
    119 	if (xml == NULL) {
    120 		close(fd);
    121 		return NULL;
    122 	}
    123 
    124 	(void)lseek(fd, 0, SEEK_SET);
    125 	if (read(fd, xml, len) != len) {
    126 		close(fd);
    127 		free(xml);
    128 		return NULL;
    129 	}
    130 
    131 	dict = prop_dictionary_internalize(xml);
    132 
    133 	free(xml);
    134 	close(fd);
    135 
    136 	return dict;
    137 }
    138 
    139 int
    140 write_config(prop_dictionary_t dict)
    141 {
    142 	char *xml;
    143 	size_t len;
    144 	int fd;
    145 
    146 	fd = open(config_file, O_WRONLY | O_CREAT | O_TRUNC, 0640);
    147 	if (fd < 0)
    148 		return 0;
    149 
    150 	xml = prop_dictionary_externalize(dict);
    151 	if (xml == NULL) {
    152 		close(fd);
    153 		return 0;
    154 	}
    155 
    156 	len = strlen(xml);
    157 	if (write(fd, xml, len) != len) {
    158 		free(xml);
    159 		close(fd);
    160 		return 0;
    161 	}
    162 
    163 	free(xml);
    164 	close(fd);
    165 
    166 	return 1;
    167 }
    168