summaryrefslogtreecommitdiff
path: root/support/dktools/dk3print.ctr
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/dktools/dk3print.ctr
Initial commit
Diffstat (limited to 'support/dktools/dk3print.ctr')
-rw-r--r--support/dktools/dk3print.ctr1297
1 files changed, 1297 insertions, 0 deletions
diff --git a/support/dktools/dk3print.ctr b/support/dktools/dk3print.ctr
new file mode 100644
index 0000000000..d979b0191d
--- /dev/null
+++ b/support/dktools/dk3print.ctr
@@ -0,0 +1,1297 @@
+%% options
+
+copyright owner = Dirk Krause
+copyright year = 2013-xxxx
+license = bsd
+
+
+%% header
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** Clean up details for Windows printer.
+ @param ptr Details to clean up.
+*/
+void
+dk3print_win_det_cleanup(dk3_printer_windows_details_t *ptr);
+
+/** Clean up details for LPRng/LPD printer.
+ @param ptr Details to clean up.
+*/
+void
+dk3print_lprng_det_cleanup(dk3_printer_lprng_details_t *ptr);
+
+/** Clean up details for socket printer.
+ @param ptr Details to clean up.
+*/
+void
+dk3print_socket_det_cleanup(dk3_printer_socket_t *ptr);
+
+/** Clean up type-specific details for a printer.
+ @param tp Printer type.
+ @param ptr Pointer to details data.
+*/
+void
+dk3print_det_cleanup(int tp, dk3_printer_details_t *ptr);
+
+/** Create new printer entry if necessary.
+ @param pc Print configuration to set up.
+ @param name Printer name.
+ @return Pointer to new or existing printer entry on success,
+ NULL on error.
+*/
+dk3_printer_t *
+dk3print_printer_new(dk3_print_conf_t *pc, dkChar const *name);
+
+/** Destroy printer entry in print configuration, release memory.
+ @param ptr Printer entry to destroy.
+*/
+void
+dk3print_printer_delete(dk3_printer_t *ptr);
+
+/** Destroy printer alias entry in print configuration, release memory.
+ @param ptr Alias to destroy.
+*/
+void
+dk3print_alias_delete(dk3_printer_alias_t *ptr);
+
+/** Create new print host entry if necessary.
+ @param pc Print configuration to set up.
+ @param name Print host name.
+ @return Pointer to new or existing printer entry on success,
+ NULL on error.
+*/
+dk3_print_host_t *
+dk3print_host_new(dk3_print_conf_t *pc, dkChar const *name);
+
+/** Destroy print host entry in print configuration, release memory.
+ @param ptr Print host entry to destroy.
+*/
+void
+dk3print_host_delete(dk3_print_host_t *ptr);
+
+/** Destroy print host alias entry in print configuration, release memory.
+ @param ptr Alias to destroy.
+*/
+void
+dk3print_host_alias_delete(dk3_print_host_alias_t *ptr);
+
+/** Destroy print configuration, release resources.
+ @param pc Print configuration to destroy.
+*/
+void
+dk3print_conf_delete(dk3_print_conf_t *pc);
+
+/** Create new print configuration, allocate resources for storages.
+ @param app Application structur, must not be NULL.
+ @return Pointer to new configuration on success, NULL on error.
+*/
+dk3_print_conf_t *
+dk3print_config_new(dk3_app_t *app);
+
+/** Create new print configuration, initialize it from configuration files.
+ @param app Application structure, must not be NULL.
+ @param useConfigFiles Flag: Use dk3print configuration files.
+ @return Pointer to new configuration on success, NULL on error.
+*/
+dk3_print_conf_t *
+dk3print_conf_open(dk3_app_t *app, int useConfigFiles);
+
+/** Close print configuration.
+ @param pc Print configuration to close.
+*/
+void
+dk3print_conf_close(dk3_print_conf_t *pc);
+
+/** Initialize details based on print queue type.
+ @param det Details to initialize.
+ @param tp Print queue type.
+*/
+void
+dk3print_set_det(dk3_printer_details_t *det, int tp);
+
+/** Find named printer (either directly or via alias).
+ @param pc Print configuration.
+ @param pn Printer name.
+ @return Pointer to printer on success, NULL on error (not found).
+*/
+dk3_printer_t *
+dk3print_get_printer(dk3_print_conf_t *pc, dkChar const *pn);
+
+/** Find named print host (either directly or via alias).
+ @param pc Print configuration.
+ @param hn Host name.
+ @return Pointer to print host on success, NULL on error (not found).
+*/
+dk3_print_host_t *
+dk3print_get_host(dk3_print_conf_t *pc, dkChar const *hn);
+
+#ifdef __cplusplus
+}
+#endif
+
+%% module
+
+
+
+#include "dk3all.h"
+#include "dk3print.h"
+#include "dk3prcfg.h"
+#include "dk3prreq.h"
+
+
+
+$!trace-include
+
+
+
+/** Keywords used by the module, not localized.
+*/
+static dkChar const * const dk3print_nl[] = {
+$!string-table macro=dkT
+#
+# 0: Name of configuration file.
+#
+dk3print.conf
+#
+# 1: String table file name.
+#
+dk3print.str
+#
+# 2: PS
+#
+PS
+#
+# 3: PostScript
+#
+PostScript
+#
+# 4: Driver name GhostScript PDF
+#
+Ghostscript PDF
+#
+# 5: Default PS level preference.
+#
+/print/ps/level
+#
+# 6: Conversion format for PS level.
+#
+%d
+$!end
+};
+
+
+
+/** Texts used by the module, normaly replaced by localized texts.
+*/
+static dkChar const * const dk3print_msg[] = {
+$!string-table macro=dkT
+#
+# 0
+#
+PRINTER:
+#
+# 1
+#
+PRIMARY CONFIGURATION:
+#
+# 2
+#
+SECONDARY CONFIGURATION:
+#
+# 3
+#
+Printer type:
+#
+# 4
+#
+Host:
+#
+# 5
+#
+Queue:
+#
+# 6
+#
+Port:
+#
+# 7
+#
+Connect timeout:
+#
+# 8
+#
+Send timeout:
+#
+# 9
+#
+Receive timeout:
+#
+# 10
+#
+PS level:
+#
+# 11
+#
+Default:
+#
+# 12
+#
+SNMP host:
+#
+# 13
+#
+SNMP community:
+#
+# 14
+#
+SNMP version:
+#
+# 15
+#
+Orderly release:
+#
+# 16
+#
+unknown
+#
+# 17
+#
+Windows
+#
+# 18
+#
+Socket
+#
+# 19
+#
+LPD
+#
+# 20
+#
+LPRng
+#
+# 21
+#
+yes
+#
+# 22
+#
+no
+#
+# 23 24 25 26
+#
+1
+2c
+2p
+3
+#
+# 27
+#
+Missing closing bracket ']'!
+#
+# 28 29
+#
+Keyword "
+" not allowed here, must be "host" or "printer"!
+#
+# 30
+#
+Missing name for printer or host!
+#
+# 31 32
+#
+Printer "
+" already exists!
+#
+# 33 34
+#
+Printer alias "
+" already exists!
+#
+# 35 36
+#
+Option "
+" requires an argument!
+#
+# 37
+#
+Queue type is already set!
+#
+# 38 39
+#
+Unknown print queue type "
+"!
+#
+# 40 41
+#
+Overwriting host name "
+"!
+#
+# 42 43
+#
+Option "
+" invalid for printer type!
+#
+# 44 45
+#
+Overwriting queue name "
+"!
+#
+# 46 47
+#
+Overwriting port number
+!
+#
+# 48
+#
+Numeric overflow!
+#
+# 49 50
+#
+Illegal PS level "
+", must be 2 or 3!
+#
+# 51 52
+#
+Overwriting SNMP host "
+"!
+#
+# 53 54
+#
+Overwriting SNMP community "
+"!
+#
+# 55
+#
+Overwriting SNMP version!
+#
+# 56 57
+#
+Unknown SNMP version "
+"!
+#
+# 58 59
+#
+Overwriting receive timeout "
+"!
+#
+# 60 61
+#
+Overwriting send timeout "
+"!
+#
+# 62 63
+#
+Overwriting connect timeout "
+"!
+#
+# 64 65
+#
+Unknown configuration option "
+"!
+#
+# 66 67
+#
+Print host "
+" already exists!
+#
+# 68 69
+#
+Print host alias "
+" already exists!
+#
+# 70 71
+#
+Illegal encoding "
+" for LPD protocol!
+#
+# 72
+#
+Line outside scope, not belonging to printer or host!
+$!end
+};
+
+
+
+void
+dk3print_win_det_cleanup(dk3_printer_windows_details_t *ptr)
+{
+ $? "+ dk3print_win_det_cleanup"
+ if(ptr) {
+ }
+ $? "- dk3print_win_det_cleanup"
+}
+
+
+
+void
+dk3print_lprng_det_cleanup(dk3_printer_lprng_details_t *ptr)
+{
+ $? "+ dk3print_lprng_det_cleanup"
+ if(ptr) {
+ dk3_release(ptr->hostname);
+ dk3_release(ptr->queuename);
+ }
+ $? "- dk3print_lprng_det_cleanup"
+}
+
+
+
+void
+dk3print_socket_det_cleanup(dk3_printer_socket_t *ptr)
+{
+ $? "+ dk3print_socket_det_cleanup"
+ if(ptr) {
+ dk3_release(ptr->hostname);
+ ptr->portno = 0;
+ ptr->ordrel = 0x00;
+ }
+ $? "- dk3print_socket_det_cleanup"
+}
+
+
+
+void
+dk3print_det_cleanup(int tp, dk3_printer_details_t *ptr)
+{
+ $? "+ dk3print_det_cleanup"
+ if(ptr) {
+ switch(tp) {
+ case DK3_PRINTER_TYPE_WINDOWS: {
+ dk3print_win_det_cleanup(&(ptr->win));
+ } break;
+ case DK3_PRINTER_TYPE_SOCKET: {
+ dk3print_socket_det_cleanup(&(ptr->sock));
+ } break;
+ case DK3_PRINTER_TYPE_LPD: {
+ dk3print_lprng_det_cleanup(&(ptr->lprng));
+ } break;
+ case DK3_PRINTER_TYPE_LPRNG: {
+ dk3print_lprng_det_cleanup(&(ptr->lprng));
+ } break;
+ }
+ }
+ $? "- dk3print_det_cleanup"
+}
+
+
+
+void
+dk3print_printer_delete(dk3_printer_t *ptr)
+{
+ $? "+ dk3print_printer_delete"
+ if(ptr) {
+ dk3print_det_cleanup(ptr->t_p, &(ptr->det_p));
+ dk3print_det_cleanup(ptr->t_s, &(ptr->det_s));
+ dk3_release(ptr->name);
+ dk3_release(ptr->h_snmp);
+ dk3_release(ptr->c_snmp);
+ dk3_delete(ptr);
+ } $? "- dk3print_printer_delete"
+}
+
+
+
+dk3_printer_t *
+dk3print_printer_new(dk3_print_conf_t *pc, dkChar const *name)
+{
+ dk3_printer_t *back = NULL;
+ if((pc) && (name)) {
+ back =
+ (dk3_printer_t *)dk3sto_it_find_like(pc->iPrinters, (void *)name, 1);
+ if(!(back)) {
+ back = dk3_new_app(dk3_printer_t,1,pc->app);
+ if(back) {
+ back->name = NULL;
+ back->h_snmp = NULL;
+ back->c_snmp = 0;
+ back->t_p = DK3_PRINTER_TYPE_NONE;
+ back->t_s = DK3_PRINTER_TYPE_NONE;
+ back->v_snmp = DK3_SNMP_VERSION_UNKNOWN;
+ back->ps = 0;
+ back->name = dk3str_dup_app(name, pc->app);
+ if(back->name) {
+ if(!dk3sto_add(pc->sPrinters, back)) {
+ dk3print_printer_delete(back);
+ back = NULL;
+ }
+ } else {
+ dk3print_printer_delete(back);
+ back = NULL;
+ }
+ }
+ }
+ if(back) {
+ if(!(pc->defPrinter)) {
+ pc->defPrinter = back;
+ }
+ }
+ }
+ return back;
+}
+
+
+
+void
+dk3print_alias_delete(dk3_printer_alias_t *ptr)
+{
+ $? "+ dk3print_alias_delete"
+ if(ptr) {
+ dk3_release(ptr->name);
+ ptr->printer = NULL;
+ dk3_delete(ptr);
+ }
+ $? "- dk3print_alias_delete"
+}
+
+
+
+void
+dk3print_host_delete(dk3_print_host_t *ptr)
+{
+ $? "+ dk3print_host_delete"
+ if(ptr) {
+ dk3_release(ptr->name);
+ dk3_delete(ptr);
+ }
+ $? "- dk3print_host_delete"
+}
+
+
+
+dk3_print_host_t *
+dk3print_host_new(dk3_print_conf_t *pc, dkChar const *name)
+{
+ dk3_print_host_t *back = NULL;
+ if((pc) && (name)) {
+ back =
+ (dk3_print_host_t *)dk3sto_it_find_like(pc->iPrintHosts, (void *)name, 1);
+ if(!(back)) {
+ back = dk3_new_app(dk3_print_host_t,1,pc->app);
+ if(back) {
+ back->name = NULL;
+ /* Further initialization. */
+ back->to_c = -1.0;
+ back->to_r = -1.0;
+ back->to_s = -1.0;
+ back->enc = DK3_ENCODING_UTF8;
+ /* Save name. */
+ back->name = dk3str_dup_app(name, pc->app);
+ if(back->name) {
+ /* Add to configuration. */
+ if(!dk3sto_add(pc->sPrintHosts, back)) {
+ dk3print_host_delete(back);
+ back = NULL;
+ }
+ } else {
+ dk3print_host_delete(back);
+ back = NULL;
+ }
+ }
+ }
+ if(back) {
+ if(!(pc->defHost)) {
+ pc->defHost = back;
+ }
+ }
+ }
+ return back;
+}
+
+
+
+void
+dk3print_host_alias_delete(dk3_print_host_alias_t *ptr)
+{
+ $? "+ dk3print_host_alias_delete"
+ if(ptr) {
+ dk3_release(ptr->name);
+ ptr->host = NULL;
+ dk3_delete(ptr);
+ }
+ $? "- dk3print_host_alias_delete"
+}
+
+
+
+void
+dk3print_conf_delete(dk3_print_conf_t *pc)
+{
+ void *vptr; /* Current element to delete. */
+
+ $? "+ dk3print_conf_delete %!8s", TR_8PTR(pc)
+ if(pc) {
+ pc->defPrinter = NULL;
+ pc->defHost = NULL;
+ if(pc->sHostAliases) {
+ if(pc->iHostAliases) {
+ dk3sto_it_reset(pc->iHostAliases);
+ while(NULL != (vptr = dk3sto_it_next(pc->iHostAliases))) {
+ dk3print_host_alias_delete((dk3_print_host_alias_t *)vptr);
+ }
+ dk3sto_it_close(pc->iHostAliases);
+ }
+ dk3sto_close(pc->sHostAliases);
+ } pc->sHostAliases = NULL; pc->iHostAliases = NULL;
+ if(pc->sPrintHosts) {
+ if(pc->iPrintHosts) {
+ dk3sto_it_reset(pc->iPrintHosts);
+ while(NULL != (vptr = dk3sto_it_next(pc->iPrintHosts))) {
+ dk3print_host_delete((dk3_print_host_t *)vptr);
+ }
+ dk3sto_it_close(pc->iPrintHosts);
+ }
+ dk3sto_close(pc->sPrintHosts);
+ } pc->sPrintHosts = NULL; pc->iPrintHosts = NULL;
+ if(pc->sPrintAliases) {
+ if(pc->iPrintAliases) {
+ dk3sto_it_reset(pc->iPrintAliases);
+ while(NULL != (vptr = dk3sto_it_next(pc->iPrintAliases))) {
+ dk3print_alias_delete((dk3_printer_alias_t *)vptr);
+ }
+ dk3sto_it_close(pc->iPrintAliases);
+ }
+ dk3sto_close(pc->sPrintAliases);
+ } pc->sPrintAliases = NULL; pc->iPrintAliases = NULL;
+ if(pc->sPrinters) {
+ if(pc->iPrinters) {
+ dk3sto_it_reset(pc->iPrinters);
+ while(NULL != (vptr = dk3sto_it_next(pc->iPrinters))) {
+ dk3print_printer_delete((dk3_printer_t *)vptr);
+ }
+ dk3sto_it_close(pc->iPrinters);
+ }
+ dk3sto_close(pc->sPrinters);
+ } pc->sPrinters = NULL; pc->iPrinters = NULL;
+ pc->app = NULL;
+ pc->msg = NULL;
+ dk3_delete(pc);
+ } $? "- dk3print_conf_delete"
+}
+
+
+
+/** Compare two printers by name.
+ @param l Left object.
+ @param r Right object or name.
+ @param cr Comparison criteria (0=printer/printer, 1=printer/name).
+ @return Comparison result.
+*/
+static
+int
+dk3print_compare_printer(void const *l, void const *r, int cr)
+{
+ int back = 0;
+ dk3_printer_t const *pl; /* Pointer to left printer. */
+ dk3_printer_t const *pr; /* Pointer to right printer. */
+ dkChar const *n; /* Pointer to right name. */
+ if(l) {
+ if(r) {
+ pl = (dk3_printer_t const *)l;
+ switch(cr) {
+ case 1: {
+ n = (dkChar const *)r;
+ if(pl->name) {
+ back = dk3str_cmp(pl->name, n);
+ if(back < 0) back = -1;
+ if(back > 0) back = 1;
+ } else {
+ back = -1;
+ }
+ } break;
+ default: {
+ pr = (dk3_printer_t const *)r;
+ if(pl->name) {
+ if(pr->name) {
+ back = dk3str_cmp(pl->name, pr->name);
+ if(back < 0) back = -1;
+ if(back > 0) back = 1;
+ } else {
+ back = 1;
+ }
+ } else {
+ if(pr->name) {
+ back = -1;
+ }
+ }
+ } break;
+ }
+ } else {
+ back = 1;
+ }
+ } else {
+ if(r) {
+ back = -1;
+ }
+ }
+ return back;
+}
+
+
+
+/** Compare two printer aliases by name.
+ @param l Left object.
+ @param r Right object or name.
+ @param cr Comparison criteria (0=alias/alias, 1=alias/name).
+ @return Comparison result.
+*/
+static
+int
+dk3print_compare_print_alias(void const *l, void const *r, int cr)
+{
+ int back = 0;
+ dk3_printer_alias_t const *pl; /* Left printer alias. */
+ dk3_printer_alias_t const *pr; /* Right printer alias. */
+ dkChar const *n; /* Right alias name. */
+ if(l) {
+ if(r) {
+ pl = (dk3_printer_alias_t const *)l;
+ switch(cr) {
+ case 1: {
+ n = (dkChar const *)r;
+ if(pl->name) {
+ back = dk3str_cmp(pl->name, n);
+ if(back < 0) back = -1;
+ if(back > 0) back = 1;
+ } else {
+ back = -1;
+ }
+ } break;
+ default: {
+ pr = (dk3_printer_alias_t const *)r;
+ if(pl->name) {
+ if(pr->name) {
+ back = dk3str_cmp(pl->name, pr->name);
+ if(back < 0) back = -1;
+ if(back > 0) back = 1;
+ } else {
+ back = 1;
+ }
+ } else {
+ if(pr->name) {
+ back = -1;
+ }
+ }
+ } break;
+ }
+ } else {
+ back = 1;
+ }
+ } else {
+ if(r) {
+ back = -1;
+ }
+ }
+ return back;
+}
+
+
+
+/** Compare two print hosts by name.
+ @param l Left object.
+ @param r Right object or name.
+ @param cr Comparison criteria (0=host/host, 1=host/name).
+ @return Comparison result.
+*/
+static
+int
+dk3print_compare_host(void const *l, void const *r, int cr)
+{
+ int back = 0;
+ dk3_print_host_t const *pl; /* Left print host. */
+ dk3_print_host_t const *pr; /* Right print host. */
+ dkChar const *n; /* Right print host name. */
+ if(l) {
+ if(r) {
+ pl = (dk3_print_host_t const *)l;
+ switch(cr) {
+ case 1: {
+ n = (dkChar const *)r;
+ if(pl->name) {
+ back = dk3str_cmp(pl->name, n);
+ if(back < 0) back = -1;
+ if(back > 0) back = 1;
+ } else {
+ back = -1;
+ }
+ } break;
+ default: {
+ pr = (dk3_print_host_t const *)r;
+ if(pl->name) {
+ if(pr->name) {
+ back = dk3str_cmp(pl->name, pr->name);
+ if(back < 0) back = -1;
+ if(back > 0) back = 1;
+ } else {
+ back = 1;
+ }
+ } else {
+ if(pr->name) {
+ back = -1;
+ }
+ }
+ } break;
+ }
+ } else {
+ back = 1;
+ }
+ } else {
+ if(r) {
+ back = -1;
+ }
+ }
+ return back;
+}
+
+
+
+/** Compare two print host aliases by name.
+ @param l Left object.
+ @param r Right object or name.
+ @param cr Comparison criteria (0=alias/alias, 1=alias/name).
+ @return Comparison result.
+*/
+static
+int
+dk3print_compare_host_alias(void const *l, void const *r, int cr)
+{
+ int back = 0;
+ dk3_print_host_alias_t const *pl; /* Left host alias. */
+ dk3_print_host_alias_t const *pr; /* Right host alias. */
+ dkChar const *n; /* Right alias name. */
+ if(l) {
+ if(r) {
+ pl = (dk3_print_host_alias_t const *)l;
+ switch(cr) {
+ case 1: {
+ n = (dkChar const *)r;
+ if(pl->name) {
+ back = dk3str_cmp(pl->name, n);
+ if(back < 0) back = -1;
+ if(back > 0) back = 1;
+ } else {
+ back = -1;
+ }
+ } break;
+ default: {
+ pr = (dk3_print_host_alias_t const *)r;
+ if(pl->name) {
+ if(pr->name) {
+ back = dk3str_cmp(pl->name, pr->name);
+ if(back < 0) back = -1;
+ if(back > 0) back = 1;
+ } else {
+ back = 1;
+ }
+ } else {
+ if(pr->name) {
+ back = -1;
+ }
+ }
+ } break;
+ }
+ } else {
+ back = 1;
+ }
+ } else {
+ if(r) {
+ back = -1;
+ }
+ }
+ return back;
+}
+
+
+
+dk3_print_conf_t *
+dk3print_config_new(dk3_app_t *app)
+{
+ dk3_print_conf_t *back = NULL;
+ int ok = 0; /* Flag: Success. */
+ $? "+ dk3print_config_new"
+ if(app) {
+ back = dk3_new_app(dk3_print_conf_t,1,app);
+ if(back) {
+ /* Initialize members. */
+ back->sPrinters = NULL; back->iPrinters = NULL;
+ back->sPrintAliases = NULL; back->iPrintAliases = NULL;
+ back->sPrintHosts = NULL; back->iPrintHosts = NULL;
+ back->defPrinter = NULL;
+ back->defHost = NULL;
+ back->defPsl = 3;
+ back->app = app;
+ back->msg = dk3app_messages(app, dk3print_nl[1], dk3print_msg);
+ if(!(back->msg)) { back->msg = dk3print_msg; }
+ /* Allocate resources for storages. */
+ back->sPrinters = dk3sto_open_app(app);
+ if(back->sPrinters) {
+ dk3sto_set_comp(back->sPrinters, dk3print_compare_printer, 0);
+ back->iPrinters = dk3sto_it_open(back->sPrinters);
+ if(back->iPrinters) {
+ back->sPrintAliases = dk3sto_open_app(app);
+ if(back->sPrintAliases) {
+ dk3sto_set_comp(back->sPrintAliases,dk3print_compare_print_alias,0);
+ back->iPrintAliases = dk3sto_it_open(back->sPrintAliases);
+ if(back->iPrintAliases) {
+ back->sPrintHosts = dk3sto_open_app(app);
+ if(back->sPrintHosts) {
+ dk3sto_set_comp(back->sPrintHosts, dk3print_compare_host, 0);
+ back->iPrintHosts = dk3sto_it_open(back->sPrintHosts);
+ if(back->iPrintHosts) {
+ back->sHostAliases = dk3sto_open_app(app);
+ if(back->sHostAliases) {
+ dk3sto_set_comp(
+ back->sHostAliases, dk3print_compare_host_alias, 0
+ );
+ back->iHostAliases = dk3sto_it_open(back->sHostAliases);
+ if(back->iHostAliases) {
+ ok = 1;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ /* Destroy configuration if allocations failed. */
+ if(!(ok)) {
+ dk3print_conf_delete(back);
+ back = NULL;
+ }
+ }
+ } $? "- dk3print_config_new %!8s", TR_8PTR(back)
+ return back;
+}
+
+
+#if DK3_ON_WINDOWS
+
+/** Check whether a printer name contains the words
+ "PostScript" or "PS".
+ @param pc Print configuration to set up.
+ @param pr Printer structure to modify.
+ @param str Printer string to check.
+*/
+static
+void
+dk3print_check_postscript(
+ dk3_print_conf_t *pc,
+ dk3_printer_t *pr,
+ dkChar const *str
+)
+{
+ dkChar buf[DK3_MAX_PATH]; /* Private copy. */
+ dkChar *c; /* Current string. */
+ dkChar *n; /* Next string. */
+
+ if(!(pr->ps)) {
+ if(dk3str_len(str) < DK3_SIZEOF(buf,dkChar)) {
+ dk3str_cpy_not_overlapped(buf, str);
+ c = dk3str_start(buf, NULL);
+ while(c) {
+ n = dk3str_next(c, NULL);
+ if(dk3str_cmp(c, dk3print_nl[2]) == 0) {
+ pr->ps = pc->defPsl;
+ } else {
+ if(dk3str_casecmp(c, dk3print_nl[3]) == 0) {
+ pr->ps = pc->defPsl;
+ }
+ }
+ c = n;
+ }
+ }
+ if(!(pr->ps)) {
+ if(dk3str_casecmp(str, dk3print_nl[4]) == 0) {
+ pr->ps = pc->defPsl;
+ }
+ }
+ }
+}
+
+#endif
+
+
+
+#if DK3_ON_WINDOWS
+/* + Windows */
+
+/** Read printer information from system.
+ @param pc Print configuration to set up.
+ @return 1 on success, 0 on error.
+*/
+static
+int
+dk3print_read_system(dk3_print_conf_t *pc)
+{
+ dkChar buf[1024]; /* Default printer name. */
+ char *peb; /* Result buffer. */
+#if DK3_CHAR_SIZE > 1
+ PRINTER_INFO_2W *pi2; /* Printer information. */
+#else
+ PRINTER_INFO_2A *pi2; /* Printer information. */
+#endif
+ dk3_printer_t *printer; /* New printer to create. */
+ DWORD cbNeeded; /* Bytes needed. */
+ DWORD cbReturned; /* Bytes returned. */
+ DWORD szPeb; /* Size of peb. */
+ DWORD i; /* Index of current information. */
+ DWORD dwSz; /* Buffer size. */
+ BOOL res; /* Operation result. */
+ int back = 1;
+ $? "+ dk3print_read_system (Windows 16-bit)"
+ cbNeeded = cbReturned = szPeb = (DWORD)0UL;
+#if DK3_CHAR_SIZE > 1
+ EnumPrintersW(PRINTER_ENUM_LOCAL,NULL,2,NULL,szPeb,&cbNeeded,&cbReturned);
+#else
+ EnumPrintersA(PRINTER_ENUM_LOCAL,NULL,2,NULL,szPeb,&cbNeeded,&cbReturned);
+#endif
+ if(cbNeeded > 0) {
+ szPeb = cbNeeded;
+ peb = dk3_new_app(char,(size_t)cbNeeded,pc->app);
+ if(peb) {
+ cbNeeded = cbReturned = (DWORD)0UL;
+#if DK3_CHAR_SIZE > 1
+ res = EnumPrintersW(
+ PRINTER_ENUM_LOCAL,NULL,2,(LPBYTE)peb,szPeb,&cbNeeded,&cbReturned
+ );
+#else
+ res = EnumPrintersA(
+ PRINTER_ENUM_LOCAL,NULL,2,(LPBYTE)peb,szPeb,&cbNeeded,&cbReturned
+ );
+#endif
+ if(res) {
+ if((DWORD)0UL < cbReturned) {
+ pi2 = (PRINTER_INFO_2 *)peb;
+ for(i = (DWORD)0UL; i < cbReturned; i++) {
+ printer = dk3print_printer_new(pc, pi2->pPrinterName);
+ if(printer) {
+ printer->t_p = DK3_PRINTER_TYPE_WINDOWS;
+ if(pi2->pDriverName) {
+ dk3print_check_postscript(pc, printer, pi2->pDriverName);
+ }
+ if(pi2->pComment) {
+ dk3print_check_postscript(pc, printer, pi2->pComment);
+ }
+ } else {
+ back = 0;
+ }
+ pi2++;
+ }
+ } else { $? ". no printers found"
+ }
+ } else { $? "! printer enumeration failed"
+ back = 0;
+ }
+ dk3_delete(peb);
+ } else { $? "! memory"
+ back = 0;
+ }
+ if(back) {
+ dwSz = (DWORD)DK3_SIZEOF(buf,dkChar) ;
+#if DK3_CHAR_SIZE > 1
+ res = GetDefaultPrinterW(buf, &dwSz);
+#else
+ res = GetDefaultPrinterA(buf, &dwSz);
+#endif
+ if(res) {
+ if(dwSz > (DWORD)0UL) {
+ if(dwSz < DK3_SIZEOF(buf,dkChar)) {
+ buf[dwSz - 1] = dkT('\0');
+ printer =
+ (dk3_printer_t *)dk3sto_it_find_like(pc->iPrinters, (void *)buf, 1);
+ if(printer) {
+ pc->defPrinter = printer;
+ }
+ }
+ }
+ }
+ }
+ } else { $? ". no local printers"
+ }
+ $? "- dk3print_read_system %d", back
+ return back;
+}
+
+/* - Windows */
+
+#else
+/* + non-Windows */
+
+/** Read printer information from system.
+ @param pc Print configuration to set up.
+ @return 1 on success, 0 on error.
+*/
+static
+int
+dk3print_read_system(dk3_print_conf_t *pc)
+{
+ int back = 1;
+ $? "+ dk3print_read_system (UNIX)"
+ /* ##### Read /etc/printcap */
+ $? "- dk3print_read_system %d", back
+ return back;
+}
+
+/* - non-Windows */
+#endif
+
+
+/** Read print configuration data from system and/or configuration files.
+ @param pc Print configuration to set up.
+ @param useConfigFiles Flag: Use dk3print conf files.
+ @return 1 on success, 0 on error.
+*/
+static
+int
+dk3print_read_configuration(dk3_print_conf_t *pc, int useConfigFiles)
+{
+ dkChar buf[512]; /* Buffer used for values in diagnostics. */
+ int i; /* Temporary conversion result (PS level). */
+ int back = 0;
+
+ $? "+ dk3print_read_configuration"
+ /* Find default PS level from preferences */
+ if(pc->app) {
+ if(dk3app_get_pref(pc->app, dk3print_nl[5], buf, DK3_SIZEOF(buf,dkChar))) {
+#if VERSION_BEFORE_20140716
+ if(dk3sf_sscanf3(buf, dk3print_nl[6], &i) == 1)
+#else
+ if (0 != dk3ma_i_from_string(&i, buf, NULL))
+#endif
+ {
+ if((2 <= i) && (3 >= i)) {
+ pc->defPsl = i;
+ } else {
+ /* ERROR: PS level out of range! */
+ dk3app_log_3(pc->app, DK3_LL_ERROR, pc->msg, 49, 50, buf);
+ }
+ } else {
+ /* ERROR: PS level not numeric! */
+ dk3app_log_3(pc->app, DK3_LL_ERROR, pc->msg, 49, 50, buf);
+ }
+ }
+ }
+ /* Read system printers and configuration files. */
+ if(dk3print_read_system(pc)) {
+ if(useConfigFiles) {
+ if(dk3print_read_config_files(pc)) {
+ back = 1;
+ }
+ } else {
+ back = 1;
+ }
+ } $? "- dk3print_read_configuration %d", back
+ return back;
+}
+
+
+
+dk3_print_conf_t *
+dk3print_conf_open(dk3_app_t *app, int useConfigFiles)
+{
+ dk3_print_conf_t *back = NULL;
+ $? "+ dk3print_conf_open"
+ if(app) {
+ back = dk3print_config_new(app);
+ if(back) {
+ if(!dk3print_read_configuration(back, useConfigFiles)) {
+ dk3print_conf_delete(back);
+ back = NULL;
+ }
+ }
+ } $? "- dk3print_conf_open %!8s", TR_8PTR(back)
+ return back;
+}
+
+
+
+void
+dk3print_conf_close(dk3_print_conf_t *pc)
+{
+ $? "+ dk3print_conf_close"
+ if(pc) {
+ dk3print_conf_delete(pc);
+ } $? "- dk3print_conf_close"
+}
+
+
+
+void
+dk3print_set_det(dk3_printer_details_t *det, int tp)
+{
+ $? "+ dk3print_set_det"
+ if(det) {
+ switch(tp) {
+ case DK3_PRINTER_TYPE_WINDOWS: {
+ (det->win).dummy = 0;
+ } break;
+ case DK3_PRINTER_TYPE_SOCKET: {
+ (det->sock).hostname = NULL;
+ (det->sock).portno = 0;
+ (det->sock).ordrel = 0x00;
+ (det->sock).to_c = -1.0;
+ (det->sock).to_r = -1.0;
+ (det->sock).to_s = -1.0;
+ } break;
+ case DK3_PRINTER_TYPE_LPD: case DK3_PRINTER_TYPE_LPRNG: {
+ (det->lprng).hostname = NULL;
+ (det->lprng).queuename = NULL;
+ (det->lprng).to_c = -1.0;
+ (det->lprng).to_r = -1.0;
+ (det->lprng).to_s = -1.0;
+ } break;
+ default: {
+ } break;
+ }
+ } $? "- dk3print_set_det"
+}
+
+
+
+dk3_printer_t *
+dk3print_get_printer(dk3_print_conf_t *pc, dkChar const *pn)
+{
+ dk3_printer_t *back = NULL;
+ dk3_printer_alias_t *al = NULL; /* Alias for printer. */
+
+ if((pc) && (pn)) {
+ back = (dk3_printer_t *)dk3sto_it_find_like(pc->iPrinters, (void *)pn, 1);
+ if(!(back)) {
+ al = (dk3_printer_alias_t *)dk3sto_it_find_like(
+ pc->iPrintAliases, (void *)pn, 1
+ );
+ if(al) {
+ back = al->printer;
+ }
+ }
+ }
+ return back;
+}
+
+
+dk3_print_host_t *
+dk3print_get_host(dk3_print_conf_t *pc, dkChar const *hn)
+{
+ dk3_print_host_t *back = NULL;
+ dk3_print_host_alias_t *al = NULL; /* Alias for host. */
+
+ if((pc) && (hn)) {
+ back = (dk3_print_host_t *)dk3sto_it_find_like(
+ pc->iPrintHosts, (void *)hn, 1
+ );
+ if(!(back)) {
+ al = (dk3_print_host_alias_t *)dk3sto_it_find_like(
+ pc->iHostAliases, (void *)hn, 1
+ );
+ if(al) {
+ back = al->host;
+ }
+ }
+ }
+ return back;
+}
+