cleanup: remove tree/file parameter where possible
[hed.git] / src / main.c
blobe69f094207fa99581a800af909d7c804a1f3802f
1 /* $Id$ */
3 /*
4 * hed - Hexadecimal editor
5 * Copyright (C) 2004 Petr Baudis <pasky@ucw.cz>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * 'Away now, Shadowfax! Run, greatheart, run as you have never run before! Now
23 * we are come to the lands where you were foaled and every stone you know. Run
24 * now! Hope is in speed!'
26 * Shadowfax tossed his head and cried aloud, as if a trumpet had summoned him
27 * to battle. Then he sprang forward. Fire flew from his feet; night rushed
28 * over him.
30 * As he fell slowly into sleep, Pippin had a strange feeling: he and Gandalf
31 * were still as stone, seated upon the statue of a running horse, while the
32 * world rolled away beneath his feet with a great noise of wind.
35 /* Feature macros needed for:
36 * - getopt_long
38 #define _GNU_SOURCE
40 #include <config.h>
42 #include <errno.h>
43 #include <getopt.h>
44 #include <stdlib.h>
45 #include <sysexits.h> /* XXX: Portability? */
47 #include <hed.h>
49 #include <config/config.h>
50 #include <libhed/file.h>
51 #include <term/term.h>
52 /* #include <ui/cmdline.h> */
53 #include <ui/core.h>
54 #include <ui/fileshow.h>
55 /* #include <ui/windowbar.h> */
56 #include <main.h>
58 #define VERSION "0.5"
61 static char *kbdplay;
63 bool terminus = 0;
64 int prg_retval = EX_OK;
66 bool opt_spreadview = 0;
68 static const char namevers[] = "HEd " VERSION;
69 static const char maintainer[] = "<pasky@ucw.cz>";
71 #ifdef HAVE_GETOPT_LONG
72 # define LONG_OR_SHORT(l, s) l
73 #else
74 # define LONG_OR_SHORT(l, s) s
75 #endif
77 static void
78 complain_swap(struct hed_file *file)
80 fprintf(stderr,
81 "Error: Swap file %s for file %s exists\n"
82 "This means that hed did not quit properly the last time\n"
83 "you edited this file. You can use the swap file to restore\n"
84 "some of your unsaved work. Run hed with --restore-swp to\n"
85 "do that (until you save your file, the swap file will\n"
86 "stay around). Run hed with --remove-swp to get rid of\n"
87 "the file. (You can also use both options simultanously.)\n",
88 hed_file_swap_name(file), file->name);
91 static void
92 usage(char *exe)
94 printf("%s, a visual hexadecimal editor.\n"
95 "Usage: %s [OPTION]... FILE\n"
96 "\n"
97 "Startup:\n"
98 LONG_OR_SHORT(
99 " -V, --version display the version of HEd and exit.\n",
100 " -V display the version of HEd and exit.\n")
101 LONG_OR_SHORT(
102 " -h, --help print this help.\n",
103 " -h print this help.\n")
104 "\n"
105 "Visuals:\n"
106 LONG_OR_SHORT(
107 " -s, --spread fill the maximal width of line.\n",
108 " -s fill the maximal width of line.\n")
109 "\n"
110 "Controls:\n"
111 LONG_OR_SHORT(
112 " --normal start in the N(ormal) mode (DEFAULT).\n",
114 LONG_OR_SHORT(
115 " -r, --replace start in the R(eplace) mode.\n",
116 " -r start in the R(eplace) mode.\n")
117 LONG_OR_SHORT(
118 " -i, --insert start in the I(nsert) mode.\n",
119 " -i start in the I(nsert) mode.\n")
120 "\n"
121 LONG_OR_SHORT(
122 " -p, --play=... the value is interpreted as keyboard input fed to hed.\n",
123 " -p ... the value is interpreted as keyboard input fed to hed.\n")
124 "\n"
125 "Mail bug reports and suggestions to %s.\n",
126 namevers, exe, maintainer);
127 exit(EX_USAGE);
130 static void
131 version(void)
133 printf("%s\n\n"
134 "\n"
135 "Copyright (C) 2004, 2005 Petr Baudis <pasky@ucw.cz>\n"
136 "This program is distributed in the hope that it will be useful,\n"
137 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
138 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
139 "GNU General Public License for more details.\n",
140 namevers);
141 exit(EX_USAGE);
145 main(int argc, char *argv[])
147 struct hed_file *file;
148 bool show_usage = 0, show_version = 0;
149 bool remove_swp = 0, restore_swp = 0;
151 while (1) {
152 #ifdef HAVE_GETOPT_LONG
153 static struct option long_options[] = {
154 { "help", 0, 0, 'h' },
155 { "insert", 0, 0, 'i' },
156 { "normal", 0, 0, 1 },
157 { "play", 1, 0, 'p' },
158 { "remove-swp", 0, 0, 2 },
159 { "replace", 0, 0, 'r' },
160 { "restore-swp",0, 0, 3 },
161 { "spread", 0, 0, 's' },
162 { "version", 0, 0, 'V' },
163 { NULL, 0, 0, 0 }
165 int optindex;
166 #endif
167 //char *numguard = NULL;
168 int c;
170 #ifdef HAVE_GETOPT_LONG
171 c = getopt_long(argc, argv, "hiprsV", long_options, &optindex);
172 #else
173 c = getopt(argc, argv, "hiprsV");
174 #endif
175 if (c == -1) break;
177 switch (c) {
178 case 'h':
179 case '?': show_usage = 1; break;
180 case 'i': opt_defmode = MODE_INSERT; break;
181 case 1 : opt_defmode = MODE_NORMAL; break;
182 case 'r': opt_defmode = MODE_REPLACE; break;
183 case 's': opt_spreadview = 1; break;
184 case 'V': show_version = 1; break;
185 case 2 : remove_swp = 1; break;
186 case 3 : restore_swp = 1; break;
187 case 'p': kbdplay = optarg; break;
191 if (show_version) version();
192 if (show_usage || argc <= optind) usage(argv[0]);
194 config_init();
196 /* Do not check for errors. In the worst case the editor won't
197 * handle file truncates, but that's usually not fatal.
198 * TODO: Issue a warning in such a way that it doesn't get
199 * immediately overwritten.
201 libhed_init();
203 file = hed_open(argv[optind]);
204 if (!file) {
205 perror(argv[optind]);
206 exit(EX_NOINPUT);
209 if (restore_swp) {
210 if (hed_file_read_swap(file)) {
211 if (errno)
212 perror(file->name);
213 else
214 fprintf(stderr, "Restoring %s failed, sorry\n",
215 file->name);
216 hed_close(file);
217 exit(EX_NOINPUT);
219 if (remove_swp)
220 hed_file_remove_swap(file);
222 } else {
223 if (remove_swp)
224 hed_file_remove_swap(file);
225 if (hed_file_has_swap(file)) {
226 complain_swap(file);
227 hed_close(file);
228 exit(EX_NOINPUT);
232 term_init();
233 ui_init();
234 /* windowbar_init(); */
235 fileshow_init(file);
236 /* cmdline_init(); */
238 ui_redraw();
240 while (!terminus) {
241 struct ui_event event;
243 if (kbdplay && *kbdplay) {
244 event.type = EV_KEYBOARD;
245 event.v.keyboard.ch = *kbdplay++;
246 ui_propevent(&event);
247 } else {
248 if (ui_pollevent(&event))
249 break;
250 ui_propevent(&event);
254 ui_done();
255 term_done();
256 hed_close(file);
258 exit(prg_retval);