util.c 3.3 KB

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