args.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. Handle command line arguments.
  3. Copyright (C) 2009 The Free Software Foundation, Inc.
  4. Written by:
  5. Slava Zanko <slavazanko@gmail.com>, 2009.
  6. This file is part of the Midnight Commander.
  7. The Midnight Commander is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  11. The Midnight Commander is distributed in the hope that it will be
  12. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. MA 02110-1301, USA.
  19. */
  20. #include <config.h>
  21. #include <stdio.h>
  22. #include "../src/global.h"
  23. #include "../src/tty/tty.h"
  24. #include "../src/args.h"
  25. #include "../src/strutil.h"
  26. /*** external variables **************************************************************************/
  27. extern int reset_hp_softkeys;
  28. extern char *mc_main_sysconf_dir;
  29. extern char *mc_main_sharedata_dir;
  30. /* colors specified on the command line: they override any other setting */
  31. extern char *command_line_colors;
  32. extern const char *edit_one_file;
  33. extern const char *view_one_file;
  34. /*** global variables ****************************************************************************/
  35. /* If using a subshell for evaluating commands this is true */
  36. gboolean mc_args__use_subshell =
  37. #ifdef SUBSHELL_OPTIONAL
  38. FALSE;
  39. #else
  40. TRUE;
  41. #endif
  42. /* If true, show version info and exit */
  43. gboolean mc_args__version = FALSE;
  44. /* If true, assume we are running on an xterm terminal */
  45. gboolean mc_args__force_xterm = FALSE;
  46. gboolean mc_args__nomouse = FALSE;
  47. /* For slow terminals */
  48. gboolean mc_args__slow_terminal = FALSE;
  49. /* If true use +, -, | for line drawing */
  50. gboolean mc_args__ugly_line_drawing = FALSE;
  51. /* Set to force black and white display at program startup */
  52. gboolean mc_args__disable_colors = FALSE;
  53. /* Force colors, only used by Slang */
  54. gboolean mc_args__force_colors = FALSE;
  55. /* Show in specified skin */
  56. char *mc_args__skin = NULL;
  57. char *mc_args__last_wd_file = NULL;
  58. /* when enabled NETCODE, use folowing file as logfile */
  59. char *mc_args__netfs_logfile = NULL;
  60. /* keymap file */
  61. char *mc_args__keymap_file = NULL;
  62. /* Debug level*/
  63. int mc_args__debug_level = 0;
  64. /*** file scope macro definitions ****************************************************************/
  65. /*** file scope type declarations ****************************************************************/
  66. /*** file scope variables ************************************************************************/
  67. static GOptionContext *context;
  68. static gboolean mc_args__nouse_subshell = FALSE;
  69. static gboolean mc_args__show_datadirs = FALSE;
  70. GOptionGroup *main_group;
  71. static const GOptionEntry argument_main_table[] = {
  72. /* generic options */
  73. {
  74. "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
  75. &mc_args__version,
  76. N_("Displays the current version"),
  77. NULL
  78. },
  79. /* options for wrappers */
  80. {
  81. "datadir", 'f', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
  82. &mc_args__show_datadirs,
  83. N_("Print data directory"),
  84. NULL
  85. },
  86. {
  87. "printwd", 'P', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
  88. &mc_args__last_wd_file,
  89. N_("Print last working directory to specified file"),
  90. "<file>"
  91. },
  92. #ifdef HAVE_SUBSHELL_SUPPORT
  93. {
  94. "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
  95. &mc_args__use_subshell,
  96. N_("Enables subshell support (default)"),
  97. NULL
  98. },
  99. {
  100. "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
  101. &mc_args__nouse_subshell,
  102. N_("Disables subshell support"),
  103. NULL
  104. },
  105. #endif
  106. /* debug options */
  107. #ifdef USE_NETCODE
  108. {
  109. "ftplog", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
  110. &mc_args__netfs_logfile,
  111. N_("Log ftp dialog to specified file"),
  112. "<file>"
  113. },
  114. #ifdef WITH_SMBFS
  115. {
  116. "debuglevel", 'D', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_INT,
  117. &mc_args__debug_level,
  118. N_("Set debug level"),
  119. "<integer>"
  120. },
  121. #endif
  122. #endif
  123. /* single file operations */
  124. {
  125. "view", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
  126. &view_one_file,
  127. N_("Launches the file viewer on a file"),
  128. "<file>"
  129. },
  130. {
  131. "edit", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
  132. &edit_one_file,
  133. N_("Edits one file"),
  134. "<file>"
  135. },
  136. {NULL}
  137. };
  138. GOptionGroup *terminal_group;
  139. #define ARGS_TERM_OPTIONS 0
  140. static const GOptionEntry argument_terminal_table[] = {
  141. /* terminal options */
  142. {
  143. "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  144. &mc_args__force_xterm,
  145. N_("Forces xterm features"),
  146. NULL
  147. },
  148. {
  149. "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  150. &mc_args__nomouse,
  151. N_("Disable mouse support in text version"),
  152. NULL
  153. },
  154. #ifdef HAVE_SLANG
  155. {
  156. "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  157. &SLtt_Try_Termcap,
  158. N_("Tries to use termcap instead of terminfo"),
  159. NULL
  160. },
  161. #endif
  162. {
  163. "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  164. &mc_args__slow_terminal,
  165. N_("To run on slow terminals"),
  166. NULL
  167. },
  168. {
  169. "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  170. &mc_args__ugly_line_drawing,
  171. N_("Use stickchars to draw"),
  172. NULL
  173. },
  174. {
  175. "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
  176. &reset_hp_softkeys,
  177. N_("Resets soft keys on HP terminals"),
  178. NULL
  179. },
  180. {
  181. "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
  182. &mc_args__keymap_file,
  183. N_("Load definitions of key bindings from specified file"),
  184. "<file>"
  185. },
  186. {NULL}
  187. };
  188. #undef ARGS_TERM_OPTIONS
  189. GOptionGroup *color_group;
  190. #define ARGS_COLOR_OPTIONS 0
  191. // #define ARGS_COLOR_OPTIONS G_OPTION_FLAG_IN_MAIN
  192. static const GOptionEntry argument_color_table[] = {
  193. /* color options */
  194. {
  195. "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
  196. &mc_args__disable_colors,
  197. N_("Requests to run in black and white"),
  198. NULL
  199. },
  200. {
  201. "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
  202. &mc_args__force_colors,
  203. N_("Request to run in color mode"),
  204. NULL
  205. },
  206. {
  207. "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
  208. &command_line_colors,
  209. N_("Specifies a color configuration"),
  210. "<string>"
  211. },
  212. {
  213. "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
  214. &mc_args__skin,
  215. N_("Show mc with specified skin"),
  216. "<string>"
  217. },
  218. {NULL}
  219. };
  220. #undef ARGS_COLOR_OPTIONS
  221. static gchar *mc_args__loc__colors_string = NULL;
  222. static gchar *mc_args__loc__footer_string = NULL;
  223. static gchar *mc_args__loc__header_string = NULL;
  224. static gchar *mc_args__loc__usage_string = NULL;
  225. /*** file scope functions ************************************************************************/
  226. /* --------------------------------------------------------------------------------------------- */
  227. static void
  228. mc_args_clean_temp_help_strings(void)
  229. {
  230. g_free(mc_args__loc__colors_string);
  231. mc_args__loc__colors_string = NULL;
  232. g_free(mc_args__loc__footer_string);
  233. mc_args__loc__footer_string = NULL;
  234. g_free(mc_args__loc__header_string);
  235. mc_args__loc__header_string = NULL;
  236. g_free(mc_args__loc__usage_string);
  237. mc_args__loc__usage_string = NULL;
  238. }
  239. /* --------------------------------------------------------------------------------------------- */
  240. static GOptionGroup *
  241. mc_args_new_color_group(void)
  242. {
  243. /*
  244. * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
  245. * and editmarked. To preserve translations, lines should be split.
  246. */
  247. mc_args__loc__colors_string = g_strdup_printf("%s%s",
  248. /* TRANSLATORS: don't translate keywords and names of colors */
  249. _( "--colors KEYWORD={FORE},{BACK}\n\n"
  250. "{FORE} and {BACK} can be omitted, and the default will be used\n"
  251. "\n" "Keywords:\n"
  252. " Global: errors, reverse, gauge, input, viewunderline\n"
  253. " File display: normal, selected, marked, markselect\n"
  254. " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
  255. " errdhotfocus\n"
  256. " Menus: menu, menuhot, menusel, menuhotsel\n"
  257. " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
  258. " editlinestate\n"),
  259. /* TRANSLATORS: don't translate keywords and names of colors */
  260. _( " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
  261. "\n" "Colors:\n"
  262. " black, gray, red, brightred, green, brightgreen, brown,\n"
  263. " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
  264. " brightcyan, lightgray and white\n\n")
  265. );
  266. return g_option_group_new ("color", mc_args__loc__colors_string,
  267. _("Color options"),NULL, NULL);
  268. }
  269. /* --------------------------------------------------------------------------------------------- */
  270. static gchar *
  271. mc_args_add_usage_info(void)
  272. {
  273. mc_args__loc__usage_string = g_strdup_printf("[%s] %s\n %s - %s\n",
  274. _("+number"),
  275. _("[this_dir] [other_panel_dir]"),
  276. _("+number"),
  277. _("Set initial line number for the internal editor")
  278. );
  279. return mc_args__loc__usage_string;
  280. }
  281. /* --------------------------------------------------------------------------------------------- */
  282. static void
  283. mc_args_add_extended_info_to_help(void)
  284. {
  285. mc_args__loc__footer_string = g_strdup_printf("%s",
  286. _
  287. ("\n"
  288. "Please send any bug reports (including the output of `mc -V')\n"
  289. "to mc-devel@gnome.org\n")
  290. );
  291. mc_args__loc__header_string = g_strdup_printf (_("GNU Midnight Commander %s\n"), VERSION);
  292. #if GLIB_CHECK_VERSION(2,12,0)
  293. g_option_context_set_description (context, mc_args__loc__footer_string);
  294. g_option_context_set_summary (context, mc_args__loc__header_string);
  295. #endif
  296. }
  297. /* --------------------------------------------------------------------------------------------- */
  298. static gboolean
  299. mc_args_process(void)
  300. {
  301. if (mc_args__version){
  302. show_version ();
  303. return FALSE;
  304. }
  305. if (mc_args__show_datadirs){
  306. printf ("%s (%s)\n", mc_main_sysconf_dir, mc_main_sharedata_dir);
  307. return FALSE;
  308. }
  309. if (mc_args__force_colors)
  310. mc_args__disable_colors = FALSE;
  311. #ifdef HAVE_SUBSHELL_SUPPORT
  312. if (mc_args__nouse_subshell)
  313. mc_args__use_subshell = 0;
  314. if (mc_args__nouse_subshell)
  315. mc_args__use_subshell = 0;
  316. #endif /* HAVE_SUBSHELL_SUPPORT */
  317. return TRUE;
  318. }
  319. /* --------------------------------------------------------------------------------------------- */
  320. static gchar *
  321. mc_args__convert_help_to_syscharset(const gchar *charset, const gchar *error_message, const gchar *help_str)
  322. {
  323. GString *buffer = g_string_new("");
  324. GIConv conv = g_iconv_open ( charset, "UTF-8");
  325. gchar *full_help_str = g_strdup_printf("%s\n\n%s\n",error_message,help_str);
  326. str_convert (conv, full_help_str, buffer);
  327. g_free(full_help_str);
  328. g_iconv_close (conv);
  329. return g_string_free(buffer, FALSE);
  330. }
  331. /* --------------------------------------------------------------------------------------------- */
  332. /*** public functions ****************************************************************************/
  333. /* --------------------------------------------------------------------------------------------- */
  334. gboolean
  335. mc_args_handle(int *argc, char ***argv, const gchar *translation_domain)
  336. {
  337. GError *error = NULL;
  338. const gchar *_system_codepage = str_detect_termencoding();
  339. if ( !str_isutf8 (_system_codepage))
  340. bind_textdomain_codeset ("mc", "UTF-8");
  341. context = g_option_context_new (mc_args_add_usage_info());
  342. g_option_context_set_ignore_unknown_options (context, FALSE);
  343. mc_args_add_extended_info_to_help();
  344. main_group = g_option_group_new ("main", _("Main options"),
  345. _("Main options"),NULL, NULL);
  346. g_option_group_add_entries (main_group, argument_main_table);
  347. g_option_context_set_main_group (context, main_group);
  348. g_option_group_set_translation_domain(main_group, translation_domain);
  349. terminal_group = g_option_group_new ("terminal", _("Terminal options"),
  350. _("Terminal options"),NULL, NULL);
  351. g_option_group_add_entries (terminal_group, argument_terminal_table);
  352. g_option_context_add_group (context, terminal_group);
  353. g_option_group_set_translation_domain(terminal_group, translation_domain);
  354. color_group = mc_args_new_color_group();
  355. g_option_group_add_entries (color_group, argument_color_table);
  356. g_option_context_add_group (context, color_group);
  357. g_option_group_set_translation_domain(color_group, translation_domain);
  358. if (! g_option_context_parse (context, argc, argv, &error)) {
  359. if (error != NULL)
  360. {
  361. gchar *full_help_str;
  362. gchar *help_str;
  363. #if GLIB_CHECK_VERSION(2,14,0)
  364. help_str = g_option_context_get_help (context, TRUE, NULL);
  365. #else
  366. help_str = g_strdup("");
  367. #endif
  368. if ( !str_isutf8 (_system_codepage))
  369. full_help_str = mc_args__convert_help_to_syscharset(_system_codepage,error->message, help_str);
  370. else
  371. full_help_str = g_strdup_printf("%s\n\n%s\n",error->message,help_str);
  372. fprintf(stderr, "%s",full_help_str);
  373. g_free(help_str);
  374. g_free(full_help_str);
  375. g_error_free (error);
  376. }
  377. g_option_context_free (context);
  378. mc_args_clean_temp_help_strings();
  379. return FALSE;
  380. }
  381. g_option_context_free (context);
  382. mc_args_clean_temp_help_strings();
  383. if ( !str_isutf8 (_system_codepage))
  384. bind_textdomain_codeset ("mc", _system_codepage);
  385. return mc_args_process();
  386. }
  387. /* --------------------------------------------------------------------------------------------- */