| 1 | # |
| 2 | |
| 3 | /* |
| 4 | * Versatec printer daemon |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <dir.h> |
| 10 | #include <signal.h> |
| 11 | #include <stat.h> |
| 12 | #include <sgtty.h> |
| 13 | #include <errno.h> |
| 14 | |
| 15 | extern int errno; |
| 16 | |
| 17 | #define SETSTATE (('v'<<8)|1) |
| 18 | #define VRAST "/usr/local/lib/vrast" |
| 19 | int prtmode[] = {0100, 0, 0}; |
| 20 | |
| 21 | char line[128]; |
| 22 | char linep[127]; |
| 23 | char banbuf[64]; |
| 24 | char printflag; |
| 25 | int linel; |
| 26 | FILE *dfb; |
| 27 | char dfname [27] = "/usr/vpd/"; |
| 28 | int waittm = 6; |
| 29 | struct dir dbuf; |
| 30 | int onalrm (); |
| 31 | char fonts[4][50] = |
| 32 | { |
| 33 | "/usr/lib/vfont/R", |
| 34 | "/usr/lib/vfont/I", |
| 35 | "/usr/lib/vfont/B", |
| 36 | "/usr/lib/vfont/S" |
| 37 | }; |
| 38 | |
| 39 | main (argc, argv) |
| 40 | { |
| 41 | char dp, n; |
| 42 | register char *p1, *p2; |
| 43 | register int df; |
| 44 | struct stat stb; |
| 45 | int offline = 0; |
| 46 | int i; |
| 47 | |
| 48 | signal (SIGHUP, SIG_IGN); |
| 49 | signal (SIGINT, SIG_IGN); |
| 50 | signal (SIGQUIT, SIG_IGN); |
| 51 | signal (SIGTERM, SIG_IGN); |
| 52 | /* |
| 53 | * Close all files, open root as 0, 1, 2 |
| 54 | * to assure standard environment |
| 55 | */ |
| 56 | for (df=0; df<=15; df++) |
| 57 | close (df); |
| 58 | open ("/", 0); |
| 59 | dup (0); |
| 60 | dup (0); |
| 61 | if (chdir ("/usr/vpd") < 0) |
| 62 | exit (1); |
| 63 | if (stat ("lock", &stb) >= 0) |
| 64 | exit (); |
| 65 | if ((df=creat ("lock", 0)) < 0) |
| 66 | exit (); |
| 67 | close (df); |
| 68 | i = fork (); |
| 69 | if (i < 0) |
| 70 | { |
| 71 | unlink ("lock"); |
| 72 | exit (); |
| 73 | } |
| 74 | if (i != 0) |
| 75 | exit (); |
| 76 | for (;;) |
| 77 | { |
| 78 | if (open ("/dev/vp0", 1) == 3) |
| 79 | break; |
| 80 | if (errno != EIO) |
| 81 | { |
| 82 | perror ("/dev/vp0"); |
| 83 | unlink ("lock"); |
| 84 | exit (); |
| 85 | } |
| 86 | if (offline == 0) |
| 87 | { |
| 88 | int f = open ("/dev/tty", 1); |
| 89 | offline++; |
| 90 | if (f > 0) |
| 91 | { |
| 92 | write (f, "Versatec is offline\n", 20); |
| 93 | close (f); |
| 94 | } |
| 95 | } |
| 96 | sleep (10); |
| 97 | } |
| 98 | dp = open (".", 0); |
| 99 | /* |
| 100 | * the main loop |
| 101 | */ |
| 102 | for (;;) |
| 103 | { |
| 104 | /* |
| 105 | * find the first queueable directory entry |
| 106 | */ |
| 107 | lseek (dp, 0, 0); |
| 108 | for (;;) |
| 109 | { |
| 110 | n = read (dp, &dbuf, sizeof dbuf); |
| 111 | if (n <= 0) |
| 112 | { |
| 113 | /* |
| 114 | * if there was a job printed |
| 115 | * pull it out of the toner bath |
| 116 | */ |
| 117 | if (printflag) |
| 118 | feedpage (); |
| 119 | unlink ("lock"); |
| 120 | exit (); |
| 121 | } |
| 122 | if |
| 123 | ( |
| 124 | dbuf.d_ino |
| 125 | && |
| 126 | dbuf.d_name[0] == 'd' |
| 127 | && |
| 128 | dbuf.d_name[1] == 'f' |
| 129 | ) |
| 130 | { |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | strcpy (&dfname [9], dbuf.d_name); |
| 135 | if (trysend (dfname) == 0) /* everything was ok */ |
| 136 | { |
| 137 | write (3, "\n\n\n", 3); |
| 138 | printflag++; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | trysend (file) |
| 144 | char *file; |
| 145 | { |
| 146 | register int i; |
| 147 | extern int badexit (); |
| 148 | char plot; |
| 149 | |
| 150 | resfonts(); |
| 151 | dfb = fopen (file, "r"); |
| 152 | if (dfb == NULL) |
| 153 | return (0); |
| 154 | for (*banbuf = plot = 0; getline ();) |
| 155 | { |
| 156 | switch (line[0]) |
| 157 | { |
| 158 | case 'L': |
| 159 | strcpy (banbuf, line + 1); |
| 160 | continue; |
| 161 | |
| 162 | case '1': |
| 163 | case '2': |
| 164 | case '3': |
| 165 | case '4': |
| 166 | strcpy (fonts[line[0]-'1'], line + 1); |
| 167 | continue; |
| 168 | |
| 169 | case 'F': |
| 170 | case 'T': |
| 171 | if (send (line[0])) |
| 172 | { |
| 173 | ioctl (3, SETSTATE, prtmode); |
| 174 | write (3, "\nJOB ABORTED***\n", 16); |
| 175 | } |
| 176 | continue; |
| 177 | |
| 178 | case 'P': |
| 179 | if (plot) |
| 180 | { |
| 181 | plot = 0; |
| 182 | if (send (line[0])) |
| 183 | { |
| 184 | ioctl (3, SETSTATE, prtmode); |
| 185 | write (3, "\nJOB ABORTED***\n", 16); |
| 186 | } |
| 187 | else |
| 188 | continue; |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | strcpy (linep, line + 1); |
| 193 | plot++; |
| 194 | continue; |
| 195 | } |
| 196 | |
| 197 | case 'U': |
| 198 | continue; |
| 199 | |
| 200 | case 'M': |
| 201 | continue; |
| 202 | } |
| 203 | } |
| 204 | /* |
| 205 | * Second pass. |
| 206 | * Unlink files and send mail. |
| 207 | */ |
| 208 | fseek (dfb, 0, 0); |
| 209 | while (getline ()) |
| 210 | { |
| 211 | switch (*line) |
| 212 | { |
| 213 | default: |
| 214 | continue; |
| 215 | |
| 216 | case 'U': |
| 217 | unlink (line + 1); |
| 218 | continue; |
| 219 | |
| 220 | case 'M': |
| 221 | sendmail (); |
| 222 | continue; |
| 223 | } |
| 224 | } |
| 225 | fclose (dfb); |
| 226 | unlink (file); |
| 227 | return (0); |
| 228 | } |
| 229 | |
| 230 | static char ifonts[4][50] = |
| 231 | { |
| 232 | "/usr/lib/vfont/R", |
| 233 | "/usr/lib/vfont/I", |
| 234 | "/usr/lib/vfont/B", |
| 235 | "/usr/lib/vfont/S" |
| 236 | }; |
| 237 | |
| 238 | resfonts() |
| 239 | { |
| 240 | int i; |
| 241 | for (i = 0; i < 4; i++) |
| 242 | strcpy(fonts[i], ifonts[i]); |
| 243 | } |
| 244 | sendmail () |
| 245 | { |
| 246 | static int p[2]; |
| 247 | register i; |
| 248 | int stat; |
| 249 | |
| 250 | pipe (p); |
| 251 | if (fork ()==0) |
| 252 | { |
| 253 | alarm (0); |
| 254 | close (0); |
| 255 | dup (p[0]); |
| 256 | for (i=3; i<=15; i++) |
| 257 | close (i); |
| 258 | execl ("/bin/mail", "mail", &line[1], 0); |
| 259 | exit (); |
| 260 | } |
| 261 | close (1); |
| 262 | dup (p[1]); |
| 263 | printf ("Your versatec job is done\n"); |
| 264 | close (1); |
| 265 | close (p[0]); |
| 266 | close (p[1]); |
| 267 | open ("/", 0); |
| 268 | wait (&stat); |
| 269 | } |
| 270 | |
| 271 | getline () |
| 272 | { |
| 273 | register char *lp; |
| 274 | register int c; |
| 275 | |
| 276 | lp = line; |
| 277 | linel = 0; |
| 278 | while ((c = getc (dfb)) != '\n') |
| 279 | { |
| 280 | if (c<0) |
| 281 | return (0); |
| 282 | if (c=='\t') |
| 283 | { |
| 284 | do |
| 285 | { |
| 286 | *lp++ = ' '; |
| 287 | linel++; |
| 288 | } while ((linel & 07) != 0); |
| 289 | continue; |
| 290 | } |
| 291 | *lp++ = c; |
| 292 | linel++; |
| 293 | } |
| 294 | *lp++ = 0; |
| 295 | return (1); |
| 296 | } |
| 297 | |
| 298 | int pid; |
| 299 | |
| 300 | send (c) |
| 301 | char c; |
| 302 | { |
| 303 | int p; |
| 304 | |
| 305 | if (pid = fork ()) |
| 306 | { |
| 307 | if (pid == -1) |
| 308 | return (1); |
| 309 | setexit (); |
| 310 | signal (SIGALRM, onalrm); |
| 311 | alarm (30); |
| 312 | wait (&p); |
| 313 | alarm (0); |
| 314 | return (p); |
| 315 | } |
| 316 | ioctl (3, SETSTATE, prtmode); |
| 317 | if (c == 'F') |
| 318 | { |
| 319 | if (banbuf[0]) |
| 320 | { |
| 321 | execl ("/usr/lib/vpf", "vpf", "-b", banbuf, line+1, 0); |
| 322 | return (1); |
| 323 | } |
| 324 | execl ("/usr/lib/vpf", "vpf", line, 0); |
| 325 | } |
| 326 | else if (c == 'T') |
| 327 | { |
| 328 | int i; |
| 329 | int rm; |
| 330 | unlink (".railmag"); |
| 331 | rm = creat (".railmag", 0666); |
| 332 | for (i = 0; i < 4; i++) |
| 333 | { |
| 334 | if (fonts[i][0] != '/') |
| 335 | write (rm, "/usr/lib/vfont/", strlen ("/usr/lib/vfont/")); |
| 336 | write (rm, fonts[i], strlen (fonts[i])); |
| 337 | write (rm, "\n", 1); |
| 338 | } |
| 339 | close (rm); |
| 340 | if (banbuf[0]) |
| 341 | { |
| 342 | execl ("/usr/lib/vcat", "vcat", "-3", "-b", banbuf, line+1, 0); |
| 343 | return (1); |
| 344 | } |
| 345 | execl ("/usr/lib/vcat", "vcat", "-3", line+1, 0); |
| 346 | } |
| 347 | else if (c == 'P') |
| 348 | { |
| 349 | close (1); |
| 350 | dup (3); |
| 351 | if (banbuf[0]) |
| 352 | { |
| 353 | execl (VRAST, "vrast", "-v", "-b", banbuf, line+1, linep, 0); |
| 354 | return (1); |
| 355 | } |
| 356 | execl (VRAST, "vrast", "-v", line+1, linep, 0); |
| 357 | } |
| 358 | |
| 359 | return (1); |
| 360 | } |
| 361 | |
| 362 | onalrm () |
| 363 | { |
| 364 | struct stat stb; |
| 365 | |
| 366 | signal (SIGALRM, onalrm); |
| 367 | if (stat (dfname, &stb) < 0) |
| 368 | kill (pid, SIGEMT); |
| 369 | reset (); |
| 370 | } |
| 371 | |
| 372 | struct sgttyb ttyb = |
| 373 | { |
| 374 | B1200, B1200, |
| 375 | 0, 0, |
| 376 | XTABS|ANYP|NL2|CR2 |
| 377 | }; |
| 378 | |
| 379 | /* |
| 380 | * skip 16 inches |
| 381 | */ |
| 382 | feedpage () |
| 383 | { |
| 384 | char i; |
| 385 | |
| 386 | ioctl (3, SETSTATE, prtmode); |
| 387 | for (i = 0; i < 18; i++) |
| 388 | write (3, "\n\n\n\n\n\n\n\n", 8); |
| 389 | } |