]> git.sur5r.net Git - c128-kasse/blobdiff - src/print.c
Do not crash when format_euro() fails
[c128-kasse] / src / print.c
index 0ca142294e8cc118d59f7d6fa0fdd6ab9152ec54..a0e63df655aed53ab9b2d2be68a6ec2fcefb0108 100644 (file)
@@ -10,8 +10,9 @@
 #include <cbm.h>
 
 #include "general.h"
-#define _IS_PRINT
+#define IS_PRINT_C
 #include "print.h"
+#include "globals.h"
 
 /* NOTE: undocumented function which scratches files
    We need to use this function because linking unistd.h
@@ -28,45 +29,66 @@ int log_heap_flushed = 0;
 const int LOG_SIZE = 8192;
 
 void init_log(void) {
-       log_heap_buf = malloc(sizeof(char) * LOG_SIZE);
-       if (log_heap_buf == NULL) {
-               cprintf("malloc(log_heap_buf) failed");
-               exit(1);
-       }
+  log_heap_buf = malloc(sizeof(char) * LOG_SIZE);
+  if (log_heap_buf == NULL) {
+    cprintf("malloc(log_heap_buf) failed");
+    exit(1);
+  }
 }
 
 void print_the_buffer(void) {
-       BYTE c;
+  BYTE status;
+  char *c;
+
+  if (!printing)
+    return;
+
 RETRY:
-       c = cbm_open((BYTE)4, (BYTE)4, (BYTE)0, NULL);
-       if (c != 0) {
-               c128_perror(c, "cbm_open(printer)");
-               if (retry_or_quit() == 'q')
-                       exit(1);
-
-               goto RETRY;
-       }
-       c = cbm_write((BYTE)4, print_buffer, strlen(print_buffer));
-       if (c != strlen(print_buffer)) {
-               c128_perror(c, "write(printer)");
-               if (retry_or_quit() == 'q')
-                       exit(1);
-               goto RETRY;
-       }
-       cbm_close((BYTE)4);
-       log_file(print_buffer);
+  status = cbm_open((BYTE)4, (BYTE)4, (BYTE)0, NULL);
+  if (status != 0) {
+    c128_perror(status, "cbm_open(printer)");
+
+    do {
+      cprintf("\r\nr)etry, c)ontinue or q)uit?\r\n");
+      c = get_input();
+    } while ((*c != 'r') && (*c != 'c') && (*c != 'q'));
+
+    if (*c == 'q')
+      exit(1);
+    else if (*c == 'c') {
+      printing = 0;
+      return;
+    }
+
+    goto RETRY;
+  }
+  status = cbm_write((BYTE)4, print_buffer, strlen(print_buffer));
+  if (status != strlen(print_buffer)) {
+    c128_perror(status, "write(printer)");
+    if (retry_or_quit() == 'q')
+      exit(1);
+    goto RETRY;
+  }
+  cbm_close((BYTE)4);
+  log_file(print_buffer);
 }
 
 void print_header(void) {
-       sprintf(print_buffer, "%c--------------------------------------------------------------------------------\r", 17);
-       print_the_buffer();
-
-       sprintf(print_buffer, "%c#kauf Uhrzeit  - Ding      -     Preis - Restguthbn - # - Nickname\r", 17);
-       print_the_buffer();
-
-       sprintf(print_buffer, "%c--------------------------------------------------------------------------------\r", 17);
-       print_the_buffer();
-
+  sprintf(print_buffer, "%c----------------------------------------------------"
+                        "----------------------------\r",
+          17);
+  print_the_buffer();
+
+  sprintf(
+      print_buffer,
+      "%c#kauf Uhrzeit  - Ding      -     Preis - Restguthbn - # - Nickname\r",
+      17);
+  print_the_buffer();
+
+  sprintf(print_buffer, "%c----------------------------------------------------"
+                        "----------------------------\r",
+          17);
+  print_the_buffer();
 }
 
 /*
@@ -75,28 +97,30 @@ void print_header(void) {
  *
  */
 void log_flush(void) {
-       int c;
-       static char filename[8];
-       sprintf(filename, "log-%d", log_num);
-
-       /* If we have written to this logfile before, we need to remove it first */
-       if (log_heap_flushed > 0)
-               _sysremove(filename);
-
-       if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)1, filename)) != 0) {
-               c128_perror(c, "cbm_open(log)");
-               exit(1);
-       }
-       c = cbm_write((BYTE)8, log_heap_buf, log_heap_offset);
-       if (c != log_heap_offset) {
-               textcolor(TC_LIGHT_RED);
-               cprintf("\r\nCould not save logfile (wrote %d bytes, wanted %d bytes), please make sure the floppy is not full!\n", c, log_heap_offset);
-               c128_perror(c, "cbm_write");
-               exit(1);
-       }
-       cbm_close((BYTE)8);
-
-       log_heap_flushed = log_heap_offset;
+  int c;
+  static char filename[8];
+  sprintf(filename, "log-%d", log_num);
+
+  /* If we have written to this logfile before, we need to remove it first */
+  if (log_heap_flushed > 0)
+    _sysremove(filename);
+
+  if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)1, filename)) != 0) {
+    c128_perror(c, "cbm_open(log)");
+    exit(1);
+  }
+  c = cbm_write((BYTE)8, log_heap_buf, log_heap_offset);
+  if (c != log_heap_offset) {
+    textcolor(TC_LIGHT_RED);
+    cprintf("\r\nCould not save logfile (wrote %d bytes, wanted %d bytes), "
+            "please make sure the floppy is not full!\n",
+            c, log_heap_offset);
+    c128_perror(c, "cbm_write");
+    exit(1);
+  }
+  cbm_close((BYTE)8);
+
+  log_heap_flushed = log_heap_offset;
 }
 
 /*
@@ -105,14 +129,14 @@ void log_flush(void) {
  *
  */
 void log_file(const char *s) {
-       strcpy(log_heap_buf+log_heap_offset, s);
-       log_heap_offset += strlen(s);
-
-       /* Force a flush when there are only five lines left */
-       if (log_heap_offset > (LOG_SIZE - (5 * 80))) {
-               log_flush();
-               log_num++;
-               log_heap_offset = 0;
-               log_heap_flushed = 0;
-       }
+  strcpy(log_heap_buf + log_heap_offset, s);
+  log_heap_offset += strlen(s);
+
+  /* Force a flush when there are only five lines left */
+  if (log_heap_offset > (LOG_SIZE - (5 * 80))) {
+    log_flush();
+    log_num++;
+    log_heap_offset = 0;
+    log_heap_flushed = 0;
+  }
 }