aboutsummaryrefslogtreecommitdiffstats
path: root/print.c
blob: b07a6e070676ee4be14a6fdd6252f1353665c2af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2004-2018 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Bostom MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * print.c
 *
 * abcprintd backend - also usable for abc80sim
 *
 */

#include "compiler.h"
#include "abcprintd.h"
#include "tempfile.h"

#ifdef __WIN32__
const char lpr_command[] = "powershell -command \"GetContent -raw -path '*' | OutPrinter\"";
#else
const char lpr_command[] = "lpr '*'";
#endif

static struct temp_file *temp;

static void print_finish(void)
{
  const char *p;
  char *cmd, *q;
  size_t cmdlen, namelen;

  if (!temp)
    return;

  fflush(temp->f);

  namelen = temp->namelen;
  cmdlen = 0;
  for (p = lpr_command; *p; p++) {
    cmdlen += (*p == '*') ? namelen : 1;
  }
  cmd = malloc(cmdlen+1);

  if (cmd) {
    for (p = lpr_command, q = cmd; *p; p++) {
      if (*p == '*') {
	memcpy(q, temp->filename, namelen);
	q += namelen;
      } else {
	*q++ = *p;
      }
    }
    *q = '\0';

    system(cmd);
    free(cmd);
  }
  close_temp(&temp);
}

static void output(unsigned char c)
{
  static const wchar_t abc_to_unicode[256] =
    L"\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
    L"\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
    L" !\"#¤%&\'()*+,-./0123456789:;<=>?"
    L"ÉABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÅÜ_"
    L"éabcdefghijklmnopqrstuvwxyzäöåü\x25a0"
    L"\x20ac\x25a1\x201a\x0192\x201e\x2026\x2020\x2021"
    L"\x02c6\x2030\x0160\x2039\x0152\x2190\x017d\x2192"
    L"\x2191\x2018\x2019\x201c\x201d\x2022\x2013\x2014"
    L"\x02dc\x2122\x0161\x203a\x0153\x2193\x017e\x0178"
    L"\240\241\242\243$\245\246\247\250\251\252\253\254\255\256\257"
    L"\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277"
    L"\300\301\302\303[]\306\307\310@\312\313\314\315\316\317"
    L"\320\321\322\323\324\325\\\327\330\331\332\333^\335\336\337"
    L"\340\341\342\343{}\346\347\350`\352\353\354\355\356\357"
    L"\360\361\362\363\364\365|\367\370\371\372\373~\375\376\377";

  if (!temp) {
    if (c < '\b' || (c > '\r' && c < 31))
      temp = temp_file(TF_BINARY);
    else
      temp = temp_file(TF_UNICODE);
  }

  if (temp->mode == TF_BINARY)
    putc(c, temp->f);
  else if (c != '\r')
    putwc(abc_to_unicode[c], temp->f);
}

enum input_state {
  is_normal,                  /* Normal operation */
  is_ff,                      /* 0xFF received */
  is_file,                    /* File operation in progress */
};
static enum input_state is;

static void cleanup_temp(void)
{
  /* Print job still active on exit, just delete it */
  close_temp(&temp);
}

void abcprint_init(void)
{
  is = is_normal;
  atexit(cleanup_temp);
}

void abcprint(const void *data, size_t len)
{
  const unsigned char *dp = data;
  unsigned char c;

  while (len--) {
    c = *dp++;

    switch ( is ) {
    case is_normal:
      if ( c == 0xff )
	is = is_ff;
      else
	output(c);
      break;

    case is_ff:
      if (c == 0) {
	/* End of job */
	print_finish();
	is = is_normal;
      } else if (c >= 0xa0 && c <= 0xbf) {
	/* Opcode range reserved for file ops */
	is = file_op(c) ? is_file : is_normal;
      } else {
	output(c);
	is = is_normal;
      }
      break;

    case is_file:
      is = file_op(c) ? is_file : is_normal;
      break;
    }
  }
}