ddb.c revision 176833
1116742Ssam/*- 2116904Ssam * Copyright (c) 2007 Robert N. M. Watson 3186904Ssam * All rights reserved. 4116742Ssam * 5116742Ssam * Redistribution and use in source and binary forms, with or without 6116742Ssam * modification, are permitted provided that the following conditions 7116742Ssam * are met: 8116742Ssam * 1. Redistributions of source code must retain the above copyright 9116742Ssam * notice, this list of conditions and the following disclaimer. 10116904Ssam * 2. Redistributions in binary form must reproduce the above copyright 11116904Ssam * notice, this list of conditions and the following disclaimer in the 12116904Ssam * documentation and/or other materials provided with the distribution. 13116904Ssam * 14116742Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15116904Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16116904Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17116904Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18116904Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19116904Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20116904Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21116904Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22116904Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23116904Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24116904Ssam * SUCH DAMAGE. 25116742Ssam */ 26116742Ssam 27116742Ssam#include <sys/cdefs.h> 28116742Ssam__FBSDID("$FreeBSD: head/sbin/ddb/ddb.c 176833 2008-03-05 17:51:06Z brooks $"); 29116742Ssam 30116742Ssam#include <err.h> 31116742Ssam#include <stdio.h> 32116742Ssam#include <stdlib.h> 33116742Ssam#include <string.h> 34127646Ssam#include <sysexits.h> 35127646Ssam#include <unistd.h> 36178354Ssam 37127646Ssam#include "ddb.h" 38116742Ssam 39116742Ssamvoid ddb_readfile(char *file); 40116742Ssamvoid ddb_main(int argc, char *argv[]); 41164033Srwatson 42116742Ssamvoid 43116742Ssamusage(void) 44116742Ssam{ 45187678Sthompsa 46116742Ssam fprintf(stderr, "usage:\n"); 47116742Ssam fprintf(stderr, "ddb script scriptname\n"); 48152315Sru fprintf(stderr, "ddb script scriptname=script\n"); 49116742Ssam fprintf(stderr, "ddb scripts\n"); 50116742Ssam fprintf(stderr, "ddb unscript scriptname\n"); 51116742Ssam exit(EX_USAGE); 52127646Ssam} 53127646Ssam 54127646Ssamvoid 55127646Ssamddb_readfile(char *filename) 56127646Ssam{ 57127646Ssam char buf[BUFSIZ]; 58127646Ssam FILE* f; 59127646Ssam 60127646Ssam if ((f = fopen(filename, "r")) == NULL) 61127646Ssam err(EX_UNAVAILABLE, "fopen: %s", filename); 62116742Ssam 63116742Ssam#define WHITESP " \t" 64178354Ssam#define MAXARG 2 65178354Ssam while (fgets(buf, BUFSIZ, f)) { 66186904Ssam int argc = 0; 67186904Ssam char *argv[MAXARG]; 68186904Ssam size_t spn; 69116742Ssam 70178354Ssam spn = strlen(buf); 71178354Ssam if (buf[spn-1] == '\n') 72178354Ssam buf[spn-1] = '\0'; 73138568Ssam 74178354Ssam spn = strspn(buf, WHITESP); 75170530Ssam argv[0] = buf + spn; 76170530Ssam if (*argv[0] == '#' || *argv[0] == '\0') 77116742Ssam continue; 78178354Ssam argc++; 79178354Ssam 80138568Ssam spn = strcspn(argv[0], WHITESP); 81178354Ssam argv[1] = argv[0] + spn + strspn(argv[0] + spn, WHITESP);; 82138568Ssam argv[0][spn] = '\0'; 83138568Ssam if (*argv[1] != '\0') 84138568Ssam argc++; 85138568Ssam 86138568Ssam#ifdef DEBUG 87138568Ssam { 88138568Ssam int i; 89138568Ssam printf("argc = %d\n", argc); 90138568Ssam for (i = 0; i < argc; i++) { 91138568Ssam printf("arg[%d] = %s\n", i, argv[i]); 92138568Ssam } 93138568Ssam } 94138568Ssam#endif 95138568Ssam ddb_main(argc, argv); 96178354Ssam } 97138568Ssam} 98178354Ssam 99138568Ssamvoid 100138568Ssamddb_main(int argc, char *argv[]) 101138568Ssam{ 102138568Ssam 103178354Ssam if (argc < 1) 104178354Ssam usage(); 105138568Ssam 106138568Ssam if (strcmp(argv[0], "script") == 0) 107138568Ssam ddb_script(argc, argv); 108138568Ssam else if (strcmp(argv[0], "scripts") == 0) 109138568Ssam ddb_scripts(argc, argv); 110138568Ssam else if (strcmp(argv[0], "unscript") == 0) 111178354Ssam ddb_unscript(argc, argv); 112138568Ssam else 113164033Srwatson usage(); 114138568Ssam} 115178354Ssam 116138568Ssamint 117138568Ssammain(int argc, char *argv[]) 118138568Ssam{ 119138568Ssam 120138568Ssam /* 121138568Ssam * If we've only got one argument and it's an absolute path to a file, 122138568Ssam * interpret as a file to be read in. 123138568Ssam */ 124138568Ssam if (argc == 2 && argv[1][0] == '/' && access(argv[1], R_OK) == 0) 125138568Ssam ddb_readfile(argv[1]); 126138568Ssam else 127138568Ssam ddb_main(argc-1, argv+1); 128138568Ssam exit(EX_OK); 129138568Ssam} 130138568Ssam