This source file includes following definitions.
- mc_args_clean_temp_help_strings
- mc_args_new_color_group
- mc_args_add_usage_info
- mc_args_add_extended_info_to_help
- mc_args__convert_help_to_syscharset
- parse_mc_e_argument
- parse_mc_v_argument
- parse_mcedit_arguments
- mc_setup_run_mode
- mc_args_parse
- mc_args_show_info
- mc_setup_by_args
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 #include <config.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30
31 #include "lib/global.h"
32 #include "lib/tty/tty.h"
33 #include "lib/strutil.h"
34 #include "lib/vfs/vfs.h"
35 #include "lib/util.h"
36
37 #include "src/textconf.h"
38
39 #ifdef USE_INTERNAL_EDIT
40 #include "editor/edit.h"
41 #endif
42
43 #include "src/args.h"
44
45
46
47
48
49
50 gboolean mc_args__force_xterm = FALSE;
51
52 gboolean mc_args__nomouse = FALSE;
53
54
55 gboolean mc_args__force_colors = FALSE;
56
57
58 gboolean mc_args__nokeymap = FALSE;
59
60 char *mc_args__last_wd_file = NULL;
61
62
63 char *mc_args__netfs_logfile = NULL;
64
65
66 char *mc_args__keymap_file = NULL;
67
68 void *mc_run_param0 = NULL;
69 char *mc_run_param1 = NULL;
70
71
72
73
74
75
76
77 static gboolean parse_mc_e_argument (const gchar * option_name, const gchar * value,
78 gpointer data, GError ** mcerror);
79 static gboolean parse_mc_v_argument (const gchar * option_name, const gchar * value,
80 gpointer data, GError ** mcerror);
81
82
83
84
85 static gboolean mc_args__show_version = FALSE;
86
87 static GOptionContext *context;
88
89 #ifdef ENABLE_SUBSHELL
90 static gboolean mc_args__nouse_subshell = FALSE;
91 #endif
92 static gboolean mc_args__show_datadirs = FALSE;
93 static gboolean mc_args__show_datadirs_extended = FALSE;
94 #ifdef ENABLE_CONFIGURE_ARGS
95 static gboolean mc_args__show_configure_opts = FALSE;
96 #endif
97
98 static GOptionGroup *main_group;
99
100 static const GOptionEntry argument_main_table[] = {
101
102
103 {
104 "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
105 &mc_args__show_version,
106 N_("Displays the current version"),
107 NULL
108 },
109
110
111 {
112 "datadir", 'f', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
113 &mc_args__show_datadirs,
114 N_("Print data directory"),
115 NULL
116 },
117
118
119 {
120 "datadir-info", 'F', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
121 &mc_args__show_datadirs_extended,
122 N_("Print extended info about used data directories"),
123 NULL
124 },
125
126 #ifdef ENABLE_CONFIGURE_ARGS
127
128 {
129 "configure-options", '\0', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
130 &mc_args__show_configure_opts,
131 N_("Print configure options"),
132 NULL
133 },
134 #endif
135
136 {
137 "printwd", 'P', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
138 &mc_args__last_wd_file,
139 N_("Print last working directory to specified file"),
140 N_("<file>")
141 },
142
143 #ifdef ENABLE_SUBSHELL
144 {
145 "subshell", 'U', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
146 &mc_global.tty.use_subshell,
147 N_("Enables subshell support (default)"),
148 NULL
149 },
150
151 {
152 "nosubshell", 'u', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
153 &mc_args__nouse_subshell,
154 N_("Disables subshell support"),
155 NULL
156 },
157 #endif
158
159
160 #ifdef ENABLE_VFS_FTP
161 {
162 "ftplog", 'l', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING,
163 &mc_args__netfs_logfile,
164 N_("Log ftp dialog to specified file"),
165 N_("<file>")
166 },
167 #endif
168
169 {
170
171 "view", 'v', G_OPTION_FLAG_IN_MAIN | G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
172 (gpointer) parse_mc_v_argument,
173 N_("Launches the file viewer on a file"),
174 N_("<file>")
175 },
176
177 {
178
179 "edit", 'e', G_OPTION_FLAG_IN_MAIN | G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
180 (gpointer) parse_mc_e_argument,
181 N_("Edit files"),
182 N_("<file> ...")
183 },
184
185 G_OPTION_ENTRY_NULL
186
187 };
188
189 static GOptionGroup *terminal_group;
190 #define ARGS_TERM_OPTIONS 0
191 static const GOptionEntry argument_terminal_table[] = {
192
193
194 {
195 "xterm", 'x', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
196 &mc_args__force_xterm,
197 N_("Forces xterm features"),
198 NULL
199 },
200
201 {
202 "no-x11", 'X', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
203 &mc_global.tty.disable_x11,
204 N_("Disable X11 support"),
205 NULL
206 },
207
208 {
209 "oldmouse", 'g', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
210 &mc_global.tty.old_mouse,
211 N_("Tries to use an old highlight mouse tracking"),
212 NULL
213 },
214
215 {
216 "nomouse", 'd', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
217 &mc_args__nomouse,
218 N_("Disable mouse support in text version"),
219 NULL
220 },
221
222 #ifdef HAVE_SLANG
223 {
224 "termcap", 't', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
225 &SLtt_Try_Termcap,
226 N_("Tries to use termcap instead of terminfo"),
227 NULL
228 },
229 #endif
230
231 {
232 "slow", 's', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
233 &mc_global.tty.slow_terminal,
234 N_("To run on slow terminals"),
235 NULL
236 },
237
238 {
239 "stickchars", 'a', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
240 &mc_global.tty.ugly_line_drawing,
241 N_("Use stickchars to draw"),
242 NULL
243 },
244
245 #ifdef HAVE_SLANG
246 {
247 "resetsoft", 'k', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
248 &reset_hp_softkeys,
249 N_("Resets soft keys on HP terminals"),
250 NULL
251 },
252 #endif
253
254 {
255 "keymap", 'K', ARGS_TERM_OPTIONS, G_OPTION_ARG_STRING,
256 &mc_args__keymap_file,
257 N_("Load definitions of key bindings from specified file"),
258 N_("<file>")
259 },
260
261 {
262 "nokeymap", '\0', ARGS_TERM_OPTIONS, G_OPTION_ARG_NONE,
263 &mc_args__nokeymap,
264 N_("Don't load definitions of key bindings from file, use defaults"),
265 NULL
266 },
267
268 G_OPTION_ENTRY_NULL
269
270 };
271
272 #undef ARGS_TERM_OPTIONS
273
274 static GOptionGroup *color_group;
275 #define ARGS_COLOR_OPTIONS 0
276
277 static const GOptionEntry argument_color_table[] = {
278
279
280 {
281 "nocolor", 'b', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
282 &mc_global.tty.disable_colors,
283 N_("Requests to run in black and white"),
284 NULL
285 },
286
287 {
288 "color", 'c', ARGS_COLOR_OPTIONS, G_OPTION_ARG_NONE,
289 &mc_args__force_colors,
290 N_("Request to run in color mode"),
291 NULL
292 },
293
294 {
295 "colors", 'C', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
296 &mc_global.tty.command_line_colors,
297 N_("Specifies a color configuration"),
298 N_("<string>")
299 },
300
301 {
302 "skin", 'S', ARGS_COLOR_OPTIONS, G_OPTION_ARG_STRING,
303 &mc_global.tty.skin,
304 N_("Show mc with specified skin"),
305 N_("<string>")
306 },
307
308 G_OPTION_ENTRY_NULL
309
310 };
311
312 #undef ARGS_COLOR_OPTIONS
313
314 static gchar *mc_args__loc__colors_string = NULL;
315 static gchar *mc_args__loc__footer_string = NULL;
316 static gchar *mc_args__loc__header_string = NULL;
317 static gchar *mc_args__loc__usage_string = NULL;
318
319
320
321
322
323 static void
324 mc_args_clean_temp_help_strings (void)
325 {
326 MC_PTR_FREE (mc_args__loc__colors_string);
327 MC_PTR_FREE (mc_args__loc__footer_string);
328 MC_PTR_FREE (mc_args__loc__header_string);
329 MC_PTR_FREE (mc_args__loc__usage_string);
330 }
331
332
333
334 static GOptionGroup *
335 mc_args_new_color_group (void)
336 {
337
338
339 mc_args__loc__colors_string = g_strdup_printf ("%s\n%s",
340
341 _("--colors KEYWORD={FORE},{BACK},{ATTR}:KEYWORD2=...\n\n"
342 "{FORE}, {BACK} and {ATTR} can be omitted, and the default will be used\n"
343 "\n Keywords:\n"
344 " Global: errors, disabled, reverse, gauge, header\n"
345 " input, inputmark, inputunchanged, commandlinemark\n"
346 " bbarhotkey, bbarbutton, statusbar\n"
347 " File display: normal, selected, marked, markselect\n"
348 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
349 " errdhotfocus\n"
350 " Menus: menunormal, menuhot, menusel, menuhotsel, menuinactive\n"
351 " Popup menus: pmenunormal, pmenusel, pmenutitle\n"
352 " Editor: editnormal, editbold, editmarked, editwhitespace, editnonprintable,\n"
353 " editlinestate, editbg, editframe, editframeactive\n"
354 " editframedrag\n"
355 " Viewer: viewnormal,viewbold, viewunderline, viewselected\n"
356 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"),
357
358 _("Standard Colors:\n"
359 " black, gray, red, brightred, green, brightgreen, brown,\n"
360 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
361 " brightcyan, lightgray and white\n\n"
362 "Extended colors, when 256 colors are available:\n"
363 " color16 to color255, or rgb000 to rgb555 and gray0 to gray23\n\n"
364 "Attributes:\n"
365 " bold, italic, underline, reverse, blink; append more with '+'\n")
366 );
367
368
369 return g_option_group_new ("color", mc_args__loc__colors_string,
370 _("Color options"), NULL, NULL);
371
372 }
373
374
375
376 static gchar *
377 mc_args_add_usage_info (void)
378 {
379 gchar *s;
380
381 switch (mc_global.mc_run_mode)
382 {
383 #ifdef USE_INTERNAL_EDIT
384 case MC_RUN_EDITOR:
385 s = g_strdup_printf ("%s\n", _("[+lineno] file1[:lineno] [file2[:lineno]...]"));
386 break;
387 #endif
388 case MC_RUN_VIEWER:
389 s = g_strdup_printf ("%s\n", _("file"));
390 break;
391 #ifdef USE_DIFF_VIEW
392 case MC_RUN_DIFFVIEWER:
393 s = g_strdup_printf ("%s\n", _("file1 file2"));
394 break;
395 #endif
396 case MC_RUN_FULL:
397 default:
398 s = g_strdup_printf ("%s\n", _("[this_dir] [other_panel_dir]"));
399 }
400
401 mc_args__loc__usage_string = s;
402
403 return mc_args__loc__usage_string;
404 }
405
406
407
408 static void
409 mc_args_add_extended_info_to_help (void)
410 {
411 mc_args__loc__footer_string = g_strdup_printf ("%s",
412 _
413 ("\n"
414 "Please send any bug reports (including the output of 'mc -V')\n"
415 "as tickets at www.midnight-commander.org\n"));
416 mc_args__loc__header_string =
417 g_strdup_printf (_("GNU Midnight Commander %s\n"), mc_global.mc_version);
418
419 g_option_context_set_description (context, mc_args__loc__footer_string);
420 g_option_context_set_summary (context, mc_args__loc__header_string);
421 }
422
423
424
425 static GString *
426 mc_args__convert_help_to_syscharset (const gchar *charset, const gchar *error_message_str,
427 const gchar *help_str)
428 {
429 GString *buffer;
430 GIConv conv;
431 gchar *full_help_str;
432
433 buffer = g_string_new ("");
434 conv = g_iconv_open (charset, "UTF-8");
435 full_help_str = g_strdup_printf ("%s\n\n%s\n", error_message_str, help_str);
436
437 str_convert (conv, full_help_str, buffer);
438
439 g_free (full_help_str);
440 g_iconv_close (conv);
441
442 return buffer;
443 }
444
445
446
447 static gboolean
448 parse_mc_e_argument (const gchar *option_name, const gchar *value, gpointer data, GError **mcerror)
449 {
450 (void) option_name;
451 (void) value;
452 (void) data;
453
454 mc_return_val_if_error (mcerror, FALSE);
455
456 mc_global.mc_run_mode = MC_RUN_EDITOR;
457
458 return TRUE;
459 }
460
461
462
463 static gboolean
464 parse_mc_v_argument (const gchar *option_name, const gchar *value, gpointer data, GError **mcerror)
465 {
466 (void) option_name;
467 (void) value;
468 (void) data;
469
470 mc_return_val_if_error (mcerror, FALSE);
471
472 mc_global.mc_run_mode = MC_RUN_VIEWER;
473
474 return TRUE;
475 }
476
477
478
479 #ifdef USE_INTERNAL_EDIT
480
481
482
483
484
485
486
487
488 static GList *
489 parse_mcedit_arguments (int argc, char **argv)
490 {
491 GList *flist = NULL;
492 int i;
493 long first_line_number = -1;
494
495 for (i = 0; i < argc; i++)
496 {
497 char *tmp;
498 char *end, *p;
499 edit_arg_t *arg;
500
501 tmp = argv[i];
502
503
504
505
506 if (*tmp == '+')
507 {
508 long lineno;
509 char *error;
510
511 lineno = strtol (tmp + 1, &error, 10);
512
513 if (*error == '\0')
514 {
515
516 first_line_number = lineno;
517 continue;
518 }
519
520 }
521
522
523
524
525
526
527
528 end = tmp + strlen (tmp);
529 p = end;
530
531 if (p > tmp && p[-1] == ':')
532 p--;
533 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
534 p--;
535
536 if (tmp < p && p < end && p[-1] == ':')
537 {
538 char *fname;
539 vfs_path_t *tmp_vpath, *fname_vpath;
540 struct stat st;
541
542 fname = g_strndup (tmp, p - 1 - tmp);
543 tmp_vpath = vfs_path_from_str (tmp);
544 fname_vpath = vfs_path_from_str (fname);
545
546
547
548
549
550 if (mc_stat (tmp_vpath, &st) == -1 && mc_stat (fname_vpath, &st) != -1)
551 {
552 arg = edit_arg_vpath_new (fname_vpath, atoi (p));
553 vfs_path_free (tmp_vpath, TRUE);
554 }
555 else
556 {
557 arg = edit_arg_vpath_new (tmp_vpath, 0);
558 vfs_path_free (fname_vpath, TRUE);
559 }
560
561 g_free (fname);
562 }
563 else
564 arg = edit_arg_new (tmp, 0);
565
566 flist = g_list_prepend (flist, arg);
567 }
568
569 if (flist == NULL)
570 flist = g_list_prepend (flist, edit_arg_new (NULL, 0));
571 else if (first_line_number != -1)
572 {
573
574 GList *l;
575
576 l = g_list_last (flist);
577 ((edit_arg_t *) l->data)->line_number = first_line_number;
578 }
579
580 return flist;
581 }
582 #endif
583
584
585
586
587
588 void
589 mc_setup_run_mode (char **argv)
590 {
591 const char *base;
592
593 base = x_basename (argv[0]);
594
595 if (strncmp (base, "mcv", 3) == 0 || strcmp (base, "view") == 0)
596 {
597
598 mc_global.mc_run_mode = MC_RUN_VIEWER;
599 }
600 #ifdef USE_INTERNAL_EDIT
601 else if (strncmp (base, "mce", 3) == 0 || strcmp (base, "vi") == 0)
602 {
603
604 mc_global.mc_run_mode = MC_RUN_EDITOR;
605 }
606 #endif
607 #ifdef USE_DIFF_VIEW
608 else if (strncmp (base, "mcd", 3) == 0 || strcmp (base, "diff") == 0)
609 {
610
611 mc_global.mc_run_mode = MC_RUN_DIFFVIEWER;
612 }
613 #endif
614 }
615
616
617
618 gboolean
619 mc_args_parse (int *argc, char ***argv, const char *translation_domain, GError **mcerror)
620 {
621 const gchar *_system_codepage;
622 gboolean ok = TRUE;
623
624 mc_return_val_if_error (mcerror, FALSE);
625
626 _system_codepage = str_detect_termencoding ();
627
628 #ifdef ENABLE_NLS
629 if (!str_isutf8 (_system_codepage))
630 bind_textdomain_codeset ("mc", "UTF-8");
631 #endif
632
633 context = g_option_context_new (mc_args_add_usage_info ());
634
635 g_option_context_set_ignore_unknown_options (context, FALSE);
636
637 mc_args_add_extended_info_to_help ();
638
639 main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);
640
641 g_option_group_add_entries (main_group, argument_main_table);
642 g_option_context_set_main_group (context, main_group);
643 g_option_group_set_translation_domain (main_group, translation_domain);
644
645 terminal_group = g_option_group_new ("terminal", _("Terminal options"),
646 _("Terminal options"), NULL, NULL);
647
648 g_option_group_add_entries (terminal_group, argument_terminal_table);
649 g_option_context_add_group (context, terminal_group);
650 g_option_group_set_translation_domain (terminal_group, translation_domain);
651
652 color_group = mc_args_new_color_group ();
653
654 g_option_group_add_entries (color_group, argument_color_table);
655 g_option_context_add_group (context, color_group);
656 g_option_group_set_translation_domain (color_group, translation_domain);
657
658 if (!g_option_context_parse (context, argc, argv, mcerror))
659 {
660 if (*mcerror == NULL)
661 mc_propagate_error (mcerror, 0, "%s\n", _("Arguments parse error!"));
662 else
663 {
664 gchar *help_str;
665
666 help_str = g_option_context_get_help (context, TRUE, NULL);
667
668 if (str_isutf8 (_system_codepage))
669 mc_replace_error (mcerror, (*mcerror)->code, "%s\n\n%s\n", (*mcerror)->message,
670 help_str);
671 else
672 {
673 GString *full_help_str;
674
675 full_help_str =
676 mc_args__convert_help_to_syscharset (_system_codepage, (*mcerror)->message,
677 help_str);
678 mc_replace_error (mcerror, (*mcerror)->code, "%s", full_help_str->str);
679 g_string_free (full_help_str, TRUE);
680 }
681 g_free (help_str);
682 }
683
684 ok = FALSE;
685 }
686
687 g_option_context_free (context);
688 mc_args_clean_temp_help_strings ();
689
690 #ifdef ENABLE_NLS
691 if (!str_isutf8 (_system_codepage))
692 bind_textdomain_codeset ("mc", _system_codepage);
693 #endif
694
695 return ok;
696 }
697
698
699
700 gboolean
701 mc_args_show_info (void)
702 {
703 if (mc_args__show_version)
704 {
705 show_version ();
706 return FALSE;
707 }
708
709 if (mc_args__show_datadirs)
710 {
711 printf ("%s (%s)\n", mc_global.sysconfig_dir, mc_global.share_data_dir);
712 return FALSE;
713 }
714
715 if (mc_args__show_datadirs_extended)
716 {
717 show_datadirs_extended ();
718 return FALSE;
719 }
720
721 #ifdef ENABLE_CONFIGURE_ARGS
722 if (mc_args__show_configure_opts)
723 {
724 show_configure_options ();
725 return FALSE;
726 }
727 #endif
728
729 return TRUE;
730 }
731
732
733
734 gboolean
735 mc_setup_by_args (int argc, char **argv, GError **mcerror)
736 {
737 char *tmp;
738
739 mc_return_val_if_error (mcerror, FALSE);
740
741 if (mc_args__force_colors)
742 mc_global.tty.disable_colors = FALSE;
743
744 #ifdef ENABLE_SUBSHELL
745 if (mc_args__nouse_subshell)
746 mc_global.tty.use_subshell = FALSE;
747 #endif
748
749 #ifdef ENABLE_VFS_FTP
750 if (mc_args__netfs_logfile != NULL)
751 {
752 vfs_path_t *vpath;
753
754 vpath = vfs_path_from_str ("ftp://");
755 mc_setctl (vpath, VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
756 vfs_path_free (vpath, TRUE);
757 }
758 #endif
759
760 tmp = (argc > 0) ? argv[1] : NULL;
761
762 switch (mc_global.mc_run_mode)
763 {
764 case MC_RUN_EDITOR:
765 #ifdef USE_INTERNAL_EDIT
766 mc_run_param0 = parse_mcedit_arguments (argc - 1, &argv[1]);
767 break;
768 #else
769 mc_propagate_error (mcerror, 0, "%s\n", _("MC is built without builtin editor."));
770 return FALSE;
771 #endif
772
773 case MC_RUN_VIEWER:
774 if (tmp == NULL)
775 {
776 mc_propagate_error (mcerror, 0, "%s\n", _("No arguments given to the viewer."));
777 return FALSE;
778 }
779
780 mc_run_param0 = g_strdup (tmp);
781 break;
782
783 #ifdef USE_DIFF_VIEW
784 case MC_RUN_DIFFVIEWER:
785 if (argc < 3)
786 {
787 mc_propagate_error (mcerror, 0, "%s\n",
788 _("Two files are required to invoke the diffviewer."));
789 return FALSE;
790 }
791 MC_FALLTHROUGH;
792 #endif
793
794 case MC_RUN_FULL:
795 default:
796
797
798 if (tmp != NULL)
799 {
800 mc_run_param0 = g_strdup (tmp);
801 tmp = (argc > 1) ? argv[2] : NULL;
802 if (tmp != NULL)
803 mc_run_param1 = g_strdup (tmp);
804 }
805 break;
806 }
807
808 return TRUE;
809 }
810
811