OSDN Git Service

[Fix] #40233 auto_dump_stream ファイルディスクリプタ周りをビルドが通るように展開. / Expand so that the build...
[hengband/hengband.git] / src / cmd / cmd-macro.c
1 #include "angband.h"
2 #include "cmd-macro.h"
3 #include "cmd-gameoption.h"
4 #include "files.h"
5 #include "io/read-pref-file.h"
6 #include "term.h"
7
8 /*!
9  * @brief マクロ情報をprefファイルに保存する /
10  * @param fname ファイル名
11  * @return なし
12  */
13 static void macro_dump(FILE *auto_dump_stream, concptr fname)
14 {
15         static concptr mark = "Macro Dump";
16         char buf[1024];
17         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, fname);
18         FILE_TYPE(FILE_TYPE_TEXT);
19         if (!open_auto_dump(auto_dump_stream, buf, mark)) return;
20
21         auto_dump_printf(auto_dump_stream, _("\n# 自動マクロセーブ\n\n", "\n# Automatic macro dump\n\n"));
22         for (int i = 0; i < macro__num; i++)
23         {
24                 ascii_to_text(buf, macro__act[i]);
25                 auto_dump_printf(auto_dump_stream, "A:%s\n", buf);
26                 ascii_to_text(buf, macro__pat[i]);
27                 auto_dump_printf(auto_dump_stream, "P:%s\n", buf);
28                 auto_dump_printf(auto_dump_stream, "\n");
29         }
30
31         close_auto_dump(auto_dump_stream);
32 }
33
34
35 /*!
36  * @brief マクロのトリガーキーを取得する /
37  * Hack -- ask for a "trigger" (see below)
38  * @param buf キー表記を保管するバッファ
39  * @return なし
40  * @details
41  * <pre>
42  * Note the complex use of the "inkey()" function from "util.c".
43  *
44  * Note that both "flush()" calls are extremely important.
45  * </pre>
46  */
47 static void do_cmd_macro_aux(char *buf)
48 {
49         flush();
50         inkey_base = TRUE;
51         char i = inkey();
52         int n = 0;
53         while (i)
54         {
55                 buf[n++] = i;
56                 inkey_base = TRUE;
57                 inkey_scan = TRUE;
58                 i = inkey();
59         }
60
61         buf[n] = '\0';
62         flush();
63         char tmp[1024];
64         ascii_to_text(tmp, buf);
65         Term_addstr(-1, TERM_WHITE, tmp);
66 }
67
68
69 /*!
70  * @brief マクロのキー表記からアスキーコードを得てターミナルに表示する /
71  * Hack -- ask for a keymap "trigger" (see below)
72  * @param buf キー表記を取得するバッファ
73  * @return なし
74  * @details
75  * <pre>
76  * Note that both "flush()" calls are extremely important.  This may
77  * no longer be true, since "util.c" is much simpler now.
78  * </pre>
79  */
80 static void do_cmd_macro_aux_keymap(char *buf)
81 {
82         char tmp[1024];
83         flush();
84         buf[0] = inkey();
85         buf[1] = '\0';
86         ascii_to_text(tmp, buf);
87         Term_addstr(-1, TERM_WHITE, tmp);
88         flush();
89 }
90
91
92 /*!
93  * @brief キーマップをprefファイルにダンプする /
94  * Hack -- append all keymaps to the given file
95  * @param fname ファイルネーム
96  * @return エラーコード
97  * @details
98  */
99 static errr keymap_dump(concptr fname)
100 {
101         FILE *auto_dump_stream;
102         static concptr mark = "Keymap Dump";
103         char key[1024];
104         char buf[1024];
105         BIT_FLAGS mode;
106         if (rogue_like_commands)
107         {
108                 mode = KEYMAP_MODE_ROGUE;
109         }
110         else
111         {
112                 mode = KEYMAP_MODE_ORIG;
113         }
114
115         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, fname);
116         FILE_TYPE(FILE_TYPE_TEXT);
117         if (!open_auto_dump(auto_dump_stream, buf, mark)) return -1;
118
119         auto_dump_printf(auto_dump_stream, _("\n# 自動キー配置セーブ\n\n", "\n# Automatic keymap dump\n\n"));
120         for (int i = 0; i < 256; i++)
121         {
122                 concptr act;
123                 act = keymap_act[mode][i];
124                 if (!act) continue;
125
126                 buf[0] = (char)i;
127                 buf[1] = '\0';
128                 ascii_to_text(key, buf);
129                 ascii_to_text(buf, act);
130                 auto_dump_printf(auto_dump_stream, "A:%s\n", buf);
131                 auto_dump_printf(auto_dump_stream, "C:%d:%s\n", mode, key);
132         }
133
134         close_auto_dump(auto_dump_stream);
135         return 0;
136 }
137
138
139 /*!
140  * @brief マクロを設定するコマンドのメインルーチン /
141  * Interact with "macros"
142  * @return なし
143  * @details
144  * <pre>
145  * Note that the macro "action" must be defined before the trigger.
146  *
147  * Could use some helpful instructions on this page.
148  * </pre>
149  */
150 void do_cmd_macros(player_type *creature_ptr)
151 {
152         char tmp[1024];
153         char buf[1024];
154         FILE *auto_dump_stream;
155         BIT_FLAGS mode = rogue_like_commands ? KEYMAP_MODE_ROGUE : KEYMAP_MODE_ORIG;
156         FILE_TYPE(FILE_TYPE_TEXT);
157         screen_save();
158         while (TRUE)
159         {
160                 Term_clear();
161                 prt(_("[ マクロの設定 ]", "Interact with Macros"), 2, 0);
162                 prt(_("マクロ行動が(もしあれば)下に表示されます:", "Current action (if any) shown below:"), 20, 0);
163                 ascii_to_text(buf, macro__buf);
164                 prt(buf, 22, 0);
165
166                 prt(_("(1) ユーザー設定ファイルのロード", "(1) Load a user pref file"), 4, 5);
167                 prt(_("(2) ファイルにマクロを追加", "(2) Append macros to a file"), 5, 5);
168                 prt(_("(3) マクロの確認", "(3) Query a macro"), 6, 5);
169                 prt(_("(4) マクロの作成", "(4) Create a macro"), 7, 5);
170                 prt(_("(5) マクロの削除", "(5) Remove a macro"), 8, 5);
171                 prt(_("(6) ファイルにキー配置を追加", "(6) Append keymaps to a file"), 9, 5);
172                 prt(_("(7) キー配置の確認", "(7) Query a keymap"), 10, 5);
173                 prt(_("(8) キー配置の作成", "(8) Create a keymap"), 11, 5);
174                 prt(_("(9) キー配置の削除", "(9) Remove a keymap"), 12, 5);
175                 prt(_("(0) マクロ行動の入力", "(0) Enter a new action"), 13, 5);
176
177                 prt(_("コマンド: ", "Command: "), 16, 0);
178                 int i = inkey();
179                 if (i == ESCAPE) break;
180
181                 else if (i == '1')
182                 {
183                         prt(_("コマンド: ユーザー設定ファイルのロード", "Command: Load a user pref file"), 16, 0);
184                         prt(_("ファイル: ", "File: "), 18, 0);
185                         sprintf(tmp, "%s.prf", creature_ptr->base_name);
186                         if (!askfor(tmp, 80)) continue;
187
188                         errr err = process_pref_file(creature_ptr, tmp);
189                         if (-2 == err)
190                                 msg_format(_("標準の設定ファイル'%s'を読み込みました。", "Loaded default '%s'."), tmp);
191                         else if (err)
192                                 msg_format(_("'%s'の読み込みに失敗しました!", "Failed to load '%s'!"), tmp);
193                         else
194                                 msg_format(_("'%s'を読み込みました。", "Loaded '%s'."), tmp);
195                 }
196                 else if (i == '2')
197                 {
198                         prt(_("コマンド: マクロをファイルに追加する", "Command: Append macros to a file"), 16, 0);
199                         prt(_("ファイル: ", "File: "), 18, 0);
200                         sprintf(tmp, "%s.prf", creature_ptr->base_name);
201                         if (!askfor(tmp, 80)) continue;
202
203                         macro_dump(auto_dump_stream, tmp);
204                         msg_print(_("マクロを追加しました。", "Appended macros."));
205                 }
206                 else if (i == '3')
207                 {
208                         prt(_("コマンド: マクロの確認", "Command: Query a macro"), 16, 0);
209                         prt(_("トリガーキー: ", "Trigger: "), 18, 0);
210                         do_cmd_macro_aux(buf);
211                         int k = macro_find_exact(buf);
212                         if (k < 0)
213                         {
214                                 msg_print(_("そのキーにはマクロは定義されていません。", "Found no macro."));
215                         }
216                         else
217                         {
218                                 strcpy(macro__buf, macro__act[k]);
219                                 ascii_to_text(buf, macro__buf);
220                                 prt(buf, 22, 0);
221                                 msg_print(_("マクロを確認しました。", "Found a macro."));
222                         }
223                 }
224                 else if (i == '4')
225                 {
226                         prt(_("コマンド: マクロの作成", "Command: Create a macro"), 16, 0);
227                         prt(_("トリガーキー: ", "Trigger: "), 18, 0);
228                         do_cmd_macro_aux(buf);
229                         clear_from(20);
230                         c_prt(TERM_L_RED, _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
231                                 "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."), 22, 0);
232                         prt(_("マクロ行動: ", "Action: "), 20, 0);
233                         ascii_to_text(tmp, macro__buf);
234                         if (askfor(tmp, 80))
235                         {
236                                 text_to_ascii(macro__buf, tmp);
237                                 macro_add(buf, macro__buf);
238                                 msg_print(_("マクロを追加しました。", "Added a macro."));
239                         }
240                 }
241                 else if (i == '5')
242                 {
243                         prt(_("コマンド: マクロの削除", "Command: Remove a macro"), 16, 0);
244                         prt(_("トリガーキー: ", "Trigger: "), 18, 0);
245                         do_cmd_macro_aux(buf);
246                         macro_add(buf, buf);
247                         msg_print(_("マクロを削除しました。", "Removed a macro."));
248                 }
249                 else if (i == '6')
250                 {
251                         prt(_("コマンド: キー配置をファイルに追加する", "Command: Append keymaps to a file"), 16, 0);
252                         prt(_("ファイル: ", "File: "), 18, 0);
253                         sprintf(tmp, "%s.prf", creature_ptr->base_name);
254                         if (!askfor(tmp, 80)) continue;
255
256                         (void)keymap_dump(tmp);
257                         msg_print(_("キー配置を追加しました。", "Appended keymaps."));
258                 }
259                 else if (i == '7')
260                 {
261                         prt(_("コマンド: キー配置の確認", "Command: Query a keymap"), 16, 0);
262                         prt(_("押すキー: ", "Keypress: "), 18, 0);
263                         do_cmd_macro_aux_keymap(buf);
264                         concptr act = keymap_act[mode][(byte)(buf[0])];
265                         if (!act)
266                         {
267                                 msg_print(_("キー配置は定義されていません。", "Found no keymap."));
268                         }
269                         else
270                         {
271                                 strcpy(macro__buf, act);
272                                 ascii_to_text(buf, macro__buf);
273                                 prt(buf, 22, 0);
274                                 msg_print(_("キー配置を確認しました。", "Found a keymap."));
275                         }
276                 }
277                 else if (i == '8')
278                 {
279                         prt(_("コマンド: キー配置の作成", "Command: Create a keymap"), 16, 0);
280                         prt(_("押すキー: ", "Keypress: "), 18, 0);
281                         do_cmd_macro_aux_keymap(buf);
282                         clear_from(20);
283                         c_prt(TERM_L_RED, _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
284                                 "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."), 22, 0);
285                         prt(_("行動: ", "Action: "), 20, 0);
286                         ascii_to_text(tmp, macro__buf);
287                         if (askfor(tmp, 80))
288                         {
289                                 text_to_ascii(macro__buf, tmp);
290                                 string_free(keymap_act[mode][(byte)(buf[0])]);
291                                 keymap_act[mode][(byte)(buf[0])] = string_make(macro__buf);
292                                 msg_print(_("キー配置を追加しました。", "Added a keymap."));
293                         }
294                 }
295                 else if (i == '9')
296                 {
297                         prt(_("コマンド: キー配置の削除", "Command: Remove a keymap"), 16, 0);
298                         prt(_("押すキー: ", "Keypress: "), 18, 0);
299                         do_cmd_macro_aux_keymap(buf);
300                         string_free(keymap_act[mode][(byte)(buf[0])]);
301                         keymap_act[mode][(byte)(buf[0])] = NULL;
302                         msg_print(_("キー配置を削除しました。", "Removed a keymap."));
303                 }
304                 else if (i == '0')
305                 {
306                         prt(_("コマンド: マクロ行動の入力", "Command: Enter a new action"), 16, 0);
307                         clear_from(20);
308                         c_prt(TERM_L_RED, _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
309                                 "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."), 22, 0);
310                         prt(_("マクロ行動: ", "Action: "), 20, 0);
311                         tmp[80] = '\0';
312                         if (!askfor(buf, 80)) continue;
313
314                         text_to_ascii(macro__buf, buf);
315                 }
316                 else
317                 {
318                         bell();
319                 }
320
321                 msg_erase();
322         }
323
324         screen_load();
325 }