📄 connection.c
字号:
i = flock(cxp->cx_pr_fd, LOCK_EX); } if (i < 0) { log("broken lock on %s", LP); exit(1); } } if (isatty(cxp->cx_pr_fd)) { set_tty_modes(cxp->cx_pr_fd); } open_output_filter(cxp); return 0;}/* * nor_close(cxp) -- close a device or lat connection */static intnor_close(cxp)register CXP cxp;{ register int retval; retval = close_output_filter(cxp); (void) close(cxp->cx_pr_fd); /* close printer */ cxp->cx_pr_fd = cxp->cx_out_fd = -1; return retval;}/* * nor_stop(cxp) -- if there is an output filter stop it * * This involves sending it a C-Y C-A pair of control * characters which it is supposed to recognise a send * itself a stop signal * We wait for it to stop, so lets hope it does! */static intnor_stop(cxp)register CXP cxp;{ if (cxp->cx_output_filter) { /* stop output filter */ if (fc_stop(cxp->cx_output_filter) < 0) { union wait o_status; o_status = fc_wait(cxp->cx_output_filter); log("output filter died (%d)", o_status.w_retcode); fc_delete(cxp->cx_output_filter, 1); cxp->cx_output_filter = 0; return(1); } } return 0;}/* * nor_start(cxp) -- if we're stopped, restart the output filter * * This is the old algorithm which kills the old filter * and starts a new invocation. * It seems a monstrously inefficient way of doing it. * The whole mechanism should be swept away in my view (thoms) */static intnor_start(cxp)register CXP cxp;{ if (cxp->cx_output_filter && cxp->cx_output_filter->fc_state == fc_stopped) { cx_close(cxp); /* close output and kill filter */ cx_open(cxp); /* open output and restart filter */ } return 0; /* always succeeds for now */}/* * sane_start(cxp) -- if we're stopped, restart the output filter * * This should restart the filter in the proper * Berkeley fashion as requested by Brian Reid in his QAR */static intsane_start(cxp)register CXP cxp;{ if (cxp->cx_output_filter) return fc_start(cxp->cx_output_filter); return 0;}/* * lat_open(cxp) -- open a lat connection */static intlat_open(cxp)register CXP cxp;{ if (!(*LP && TS && (OP || OS))) { log("%s %s\n%s", CT_choices[(int)cxp->cx_type], "connection requires TS and either OP or OS", "and non-null LP in printcap"); exit(1); } status("waiting for %s to connect", printer); cxp->cx_pr_fd = lat_conn(LP,TS,OP,OS); if (cxp->cx_pr_fd < 0) { log("cannot open %s: %s",LP,strerror(errno)); exit(1); } set_tty_modes(cxp->cx_pr_fd); open_output_filter(cxp); status("%s is ready and printing via %s", printer, CT_choices[(int)cxp->cx_type]); return 0;}/* * rem_open(cxp) -- open socket connection to remote Berkeley lpd */static intrem_open(cxp)register CXP cxp;{ int i, n; if (!(RM && RP)) { log("%s connection requires RM and RP in printcap", CT_choices[(int)cxp->cx_type]); exit(1); } for (i = 1; ; i = i < 32 ? i << 1 : i) { cxp->cx_pr_fd = getport(RM); if (cxp->cx_pr_fd >= 0) { (void) sprintf(line, "\2%s\n", RP); n = strlen(line); if (write(cxp->cx_pr_fd, line, n) != n) break; if (noresponse(cxp->cx_pr_fd)) { if (flock(cxp->cx_pr_fd, LOCK_UN) < 0) { log("cannot unlock %s", LP); exit(1); } (void) close(cxp->cx_pr_fd); } else break; } else { log("getport(%s) failed: %s", RM, err_string()); } if (i == 1) status("waiting for %s to come up", RM); sleep(i); } status("sending to %s", RM); return 0;}/* * rem_close(cxp) -- close socket connection */static intrem_close(cxp)register CXP cxp;{ register int retval; retval = close(cxp->cx_pr_fd); /* close socket connection */ cxp->cx_pr_fd = -1; return retval;}/* * net_open(cxp) -- open up network filter * * This is just opening the output filter but without opening * up a device */static intnet_open(cxp)register CXP cxp;{ if (!(OF && *OF)) { log("%s connection requires OF in printcap", CT_choices[(int)cxp->cx_type]); exit(1); } open_output_filter(cxp); cxp->cx_pr_fd = cxp->cx_out_fd; status("%s is ready and printing via %s", printer, CT_choices[(int)cxp->cx_type]); return 0;}/* * net_close(cxp) -- close network filter */static intnet_close(cxp)register CXP cxp;{ register int retval; retval = close_output_filter(cxp); cxp->cx_pr_fd = cxp->cx_out_fd = -1; return retval;}/* * tcp_open(cxp) -- open tcp connection */static inttcp_open(cxp)register CXP cxp;{ if (LP[0] != '@') { log("%s connection requires lp in printcap starts with @", CT_choices[(int)cxp->cx_type]); exit(1); } if ((cxp->cx_pr_fd = tcp_conn(&LP[1])) < 0) { log("Cannot open tcp connection %s", LP); exit(1); } open_output_filter(cxp); return 0;}/* * cx_nop(cxp) -- no-op function for insertion in switch table *//*ARGSUSED*/static intcx_nop(cxp)register CXP cxp;{ return 0;}/***************************************************************** Main entry points for connection object****************************************************************//* * cx_init -- initialise the object structure */voidcx_init(cxp, connection_type)register CXP cxp;enum connection_type_e connection_type;{ cxp->cx_state = cxs_closed; cxp->cx_out_fd = cxp->cx_pr_fd = -1; cxp->cx_output_filter = NULL; cxp->cx_type = connection_type;}/* * cx_delete -- de-initialise the connection object */voidcx_delete(cxp, on_heap)CXP cxp;int on_heap;{ if (cxp && on_heap) free(cxp);}/* * cx_open -- open call on connection object */intcx_open(cxp)register CXP cxp;{ register int retval = 0; if (cxp->cx_state == cxs_closed) { cxp->cx_state = cxs_open; dlog(0, "Opening output connection for %s", CT_choices[(int)cxp->cx_type]); retval = (*cx_sw_tab[(int)cxp->cx_type].cxf_open)(cxp); if (retval == 0) { status("%s is ready and printing via %s", printer, CT_choices[(int)cxp->cx_type]); } } return retval;}/* * cx_close -- close call on connection object */intcx_close(cxp)register CXP cxp;{ if (cxp->cx_state == cxs_open) { cxp->cx_state = cxs_closed; dlog(0, "Closing output connection for %s", CT_choices[(int)cxp->cx_type]); return (*cx_sw_tab[(int)cxp->cx_type].cxf_close)(cxp); } else return 0;}/* * cx_stop -- stop call on connection object * * See nor_stop above for non_null implementation */intcx_stop(cxp)register CXP cxp;{ if (cxp->cx_state == cxs_open) { return((*cx_sw_tab[(int)cxp->cx_type].cxf_stop)(cxp)); } else return 0;}/* * cx_start -- start call on connection object * * See nor_start for non_null implementation */intcx_start(cxp)register CXP cxp;{ if (cxp->cx_state == cxs_open) { return ((*cx_sw_tab[(int)cxp->cx_type].cxf_start)(cxp)); } else return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -