1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/*
* HT Editor
* main.cc
*
* Copyright (C) 1999-2002 Stefan Weyergraf
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "analy_register.h"
#include "asm.h"
#include "htapp.h"
#include "atom.h"
#include "htcfg.h"
#include "htclipboard.h"
#include "display.h"
#include "except.h"
#include "hthist.h"
#include "htiobox.h"
#include "keyb.h"
#include "htidle.h"
#include "htmenu.h"
#include "htpal.h"
#include "htinfo.h"
#include "htreg.h"
#include "sys.h"
#include "snprintf.h"
#include "info/infoview.h"
#include "log.h"
#include "stddata.h"
const char *htcopyrights[]=
{
ht_name" "ht_version" (%s) "__TIME__" on "__DATE__,
ht_copyright1,
ht_copyright2,
NULL
};
static void add_file_history_entry(char *n)
{
List *hist=(List*)getAtomValue(HISTATOM_FILE);
if (hist) insert_history_entry(hist, n, 0);
}
typedef bool (*initfunc)();
typedef void (*donefunc)();
struct initdonefunc {
initfunc i;
donefunc d;
const char *name;
};
#define INITDONE(name) { init_##name, done_##name, #name }
initdonefunc initdone[] = {
INITDONE(system),
INITDONE(atom),
INITDONE(data),
INITDONE(pal),
INITDONE(registry),
INITDONE(keyb),
INITDONE(idle),
INITDONE(menu),
INITDONE(hist),
INITDONE(clipboard),
INITDONE(obj),
INITDONE(analyser),
INITDONE(asm),
INITDONE(stddata),
INITDONE(cfg),
INITDONE(app)
};
int htstate;
static bool init()
{
for (htstate=0; htstate<(int)(sizeof initdone / sizeof initdone[0]); htstate++) {
if (!initdone[htstate].i()) return false;
}
return true;
}
static void done()
{
for (htstate--; htstate>=0; htstate--) {
initdone[htstate].d();
}
}
static void load_file(char *fn, uint mode)
{
char cfn[HT_NAME_MAX];
char cwd[HT_NAME_MAX];
cwd[0] = 0;
getcwd(cwd, sizeof cwd);
if (sys_common_canonicalize(cfn, fn, cwd, sys_is_path_delim)==0) {
add_file_history_entry(cfn);
((ht_app*)app)->create_window_file(cfn, mode, true);
}
}
static void show_help()
{
((ht_app*)app)->create_window_help(MAGIC_HT_HELP, "Top");
}
static void show_version()
{
const char **copyrights = htcopyrights;
while (*copyrights) {
printf(*copyrights, sys_get_name());
puts("");
copyrights++;
}
exit(0);
}
static void params(int argc, char *argv[], bool started)
{
int escaped_params_start = 0;
// FIXME: FOM_AUTO should be the default
int load_mode = FOM_BIN;
bool showhelp = false;
#define PARAM_ERROR(a...) {if (started) LOG_EX(LOG_ERROR, a);}
#define EXPECT_PARAMEND(b) if ((j+1)!=len) { PARAM_ERROR("syntax error in options"); b;}
#define EXPECT_PARAMS(p, b) if (i+p+1 > argc) { PARAM_ERROR("syntax error in options"); b;}
#define NOTHING ((void)(0))
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
int len = strlen(argv[i]);
if (len==1) PARAM_ERROR("unknown option: %s", argv[i]);
if (argv[i][1] == '-') {
if (len == 2) {
// --
escaped_params_start = i+1;
break;
}
if (strcmp(argv[i], "--auto") == 0) {
EXPECT_PARAMS(1, NOTHING) else {
if (started) load_file(argv[i+1], FOM_AUTO);
i++;
}
} else
if (strcmp(argv[i], "--bin") == 0) {
EXPECT_PARAMS(1, NOTHING) else {
if (started) load_file(argv[i+1], FOM_BIN);
i++;
}
} else
if (strcmp(argv[i], "--help") == 0) {
showhelp = true;
} else
if (strcmp(argv[i], "--project") == 0) {
EXPECT_PARAMS(1, NOTHING) else {
if (started) ((ht_app*)app)->project_opencreate(argv[i+1]);
i++;
}
} else
if (strcmp(argv[i], "--text") == 0) {
EXPECT_PARAMS(1, NOTHING) else {
if (started) load_file(argv[i+1], FOM_TEXT);
i++;
}
} else
if (strcmp(argv[i], "--version") == 0) {
show_version();
} else
if (strcmp(argv[i], "--cfg-shared") == 0) {
ht_cfg_use_homedir = false;
} else
{
PARAM_ERROR("unknown option: %s", argv[i]);
}
} else {
for (int j=1; j<len; j++) {
switch (argv[i][j]) {
case 'A':
load_mode = FOM_AUTO;
break;
case 'B':
load_mode = FOM_BIN;
break;
case 'T':
load_mode = FOM_TEXT;
break;
case 'a':
EXPECT_PARAMEND(break);
EXPECT_PARAMS(1, break);
if (started) load_file(argv[i+1], FOM_AUTO);
i++;
break;
case 'b':
EXPECT_PARAMEND(break);
EXPECT_PARAMS(1, break);
if (started) load_file(argv[i+1], FOM_BIN);
i++;
break;
case 'h':
showhelp = true;
break;
case 'p':
EXPECT_PARAMEND(break);
EXPECT_PARAMS(1, break);
if (started) ((ht_app*)app)->project_opencreate(argv[i+1]);
i++;
break;
case 't':
EXPECT_PARAMEND(break);
EXPECT_PARAMS(1, break);
if (started) load_file(argv[i+1], FOM_TEXT);
i++;
break;
case 'v':
show_version();
break;
default:
PARAM_ERROR("unknown option: -%c", argv[i][j]);
break;
}
}
}
} else {
if (started) {
load_file(argv[i], load_mode);
}
}
}
if (escaped_params_start && started) {
char **s = &argv[escaped_params_start];
while (*s) {
load_file(*s, load_mode);
s++;
}
}
if (showhelp && started) show_help();
}
#if defined(WIN32) || defined(__WIN32__)
#define LEAN_AND_MEAN
#include <windows.h>
#endif
int main(int argc, char *argv[])
{
#if defined(WIN32) || defined(__WIN32__)
HMODULE h = GetModuleHandle(NULL);
GetModuleFileName(h, appname, sizeof appname-1);
#else
ht_strlcpy(appname, argv[0], sizeof appname);
#endif
params(argc, argv, false);
if (!init()) {
int init_failed = htstate;
done();
printf("init(): fatal error in init_%s\n", initdone[init_failed].name);
return 1;
}
((ht_app*)app)->sendmsg(msg_draw);
const char **copyrights = htcopyrights;
while (*copyrights) {
LOG(*copyrights, sys_get_name());
copyrights++;
}
LOG("appname = %s", appname);
LOG("config = %s", systemconfig_file);
int systemconfig_version = 0; // -1 for older and 1 for newer file found
int systemconfig_magic = 0; // !=0 meens wrong magic found
loadstore_result load;
int error_info;
if (!load_systemconfig(&load, &error_info)) {
switch (load) {
case LS_OK:
break;
case LS_ERROR_NOT_FOUND:
LOG_EX(LOG_WARN, "couldn't load configuration file, using defaults");
break;
case LS_ERROR_READ:
LOG_EX(LOG_ERROR, "couldn't read configuration file...");
infobox("couldn't read configuration file...");
break;
case LS_ERROR_MAGIC:
case LS_ERROR_FORMAT:
LOG_EX(LOG_ERROR, "%s %s %s...", "current configuration file ("SYSTEM_CONFIG_FILE_NAME") has", "invalid", "magic");
systemconfig_magic = true;
break;
case LS_ERROR_VERSION:
LOG_EX(LOG_ERROR, "%s %s %s...", "current configuration file ("SYSTEM_CONFIG_FILE_NAME") has", "wrong", "version");
if (error_info < ht_systemconfig_fileversion) {
systemconfig_version = -1;
} else {
systemconfig_version = 1;
}
break;
case LS_ERROR_CORRUPTED:
// done();
delete screen;
printf("\n\n\nfatal error loading configuration file (%s)", systemconfig_file);
if (error_info) {
printf(":\nerror near line %d\n", error_info);
} else {
printf(".\n");
}
printf("please try to delete it.\n");
return 1;
default: {assert(0);}
}
}
try {
params(argc, argv, true);
ht_app *a = ((ht_app*)app);
a->run(false);
} catch (const Exception &x) {
done();
ht_fprintf(stderr, "\n\nFATAL: %s: %y\n", "unhandled exception", &x);
return 1;
} catch (...) {
done();
fprintf(stderr, "\n\nFATAL: unknown %s!?\n", "unhandled exception");
return 1;
}
loadstore_result save = LS_OK;
bool save_config = true;
if (systemconfig_magic) {
if (confirmbox_modal("%s %s %s...\noverwrite it?", "current configuration file ("SYSTEM_CONFIG_FILE_NAME") has", "wrong", "magic")!=button_ok) {
save_config = false;
}
}
if (systemconfig_version < 0) {
if (confirmbox_modal("%s %s %s...\noverwrite it?", "current configuration file ("SYSTEM_CONFIG_FILE_NAME") has", "older", "version")!=button_ok) {
save_config = false;
}
} else if (systemconfig_version > 0) {
if (confirmbox_modal("%s %s %s...\noverwrite it?", "current configuration file ("SYSTEM_CONFIG_FILE_NAME") has", "NEWER", "version")!=button_ok) {
save_config = false;
}
}
String err;
if (save_config) {
LOG("saving config...");
save = save_systemconfig(err);
}
LOG("exit.");
done();
if (save != LS_OK) {
ht_printf("save_systemconfig(): error: %y\n", &err);
return 1;
}
return 0;
}
|