mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 01:18:00 +00:00
20f818af12
I mentioned recently (in commit 9e7d4c53d8
) message that I'm no
longer fond of the variable name 'ret', because it's used in two quite
different contexts: it's the return value from a subroutine you just
called (e.g. 'int ret = read(fd, buf, len);' and then check for error
or EOF), or it's the value you're preparing to return from the
_containing_ routine (maybe by assigning it a default value and then
conditionally modifying it, or by starting at NULL and reallocating,
or setting it just before using the 'goto out' cleanup idiom). In the
past I've occasionally made mistakes by forgetting which meaning the
variable had, or accidentally conflating both uses.
If all else fails, I now prefer 'retd' (short for 'returned') in the
former situation, and 'toret' (obviously, the value 'to return') in
the latter case. But even better is to pick a name that actually says
something more specific about what the thing actually is.
One particular bad habit throughout this codebase is to have a set of
functions that deal with some object type (say 'Foo'), all *but one*
of which take a 'Foo *foo' parameter, but the foo_new() function
starts with 'Foo *ret = snew(Foo)'. If all the rest of them think the
canonical name for the ambient Foo is 'foo', so should foo_new()!
So here's a no-brainer start on cutting down on the uses of 'ret': I
looked for all the cases where it was being assigned the result of an
allocation, and renamed the variable to be a description of the thing
being allocated. In the case of a new() function belonging to a
family, I picked the same name as the rest of the functions in its own
family, for consistency. In other cases I picked something sensible.
One case where it _does_ make sense not to use your usual name for the
variable type is when you're cloning an existing object. In that case,
_neither_ of the Foo objects involved should be called 'foo', because
it's ambiguous! They should be named so you can see which is which. In
the two cases I found here, I've called them 'orig' and 'copy'.
As in the previous refactoring, many thanks to clang-rename for the
help.
229 lines
6.0 KiB
C
229 lines
6.0 KiB
C
/*
|
|
* Printing interface for PuTTY.
|
|
*/
|
|
|
|
#include "putty.h"
|
|
#include <winspool.h>
|
|
|
|
struct printer_enum_tag {
|
|
int nprinters;
|
|
DWORD enum_level;
|
|
union {
|
|
LPPRINTER_INFO_4 i4;
|
|
LPPRINTER_INFO_5 i5;
|
|
} info;
|
|
};
|
|
|
|
struct printer_job_tag {
|
|
HANDLE hprinter;
|
|
};
|
|
|
|
DECL_WINDOWS_FUNCTION(static, BOOL, EnumPrinters,
|
|
(DWORD, LPTSTR, DWORD, LPBYTE, DWORD, LPDWORD, LPDWORD));
|
|
DECL_WINDOWS_FUNCTION(static, BOOL, OpenPrinter,
|
|
(LPTSTR, LPHANDLE, LPPRINTER_DEFAULTS));
|
|
DECL_WINDOWS_FUNCTION(static, BOOL, ClosePrinter, (HANDLE));
|
|
DECL_WINDOWS_FUNCTION(static, DWORD, StartDocPrinter, (HANDLE, DWORD, LPBYTE));
|
|
DECL_WINDOWS_FUNCTION(static, BOOL, EndDocPrinter, (HANDLE));
|
|
DECL_WINDOWS_FUNCTION(static, BOOL, StartPagePrinter, (HANDLE));
|
|
DECL_WINDOWS_FUNCTION(static, BOOL, EndPagePrinter, (HANDLE));
|
|
DECL_WINDOWS_FUNCTION(static, BOOL, WritePrinter,
|
|
(HANDLE, LPVOID, DWORD, LPDWORD));
|
|
|
|
static void init_winfuncs(void)
|
|
{
|
|
static bool initialised = false;
|
|
if (initialised)
|
|
return;
|
|
{
|
|
HMODULE winspool_module = load_system32_dll("winspool.drv");
|
|
/* Some MSDN documentation claims that some of the below functions
|
|
* should be loaded from spoolss.dll, but this doesn't seem to
|
|
* be reliable in practice.
|
|
* Nevertheless, we load spoolss.dll ourselves using our safe
|
|
* loading method, against the possibility that winspool.drv
|
|
* later loads it unsafely. */
|
|
(void) load_system32_dll("spoolss.dll");
|
|
GET_WINDOWS_FUNCTION_PP(winspool_module, EnumPrinters);
|
|
GET_WINDOWS_FUNCTION_PP(winspool_module, OpenPrinter);
|
|
GET_WINDOWS_FUNCTION_PP(winspool_module, ClosePrinter);
|
|
GET_WINDOWS_FUNCTION_PP(winspool_module, StartDocPrinter);
|
|
GET_WINDOWS_FUNCTION_PP(winspool_module, EndDocPrinter);
|
|
GET_WINDOWS_FUNCTION_PP(winspool_module, StartPagePrinter);
|
|
GET_WINDOWS_FUNCTION_PP(winspool_module, EndPagePrinter);
|
|
GET_WINDOWS_FUNCTION_PP(winspool_module, WritePrinter);
|
|
}
|
|
initialised = true;
|
|
}
|
|
|
|
static bool printer_add_enum(int param, DWORD level, char **buffer,
|
|
int offset, int *nprinters_ptr)
|
|
{
|
|
DWORD needed = 0, nprinters = 0;
|
|
|
|
init_winfuncs();
|
|
|
|
*buffer = sresize(*buffer, offset+512, char);
|
|
|
|
/*
|
|
* Exploratory call to EnumPrinters to determine how much space
|
|
* we'll need for the output.
|
|
*
|
|
* If we get ERROR_INSUFFICIENT_BUFFER, that's fine, we're
|
|
* prepared to deal with it. Any other error, we return failure.
|
|
*/
|
|
if (p_EnumPrinters(param, NULL, level, (LPBYTE)((*buffer)+offset), 512,
|
|
&needed, &nprinters) == 0 &&
|
|
GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
|
return false;
|
|
|
|
if (needed < 512)
|
|
needed = 512;
|
|
|
|
*buffer = sresize(*buffer, offset+needed, char);
|
|
|
|
if (p_EnumPrinters(param, NULL, level, (LPBYTE)((*buffer)+offset),
|
|
needed, &needed, &nprinters) == 0)
|
|
return false;
|
|
|
|
*nprinters_ptr += nprinters;
|
|
|
|
return true;
|
|
}
|
|
|
|
printer_enum *printer_start_enum(int *nprinters_ptr)
|
|
{
|
|
printer_enum *pe = snew(printer_enum);
|
|
char *buffer = NULL;
|
|
|
|
*nprinters_ptr = 0; /* default return value */
|
|
buffer = snewn(512, char);
|
|
|
|
/*
|
|
* Determine what enumeration level to use.
|
|
* When enumerating printers, we need to use PRINTER_INFO_4 on
|
|
* NT-class systems to avoid Windows looking too hard for them and
|
|
* slowing things down; and we need to avoid PRINTER_INFO_5 as
|
|
* we've seen network printers not show up.
|
|
* On 9x-class systems, PRINTER_INFO_4 isn't available and
|
|
* PRINTER_INFO_5 is recommended.
|
|
* Bletch.
|
|
*/
|
|
if (osPlatformId != VER_PLATFORM_WIN32_NT) {
|
|
pe->enum_level = 5;
|
|
} else {
|
|
pe->enum_level = 4;
|
|
}
|
|
|
|
if (!printer_add_enum(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
|
|
pe->enum_level, &buffer, 0, nprinters_ptr))
|
|
goto error;
|
|
|
|
switch (pe->enum_level) {
|
|
case 4:
|
|
pe->info.i4 = (LPPRINTER_INFO_4)buffer;
|
|
break;
|
|
case 5:
|
|
pe->info.i5 = (LPPRINTER_INFO_5)buffer;
|
|
break;
|
|
}
|
|
pe->nprinters = *nprinters_ptr;
|
|
|
|
return pe;
|
|
|
|
error:
|
|
sfree(buffer);
|
|
sfree(pe);
|
|
*nprinters_ptr = 0;
|
|
return NULL;
|
|
}
|
|
|
|
char *printer_get_name(printer_enum *pe, int i)
|
|
{
|
|
if (!pe)
|
|
return NULL;
|
|
if (i < 0 || i >= pe->nprinters)
|
|
return NULL;
|
|
switch (pe->enum_level) {
|
|
case 4:
|
|
return pe->info.i4[i].pPrinterName;
|
|
case 5:
|
|
return pe->info.i5[i].pPrinterName;
|
|
default:
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
void printer_finish_enum(printer_enum *pe)
|
|
{
|
|
if (!pe)
|
|
return;
|
|
switch (pe->enum_level) {
|
|
case 4:
|
|
sfree(pe->info.i4);
|
|
break;
|
|
case 5:
|
|
sfree(pe->info.i5);
|
|
break;
|
|
}
|
|
sfree(pe);
|
|
}
|
|
|
|
printer_job *printer_start_job(char *printer)
|
|
{
|
|
printer_job *pj = snew(printer_job);
|
|
DOC_INFO_1 docinfo;
|
|
bool jobstarted = false, pagestarted = false;
|
|
|
|
init_winfuncs();
|
|
|
|
pj->hprinter = NULL;
|
|
if (!p_OpenPrinter(printer, &pj->hprinter, NULL))
|
|
goto error;
|
|
|
|
docinfo.pDocName = "PuTTY remote printer output";
|
|
docinfo.pOutputFile = NULL;
|
|
docinfo.pDatatype = "RAW";
|
|
|
|
if (!p_StartDocPrinter(pj->hprinter, 1, (LPBYTE)&docinfo))
|
|
goto error;
|
|
jobstarted = true;
|
|
|
|
if (!p_StartPagePrinter(pj->hprinter))
|
|
goto error;
|
|
pagestarted = true;
|
|
|
|
return pj;
|
|
|
|
error:
|
|
if (pagestarted)
|
|
p_EndPagePrinter(pj->hprinter);
|
|
if (jobstarted)
|
|
p_EndDocPrinter(pj->hprinter);
|
|
if (pj->hprinter)
|
|
p_ClosePrinter(pj->hprinter);
|
|
sfree(pj);
|
|
return NULL;
|
|
}
|
|
|
|
void printer_job_data(printer_job *pj, const void *data, size_t len)
|
|
{
|
|
DWORD written;
|
|
|
|
if (!pj)
|
|
return;
|
|
|
|
p_WritePrinter(pj->hprinter, (void *)data, len, &written);
|
|
}
|
|
|
|
void printer_finish_job(printer_job *pj)
|
|
{
|
|
if (!pj)
|
|
return;
|
|
|
|
p_EndPagePrinter(pj->hprinter);
|
|
p_EndDocPrinter(pj->hprinter);
|
|
p_ClosePrinter(pj->hprinter);
|
|
sfree(pj);
|
|
}
|