static int queue_insert (lua_State *L) { queue *q = checkqueue(L, 1); lua_settop(L, 2); /* queue, element */ if (lua_isnil(L, 2)) return 0; lua_getfenv(L, 1); lua_pushvalue(L, 2); lua_rawseti(L, -2, ++(q->tail)); return 0; }
main(int argc, char *argv[]) { int c, usgflg = 0, inputfd, printerfd, sendport; char *desthostname, *hnend; char portstr[4]; if (signal(SIGALRM, alarmhandler) == SIG_ERR) { fprintf(stderr, "failed to set alarm handler\n"); exit(1); } while ((c = getopt(argc, argv, "Dd:k:qs:t:H:P:")) != -1) switch (c) { case 'D': debugflag = 1; debug("debugging on\n"); break; case 'd': printername = optarg; break; case 'k': if (statflag) { fprintf(stderr, "cannot have both -k and -q flags\n"); exit(1); } killflag = 1; killarg = optarg; break; case 'q': if (killflag) { fprintf(stderr, "cannot have both -q and -k flags\n"); exit(1); } statflag = 1; break; case 's': seqno = strtol(optarg, NULL, 10); if (seqno < 0 || seqno > 999) seqno = 0; break; case 't': switch (filetype) { case 'c': case 'd': case 'f': case 'g': case 'l': case 'n': case 'o': case 'p': case 'r': case 't': case 'v': case 'z': filetype = optarg[0]; break; default: usgflg++; break; } break; case 'H': strncpy(hostname, optarg, MAXHOSTNAMELEN); break; case 'P': username = optarg; break; default: case '?': fprintf(stderr, "unknown option %c\n", c); usgflg++; } if (argc < 2) usgflg++; if (optind < argc) { desthostname = argv[optind++]; } else usgflg++; if (usgflg) { fprintf(stderr, "usage: to send a job - %s -d printer -H hostname -P username [-s seqno] [-t[cdfgklnoprtvz]] desthost [filename]\n", argv[0]); fprintf(stderr, " to check status - %s -d printer -q desthost\n", argv[0]); fprintf(stderr, " to kill a job - %s -d printer -P username -k jobname desthost\n", argv[0]); exit(1); } /* make sure the file to send is here and ready * otherwise the TCP connection times out. */ if (!statflag && !killflag) { if (optind < argc) { inputname = argv[optind++]; debug("open("); debug(inputname); debug(")\n"); inputfd = open(inputname, O_RDONLY); if (inputfd < 0) { fprintf(stderr, "open(%s) failed\n", inputname); exit(1); } } else { inputname = "stdin"; tmpnam(tmpfilename); debug("using stdin\n"); if ((inputfd = open(tmpfilename, O_RDWR|O_CREAT, 0600)) < 0) { fprintf(stderr, "open(%s) failed\n", tmpfilename); exit(1); } atexit(cleanup); debug("copy input to temp file "); debug(tmpfilename); debug("\n"); if (!copyfile(0, inputfd, 0L)) { fprintf(stderr, "failed to copy file to temporary file\n"); exit(1); } if (lseek(inputfd, 0L, 0) < 0) { fprintf(stderr, "failed to seek back to the beginning of the temporary file\n"); exit(1); } } } sprintf(strbuf, "%s", netmkaddr(desthostname, "tcp", "printer")); fprintf(stderr, "connecting to %s\n", strbuf); for (sendport=721; sendport<=731; sendport++) { sprintf(portstr, "%3.3d", sendport); fprintf(stderr, " trying from port %s...", portstr); debug(" dial("); debug(strbuf); debug(", "); debug(portstr); debug(", 0, 0) ..."); printerfd = dial(strbuf, portstr, 0, 0); if (printerfd >= 0) { fprintf(stderr, "connected\n"); break; } fprintf(stderr, "failed\n"); sleep(REDIALTIMEOUT); } if (printerfd < 0) { fprintf(stderr, "Cannot open a valid port!\n"); fprintf(stderr, "- All source ports [721-731] may be busy.\n"); fprintf(stderr, "- Is recipient ready and online?\n"); fprintf(stderr, "- If all else fails, cycle the power!\n"); exit(1); } /* hostname[8] = '\0'; */ #ifndef PLAN9 if (gethostname(hostname, sizeof(hostname)) < 0) { perror("gethostname"); exit(1); } #endif /* if ((hnend = strchr(hostname, '.')) != NULL) *hnend = '\0'; */ if (statflag) { checkqueue(printerfd); } else if (killflag) { killjob(printerfd); } else { sendjob(inputfd, printerfd); } exit(0); }
static int queue_isempty (lua_State *L) { queue *q = checkqueue(L, 1); lua_pushboolean(L, len(q) == 0); return 1; }
static int queue_front (lua_State *L) { return retrieve(L, checkqueue(L, 1), 1); }
static int queue_retrieve (lua_State *L) { return retrieve(L, checkqueue(L, 1), lua_toboolean(L, 2)); }