util.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /* Allocate and assign growable string */
  80. struct gstr str_assign(const char *s)
  81. {
  82. struct gstr gs;
  83. gs.s = strdup(s);
  84. gs.len = strlen(s) + 1;
  85. gs.max_width = 0;
  86. return gs;
  87. }
  88. /* Free storage for growable string */
  89. void str_free(struct gstr *gs)
  90. {
  91. if (gs->s)
  92. free(gs->s);
  93. gs->s = NULL;
  94. gs->len = 0;
  95. }
  96. /* Append to growable string */
  97. void str_append(struct gstr *gs, const char *s)
  98. {
  99. size_t l;
  100. if (s) {
  101. l = strlen(gs->s) + strlen(s) + 1;
  102. if (l > gs->len) {
  103. gs->s = realloc(gs->s, l);
  104. gs->len = l;
  105. }
  106. strcat(gs->s, s);
  107. }
  108. }
  109. /* Append printf formatted string to growable string */
  110. void str_printf(struct gstr *gs, const char *fmt, ...)
  111. {
  112. va_list ap;
  113. char s[10000]; /* big enough... */
  114. va_start(ap, fmt);
  115. vsnprintf(s, sizeof(s), fmt, ap);
  116. str_append(gs, s);
  117. va_end(ap);
  118. }
  119. /* Retrieve value of growable string */
  120. const char *str_get(struct gstr *gs)
  121. {
  122. return gs->s;
  123. }
  124. void *xmalloc(size_t size)
  125. {
  126. void *p = malloc(size);
  127. if (p)
  128. return p;
  129. fprintf(stderr, "Out of memory.\n");
  130. exit(1);
  131. }
  132. void *xcalloc(size_t nmemb, size_t size)
  133. {
  134. void *p = calloc(nmemb, size);
  135. if (p)
  136. return p;
  137. fprintf(stderr, "Out of memory.\n");
  138. exit(1);
  139. }