main.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419
  1. /* Main program for the Midnight Commander
  2. Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
  3. 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
  4. Written by: 1994, 1995, 1996, 1997 Miguel de Icaza
  5. 1994, 1995 Janne Kukonlehto
  6. 1997 Norbert Warmuth
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU 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, MA 02110-1301, USA. */
  18. /** \file main.c
  19. * \brief Source: this is a main module
  20. */
  21. #include <config.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include <locale.h>
  25. #include <signal.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <fcntl.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <sys/wait.h>
  33. #include <unistd.h>
  34. #include <pwd.h>
  35. #include "global.h"
  36. #include "../src/tty/tty.h"
  37. #include "../src/tty/color.h"
  38. #include "../src/tty/mouse.h"
  39. #include "../src/tty/key.h" /* For init_key() and mi_getch() */
  40. #include "../src/tty/win.h"
  41. #include "dir.h"
  42. #include "dialog.h"
  43. #include "menu.h"
  44. #include "panel.h"
  45. #include "main.h"
  46. #include "option.h"
  47. #include "tree.h"
  48. #include "treestore.h"
  49. #include "cons.saver.h"
  50. #include "subshell.h"
  51. #include "setup.h" /* save_setup() */
  52. #include "../src/mcconfig/mcconfig.h"
  53. #include "boxes.h" /* sort_box() */
  54. #include "layout.h"
  55. #include "cmd.h" /* Normal commands */
  56. #include "hotlist.h"
  57. #include "panelize.h"
  58. #include "learn.h" /* learn_keys() */
  59. #include "listmode.h"
  60. #include "execute.h"
  61. #include "ext.h" /* For flush_extension_file() */
  62. #include "strutil.h"
  63. #include <pwd.h> /* for username in xterm title */
  64. /* Listbox for the command history feature */
  65. #include "widget.h"
  66. #include "command.h"
  67. #include "wtools.h"
  68. #include "chmod.h"
  69. #include "chown.h"
  70. #include "achown.h"
  71. #ifdef WITH_SMBFS
  72. #include "../vfs/smbfs.h" /* smbfs_set_debug() */
  73. #endif
  74. #ifdef USE_INTERNAL_EDIT
  75. # include "../edit/edit.h"
  76. #endif
  77. #ifdef HAVE_CHARSET
  78. #include "charsets.h"
  79. #endif /* HAVE_CHARSET */
  80. #ifdef USE_VFS
  81. #include "../vfs/gc.h"
  82. #endif
  83. #include "popt.h"
  84. /* When the modes are active, left_panel, right_panel and tree_panel */
  85. /* Point to a proper data structure. You should check with the functions */
  86. /* get_current_type and get_other_type the types of the panels before using */
  87. /* This pointer variables */
  88. /* The structures for the panels */
  89. WPanel *left_panel = NULL;
  90. WPanel *right_panel = NULL;
  91. /* The pointer to the tree */
  92. WTree *the_tree = NULL;
  93. /* The Menubar */
  94. struct WMenu *the_menubar = NULL;
  95. /* Pointers to the selected and unselected panel */
  96. WPanel *current_panel = NULL;
  97. /* Set if the command is being run from the "Right" menu */
  98. int is_right = 0;
  99. /* Set when main loop should be terminated */
  100. volatile int quit = 0;
  101. /* Set if you want the possible completions dialog for the first time */
  102. int show_all_if_ambiguous = 0;
  103. /* Set when cd symlink following is desirable (bash mode) */
  104. int cd_symlinks = 1;
  105. /* If set then dialogs just clean the screen when refreshing, else */
  106. /* they do a complete refresh, refreshing all the parts of the program */
  107. int fast_refresh = 0;
  108. /* If true, marking a files moves the cursor down */
  109. int mark_moves_down = 1;
  110. /* If true, at startup the user-menu is invoked */
  111. int auto_menu = 0;
  112. /* If true, use + and \ keys normally and select/unselect do if M-+ / M-\
  113. and M-- and keypad + / - */
  114. int alternate_plus_minus = 0;
  115. /* If true, then the +, - and \ keys have their special meaning only if the
  116. * command line is emtpy, otherwise they behave like regular letters
  117. */
  118. int only_leading_plus_minus = 1;
  119. int pause_after_run = pause_on_dumb_terminals;
  120. /* It true saves the setup when quitting */
  121. int auto_save_setup = 1;
  122. #ifndef HAVE_CHARSET
  123. /* If true, allow characters in the range 160-255 */
  124. int eight_bit_clean = 1;
  125. /*
  126. * If true, also allow characters in the range 128-159.
  127. * This is reported to break on many terminals (xterm, qansi-m).
  128. */
  129. int full_eight_bits = 0;
  130. #endif /* !HAVE_CHARSET */
  131. /*
  132. * If utf-8 terminal utf8_display = 1
  133. * Display bits set UTF-8
  134. *
  135. */
  136. int utf8_display = 0;
  137. /* If true use the internal viewer */
  138. int use_internal_view = 1;
  139. /* Have we shown the fast-reload warning in the past? */
  140. int fast_reload_w = 0;
  141. /* Move page/item? When clicking on the top or bottom of a panel */
  142. int mouse_move_pages = 1;
  143. /* If true: l&r arrows are used to chdir if the input line is empty */
  144. int navigate_with_arrows = 0;
  145. /* If true use +, -, | for line drawing */
  146. int force_ugly_line_drawing = 0;
  147. /* If true program softkeys (HP terminals only) on startup and after every
  148. command ran in the subshell to the description found in the termcap/terminfo
  149. database */
  150. int reset_hp_softkeys = 0;
  151. /* The prompt */
  152. const char *prompt = NULL;
  153. /* The widget where we draw the prompt */
  154. WLabel *the_prompt;
  155. /* The hint bar */
  156. WLabel *the_hint;
  157. /* The button bar */
  158. WButtonBar *the_bar;
  159. /* For slow terminals */
  160. int slow_terminal = 0;
  161. /* Mouse type: GPM, xterm or none */
  162. Mouse_Type use_mouse_p = MOUSE_NONE;
  163. /* If true, assume we are running on an xterm terminal */
  164. static int force_xterm = 0;
  165. /* If on, default for "No" in delete operations */
  166. int safe_delete = 0;
  167. /* Controls screen clearing before an exec */
  168. int clear_before_exec = 1;
  169. /* Asks for confirmation before deleting a file */
  170. int confirm_delete = 1;
  171. /* Asks for confirmation before deleting a hotlist entry */
  172. int confirm_directory_hotlist_delete = 1;
  173. /* Asks for confirmation before overwriting a file */
  174. int confirm_overwrite = 1;
  175. /* Asks for confirmation before executing a program by pressing enter */
  176. int confirm_execute = 0;
  177. /* Asks for confirmation before leaving the program */
  178. int confirm_exit = 1;
  179. /* Asks for confirmation when using F3 to view a directory and there
  180. are tagged files */
  181. int confirm_view_dir = 0;
  182. /* This flag indicates if the pull down menus by default drop down */
  183. int drop_menus = 0;
  184. /* if skip_check_codeset = 1 do not show warning about
  185. * system and display codeset is different
  186. */
  187. int skip_check_codeset = 0;
  188. /* The dialog handle for the main program */
  189. Dlg_head *midnight_dlg = NULL;
  190. /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
  191. /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
  192. int update_prompt = 0;
  193. /* The home directory */
  194. const char *home_dir = NULL;
  195. /* The value of the other directory, only used when loading the setup */
  196. char *other_dir = NULL;
  197. /* Only used at program boot */
  198. int boot_current_is_left = 1;
  199. static char *this_dir = NULL;
  200. /* If this is true, then when browsing the tree the other window will
  201. * automatically reload it's directory with the contents of the currently
  202. * selected directory.
  203. */
  204. int xtree_mode = 0;
  205. /* If set, then print to the given file the last directory we were at */
  206. static char *last_wd_file = NULL;
  207. static char *last_wd_string = NULL;
  208. /* Set to 1 to suppress printing the last directory */
  209. static int print_last_revert = 0;
  210. /* Force colors, only used by Slang */
  211. int force_colors = 0;
  212. /* colors specified on the command line: they override any other setting */
  213. char *command_line_colors = NULL;
  214. /* File name to view if argument was supplied */
  215. static const char *view_one_file = NULL;
  216. /* File name to edit if argument was supplied */
  217. const char *edit_one_file = NULL;
  218. /* Line to start the editor on */
  219. static int edit_one_file_start_line = 0;
  220. /* Used so that widgets know if they are being destroyed or
  221. shut down */
  222. int midnight_shutdown = 0;
  223. /* The user's shell */
  224. const char *shell = NULL;
  225. /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
  226. char *mc_home = NULL;
  227. /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
  228. char *mc_home_alt = NULL;
  229. char cmd_buf[512];
  230. static void
  231. reload_panelized (WPanel *panel)
  232. {
  233. int i, j;
  234. dir_list *list = &panel->dir;
  235. if (panel != current_panel)
  236. mc_chdir (panel->cwd);
  237. for (i = 0, j = 0; i < panel->count; i++) {
  238. if (list->list[i].f.marked) {
  239. /* Unmark the file in advance. In case the following mc_lstat
  240. * fails we are done, else we have to mark the file again
  241. * (Note: do_file_mark depends on a valid "list->list [i].buf").
  242. * IMO that's the best way to update the panel's summary status
  243. * -- Norbert
  244. */
  245. do_file_mark (panel, i, 0);
  246. }
  247. if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
  248. g_free (list->list[i].fname);
  249. continue;
  250. }
  251. if (list->list[i].f.marked)
  252. do_file_mark (panel, i, 1);
  253. if (j != i)
  254. list->list[j] = list->list[i];
  255. j++;
  256. }
  257. if (j == 0)
  258. panel->count = set_zero_dir (list);
  259. else
  260. panel->count = j;
  261. if (panel != current_panel)
  262. mc_chdir (current_panel->cwd);
  263. }
  264. static void
  265. update_one_panel_widget (WPanel *panel, int force_update,
  266. const char *current_file)
  267. {
  268. int free_pointer;
  269. char *my_current_file = NULL;
  270. if (force_update & UP_RELOAD) {
  271. panel->is_panelized = 0;
  272. mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
  273. memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
  274. }
  275. /* If current_file == -1 (an invalid pointer) then preserve selection */
  276. if (current_file == UP_KEEPSEL) {
  277. free_pointer = 1;
  278. my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
  279. current_file = my_current_file;
  280. } else
  281. free_pointer = 0;
  282. if (panel->is_panelized)
  283. reload_panelized (panel);
  284. else
  285. panel_reload (panel);
  286. try_to_select (panel, current_file);
  287. panel->dirty = 1;
  288. if (free_pointer)
  289. g_free (my_current_file);
  290. }
  291. void
  292. panel_clean_dir (WPanel *panel)
  293. {
  294. int count = panel->count;
  295. panel->count = 0;
  296. panel->top_file = 0;
  297. panel->selected = 0;
  298. panel->marked = 0;
  299. panel->dirs_marked = 0;
  300. panel->total = 0;
  301. panel->searching = 0;
  302. panel->is_panelized = 0;
  303. panel->dirty = 1;
  304. clean_dir (&panel->dir, count);
  305. }
  306. static void
  307. update_one_panel (int which, int force_update, const char *current_file)
  308. {
  309. WPanel *panel;
  310. if (get_display_type (which) != view_listing)
  311. return;
  312. panel = (WPanel *) get_panel_widget (which);
  313. update_one_panel_widget (panel, force_update, current_file);
  314. }
  315. /* This routine reloads the directory in both panels. It tries to
  316. * select current_file in current_panel and other_file in other_panel.
  317. * If current_file == -1 then it automatically sets current_file and
  318. * other_file to the currently selected files in the panels.
  319. *
  320. * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
  321. * will not reload the other panel.
  322. */
  323. void
  324. update_panels (int force_update, const char *current_file)
  325. {
  326. int reload_other = !(force_update & UP_ONLY_CURRENT);
  327. WPanel *panel;
  328. update_one_panel (get_current_index (), force_update, current_file);
  329. if (reload_other)
  330. update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
  331. if (get_current_type () == view_listing)
  332. panel = (WPanel *) get_panel_widget (get_current_index ());
  333. else
  334. panel = (WPanel *) get_panel_widget (get_other_index ());
  335. mc_chdir (panel->cwd);
  336. }
  337. /* Save current stat of directories to avoid reloading the panels */
  338. /* when no modifications have taken place */
  339. void
  340. save_cwds_stat (void)
  341. {
  342. if (fast_reload) {
  343. mc_stat (current_panel->cwd, &(current_panel->dir_stat));
  344. if (get_other_type () == view_listing)
  345. mc_stat (other_panel->cwd, &(other_panel->dir_stat));
  346. }
  347. }
  348. #ifdef HAVE_SUBSHELL_SUPPORT
  349. void
  350. do_possible_cd (const char *new_dir)
  351. {
  352. if (!do_cd (new_dir, cd_exact))
  353. message (D_ERROR, _("Warning"),
  354. _(" The Commander can't change to the directory that \n"
  355. " the subshell claims you are in. Perhaps you have \n"
  356. " deleted your working directory, or given yourself \n"
  357. " extra access permissions with the \"su\" command? "));
  358. }
  359. void
  360. do_update_prompt (void)
  361. {
  362. if (update_prompt) {
  363. printf ("%s", subshell_prompt);
  364. fflush (stdout);
  365. update_prompt = 0;
  366. }
  367. }
  368. #endif /* HAVE_SUBSHELL_SUPPORT */
  369. void
  370. change_panel (void)
  371. {
  372. free_completions (cmdline);
  373. dlg_one_down (midnight_dlg);
  374. }
  375. /* Stop MC main dialog and the current dialog if it exists.
  376. * Needed to provide fast exit from MC viewer or editor on shell exit */
  377. static void
  378. stop_dialogs (void)
  379. {
  380. midnight_dlg->running = 0;
  381. if (current_dlg) {
  382. current_dlg->running = 0;
  383. }
  384. }
  385. static int
  386. quit_cmd_internal (int quiet)
  387. {
  388. int q = quit;
  389. if (quiet || !confirm_exit) {
  390. q = 1;
  391. } else {
  392. if (query_dialog
  393. (_(" The Midnight Commander "),
  394. _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
  395. 2, _("&Yes"), _("&No")) == 0)
  396. q = 1;
  397. }
  398. if (q) {
  399. #ifdef HAVE_SUBSHELL_SUPPORT
  400. if (!use_subshell)
  401. stop_dialogs ();
  402. else if ((q = exit_subshell ()))
  403. #endif
  404. stop_dialogs ();
  405. }
  406. if (q)
  407. quit |= 1;
  408. return quit;
  409. }
  410. static void
  411. quit_cmd (void)
  412. {
  413. quit_cmd_internal (0);
  414. }
  415. void
  416. quiet_quit_cmd (void)
  417. {
  418. print_last_revert = 1;
  419. quit_cmd_internal (1);
  420. }
  421. /*
  422. * Touch window and refresh window functions
  423. */
  424. /* This routine untouches the first line on both panels in order */
  425. /* to avoid the refreshing the menu bar */
  426. void
  427. repaint_screen (void)
  428. {
  429. do_refresh ();
  430. tty_refresh ();
  431. }
  432. /* Wrapper for do_subshell_chdir, check for availability of subshell */
  433. void
  434. subshell_chdir (const char *directory)
  435. {
  436. #ifdef HAVE_SUBSHELL_SUPPORT
  437. if (use_subshell) {
  438. if (vfs_current_is_local ())
  439. do_subshell_chdir (directory, 0, 1);
  440. }
  441. #endif /* HAVE_SUBSHELL_SUPPORT */
  442. }
  443. void
  444. directory_history_add (struct WPanel *panel, const char *dir)
  445. {
  446. char *tmp;
  447. tmp = g_strdup (dir);
  448. strip_password (tmp, 1);
  449. panel->dir_history = list_append_unique (panel->dir_history, tmp);
  450. }
  451. /*
  452. * If we moved to the parent directory move the selection pointer to
  453. * the old directory name; If we leave VFS dir, remove FS specificator.
  454. *
  455. * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
  456. */
  457. static const char *
  458. get_parent_dir_name (const char *cwd, const char *lwd)
  459. {
  460. const char *p;
  461. if (strlen (lwd) > strlen (cwd))
  462. if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
  463. ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
  464. cwd[1] == '\0'))) {
  465. return (p + 1);
  466. }
  467. return NULL;
  468. }
  469. /*
  470. * Changes the current directory of the panel.
  471. * Don't record change in the directory history.
  472. */
  473. static int
  474. _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
  475. {
  476. const char *directory;
  477. char *olddir;
  478. char temp[MC_MAXPATHLEN];
  479. char *translated_url;
  480. if (cd_type == cd_parse_command) {
  481. while (*new_dir == ' ')
  482. new_dir++;
  483. }
  484. olddir = g_strdup (panel->cwd);
  485. new_dir = translated_url = vfs_translate_url (new_dir);
  486. /* Convert *new_path to a suitable pathname, handle ~user */
  487. if (cd_type == cd_parse_command) {
  488. if (!strcmp (new_dir, "-")) {
  489. strcpy (temp, panel->lwd);
  490. new_dir = temp;
  491. }
  492. }
  493. directory = *new_dir ? new_dir : home_dir;
  494. if (mc_chdir (directory) == -1) {
  495. strcpy (panel->cwd, olddir);
  496. g_free (olddir);
  497. g_free (translated_url);
  498. return 0;
  499. }
  500. g_free (translated_url);
  501. /* Success: save previous directory, shutdown status of previous dir */
  502. strcpy (panel->lwd, olddir);
  503. free_completions (cmdline);
  504. mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
  505. vfs_release_path (olddir);
  506. subshell_chdir (panel->cwd);
  507. /* Reload current panel */
  508. panel_clean_dir (panel);
  509. panel->count =
  510. do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
  511. panel->reverse, panel->case_sensitive,
  512. panel->exec_first, panel->filter);
  513. try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
  514. load_hint (0);
  515. panel->dirty = 1;
  516. update_xterm_title_path ();
  517. g_free (olddir);
  518. return 1;
  519. }
  520. /*
  521. * Changes the current directory of the panel.
  522. * Record change in the directory history.
  523. */
  524. int
  525. do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
  526. {
  527. int r;
  528. r = _do_panel_cd (panel, new_dir, cd_type);
  529. if (r)
  530. directory_history_add (panel, panel->cwd);
  531. return r;
  532. }
  533. int
  534. do_cd (const char *new_dir, enum cd_enum exact)
  535. {
  536. return (do_panel_cd (current_panel, new_dir, exact));
  537. }
  538. void
  539. directory_history_next (WPanel *panel)
  540. {
  541. GList *nextdir;
  542. nextdir = g_list_next (panel->dir_history);
  543. if (!nextdir)
  544. return;
  545. if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
  546. panel->dir_history = nextdir;
  547. }
  548. void
  549. directory_history_prev (WPanel *panel)
  550. {
  551. GList *prevdir;
  552. prevdir = g_list_previous (panel->dir_history);
  553. if (!prevdir)
  554. return;
  555. if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
  556. panel->dir_history = prevdir;
  557. }
  558. void
  559. directory_history_list (WPanel *panel)
  560. {
  561. char *s;
  562. if (!panel->dir_history)
  563. return;
  564. s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
  565. if (!s)
  566. return;
  567. if (_do_panel_cd (panel, s, cd_exact))
  568. directory_history_add (panel, panel->cwd);
  569. else
  570. message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
  571. g_free (s);
  572. }
  573. #ifdef HAVE_SUBSHELL_SUPPORT
  574. int
  575. load_prompt (int fd, void *unused)
  576. {
  577. (void) fd;
  578. (void) unused;
  579. if (!read_subshell_prompt ())
  580. return 0;
  581. /* Don't actually change the prompt if it's invisible */
  582. if (current_dlg == midnight_dlg && command_prompt) {
  583. char *tmp_prompt;
  584. int prompt_len;
  585. tmp_prompt = strip_ctrl_codes (subshell_prompt);
  586. prompt_len = str_term_width1 (tmp_prompt);
  587. /* Check for prompts too big */
  588. if (COLS > 8 && prompt_len > COLS - 8) {
  589. tmp_prompt[COLS - 8] = '\0';
  590. prompt_len = COLS - 8;
  591. }
  592. prompt = tmp_prompt;
  593. label_set_text (the_prompt, prompt);
  594. winput_set_origin ((WInput *) cmdline, prompt_len,
  595. COLS - prompt_len);
  596. /* since the prompt has changed, and we are called from one of the
  597. * get_event channels, the prompt updating does not take place
  598. * automatically: force a cursor update and a screen refresh
  599. */
  600. update_cursor (midnight_dlg);
  601. tty_refresh ();
  602. }
  603. update_prompt = 1;
  604. return 0;
  605. }
  606. #endif /* HAVE_SUBSHELL_SUPPORT */
  607. /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
  608. int
  609. maybe_cd (int move_up_dir)
  610. {
  611. if (navigate_with_arrows) {
  612. if (!cmdline->buffer[0]) {
  613. if (move_up_dir) {
  614. do_cd ("..", cd_exact);
  615. return 1;
  616. }
  617. if (S_ISDIR (selection (current_panel)->st.st_mode)
  618. || link_isdir (selection (current_panel))) {
  619. do_cd (selection (current_panel)->fname, cd_exact);
  620. return 1;
  621. }
  622. }
  623. }
  624. return 0;
  625. }
  626. static void
  627. sort_cmd (void)
  628. {
  629. WPanel *p;
  630. sortfn *sort_order;
  631. if (!SELECTED_IS_PANEL)
  632. return;
  633. p = MENU_PANEL;
  634. sort_order = sort_box (p->sort_type, &p->reverse,
  635. &p->case_sensitive,
  636. &p->exec_first);
  637. panel_set_sort_order (p, sort_order);
  638. }
  639. static void
  640. treebox_cmd (void)
  641. {
  642. char *sel_dir;
  643. sel_dir = tree_box (selection (current_panel)->fname);
  644. if (sel_dir) {
  645. do_cd (sel_dir, cd_exact);
  646. g_free (sel_dir);
  647. }
  648. }
  649. #ifdef LISTMODE_EDITOR
  650. static void
  651. listmode_cmd (void)
  652. {
  653. char *newmode;
  654. if (get_current_type () != view_listing)
  655. return;
  656. newmode = listmode_edit (current_panel->user_format);
  657. if (!newmode)
  658. return;
  659. g_free (current_panel->user_format);
  660. current_panel->list_type = list_user;
  661. current_panel->user_format = newmode;
  662. set_panel_formats (current_panel);
  663. do_refresh ();
  664. }
  665. #endif /* LISTMODE_EDITOR */
  666. /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
  667. static menu_entry LeftMenu[] = {
  668. {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
  669. {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
  670. {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
  671. {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
  672. {' ', "", NULL_HOTKEY, 0},
  673. {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
  674. {' ', "", NULL_HOTKEY, 0},
  675. {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
  676. #ifdef HAVE_CHARSET
  677. {' ', "",NULL_HOTKEY, 0},
  678. {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
  679. #endif
  680. #ifdef USE_NETCODE
  681. {' ', "", NULL_HOTKEY, 0},
  682. #ifdef ENABLE_VFS_MCFS
  683. {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
  684. #endif
  685. {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
  686. {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
  687. #ifdef WITH_SMBFS
  688. {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
  689. #endif
  690. #endif
  691. {' ', "", NULL_HOTKEY, 0},
  692. {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
  693. };
  694. static menu_entry RightMenu[] = {
  695. {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
  696. {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
  697. {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
  698. {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
  699. {' ', "", NULL_HOTKEY, 0},
  700. {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
  701. {' ', "", NULL_HOTKEY, 0},
  702. {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
  703. #ifdef HAVE_CHARSET
  704. {' ', "",NULL_HOTKEY, 0},
  705. {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
  706. #endif
  707. #ifdef USE_NETCODE
  708. {' ', "", NULL_HOTKEY, 0},
  709. #ifdef ENABLE_VFS_MCFS
  710. {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
  711. #endif
  712. {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
  713. {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
  714. #ifdef WITH_SMBFS
  715. {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
  716. #endif
  717. #endif
  718. {' ', "", NULL_HOTKEY, 0},
  719. {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
  720. };
  721. static menu_entry FileMenu[] = {
  722. {' ', N_("&View F3"), NULL_HOTKEY, view_cmd},
  723. {' ', N_("Vie&w file... "), NULL_HOTKEY, view_file_cmd},
  724. {' ', N_("&Filtered view M-!"), NULL_HOTKEY, filtered_view_cmd},
  725. {' ', N_("&Edit F4"), NULL_HOTKEY, edit_cmd},
  726. {' ', N_("&Copy F5"), NULL_HOTKEY, copy_cmd},
  727. {' ', N_("c&Hmod C-x c"), NULL_HOTKEY, chmod_cmd},
  728. {' ', N_("&Link C-x l"), NULL_HOTKEY, link_cmd},
  729. {' ', N_("&SymLink C-x s"), NULL_HOTKEY, symlink_cmd},
  730. {' ', N_("edit s&Ymlink C-x C-s"), NULL_HOTKEY, edit_symlink_cmd},
  731. {' ', N_("ch&Own C-x o"), NULL_HOTKEY, chown_cmd},
  732. {' ', N_("&Advanced chown "), NULL_HOTKEY, chown_advanced_cmd},
  733. {' ', N_("&Rename/Move F6"), NULL_HOTKEY, ren_cmd},
  734. {' ', N_("&Mkdir F7"), NULL_HOTKEY, mkdir_cmd},
  735. {' ', N_("&Delete F8"), NULL_HOTKEY, delete_cmd},
  736. {' ', N_("&Quick cd M-c"), NULL_HOTKEY, quick_cd_cmd},
  737. {' ', "", NULL_HOTKEY, 0},
  738. {' ', N_("select &Group M-+"), NULL_HOTKEY, select_cmd},
  739. {' ', N_("u&Nselect group M-\\"), NULL_HOTKEY, unselect_cmd},
  740. {' ', N_("reverse selec&Tion M-*"), NULL_HOTKEY, reverse_selection_cmd},
  741. {' ', "", NULL_HOTKEY, 0},
  742. {' ', N_("e&Xit F10"), NULL_HOTKEY, quit_cmd}
  743. };
  744. static menu_entry CmdMenu[] = {
  745. {' ', N_("&User menu F2"), NULL_HOTKEY, user_file_menu_cmd},
  746. /* I know, I'm lazy, but the tree widget when it's not running
  747. * as a panel still has some problems, I have not yet finished
  748. * the WTree widget port, sorry.
  749. */
  750. {' ', N_("&Directory tree"), NULL_HOTKEY, treebox_cmd},
  751. {' ', N_("&Find file M-?"), NULL_HOTKEY, find_cmd},
  752. {' ', N_("s&Wap panels C-u"), NULL_HOTKEY, swap_cmd},
  753. {' ', N_("switch &Panels on/off C-o"), NULL_HOTKEY, view_other_cmd},
  754. {' ', N_("&Compare directories C-x d"), NULL_HOTKEY, compare_dirs_cmd},
  755. {' ', N_("e&Xternal panelize C-x !"), NULL_HOTKEY, external_panelize},
  756. {' ', N_("show directory s&Izes"), NULL_HOTKEY, dirsizes_cmd},
  757. {' ', "", NULL_HOTKEY, 0},
  758. {' ', N_("Command &history M-h"), NULL_HOTKEY, history_cmd},
  759. {' ', N_("di&Rectory hotlist C-\\"), NULL_HOTKEY, quick_chdir_cmd},
  760. #ifdef USE_VFS
  761. {' ', N_("&Active VFS list C-x a"), NULL_HOTKEY, reselect_vfs},
  762. #endif
  763. #ifdef WITH_BACKGROUND
  764. {' ', N_("&Background jobs C-x j"), NULL_HOTKEY, jobs_cmd},
  765. #endif
  766. {' ', "", NULL_HOTKEY, 0},
  767. #ifdef USE_EXT2FSLIB
  768. {' ', N_("&Undelete files (ext2fs only)"), NULL_HOTKEY, undelete_cmd},
  769. #endif
  770. #ifdef LISTMODE_EDITOR
  771. {' ', N_("&Listing format edit"), NULL_HOTKEY, listmode_cmd},
  772. #endif
  773. #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
  774. {' ', "", NULL_HOTKEY, 0},
  775. #endif
  776. {' ', N_("Edit &extension file"), NULL_HOTKEY, ext_cmd},
  777. {' ', N_("Edit &menu file"), NULL_HOTKEY, edit_mc_menu_cmd}
  778. };
  779. /* Must keep in sync with the constants in menu_cmd */
  780. static menu_entry OptMenu[] = {
  781. {' ', N_("&Configuration..."), NULL_HOTKEY, configure_box},
  782. {' ', N_("&Layout..."), NULL_HOTKEY, layout_cmd},
  783. {' ', N_("c&Onfirmation..."), NULL_HOTKEY, confirm_box},
  784. {' ', N_("&Display bits..."), NULL_HOTKEY, display_bits_box},
  785. {' ', N_("learn &Keys..."), NULL_HOTKEY, learn_keys},
  786. #ifdef USE_VFS
  787. {' ', N_("&Virtual FS..."), NULL_HOTKEY, configure_vfs},
  788. #endif /* !USE_VFS */
  789. {' ', "", NULL_HOTKEY, 0},
  790. {' ', N_("&Save setup"), NULL_HOTKEY, save_setup_cmd}
  791. };
  792. #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
  793. static Menu *MenuBar[5];
  794. void
  795. init_menu (void)
  796. {
  797. MenuBar[0] =
  798. create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
  799. LeftMenu, menu_entries (LeftMenu),
  800. "[Left and Right Menus]");
  801. MenuBar[1] =
  802. create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
  803. "[File Menu]");
  804. MenuBar[2] =
  805. create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
  806. "[Command Menu]");
  807. MenuBar[3] =
  808. create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
  809. "[Options Menu]");
  810. MenuBar[4] =
  811. create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
  812. RightMenu, menu_entries (RightMenu),
  813. "[Left and Right Menus]");
  814. }
  815. void
  816. done_menu (void)
  817. {
  818. int i;
  819. for (i = 0; i < 5; i++) {
  820. destroy_menu (MenuBar[i]);
  821. }
  822. }
  823. static void
  824. menu_last_selected_cmd (void)
  825. {
  826. the_menubar->active = 1;
  827. the_menubar->dropped = drop_menus;
  828. the_menubar->previous_widget = midnight_dlg->current->dlg_id;
  829. dlg_select_widget (the_menubar);
  830. }
  831. static void
  832. menu_cmd (void)
  833. {
  834. if (the_menubar->active)
  835. return;
  836. if ((get_current_index () == 0) ^ (!current_panel->active))
  837. the_menubar->selected = 0;
  838. else
  839. the_menubar->selected = 4;
  840. menu_last_selected_cmd ();
  841. }
  842. /* Flag toggling functions */
  843. void
  844. toggle_fast_reload (void)
  845. {
  846. fast_reload = !fast_reload;
  847. if (fast_reload_w == 0 && fast_reload) {
  848. message (D_NORMAL, _(" Information "),
  849. _
  850. (" Using the fast reload option may not reflect the exact \n"
  851. " directory contents. In this case you'll need to do a \n"
  852. " manual reload of the directory. See the man page for \n"
  853. " the details. "));
  854. fast_reload_w = 1;
  855. }
  856. }
  857. void
  858. toggle_mix_all_files (void)
  859. {
  860. mix_all_files = !mix_all_files;
  861. update_panels (UP_RELOAD, UP_KEEPSEL);
  862. }
  863. void
  864. toggle_show_backup (void)
  865. {
  866. show_backups = !show_backups;
  867. update_panels (UP_RELOAD, UP_KEEPSEL);
  868. }
  869. void
  870. toggle_show_hidden (void)
  871. {
  872. show_dot_files = !show_dot_files;
  873. update_panels (UP_RELOAD, UP_KEEPSEL);
  874. }
  875. /*
  876. * Just a hack for allowing url-like pathnames to be accepted from the
  877. * command line.
  878. */
  879. static void
  880. translated_mc_chdir (char *dir)
  881. {
  882. char *newdir;
  883. newdir = vfs_translate_url (dir);
  884. mc_chdir (newdir);
  885. g_free (newdir);
  886. }
  887. static void
  888. create_panels (void)
  889. {
  890. int current_index;
  891. int other_index;
  892. int current_mode;
  893. int other_mode;
  894. char original_dir[1024];
  895. original_dir[0] = 0;
  896. if (boot_current_is_left) {
  897. current_index = 0;
  898. other_index = 1;
  899. current_mode = startup_left_mode;
  900. other_mode = startup_right_mode;
  901. } else {
  902. current_index = 1;
  903. other_index = 0;
  904. current_mode = startup_right_mode;
  905. other_mode = startup_left_mode;
  906. }
  907. /* Creates the left panel */
  908. if (this_dir) {
  909. if (other_dir) {
  910. /* Ok, user has specified two dirs, save the original one,
  911. * since we may not be able to chdir to the proper
  912. * second directory later
  913. */
  914. mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
  915. }
  916. translated_mc_chdir (this_dir);
  917. }
  918. set_display_type (current_index, current_mode);
  919. /* The other panel */
  920. if (other_dir) {
  921. if (original_dir[0])
  922. translated_mc_chdir (original_dir);
  923. translated_mc_chdir (other_dir);
  924. }
  925. set_display_type (other_index, other_mode);
  926. if (startup_left_mode == view_listing) {
  927. current_panel = left_panel;
  928. } else {
  929. if (right_panel)
  930. current_panel = right_panel;
  931. else
  932. current_panel = left_panel;
  933. }
  934. /* Create the nice widgets */
  935. cmdline = command_new (0, 0, 0);
  936. the_prompt = label_new (0, 0, prompt);
  937. the_prompt->transparent = 1;
  938. the_bar = buttonbar_new (keybar_visible);
  939. the_hint = label_new (0, 0, 0);
  940. the_hint->transparent = 1;
  941. the_hint->auto_adjust_cols = 0;
  942. the_hint->widget.cols = COLS;
  943. the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
  944. }
  945. static void
  946. copy_current_pathname (void)
  947. {
  948. char *cwd_path;
  949. if (!command_prompt)
  950. return;
  951. cwd_path = remove_encoding_from_path (current_panel->cwd);
  952. command_insert (cmdline, cwd_path, 0);
  953. if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
  954. command_insert (cmdline, PATH_SEP_STR, 0);
  955. g_free (cwd_path);
  956. }
  957. static void
  958. copy_other_pathname (void)
  959. {
  960. char *cwd_path;
  961. if (get_other_type () != view_listing)
  962. return;
  963. if (!command_prompt)
  964. return;
  965. cwd_path = remove_encoding_from_path (other_panel->cwd);
  966. command_insert (cmdline, cwd_path, 0);
  967. if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
  968. command_insert (cmdline, PATH_SEP_STR, 0);
  969. g_free (cwd_path);
  970. }
  971. static void
  972. copy_readlink (WPanel *panel)
  973. {
  974. if (!command_prompt)
  975. return;
  976. if (S_ISLNK (selection (panel)->st.st_mode)) {
  977. char buffer[MC_MAXPATHLEN];
  978. char *p =
  979. concat_dir_and_file (panel->cwd, selection (panel)->fname);
  980. int i;
  981. i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
  982. g_free (p);
  983. if (i > 0) {
  984. buffer[i] = 0;
  985. command_insert (cmdline, buffer, 1);
  986. }
  987. }
  988. }
  989. static void
  990. copy_current_readlink (void)
  991. {
  992. copy_readlink (current_panel);
  993. }
  994. static void
  995. copy_other_readlink (void)
  996. {
  997. if (get_other_type () != view_listing)
  998. return;
  999. copy_readlink (other_panel);
  1000. }
  1001. /* Insert the selected file name into the input line */
  1002. static void
  1003. copy_prog_name (void)
  1004. {
  1005. char *tmp;
  1006. if (!command_prompt)
  1007. return;
  1008. if (get_current_type () == view_tree) {
  1009. WTree *tree = (WTree *) get_panel_widget (get_current_index ());
  1010. tmp = tree_selected_name (tree);
  1011. } else
  1012. tmp = selection (current_panel)->fname;
  1013. command_insert (cmdline, tmp, 1);
  1014. }
  1015. static void
  1016. copy_tagged (WPanel *panel)
  1017. {
  1018. int i;
  1019. if (!command_prompt)
  1020. return;
  1021. input_disable_update (cmdline);
  1022. if (panel->marked) {
  1023. for (i = 0; i < panel->count; i++) {
  1024. if (panel->dir.list[i].f.marked)
  1025. command_insert (cmdline, panel->dir.list[i].fname, 1);
  1026. }
  1027. } else {
  1028. command_insert (cmdline, panel->dir.list[panel->selected].fname,
  1029. 1);
  1030. }
  1031. input_enable_update (cmdline);
  1032. }
  1033. static void
  1034. copy_current_tagged (void)
  1035. {
  1036. copy_tagged (current_panel);
  1037. }
  1038. static void
  1039. copy_other_tagged (void)
  1040. {
  1041. if (get_other_type () != view_listing)
  1042. return;
  1043. copy_tagged (other_panel);
  1044. }
  1045. static void
  1046. init_labels (void)
  1047. {
  1048. buttonbar_set_label (midnight_dlg, 1, _("Help"), help_cmd);
  1049. buttonbar_set_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
  1050. buttonbar_set_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
  1051. buttonbar_set_label (midnight_dlg, 10, _("Quit"), quit_cmd);
  1052. }
  1053. static const key_map ctl_x_map[] = {
  1054. {XCTRL ('c'), quit_cmd},
  1055. {'d', compare_dirs_cmd},
  1056. #ifdef USE_VFS
  1057. {'a', reselect_vfs},
  1058. #endif /* USE_VFS */
  1059. {'p', copy_current_pathname},
  1060. {XCTRL ('p'), copy_other_pathname},
  1061. {'t', copy_current_tagged},
  1062. {XCTRL ('t'), copy_other_tagged},
  1063. {'c', chmod_cmd},
  1064. {'o', chown_cmd},
  1065. {'r', copy_current_readlink},
  1066. {XCTRL ('r'), copy_other_readlink},
  1067. {'l', link_cmd},
  1068. {'s', symlink_cmd},
  1069. {XCTRL ('s'), edit_symlink_cmd},
  1070. {'i', info_cmd_no_menu},
  1071. {'q', quick_cmd_no_menu},
  1072. {'h', add2hotlist_cmd},
  1073. {'!', external_panelize},
  1074. #ifdef WITH_BACKGROUND
  1075. {'j', jobs_cmd},
  1076. #endif /* WITH_BACKGROUND */
  1077. {0, 0}
  1078. };
  1079. static int ctl_x_map_enabled = 0;
  1080. static void
  1081. ctl_x_cmd (void)
  1082. {
  1083. ctl_x_map_enabled = 1;
  1084. }
  1085. static void
  1086. nothing (void)
  1087. {
  1088. }
  1089. static const key_map default_map[] = {
  1090. {KEY_F (19), menu_last_selected_cmd},
  1091. {KEY_F (20), quiet_quit_cmd},
  1092. {XCTRL ('@'), smart_dirsize_cmd},
  1093. /* Copy useful information to the command line */
  1094. {ALT ('a'), copy_current_pathname},
  1095. {ALT ('A'), copy_other_pathname},
  1096. {ALT ('c'), quick_cd_cmd},
  1097. /* To access the directory hotlist */
  1098. {XCTRL ('\\'), quick_chdir_cmd},
  1099. /* Suspend */
  1100. {XCTRL ('z'), suspend_cmd},
  1101. /* The filtered view command */
  1102. {ALT ('!'), filtered_view_cmd},
  1103. /* Find file */
  1104. {ALT ('?'), find_cmd},
  1105. /* Panel refresh */
  1106. {XCTRL ('r'), reread_cmd},
  1107. /* Toggle listing between long, user defined and full formats */
  1108. {ALT ('t'), toggle_listing_cmd},
  1109. /* Swap panels */
  1110. {XCTRL ('u'), swap_cmd},
  1111. /* View output */
  1112. {XCTRL ('o'), view_other_cmd},
  1113. /* Control-X keybindings */
  1114. {XCTRL ('x'), ctl_x_cmd},
  1115. /* Show/hide hidden files */
  1116. {ALT ('.'), toggle_show_hidden},
  1117. /* Trap dlg's exit commands */
  1118. {ESC_CHAR, nothing},
  1119. {XCTRL ('c'), nothing},
  1120. {XCTRL ('g'), nothing},
  1121. {0, 0},
  1122. };
  1123. static void
  1124. setup_sigwinch (void)
  1125. {
  1126. #if (defined(HAVE_SLANG) || (NCURSES_VERSION_MAJOR >= 4)) && defined(SIGWINCH)
  1127. struct sigaction act, oact;
  1128. act.sa_handler = flag_winch;
  1129. sigemptyset (&act.sa_mask);
  1130. act.sa_flags = 0;
  1131. #ifdef SA_RESTART
  1132. act.sa_flags |= SA_RESTART;
  1133. #endif
  1134. sigaction (SIGWINCH, &act, &oact);
  1135. #endif
  1136. }
  1137. static void
  1138. setup_pre (void)
  1139. {
  1140. /* Call all the inits */
  1141. #ifdef HAVE_CHARSET
  1142. /*
  1143. * Don't restrict the output on the screen manager level,
  1144. * the translation tables take care of it.
  1145. */
  1146. #define full_eight_bits (1)
  1147. #define eight_bit_clean (1)
  1148. #endif /* !HAVE_CHARSET */
  1149. #ifndef HAVE_SLANG
  1150. meta (stdscr, eight_bit_clean);
  1151. #else
  1152. SLsmg_Display_Eight_Bit = full_eight_bits ? 128 : 160;
  1153. #endif
  1154. }
  1155. static void
  1156. init_xterm_support (void)
  1157. {
  1158. const char *termvalue;
  1159. termvalue = getenv ("TERM");
  1160. if (!termvalue || !(*termvalue)) {
  1161. fputs (_("The TERM environment variable is unset!\n"), stderr);
  1162. exit (1);
  1163. }
  1164. /* Check mouse capabilities */
  1165. xmouse_seq = tty_tgetstr ("Km");
  1166. if (strcmp (termvalue, "cygwin") == 0) {
  1167. force_xterm = 1;
  1168. use_mouse_p = MOUSE_DISABLED;
  1169. }
  1170. if (force_xterm || strncmp (termvalue, "xterm", 5) == 0
  1171. || strncmp (termvalue, "rxvt", 4) == 0
  1172. || strcmp (termvalue, "Eterm") == 0
  1173. || strcmp (termvalue, "dtterm") == 0) {
  1174. xterm_flag = 1;
  1175. /* Default to the standard xterm sequence */
  1176. if (!xmouse_seq) {
  1177. xmouse_seq = ESC_STR "[M";
  1178. }
  1179. /* Enable mouse unless explicitly disabled by --nomouse */
  1180. if (use_mouse_p != MOUSE_DISABLED) {
  1181. const char *color_term = getenv ("COLORTERM");
  1182. if (strncmp (termvalue, "rxvt", 4) == 0 ||
  1183. (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
  1184. strcmp (termvalue, "Eterm") == 0) {
  1185. use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
  1186. } else {
  1187. use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
  1188. }
  1189. }
  1190. }
  1191. }
  1192. static void
  1193. setup_mc (void)
  1194. {
  1195. setup_pre ();
  1196. init_menu ();
  1197. create_panels ();
  1198. setup_panels ();
  1199. #ifdef HAVE_SUBSHELL_SUPPORT
  1200. if (use_subshell)
  1201. add_select_channel (subshell_pty, load_prompt, 0);
  1202. #endif /* !HAVE_SUBSHELL_SUPPORT */
  1203. setup_sigwinch ();
  1204. if (baudrate () < 9600 || slow_terminal) {
  1205. verbose = 0;
  1206. }
  1207. init_mouse ();
  1208. }
  1209. static void
  1210. setup_dummy_mc ()
  1211. {
  1212. char d[MC_MAXPATHLEN];
  1213. mc_get_current_wd (d, MC_MAXPATHLEN);
  1214. setup_mc ();
  1215. mc_chdir (d);
  1216. }
  1217. static void check_codeset()
  1218. {
  1219. const char *_system_codepage = NULL;
  1220. #ifdef HAVE_CHARSET
  1221. const char *_source_codepage = NULL;
  1222. const char *_display_codepage = NULL;
  1223. int profile_changed = 0;
  1224. #define CONFY 16
  1225. #define CONFX 54
  1226. if ( !skip_check_codeset ) {
  1227. QuickWidget ecs_widgets [] = {
  1228. { quick_button, 4, 6, 13, CONFY, N_("&Skip"),
  1229. 0, B_EXIT, 0, 0, NULL , NULL, NULL},
  1230. { quick_button, 1, 11, 13, CONFY, N_("&Fix it"),
  1231. 0, B_ENTER, 0, 0, NULL , NULL, NULL},
  1232. { quick_checkbox, 1, 13, 11, CONFY, N_("don't ask again"),
  1233. 11, 0, &skip_check_codeset, NULL, NULL , NULL, NULL},
  1234. { quick_label, 2, 30, 3, CONFY, N_("Chosen display charset (Settings->Display bits)\n"
  1235. "or source codeset (in mcedit ctrl-t) \n"
  1236. "does not match one set via locale. \n"
  1237. "Set correct codeset manually or press <<Fix it>> \n"
  1238. "to set locale default.\n\n"
  1239. "Or set \'don't ask again\' and press <<Skip>>"),
  1240. 0, 0, 0, 0, NULL , NULL, NULL},
  1241. NULL_QuickWidget
  1242. };
  1243. QuickDialog ecs =
  1244. { CONFX, CONFY, -1, -1, N_(" Confirmation "), "[Confirmation]",
  1245. ecs_widgets, 0
  1246. };
  1247. _system_codepage = str_detect_termencoding();
  1248. _source_codepage = get_codepage_id (source_codepage);
  1249. _display_codepage = get_codepage_id (display_codepage);
  1250. if ( (strcmp (_system_codepage, _display_codepage)) ||
  1251. (strcmp (_system_codepage, _source_codepage)) ) {
  1252. if (quick_dialog (&ecs) == B_ENTER){
  1253. display_codepage = get_codepage_index (_system_codepage);
  1254. cp_display = get_codepage_id (display_codepage);
  1255. if ( !strcmp (cp_display, _system_codepage)) {
  1256. mc_config_set_string(mc_main_config, "Misc", "display_codepage", cp_display);
  1257. mc_config_set_string(mc_main_config, "Misc", "source_codepage", cp_display);
  1258. display_codepage = get_codepage_index ( cp_display );
  1259. utf8_display = str_isutf8 (_system_codepage);
  1260. source_codepage = display_codepage;
  1261. cp_source = cp_display;
  1262. profile_changed = 1;
  1263. }
  1264. } else {
  1265. if ( skip_check_codeset ) {
  1266. mc_config_set_int(mc_main_config, "Midnight-Commander", "skip_check_codeset", 1);
  1267. profile_changed = 1;
  1268. }
  1269. }
  1270. }
  1271. if ( profile_changed )
  1272. save_configure ();
  1273. }
  1274. #else /* HAVE_CHARSET */
  1275. _system_codepage = str_detect_termencoding();
  1276. utf8_display = str_isutf8 (_system_codepage);
  1277. #endif /* HAVE_CHARSET */
  1278. }
  1279. static void
  1280. done_mc (void)
  1281. {
  1282. disable_mouse ();
  1283. done_menu ();
  1284. /* Setup shutdown
  1285. *
  1286. * We sync the profiles since the hotlist may have changed, while
  1287. * we only change the setup data if we have the auto save feature set
  1288. */
  1289. if (auto_save_setup)
  1290. save_setup (); /* does also call save_hotlist */
  1291. else {
  1292. save_hotlist ();
  1293. save_panel_types ();
  1294. }
  1295. done_screen ();
  1296. vfs_add_current_stamps ();
  1297. }
  1298. /* This should be called after destroy_dlg since panel widgets
  1299. * save their state on the profiles
  1300. */
  1301. static void
  1302. done_mc_profile (void)
  1303. {
  1304. done_setup ();
  1305. }
  1306. static cb_ret_t
  1307. midnight_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
  1308. {
  1309. int i;
  1310. switch (msg) {
  1311. case DLG_IDLE:
  1312. /* We only need the first idle event */
  1313. set_idle_proc (h, 0);
  1314. if (auto_menu) {
  1315. user_file_menu_cmd ();
  1316. }
  1317. return MSG_HANDLED;
  1318. case DLG_KEY:
  1319. if (ctl_x_map_enabled) {
  1320. ctl_x_map_enabled = 0;
  1321. for (i = 0; ctl_x_map[i].key_code; i++)
  1322. if (parm == ctl_x_map[i].key_code) {
  1323. (*ctl_x_map[i].fn) ();
  1324. return MSG_HANDLED;
  1325. }
  1326. }
  1327. /* FIXME: should handle all menu shortcuts before this point */
  1328. if (the_menubar->active)
  1329. return MSG_NOT_HANDLED;
  1330. if (parm == KEY_F (10)) {
  1331. quit_cmd ();
  1332. return MSG_HANDLED;
  1333. }
  1334. if (parm == '\t')
  1335. free_completions (cmdline);
  1336. if (parm == '\n') {
  1337. for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
  1338. cmdline->buffer[i] == '\t'); i++);
  1339. if (cmdline->buffer[i]) {
  1340. send_message ((Widget *) cmdline, WIDGET_KEY, parm);
  1341. return MSG_HANDLED;
  1342. }
  1343. stuff (cmdline, "", 0);
  1344. cmdline->point = 0;
  1345. }
  1346. /* Ctrl-Enter and Alt-Enter */
  1347. if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
  1348. && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
  1349. copy_prog_name ();
  1350. return MSG_HANDLED;
  1351. }
  1352. /* Ctrl-Shift-Enter */
  1353. if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
  1354. copy_current_pathname ();
  1355. copy_prog_name ();
  1356. return MSG_HANDLED;
  1357. }
  1358. if ((!alternate_plus_minus || !(console_flag || xterm_flag))
  1359. && !quote && !current_panel->searching) {
  1360. if (!only_leading_plus_minus) {
  1361. /* Special treatement, since the input line will eat them */
  1362. if (parm == '+') {
  1363. select_cmd ();
  1364. return MSG_HANDLED;
  1365. }
  1366. if (parm == '\\' || parm == '-') {
  1367. unselect_cmd ();
  1368. return MSG_HANDLED;
  1369. }
  1370. if (parm == '*') {
  1371. reverse_selection_cmd ();
  1372. return MSG_HANDLED;
  1373. }
  1374. } else if (!command_prompt || !cmdline->buffer[0]) {
  1375. /* Special treatement '+', '-', '\', '*' only when this is
  1376. * first char on input line
  1377. */
  1378. if (parm == '+') {
  1379. select_cmd ();
  1380. return MSG_HANDLED;
  1381. }
  1382. if (parm == '\\' || parm == '-') {
  1383. unselect_cmd ();
  1384. return MSG_HANDLED;
  1385. }
  1386. if (parm == '*') {
  1387. reverse_selection_cmd ();
  1388. return MSG_HANDLED;
  1389. }
  1390. }
  1391. }
  1392. return MSG_NOT_HANDLED;
  1393. case DLG_HOTKEY_HANDLED:
  1394. if ((get_current_type () == view_listing) && current_panel->searching) {
  1395. current_panel->searching = 0;
  1396. current_panel->dirty = 1;
  1397. }
  1398. return MSG_HANDLED;
  1399. case DLG_UNHANDLED_KEY:
  1400. if (command_prompt) {
  1401. int v;
  1402. v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
  1403. if (v)
  1404. return v;
  1405. }
  1406. if (ctl_x_map_enabled) {
  1407. ctl_x_map_enabled = 0;
  1408. for (i = 0; ctl_x_map[i].key_code; i++)
  1409. if (parm == ctl_x_map[i].key_code) {
  1410. (*ctl_x_map[i].fn) ();
  1411. return MSG_HANDLED;
  1412. }
  1413. } else {
  1414. for (i = 0; default_map[i].key_code; i++) {
  1415. if (parm == default_map[i].key_code) {
  1416. (*default_map[i].fn) ();
  1417. return MSG_HANDLED;
  1418. }
  1419. }
  1420. }
  1421. return MSG_NOT_HANDLED;
  1422. case DLG_DRAW:
  1423. /* We handle the special case of the output lines */
  1424. if (console_flag && output_lines)
  1425. show_console_contents (output_start_y,
  1426. LINES - output_lines - keybar_visible -
  1427. 1, LINES - keybar_visible - 1);
  1428. return MSG_HANDLED;
  1429. case DLG_POST_KEY:
  1430. if (!the_menubar->active)
  1431. update_dirty_panels ();
  1432. return MSG_HANDLED;
  1433. default:
  1434. return default_dlg_callback (h, msg, parm);
  1435. }
  1436. }
  1437. /* Show current directory in the xterm title */
  1438. void
  1439. update_xterm_title_path (void)
  1440. {
  1441. const char *path;
  1442. char host[BUF_TINY];
  1443. char *p;
  1444. struct passwd *pw = NULL;
  1445. char *login = NULL;
  1446. int res = 0;
  1447. if (xterm_flag && xterm_title) {
  1448. path = strip_home_and_password (current_panel->cwd);
  1449. res = gethostname(host, sizeof (host));
  1450. if ( res ) { /* On success, res = 0 */
  1451. host[0] = '\0';
  1452. } else {
  1453. host[sizeof (host) - 1] = '\0';
  1454. }
  1455. pw = getpwuid(getuid());
  1456. if ( pw ) {
  1457. login = g_strdup_printf ("%s@%s", pw->pw_name, host);
  1458. } else {
  1459. login = g_strdup (host);
  1460. }
  1461. p = g_strdup_printf ("mc [%s]:%s", login, path);
  1462. fprintf (stdout, "\33]0;%s\7", str_term_form (p));
  1463. g_free (login);
  1464. g_free (p);
  1465. if (!alternate_plus_minus)
  1466. numeric_keypad_mode ();
  1467. fflush (stdout);
  1468. }
  1469. }
  1470. /*
  1471. * Load new hint and display it.
  1472. * IF force is not 0, ignore the timeout.
  1473. */
  1474. void
  1475. load_hint (int force)
  1476. {
  1477. char *hint;
  1478. if (!the_hint->widget.parent)
  1479. return;
  1480. if (!message_visible) {
  1481. label_set_text (the_hint, 0);
  1482. return;
  1483. }
  1484. if ((hint = get_random_hint (force))) {
  1485. if (*hint)
  1486. set_hintbar (hint);
  1487. g_free (hint);
  1488. } else {
  1489. char text[BUF_SMALL];
  1490. g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
  1491. VERSION);
  1492. set_hintbar (text);
  1493. }
  1494. }
  1495. static void
  1496. setup_panels_and_run_mc (void)
  1497. {
  1498. add_widget (midnight_dlg, the_menubar);
  1499. add_widget (midnight_dlg, get_panel_widget (0));
  1500. add_widget (midnight_dlg, get_panel_widget (1));
  1501. add_widget (midnight_dlg, the_hint);
  1502. load_hint (1);
  1503. add_widget (midnight_dlg, cmdline);
  1504. add_widget (midnight_dlg, the_prompt);
  1505. add_widget (midnight_dlg, the_bar);
  1506. init_labels ();
  1507. if (boot_current_is_left)
  1508. dlg_select_widget (get_panel_widget (0));
  1509. else
  1510. dlg_select_widget (get_panel_widget (1));
  1511. /* Run the Midnight Commander if no file was specified in the command line */
  1512. run_dlg (midnight_dlg);
  1513. }
  1514. /* result must be free'd (I think this should go in util.c) */
  1515. static char *
  1516. prepend_cwd_on_local (const char *filename)
  1517. {
  1518. char *d;
  1519. int l;
  1520. if (vfs_file_is_local (filename)) {
  1521. if (*filename == PATH_SEP) /* an absolute pathname */
  1522. return g_strdup (filename);
  1523. d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
  1524. mc_get_current_wd (d, MC_MAXPATHLEN);
  1525. l = strlen (d);
  1526. d[l++] = PATH_SEP;
  1527. strcpy (d + l, filename);
  1528. canonicalize_pathname (d);
  1529. return d;
  1530. } else
  1531. return g_strdup (filename);
  1532. }
  1533. static int
  1534. mc_maybe_editor_or_viewer (void)
  1535. {
  1536. if (!(view_one_file || edit_one_file))
  1537. return 0;
  1538. setup_dummy_mc ();
  1539. /* Invoke the internal view/edit routine with:
  1540. * the default processing and forcing the internal viewer/editor
  1541. */
  1542. if (view_one_file) {
  1543. char *path = NULL;
  1544. path = prepend_cwd_on_local (view_one_file);
  1545. view_file (path, 0, 1);
  1546. g_free (path);
  1547. }
  1548. #ifdef USE_INTERNAL_EDIT
  1549. else {
  1550. edit_file (edit_one_file, edit_one_file_start_line);
  1551. }
  1552. #endif /* USE_INTERNAL_EDIT */
  1553. midnight_shutdown = 1;
  1554. done_mc ();
  1555. return 1;
  1556. }
  1557. /* Run the main dialog that occupies the whole screen */
  1558. static void
  1559. do_nc (void)
  1560. {
  1561. int midnight_colors[4];
  1562. midnight_colors[0] = NORMAL_COLOR; /* NORMALC */
  1563. midnight_colors[1] = REVERSE_COLOR; /* FOCUSC */
  1564. midnight_colors[2] = INPUT_COLOR; /* HOT_NORMALC */
  1565. midnight_colors[3] = NORMAL_COLOR; /* HOT_FOCUSC */
  1566. midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
  1567. "[main]", NULL, DLG_WANT_IDLE);
  1568. /* start check display_codepage and source_codepage */
  1569. check_codeset();
  1570. /* Check if we were invoked as an editor or file viewer */
  1571. if (!mc_maybe_editor_or_viewer ()) {
  1572. setup_mc ();
  1573. setup_panels_and_run_mc ();
  1574. /* Program end */
  1575. midnight_shutdown = 1;
  1576. /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
  1577. if (last_wd_file && vfs_current_is_local ()) {
  1578. last_wd_string = g_strdup (current_panel->cwd);
  1579. }
  1580. done_mc ();
  1581. }
  1582. destroy_dlg (midnight_dlg);
  1583. current_panel = 0;
  1584. done_mc_profile ();
  1585. }
  1586. /* POSIX version. The only version we support. */
  1587. static void
  1588. OS_Setup (void)
  1589. {
  1590. const char *mc_libdir;
  1591. shell = getenv ("SHELL");
  1592. if (!shell || !*shell) {
  1593. struct passwd *pwd;
  1594. pwd = getpwuid (geteuid ());
  1595. if (pwd != NULL)
  1596. shell = g_strdup (pwd->pw_shell);
  1597. }
  1598. if (!shell || !*shell)
  1599. shell = "/bin/sh";
  1600. /* This is the directory, where MC was installed, on Unix this is DATADIR */
  1601. /* and can be overriden by the MC_DATADIR environment variable */
  1602. if ((mc_libdir = getenv ("MC_DATADIR")) != NULL) {
  1603. mc_home = g_strdup (mc_libdir);
  1604. } else {
  1605. mc_home = g_strdup (SYSCONFDIR);
  1606. }
  1607. mc_home_alt = mc_libdir != NULL ? g_strdup (SYSCONFDIR) : g_strdup (DATADIR);
  1608. }
  1609. static void
  1610. sigchld_handler_no_subshell (int sig)
  1611. {
  1612. #ifdef __linux__
  1613. int pid, status;
  1614. if (!console_flag)
  1615. return;
  1616. /* COMMENT: if it were true that after the call to handle_console(..INIT)
  1617. the value of console_flag never changed, we could simply not install
  1618. this handler at all if (!console_flag && !use_subshell). */
  1619. /* That comment is no longer true. We need to wait() on a sigchld
  1620. handler (that's at least what the tarfs code expects currently). */
  1621. pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
  1622. if (pid == cons_saver_pid) {
  1623. if (WIFSTOPPED (status)) {
  1624. /* Someone has stopped cons.saver - restart it */
  1625. kill (pid, SIGCONT);
  1626. } else {
  1627. /* cons.saver has died - disable console saving */
  1628. handle_console (CONSOLE_DONE);
  1629. console_flag = 0;
  1630. }
  1631. }
  1632. /* If we got here, some other child exited; ignore it */
  1633. #endif /* __linux__ */
  1634. (void) sig;
  1635. }
  1636. static void
  1637. init_sigchld (void)
  1638. {
  1639. struct sigaction sigchld_action;
  1640. sigchld_action.sa_handler =
  1641. #ifdef HAVE_SUBSHELL_SUPPORT
  1642. use_subshell ? sigchld_handler :
  1643. #endif /* HAVE_SUBSHELL_SUPPORT */
  1644. sigchld_handler_no_subshell;
  1645. sigemptyset (&sigchld_action.sa_mask);
  1646. #ifdef SA_RESTART
  1647. sigchld_action.sa_flags = SA_RESTART;
  1648. #else
  1649. sigchld_action.sa_flags = 0;
  1650. #endif /* !SA_RESTART */
  1651. if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
  1652. #ifdef HAVE_SUBSHELL_SUPPORT
  1653. /*
  1654. * This may happen on QNX Neutrino 6, where SA_RESTART
  1655. * is defined but not implemented. Fallback to no subshell.
  1656. */
  1657. use_subshell = 0;
  1658. #endif /* HAVE_SUBSHELL_SUPPORT */
  1659. }
  1660. }
  1661. static void
  1662. print_mc_usage (poptContext ctx, FILE *stream)
  1663. {
  1664. int leftColWidth;
  1665. poptSetOtherOptionHelp (ctx,
  1666. _("[flags] [this_dir] [other_panel_dir]\n"));
  1667. /* print help for options */
  1668. leftColWidth = poptPrintHelp (ctx, stream, 0);
  1669. fprintf (stream, " %-*s %s\n", leftColWidth, _("+number"),
  1670. _("Set initial line number for the internal editor"));
  1671. fputs (_
  1672. ("\n"
  1673. "Please send any bug reports (including the output of `mc -V')\n"
  1674. "to mc-devel@gnome.org\n"), stream);
  1675. show_version (0);
  1676. }
  1677. static void
  1678. print_color_usage (void)
  1679. {
  1680. /*
  1681. * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
  1682. * and editmarked. To preserve translations, lines should be split.
  1683. */
  1684. /* TRANSLATORS: don't translate keywords and names of colors */
  1685. fputs (_
  1686. ("--colors KEYWORD={FORE},{BACK}\n\n"
  1687. "{FORE} and {BACK} can be omitted, and the default will be used\n"
  1688. "\n" "Keywords:\n"
  1689. " Global: errors, reverse, gauge, input, viewunderline\n"
  1690. " File display: normal, selected, marked, markselect\n"
  1691. " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
  1692. " errdhotfocus\n"
  1693. " Menus: menu, menuhot, menusel, menuhotsel\n"
  1694. " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
  1695. " editlinestate\n"), stdout);
  1696. fputs (_
  1697. (
  1698. " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
  1699. " File types: directory, executable, link, stalelink, device, special, core\n"
  1700. "\n" "Colors:\n"
  1701. " black, gray, red, brightred, green, brightgreen, brown,\n"
  1702. " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
  1703. " brightcyan, lightgray and white\n\n"), stdout);
  1704. }
  1705. static void
  1706. process_args (poptContext ctx, int c, const char *option_arg)
  1707. {
  1708. switch (c) {
  1709. case 'V':
  1710. show_version (1);
  1711. exit (0);
  1712. break;
  1713. case 'c':
  1714. disable_colors = 0;
  1715. #ifdef HAVE_SLANG
  1716. force_colors = 1;
  1717. #endif /* HAVE_SLANG */
  1718. break;
  1719. case 'f':
  1720. printf ("%s (%s)\n", mc_home, mc_home_alt);
  1721. exit (0);
  1722. break;
  1723. #ifdef USE_NETCODE
  1724. case 'l':
  1725. mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) option_arg);
  1726. #ifdef WITH_SMBFS
  1727. smbfs_set_debugf (option_arg);
  1728. #endif /* WITH_SMBFS */
  1729. break;
  1730. #ifdef WITH_SMBFS
  1731. case 'D':
  1732. smbfs_set_debug (atoi (option_arg));
  1733. break;
  1734. #endif /* WITH_SMBFS */
  1735. #endif /* USE_NETCODE */
  1736. case 'd':
  1737. use_mouse_p = MOUSE_DISABLED;
  1738. break;
  1739. #ifdef HAVE_SUBSHELL_SUPPORT
  1740. case 'u':
  1741. use_subshell = 0;
  1742. break;
  1743. #endif /* HAVE_SUBSHELL_SUPPORT */
  1744. case 'H':
  1745. print_color_usage ();
  1746. exit (0);
  1747. break;
  1748. case 'h':
  1749. print_mc_usage (ctx, stdout);
  1750. exit (0);
  1751. }
  1752. }
  1753. static const struct poptOption argument_table[] = {
  1754. /* generic options */
  1755. {"help", 'h', POPT_ARG_NONE, {NULL}, 'h',
  1756. N_("Displays this help message"), NULL},
  1757. {"version", 'V', POPT_ARG_NONE, {NULL}, 'V',
  1758. N_("Displays the current version"), NULL},
  1759. /* terminal options */
  1760. {"xterm", 'x', POPT_ARG_NONE, {&force_xterm}, 0,
  1761. N_("Forces xterm features"), NULL},
  1762. {"nomouse", 'd', POPT_ARG_NONE, {NULL}, 'd',
  1763. N_("Disable mouse support in text version"), NULL},
  1764. #if defined(HAVE_SLANG)
  1765. {"termcap", 't', 0, {&SLtt_Try_Termcap}, 0,
  1766. N_("Tries to use termcap instead of terminfo"), NULL},
  1767. #endif
  1768. {"resetsoft", 'k', POPT_ARG_NONE, {&reset_hp_softkeys}, 0,
  1769. N_("Resets soft keys on HP terminals"), NULL},
  1770. {"slow", 's', POPT_ARG_NONE, {&slow_terminal}, 0,
  1771. N_("To run on slow terminals"), NULL},
  1772. {"stickchars", 'a', 0, {&force_ugly_line_drawing}, 0,
  1773. N_("Use stickchars to draw"), NULL},
  1774. /* color options */
  1775. {"nocolor", 'b', POPT_ARG_NONE, {&disable_colors}, 0,
  1776. N_("Requests to run in black and white"), NULL},
  1777. {"color", 'c', POPT_ARG_NONE, {NULL}, 'c',
  1778. N_("Request to run in color mode"), NULL},
  1779. {"colors", 'C', POPT_ARG_STRING, {&command_line_colors}, 0,
  1780. N_("Specifies a color configuration"), NULL},
  1781. {"help-colors", 'H', POPT_ARG_NONE, {NULL}, 'H',
  1782. N_("Displays a help screen on how to change the color scheme"), NULL},
  1783. /* debug options */
  1784. #ifdef USE_NETCODE
  1785. {"ftplog", 'l', POPT_ARG_STRING, {NULL}, 'l',
  1786. N_("Log ftp dialog to specified file"), NULL},
  1787. #ifdef WITH_SMBFS
  1788. {"debuglevel", 'D', POPT_ARG_STRING, {NULL}, 'D',
  1789. N_("Set debug level"), NULL},
  1790. #endif
  1791. #endif
  1792. /* options for wrappers */
  1793. {"datadir", 'f', POPT_ARG_NONE, {NULL}, 'f',
  1794. N_("Print data directory"), NULL},
  1795. {"printwd", 'P', POPT_ARG_STRING, {&last_wd_file}, 0,
  1796. N_("Print last working directory to specified file"), NULL},
  1797. /* subshell options */
  1798. #ifdef HAVE_SUBSHELL_SUPPORT
  1799. {"subshell", 'U', POPT_ARG_NONE, {&use_subshell}, 0,
  1800. N_("Enables subshell support (default)"), NULL},
  1801. {"nosubshell", 'u', POPT_ARG_NONE, {NULL}, 'u',
  1802. N_("Disables subshell support"), NULL},
  1803. #endif
  1804. /* single file operations */
  1805. {"view", 'v', POPT_ARG_STRING, {&view_one_file}, 0,
  1806. N_("Launches the file viewer on a file"), NULL},
  1807. #ifdef USE_INTERNAL_EDIT
  1808. {"edit", 'e', POPT_ARG_STRING, {&edit_one_file}, 0,
  1809. N_("Edits one file"), NULL},
  1810. #endif
  1811. {NULL, '\0', 0, {NULL}, 0, NULL , NULL}
  1812. };
  1813. static void
  1814. handle_args (int argc, char *argv[])
  1815. {
  1816. char *tmp;
  1817. poptContext ctx;
  1818. const char *base;
  1819. int c;
  1820. ctx =
  1821. poptGetContext ("mc", argc, argv, argument_table,
  1822. POPT_CONTEXT_NO_EXEC);
  1823. while ((c = poptGetNextOpt (ctx)) > 0) {
  1824. process_args (ctx, c, poptGetOptArg (ctx));
  1825. }
  1826. if (c < -1) {
  1827. print_mc_usage (ctx, stderr);
  1828. fprintf (stderr, "%s: %s\n",
  1829. poptBadOption (ctx, POPT_BADOPTION_NOALIAS),
  1830. poptStrerror (c));
  1831. exit (1);
  1832. }
  1833. tmp = poptGetArg (ctx);
  1834. /*
  1835. * Check for special invocation names mcedit and mcview,
  1836. * if none apply then set the current directory and the other
  1837. * directory from the command line arguments
  1838. */
  1839. base = x_basename (argv[0]);
  1840. if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
  1841. edit_one_file = "";
  1842. if (tmp) {
  1843. /*
  1844. * Check for filename:lineno, followed by an optional colon.
  1845. * This format is used by many programs (especially compilers)
  1846. * in error messages and warnings. It is supported so that
  1847. * users can quickly copy and paste file locations.
  1848. */
  1849. char *end = tmp + strlen (tmp), *p = end;
  1850. if (p > tmp && p[-1] == ':')
  1851. p--;
  1852. while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
  1853. p--;
  1854. if (tmp < p && p < end && p[-1] == ':') {
  1855. struct stat st;
  1856. gchar *fname = g_strndup (tmp, p - 1 - tmp);
  1857. /*
  1858. * Check that the file before the colon actually exists.
  1859. * If it doesn't exist, revert to the old behavior.
  1860. */
  1861. if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
  1862. edit_one_file = fname;
  1863. edit_one_file_start_line = atoi (p);
  1864. } else {
  1865. g_free (fname);
  1866. goto try_plus_filename;
  1867. }
  1868. } else {
  1869. try_plus_filename:
  1870. if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
  1871. int start_line = atoi (tmp);
  1872. if (start_line > 0) {
  1873. char *file = poptGetArg (ctx);
  1874. if (file) {
  1875. tmp = file;
  1876. edit_one_file_start_line = start_line;
  1877. }
  1878. }
  1879. }
  1880. edit_one_file = g_strdup (tmp);
  1881. }
  1882. }
  1883. } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
  1884. if (tmp)
  1885. view_one_file = g_strdup (tmp);
  1886. else {
  1887. fputs ("No arguments given to the viewer\n", stderr);
  1888. exit (1);
  1889. }
  1890. } else {
  1891. /* sets the current dir and the other dir */
  1892. if (tmp) {
  1893. this_dir = g_strdup (tmp);
  1894. if ((tmp = poptGetArg (ctx)))
  1895. other_dir = g_strdup (tmp);
  1896. }
  1897. }
  1898. poptFreeContext (ctx);
  1899. }
  1900. int
  1901. main (int argc, char *argv[])
  1902. {
  1903. struct stat s;
  1904. char *mc_dir;
  1905. /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
  1906. setlocale (LC_ALL, "");
  1907. bindtextdomain ("mc", LOCALEDIR);
  1908. textdomain ("mc");
  1909. /* Set up temporary directory */
  1910. mc_tmpdir ();
  1911. OS_Setup ();
  1912. /* This variable is used by the subshell */
  1913. home_dir = getenv ("HOME");
  1914. if (!home_dir) {
  1915. /* mc_home was computed by OS_Setup */
  1916. home_dir = mc_home;
  1917. }
  1918. str_init_strings (NULL);
  1919. vfs_init ();
  1920. #ifdef USE_INTERNAL_EDIT
  1921. edit_stack_init ();
  1922. #endif
  1923. #ifdef HAVE_SLANG
  1924. SLtt_Ignore_Beep = 1;
  1925. #endif
  1926. handle_args (argc, argv);
  1927. /* NOTE: This has to be called before slang_init or whatever routine
  1928. calls any define_sequence */
  1929. init_key ();
  1930. /* Must be done before installing the SIGCHLD handler [[FIXME]] */
  1931. handle_console (CONSOLE_INIT);
  1932. #ifdef HAVE_SUBSHELL_SUPPORT
  1933. /* Don't use subshell when invoked as viewer or editor */
  1934. if (edit_one_file || view_one_file)
  1935. use_subshell = 0;
  1936. if (use_subshell)
  1937. subshell_get_console_attributes ();
  1938. #endif /* HAVE_SUBSHELL_SUPPORT */
  1939. /* Install the SIGCHLD handler; must be done before init_subshell() */
  1940. init_sigchld ();
  1941. /* We need this, since ncurses endwin () doesn't restore the signals */
  1942. save_stop_handler ();
  1943. /* Must be done before init_subshell, to set up the terminal size: */
  1944. /* FIXME: Should be removed and LINES and COLS computed on subshell */
  1945. #ifdef HAVE_SLANG
  1946. slang_init ();
  1947. #endif
  1948. start_interrupt_key ();
  1949. /* NOTE: This call has to be after slang_init. It's the small part from
  1950. the previous init_key which had to be moved after the call of slang_init */
  1951. init_key_input_fd ();
  1952. load_setup ();
  1953. init_curses ();
  1954. /* create home directory */
  1955. /* do it after the screen library initialization to show the error message */
  1956. mc_dir = concat_dir_and_file (home_dir, MC_BASE);
  1957. canonicalize_pathname (mc_dir);
  1958. if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
  1959. && mkdir (mc_dir, 0700) != 0)
  1960. message (D_ERROR, _("Warning"),
  1961. _("Cannot create %s directory"), mc_dir);
  1962. g_free (mc_dir);
  1963. init_xterm_support ();
  1964. #ifdef HAVE_SUBSHELL_SUPPORT
  1965. /* Done here to ensure that the subshell doesn't */
  1966. /* inherit the file descriptors opened below, etc */
  1967. if (use_subshell)
  1968. init_subshell ();
  1969. #endif /* HAVE_SUBSHELL_SUPPORT */
  1970. /* Removing this from the X code let's us type C-c */
  1971. load_key_defs ();
  1972. /* Also done after init_subshell, to save any shell init file messages */
  1973. if (console_flag)
  1974. handle_console (CONSOLE_SAVE);
  1975. if (alternate_plus_minus)
  1976. application_keypad_mode ();
  1977. #ifdef HAVE_SUBSHELL_SUPPORT
  1978. if (use_subshell) {
  1979. prompt = strip_ctrl_codes (subshell_prompt);
  1980. if (!prompt)
  1981. prompt = "";
  1982. } else
  1983. #endif /* HAVE_SUBSHELL_SUPPORT */
  1984. prompt = (geteuid () == 0) ? "# " : "$ ";
  1985. /* Program main loop */
  1986. if (!midnight_shutdown)
  1987. do_nc ();
  1988. /* Save the tree store */
  1989. tree_store_save ();
  1990. /* Virtual File System shutdown */
  1991. vfs_shut ();
  1992. flush_extension_file (); /* does only free memory */
  1993. endwin ();
  1994. #ifdef HAVE_SLANG
  1995. slang_shutdown ();
  1996. #endif
  1997. if (console_flag && !(quit & SUBSHELL_EXIT))
  1998. handle_console (CONSOLE_RESTORE);
  1999. if (alternate_plus_minus)
  2000. numeric_keypad_mode ();
  2001. signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
  2002. if (console_flag)
  2003. handle_console (CONSOLE_DONE);
  2004. putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
  2005. if (last_wd_file && last_wd_string && !print_last_revert
  2006. && !edit_one_file && !view_one_file) {
  2007. int last_wd_fd =
  2008. open (last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
  2009. S_IRUSR | S_IWUSR);
  2010. if (last_wd_fd != -1) {
  2011. write (last_wd_fd, last_wd_string, strlen (last_wd_string));
  2012. close (last_wd_fd);
  2013. }
  2014. }
  2015. g_free (last_wd_string);
  2016. g_free (mc_home_alt);
  2017. g_free (mc_home);
  2018. done_key ();
  2019. #ifdef HAVE_CHARSET
  2020. free_codepages_list ();
  2021. #endif
  2022. str_uninit_strings ();
  2023. g_free (this_dir);
  2024. g_free (other_dir);
  2025. #ifdef USE_INTERNAL_EDIT
  2026. edit_stack_free ();
  2027. #endif
  2028. return 0;
  2029. }