summaryrefslogtreecommitdiff
path: root/module
diff options
context:
space:
mode:
authorJohn Mark Bell <jmb@netsurf-browser.org>2008-11-11 16:40:48 +0000
committerJohn Mark Bell <jmb@netsurf-browser.org>2008-11-11 16:40:48 +0000
commit27d368b005bdf092442321735c0d426f518d9199 (patch)
tree67067758c115c8836dc23be85fb5ddbf1e2d7045 /module
parent0d244e11569291f8eec9c4268510f72ae3cbfc2a (diff)
downloadiconv-27d368b005bdf092442321735c0d426f518d9199.tar.gz
iconv-27d368b005bdf092442321735c0d426f518d9199.tar.bz2
Some kind of half-arsed attempt at parsing command line parameters for *Iconv.
Actually invoke this command through the wrapper code. svn path=/trunk/iconv/; revision=5684
Diffstat (limited to 'module')
-rw-r--r--module/module.c141
-rw-r--r--module/wrapper.c44
2 files changed, 181 insertions, 4 deletions
diff --git a/module/module.c b/module/module.c
index de04796..35c5ab1 100644
--- a/module/module.c
+++ b/module/module.c
@@ -2,6 +2,7 @@
#include <errno.h>
#include <stdbool.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -20,6 +21,7 @@
static _kernel_oserror ErrorGeneric = { 0x0, "" };
static size_t iconv_convert(_kernel_swi_regs *r);
+static _kernel_oserror *do_iconv(int argc, const char *args);
static int errno_to_iconv_error(int num);
/* Module initialisation */
@@ -163,9 +165,13 @@ _kernel_oserror *command_handler(const char *arg_string, int argc,
UNUSED(pw);
switch (cmd_no) {
+ case CMD_Iconv:
+ return do_iconv(argc, arg_string);
+ break;
case CMD_ReadAliases:
+ /** \todo this is rather nasty, and hard-coded for RISC OS */
free_alias_data();
- if (!create_alias_data(ALIASES_FILE)) {
+ if (!create_alias_data("Unicode:Files.Aliases")) {
strcpy(ErrorGeneric.errmess,
"Failed reading Aliases file.");
return &ErrorGeneric;
@@ -200,6 +206,139 @@ size_t iconv_convert(_kernel_swi_regs *regs)
return ret;
}
+_kernel_oserror *do_iconv(int argc, const char *args)
+{
+ char from[64] = "", to[64] = "";
+ char *f, *t;
+ bool list = false;
+ char out[4096] = "";
+ char *o;
+ const char *p = args;
+ FILE *ofp;
+ iconv_t cd;
+
+ /* Parse options */
+ while (argc > 0 && *p == '-') {
+ if (*(p+1) < ' ')
+ break;
+
+ switch (*(p+1)) {
+ case 'f':
+ f = from;
+ p += 2;
+ if (*p == ' ') {
+ argc--;
+ while (*p == ' ')
+ p++;
+ }
+ while (*p > ' ') {
+ if (f - from < (int) sizeof(from) - 1)
+ *f++ = *p;
+ p++;
+ }
+ *f = '\0';
+ while (*p == ' ')
+ p++;
+ argc--;
+ break;
+ case 't':
+ t = to;
+ p += 2;
+ if (*p == ' ') {
+ argc--;
+ while (*p == ' ')
+ p++;
+ }
+ while (*p > ' ') {
+ if (t - to < (int) sizeof(to) - 1)
+ *t++ = *p;
+ p++;
+ }
+ *t = '\0';
+ while (*p == ' ')
+ p++;
+ argc--;
+ break;
+ case 'l':
+ list = true;
+ p += 2;
+ argc--;
+ break;
+ case 'c':
+ case 'o':
+ o = out;
+ p += 2;
+ if (*p == ' ') {
+ argc--;
+ while (*p == ' ')
+ p++;
+ }
+ while (*p > ' ') {
+ if (o - out < (int) sizeof(out) - 1)
+ *o++ = *p;
+ p++;
+ }
+ *o = '\0';
+ while (*p == ' ')
+ p++;
+ argc--;
+ break;
+ case 's':
+ case 'v':
+ default:
+ snprintf(ErrorGeneric.errmess,
+ sizeof(ErrorGeneric.errmess),
+ "Iconv: invalid option -- %c", *(p+1));
+ return &ErrorGeneric;
+ }
+ }
+
+ if (list) {
+ /** \todo dump aliases */
+ return NULL;
+ }
+
+ /** \todo better defaults */
+ if (from[0] == '\0')
+ strcpy(from, "ISO-8859-1");
+
+ if (to[0] == '\0')
+ strcpy(to, "ISO-8859-1");
+
+ cd = iconv_open(to, from);
+ if (cd == (iconv_t) -1) {
+ snprintf(ErrorGeneric.errmess, sizeof(ErrorGeneric.errmess),
+ "Cannot convert from %s to %s", from, to);
+ return &ErrorGeneric;
+ }
+
+ if (out[0] == '\0')
+ ofp = stdout;
+ else
+ ofp = fopen(out, "w");
+ if (ofp == NULL) {
+ iconv_close(cd);
+ strcpy(ErrorGeneric.errmess, "Unable to open output file.");
+ return &ErrorGeneric;
+ }
+
+ while (argc-- > 0) {
+ /* Remaining parameters are input files */
+
+ /** \todo convert input */
+
+ /* Reset cd */
+ iconv(cd, NULL, NULL, NULL, NULL);
+ }
+
+ if (ofp != stdout)
+ fclose(ofp);
+
+ iconv_close(cd);
+
+ return NULL;
+}
+
int errno_to_iconv_error(int num)
{
switch (num) {
diff --git a/module/wrapper.c b/module/wrapper.c
index 506a5ae..6b15a25 100644
--- a/module/wrapper.c
+++ b/module/wrapper.c
@@ -1,6 +1,8 @@
/* Wrapper around module API so we can run/test it on non-RO machines */
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include "header.h"
@@ -9,24 +11,60 @@
int main(int argc, char **argv)
{
_kernel_oserror *error;
+ char *buf = NULL;
+ int buf_alloc = 0, buf_len = 0;
- if (argc != 1) {
- printf("Usage: %s\n", argv[0]);
- return 1;
+ for (int i = 1; i < argc; i++) {
+ /* Need a leading space for all but the first argument */
+ int req = buf_len + (buf_len > 0 ? 1 : 0) + strlen(argv[i]) + 1;
+
+ while (req > buf_alloc) {
+ char *temp = realloc(buf, buf_alloc + 4096);
+ if (temp == NULL) {
+ printf("Insufficient memory for parameters");
+ return 1;
+ }
+
+ buf = temp;
+ buf_alloc += 4096;
+ }
+
+ if (buf_len > 0) {
+ memcpy(buf + buf_len, " ", 1);
+ buf_len++;
+ }
+
+ memcpy(buf + buf_len, argv[i], strlen(argv[i]));
+
+ buf_len += strlen(argv[i]);
+
+ buf[buf_len] = '\0';
}
error = mod_init(NULL, 0, NULL);
if (error != NULL) {
+ free(buf);
printf("mod_init: %s (%d)\n", error->errmess, error->errnum);
return 1;
}
+ error = command_handler(buf, argc - 1, CMD_Iconv, NULL);
+ if (error != NULL) {
+ free(buf);
+ printf("command_handler: %s (%d)\n", error->errmess,
+ error->errnum);
+ return 1;
+ }
+
error = mod_fini(0, 0, NULL);
if (error != NULL) {
+ free(buf);
printf("mod_fini: %s (%d)\n", error->errmess, error->errnum);
return 1;
}
+ free(buf);
+
return 0;
}