args.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2011-2018 K. Lange
  5. *
  6. * Kernel Argument Manager
  7. *
  8. * Arguments to the kernel are provided from the bootloader and
  9. * provide information such as what mode to pass to init, or what
  10. * hard disk partition should be mounted as root.
  11. *
  12. * This module provides access
  13. */
  14. #include <kernel/system.h>
  15. #include <kernel/logging.h>
  16. #include <kernel/args.h>
  17. #include <kernel/tokenize.h>
  18. #include <toaru/hashmap.h>
  19. char * cmdline = NULL;
  20. hashmap_t * kernel_args_map = NULL;
  21. /**
  22. * Check if an argument was provided to the kernel. If the argument is
  23. * a simple switch, a response of 1 can be considered "on" for that
  24. * argument; otherwise, this just notes that the argument was present,
  25. * so the caller should check whether it is correctly set.
  26. */
  27. int args_present(char * karg) {
  28. if (!kernel_args_map) return 0; /* derp */
  29. return hashmap_has(kernel_args_map, karg);
  30. }
  31. /**
  32. * Return the value associated with an argument provided to the kernel.
  33. */
  34. char * args_value(char * karg) {
  35. if (!kernel_args_map) return 0; /* derp */
  36. return hashmap_get(kernel_args_map, karg);
  37. }
  38. /**
  39. * Parse the given arguments to the kernel.
  40. *
  41. * @param arg A string containing all arguments, separated by spaces.
  42. */
  43. void args_parse(char * _arg) {
  44. /* Sanity check... */
  45. if (!_arg) { return; }
  46. char * arg = strdup(_arg);
  47. char * argv[1024];
  48. int argc = tokenize(arg, " ", argv);
  49. /* New let's parse the tokens into the arguments list so we can index by key */
  50. if (!kernel_args_map) {
  51. kernel_args_map = hashmap_create(10);
  52. }
  53. for (int i = 0; i < argc; ++i) {
  54. char * c = strdup(argv[i]);
  55. char * name;
  56. char * value;
  57. name = c;
  58. value = NULL;
  59. /* Find the first = and replace it with a null */
  60. char * v = c;
  61. while (*v) {
  62. if (*v == '=') {
  63. *v = '\0';
  64. v++;
  65. value = v;
  66. goto _break;
  67. }
  68. v++;
  69. }
  70. _break:
  71. hashmap_set(kernel_args_map, name, value);
  72. }
  73. free(arg);
  74. }