util.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
  3. * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
  4. *
  5. * Released under the terms of the GNU GPL v2.0.
  6. */
  7. #include <string.h>
  8. #include "lkc.h"
  9. /* file already present in list? If not add it */
  10. struct file *file_lookup(const char *name)
  11. {
  12. struct file *file;
  13. const char *file_name = sym_expand_string_value(name);
  14. for (file = file_list; file; file = file->next) {
  15. if (!strcmp(name, file->name)) {
  16. free((void *)file_name);
  17. return file;
  18. }
  19. }
  20. file = malloc(sizeof(*file));
  21. memset(file, 0, sizeof(*file));
  22. file->name = file_name;
  23. file->next = file_list;
  24. file_list = file;
  25. return file;
  26. }
  27. /* write a dependency file as used by kbuild to track dependencies */
  28. int file_write_dep(const char *name)
  29. {
  30. struct symbol *sym, *env_sym;
  31. struct expr *e;
  32. struct file *file;
  33. FILE *out;
  34. if (!name)
  35. name = ".kconfig.d";
  36. out = fopen("..config.tmp", "w");
  37. if (!out)
  38. return 1;
  39. fprintf(out, "deps_config := \\\n");
  40. for (file = file_list; file; file = file->next) {
  41. if (file->next)
  42. fprintf(out, "\t%s \\\n", file->name);
  43. else
  44. fprintf(out, "\t%s\n", file->name);
  45. }
  46. fprintf(out, "\n%s: \\\n"
  47. "\t$(deps_config)\n\n", conf_get_autoconfig_name());
  48. expr_list_for_each_sym(sym_env_list, e, sym) {
  49. struct property *prop;
  50. const char *value;
  51. prop = sym_get_env_prop(sym);
  52. env_sym = prop_get_symbol(prop);
  53. if (!env_sym)
  54. continue;
  55. value = getenv(env_sym->name);
  56. if (!value)
  57. value = "";
  58. fprintf(out, "ifneq \"$(%s)\" \"%s\"\n", env_sym->name, value);
  59. fprintf(out, "%s: FORCE\n", conf_get_autoconfig_name());
  60. fprintf(out, "endif\n");
  61. }
  62. fprintf(out, "\n$(deps_config): ;\n");
  63. fclose(out);
  64. rename("..config.tmp", name);
  65. return 0;
  66. }
  67. /* Allocate initial growable string */
  68. struct gstr str_new(void)
  69. {
  70. struct gstr gs;
  71. gs.s = malloc(sizeof(char) * 64);
  72. gs.len = 64;
  73. gs.max_width = 0;
  74. strcpy(gs.s, "\0");
  75. return gs;
  76. }
  77. /* Allocate and assign growable string */
  78. struct gstr str_assign(const char *s)
  79. {
  80. struct gstr gs;
  81. gs.s = strdup(s);
  82. gs.len = strlen(s) + 1;
  83. gs.max_width = 0;
  84. return gs;
  85. }
  86. /* Free storage for growable string */
  87. void str_free(struct gstr *gs)
  88. {
  89. if (gs->s)
  90. free(gs->s);
  91. gs->s = NULL;
  92. gs->len = 0;
  93. }
  94. /* Append to growable string */
  95. void str_append(struct gstr *gs, const char *s)
  96. {
  97. size_t l;
  98. if (s) {
  99. l = strlen(gs->s) + strlen(s) + 1;
  100. if (l > gs->len) {
  101. gs->s = realloc(gs->s, l);
  102. gs->len = l;
  103. }
  104. strcat(gs->s, s);
  105. }
  106. }
  107. /* Append printf formatted string to growable string */
  108. void str_printf(struct gstr *gs, const char *fmt, ...)
  109. {
  110. va_list ap;
  111. char s[10000]; /* big enough... */
  112. va_start(ap, fmt);
  113. vsnprintf(s, sizeof(s), fmt, ap);
  114. str_append(gs, s);
  115. va_end(ap);
  116. }
  117. /* Retrieve value of growable string */
  118. const char *str_get(struct gstr *gs)
  119. {
  120. return gs->s;
  121. }