util.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. char *str;
  31. char buf[PATH_MAX+1], buf2[PATH_MAX+1], dir[PATH_MAX+1];
  32. struct symbol *sym, *env_sym;
  33. struct expr *e;
  34. struct file *file;
  35. FILE *out;
  36. if (!name)
  37. name = ".kconfig.d";
  38. strcpy(dir, conf_get_configname());
  39. str = strrchr(dir, '/');
  40. if (str)
  41. str[1] = 0;
  42. else
  43. dir[0] = 0;
  44. sprintf(buf, "%s..config.tmp", dir);
  45. out = fopen(buf, "w");
  46. if (!out)
  47. return 1;
  48. fprintf(out, "deps_config := \\\n");
  49. for (file = file_list; file; file = file->next) {
  50. if (file->next)
  51. fprintf(out, "\t%s \\\n", file->name);
  52. else
  53. fprintf(out, "\t%s\n", file->name);
  54. }
  55. fprintf(out, "\n%s: \\\n"
  56. "\t$(deps_config)\n\n", conf_get_autoconfig_name());
  57. expr_list_for_each_sym(sym_env_list, e, sym) {
  58. struct property *prop;
  59. const char *value;
  60. prop = sym_get_env_prop(sym);
  61. env_sym = prop_get_symbol(prop);
  62. if (!env_sym)
  63. continue;
  64. value = getenv(env_sym->name);
  65. if (!value)
  66. value = "";
  67. fprintf(out, "ifneq \"$(%s)\" \"%s\"\n", env_sym->name, value);
  68. fprintf(out, "%s: FORCE\n", conf_get_autoconfig_name());
  69. fprintf(out, "endif\n");
  70. }
  71. fprintf(out, "\n$(deps_config): ;\n");
  72. fclose(out);
  73. sprintf(buf2, "%s%s", dir, name);
  74. rename(buf, buf2);
  75. return 0;
  76. }
  77. /* Allocate initial growable string */
  78. struct gstr str_new(void)
  79. {
  80. struct gstr gs;
  81. gs.s = malloc(sizeof(char) * 64);
  82. gs.len = 64;
  83. gs.max_width = 0;
  84. strcpy(gs.s, "\0");
  85. return gs;
  86. }
  87. /* Allocate and assign growable string */
  88. struct gstr str_assign(const char *s)
  89. {
  90. struct gstr gs;
  91. gs.s = strdup(s);
  92. gs.len = strlen(s) + 1;
  93. gs.max_width = 0;
  94. return gs;
  95. }
  96. /* Free storage for growable string */
  97. void str_free(struct gstr *gs)
  98. {
  99. if (gs->s)
  100. free(gs->s);
  101. gs->s = NULL;
  102. gs->len = 0;
  103. }
  104. /* Append to growable string */
  105. void str_append(struct gstr *gs, const char *s)
  106. {
  107. size_t l;
  108. if (s) {
  109. l = strlen(gs->s) + strlen(s) + 1;
  110. if (l > gs->len) {
  111. gs->s = realloc(gs->s, l);
  112. gs->len = l;
  113. }
  114. strcat(gs->s, s);
  115. }
  116. }
  117. /* Append printf formatted string to growable string */
  118. void str_printf(struct gstr *gs, const char *fmt, ...)
  119. {
  120. va_list ap;
  121. char s[10000]; /* big enough... */
  122. va_start(ap, fmt);
  123. vsnprintf(s, sizeof(s), fmt, ap);
  124. str_append(gs, s);
  125. va_end(ap);
  126. }
  127. /* Retrieve value of growable string */
  128. const char *str_get(struct gstr *gs)
  129. {
  130. return gs->s;
  131. }