util.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <stdarg.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "lkc.h"
  11. /* file already present in list? If not add it */
  12. struct file *file_lookup(const char *name)
  13. {
  14. struct file *file;
  15. const char *file_name = sym_expand_string_value(name);
  16. for (file = file_list; file; file = file->next) {
  17. if (!strcmp(name, file->name)) {
  18. free((void *)file_name);
  19. return file;
  20. }
  21. }
  22. file = xmalloc(sizeof(*file));
  23. memset(file, 0, sizeof(*file));
  24. file->name = file_name;
  25. file->next = file_list;
  26. file_list = file;
  27. return file;
  28. }
  29. /* write a dependency file as used by kbuild to track dependencies */
  30. int file_write_dep(const char *name)
  31. {
  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. out = fopen("..config.tmp", "w");
  39. if (!out)
  40. return 1;
  41. fprintf(out, "deps_config := \\\n");
  42. for (file = file_list; file; file = file->next) {
  43. if (file->next)
  44. fprintf(out, "\t%s \\\n", file->name);
  45. else
  46. fprintf(out, "\t%s\n", file->name);
  47. }
  48. fprintf(out, "\n%s: \\\n"
  49. "\t$(deps_config)\n\n", conf_get_autoconfig_name());
  50. expr_list_for_each_sym(sym_env_list, e, sym) {
  51. struct property *prop;
  52. const char *value;
  53. prop = sym_get_env_prop(sym);
  54. env_sym = prop_get_symbol(prop);
  55. if (!env_sym)
  56. continue;
  57. value = getenv(env_sym->name);
  58. if (!value)
  59. value = "";
  60. fprintf(out, "ifneq \"$(%s)\" \"%s\"\n", env_sym->name, value);
  61. fprintf(out, "%s: FORCE\n", conf_get_autoconfig_name());
  62. fprintf(out, "endif\n");
  63. }
  64. fprintf(out, "\n$(deps_config): ;\n");
  65. fclose(out);
  66. rename("..config.tmp", name);
  67. return 0;
  68. }
  69. /* Allocate initial growable string */
  70. struct gstr str_new(void)
  71. {
  72. struct gstr gs;
  73. gs.s = xmalloc(sizeof(char) * 64);
  74. gs.len = 64;
  75. gs.max_width = 0;
  76. strcpy(gs.s, "\0");
  77. return gs;
  78. }
  79. /* Free storage for growable string */
  80. void str_free(struct gstr *gs)
  81. {
  82. if (gs->s)
  83. free(gs->s);
  84. gs->s = NULL;
  85. gs->len = 0;
  86. }
  87. /* Append to growable string */
  88. void str_append(struct gstr *gs, const char *s)
  89. {
  90. size_t l;
  91. if (s) {
  92. l = strlen(gs->s) + strlen(s) + 1;
  93. if (l > gs->len) {
  94. gs->s = realloc(gs->s, l);
  95. gs->len = l;
  96. }
  97. strcat(gs->s, s);
  98. }
  99. }
  100. /* Append printf formatted string to growable string */
  101. void str_printf(struct gstr *gs, const char *fmt, ...)
  102. {
  103. va_list ap;
  104. char s[10000]; /* big enough... */
  105. va_start(ap, fmt);
  106. vsnprintf(s, sizeof(s), fmt, ap);
  107. str_append(gs, s);
  108. va_end(ap);
  109. }
  110. /* Retrieve value of growable string */
  111. const char *str_get(struct gstr *gs)
  112. {
  113. return gs->s;
  114. }
  115. void *xmalloc(size_t size)
  116. {
  117. void *p = malloc(size);
  118. if (p)
  119. return p;
  120. fprintf(stderr, "Out of memory.\n");
  121. exit(1);
  122. }
  123. void *xcalloc(size_t nmemb, size_t size)
  124. {
  125. void *p = calloc(nmemb, size);
  126. if (p)
  127. return p;
  128. fprintf(stderr, "Out of memory.\n");
  129. exit(1);
  130. }