use strbuff instead of str_buff & str_idx
[build-config.git] / src / config / lxrgmr-code / util.c
blobe6f71ca821e9a3e5417e5a99249702e77f10da25
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
4 * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
5 */
7 #include <stdarg.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "lkglob.h"
12 /************************************************************************
13 * $PROJECT Project
15 * (c) Copyright 2009, $COMPANY_EN. Inc., $CITY_EN, $COUNTRY_EN
16 * (c) Copyright 2009, $COMPANY_CN. Inc., $CITY_CN, $COUNTRY_CN
17 ************************************************************************
18 * filename: Util.c
19 * function: Util模块。
20 * createdate: 2023-04-03
21 * author: devenkong@126.com
22 * note:this file is wirten for various util functions.
25 ************************************************************************/
26 /* Modify record */
27 /************************************************************************
28 * date: $DATE
29 * author: $AUTHOR
30 * note:
32 ************************************************************************/
35 模块引入函数:
40 数据接口:
44 /*******************************
45 *******************************
46 * 1. Include Files
47 *******************************
48 *******************************/
49 #define MODULE_NAME MODULE_CLASS_SYSLIB
51 /* System include files */
52 //#include "type.h"
53 #include <stdio.h>
54 #include <errno.h>
55 #include <memory.h>
56 #include <string.h>
57 #include <stdlib.h>
58 #include <unistd.h>
60 #include <stdarg.h>
62 /* Project module include files */
63 #include "Util.h"
64 #include "StrBuff.h"
65 #include "AQStack.h"
67 /* YYSTYPE & yylval */
68 #include "gmr/parser.tab.h"
70 /* Private include files */
71 //#include <XXX_Private.h>
74 /*******************************
75 *******************************
76 * 2. Global Variables &
77 * Micro Defines &
78 * Type Defines &
79 * Type Proto
80 *******************************
81 *******************************/
83 int last_token = 0;
86 * set content flag automatically.
87 * referenced from script.l(script.lex.c).
89 int g_content_before_cmnt = 0;
90 #define RETURN(token) g_content_before_cmnt=1; return token;
92 /* those variables are defined in script.lex.c */
93 extern char *yytext;
94 extern int yylineno;
95 extern size_t yyleng;
97 /* it is defined in menu.c */
98 extern struct file *file_list;
101 #define START_STRSIZE 16
103 static int curr_state = 0;
104 static int init_state = 0;
106 static volatile uint8 gau8StateQStackBuf[128];
107 static QSTACK gstStateQStack;
111 /*******************************
112 *******************************
113 * 3. Private Functions Implement
114 *******************************
115 *******************************/
117 /*********************************************************************
118 * 名字: Util_Example
119 * 功能: util功能模块的初始化函数。
120 * 参数: i32Value1:32位带符号整形值参数1
121 * 返回: 0表示运行ok,负数表示err id。
122 *********************************************************************/
123 static int32 Util_Example (int32 i32Value1)
125 return 0;
129 /*******************************
130 *******************************
131 * 4. Public Functions Implement
132 *******************************
133 *******************************/
135 void *xmalloc(size_t size)
137 void *p = malloc(size);
138 if (p)
139 return p;
140 fprintf(stderr, "Out of memory.\n");
141 exit(1);
144 void *xcalloc(size_t nmemb, size_t size)
146 void *p = calloc(nmemb, size);
147 if (p)
148 return p;
149 fprintf(stderr, "Out of memory.\n");
150 exit(1);
153 void *xrealloc(void *p, size_t size)
155 p = realloc(p, size);
156 if (p)
157 return p;
158 fprintf(stderr, "Out of memory.\n");
159 exit(1);
162 char *xstrdup(const char *s)
164 char *p;
166 p = strdup(s);
167 if (p)
168 return p;
169 fprintf(stderr, "Out of memory.\n");
170 exit(1);
173 char *xstrndup(const char *s, size_t n)
175 char *p;
177 p = strndup(s, n);
178 if (p)
179 return p;
180 fprintf(stderr, "Out of memory.\n");
181 exit(1);
184 /* it's just a wrap for interface name. */
185 void xfree(const char *s)
187 if (s)
188 free((void *)s);
191 /*********************************************************************
192 * file operation.
193 *********************************************************************/
195 /* file already present in list? If not add it */
196 struct file *file_lookup(const char *name)
198 struct file *file;
199 // TBD: append expand func if link with corresonding code.
200 char *file_name = (char *)name;//sym_expand_string_value(name);
202 printf("name = %s\n\n", name);
203 printf("file_name = %s\n\n", file_name);
204 for (file = file_list; file; file = file->next) {
205 if (!strcmp(name, file->name)) {
206 free(file_name);
207 return file;
211 file = xmalloc(sizeof(*file));
212 memset(file, 0, sizeof(*file));
213 file->name = file_name;
214 file->next = file_list;
215 file_list = file;
216 return file;
219 /* write a dependency file as used by kbuild to track dependencies */
220 int file_write_dep(const char *name)
222 #ifndef LXRGMR_CODE
223 char *str;
224 char buf[PATH_MAX+1], buf2[PATH_MAX+1], dir[PATH_MAX+1];
225 struct symbol *sym, *env_sym;
226 struct expr *e;
227 struct file *file;
228 FILE *out;
230 extern int make_parent_dir(const char *path);
232 // TBD: append this.
233 // if (!name)
234 // name = ".kconfig.d";
236 strcpy(dir, conf_get_configname());
237 str = strrchr(dir, '/');
238 if (str)
239 str[1] = 0;
240 else
241 dir[0] = 0;
243 sprintf(buf, "%s..config.tmp", dir);
244 out = fopen(buf, "w");
245 if (!out)
246 return 1;
247 fprintf(out, "deps_config := \\\n");
248 for (file = file_list; file; file = file->next) {
249 if (file->next)
250 fprintf(out, "\t%s \\\n", file->name);
251 else
252 fprintf(out, "\t%s\n", file->name);
254 fprintf(out, "\n%s: \\\n"
255 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
257 // TBD:
258 // this piece of code should be replaced by functions below.
259 // append code to env_write_dep(), and check if there are other place
260 // invoke function.
261 //env_write_dep(out, conf_get_autoconfig_name());
262 expr_list_for_each_sym(sym_env_list, e, sym) {
263 struct property *prop;
264 const char *value;
266 prop = sym_get_env_prop(sym);
267 env_sym = prop_get_symbol(prop);
268 if (!env_sym)
269 continue;
270 value = getenv(env_sym->name);
271 if (!value)
272 value = "";
273 fprintf(out, "ifneq \"$(%s)\" \"%s\"\n", env_sym->name, value);
274 fprintf(out, "%s: FORCE\n", conf_get_autoconfig_name());
275 fprintf(out, "endif\n");
278 fprintf(out, "\n$(deps_config): ;\n");
279 fclose(out);
281 if (make_parent_dir(name))
282 return 1;
283 sprintf(buf2, "%s%s", dir, name);
284 rename(buf, buf2);
285 // rename("..config.tmp", name);
286 #endif
288 return 0;
291 /*********************************************************************
292 * state operation.
293 *********************************************************************/
295 void STATE_INIT (int state)
297 curr_state = state;
298 init_state = state;
300 QStackIF_InitQueue (&gstStateQStack, (char *)&gau8StateQStackBuf, 128);
301 STATE_SET(curr_state);
304 void STATE_ENTER (int state)
306 QStackIF_PushU32(&gstStateQStack, (unsigned int)curr_state);
307 curr_state=state;
308 STATE_SET(curr_state);
311 int STATE_RESTORE (void)
313 if (QStackIF_IsEmpty(&gstStateQStack))
315 printf("err: can not pop state from stack.\n");
316 return init_state;
319 curr_state = (int)QStackIF_PopU32(&gstStateQStack);
320 STATE_SET(curr_state);
322 return curr_state;
325 #if 0
326 /*********************************************************************
327 * 名字: UtilIF_Testing
328 * 功能: util模块测试函数。
329 * 参数: 无
330 * 返回: 无
331 *********************************************************************/
332 void UtilIF_Testing (void)
334 UtilIF_Init ();
336 Util_Example (1);
338 #else
339 void UtilIF_Testing (void){}
340 #endif
342 /*******************************
343 *******************************
344 * End Of File
345 *******************************
346 *******************************/