Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / include / grub / misc.h
blob33e6b73ccf2c40c58c2ca4b2767eb7d5f4d8317f
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.
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.
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.
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/>.
20 #ifndef GRUB_MISC_HEADER
21 #define GRUB_MISC_HEADER 1
23 #include <stdarg.h>
24 #include <grub/types.h>
25 #include <grub/symbol.h>
26 #include <grub/err.h>
27 #include <grub/i18n.h>
29 /* GCC version checking borrowed from glibc. */
30 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
31 # define GNUC_PREREQ(maj,min) \
32 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
33 #else
34 # define GNUC_PREREQ(maj,min) 0
35 #endif
37 /* Does this compiler support compile-time error attributes? */
38 #if GNUC_PREREQ(4,3)
39 # define ATTRIBUTE_ERROR(msg) \
40 __attribute__ ((__error__ (msg)))
41 #else
42 # define ATTRIBUTE_ERROR(msg) __attribute__ ((noreturn))
43 #endif
45 #define ALIGN_UP(addr, align) \
46 ((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1))
47 #define ALIGN_UP_OVERHEAD(addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1))
48 #define ALIGN_DOWN(addr, align) \
49 ((addr) & ~((typeof (addr)) align - 1))
50 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
51 #define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
53 #define grub_dprintf(condition, fmt, args...) grub_real_dprintf(GRUB_FILE, __LINE__, condition, fmt, ## args)
55 void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n);
56 char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src);
57 char *EXPORT_FUNC(grub_strncpy) (char *dest, const char *src, int c);
58 static inline char *
59 grub_stpcpy (char *dest, const char *src)
61 char *d = dest;
62 const char *s = src;
65 *d++ = *s;
66 while (*s++ != '\0');
68 return d - 1;
71 /* XXX: If grub_memmove is too slow, we must implement grub_memcpy. */
72 static inline void *
73 grub_memcpy (void *dest, const void *src, grub_size_t n)
75 return grub_memmove (dest, src, n);
78 static inline char *
79 grub_strcat (char *dest, const char *src)
81 char *p = dest;
83 while (*p)
84 p++;
86 while ((*p = *src) != '\0')
88 p++;
89 src++;
92 return dest;
95 static inline char *
96 grub_strncat (char *dest, const char *src, int c)
98 char *p = dest;
100 while (*p)
101 p++;
103 while (c-- && (*p = *src) != '\0')
105 p++;
106 src++;
109 *p = '\0';
111 return dest;
114 /* Prototypes for aliases. */
115 #ifndef GRUB_UTIL
116 #ifdef __APPLE__
117 int __attribute__ ((regparm(0))) EXPORT_FUNC(memcmp) (const void *s1, const void *s2, grub_size_t n);
118 void *__attribute__ ((regparm(0))) EXPORT_FUNC(memmove) (void *dest, const void *src, grub_size_t n);
119 void *__attribute__ ((regparm(0))) EXPORT_FUNC(memcpy) (void *dest, const void *src, grub_size_t n);
120 void *__attribute__ ((regparm(0))) EXPORT_FUNC(memset) (void *s, int c, grub_size_t n);
121 #else
122 int EXPORT_FUNC(memcmp) (const void *s1, const void *s2, grub_size_t n);
123 void *EXPORT_FUNC(memmove) (void *dest, const void *src, grub_size_t n);
124 void *EXPORT_FUNC(memcpy) (void *dest, const void *src, grub_size_t n);
125 void *EXPORT_FUNC(memset) (void *s, int c, grub_size_t n);
126 #endif
127 #endif
129 int EXPORT_FUNC(grub_memcmp) (const void *s1, const void *s2, grub_size_t n);
130 int EXPORT_FUNC(grub_strcmp) (const char *s1, const char *s2);
131 int EXPORT_FUNC(grub_strncmp) (const char *s1, const char *s2, grub_size_t n);
133 char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
134 char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
135 int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
137 /* Copied from gnulib.
138 Written by Bruno Haible <bruno@clisp.org>, 2005. */
139 static inline char *
140 grub_strstr (const char *haystack, const char *needle)
142 /* Be careful not to look at the entire extent of haystack or needle
143 until needed. This is useful because of these two cases:
144 - haystack may be very long, and a match of needle found early,
145 - needle may be very long, and not even a short initial segment of
146 needle may be found in haystack. */
147 if (*needle != '\0')
149 /* Speed up the following searches of needle by caching its first
150 character. */
151 char b = *needle++;
153 for (;; haystack++)
155 if (*haystack == '\0')
156 /* No match. */
157 return 0;
158 if (*haystack == b)
159 /* The first character matches. */
161 const char *rhaystack = haystack + 1;
162 const char *rneedle = needle;
164 for (;; rhaystack++, rneedle++)
166 if (*rneedle == '\0')
167 /* Found a match. */
168 return (char *) haystack;
169 if (*rhaystack == '\0')
170 /* No match. */
171 return 0;
172 if (*rhaystack != *rneedle)
173 /* Nothing in this round. */
174 break;
179 else
180 return (char *) haystack;
183 int EXPORT_FUNC(grub_isspace) (int c);
184 int EXPORT_FUNC(grub_isprint) (int c);
186 static inline int
187 grub_iscntrl (int c)
189 return (c >= 0x00 && c <= 0x1F) || c == 0x7F;
192 static inline int
193 grub_isalpha (int c)
195 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
198 static inline int
199 grub_islower (int c)
201 return (c >= 'a' && c <= 'z');
204 static inline int
205 grub_isupper (int c)
207 return (c >= 'A' && c <= 'Z');
210 static inline int
211 grub_isgraph (int c)
213 return (c >= '!' && c <= '~');
216 static inline int
217 grub_isdigit (int c)
219 return (c >= '0' && c <= '9');
222 static inline int
223 grub_isxdigit (int c)
225 return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
228 static inline int
229 grub_isalnum (int c)
231 return grub_isalpha (c) || grub_isdigit (c);
234 static inline int
235 grub_tolower (int c)
237 if (c >= 'A' && c <= 'Z')
238 return c - 'A' + 'a';
240 return c;
243 static inline int
244 grub_toupper (int c)
246 if (c >= 'a' && c <= 'z')
247 return c - 'a' + 'A';
249 return c;
252 static inline int
253 grub_strcasecmp (const char *s1, const char *s2)
255 while (*s1 && *s2)
257 if (grub_tolower ((grub_uint8_t) *s1)
258 != grub_tolower ((grub_uint8_t) *s2))
259 break;
261 s1++;
262 s2++;
265 return (int) grub_tolower ((grub_uint8_t) *s1)
266 - (int) grub_tolower ((grub_uint8_t) *s2);
269 static inline int
270 grub_strncasecmp (const char *s1, const char *s2, grub_size_t n)
272 if (n == 0)
273 return 0;
275 while (*s1 && *s2 && --n)
277 if (grub_tolower (*s1) != grub_tolower (*s2))
278 break;
280 s1++;
281 s2++;
284 return (int) grub_tolower ((grub_uint8_t) *s1)
285 - (int) grub_tolower ((grub_uint8_t) *s2);
288 unsigned long EXPORT_FUNC(grub_strtoul) (const char *str, char **end, int base);
289 unsigned long long EXPORT_FUNC(grub_strtoull) (const char *str, char **end, int base);
291 static inline long
292 grub_strtol (const char *str, char **end, int base)
294 int negative = 0;
295 unsigned long magnitude;
297 while (*str && grub_isspace (*str))
298 str++;
300 if (*str == '-')
302 negative = 1;
303 str++;
306 magnitude = grub_strtoull (str, end, base);
307 if (negative)
309 if (magnitude > (unsigned long) GRUB_LONG_MAX + 1)
311 grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
312 return GRUB_LONG_MIN;
314 return -((long) magnitude);
316 else
318 if (magnitude > GRUB_LONG_MAX)
320 grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
321 return GRUB_LONG_MAX;
323 return (long) magnitude;
327 char *EXPORT_FUNC(grub_strdup) (const char *s) __attribute__ ((warn_unused_result));
328 char *EXPORT_FUNC(grub_strndup) (const char *s, grub_size_t n) __attribute__ ((warn_unused_result));
329 void *EXPORT_FUNC(grub_memset) (void *s, int c, grub_size_t n);
330 grub_size_t EXPORT_FUNC(grub_strlen) (const char *s) __attribute__ ((warn_unused_result));
331 int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
332 int EXPORT_FUNC(grub_printf_) (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
334 /* Replace all `ch' characters of `input' with `with' and copy the
335 result into `output'; return EOS address of `output'. */
336 static inline char *
337 grub_strchrsub (char *output, const char *input, char ch, const char *with)
339 while (*input)
341 if (*input == ch)
343 grub_strcpy (output, with);
344 output += grub_strlen (with);
345 input++;
346 continue;
348 *output++ = *input++;
350 *output = '\0';
351 return output;
354 extern void (*EXPORT_VAR (grub_xputs)) (const char *str);
356 static inline int
357 grub_puts (const char *s)
359 const char nl[2] = "\n";
360 grub_xputs (s);
361 grub_xputs (nl);
363 return 1; /* Cannot fail. */
366 int EXPORT_FUNC(grub_puts_) (const char *s);
367 void EXPORT_FUNC(grub_real_dprintf) (const char *file,
368 const int line,
369 const char *condition,
370 const char *fmt, ...) __attribute__ ((format (printf, 4, 5)));
371 int EXPORT_FUNC(grub_vprintf) (const char *fmt, va_list args);
372 int EXPORT_FUNC(grub_snprintf) (char *str, grub_size_t n, const char *fmt, ...)
373 __attribute__ ((format (printf, 3, 4)));
374 int EXPORT_FUNC(grub_vsnprintf) (char *str, grub_size_t n, const char *fmt,
375 va_list args);
376 char *EXPORT_FUNC(grub_xasprintf) (const char *fmt, ...)
377 __attribute__ ((format (printf, 1, 2))) __attribute__ ((warn_unused_result));
378 char *EXPORT_FUNC(grub_xvasprintf) (const char *fmt, va_list args) __attribute__ ((warn_unused_result));
379 void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn));
380 void EXPORT_FUNC(grub_abort) (void) __attribute__ ((noreturn));
381 grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
382 grub_uint64_t d,
383 grub_uint64_t *r);
385 #if !defined(GRUB_UTIL) && NEED_ENABLE_EXECUTE_STACK
386 void EXPORT_FUNC(__enable_execute_stack) (void *addr);
387 #endif
389 #if !defined(GRUB_UTIL) && NEED_REGISTER_FRAME_INFO
390 void EXPORT_FUNC (__register_frame_info) (void);
391 void EXPORT_FUNC (__deregister_frame_info) (void);
392 #endif
394 /* Inline functions. */
396 static inline char *
397 grub_memchr (const void *p, int c, grub_size_t len)
399 const char *s = p;
400 const char *e = s + len;
402 for (; s < e; s++)
403 if (*s == c)
404 return (char *) s;
406 return 0;
410 static inline unsigned int
411 grub_abs (int x)
413 if (x < 0)
414 return (unsigned int) (-x);
415 else
416 return (unsigned int) x;
419 /* Rounded-up division */
420 static inline unsigned int
421 grub_div_roundup (unsigned int x, unsigned int y)
423 return (x + y - 1) / y;
426 /* Reboot the machine. */
427 #if defined (GRUB_MACHINE_EMU) || defined (GRUB_MACHINE_QEMU_MIPS)
428 void EXPORT_FUNC(grub_reboot) (void) __attribute__ ((noreturn));
429 #else
430 void grub_reboot (void) __attribute__ ((noreturn));
431 #endif
433 #ifdef GRUB_MACHINE_PCBIOS
434 /* Halt the system, using APM if possible. If NO_APM is true, don't
435 * use APM even if it is available. */
436 void grub_halt (int no_apm) __attribute__ ((noreturn));
437 #elif defined (__mips__)
438 void EXPORT_FUNC (grub_halt) (void) __attribute__ ((noreturn));
439 #else
440 void grub_halt (void) __attribute__ ((noreturn));
441 #endif
443 #ifdef GRUB_MACHINE_EMU
444 /* Flag to control module autoloading in normal mode. */
445 extern int EXPORT_VAR(grub_no_autoload);
446 #else
447 #define grub_no_autoload 0
448 #endif
450 static inline void
451 grub_error_save (struct grub_error_saved *save)
453 grub_memcpy (save->errmsg, grub_errmsg, sizeof (save->errmsg));
454 save->grub_errno = grub_errno;
455 grub_errno = GRUB_ERR_NONE;
458 static inline void
459 grub_error_load (const struct grub_error_saved *save)
461 grub_memcpy (grub_errmsg, save->errmsg, sizeof (grub_errmsg));
462 grub_errno = save->grub_errno;
465 #endif /* ! GRUB_MISC_HEADER */