util.c 3.4 KB

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