diff options
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/getopt.c | 115 | ||||
-rw-r--r-- | src/library/gettext.c | 111 |
2 files changed, 226 insertions, 0 deletions
diff --git a/src/library/getopt.c b/src/library/getopt.c new file mode 100644 index 0000000..fb8410e --- /dev/null +++ b/src/library/getopt.c @@ -0,0 +1,115 @@ +/* + * Small reimplementation of getopt(). + * + * Copyright 2012 Andrew Wood, distributed under the Artistic License 2.0. + */ + +#include "config.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#ifndef HAVE_GETOPT + +char *minioptarg = NULL; +int minioptind = 0; +int miniopterr = 1; +int minioptopt = 0; + + +/* + * Minimalist getopt() clone, which handles short options only and doesn't + * permute argv[]. + */ +int minigetopt(int argc, char **argv, char *optstring) +{ + static int nextchar = 0; + int optchar; + int i; + + if ((minioptind == 0) && (argc > 0)) + minioptind++; + + if ((nextchar > 0) && (argv[minioptind][nextchar] == 0)) { + minioptind++; + nextchar = 0; + } + + if (minioptind >= argc) + return -1; + + /* + * End of options if arg doesn't start with "-" + */ + if (argv[minioptind][0] != '-') + return -1; + + /* + * End of options if arg is just "-" + */ + if (argv[minioptind][1] == 0) + return -1; + + /* + * End of options if arg is "--", but don't include the "--" in the + * non-option arguments + */ + if ((argv[minioptind][1] == '-') && (argv[minioptind][2] == 0)) { + minioptind++; + return -1; + } + + if (nextchar == 0) + nextchar = 1; + + optchar = argv[minioptind][nextchar++]; + + for (i = 0; optstring[i] != 0 && optstring[i] != optchar; i++) { + } + + if (optstring[i] == 0) { + minioptopt = optchar; + if (miniopterr) + fprintf(stderr, "%s: invalid option -- %c\n", + argv[0], optchar); + return '?'; + } + + if (optstring[i + 1] != ':') { + minioptarg = NULL; + return optchar; + } + + /* + * At this point we've got an option that takes an argument. + */ + + /* + * Next character isn't 0, so the argument is within this array + * element (i.e. "-dFOO"). + */ + if (argv[minioptind][nextchar] != 0) { + minioptarg = &(argv[minioptind][nextchar]); + nextchar = 0; + minioptind++; + return optchar; + } + + /* + * Argument is in the next array element (i.e. "-d FOO"). + */ + nextchar = 0; + minioptind++; + if (minioptind >= argc) { + fprintf(stderr, "%s: option `-%c' requires an argument\n", + argv[0], optchar); + return ':'; + } + minioptarg = argv[minioptind++]; + + return optchar; +} + +#endif /* HAVE_GETOPT */ + +/* EOF */ diff --git a/src/library/gettext.c b/src/library/gettext.c new file mode 100644 index 0000000..b964fde --- /dev/null +++ b/src/library/gettext.c @@ -0,0 +1,111 @@ +/* + * Very minimal (and stupid) implementation of gettext, with a fixed lookup + * table. + * + * This library ONLY handles gettext(), and that only for the basic form (it + * translates strings to other strings with no other modification, so %2$d + * style constructs are not dealt with). The setlocale(), bindtextdomain(), + * and textdomain() functions are ignored. + * + * To use this library, create a function that, given a language string, + * returns a struct msg_table_s[] of msgid and msgstr pairs, with the end + * of the table being marked by a NULL msgid. The po2table.sh script will do + * this. + * + * Copyright 2012 Andrew Wood, distributed under the Artistic License 2.0. + */ + +#include "config.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#ifndef HAVE_GETTEXT + +struct msgtable_s { + char *msgid; + char *msgstr; +}; + +#if ENABLE_NLS +struct msgtable_s *minigettext__gettable(char *); +#else /* ENABLE_NLS */ +struct msgtable_s *minigettext__gettable(char *a) +{ + return NULL; +} +#endif /* ENABLE_NLS */ + +char *minisetlocale(char *a, char *b) +{ + return NULL; +} + + +char *minibindtextdomain(char *a, char *b) +{ + return NULL; +} + + +char *minitextdomain(char *a) +{ + return NULL; +} + + +char *minigettext(char *msgid) +{ + static struct msgtable_s *table = NULL; + static int tried_lang = 0; + char *lang; + int i; + + if (msgid == NULL) + return msgid; + + if (tried_lang == 0) { + lang = getenv("LANGUAGE"); /* RATS: ignore */ + if (lang) + table = minigettext__gettable(lang); + + if (table == NULL) { + lang = getenv("LANG"); /* RATS: ignore */ + if (lang) + table = minigettext__gettable(lang); + } + + if (table == NULL) { + lang = getenv("LC_ALL"); /* RATS: ignore */ + if (lang) + table = minigettext__gettable(lang); + } + + if (table == NULL) { + lang = getenv("LC_MESSAGES"); /* RATS: ignore */ + if (lang) + table = minigettext__gettable(lang); + } + + tried_lang = 1; + } + + if (table == NULL) + return msgid; + + for (i = 0; table[i].msgid; i++) { + if (strcmp(table[i].msgid, msgid) == 0) { + if (table[i].msgstr == 0) + return msgid; + if (table[i].msgstr[0] == 0) + return msgid; + return table[i].msgstr; + } + } + + return msgid; +} + +#endif /* HAVE_GETTEXT */ + +/* EOF */ |