vars.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* $OpenBSD: vars.c,v 1.15 2014/07/11 12:33:12 jasper Exp $ */
  2. /*
  3. * Copyright (c) 1998-2000 Michael Shalayeff
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. *
  27. */
  28. #include <sys/param.h>
  29. #include <libsa.h>
  30. #include <sys/reboot.h>
  31. #include <lib/libkern/funcs.h>
  32. #include "cmd.h"
  33. extern char prog_ident[];
  34. extern int debug;
  35. int db_console = -1;
  36. static int Xaddr(void);
  37. static int Xdevice(void);
  38. #ifdef DEBUG
  39. static int Xdebug(void);
  40. #endif
  41. static int Xdb_console(void);
  42. static int Ximage(void);
  43. static int Xhowto(void);
  44. static int Xtty(void);
  45. static int Xtimeout(void);
  46. int Xset(void);
  47. int Xenv(void);
  48. const struct cmd_table cmd_set[] = {
  49. {"addr", CMDT_VAR, Xaddr},
  50. {"howto", CMDT_VAR, Xhowto},
  51. #ifdef DEBUG
  52. {"debug", CMDT_VAR, Xdebug},
  53. #endif
  54. {"device", CMDT_VAR, Xdevice},
  55. {"tty", CMDT_VAR, Xtty},
  56. {"image", CMDT_VAR, Ximage},
  57. {"timeout",CMDT_VAR, Xtimeout},
  58. {"db_console", CMDT_VAR, Xdb_console},
  59. {NULL,0}
  60. };
  61. #ifdef DEBUG
  62. static int
  63. Xdebug(void)
  64. {
  65. if (cmd.argc != 2)
  66. printf( "o%s\n", debug? "n": "ff" );
  67. else
  68. debug = (cmd.argv[1][0] == '0' ||
  69. (cmd.argv[1][0] == 'o' && cmd.argv[1][1] == 'f'))?
  70. 0: 1;
  71. return 0;
  72. }
  73. #endif
  74. int
  75. Xdb_console(void)
  76. {
  77. if (cmd.argc != 2) {
  78. switch (db_console) {
  79. case 0:
  80. printf("off\n");
  81. break;
  82. case 1:
  83. printf("on\n");
  84. break;
  85. default:
  86. printf("unset\n");
  87. break;
  88. }
  89. } else {
  90. if (strcmp(cmd.argv[1], "0") == 0 ||
  91. strcmp(cmd.argv[1], "off") == 0)
  92. db_console = 0;
  93. else if (strcmp(cmd.argv[1], "1") == 0 ||
  94. strcmp(cmd.argv[1], "on") == 0)
  95. db_console = 1;
  96. }
  97. return (0);
  98. }
  99. static int
  100. Xtimeout(void)
  101. {
  102. if (cmd.argc != 2)
  103. printf( "%d\n", cmd.timeout );
  104. else
  105. cmd.timeout = (int)strtol( cmd.argv[1], (char **)NULL, 0 );
  106. return 0;
  107. }
  108. /* called only w/ no arguments */
  109. int
  110. Xset(void)
  111. {
  112. const struct cmd_table *ct;
  113. printf("%s\n", prog_ident);
  114. for (ct = cmd_set; ct->cmd_name != NULL; ct++) {
  115. printf("%s\t ", ct->cmd_name);
  116. (*ct->cmd_exec)();
  117. }
  118. return 0;
  119. }
  120. static int
  121. Xdevice(void)
  122. {
  123. if (cmd.argc != 2)
  124. printf("%s\n", cmd.bootdev);
  125. else
  126. strlcpy(cmd.bootdev, cmd.argv[1], sizeof(cmd.bootdev));
  127. return 0;
  128. }
  129. static int
  130. Ximage(void)
  131. {
  132. if (cmd.argc != 2)
  133. printf("%s\n", cmd.image);
  134. else
  135. strlcpy(cmd.image, cmd.argv[1], sizeof(cmd.image));
  136. return 0;
  137. }
  138. static int
  139. Xaddr(void)
  140. {
  141. if (cmd.argc != 2)
  142. printf("%p\n", cmd.addr);
  143. else
  144. cmd.addr = (void *)strtol(cmd.argv[1], NULL, 0);
  145. return 0;
  146. }
  147. static int
  148. Xtty(void)
  149. {
  150. dev_t dev;
  151. if (cmd.argc != 2)
  152. printf("%s\n", ttyname(0));
  153. else {
  154. dev = ttydev(cmd.argv[1]);
  155. if (dev == NODEV)
  156. printf("%s not a console device\n", cmd.argv[1]);
  157. else {
  158. printf("switching console to %s\n", cmd.argv[1]);
  159. if (cnset(dev))
  160. printf("%s console not present\n",
  161. cmd.argv[1]);
  162. else
  163. printf("%s\n", prog_ident);
  164. }
  165. }
  166. return 0;
  167. }
  168. static int
  169. Xhowto(void)
  170. {
  171. if (cmd.argc == 1) {
  172. if (cmd.boothowto) {
  173. putchar('-');
  174. if (cmd.boothowto & RB_ASKNAME)
  175. putchar('a');
  176. if (cmd.boothowto & RB_CONFIG)
  177. putchar('c');
  178. if (cmd.boothowto & RB_SINGLE)
  179. putchar('s');
  180. if (cmd.boothowto & RB_KDB)
  181. putchar('d');
  182. }
  183. putchar('\n');
  184. } else
  185. bootparse(1);
  186. return 0;
  187. }
  188. int
  189. bootparse(int i)
  190. {
  191. char *cp;
  192. int howto = cmd.boothowto;
  193. for (; i < cmd.argc; i++) {
  194. cp = cmd.argv[i];
  195. if (*cp == '-') {
  196. while (*++cp) {
  197. switch (*cp) {
  198. case 'a':
  199. howto |= RB_ASKNAME;
  200. break;
  201. case 'c':
  202. howto |= RB_CONFIG;
  203. break;
  204. case 's':
  205. howto |= RB_SINGLE;
  206. break;
  207. case 'd':
  208. howto |= RB_KDB;
  209. break;
  210. default:
  211. printf("howto: bad option: %c\n", *cp);
  212. return 1;
  213. }
  214. }
  215. } else {
  216. printf("boot: illegal argument %s\n", cmd.argv[i]);
  217. return 1;
  218. }
  219. }
  220. cmd.boothowto = howto;
  221. return 0;
  222. }
  223. /*
  224. * maintain environment as a sequence of '\n' separated
  225. * variable definitions in the form <name>=[<value>]
  226. * terminated by the usual '\0'
  227. */
  228. char *environ;
  229. int
  230. Xenv(void)
  231. {
  232. if (cmd.argc == 1) {
  233. if (environ)
  234. printf("%s", environ);
  235. else
  236. printf("empty\n");
  237. } else {
  238. char *p, *q;
  239. int l;
  240. for (p = environ; p && *p; p = q) {
  241. l = strlen(cmd.argv[1]);
  242. for (q = p; *q != '='; q++)
  243. ;
  244. l = max(l, q - p) + 1;
  245. for (q = p; *q != '\n'; q++)
  246. ;
  247. if (*q)
  248. q++;
  249. if (!strncmp(p, cmd.argv[1], l)) {
  250. while((*p++ = *q++))
  251. ;
  252. p--;
  253. }
  254. }
  255. if (!p)
  256. p = environ = alloc(4096);
  257. snprintf(p, environ + 4096 - p, "%s=%s\n",
  258. cmd.argv[1], (cmd.argc==3?cmd.argv[2]:""));
  259. }
  260. return 0;
  261. }