aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c62
1 files changed, 43 insertions, 19 deletions
diff --git a/src/main.c b/src/main.c
index 467eb72..eb3c0b4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2012-2023 Robin Haberkorn
+ * Copyright (C) 2012-2024 Robin Haberkorn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -23,6 +23,7 @@
#include <string.h>
#include <stdlib.h>
#include <signal.h>
+#include <locale.h>
#include <glib.h>
#include <glib/gprintf.h>
@@ -104,9 +105,10 @@ teco_get_default_config_path(const gchar *program)
static gchar *teco_eval_macro = NULL;
static gboolean teco_mung_file = FALSE;
static gboolean teco_mung_profile = TRUE;
+static gboolean teco_8bit_clean = FALSE;
static gchar *
-teco_process_options(gint *argc, gchar ***argv)
+teco_process_options(gchar ***argv)
{
static const GOptionEntry option_entries[] = {
{"eval", 'e', 0, G_OPTION_ARG_STRING, &teco_eval_macro,
@@ -119,6 +121,8 @@ teco_process_options(gint *argc, gchar ***argv)
"Do not mung "
"$SCITECOCONFIG" G_DIR_SEPARATOR_S INI_FILE " "
"even if it exists"},
+ {"8bit", '8', 0, G_OPTION_ARG_NONE, &teco_8bit_clean,
+ "Use ANSI encoding by default and disable automatic EOL conversion"},
{NULL}
};
@@ -133,7 +137,7 @@ teco_process_options(gint *argc, gchar ***argv)
g_option_context_set_description(
options,
"Bug reports should go to <" PACKAGE_BUGREPORT "> or "
- "<" PACKAGE_URL_DEV ">."
+ "<" PACKAGE_URL ">."
);
g_option_context_add_main_entries(options, option_entries, NULL);
@@ -155,7 +159,7 @@ teco_process_options(gint *argc, gchar ***argv)
*/
g_option_context_set_strict_posix(options, TRUE);
- if (!g_option_context_parse(options, argc, argv, &error)) {
+ if (!g_option_context_parse_strv(options, argv, &error)) {
g_fprintf(stderr, "Option parsing failed: %s\n",
error->message);
exit(EXIT_FAILURE);
@@ -170,16 +174,13 @@ teco_process_options(gint *argc, gchar ***argv)
* and "--" is not the first non-option argument as in
* sciteco foo -- -C bar.
*/
- if (*argc >= 2 && !strcmp((*argv)[1], "--")) {
- (*argv)[1] = (*argv)[0];
- (*argv)++;
- (*argc)--;
- }
+ if ((*argv)[0] && !g_strcmp0((*argv)[1], "--"))
+ g_free(teco_strv_remove(*argv, 1));
gchar *mung_filename = NULL;
if (teco_mung_file) {
- if (*argc < 2) {
+ if (!(*argv)[0] || !(*argv)[1]) {
g_fprintf(stderr, "Script to mung expected!\n");
exit(EXIT_FAILURE);
}
@@ -190,11 +191,7 @@ teco_process_options(gint *argc, gchar ***argv)
exit(EXIT_FAILURE);
}
- mung_filename = g_strdup((*argv)[1]);
-
- (*argv)[1] = (*argv)[0];
- (*argv)++;
- (*argc)--;
+ mung_filename = teco_strv_remove(*argv, 1);
}
return mung_filename;
@@ -306,12 +303,39 @@ main(int argc, char **argv)
signal(SIGINT, teco_sigint_handler);
signal(SIGTERM, teco_sigint_handler);
- g_autofree gchar *mung_filename = teco_process_options(&argc, &argv);
+ /*
+ * Important for Unicode handling in curses and glib.
+ * In particular, in order to accept Unicode characters
+ * in option strings.
+ *
+ * NOTE: Windows 10 accepts ".UTF8" here, so the "ANSI"
+ * versions of win32 API functions accept UTF-8.
+ * We want to support older versions, though and
+ * glib happily converts to Windows' native UTF-16.
+ */
+ setlocale(LC_ALL, "");
+
+#ifdef G_OS_WIN32
+ /*
+ * main()'s argv is in the system locale, so we might loose
+ * information when passing it to g_option_context_parse().
+ * The remaining strings are also not guaranteed to be in
+ * UTF-8.
+ */
+ g_auto(GStrv) argv_utf8 = g_win32_get_command_line();
+#else
+ g_auto(GStrv) argv_utf8 = g_strdupv(argv);
+#endif
+ g_autofree gchar *mung_filename = teco_process_options(&argv_utf8);
/*
* All remaining arguments in argv are arguments
* to the macro or munged file.
*/
+ if (teco_8bit_clean)
+ /* equivalent to 16,4ED but executed earlier */
+ teco_ed = (teco_ed & ~TECO_ED_AUTOEOL) | TECO_ED_DEFAULT_ANSI;
+
/*
* Theoretically, QReg tables should only be initialized
* after the interface, since they contain Scintilla documents.
@@ -343,7 +367,7 @@ main(int argc, char **argv)
/* current working directory ("$") */
teco_qreg_table_insert(&teco_qreg_table_globals, teco_qreg_workingdir_new());
/* environment defaults and registers */
- teco_initialize_environment(argv[0]);
+ teco_initialize_environment(argv_utf8[0]);
teco_qreg_table_t local_qregs;
teco_qreg_table_init(&local_qregs, TRUE);
@@ -361,8 +385,8 @@ main(int argc, char **argv)
* Also, the Unnamed Buffer should be kept empty for piping.
* Therefore, it would be best to store the arguments in Q-Regs, e.g. $0,$1,$2...
*/
- for (gint i = 1; i < argc; i++) {
- teco_interface_ssm(SCI_APPENDTEXT, strlen(argv[i]), (sptr_t)argv[i]);
+ for (gint i = 1; argv_utf8[i]; i++) {
+ teco_interface_ssm(SCI_APPENDTEXT, strlen(argv_utf8[i]), (sptr_t)argv_utf8[i]);
teco_interface_ssm(SCI_APPENDTEXT, 1, (sptr_t)"\n");
}