1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
4 * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
12 /************************************************************************
15 * (c) Copyright 2009, $COMPANY_EN. Inc., $CITY_EN, $COUNTRY_EN
16 * (c) Copyright 2009, $COMPANY_CN. Inc., $CITY_CN, $COUNTRY_CN
17 ************************************************************************
20 * createdate: 2023-04-03
21 * author: devenkong@126.com
22 * note:this file is wirten for various util functions.
25 ************************************************************************/
27 /************************************************************************
32 ************************************************************************/
44 /*******************************
45 *******************************
47 *******************************
48 *******************************/
49 #define MODULE_NAME MODULE_CLASS_SYSLIB
51 /* System include files */
62 /* Project module include files */
67 /* YYSTYPE & yylval */
68 #include "gmr/parser.tab.h"
70 /* Private include files */
71 //#include <XXX_Private.h>
74 /*******************************
75 *******************************
76 * 2. Global Variables &
80 *******************************
81 *******************************/
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 */
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 /*********************************************************************
119 * 功能: util功能模块的初始化函数。
120 * 参数: i32Value1:32位带符号整形值参数1
121 * 返回: 0表示运行ok,负数表示err id。
122 *********************************************************************/
123 static int32
Util_Example (int32 i32Value1
)
129 /*******************************
130 *******************************
131 * 4. Public Functions Implement
132 *******************************
133 *******************************/
135 void *xmalloc(size_t size
)
137 void *p
= malloc(size
);
140 fprintf(stderr
, "Out of memory.\n");
144 void *xcalloc(size_t nmemb
, size_t size
)
146 void *p
= calloc(nmemb
, size
);
149 fprintf(stderr
, "Out of memory.\n");
153 void *xrealloc(void *p
, size_t size
)
155 p
= realloc(p
, size
);
158 fprintf(stderr
, "Out of memory.\n");
162 char *xstrdup(const char *s
)
169 fprintf(stderr
, "Out of memory.\n");
173 char *xstrndup(const char *s
, size_t n
)
180 fprintf(stderr
, "Out of memory.\n");
184 /* it's just a wrap for interface name. */
185 void xfree(const char *s
)
191 /*********************************************************************
193 *********************************************************************/
195 /* file already present in list? If not add it */
196 struct file
*file_lookup(const char *name
)
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
)) {
211 file
= xmalloc(sizeof(*file
));
212 memset(file
, 0, sizeof(*file
));
213 file
->name
= file_name
;
214 file
->next
= file_list
;
219 /* write a dependency file as used by kbuild to track dependencies */
220 int file_write_dep(const char *name
)
224 char buf
[PATH_MAX
+1], buf2
[PATH_MAX
+1], dir
[PATH_MAX
+1];
225 struct symbol
*sym
, *env_sym
;
230 extern int make_parent_dir(const char *path
);
234 // name = ".kconfig.d";
236 strcpy(dir
, conf_get_configname());
237 str
= strrchr(dir
, '/');
243 sprintf(buf
, "%s..config.tmp", dir
);
244 out
= fopen(buf
, "w");
247 fprintf(out
, "deps_config := \\\n");
248 for (file
= file_list
; file
; file
= file
->next
) {
250 fprintf(out
, "\t%s \\\n", file
->name
);
252 fprintf(out
, "\t%s\n", file
->name
);
254 fprintf(out
, "\n%s: \\\n"
255 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
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
261 //env_write_dep(out, conf_get_autoconfig_name());
262 expr_list_for_each_sym(sym_env_list
, e
, sym
) {
263 struct property
*prop
;
266 prop
= sym_get_env_prop(sym
);
267 env_sym
= prop_get_symbol(prop
);
270 value
= getenv(env_sym
->name
);
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");
281 if (make_parent_dir(name
))
283 sprintf(buf2
, "%s%s", dir
, name
);
285 // rename("..config.tmp", name);
291 /*********************************************************************
293 *********************************************************************/
295 void STATE_INIT (int 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
);
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");
319 curr_state
= (int)QStackIF_PopU32(&gstStateQStack
);
320 STATE_SET(curr_state
);
326 /*********************************************************************
331 *********************************************************************/
332 void UtilIF_Testing (void)
339 void UtilIF_Testing (void){}
342 /*******************************
343 *******************************
345 *******************************
346 *******************************/