File: vtprint.c

package info (click to toggle)
vtprint 2.0.2-13
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 452 kB
  • ctags: 191
  • sloc: ansic: 1,122; sed: 733; makefile: 291; sh: 96
file content (676 lines) | stat: -rw-r--r-- 20,724 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/**************************************************************************
 **************************************************************************
 **                                                                      **
 ** vtprint.c       vtprint C source file                                **
 ** =========                                                            **
 **                                                                      **
 ** Purpose:        Provides printing services to a terminal printer     **
 **                 using terminal escape sequences.                     **
 **                                                                      **
 ** Author:         Garrett D'Amore <garrett@sciences.sdsu.edu>          **
 **                                                                      **
 ** Copyright:      1994, Garrett E. D'Amore                             **
 **                                                                      **
 ** NO WARRANTY:    This program is provided entirely without warranty.  **
 **                 The user assumes full responsibility for the use of  **
 **                 this program, and agrees to indemnify the author and **
 **                 the copyright holder from any damage or loss that    **
 **                 may result from the use of or inability to use this  **
 **                 program.  In simple language: YOU USE THIS PROGRAM   **
 **                 AT YOUR OWN RISK!                                    **
 **                                                                      **
 ** Warning:        None.                                                **
 **                                                                      **
 ** Usage:          vtprint [options] [files]                            **
 **                                                                      **
 ** Restrictions:   None.                                                **
 **                                                                      **
 ** Algorithm:      None.                                                **
 **                                                                      **
 ** References:     None.                                                **
 **                                                                      **
 ** File formats:   vtprintcap(5)                                        **
 **                                                                      **
 ** Rev. History:   June 4, 1994    Garrett D'Amore                      **
 **                 -- Initial coding.                                   **
 **                                                                      **
 ** Notes:          None.                                                **
 **                                                                      **
 **************************************************************************
 **************************************************************************/

/* >>>>>>>>>> Headers <<<<<<<<<< */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <memory.h>

#include "arglist.h"
#include "termstate.h"

/* >>>>>>>>>> Defines <<<<<<<<<< */

#define CR  '\xD'
#define LF  '\xA'
#define FF  '\xC'

#ifndef MAN_SECT
#define MAN_SECT "1"
#endif

#ifndef VERSION_NO
#define VERSION_NO "2.0dev"
#endif

#ifndef VERSION_DT
#define VERSION_DT "January 1, 1970"
#endif

#ifndef DEVTTY
#define DEVTTY "/dev/tty"
#endif

#ifndef LIBFILE
#define LIBFILE "/etc/vtprintcap"
#endif

/* >>>>>>>>>> Variables <<<<<<<<<< */

/* stuff used by getopt() */
extern char *optarg;
extern int  optind;

/* default printer control codes (ANSI/vt100/vt220) */
char    *ptr_on  = "\033[5i";
char    *ptr_off = "\033[4i";

/* string operating parameters */
char    *termname = "";
char    *libtermname = LIBFILE;
char    *devname = DEVTTY;
char    *vtbasename = "";

/* modes of operation */
int     quiet   = 0;    /* minimal message reporting? */
int     binary  = 0;    /* binary newline translation? */
int     ffeed   = 1;    /* formfeed between files? */
int     device  = 0;    /* use tty device instead of stdout? */
int     stripcr = 0;    /* strip CRs from CR/LFs? */
int     addcr   = 0;    /* add LFs to bare CRs? */
int     builtin = 0;    /* use builtin escape codes by default? */
int     forcenv = 0;    /* force using the getenv(TERM) value */
int     dowork  = 1;    /* whether or not to actually do processing */

/* terminal states */
int         state_saved = 0;
termstate   oldstate;
termstate   newstate;

/* output file descriptor */
FILE    *outfile;

/* >>>>>>>>>> Prototypes <<<<<<<<<< */

void    sighandler (int);
int     process_opts (arglist *);
void    help (void);
void    license (void);
void    warranty (void);
void    intro (void);

/* >>>>>>>>>> Functions <<<<<<<<<< */

/***************************************
 *
 *  sighandler      Signal handler for terminal clean-up.
 *
 *  Purpose         Ensures that the terminal is restored to a "sane"
 *                  state when a signal is received, and then exits
 *                  from program.
 *
 *  Parameters      signum:     signal number that was caught
 *
 *  Returns         None.
 *
 */

void sighandler (int signum)
{
    char *signame;

    fprintf (outfile, "%s", ptr_off);
    fflush (outfile);
    if (state_saved) restore_termstate(fileno(outfile), &oldstate);

    if (!quiet)
    {
        switch (signum)
        {
            case SIGHUP     : { signame = "hangup"; break; }
            case SIGINT     : { signame = "interrupt"; break; }
            case SIGQUIT    : { signame = "quit"; break; }
            case SIGTERM    : { signame = "terminate"; break; }
            case SIGTSTP    : { signame = "keyboard stop"; break; }
            default         : { signame = "unknown"; break; }
        }
        fprintf (outfile,"\nCaught %s signal!  Exiting vtprint.\n", signame);
    }
    exit (1);
}

/***************************************
 *
 *  process_opts    Process command-line and environment options.
 *
 *  Purpose         Process specific program options from an arglist.
 *                  Called once for environment, then again for argv[].
 *
 *  Parameters      arglist:    arglist of parameters to process
 *
 *  Returns         Pointer to "next" index in arglist to process (stuff
 *                  that getopt () can't process itself).
 *
 */

int process_opts (arglist *args)
{
    int c;

    optind = 1; /* reset getopt index */

    args -> argv [0] = vtbasename;

    do 
    {
        c = getopt (args->argc, args->argv, "hwlvdDbBcCeEfFnNqQT:tL:V:");
        switch (c)
        {
            case 'h' : { help (); dowork = 0; break; }
            case 'w' : { warranty (); dowork = 0; break; }
            case 'l' : { license (); dowork = 0; break; }
            case 'v' : { intro(); dowork = 0; break; }

            case 'd' : { device = 1; break; }
            case 'D' : { device = 0; break; }
            case 'b' : { binary = 1; break; }
            case 'B' : { binary = 0; break; }
            case 'c' : { stripcr = 0; addcr = 1; break; }
            case 'C' : { stripcr = 0; addcr = 0; break; }
            case 'e' : { builtin = 1; forcenv = 0; break; }
            case 'E' : { builtin = 0; forcenv = 0; break; }
            case 'f' : { ffeed = 0; break; }
            case 'F' : { ffeed = 1; break; }
            case 'n' : { addcr = 0; stripcr = 1; break; }
            case 'N' : { addcr = 0; stripcr = 0; break; }
            case 'q' : { quiet = 1; break; }
            case 'Q' : { quiet = 0; break; }
            case 't' : {
                           forcenv = 1;
                           termname = getenv ("TERM");
                           break;
                       }
            case 'T' : {
                           termname = optarg;
                           break;
                       }
            case 'L' : {
                           libtermname = optarg;
                           break;
                       }
            case 'V' : {
                           devname = optarg;
                           device = 1;
                           break;
                       }
            case '?' : { help(); exit (1); }
        }
    } while (c != EOF) ;
    return (optind);
}
                       
/***************************************
 *
 *  help            Provide "basic" on-line user help.
 *
 *  Purpose         To assist users who might get lost. (RTFM).
 *
 *  Parameters      None.
 *
 *  Returns         None.
 *
 */

void help (void)
{
    if ((!strcmp (vtbasename, "vtprton")) || (!strcmp (vtbasename, "vtprtoff")))
        fprintf (stderr, "Usage: %s [ -dDeEhlqQNtvw ] \
[ -L vtprintcap ] [ -T termtype ] [ -V device ]\n", vtbasename);

    else fprintf (stderr, "Usage: %s [ -bBcCdDeEfFhlnNqQtvW ] \
[ -L vtprintcap ] [ -T termtype ] [ -V device ] [ filename... ]\n",
    vtbasename);

    fprintf (stderr, "\nPlease see the %s(%s) manual page for more\n",
        vtbasename,MAN_SECT);

    fprintf (stderr, "information about the meaning of these options.\n");
}

/***************************************
 *
 *  intro           Provides a brief intro to vtprint.
 *
 *  Purpose         Displayed whenever user starts vtprint.
 *
 *  Parameters      None.
 *
 *  Returns         None.
 *
 */

void intro (void)
{
    fprintf (stderr, "\n*** %s (v%s) ***\n\n", vtbasename, VERSION_NO);
    fprintf (stderr, "Copyright 1993-1994, Garrett D'Amore\n");
    fprintf (stderr, "Last revised %s.\n\n", VERSION_DT);
    fprintf (stderr, "NO WARRANTY!  Use \"%s -w\" for info.\n",
        vtbasename);
    fprintf (stderr, "Freely redistributable.  Use \"%s -l\" for info.\n\n",
        vtbasename);

    return;
}

void warranty (void)
{
    fprintf (stderr, "%s: This program is provided entirely without\n",
        vtbasename);
    fprintf (stderr, "%s: warranty!  The user agrees to indemnify the\n",
        vtbasename);
    fprintf (stderr, "%s: author from any claim of damage or loss that\n",
        vtbasename);
    fprintf (stderr, "%s: may result from the use of or inability to\n",
        vtbasename);
    fprintf (stderr, "%s: use this program.\n\n", vtbasename);
}

void license (void)
{
    fprintf (stderr, "%s: This program is copyrighted material.  The user\n",
        vtbasename);
    fprintf (stderr, "%s: is given the non-exclusive right to use, modify\n",
        vtbasename);
    fprintf (stderr, "%s: and redistribute this program or any derived\n",
        vtbasename);
    fprintf (stderr, "%s: work, subject to the following limitation:\n\n",
        vtbasename);
    fprintf (stderr, "%s: All copyright notices and warranty disclaimers\n",
        vtbasename);
    fprintf (stderr, "%s: must be left intact!  The user may not claim \n",
        vtbasename);
    fprintf (stderr, "%s: credit for code written by the author.\n\n",
        vtbasename);
}

/***************************************
 *
 *  vtprintcap      Loads a vtprintcap description for use.
 *
 *  Purpose         Allows use of user-defined printer control codes.
 *
 *  Parameters      None.
 *
 *  Returns         zero on successful lookup,
 *                  one on lookup failure,
 *                  negative one on file open failure,
 *                  negative two on file parse error.
 *
 */

int vtprintcap ()
{
    FILE *libfile;
    char buffer [1025];
    static char on [1025];
    static char off [1025];
    char *temp = NULL;

    if ((!termname)||(!*termname)) termname = getenv ("TERM");

    libfile = fopen (libtermname, "r");
    if (!libfile) return (-1);

    while (fgets (buffer, 1024, libfile))
    {
        if (buffer[0] == '#') buffer [0] = '\0';
        temp = strchr (buffer,'\n');
        if (temp)  *temp = '\0';

        temp = strtok (buffer, ", \t");
        while (temp)
        {
            if (!strcmp (temp, termname)) break;
            temp = strtok (NULL, ", \t");
        }
        if (temp) break;
    }
    if (temp) 
    {
        if (!fgets (on, 1025, libfile))
        {
            fclose (libfile);
            return (-2);
        }
        if (!fgets (off, 1025, libfile))
        {
            fclose (libfile);
            return (-2);
        }

        fclose (libfile);

        /* verify no illegal substitution formats are present */

        if (on [strlen (on)-1] == '\n') on [strlen (on) - 1] = '\0';
        if (off [strlen (off)-1] == '\n') off [strlen (off) -1 ] = '\0';

        temp = on;
        while (temp)
        {
            temp = strchr (temp, '%');
            if (temp)
            {
                if (temp[1] != '%') return (-2);
                temp += 2;
            }
        }
        temp = off;
        while (temp)
        {
            temp = strchr (temp, '%');
            if (temp)
            {
                if (temp[1] != '%') return (-2);
                temp += 2;
            }
        }

        if (!escape (on)) return (-2);
        if (!escape (off)) return (-2);

        ptr_on = on;
        ptr_off = off;
        return 0;
    };
    fclose (libfile);
    return 1;
}

/* >>>>>>>>>> Main <<<<<<<<<< */

int main (int argc, char *argv[])
{
    char *tmp;
    char *env;
    int  result;
    int  cnt = 0;
    int  total;

    arglist args;
    args.argv = NULL;

    /* what are we called? */
    vtbasename = strrchr (argv[0],'/');
    vtbasename = (vtbasename ? vtbasename+1 : argv[0]);

    /* set up the environment variable properly */
    tmp = getenv (vtbasename);
    if (!tmp) tmp = getenv ("VTPRINT");
    if (!tmp) tmp = "";

    env = (char *) malloc (strlen (tmp) + 1);
    if (!env)
    {
        fprintf (stderr,"%s: Out of memory!\n",  vtbasename);
        exit (1);
    }

    strcpy (env, tmp);

    /* now we go through getopt (twice actually) */

    /* first for the environment variable (if present) */

    if (env)
    {
        string2arglist (env, &args);
        process_opts (&args);
    }

    /* then command line parameters */
    argv2arglist (argc, argv, &args);
    process_opts (&args);

    if (!dowork) exit(0);

    /* display intro on screen */
    if (!quiet) intro();

    /* get library file settings if needed */
    if (!builtin)
    {
        result = vtprintcap ();
        switch (result)
        {
            case -2 : {
                        if ((!quiet) && (!forcenv))
                        {
                            fprintf (stderr, 
                            "%s: Parse error in %s, using builtin codes.\n",
                            vtbasename, libtermname);
                        }
                        else if (forcenv)
                        {
                            fprintf (stderr, "%s: Parse error in %s!\n", 
                                vtbasename, libtermname);
                            exit(1);
                        }
                        break;
                      }
            case -1 : {
                        if ((!quiet) && (!forcenv))
                        {
                            fprintf (stderr,
                            "%s: Can't open %s, using builtin codes.\n",
                            vtbasename, libtermname);
                        }
                        else if (forcenv)
                        {
                            fprintf (stderr, "%s: Can't open %s!\n",
                                vtbasename, libtermname);
                            exit (1);
                        }
                        break;
                      }
            case 0  : {
                        if (!quiet)
                        {
                            fprintf (stderr, "%s: Using %s control codes.\n",
                                vtbasename, termname);
                        }
                        break;
                      }
            case 1  : {
                        if ((!quiet) && (!forcenv))
                        {
                            fprintf (stderr,
                            "%s: Terminal %s unknown, using builtin codes.\n",
                            vtbasename, termname);
                        }
                        else if (forcenv)
                        {
                            fprintf (stderr,
                            "%s: Terminal %s unknown!\n", vtbasename, termname);
                            exit(1);
                        }
                        break;
                      }
            default : {
                        fprintf (stderr, "%s: INTERNAL ERROR!!\n", vtbasename);
                        abort();
                      }
        }
    }

    /* set up the output file descriptor */
    if (device) 
    {
        outfile = fopen (devname, "w");
        if (!outfile)
        {
            fprintf (stderr, "%s: Failed to open %s for writing!\n",
                vtbasename, devname);
            exit (1);
        }
        if (!quiet)
            fprintf (stderr,"%s: Using %s for output.\n", vtbasename, devname);
    }
    else
    {
        outfile = stdout;
        if (!quiet)
            fprintf (stderr, "%s: Using <stdout> for output.\n", vtbasename);
    }

    /* special handling for vtprton and vtprtoff */
    if (!strcmp (vtbasename, "vtprton"))
    {
        fflush (outfile);
        fprintf (outfile, "%s", ptr_on);
        fflush (outfile);
        exit (0);
    }
    if (!strcmp (vtbasename, "vtprtoff"))
    {
        fflush (outfile);
        fprintf (outfile, "%s", ptr_off);
        fflush (outfile);
        exit (0);
    }


    /* deal with terminal states */
    if (isatty (fileno(outfile)))
    {
        save_termstate(fileno (outfile), &oldstate);
        state_saved = 1;
        memcpy ((char *) &newstate, (char *) &oldstate, sizeof (termstate));
        if (binary)
        {
            raw_termstate(&newstate);
            restore_termstate (fileno (outfile), &newstate);
        }
    }
    else
    {
        binary = 0; /* force this! */
    }

    if (!quiet)
    {
        fprintf (stderr, "%s: Output flags: ", vtbasename);
        if (builtin) fprintf (stderr, "builtin ");
        if (binary) fprintf (stderr, "binary ");
        if (ffeed)  fprintf (stderr, "formfeed ");
        if (stripcr) fprintf (stderr, "noCR ");
        if (addcr) fprintf (stderr, "addCR ");
        fprintf (stderr, "\n");
        fflush (stderr);
    }

    /* establish signal handlers */
    signal (SIGHUP, sighandler);
    signal (SIGQUIT, sighandler);
    signal (SIGTSTP, sighandler);
    signal (SIGINT, sighandler);
    signal (SIGTERM, sighandler);

    total = argc - optind;

    /* the main processing loop */
    do
    {
        int ch;
        int lastch = 0;
        FILE *infile;

        if (optind == argc)
            infile = stdin;
        else infile = fopen (argv[optind], "r");

        if (!infile)
        {
            fprintf (stderr, "%s: Couldn't open %s for reading.\n",
                vtbasename, argv[optind]);
            optind++;
            continue;
        }

        fflush (outfile);
        fprintf (outfile, "%s", ptr_on);
        fflush (outfile);

        while ((ch = fgetc (infile)) != EOF)
        {
            switch (ch)
            {
                case CR :   {
                                if (!stripcr) fputc (ch, outfile);
                                break;
                            }
                case LF :   {
                                if ((lastch != CR) && (addcr))
                                    fputc (CR, outfile);
                                fputc (ch, outfile);
                                break;
                            }
                default :   {
                                if ((lastch == CR) && (stripcr))
                                    fputc (CR, outfile);
                                fputc (ch, outfile);
                                break;
                            }
            }
            lastch = ch;
        }

        fclose (infile);
        if (ffeed) fputc (FF, outfile);
        fprintf (outfile, "%s", ptr_off);
        fflush (outfile);

        cnt++;

        if (!quiet)
        {
            if (argc == optind)
                fprintf  (stderr, "%s: Printed <stdin>.\n", vtbasename);
            else
            fprintf (stderr, "%s: Printed %s.\n", vtbasename, argv[optind]);
        }   

        optind++;

    } while (optind < argc);

    if (state_saved) restore_termstate (fileno (outfile), &oldstate);

    if (!quiet) fprintf (stderr,
        "%s: Successfully printed %d file%s (%d specified).\n",
        vtbasename, cnt, cnt == 1 ? "" : "s", total);

    return 0;
}