args.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. Handle command line arguments.
  3. Copyright (C) 2009-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009.
  7. Andrew Borodin <aborodin@vmail.ru>, 2011, 2012.
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software: you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include "lib/global.h"
  24. #include "lib/tty/tty.h"
  25. #include "lib/strutil.h"
  26. #include "lib/vfs/vfs.h"
  27. #include "lib/util.h" /* x_basename() */
  28. #include "src/textconf.h"
  29. #ifdef USE_INTERNAL_EDIT
  30. #include "editor/edit.h" /* edit_arg_t */
  31. #endif
  32. #include "src/args.h"
  33. /*** external variables **************************************************************************/
  34. /*** global variables ****************************************************************************/
  35. /* If true, assume we are running on an xterm terminal */
  36. gboolean mc_args__force_xterm = FALSE;
  37. gboolean mc_args__nomouse = FALSE;
  38. /* Force colors, only used by Slang */
  39. gboolean mc_args__force_colors = FALSE;
  40. /* Don't load keymap from file and use default one */
  41. gboolean mc_args__nokeymap = FALSE;
  42. char *mc_args__last_wd_file = NULL;
  43. /* when enabled NETCODE, use following file as logfile */
  44. char *mc_args__netfs_logfile = NULL;
  45. /* keymap file */
  46. char *mc_args__keymap_file = NULL;
  47. void *mc_run_param0 = NULL;
  48. char *mc_run_param1 = NULL;
  49. /*** file scope macro definitions ****************************************************************/
  50. /*** file scope type declarations ****************************************************************/
  51. /*** forward declarations (file scope functions) *************************************************/
  52. static gboolean parse_mc_e_argument (const gchar * option_name, const gchar * value,
  53. gpointer data, GError ** mcerror);
  54. static gboolean parse_mc_v_argument (const gchar * option_name, const gchar * value,
  55. gpointer data, GError ** mcerror);
  56. /*** file scope variables ************************************************************************/
  57. /* If true, show version info and exit */
  58. static gboolean mc_args__show_version = FALSE;
  59. static GOptionContext *context;
  60. #ifdef ENABLE_SUBSHELL
  61. static gboolean mc_args__nouse_subshell = FALSE;
  62. #endif /* ENABLE_SUBSHELL */
  63. static gboolean mc_args__show_datadirs = FALSE;
  64. static gboolean mc_args__show_datadirs_extended = FALSE;
  65. #ifdef ENABLE_CONFIGURE_ARGS
  66. static gboolean mc_args__show_configure_opts = FALSE;
  67. #endif
  68. static GOptionGroup *main_group;
  69. static const GOptionEntry argument_main_table[] = {
  70. /* *INDENT-OFF* */
  71. /* generic options */
  72. {
  73. "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
  74. &mc_args__show_version,
  75. N_("Displays the current version"),
  76. NULL
  77. },
  78. /* options for wrappers */
  79. {
  80. "datadir", 'f', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
  81. &mc_args__show_datadirs,
  82. N_("Print data directory"),
  83. NULL
  84. },
  85. /* show extended information about used data directories */
  86. {
  87. "datadir-info", 'F', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
  88. &mc_args__show_datadirs_extended,
  89. N_("Print extended info about used data directories"),
  90. NULL
  91. },
  92. #ifdef ENABLE_CONFIGURE_ARGS
  93. /* show configure options */
  94. {
  95. "configure-options", '\0', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
  96. &mc_args__show_configure_opts,
  97. N_("Print configure options"),
  98. NULL
  99. },
  100. #endif
  101. {
  102. "printwd", 'P', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
  103. &mc_args__last_wd_file,
  104. N_("Print last working directory to specified file"),
  105. N_("<file>")
  106. },
  107. #ifdef ENABLE_SUBSHELL
  108. {
  109. "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
  110. &mc_global.tty.use_subshell,
  111. N_("Enables subshell support (default)"),
  112. NULL
  113. },
  114. {
  115. "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
  116. &mc_args__nouse_subshell,
  117. N_("Disables subshell support"),
  118. NULL
  119. },
  120. #endif
  121. /* debug options */
  122. #ifdef ENABLE_VFS_FTP
  123. {
  124. "ftplog", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
  125. &mc_args__netfs_logfile,
  126. N_("Log ftp dialog to specified file"),
  127. N_("<file>")
  128. },
  129. #endif /* ENABLE_VFS_FTP */
  130. {
  131. /* handle arguments manually */
  132. "view", 'v', G_OPTION_FLAG_IN_MAIN | G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  133. (gpointer) parse_mc_v_argument,
  134. N_("Launches the file viewer on a file"),
  135. N_("<file>")
  136. },
  137. {
  138. /* handle arguments manually */
  139. "edit", 'e', G_OPTION_FLAG_IN_MAIN | G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  140. (gpointer) parse_mc_e_argument,
  141. N_("Edit files"),
  142. N_("<file> ...")
  143. },
  144. G_OPTION_ENTRY_NULL
  145. /* *INDENT-ON* */
  146. };
  147. static GOptionGroup *terminal_group;
  148. #define ARGS_TERM_OPTIONS 0
  149. static const GOptionEntry argument_terminal_table[] = {
  150. /* *INDENT-OFF* */
  151. /* terminal options */
  152. {
  153. "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  154. &mc_args__force_xterm,
  155. N_("Forces xterm features"),
  156. NULL
  157. },
  158. {
  159. "no-x11", 'X', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  160. &mc_global.tty.disable_x11,
  161. N_("Disable X11 support"),
  162. NULL
  163. },
  164. {
  165. "oldmouse", 'g', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  166. &mc_global.tty.old_mouse,
  167. N_("Tries to use an old highlight mouse tracking"),
  168. NULL
  169. },
  170. {
  171. "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  172. &mc_args__nomouse,
  173. N_("Disable mouse support in text version"),
  174. NULL
  175. },
  176. #ifdef HAVE_SLANG
  177. {
  178. "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  179. &SLtt_Try_Termcap,
  180. N_("Tries to use termcap instead of terminfo"),
  181. NULL
  182. },
  183. #endif
  184. {
  185. "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  186. &mc_global.tty.slow_terminal,
  187. N_("To run on slow terminals"),
  188. NULL
  189. },
  190. {
  191. "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  192. &mc_global.tty.ugly_line_drawing,
  193. N_("Use stickchars to draw"),
  194. NULL
  195. },
  196. #ifdef HAVE_SLANG
  197. {
  198. "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  199. &reset_hp_softkeys,
  200. N_("Resets soft keys on HP terminals"),
  201. NULL
  202. },
  203. #endif
  204. {
  205. "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
  206. &mc_args__keymap_file,
  207. N_("Load definitions of key bindings from specified file"),
  208. N_("<file>")
  209. },
  210. {
  211. "nokeymap", '\0', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  212. &mc_args__nokeymap,
  213. N_("Don't load definitions of key bindings from file, use defaults"),
  214. NULL
  215. },
  216. G_OPTION_ENTRY_NULL
  217. /* *INDENT-ON* */
  218. };
  219. #undef ARGS_TERM_OPTIONS
  220. static GOptionGroup *color_group;
  221. #define ARGS_COLOR_OPTIONS 0
  222. /* #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN */
  223. static const GOptionEntry argument_color_table[] = {
  224. /* *INDENT-OFF* */
  225. /* color options */
  226. {
  227. "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
  228. &mc_global.tty.disable_colors,
  229. N_("Requests to run in black and white"),
  230. NULL
  231. },
  232. {
  233. "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
  234. &mc_args__force_colors,
  235. N_("Request to run in color mode"),
  236. NULL
  237. },
  238. {
  239. "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
  240. &mc_global.tty.command_line_colors,
  241. N_("Specifies a color configuration"),
  242. N_("<string>")
  243. },
  244. {
  245. "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
  246. &mc_global.tty.skin,
  247. N_("Show mc with specified skin"),
  248. N_("<string>")
  249. },
  250. G_OPTION_ENTRY_NULL
  251. /* *INDENT-ON* */
  252. };
  253. #undef ARGS_COLOR_OPTIONS
  254. static gchar *mc_args__loc__colors_string = NULL;
  255. static gchar *mc_args__loc__footer_string = NULL;
  256. static gchar *mc_args__loc__header_string = NULL;
  257. static gchar *mc_args__loc__usage_string = NULL;
  258. /* --------------------------------------------------------------------------------------------- */
  259. /*** file scope functions ************************************************************************/
  260. /* --------------------------------------------------------------------------------------------- */
  261. static void
  262. mc_args_clean_temp_help_strings (void)
  263. {
  264. MC_PTR_FREE (mc_args__loc__colors_string);
  265. MC_PTR_FREE (mc_args__loc__footer_string);
  266. MC_PTR_FREE (mc_args__loc__header_string);
  267. MC_PTR_FREE (mc_args__loc__usage_string);
  268. }
  269. /* --------------------------------------------------------------------------------------------- */
  270. static GOptionGroup *
  271. mc_args_new_color_group (void)
  272. {
  273. /* *INDENT-OFF* */
  274. /* FIXME: to preserve translations, lines should be split. */
  275. mc_args__loc__colors_string = g_strdup_printf ("%s\n%s",
  276. /* TRANSLATORS: don't translate keywords */
  277. _("--colors KEYWORD={FORE},{BACK},{ATTR}:KEYWORD2=...\n\n"
  278. "{FORE}, {BACK} and {ATTR} can be omitted, and the default will be used\n"
  279. "\n Keywords:\n"
  280. " Global: errors, disabled, reverse, gauge, header\n"
  281. " input, inputmark, inputunchanged, commandlinemark\n"
  282. " bbarhotkey, bbarbutton, statusbar\n"
  283. " File display: normal, selected, marked, markselect\n"
  284. " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
  285. " errdhotfocus\n"
  286. " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
  287. " Popup menus: pmenunormal, pmenusel, pmenutitle\n"
  288. " Editor: editnormal, editbold, editmarked, editwhitespace, editnonprintable,\n"
  289. " editlinestate, editbg, editframe, editframeactive\n"
  290. " editframedrag\n"
  291. " Viewer: viewnormal,viewbold, viewunderline, viewselected\n"
  292. " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
  293. /* TRANSLATORS: don't translate color names and attributes */
  294. _("Standard Colors:\n"
  295. " black, gray, red, brightred, green, brightgreen, brown,\n"
  296. " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
  297. " brightcyan, lightgray and white\n\n"
  298. "Extended colors, when 256 colors are available:\n"
  299. " color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
  300. "Attributes:\n"
  301. " bold, italic, underline, reverse, blink; append more with '+'\n")
  302. );
  303. /* *INDENT-ON* */
  304. return g_option_group_new ("color", mc_args__loc__colors_string,
  305. _("Color options"), NULL, NULL);
  306. }
  307. /* --------------------------------------------------------------------------------------------- */
  308. static gchar *
  309. mc_args_add_usage_info (void)
  310. {
  311. gchar *s;
  312. switch (mc_global.mc_run_mode)
  313. {
  314. #ifdef USE_INTERNAL_EDIT
  315. case MC_RUN_EDITOR:
  316. s = g_strdup_printf ("%s\n", _("[+lineno] file1[:lineno] [file2[:lineno]...]"));
  317. break;
  318. #endif /* USE_INTERNAL_EDIT */
  319. case MC_RUN_VIEWER:
  320. s = g_strdup_printf ("%s\n", _("file"));
  321. break;
  322. #ifdef USE_DIFF_VIEW
  323. case MC_RUN_DIFFVIEWER:
  324. s = g_strdup_printf ("%s\n", _("file1 file2"));
  325. break;
  326. #endif /* USE_DIFF_VIEW */
  327. case MC_RUN_FULL:
  328. default:
  329. s = g_strdup_printf ("%s\n", _("[this_dir] [other_panel_dir]"));
  330. }
  331. mc_args__loc__usage_string = s;
  332. return mc_args__loc__usage_string;
  333. }
  334. /* --------------------------------------------------------------------------------------------- */
  335. static void
  336. mc_args_add_extended_info_to_help (void)
  337. {
  338. mc_args__loc__footer_string = g_strdup_printf ("%s",
  339. _
  340. ("\n"
  341. "Please send any bug reports (including the output of 'mc -V')\n"
  342. "as tickets at www.midnight-commander.org\n"));
  343. mc_args__loc__header_string =
  344. g_strdup_printf (_("GNU Midnight Commander %s\n"), mc_global.mc_version);
  345. g_option_context_set_description (context, mc_args__loc__footer_string);
  346. g_option_context_set_summary (context, mc_args__loc__header_string);
  347. }
  348. /* --------------------------------------------------------------------------------------------- */
  349. static GString *
  350. mc_args__convert_help_to_syscharset (const gchar *charset, const gchar *error_message_str,
  351. const gchar *help_str)
  352. {
  353. GString *buffer;
  354. GIConv conv;
  355. gchar *full_help_str;
  356. buffer = g_string_new ("");
  357. conv = g_iconv_open (charset, "UTF-8");
  358. full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message_str, help_str);
  359. str_convert (conv, full_help_str, buffer);
  360. g_free (full_help_str);
  361. g_iconv_close (conv);
  362. return buffer;
  363. }
  364. /* --------------------------------------------------------------------------------------------- */
  365. static gboolean
  366. parse_mc_e_argument (const gchar *option_name, const gchar *value, gpointer data, GError **mcerror)
  367. {
  368. (void) option_name;
  369. (void) value;
  370. (void) data;
  371. mc_return_val_if_error (mcerror, FALSE);
  372. mc_global.mc_run_mode = MC_RUN_EDITOR;
  373. return TRUE;
  374. }
  375. /* --------------------------------------------------------------------------------------------- */
  376. static gboolean
  377. parse_mc_v_argument (const gchar *option_name, const gchar *value, gpointer data, GError **mcerror)
  378. {
  379. (void) option_name;
  380. (void) value;
  381. (void) data;
  382. mc_return_val_if_error (mcerror, FALSE);
  383. mc_global.mc_run_mode = MC_RUN_VIEWER;
  384. return TRUE;
  385. }
  386. /* --------------------------------------------------------------------------------------------- */
  387. #ifdef USE_INTERNAL_EDIT
  388. /**
  389. * Get list of filenames (and line numbers) from command line, when mc called as editor
  390. *
  391. * @param argc count of all arguments
  392. * @param argv array of strings, contains arguments
  393. * @return list of edit_arg_t objects
  394. */
  395. static GList *
  396. parse_mcedit_arguments (int argc, char **argv)
  397. {
  398. GList *flist = NULL;
  399. int i;
  400. long first_line_number = -1;
  401. for (i = 0; i < argc; i++)
  402. {
  403. char *tmp;
  404. char *end, *p;
  405. edit_arg_t *arg;
  406. tmp = argv[i];
  407. /*
  408. * First, try to get line number as +lineno.
  409. */
  410. if (*tmp == '+')
  411. {
  412. long lineno;
  413. char *error;
  414. lineno = strtol (tmp + 1, &error, 10);
  415. if (*error == '\0')
  416. {
  417. /* this is line number */
  418. first_line_number = lineno;
  419. continue;
  420. }
  421. /* this is file name */
  422. }
  423. /*
  424. * Check for filename:lineno, followed by an optional colon.
  425. * This format is used by many programs (especially compilers)
  426. * in error messages and warnings. It is supported so that
  427. * users can quickly copy and paste file locations.
  428. */
  429. end = tmp + strlen (tmp);
  430. p = end;
  431. if (p > tmp && p[-1] == ':')
  432. p--;
  433. while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
  434. p--;
  435. if (tmp < p && p < end && p[-1] == ':')
  436. {
  437. char *fname;
  438. vfs_path_t *tmp_vpath, *fname_vpath;
  439. struct stat st;
  440. fname = g_strndup (tmp, p - 1 - tmp);
  441. tmp_vpath = vfs_path_from_str (tmp);
  442. fname_vpath = vfs_path_from_str (fname);
  443. /*
  444. * Check that the file before the colon actually exists.
  445. * If it doesn't exist, create new file.
  446. */
  447. if (mc_stat (tmp_vpath, &st) == -1 && mc_stat (fname_vpath, &st) != -1)
  448. {
  449. arg = edit_arg_vpath_new (fname_vpath, atoi (p));
  450. vfs_path_free (tmp_vpath, TRUE);
  451. }
  452. else
  453. {
  454. arg = edit_arg_vpath_new (tmp_vpath, 0);
  455. vfs_path_free (fname_vpath, TRUE);
  456. }
  457. g_free (fname);
  458. }
  459. else
  460. arg = edit_arg_new (tmp, 0);
  461. flist = g_list_prepend (flist, arg);
  462. }
  463. if (flist == NULL)
  464. flist = g_list_prepend (flist, edit_arg_new (NULL, 0));
  465. else if (first_line_number != -1)
  466. {
  467. /* overwrite line number for first file */
  468. GList *l;
  469. l = g_list_last (flist);
  470. ((edit_arg_t *) l->data)->line_number = first_line_number;
  471. }
  472. return flist;
  473. }
  474. #endif /* USE_INTERNAL_EDIT */
  475. /* --------------------------------------------------------------------------------------------- */
  476. /*** public functions ****************************************************************************/
  477. /* --------------------------------------------------------------------------------------------- */
  478. void
  479. mc_setup_run_mode (char **argv)
  480. {
  481. const char *base;
  482. base = x_basename (argv[0]);
  483. if (strncmp (base, "mcv", 3) == 0 || strcmp (base, "view") == 0)
  484. {
  485. /* mcv* or view is link to mc */
  486. mc_global.mc_run_mode = MC_RUN_VIEWER;
  487. }
  488. #ifdef USE_INTERNAL_EDIT
  489. else if (strncmp (base, "mce", 3) == 0 || strcmp (base, "vi") == 0)
  490. {
  491. /* mce* or vi is link to mc */
  492. mc_global.mc_run_mode = MC_RUN_EDITOR;
  493. }
  494. #endif
  495. #ifdef USE_DIFF_VIEW
  496. else if (strncmp (base, "mcd", 3) == 0 || strcmp (base, "diff") == 0)
  497. {
  498. /* mcd* or diff is link to mc */
  499. mc_global.mc_run_mode = MC_RUN_DIFFVIEWER;
  500. }
  501. #endif /* USE_DIFF_VIEW */
  502. }
  503. /* --------------------------------------------------------------------------------------------- */
  504. gboolean
  505. mc_args_parse (int *argc, char ***argv, const char *translation_domain, GError **mcerror)
  506. {
  507. const gchar *_system_codepage;
  508. gboolean ok = TRUE;
  509. mc_return_val_if_error (mcerror, FALSE);
  510. _system_codepage = str_detect_termencoding ();
  511. #ifdef ENABLE_NLS
  512. if (!str_isutf8 (_system_codepage))
  513. bind_textdomain_codeset ("mc", "UTF-8");
  514. #endif
  515. context = g_option_context_new (mc_args_add_usage_info ());
  516. g_option_context_set_ignore_unknown_options (context, FALSE);
  517. mc_args_add_extended_info_to_help ();
  518. main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
  519. g_option_group_add_entries (main_group, argument_main_table);
  520. g_option_context_set_main_group (context, main_group);
  521. g_option_group_set_translation_domain (main_group, translation_domain);
  522. terminal_group = g_option_group_new ("terminal", _("Terminal options"),
  523. _("Terminal options"), NULL, NULL);
  524. g_option_group_add_entries (terminal_group, argument_terminal_table);
  525. g_option_context_add_group (context, terminal_group);
  526. g_option_group_set_translation_domain (terminal_group, translation_domain);
  527. color_group = mc_args_new_color_group ();
  528. g_option_group_add_entries (color_group, argument_color_table);
  529. g_option_context_add_group (context, color_group);
  530. g_option_group_set_translation_domain (color_group, translation_domain);
  531. if (!g_option_context_parse (context, argc, argv, mcerror))
  532. {
  533. if (*mcerror == NULL)
  534. mc_propagate_error (mcerror, 0, "%s\n", _("Arguments parse error!"));
  535. else
  536. {
  537. gchar *help_str;
  538. help_str = g_option_context_get_help (context, TRUE, NULL);
  539. if (str_isutf8 (_system_codepage))
  540. mc_replace_error (mcerror, (*mcerror)->code, "%s\n\n%s\n", (*mcerror)->message,
  541. help_str);
  542. else
  543. {
  544. GString *full_help_str;
  545. full_help_str =
  546. mc_args__convert_help_to_syscharset (_system_codepage, (*mcerror)->message,
  547. help_str);
  548. mc_replace_error (mcerror, (*mcerror)->code, "%s", full_help_str->str);
  549. g_string_free (full_help_str, TRUE);
  550. }
  551. g_free (help_str);
  552. }
  553. ok = FALSE;
  554. }
  555. g_option_context_free (context);
  556. mc_args_clean_temp_help_strings ();
  557. #ifdef ENABLE_NLS
  558. if (!str_isutf8 (_system_codepage))
  559. bind_textdomain_codeset ("mc", _system_codepage);
  560. #endif
  561. return ok;
  562. }
  563. /* --------------------------------------------------------------------------------------------- */
  564. gboolean
  565. mc_args_show_info (void)
  566. {
  567. if (mc_args__show_version)
  568. {
  569. show_version ();
  570. return FALSE;
  571. }
  572. if (mc_args__show_datadirs)
  573. {
  574. printf ("%s (%s)\n", mc_global.sysconfig_dir, mc_global.share_data_dir);
  575. return FALSE;
  576. }
  577. if (mc_args__show_datadirs_extended)
  578. {
  579. show_datadirs_extended ();
  580. return FALSE;
  581. }
  582. #ifdef ENABLE_CONFIGURE_ARGS
  583. if (mc_args__show_configure_opts)
  584. {
  585. show_configure_options ();
  586. return FALSE;
  587. }
  588. #endif
  589. return TRUE;
  590. }
  591. /* --------------------------------------------------------------------------------------------- */
  592. gboolean
  593. mc_setup_by_args (int argc, char **argv, GError **mcerror)
  594. {
  595. char *tmp;
  596. mc_return_val_if_error (mcerror, FALSE);
  597. if (mc_args__force_colors)
  598. mc_global.tty.disable_colors = FALSE;
  599. #ifdef ENABLE_SUBSHELL
  600. if (mc_args__nouse_subshell)
  601. mc_global.tty.use_subshell = FALSE;
  602. #endif /* ENABLE_SUBSHELL */
  603. #ifdef ENABLE_VFS_FTP
  604. if (mc_args__netfs_logfile != NULL)
  605. {
  606. vfs_path_t *vpath;
  607. vpath = vfs_path_from_str ("ftp://");
  608. mc_setctl (vpath, VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
  609. vfs_path_free (vpath, TRUE);
  610. }
  611. #endif /* ENABLE_VFS_FTP */
  612. tmp = (argc > 0) ? argv[1] : NULL;
  613. switch (mc_global.mc_run_mode)
  614. {
  615. case MC_RUN_EDITOR:
  616. #ifdef USE_INTERNAL_EDIT
  617. mc_run_param0 = parse_mcedit_arguments (argc - 1, &argv[1]);
  618. break;
  619. #else
  620. mc_propagate_error (mcerror, 0, "%s\n", _("MC is built without builtin editor."));
  621. return FALSE;
  622. #endif
  623. case MC_RUN_VIEWER:
  624. if (tmp == NULL)
  625. {
  626. mc_propagate_error (mcerror, 0, "%s\n", _("No arguments given to the viewer."));
  627. return FALSE;
  628. }
  629. mc_run_param0 = g_strdup (tmp);
  630. break;
  631. #ifdef USE_DIFF_VIEW
  632. case MC_RUN_DIFFVIEWER:
  633. if (argc < 3)
  634. {
  635. mc_propagate_error (mcerror, 0, "%s\n",
  636. _("Two files are required to invoke the diffviewer."));
  637. return FALSE;
  638. }
  639. MC_FALLTHROUGH;
  640. #endif /* USE_DIFF_VIEW */
  641. case MC_RUN_FULL:
  642. default:
  643. /* set the current dir and the other dir for filemanager,
  644. or two files for diff viewer */
  645. if (tmp != NULL)
  646. {
  647. mc_run_param0 = g_strdup (tmp);
  648. tmp = (argc > 1) ? argv[2] : NULL;
  649. if (tmp != NULL)
  650. mc_run_param1 = g_strdup (tmp);
  651. }
  652. break;
  653. }
  654. return TRUE;
  655. }
  656. /* --------------------------------------------------------------------------------------------- */