+- (int)fromBackendUntrusted:(const char *)data len:(int)len
+{
+ return term_data_untrusted(term, data, len);
+}
+
+- (void)startAlert:(NSAlert *)alert
+ withCallback:(void (*)(void *, int))callback andCtx:(void *)ctx
+{
+ if (alert_ctx || alert_qhead) {
+ /*
+ * Queue this alert to be shown later.
+ */
+ struct alert_queue *qitem = snew(struct alert_queue);
+ qitem->next = NULL;
+ qitem->alert = alert;
+ qitem->callback = callback;
+ qitem->ctx = ctx;
+ if (alert_qtail)
+ alert_qtail->next = qitem;
+ else
+ alert_qhead = qitem;
+ alert_qtail = qitem;
+ } else {
+ alert_callback = callback;
+ alert_ctx = ctx; /* NB this is assumed to need freeing! */
+ [alert beginSheetModalForWindow:self modalDelegate:self
+ didEndSelector:@selector(alertSheetDidEnd:returnCode:contextInfo:)
+ contextInfo:NULL];
+ }
+}
+
+- (void)alertSheetDidEnd:(NSAlert *)alert returnCode:(int)returnCode
+ contextInfo:(void *)contextInfo
+{
+ [self performSelectorOnMainThread:
+ @selector(alertSheetDidFinishEnding:)
+ withObject:[NSNumber numberWithInt:returnCode]
+ waitUntilDone:NO];
+}
+
+- (void)alertSheetDidFinishEnding:(id)object
+{
+ int returnCode = [object intValue];
+
+ alert_callback(alert_ctx, returnCode); /* transfers ownership of ctx */
+
+ /*
+ * If there's an alert in our queue (either already or because
+ * the callback just queued it), start it.
+ */
+ if (alert_qhead) {
+ struct alert_queue *qnext;
+
+ alert_callback = alert_qhead->callback;
+ alert_ctx = alert_qhead->ctx;
+ [alert_qhead->alert beginSheetModalForWindow:self modalDelegate:self
+ didEndSelector:@selector(alertSheetDidEnd:returnCode:contextInfo:)
+ contextInfo:NULL];
+
+ qnext = alert_qhead->next;
+ sfree(alert_qhead);
+ alert_qhead = qnext;
+ if (!qnext)
+ alert_qtail = NULL;
+ } else {
+ alert_ctx = NULL;
+ }
+}
+
+- (void)notifyRemoteExit
+{
+ int exitcode;
+
+ if (!exited && (exitcode = back->exitcode(backhandle)) >= 0)
+ [self endSession:(exitcode == 0)];
+}
+
+- (void)endSession:(int)clean
+{
+ exited = TRUE;
+ if (ldisc) {
+ ldisc_free(ldisc);
+ ldisc = NULL;
+ }
+ if (back) {
+ back->free(backhandle);
+ backhandle = NULL;
+ back = NULL;
+ //FIXME: update specials menu;
+ }
+ if (cfg.close_on_exit == FORCE_ON ||
+ (cfg.close_on_exit == AUTO && clean))
+ [self close];
+ // FIXME: else show restart menu item
+}
+
+- (Terminal *)term
+{
+ return term;
+}
+