]> git.proxmox.com Git - grub2.git/blob - include/grub/misc.h
Merge mainline into multiterm
[grub2.git] / include / grub / misc.h
1 /* misc.h - prototypes for misc functions */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
5 *
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef GRUB_MISC_HEADER
21 #define GRUB_MISC_HEADER 1
22
23 #include <stdarg.h>
24 #include <grub/types.h>
25 #include <grub/symbol.h>
26 #include <grub/err.h>
27
28 /* GCC version checking borrowed from glibc. */
29 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
30 # define GNUC_PREREQ(maj,min) \
31 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
32 #else
33 # define GNUC_PREREQ(maj,min) 0
34 #endif
35
36 /* Does this compiler support compile-time error attributes? */
37 #if GNUC_PREREQ(4,3)
38 # define ATTRIBUTE_ERROR(msg) \
39 __attribute__ ((__error__ (msg)))
40 #else
41 # define ATTRIBUTE_ERROR(msg)
42 #endif
43
44 #define ALIGN_UP(addr, align) \
45 ((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1))
46 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
47 #define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
48
49 #define grub_dprintf(condition, fmt, args...) grub_real_dprintf(__FILE__, __LINE__, condition, fmt, ## args)
50 /* XXX: If grub_memmove is too slow, we must implement grub_memcpy. */
51 #define grub_memcpy(d,s,n) grub_memmove ((d), (s), (n))
52
53 void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n);
54 char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src);
55 char *EXPORT_FUNC(grub_strncpy) (char *dest, const char *src, int c);
56 char *EXPORT_FUNC(grub_stpcpy) (char *dest, const char *src);
57
58 static inline char *
59 grub_strcat (char *dest, const char *src)
60 {
61 char *p = dest;
62
63 while (*p)
64 p++;
65
66 while ((*p = *src) != '\0')
67 {
68 p++;
69 src++;
70 }
71
72 return dest;
73 }
74
75 static inline char *
76 grub_strncat (char *dest, const char *src, int c)
77 {
78 char *p = dest;
79
80 while (*p)
81 p++;
82
83 while ((*p = *src) != '\0' && c--)
84 {
85 p++;
86 src++;
87 }
88
89 *p = '\0';
90
91 return dest;
92 }
93
94 /* Prototypes for aliases. */
95 #ifndef GRUB_UTIL
96 int EXPORT_FUNC(memcmp) (const void *s1, const void *s2, grub_size_t n);
97 void *EXPORT_FUNC(memmove) (void *dest, const void *src, grub_size_t n);
98 void *EXPORT_FUNC(memcpy) (void *dest, const void *src, grub_size_t n);
99 void *EXPORT_FUNC(memset) (void *s, int c, grub_size_t n);
100 #endif
101
102 int EXPORT_FUNC(grub_memcmp) (const void *s1, const void *s2, grub_size_t n);
103 int EXPORT_FUNC(grub_strcmp) (const char *s1, const char *s2);
104 int EXPORT_FUNC(grub_strncmp) (const char *s1, const char *s2, grub_size_t n);
105
106 char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
107 char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
108 int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
109 char *EXPORT_FUNC(grub_strstr) (const char *haystack, const char *needle);
110 int EXPORT_FUNC(grub_isspace) (int c);
111 int EXPORT_FUNC(grub_isprint) (int c);
112
113 static inline int
114 grub_isalpha (int c)
115 {
116 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
117 }
118
119 static inline int
120 grub_isgraph (int c)
121 {
122 return (c >= '!' && c <= '~');
123 }
124
125 static inline int
126 grub_isdigit (int c)
127 {
128 return (c >= '0' && c <= '9');
129 }
130
131 static inline int
132 grub_tolower (int c)
133 {
134 if (c >= 'A' && c <= 'Z')
135 return c - 'A' + 'a';
136
137 return c;
138 }
139
140 static inline int
141 grub_toupper (int c)
142 {
143 if (c >= 'a' && c <= 'z')
144 return c - 'a' + 'A';
145
146 return c;
147 }
148
149 static inline int
150 grub_strcasecmp (const char *s1, const char *s2)
151 {
152 while (*s1 && *s2)
153 {
154 if (grub_tolower (*s1) != grub_tolower (*s2))
155 break;
156
157 s1++;
158 s2++;
159 }
160
161 return (int) grub_tolower (*s1) - (int) grub_tolower (*s2);
162 }
163
164 static inline int
165 grub_strncasecmp (const char *s1, const char *s2, grub_size_t n)
166 {
167 if (n == 0)
168 return 0;
169
170 while (*s1 && *s2 && --n)
171 {
172 if (grub_tolower (*s1) != grub_tolower (*s2))
173 break;
174
175 s1++;
176 s2++;
177 }
178
179 return (int) grub_tolower (*s1) - (int) grub_tolower (*s2);
180 }
181
182
183 unsigned long EXPORT_FUNC(grub_strtoul) (const char *str, char **end, int base);
184 unsigned long long EXPORT_FUNC(grub_strtoull) (const char *str, char **end, int base);
185 char *EXPORT_FUNC(grub_strdup) (const char *s);
186 char *EXPORT_FUNC(grub_strndup) (const char *s, grub_size_t n);
187 void *EXPORT_FUNC(grub_memset) (void *s, int c, grub_size_t n);
188 grub_size_t EXPORT_FUNC(grub_strlen) (const char *s);
189 int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
190 int EXPORT_FUNC(grub_printf_) (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
191 int EXPORT_FUNC(grub_puts) (const char *s);
192 int EXPORT_FUNC(grub_puts_) (const char *s);
193 void EXPORT_FUNC(grub_real_dprintf) (const char *file,
194 const int line,
195 const char *condition,
196 const char *fmt, ...) __attribute__ ((format (printf, 4, 5)));
197 int EXPORT_FUNC(grub_vprintf) (const char *fmt, va_list args);
198 int EXPORT_FUNC(grub_sprintf) (char *str, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
199 int EXPORT_FUNC(grub_vsprintf) (char *str, const char *fmt, va_list args);
200 void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn));
201 void EXPORT_FUNC(grub_abort) (void) __attribute__ ((noreturn));
202 grub_size_t EXPORT_FUNC(grub_utf8_to_ucs4) (grub_uint32_t *dest,
203 grub_size_t destsize,
204 const grub_uint8_t *src,
205 grub_size_t srcsize,
206 const grub_uint8_t **srcend);
207 grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
208 grub_uint32_t d, grub_uint32_t *r);
209
210 #ifdef NEED_ENABLE_EXECUTE_STACK
211 void EXPORT_FUNC(__enable_execute_stack) (void *addr);
212 #endif
213
214 /* Inline functions. */
215
216 static inline unsigned int
217 grub_abs (int x)
218 {
219 if (x < 0)
220 return (unsigned int) (-x);
221 else
222 return (unsigned int) x;
223 }
224
225 static inline long
226 grub_max (long x, long y)
227 {
228 if (x > y)
229 return x;
230 else
231 return y;
232 }
233
234 /* Rounded-up division */
235 static inline unsigned int
236 grub_div_roundup (unsigned int x, unsigned int y)
237 {
238 return (x + y - 1) / y;
239 }
240
241 /* Reboot the machine. */
242 void EXPORT_FUNC (grub_reboot) (void);
243
244 #ifdef GRUB_MACHINE_PCBIOS
245 /* Halt the system, using APM if possible. If NO_APM is true, don't
246 * use APM even if it is available. */
247 void EXPORT_FUNC (grub_halt) (int no_apm);
248 #else
249 void EXPORT_FUNC (grub_halt) (void);
250 #endif
251
252 #endif /* ! GRUB_MISC_HEADER */