ddb.c 3.5 KB

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