util.c 2.7 KB

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