util.c 3.4 KB

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