home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / hpset.lzh / hpset.c next >
Text File  |  1995-03-21  |  10KB  |  570 lines

  1. /* hpset.c */
  2.  
  3. #define MAIN
  4.  
  5. #include <stdio.h>
  6. #include <termcap.h>
  7. #include <sgstat.h>
  8.  
  9.  
  10. /* These function declarations are needed to initialize
  11.     the cmds[] structure.
  12. */
  13.  
  14. char     *show_reset(), *show_orient(), *show_fsize(),
  15.         *show_fstyle(), *show_value(),    *show_wrap(),
  16.         *show_eol(), *show_lmargin(), *show_weight();
  17.  
  18. void    out_reset(), out_fsize(), out_fstyle(), out_value(), out_weight();
  19.  
  20. /* This structure contains everything we need to know about each
  21.     printer function. Note that we have included '%d's in the
  22.     .codes...these are replaced by actual values when they are
  23.     sent to the printer. Note that the order may be important...for example,
  24.     you most probably should make sure that RESET is the first function!
  25.     Other functions may depend on order (eg. margins may depend on font).
  26. */
  27.  
  28. struct
  29. {
  30.     char    *name;            /* option name */
  31.     int        default,        /* default value */
  32.             min,            /* minimum value */
  33.             max,            /* maximum value */
  34.             new;            /* new value set by user */
  35.     char    *codes;            /* printer codes for option */
  36.     char    *(*dfunc)();    /* function returning option setting name */
  37.     void    (*outfunc)();    /* function to send codes to printer */
  38. }cmds[]=
  39. {
  40.     "RESET",            1,    0,    1,        0,
  41.         "\x1bE",        show_reset,        out_reset,
  42.         
  43.     "Orientation",        0,    0,    1,        0,
  44.         "\x1b&l%dO",    show_orient,    out_value,
  45.     
  46.     "End of Line Wrap",    1,    0,    1,        0,
  47.         "\x1b&s%dC",    show_wrap,        out_value,
  48.     
  49.     "Line Termination",    0,    0,    3,        0,
  50.         "\x1b&k%dG",    show_eol,        out_value,
  51.     
  52.     "Font Size",        0,    0,    2,        0,
  53.         "\x1b(s%dH",    show_fsize,        out_fsize,
  54.         
  55.     "Font Style",        0,    0,    1,        0,
  56.         "\x1b(s%dS",    show_fstyle,    out_value,
  57.     
  58.     "Font weight",        1,    0,    2,        0,
  59.         "\x1b(s%dB",    show_weight,    out_weight,
  60.     
  61.     "Copies",            1,    1,    99,        0,
  62.         "\x1b&l%dx",    show_value,        out_value,
  63.         
  64.     "Lines per Inch",    6,    1,    48,        0,
  65.         "\x1b&l%dD",    show_value,        out_value,
  66.         
  67.     "Page Length",        60,    1,    200,    0,
  68.         "\x1b&l%dP",    show_value,        out_value,
  69.     
  70.     "Left Margin",        0,    0,    40,        0,
  71.         "\x1b&a%dL",    show_lmargin,    out_value
  72. };
  73.  
  74. FILE *outpath;
  75. char *printer="/p";
  76.  
  77. int        nrows,        /* number of rows on screen */
  78.         ncolumns,    /* number of columns on screen */
  79.         numcmds,
  80.         termcap_ok;
  81.  
  82. extern int errno;
  83.  
  84. #define TCAPSLEN 1024
  85.  
  86. /* Termcap variables */
  87.  
  88. char PC_,       /* pad character */
  89.      *BC,       /* cursor backspace */
  90.      *UP;       /* cursor up */
  91.  
  92. short ospeed;   /* terminal speed */
  93.  
  94. static char *CM,    /* cursor movement */
  95.             *CL,    /* clear screen    */
  96.             *SO,    /* standout start  */
  97.             *SE;    /* standout end    */
  98.  
  99. /*    ======================================================================
  100.     Inititialize the termcap stuff
  101. */
  102.  
  103. init_termcap()
  104. {
  105.     register char *term_type;
  106.     register int t;
  107.     static char tcapbuf[TCAPSLEN];
  108.     static char tcbuf[1024];
  109.     char *ptr;
  110.     char *temp;
  111.     
  112.     extern char *getenv();
  113.     
  114.     static struct
  115.     {
  116.         char *tcname;
  117.         char **tcptr;
  118.     }tc[]=
  119.     {
  120.         "cm", &CM,
  121.         "cl", &CL,
  122.         "so", &SO,
  123.         "se", &SE
  124.     };
  125.     
  126.     if((term_type=getenv("TERM"))==0)
  127.         terminate("Environment variable TERM not defined!");
  128.     
  129.     if(tgetent(tcbuf, term_type)<=0)
  130.         terminate("Unknown terminal type '%s'!",term_type);
  131.     
  132.     ptr=tcapbuf;
  133.     
  134.     if(temp=tgetstr("pc", &ptr)) PC_=*temp;
  135.     
  136.     for(t=0; t<sizeof(tc)/sizeof(tc[0]); t++)
  137.     {
  138.         if((*tc[t].tcptr=tgetstr(tc[t].tcname, &ptr))==NULL)
  139.             terminate("Termcap needs '%s' entry\n", tc[t].tcname);
  140.         if(ptr>=&tcbuf[TCAPSLEN])
  141.             terminate("Termcap description too big");
  142.     }
  143.     
  144.     nrows=tgetnum("li");
  145.     ncolumns=tgetnum("co");
  146.     
  147.     if(!nrows || !ncolumns)
  148.         terminate("Unable to determine screen size");
  149.         
  150.     if(ncolumns<80 || nrows<24)
  151.         terminate("Screen must be at least 80x24");
  152.     
  153.     termcap_ok=1;
  154. }
  155.  
  156. putpad(s)
  157. char *s;
  158. {
  159.    extern int write1();  /* function to print 1 character */
  160.    
  161.    if(s) tputs(s,1,write1);
  162. }
  163.  
  164. write1(c)
  165. char c;
  166. {
  167.     putc(c,stdout);
  168. }
  169.  
  170. gotoxy(x,y)        /* position cursor at specified x/y */
  171. int x,y;
  172. {
  173.    putpad(tgoto(CM,x,y));
  174. }
  175.  
  176. clrscrn()        /* Clear video screen */
  177. {
  178.    putpad(CL);
  179. }
  180.  
  181. revon()            /* Turn on reverse video */
  182. {
  183.    putpad(SO);
  184. }
  185.  
  186. revoff()        /* Turn off reverse video */
  187. {
  188.    putpad(SE);
  189. }
  190.  
  191.  
  192. /*    ==================================================
  193.     Set up the terminal
  194. */
  195.  
  196. static struct sgbuf pathbf1, pathbf2;
  197. static char pathbfv=0;   /* flag, 1==path ok to reset */
  198.  
  199.  
  200. setterml()
  201. {
  202.     setbuf(stdin, NULL);    /* single char input */
  203.     
  204.     if(!pathbfv)
  205.     {
  206.         _gs_opt(1,&pathbf1);
  207.         _gs_opt(1,&pathbf2);
  208.         pathbfv++;  /* signal okay to restore */
  209.         
  210.         pathbf1.sg_echo=
  211.         pathbf1.sg_pause=
  212.         pathbf1.sg_eofch=
  213.         pathbf1.sg_psch=
  214.         pathbf1.sg_kbich=
  215.         pathbf1.sg_kbach=0;
  216.     }
  217.     _ss_opt(1,&pathbf1);
  218.     ospeed=pathbf2.sg_baud;    /* set terminal speed for termcap */
  219. }
  220.  
  221. resterml()
  222. {
  223.     if(pathbfv) _ss_opt(1, &pathbf2);
  224.     fflush(stdout);
  225. }
  226.  
  227.  
  228. /*    ======================================================================
  229.     Display usage message
  230. */
  231.  
  232. help()
  233. {
  234.     register int t;
  235.     
  236.     static char *msg[]=
  237.     {
  238.         "Syntax: HPSet <opts>",
  239.         "Options:",
  240.         "\t-p=path     specify printer",
  241.         "\t-?          this message"
  242.     };
  243.     
  244.     for(t=0; t<sizeof(msg)/sizeof(msg[0]); puts(msg[t++]));
  245. }
  246.  
  247.  
  248. /*    ============================================================
  249.     Disaster exit
  250. */
  251.  
  252. terminate(s, p1, p2, p3, p4, p5)
  253. char *s, *p1, *p2, *p3, *p4, *p5;
  254. {
  255.     if(termcap_ok)
  256.     {
  257.         gotoxy(ncolumns-1, nrows-1);
  258.         putc('\n', stdout);
  259.     }
  260.     if(s)
  261.     {
  262.         printf(s, p1, p2, p3, p4, p5);
  263.         putc('\n', stdout);
  264.     }
  265.     resterml();
  266.     exit(0);
  267. }
  268.  
  269.  
  270. /*    ======================================================================
  271.     This set of functions are used by showline() to determine the
  272.     'value' to print for a options.
  273. */
  274.  
  275. char *
  276. show_reset(value)        /* reset printer yes/no */
  277. int value;
  278. {
  279.     static char *msg[]=
  280.     {
  281.         "No",
  282.         "Yes"
  283.     };
  284.     
  285.     return msg[value];
  286. }
  287.  
  288. char *
  289. show_orient(value)        /* page orientation */
  290. int value;
  291. {
  292.     static char *msg[]=
  293.     {
  294.         "Portrait",
  295.         "Landscape"
  296.     };
  297.     
  298.     return msg[value];
  299. }
  300.  
  301. char *
  302. show_fsize(value)        /* font pitch (cpi) */
  303. int value;
  304. {
  305.     static char *msg[]=
  306.     {
  307.         "Pica (10cpi)",
  308.         "Elite (12cpi)",
  309.         "Compressed (16.6cpi)"
  310.     };
  311.     
  312.     return msg[value];
  313. }
  314.  
  315. char *
  316. show_fstyle(value)        /* typestyle (roman/italic) */
  317. int value;
  318. {
  319.     static char *msg[]=
  320.     {
  321.         "Roman (upright)",
  322.         "Italic"
  323.     };
  324.     
  325.     return msg[value];
  326. }
  327.  
  328. char *
  329. show_wrap(value)        /* EOF wrap on/off */
  330. int value;
  331. {
  332.     static char *msg[]=
  333.     {
  334.         "Enabled",
  335.         "Disabled"
  336.     };
  337.     
  338.     return msg[value];
  339. }
  340.  
  341. char *
  342. show_eol(value)        /* EOL handling */
  343. int value;
  344. {
  345.     static char *msg[]=
  346.     {
  347.         "CR=CR, LF=LF, FF=FF",
  348.         "CR=CR+LF, LF=LF, FF=FF",
  349.         "CR=CR, LF=CR+LF, FF=CR+FF",
  350.         "CR=CR+LF, LF=CR+LF, FF=CR+FF"
  351.     };
  352.     
  353.     return msg[value];
  354. }
  355.  
  356. char *
  357. show_lmargin(value)        /* Left margin handling */
  358. int value;
  359. {
  360.     static char buff[20];
  361.     
  362.     sprintf(buff, "%d characters", value);
  363.     return buff;
  364. }
  365.  
  366. char *
  367. show_weight(value)        /* Typeface weight */
  368. int value;
  369. {
  370.     static char *msg[]=
  371.     {
  372.         "Light",
  373.         "Medium",
  374.         "Bold"
  375.     };
  376.     
  377.     return msg[value];
  378. }
  379.  
  380. char *
  381. show_value(value)        /* numeric values */
  382. int value;
  383. {
  384.     static char buff[20];
  385.     
  386.     sprintf(buff, "%d", value);
  387.     return buff;
  388. }
  389.  
  390. /*    ======================================================================
  391.     These functions send the option value to the printer. They are needed
  392.     since some cmds[] values need to be converted to printer values.
  393. */
  394.  
  395. void
  396. out_reset(n)        /* reset...don't send if value==0 */
  397. register int n;
  398. {
  399.     if(cmds[n].new) fprintf(outpath, cmds[n].codes);
  400. }
  401.  
  402. void
  403. out_fsize(n)        /* font size (pitch) */
  404. register int n;
  405. {
  406.     register int t;
  407.     
  408.     switch(cmds[n].new)
  409.     {
  410.         default:
  411.         case 0: t=10; break;
  412.         case 1: t=12; break;
  413.         case 2: t=17; break;
  414.     }
  415.     fprintf(outpath, cmds[n].codes, t);
  416. }
  417.  
  418. void
  419. out_weight(n)
  420. {
  421.     register int t;
  422.     
  423.     switch(cmds[n].new)
  424.     {
  425.         default:
  426.         case 0: t=-3; break;
  427.         case 1: t=0; break;
  428.         case 2: t=3; break;
  429.     }
  430.     fprintf(outpath, cmds[n].codes, t);
  431. }    
  432.  
  433. void
  434. out_value(n)        /* send a numeric value */
  435. register int n;
  436. {
  437.     fprintf(outpath, cmds[n].codes, cmds[n].new);
  438. }
  439.  
  440. /*    ======================================================================
  441.     Display a option line on the screen
  442. */
  443.  
  444. showline(n, mode)
  445. int n, mode;
  446. {
  447.     register char *p1, *p2;
  448.     register int l1, l2;
  449.     
  450.     gotoxy(5, n+2);
  451.     if(mode) revon();
  452.     
  453.     p1=cmds[n].name;
  454.     p2=(*cmds[n].dfunc)(cmds[n].new);
  455.     
  456.     l1=strlen(p1);
  457.     l2=strlen(p2);
  458.     
  459.     fputs(p1, stdout);
  460.     while(l1++<20) putc(' ', stdout);
  461.     fputs(p2, stdout);
  462.     while(l2++<30) putc(' ', stdout);
  463.     
  464.     if(mode) revoff();
  465. }
  466.  
  467.  
  468. /* ======================================================================
  469.     M A I N
  470. */
  471.  
  472. main(argc, argv)
  473. int argc;
  474. char *argv[];
  475. {
  476.     register int t, key, n;
  477.     register char *p;
  478.     
  479.     for(t=1; t<argc; t++)
  480.     {
  481.         p=argv[t];
  482.         if(*p++!='-') usage();
  483.         switch(tolower(*p++))
  484.         {
  485.             case 'p':
  486.                 if(*p=='=') p++;
  487.                 printer=p;
  488.                 break;
  489.                 
  490.             default:
  491.                 usage();
  492.         }
  493.     }
  494.     init_termcap();
  495.     setterml();
  496.     
  497.     for(t=0; t<sizeof(cmds)/sizeof(cmds[0]); t++)
  498.         cmds[t].new=cmds[t].default;
  499.     
  500.     clrscrn();
  501.     gotoxy(0,0);
  502.     printf("      HPSET - HP Printer Setup -- (c) 1995, Bob van der Poel Software");
  503.     gotoxy(0,nrows-1);
  504.     printf("KEYS: j/k move cursor, h/l change setting, q to quit, <ENTER> sets printer");
  505.     
  506.     for(t=0; t<sizeof(cmds)/sizeof(cmds[0]); t++) showline(t, 0);
  507.     
  508.     for(t=0; ; )
  509.     {
  510.         showline(t, 1);
  511.         key=getc(stdin);
  512.         showline(t, 0);
  513.         
  514.         switch(tolower(key))
  515.         {
  516.             case 'j':
  517.                 if(t< sizeof(cmds)/sizeof(cmds[0])-1) t++;
  518.                 else t=0;
  519.                 break;
  520.             
  521.             case 'k':
  522.                 if(t) t--;
  523.                 else t=sizeof(cmds)/sizeof(cmds[0])-1;
  524.                 break;
  525.                 
  526.             case 'l':
  527.                 if(cmds[t].new<cmds[t].max) cmds[t].new++;
  528.                 else cmds[t].new=cmds[t].min;
  529.                 break;
  530.                 
  531.             case 'h':
  532.                 if(cmds[t].new>cmds[t].min) cmds[t].new--;
  533.                 else cmds[t].new=cmds[t].max;
  534.                 break;
  535.             
  536.             case '\n':
  537.                 if((outpath=fopen(printer, "w"))==NULL)
  538.                     terminate("Can't open path to '%s', error %d",
  539.                         printer, errno);
  540.                 
  541.                 for(n=0; n<sizeof(cmds)/sizeof(cmds[0]); n++)
  542.                      (*cmds[n].outfunc)(n);
  543.                 fclose(outpath);
  544.                 
  545.                 terminate("Printer codes sent");
  546.             
  547.             case 'q':
  548.                 terminate("\x07*** PRINTER NOT SET ***");
  549.                 
  550.         }
  551.     }
  552. }
  553.  
  554. usage()
  555. {
  556.     static char *msg[]=
  557.     {
  558.         "HPSET (c) Bob van der Poel Software, 1995",
  559.         "Syntax: Hpset [opts]",
  560.         "Usage: Sets options on HP Laser Printer",
  561.         "Options:",
  562.         "   -p[=]path    set printer port (default=/p)"
  563.     };
  564.     
  565.     register int t;
  566.     
  567.     for(t=0; t<sizeof(msg)/sizeof(msg[0]); puts(msg[t++]) );
  568.     terminate(NULL);
  569. }
  570.