+#if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
+int yosys_history_offset = 0;
+std::string yosys_history_file;
+#endif
+
+void yosys_atexit()
+{
+#if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
+ if (!yosys_history_file.empty()) {
+#if defined(YOSYS_ENABLE_READLINE)
+ if (yosys_history_offset > 0) {
+ history_truncate_file(yosys_history_file.c_str(), 100);
+ append_history(where_history() - yosys_history_offset, yosys_history_file.c_str());
+ } else
+ write_history(yosys_history_file.c_str());
+#else
+ write_history(yosys_history_file.c_str());
+#endif
+ }
+
+ clear_history();
+#if defined(YOSYS_ENABLE_READLINE)
+ HIST_ENTRY **hist_list = history_list();
+ if (hist_list != NULL)
+ free(hist_list);
+#endif
+#endif
+}
+