-
Notifications
You must be signed in to change notification settings - Fork 185
/
textprint.c
129 lines (107 loc) · 2.93 KB
/
textprint.c
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
#include "print.h"
#include "charset.h"
#include "winpriv.h" // win_prefix_title, win_unprefix_title
#include <fcntl.h>
#include <pwd.h>
static wstring printer = 0;
static char * pf;
static int pd;
static const wchar BOM = 0xFEFF;
static uint np = 0;
static struct passwd * pw;
void
printer_start_job(wstring printer_name)
{
char * tempdir = tmpdir();
char * user = getenv("USER");
if (!user)
user = getenv("USERNAME");
if (!user) {
pw = getpwuid(getuid());
if (pw)
user = pw->pw_name;
else
user = "";
}
char pid[11];
sprintf(pid, "%d", getpid());
char n[11];
sprintf(n, "%d", np);
// compose $tempdir/mintty.print.$USER.$$
char * pref = "mintty-print.";
pf = malloc(strlen(tempdir) strlen(pref) strlen(user) strlen(pid) strlen(n) 7);
sprintf(pf, "%s/%s%s.%s-%s.prn", tempdir, pref, user, pid, n);
pd = open(pf, O_CREAT | O_TRUNC | O_BINARY | O_WRONLY, 0600);
if (pd >= 0) {
win_prefix_title(_W("[Printing...] "));
printer = printer_name;
write(pd, &BOM, 2);
}
}
void
printer_wwrite(wchar * wdata, uint len)
{
if (printer) {
void * wbuf = wdata;
uint wlen = len * sizeof(wchar);
uint n;
while ((n = write(pd, wbuf, wlen)) > 0) {
wbuf = n;
wlen -= n;
}
}
}
void
printer_write(char * data, uint len)
{
if (printer) {
char * buf = malloc(len 1);
for (uint i = 0; i < len; i ) {
if (data[i])
buf[i] = data[i];
else
buf[i] = ' ';
}
buf[len] = '\0';
wchar * wdata = cs__mbstowcs(buf);
write(pd, wdata, wcslen(wdata) * sizeof(wchar));
free(buf);
free(wdata);
}
}
void
printer_finish_job(void)
{
if (printer) {
close(pd);
// get rid of \ in case tmpdir was derived from %LOCALAPPDATA%
for (char * fi = pf; *fi; fi )
if (*fi == '\\')
*fi = '/';
char * wf = path_posix_to_win_a(pf);
char * pn = cs__wcstoutf(printer);
strcpy(strchr(pf, '\0') - 4, ".cmd");
int cmdfile = open(pf, O_CREAT | O_TRUNC | O_BINARY | O_WRONLY, 0700);
// chcp 65001 tricks Windows into accepting UTF-8 printer and file names
// but it needs to be restored
// retrieve current value of `chcp`
// which is not necessarily the same as GetOEMCP() !
FILE * chcpcom = popen("$SYSTEMROOT/System32/chcp.com | /bin/sed -e 's,.*:,,' -e 's, ,,'", "r");
char line[99];
fgets(line, sizeof line, chcpcom);
pclose(chcpcom);
int chcp = atoi(line);
char * cmdformat = "@%%SYSTEMROOT%%\\System32\\chcp 65001 > nul:\r\n@start /min %%SYSTEMROOT%%\\notepad /w /pt \"%s\" \"%s\"\r\n@%%SYSTEMROOT%%\\System32\\chcp %d > nul:";
char cmd[strlen(cmdformat) - 6 strlen(wf) strlen(pn) 22 1];
sprintf(cmd, cmdformat, wf, pn, chcp);
write(cmdfile, cmd, strlen(cmd));
close(cmdfile);
//printf("system (%s)\n", pf);
system(pf);
free(wf);
free(pn);
free(pf);
printer = 0;
win_unprefix_title(_W("[Printing...] "));
}
}