1 /*- 2 * Copyright (c) 2007 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <err.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <sysexits.h> 35 #include <unistd.h> 36 37 #include "ddb.h" 38 39 void ddb_readfile(char *file); 40 void ddb_main(int argc, char *argv[]); 41 42 void 43 usage(void) 44 { 45 46 fprintf(stderr, "usage: ddb script scriptname\n"); 47 fprintf(stderr, " ddb script scriptname=script\n"); 48 fprintf(stderr, " ddb scripts\n"); 49 fprintf(stderr, " ddb unscript scriptname\n"); 50 fprintf(stderr, " ddb pathname\n"); 51 exit(EX_USAGE); 52 } 53 54 void 55 ddb_readfile(char *filename) 56 { 57 char buf[BUFSIZ]; 58 FILE* f; 59 60 if ((f = fopen(filename, "r")) == NULL) 61 err(EX_UNAVAILABLE, "fopen: %s", filename); 62 63 #define WHITESP " \t" 64 #define MAXARG 2 65 while (fgets(buf, BUFSIZ, f)) { 66 int argc = 0; 67 char *argv[MAXARG]; 68 size_t spn; 69 70 spn = strlen(buf); 71 if (buf[spn-1] == '\n') 72 buf[spn-1] = '\0'; 73 74 spn = strspn(buf, WHITESP); 75 argv[0] = buf + spn; 76 if (*argv[0] == '#' || *argv[0] == '\0') 77 continue; 78 argc++; 79 80 spn = strcspn(argv[0], WHITESP); 81 argv[1] = argv[0] + spn + strspn(argv[0] + spn, WHITESP);; 82 argv[0][spn] = '\0'; 83 if (*argv[1] != '\0') 84 argc++; 85 86 #ifdef DEBUG 87 { 88 int i; 89 printf("argc = %d\n", argc); 90 for (i = 0; i < argc; i++) { 91 printf("arg[%d] = %s\n", i, argv[i]); 92 } 93 } 94 #endif 95 ddb_main(argc, argv); 96 } 97 } 98 99 void 100 ddb_main(int argc, char *argv[]) 101 { 102 103 if (argc < 1) 104 usage(); 105 106 if (strcmp(argv[0], "script") == 0) 107 ddb_script(argc, argv); 108 else if (strcmp(argv[0], "scripts") == 0) 109 ddb_scripts(argc, argv); 110 else if (strcmp(argv[0], "unscript") == 0) 111 ddb_unscript(argc, argv); 112 else 113 usage(); 114 } 115 116 int 117 main(int argc, char *argv[]) 118 { 119 120 /* 121 * If we've only got one argument and it's an absolute path to a file, 122 * interpret as a file to be read in. 123 */ 124 if (argc == 2 && argv[1][0] == '/' && access(argv[1], R_OK) == 0) 125 ddb_readfile(argv[1]); 126 else 127 ddb_main(argc-1, argv+1); 128 exit(EX_OK); 129 } 130