]> git.sur5r.net Git - i3/i3/blob - src/load_layout.c
Merge branch 'master' into next
[i3/i3] / src / load_layout.c
1 #undef I3__FILE__
2 #define I3__FILE__ "load_layout.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * load_layout.c: Restore (parts of) the layout, for example after an inplace
10  *                restart.
11  *
12  */
13 #include "all.h"
14
15 #include <yajl/yajl_common.h>
16 #include <yajl/yajl_gen.h>
17 #include <yajl/yajl_parse.h>
18 #include <yajl/yajl_version.h>
19
20 /* TODO: refactor the whole parsing thing */
21
22 static char *last_key;
23 static Con *json_node;
24 static Con *to_focus;
25 static bool parsing_swallows;
26 static bool parsing_rect;
27 static bool parsing_deco_rect;
28 static bool parsing_window_rect;
29 static bool parsing_geometry;
30 static bool parsing_focus;
31 struct Match *current_swallow;
32
33 /* This list is used for reordering the focus stack after parsing the 'focus'
34  * array. */
35 struct focus_mapping {
36     int old_id;
37     TAILQ_ENTRY(focus_mapping) focus_mappings;
38 };
39
40 static TAILQ_HEAD(focus_mappings_head, focus_mapping) focus_mappings =
41     TAILQ_HEAD_INITIALIZER(focus_mappings);
42
43 static int json_start_map(void *ctx) {
44     LOG("start of map, last_key = %s\n", last_key);
45     if (parsing_swallows) {
46         LOG("creating new swallow\n");
47         current_swallow = smalloc(sizeof(Match));
48         match_init(current_swallow);
49         TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches);
50     } else {
51         if (!parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
52             if (last_key && strcasecmp(last_key, "floating_nodes") == 0) {
53                 DLOG("New floating_node\n");
54                 Con *ws = con_get_workspace(json_node);
55                 json_node = con_new_skeleton(NULL, NULL);
56                 json_node->name = NULL;
57                 json_node->parent = ws;
58                 DLOG("Parent is workspace = %p\n", ws);
59             } else {
60                 Con *parent = json_node;
61                 json_node = con_new_skeleton(NULL, NULL);
62                 json_node->name = NULL;
63                 json_node->parent = parent;
64             }
65         }
66     }
67     return 1;
68 }
69
70 static int json_end_map(void *ctx) {
71     LOG("end of map\n");
72     if (!parsing_swallows && !parsing_rect && !parsing_deco_rect && !parsing_window_rect && !parsing_geometry) {
73         /* Set a few default values to simplify manually crafted layout files. */
74         if (json_node->layout == L_DEFAULT) {
75             DLOG("Setting layout = L_SPLITH\n");
76             json_node->layout = L_SPLITH;
77         }
78
79         /* Sanity check: swallow criteria don’t make any sense on a split
80          * container. */
81         if (con_is_split(json_node) > 0 && !TAILQ_EMPTY(&(json_node->swallow_head))) {
82             DLOG("sanity check: removing swallows specification from split container\n");
83             while (!TAILQ_EMPTY(&(json_node->swallow_head))) {
84                 Match *match = TAILQ_FIRST(&(json_node->swallow_head));
85                 TAILQ_REMOVE(&(json_node->swallow_head), match, matches);
86                 match_free(match);
87             }
88         }
89
90         if (json_node->type == CT_WORKSPACE) {
91             /* Ensure the workspace has a name. */
92             DLOG("Attaching workspace. name = %s\n", json_node->name);
93             if (json_node->name == NULL || strcmp(json_node->name, "") == 0) {
94                 json_node->name = sstrdup("unnamed");
95             }
96
97             /* Prevent name clashes when appending a workspace, e.g. when the
98              * user tries to restore a workspace called “1” but already has a
99              * workspace called “1”. */
100             Con *output;
101             Con *workspace = NULL;
102             TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
103             GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
104             char *base = sstrdup(json_node->name);
105             int cnt = 1;
106             while (workspace != NULL) {
107                 FREE(json_node->name);
108                 sasprintf(&(json_node->name), "%s_%d", base, cnt++);
109                 workspace = NULL;
110                 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
111                 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, json_node->name));
112             }
113             free(base);
114
115             /* Set num accordingly so that i3bar will properly sort it. */
116             json_node->num = ws_name_to_number(json_node->name);
117         }
118
119         // When appending JSON layout files that only contain the workspace
120         // _contents_, we might not have an upfront signal that the
121         // container we’re currently parsing is a floating container (like
122         // the “floating_nodes” key of the workspace container itself).
123         // That’s why we make sure the con is attached at the right place
124         // in the hierarchy in case it’s floating.
125         if (json_node->type == CT_FLOATING_CON) {
126             DLOG("fixing parent which currently is %p / %s\n", json_node->parent, json_node->parent->name);
127             json_node->parent = con_get_workspace(json_node->parent);
128
129             // Also set a size if none was supplied, otherwise the placeholder
130             // window cannot be created as X11 requests with width=0 or
131             // height=0 are invalid.
132             const Rect zero = {0, 0, 0, 0};
133             if (memcmp(&(json_node->rect), &zero, sizeof(Rect)) == 0) {
134                 DLOG("Geometry not set, combining children\n");
135                 Con *child;
136                 TAILQ_FOREACH(child, &(json_node->nodes_head), nodes) {
137                     DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
138                     json_node->rect.width += child->geometry.width;
139                     json_node->rect.height = max(json_node->rect.height, child->geometry.height);
140                 }
141             }
142
143             floating_check_size(json_node);
144         }
145
146         LOG("attaching\n");
147         con_attach(json_node, json_node->parent, true);
148         LOG("Creating window\n");
149         x_con_init(json_node, json_node->depth);
150         json_node = json_node->parent;
151     }
152
153     parsing_rect = false;
154     parsing_deco_rect = false;
155     parsing_window_rect = false;
156     parsing_geometry = false;
157     return 1;
158 }
159
160 static int json_end_array(void *ctx) {
161     LOG("end of array\n");
162     if (!parsing_swallows && !parsing_focus) {
163         con_fix_percent(json_node);
164     }
165     if (parsing_swallows) {
166         parsing_swallows = false;
167     }
168     if (parsing_focus) {
169         /* Clear the list of focus mappings */
170         struct focus_mapping *mapping;
171         TAILQ_FOREACH_REVERSE(mapping, &focus_mappings, focus_mappings_head, focus_mappings) {
172             LOG("focus (reverse) %d\n", mapping->old_id);
173             Con *con;
174             TAILQ_FOREACH(con, &(json_node->focus_head), focused) {
175                 if (con->old_id != mapping->old_id)
176                     continue;
177                 LOG("got it! %p\n", con);
178                 /* Move this entry to the top of the focus list. */
179                 TAILQ_REMOVE(&(json_node->focus_head), con, focused);
180                 TAILQ_INSERT_HEAD(&(json_node->focus_head), con, focused);
181                 break;
182             }
183         }
184         while (!TAILQ_EMPTY(&focus_mappings)) {
185             mapping = TAILQ_FIRST(&focus_mappings);
186             TAILQ_REMOVE(&focus_mappings, mapping, focus_mappings);
187             free(mapping);
188         }
189         parsing_focus = false;
190     }
191     return 1;
192 }
193
194 static int json_key(void *ctx, const unsigned char *val, size_t len) {
195     LOG("key: %.*s\n", (int)len, val);
196     FREE(last_key);
197     last_key = scalloc(len + 1, 1);
198     memcpy(last_key, val, len);
199     if (strcasecmp(last_key, "swallows") == 0)
200         parsing_swallows = true;
201
202     if (strcasecmp(last_key, "rect") == 0)
203         parsing_rect = true;
204
205     if (strcasecmp(last_key, "deco_rect") == 0)
206         parsing_deco_rect = true;
207
208     if (strcasecmp(last_key, "window_rect") == 0)
209         parsing_window_rect = true;
210
211     if (strcasecmp(last_key, "geometry") == 0)
212         parsing_geometry = true;
213
214     if (strcasecmp(last_key, "focus") == 0)
215         parsing_focus = true;
216
217     return 1;
218 }
219
220 static int json_string(void *ctx, const unsigned char *val, size_t len) {
221     LOG("string: %.*s for key %s\n", (int)len, val, last_key);
222     if (parsing_swallows) {
223         char *sval;
224         sasprintf(&sval, "%.*s", len, val);
225         if (strcasecmp(last_key, "class") == 0) {
226             current_swallow->class = regex_new(sval);
227         } else if (strcasecmp(last_key, "instance") == 0) {
228             current_swallow->instance = regex_new(sval);
229         } else if (strcasecmp(last_key, "window_role") == 0) {
230             current_swallow->window_role = regex_new(sval);
231         } else if (strcasecmp(last_key, "title") == 0) {
232             current_swallow->title = regex_new(sval);
233         } else {
234             ELOG("swallow key %s unknown\n", last_key);
235         }
236         free(sval);
237     } else {
238         if (strcasecmp(last_key, "name") == 0) {
239             json_node->name = scalloc(len + 1, 1);
240             memcpy(json_node->name, val, len);
241         } else if (strcasecmp(last_key, "sticky_group") == 0) {
242             json_node->sticky_group = scalloc(len + 1, 1);
243             memcpy(json_node->sticky_group, val, len);
244             LOG("sticky_group of this container is %s\n", json_node->sticky_group);
245         } else if (strcasecmp(last_key, "orientation") == 0) {
246             /* Upgrade path from older versions of i3 (doing an inplace restart
247              * to a newer version):
248              * "orientation" is dumped before "layout". Therefore, we store
249              * whether the orientation was horizontal or vertical in the
250              * last_split_layout. When we then encounter layout == "default",
251              * we will use the last_split_layout as layout instead. */
252             char *buf = NULL;
253             sasprintf(&buf, "%.*s", (int)len, val);
254             if (strcasecmp(buf, "none") == 0 ||
255                 strcasecmp(buf, "horizontal") == 0)
256                 json_node->last_split_layout = L_SPLITH;
257             else if (strcasecmp(buf, "vertical") == 0)
258                 json_node->last_split_layout = L_SPLITV;
259             else
260                 LOG("Unhandled orientation: %s\n", buf);
261             free(buf);
262         } else if (strcasecmp(last_key, "border") == 0) {
263             char *buf = NULL;
264             sasprintf(&buf, "%.*s", (int)len, val);
265             if (strcasecmp(buf, "none") == 0)
266                 json_node->border_style = BS_NONE;
267             else if (strcasecmp(buf, "1pixel") == 0) {
268                 json_node->border_style = BS_PIXEL;
269                 json_node->current_border_width = 1;
270             } else if (strcasecmp(buf, "pixel") == 0)
271                 json_node->border_style = BS_PIXEL;
272             else if (strcasecmp(buf, "normal") == 0)
273                 json_node->border_style = BS_NORMAL;
274             else
275                 LOG("Unhandled \"border\": %s\n", buf);
276             free(buf);
277         } else if (strcasecmp(last_key, "type") == 0) {
278             char *buf = NULL;
279             sasprintf(&buf, "%.*s", (int)len, val);
280             if (strcasecmp(buf, "root") == 0)
281                 json_node->type = CT_ROOT;
282             else if (strcasecmp(buf, "output") == 0)
283                 json_node->type = CT_OUTPUT;
284             else if (strcasecmp(buf, "con") == 0)
285                 json_node->type = CT_CON;
286             else if (strcasecmp(buf, "floating_con") == 0)
287                 json_node->type = CT_FLOATING_CON;
288             else if (strcasecmp(buf, "workspace") == 0)
289                 json_node->type = CT_WORKSPACE;
290             else if (strcasecmp(buf, "dockarea") == 0)
291                 json_node->type = CT_DOCKAREA;
292             else
293                 LOG("Unhandled \"type\": %s\n", buf);
294             free(buf);
295         } else if (strcasecmp(last_key, "layout") == 0) {
296             char *buf = NULL;
297             sasprintf(&buf, "%.*s", (int)len, val);
298             if (strcasecmp(buf, "default") == 0)
299                 /* This set above when we read "orientation". */
300                 json_node->layout = json_node->last_split_layout;
301             else if (strcasecmp(buf, "stacked") == 0)
302                 json_node->layout = L_STACKED;
303             else if (strcasecmp(buf, "tabbed") == 0)
304                 json_node->layout = L_TABBED;
305             else if (strcasecmp(buf, "dockarea") == 0)
306                 json_node->layout = L_DOCKAREA;
307             else if (strcasecmp(buf, "output") == 0)
308                 json_node->layout = L_OUTPUT;
309             else if (strcasecmp(buf, "splith") == 0)
310                 json_node->layout = L_SPLITH;
311             else if (strcasecmp(buf, "splitv") == 0)
312                 json_node->layout = L_SPLITV;
313             else
314                 LOG("Unhandled \"layout\": %s\n", buf);
315             free(buf);
316         } else if (strcasecmp(last_key, "workspace_layout") == 0) {
317             char *buf = NULL;
318             sasprintf(&buf, "%.*s", (int)len, val);
319             if (strcasecmp(buf, "default") == 0)
320                 json_node->workspace_layout = L_DEFAULT;
321             else if (strcasecmp(buf, "stacked") == 0)
322                 json_node->workspace_layout = L_STACKED;
323             else if (strcasecmp(buf, "tabbed") == 0)
324                 json_node->workspace_layout = L_TABBED;
325             else
326                 LOG("Unhandled \"workspace_layout\": %s\n", buf);
327             free(buf);
328         } else if (strcasecmp(last_key, "last_split_layout") == 0) {
329             char *buf = NULL;
330             sasprintf(&buf, "%.*s", (int)len, val);
331             if (strcasecmp(buf, "splith") == 0)
332                 json_node->last_split_layout = L_SPLITH;
333             else if (strcasecmp(buf, "splitv") == 0)
334                 json_node->last_split_layout = L_SPLITV;
335             else
336                 LOG("Unhandled \"last_splitlayout\": %s\n", buf);
337             free(buf);
338         } else if (strcasecmp(last_key, "mark") == 0) {
339             char *buf = NULL;
340             sasprintf(&buf, "%.*s", (int)len, val);
341             json_node->mark = buf;
342         } else if (strcasecmp(last_key, "floating") == 0) {
343             char *buf = NULL;
344             sasprintf(&buf, "%.*s", (int)len, val);
345             if (strcasecmp(buf, "auto_off") == 0)
346                 json_node->floating = FLOATING_AUTO_OFF;
347             else if (strcasecmp(buf, "auto_on") == 0)
348                 json_node->floating = FLOATING_AUTO_ON;
349             else if (strcasecmp(buf, "user_off") == 0)
350                 json_node->floating = FLOATING_USER_OFF;
351             else if (strcasecmp(buf, "user_on") == 0)
352                 json_node->floating = FLOATING_USER_ON;
353             free(buf);
354         } else if (strcasecmp(last_key, "scratchpad_state") == 0) {
355             char *buf = NULL;
356             sasprintf(&buf, "%.*s", (int)len, val);
357             if (strcasecmp(buf, "none") == 0)
358                 json_node->scratchpad_state = SCRATCHPAD_NONE;
359             else if (strcasecmp(buf, "fresh") == 0)
360                 json_node->scratchpad_state = SCRATCHPAD_FRESH;
361             else if (strcasecmp(buf, "changed") == 0)
362                 json_node->scratchpad_state = SCRATCHPAD_CHANGED;
363             free(buf);
364         }
365     }
366     return 1;
367 }
368
369 static int json_int(void *ctx, long long val) {
370     LOG("int %lld for key %s\n", val, last_key);
371     /* For backwards compatibility with i3 < 4.8 */
372     if (strcasecmp(last_key, "type") == 0)
373         json_node->type = val;
374
375     if (strcasecmp(last_key, "fullscreen_mode") == 0)
376         json_node->fullscreen_mode = val;
377
378     if (strcasecmp(last_key, "num") == 0)
379         json_node->num = val;
380
381     if (strcasecmp(last_key, "current_border_width") == 0)
382         json_node->current_border_width = val;
383
384     if (strcasecmp(last_key, "depth") == 0)
385         json_node->depth = val;
386
387     if (!parsing_swallows && strcasecmp(last_key, "id") == 0)
388         json_node->old_id = val;
389
390     if (parsing_focus) {
391         struct focus_mapping *focus_mapping = scalloc(1, sizeof(struct focus_mapping));
392         focus_mapping->old_id = val;
393         TAILQ_INSERT_TAIL(&focus_mappings, focus_mapping, focus_mappings);
394     }
395
396     if (parsing_rect || parsing_window_rect || parsing_geometry) {
397         Rect *r;
398         if (parsing_rect)
399             r = &(json_node->rect);
400         else if (parsing_window_rect)
401             r = &(json_node->window_rect);
402         else
403             r = &(json_node->geometry);
404         if (strcasecmp(last_key, "x") == 0)
405             r->x = val;
406         else if (strcasecmp(last_key, "y") == 0)
407             r->y = val;
408         else if (strcasecmp(last_key, "width") == 0)
409             r->width = val;
410         else if (strcasecmp(last_key, "height") == 0)
411             r->height = val;
412         else
413             ELOG("WARNING: unknown key %s in rect\n", last_key);
414         DLOG("rect now: (%d, %d, %d, %d)\n",
415              r->x, r->y, r->width, r->height);
416     }
417     if (parsing_swallows) {
418         if (strcasecmp(last_key, "id") == 0) {
419             current_swallow->id = val;
420         }
421         if (strcasecmp(last_key, "dock") == 0) {
422             current_swallow->dock = val;
423         }
424         if (strcasecmp(last_key, "insert_where") == 0) {
425             current_swallow->insert_where = val;
426         }
427     }
428
429     return 1;
430 }
431
432 static int json_bool(void *ctx, int val) {
433     LOG("bool %d for key %s\n", val, last_key);
434     if (strcasecmp(last_key, "focused") == 0 && val) {
435         to_focus = json_node;
436     }
437
438     if (parsing_swallows) {
439         if (strcasecmp(last_key, "restart_mode") == 0)
440             current_swallow->restart_mode = val;
441     }
442
443     return 1;
444 }
445
446 static int json_double(void *ctx, double val) {
447     LOG("double %f for key %s\n", val, last_key);
448     if (strcasecmp(last_key, "percent") == 0) {
449         json_node->percent = val;
450     }
451     return 1;
452 }
453
454 static json_content_t content_result;
455 static int content_level;
456
457 static int json_determine_content_deeper(void *ctx) {
458     content_level++;
459     return 1;
460 }
461
462 static int json_determine_content_shallower(void *ctx) {
463     content_level--;
464     return 1;
465 }
466
467 static int json_determine_content_string(void *ctx, const unsigned char *val, size_t len) {
468     if (strcasecmp(last_key, "type") != 0 || content_level > 1)
469         return 1;
470
471     DLOG("string = %.*s, last_key = %s\n", (int)len, val, last_key);
472     if (strncasecmp((const char *)val, "workspace", len) == 0)
473         content_result = JSON_CONTENT_WORKSPACE;
474     return 0;
475 }
476
477 /* Parses the given JSON file until it encounters the first “type” property to
478  * determine whether the file contains workspaces or regular containers, which
479  * is important to know when deciding where (and how) to append the contents.
480  * */
481 json_content_t json_determine_content(const char *filename) {
482     FILE *f;
483     if ((f = fopen(filename, "r")) == NULL) {
484         ELOG("Cannot open file \"%s\"\n", filename);
485         return JSON_CONTENT_UNKNOWN;
486     }
487     struct stat stbuf;
488     if (fstat(fileno(f), &stbuf) != 0) {
489         ELOG("Cannot fstat() \"%s\"\n", filename);
490         fclose(f);
491         return JSON_CONTENT_UNKNOWN;
492     }
493     char *buf = smalloc(stbuf.st_size);
494     int n = fread(buf, 1, stbuf.st_size, f);
495     if (n != stbuf.st_size) {
496         ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
497         fclose(f);
498         return JSON_CONTENT_UNKNOWN;
499     }
500     DLOG("read %d bytes\n", n);
501     // We default to JSON_CONTENT_CON because it is legal to not include
502     // “"type": "con"” in the JSON files for better readability.
503     content_result = JSON_CONTENT_CON;
504     content_level = 0;
505     yajl_gen g;
506     yajl_handle hand;
507     static yajl_callbacks callbacks = {
508         .yajl_string = json_determine_content_string,
509         .yajl_map_key = json_key,
510         .yajl_start_array = json_determine_content_deeper,
511         .yajl_start_map = json_determine_content_deeper,
512         .yajl_end_map = json_determine_content_shallower,
513         .yajl_end_array = json_determine_content_shallower,
514     };
515     g = yajl_gen_alloc(NULL);
516     hand = yajl_alloc(&callbacks, NULL, (void *)g);
517     /* Allowing comments allows for more user-friendly layout files. */
518     yajl_config(hand, yajl_allow_comments, true);
519     /* Allow multiple values, i.e. multiple nodes to attach */
520     yajl_config(hand, yajl_allow_multiple_values, true);
521     yajl_status stat;
522     setlocale(LC_NUMERIC, "C");
523     stat = yajl_parse(hand, (const unsigned char *)buf, n);
524     if (stat != yajl_status_ok && stat != yajl_status_client_canceled) {
525         unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
526         ELOG("JSON parsing error: %s\n", str);
527         yajl_free_error(hand, str);
528     }
529
530     setlocale(LC_NUMERIC, "");
531     yajl_complete_parse(hand);
532
533     fclose(f);
534
535     return content_result;
536 }
537
538 void tree_append_json(Con *con, const char *filename, char **errormsg) {
539     FILE *f;
540     if ((f = fopen(filename, "r")) == NULL) {
541         ELOG("Cannot open file \"%s\"\n", filename);
542         return;
543     }
544     struct stat stbuf;
545     if (fstat(fileno(f), &stbuf) != 0) {
546         ELOG("Cannot fstat() \"%s\"\n", filename);
547         fclose(f);
548         return;
549     }
550     char *buf = smalloc(stbuf.st_size);
551     int n = fread(buf, 1, stbuf.st_size, f);
552     if (n != stbuf.st_size) {
553         ELOG("File \"%s\" could not be read entirely, not loading.\n", filename);
554         fclose(f);
555         return;
556     }
557     DLOG("read %d bytes\n", n);
558     yajl_gen g;
559     yajl_handle hand;
560     static yajl_callbacks callbacks = {
561         .yajl_boolean = json_bool,
562         .yajl_integer = json_int,
563         .yajl_double = json_double,
564         .yajl_string = json_string,
565         .yajl_start_map = json_start_map,
566         .yajl_map_key = json_key,
567         .yajl_end_map = json_end_map,
568         .yajl_end_array = json_end_array,
569     };
570     g = yajl_gen_alloc(NULL);
571     hand = yajl_alloc(&callbacks, NULL, (void *)g);
572     /* Allowing comments allows for more user-friendly layout files. */
573     yajl_config(hand, yajl_allow_comments, true);
574     /* Allow multiple values, i.e. multiple nodes to attach */
575     yajl_config(hand, yajl_allow_multiple_values, true);
576     yajl_status stat;
577     json_node = con;
578     to_focus = NULL;
579     parsing_swallows = false;
580     parsing_rect = false;
581     parsing_deco_rect = false;
582     parsing_window_rect = false;
583     parsing_geometry = false;
584     parsing_focus = false;
585     setlocale(LC_NUMERIC, "C");
586     stat = yajl_parse(hand, (const unsigned char *)buf, n);
587     if (stat != yajl_status_ok) {
588         unsigned char *str = yajl_get_error(hand, 1, (const unsigned char *)buf, n);
589         ELOG("JSON parsing error: %s\n", str);
590         if (errormsg != NULL)
591             *errormsg = sstrdup((const char *)str);
592         yajl_free_error(hand, str);
593     }
594
595     /* In case not all containers were restored, we need to fix the
596      * percentages, otherwise i3 will crash immediately when rendering the
597      * next time. */
598     con_fix_percent(con);
599
600     setlocale(LC_NUMERIC, "");
601     yajl_complete_parse(hand);
602
603     fclose(f);
604     if (to_focus)
605         con_focus(to_focus);
606 }