ddb.c revision 176833
1221828Sgrehan/*- 2221828Sgrehan * Copyright (c) 2007 Robert N. M. Watson 3221828Sgrehan * All rights reserved. 4221828Sgrehan * 5221828Sgrehan * Redistribution and use in source and binary forms, with or without 6221828Sgrehan * modification, are permitted provided that the following conditions 7221828Sgrehan * are met: 8221828Sgrehan * 1. Redistributions of source code must retain the above copyright 9221828Sgrehan * notice, this list of conditions and the following disclaimer. 10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright 11221828Sgrehan * notice, this list of conditions and the following disclaimer in the 12221828Sgrehan * documentation and/or other materials provided with the distribution. 13221828Sgrehan * 14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17221828Sgrehan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24221828Sgrehan * SUCH DAMAGE. 25221828Sgrehan */ 26221828Sgrehan 27221828Sgrehan#include <sys/cdefs.h> 28221828Sgrehan__FBSDID("$FreeBSD: head/sbin/ddb/ddb.c 176833 2008-03-05 17:51:06Z brooks $"); 29221828Sgrehan 30221828Sgrehan#include <err.h> 31221828Sgrehan#include <stdio.h> 32221828Sgrehan#include <stdlib.h> 33221828Sgrehan#include <string.h> 34221828Sgrehan#include <sysexits.h> 35221828Sgrehan#include <unistd.h> 36221828Sgrehan 37221828Sgrehan#include "ddb.h" 38221828Sgrehan 39221828Sgrehanvoid ddb_readfile(char *file); 40221828Sgrehanvoid ddb_main(int argc, char *argv[]); 41221828Sgrehan 42221828Sgrehanvoid 43221828Sgrehanusage(void) 44221828Sgrehan{ 45221828Sgrehan 46221828Sgrehan fprintf(stderr, "usage:\n"); 47221828Sgrehan fprintf(stderr, "ddb script scriptname\n"); 48234761Sgrehan fprintf(stderr, "ddb script scriptname=script\n"); 49221828Sgrehan fprintf(stderr, "ddb scripts\n"); 50221828Sgrehan fprintf(stderr, "ddb unscript scriptname\n"); 51221828Sgrehan exit(EX_USAGE); 52221828Sgrehan} 53221828Sgrehan 54221828Sgrehanvoid 55221828Sgrehanddb_readfile(char *filename) 56221828Sgrehan{ 57221828Sgrehan char buf[BUFSIZ]; 58221828Sgrehan FILE* f; 59221828Sgrehan 60221828Sgrehan if ((f = fopen(filename, "r")) == NULL) 61221828Sgrehan err(EX_UNAVAILABLE, "fopen: %s", filename); 62221828Sgrehan 63221828Sgrehan#define WHITESP " \t" 64221828Sgrehan#define MAXARG 2 65221828Sgrehan while (fgets(buf, BUFSIZ, f)) { 66221828Sgrehan int argc = 0; 67221828Sgrehan char *argv[MAXARG]; 68221828Sgrehan size_t spn; 69221828Sgrehan 70221828Sgrehan spn = strlen(buf); 71221828Sgrehan if (buf[spn-1] == '\n') 72221828Sgrehan buf[spn-1] = '\0'; 73221828Sgrehan 74221828Sgrehan spn = strspn(buf, WHITESP); 75221828Sgrehan argv[0] = buf + spn; 76221828Sgrehan if (*argv[0] == '#' || *argv[0] == '\0') 77221828Sgrehan continue; 78221828Sgrehan argc++; 79221828Sgrehan 80221828Sgrehan spn = strcspn(argv[0], WHITESP); 81221828Sgrehan argv[1] = argv[0] + spn + strspn(argv[0] + spn, WHITESP);; 82221828Sgrehan argv[0][spn] = '\0'; 83221828Sgrehan if (*argv[1] != '\0') 84221828Sgrehan argc++; 85221828Sgrehan 86221828Sgrehan#ifdef DEBUG 87221828Sgrehan { 88221828Sgrehan int i; 89221828Sgrehan printf("argc = %d\n", argc); 90221828Sgrehan for (i = 0; i < argc; i++) { 91221828Sgrehan printf("arg[%d] = %s\n", i, argv[i]); 92221828Sgrehan } 93221828Sgrehan } 94221828Sgrehan#endif 95221828Sgrehan ddb_main(argc, argv); 96221828Sgrehan } 97221828Sgrehan} 98221828Sgrehan 99221828Sgrehanvoid 100221828Sgrehanddb_main(int argc, char *argv[]) 101221828Sgrehan{ 102221828Sgrehan 103221828Sgrehan if (argc < 1) 104221828Sgrehan usage(); 105221828Sgrehan 106221828Sgrehan if (strcmp(argv[0], "script") == 0) 107221828Sgrehan ddb_script(argc, argv); 108221828Sgrehan else if (strcmp(argv[0], "scripts") == 0) 109221828Sgrehan ddb_scripts(argc, argv); 110221828Sgrehan else if (strcmp(argv[0], "unscript") == 0) 111221828Sgrehan ddb_unscript(argc, argv); 112221828Sgrehan else 113221828Sgrehan usage(); 114221828Sgrehan} 115221828Sgrehan 116221828Sgrehanint 117221828Sgrehanmain(int argc, char *argv[]) 118221828Sgrehan{ 119221828Sgrehan 120221828Sgrehan /* 121221828Sgrehan * If we've only got one argument and it's an absolute path to a file, 122221828Sgrehan * interpret as a file to be read in. 123221828Sgrehan */ 124221828Sgrehan if (argc == 2 && argv[1][0] == '/' && access(argv[1], R_OK) == 0) 125221828Sgrehan ddb_readfile(argv[1]); 126221828Sgrehan else 127221828Sgrehan ddb_main(argc-1, argv+1); 128221828Sgrehan exit(EX_OK); 129221828Sgrehan} 130221828Sgrehan