/*------------------------------------------------------------------------- * C-Pluff, a plug-in framework for C * Copyright 2007 Johannes Lehtinen * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *-----------------------------------------------------------------------*/ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #ifdef HAVE_GETTEXT #include #include #endif #include /* ----------------------------------------------------------------------- * Defines * ---------------------------------------------------------------------*/ // Gettext defines #ifdef HAVE_GETTEXT #define _(String) gettext(String) #define gettext_noop(String) String #define N_(String) gettext_noop(String) #else #define _(String) (String) #define N_(String) String #define textdomain(Domain) #define bindtextdomain(Package, Directory) #endif // GNU C attribute defines #ifndef CP_GCC_NORETURN #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5) #define CP_GCC_NORETURN __attribute__((noreturn)) #else #define CP_GCC_NORETURN #endif #endif // Initializer for empty list #define STR_LIST_INITIALIZER { NULL, NULL } /* ----------------------------------------------------------------------- * Data types * ---------------------------------------------------------------------*/ /// A type for str_list_t structure typedef struct str_list_t str_list_t; /// A type for str_list_entry_t structure typedef struct str_list_entry_t str_list_entry_t; /// A string list container struct str_list_t { /// The first entry or NULL if empty str_list_entry_t *first; /// The last entry or NULL if empty str_list_entry_t *last; }; /// A holder for a string list entry struct str_list_entry_t { /// The string const char *str; /// Next entry str_list_entry_t *next; }; /* ----------------------------------------------------------------------- * Variables * ---------------------------------------------------------------------*/ /// The level of verbosity static int verbosity = 1; /* ----------------------------------------------------------------------- * Functions * ---------------------------------------------------------------------*/ /** * Prints an error message and exits. In quiet mode the error message is * not printed. * * @param msg the error message */ CP_GCC_NORETURN static void error(const char *msg) { if (verbosity >= 1) { /* TRANSLATORS: A formatting string for loader error messages. */ fprintf(stderr, _("C-Pluff Loader: ERROR: %s\n"), msg); } exit(1); } /** * Formats and prints an error message and exits. In quiet mode the error * message is not printed. * * @param msg the error message */ CP_GCC_NORETURN static void errorf(const char *msg, ...) { char buffer[256]; va_list va; va_start(va, msg); vsnprintf(buffer, sizeof(buffer), _(msg), va); va_end(va); strcpy(buffer + sizeof(buffer)/sizeof(char) - 4, "..."); error(buffer); } /** * Allocates memory using malloc and checks for failures. * * @param size the amount of memory to allocate * @return the allocated memory (always non-NULL) */ static void *chk_malloc(size_t size) { void *ptr = malloc(size); if (ptr == NULL) { error(_("Memory allocation failed.")); } else { return ptr; } } /** * Appends a new string to a string list. Copies strings by pointers. */ static void str_list_append(str_list_t *list, const char *str) { str_list_entry_t *entry = chk_malloc(sizeof(str_list_entry_t)); entry->str = str; entry->next = NULL; if (list->last != NULL) { list->last->next = entry; } if (list->first == NULL) { list->first = entry; } list->last = entry; } /** * Removes all entries from a string list. Does not free contained strings. */ static void str_list_clear(str_list_t *list) { str_list_entry_t *entry = list->first; while (entry != NULL) { str_list_entry_t *n = entry->next; free(entry); entry = n; } list->first = NULL; list->last = NULL; } /** * Prints the help text. */ static void print_help(void) { printf(_("C-Pluff Loader, version %s\n"), PACKAGE_VERSION); putchar('\n'); fputs(_("usage: cpluff-loader