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
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:
45 #include <sysexits.h> /* XXX: Portability? */
49 #include <config/config.h>
50 #include <libhed/file.h>
51 #include <term/term.h>
52 /* #include <ui/cmdline.h> */
54 #include <ui/fileshow.h>
55 /* #include <ui/windowbar.h> */
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
74 # define LONG_OR_SHORT(l, s) s
78 complain_swap(struct hed_file
*file
)
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
);
94 printf("%s, a visual hexadecimal editor.\n"
95 "Usage: %s [OPTION]... FILE\n"
99 " -V, --version display the version of HEd and exit.\n",
100 " -V display the version of HEd and exit.\n")
102 " -h, --help print this help.\n",
103 " -h print this help.\n")
107 " -s, --spread fill the maximal width of line.\n",
108 " -s fill the maximal width of line.\n")
112 " --normal start in the N(ormal) mode (DEFAULT).\n",
115 " -r, --replace start in the R(eplace) mode.\n",
116 " -r start in the R(eplace) mode.\n")
118 " -i, --insert start in the I(nsert) mode.\n",
119 " -i start in the I(nsert) mode.\n")
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")
125 "Mail bug reports and suggestions to %s.\n",
126 namevers
, exe
, maintainer
);
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",
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;
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' },
167 //char *numguard = NULL;
170 #ifdef HAVE_GETOPT_LONG
171 c
= getopt_long(argc
, argv
, "hiprsV", long_options
, &optindex
);
173 c
= getopt(argc
, argv
, "hiprsV");
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]);
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.
203 file
= hed_open(argv
[optind
]);
205 perror(argv
[optind
]);
210 if (hed_file_read_swap(file
)) {
214 fprintf(stderr
, "Restoring %s failed, sorry\n",
220 hed_file_remove_swap(file
);
224 hed_file_remove_swap(file
);
225 if (hed_file_has_swap(file
)) {
234 /* windowbar_init(); */
236 /* cmdline_init(); */
241 struct ui_event event
;
243 if (kbdplay
&& *kbdplay
) {
244 event
.type
= EV_KEYBOARD
;
245 event
.v
.keyboard
.ch
= *kbdplay
++;
246 ui_propevent(&event
);
248 if (ui_pollevent(&event
))
250 ui_propevent(&event
);