Imported Upstream version 4.4
[platform/upstream/make.git] / src / loadapi.c
1 /* API for GNU Make dynamic objects.
2 Copyright (C) 2013-2022 Free Software Foundation, Inc.
3 This file is part of GNU Make.
4
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
8 version.
9
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program.  If not, see <https://www.gnu.org/licenses/>.  */
16
17 #include "makeint.h"
18
19 #include "filedef.h"
20 #include "variable.h"
21 #include "dep.h"
22
23 /* Allocate a buffer in our context, so we can free it.  */
24 char *
25 gmk_alloc (unsigned int len)
26 {
27   return xmalloc (len);
28 }
29
30 /* Free a buffer returned by gmk_expand().  */
31 void
32 gmk_free (char *s)
33 {
34   free (s);
35 }
36
37 /* Evaluate a buffer as make syntax.
38    Ideally eval_buffer() will take const char *, but not yet.  */
39 void
40 gmk_eval (const char *buffer, const gmk_floc *gfloc)
41 {
42   /* Preserve existing variable buffer context.  */
43   char *pbuf;
44   size_t plen;
45   char *s;
46   floc fl;
47   floc *flp;
48
49   if (gfloc)
50     {
51       fl.filenm = gfloc->filenm;
52       fl.lineno = gfloc->lineno;
53       fl.offset = 0;
54       flp = &fl;
55     }
56   else
57     flp = NULL;
58
59   install_variable_buffer (&pbuf, &plen);
60
61   s = xstrdup (buffer);
62   eval_buffer (s, flp);
63   free (s);
64
65   restore_variable_buffer (pbuf, plen);
66 }
67
68 /* Expand a string and return an allocated buffer.
69    Caller must call gmk_free() with this buffer.  */
70 char *
71 gmk_expand (const char *ref)
72 {
73   return allocated_variable_expand (ref);
74 }
75
76 /* Register a function to be called from makefiles.  */
77 void
78 gmk_add_function (const char *name, gmk_func_ptr func,
79                   unsigned int min, unsigned int max, unsigned int flags)
80 {
81   define_new_function (reading_file, name, min, max, flags, func);
82 }