home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / CreativeComputers.iso / shareware / text / dvi_3.62 / source / dvisrc.lha / dvicmd.c < prev    next >
C/C++ Source or Header  |  1994-01-11  |  19KB  |  831 lines

  1. /*
  2. ** Datei: DVICMD.C
  3. ** Autor: Ingo Eichenseher
  4. **        Gerhard Wilhelms
  5. **
  6. ** Aenderung: 12.11.92
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <setjmp.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include <math.h>
  14. #ifdef __TURBOC__
  15. #ifdef IBMPC
  16. #include <conio.h>
  17. #else
  18. #include <ext.h>
  19. #endif
  20. #endif
  21. #include <stdarg.h>
  22. #include <stdlib.h>
  23. #ifndef UNIX
  24. #include <stdlib.h>
  25. #endif
  26. #include "dvimisc.h"
  27. #include "dvi.h"
  28. #include "dvidvi.h"
  29. #include "dviframe.h"
  30. #include "dvihdcp.h"
  31. #include "dvisplin.h"
  32. #include "dvidraw.h"
  33.  
  34. #define TERM_LEN 78
  35.  
  36. #if defined(IBMPC) && defined(__TURBOC__)
  37. unsigned _stklen = 16U * 1024U;
  38. #endif
  39.  
  40. static jmp_buf      halt_jump;
  41. static FILE        *input_file=NULL;
  42. static char        *path;
  43. static char         input_name[128]="dvi.opt";
  44. static long         lower[10], upper[10];
  45.  
  46. static int (*out_rout[])(void) =
  47. {
  48.     screen,
  49.     p6low,
  50.     p6high,
  51.     p6mid,
  52.     fx80,
  53.     shipfile,
  54.     hplj,
  55.     hpljlow,
  56.     bj300,
  57.     NULL
  58. };
  59.  
  60.  
  61. void out_newline(void)
  62. {
  63.     putchar('\n');
  64. }
  65.  
  66. void out_string(char *s)
  67. {
  68.     fputs(s,stdout);
  69.     fflush(stdout);
  70. }
  71.  
  72. void halt(char *format, ... )
  73. {
  74.     extern void vprint(char *, va_list);
  75.     va_list l;
  76.     va_start(l,format);
  77.     vprint(format,l);
  78.     va_end(l);
  79.     dvi_clean();
  80.     longjmp(halt_jump,1);
  81. }
  82.  
  83. int wait_for_sheet(void)
  84. {
  85.     int c;
  86.     printf("Insert new sheet into printer\n");
  87.     printf("Continue (y/n) ? ");
  88.     c=getch(); c=tolower(c); putchar('\n');
  89. #if UNIX
  90.     while(getchar()!='\n');
  91. #endif
  92.     return c=='n';
  93. }
  94.  
  95. static int get_onoff(char *string)
  96. {
  97.     if (!strcmp(string,"on")) return 1;
  98.     else if (!strcmp(string,"off")) return 0;
  99.     else halt("'on' or 'off' expected");
  100.     return 0;
  101. }
  102.  
  103. static void get_pages(char *string, int *first, int *last, int *step)
  104. {
  105.     char *s;
  106.     *step=1; *first=1; *last=9999;
  107.     if (!*string) return;
  108.     if (isdigit(*string)) *first=(int)strtol(string,&s,10);
  109.     else s=string;
  110.     if (*s==':' || *s=='-')
  111.     {
  112.     s++;
  113.     if (isdigit(*s))
  114.     {
  115.         *last=(int)strtol(string=s,&s,10);
  116.         if (*s++==':')
  117.         if (isdigit(*s))
  118.             *step=(int)strtol(string=s,&s,10);
  119.     }
  120.     else *last=9999;
  121.     }
  122.     else *last=*first;
  123.     if (*step==0) *step=1;
  124.     if (*first<*last && *step<0) *step = -*step;
  125.     if (*last<*first && *step>0) *step = -*step;
  126. }
  127.  
  128. static void get_dimen(char *string, double *dimen)
  129. {
  130.     char *s=string, c;
  131.     double r;
  132.     while(isdigit(*s) || *s=='.') s++;
  133.     if (s==string)
  134.     {
  135.     print("Illegal dimension");
  136.     return;
  137.     }
  138.     strlwr(s); c = *s; *s=0; r=atof(string); *s=c;
  139.     if (r==0.0 && *s=='\0') *dimen=0.0;
  140.     else if (!strcmp(s,"in")) *dimen=r;
  141.     else if (!strcmp(s,"cm")) *dimen=r/2.54;
  142.     else if (!strcmp(s,"pt")) *dimen=r/72.27;
  143.     else if (!strcmp(s,"pc")) *dimen=r*12.0/72.27;
  144.     else if (!strcmp(s,"bp")) *dimen=r/72.0;
  145.     else if (!strcmp(s,"mm")) *dimen=r/25.4;
  146.     else if (!strcmp(s,"dd")) *dimen=r*1238.0/72.27;
  147.     else if (!strcmp(s,"cc")) *dimen=r*12*1238.0/72.27;
  148.     else if (!strcmp(s,"sp")) *dimen=r/72.27/65536.0;
  149.     else
  150.     {
  151.     print("Illegal dimension");
  152.     return;
  153.     }
  154. }
  155.  
  156. static int is_default(long l[10])
  157. {
  158.     int i;
  159.     for (i=0; i<10; i++)
  160.     if (l[i]!=ASTERISK) return 0;
  161.     return 1;
  162. }
  163.  
  164. static int get_line(char *line, int file_only)
  165. {
  166.     if (input_file!=NULL)
  167.     {
  168.     if (fgets(line,128-1,input_file)==NULL)
  169.     {
  170.         fclose(input_file);
  171.         input_file=NULL;
  172.         return get_line(line,file_only);
  173.     }
  174.     else
  175.     {
  176.         char *p;
  177.         line[127]='\0';
  178.         if ((p=strrchr(line,'\n'))!=NULL) *p='\0';
  179.         /* if (*line) print("dvi>%s",line); */
  180.     }
  181.     }
  182.     else
  183.     {
  184.     if (file_only) return 0;
  185.     printnnl("dvi>");
  186.     gets(line);
  187.     term_clean();
  188.     }
  189.     return 1;
  190. }
  191.  
  192. typedef enum
  193. {
  194.     cmd_not_found=-2, cmd_ambiguous=-1,
  195.     cmd_hresolution, cmd_pkpath, cmd_width, cmd_height,
  196.     cmd_options, cmd_dvifile, cmd_format, cmd_exit,
  197.     cmd_output, cmd_vresolution, cmd_hoffset, cmd_voffset,
  198.     cmd_hspread, cmd_vspread, cmd_eject, cmd_separate,
  199.     cmd_lower, cmd_upper, cmd_input, cmd_magnification,
  200.     cmd_thinout, cmd_path, cmd_landscape, cmd_logfile,
  201.     cmd_hmargin, cmd_vmargin, cmd_copies, cmd_page,
  202.     cmd_pictures, cmd_imgpath, cmd_density, cmd_vfpath,
  203.     cmd_grpath, cmd_showfonts, cmd_singlesheet, cmd_memory,
  204.     cmd_tracemem, cmd_tracechars, cmd_tfmpath,
  205.     cmd_redirect, cmd_pixmem, cmd_maxmem, cmd_pathmem,
  206.     cmd_port
  207. } cmdnum_t;
  208.  
  209. typedef struct
  210. {
  211.     char *cmd_name;
  212.     int cmd_number;
  213. } cmd_t;
  214.  
  215. static cmd_t cmd_list[] =
  216. {
  217.     { "hresolution",    cmd_hresolution },
  218.     { "pkpath",         cmd_pkpath },
  219.     { "tfmpath",        cmd_tfmpath },
  220.     { "width",          cmd_width },
  221.     { "height",         cmd_height },
  222.     { "options",        cmd_options },
  223.     { "dvifile",        cmd_dvifile },
  224.     { "format",         cmd_format },
  225.     { "exit",           cmd_exit },
  226.     { "output",         cmd_output },
  227.     { "vresolution",    cmd_vresolution },
  228.     { "hoffset",        cmd_hoffset },
  229.     { "voffset",        cmd_voffset },
  230.     { "hspread",        cmd_hspread },
  231.     { "vspread",        cmd_vspread },
  232.     { "eject",          cmd_eject },
  233.     { "separate",       cmd_separate },
  234.     { "lower",          cmd_lower },
  235.     { "upper",          cmd_upper },
  236.     { "input",          cmd_input },
  237.     { "magnification",  cmd_magnification },
  238.     { "thinout",        cmd_thinout },
  239.     { "path",           cmd_path },
  240.     { "landscape",      cmd_landscape },
  241.     { "logfile",        cmd_logfile },
  242.     { "hmargin",        cmd_hmargin },
  243.     { "vmargin",        cmd_vmargin },
  244.     { "copies",         cmd_copies },
  245.     { "page",           cmd_page },
  246.     { "pictures",       cmd_pictures },
  247.     { "imgpath",        cmd_imgpath },
  248.     { "density",        cmd_density },
  249.     { "vfpath",         cmd_vfpath },
  250.     { "grpath",         cmd_grpath },
  251.     { "showfonts",      cmd_showfonts },
  252.     { "singlesheet",    cmd_singlesheet },
  253.     { "memory",         cmd_memory },
  254.     { "tracemem",       cmd_tracemem },
  255.     { "tracechars",     cmd_tracechars },
  256.     { "redirect",       cmd_redirect },
  257.     { "pixmem",         cmd_pixmem },
  258.     { "maxmem",         cmd_maxmem },
  259.     { "pathmem",        cmd_pathmem },
  260.     { "port",           cmd_port },
  261.     { NULL,             cmd_not_found }
  262. };
  263.  
  264.  
  265. static cmd_t out_list[]=
  266. {
  267.     { "screen",     0 },
  268.     { "p6low",      1 },
  269.     { "p6high",     2 },
  270.     { "p6mid",      3 },
  271.     { "fx80",       4 },
  272.     { "file",       5 },
  273.     { "hphigh",     6 },
  274.     { "hplow",      7 },
  275.     { "bj300",      8 },
  276.     { "null",       9 },
  277.     { NULL,        -1 }
  278. };
  279.  
  280. static cmdnum_t cmp_cmd(char *cmd, cmd_t *cmd_list)
  281. {
  282.     int ret = cmd_not_found;
  283.  
  284.     strlwr(cmd);
  285.     for (; cmd_list->cmd_name!=NULL; cmd_list++)
  286.     {
  287.     if (!strncmp(cmd,cmd_list->cmd_name, strlen(cmd)))
  288.     {
  289.         if (ret!=cmd_not_found) return cmd_ambiguous;
  290.         else ret = cmd_list->cmd_number;
  291.     }
  292.     }
  293.     return ret;
  294. }
  295.  
  296. static char *cmd_name(cmdnum_t n)
  297. {
  298.     cmd_t *p;
  299.  
  300.     for (p=cmd_list; p->cmd_name!=NULL && p->cmd_number!=n; p++);
  301.     if (p->cmd_name==NULL) return "";
  302.     else return p->cmd_name;
  303. }
  304.  
  305. static void get_output(char *arg)
  306. {
  307.     int i;
  308.  
  309.     if ((i=cmp_cmd(arg,out_list))<=cmd_ambiguous)
  310.     print("Unknown or ambiguous output device");
  311.     else
  312.     shipout = out_rout[i];
  313. }
  314.  
  315. static char *onoff(int v)
  316. {
  317.     return v ? "on":"off";
  318. }
  319.  
  320. static void do_commands(int file_only)
  321. {
  322.     char line[128], *cmd, *arg;
  323.     FILE *opt_file;
  324.     long r;
  325.     int i, k;
  326.  
  327.     while(1)
  328.     {
  329.     if (!get_line(line,file_only)) return;
  330.     cmd=strtok(line," =\t\n");
  331.     if (cmd==NULL) continue;
  332.     if (*cmd=='\0') continue;
  333.     arg=strtok((char*)NULL," =\t\n");
  334.  
  335.     switch(i=cmp_cmd(cmd,cmd_list))
  336.     {
  337.         case cmd_ambiguous:
  338.         print("Ambiguous command name");
  339.         break;
  340.  
  341.         case cmd_hresolution :
  342.         if (arg) r=atol(arg);
  343.         if (r<=0) print("Illegal resolution");
  344.         else op.hres=r;
  345.         break;
  346.  
  347. #ifdef IBMPC
  348.         case cmd_port :
  349.         if (arg) r=atol(arg);
  350.         if (r<0 || r>=4) print("Illegal port number");
  351.         else op.biosdev=(int)r;
  352.         break;
  353. #endif
  354.  
  355.         case cmd_pkpath :
  356.         if (arg!=NULL) strcpy(op.pk_path,arg);
  357.         else *op.pk_path=0;
  358.         break;
  359.  
  360.         case cmd_tfmpath :
  361.         if (arg!=NULL) strcpy(op.tfm_path,arg);
  362.         else *op.tfm_path=0;
  363.         break;
  364.  
  365.         case cmd_width :
  366.         if (arg==NULL) op.width=0.0;
  367.         else get_dimen(arg,&op.width);
  368.         break;
  369.  
  370.         case cmd_height :
  371.         if (arg==NULL) op.height=0.0;
  372.         else get_dimen(arg,&op.height);
  373.         break;
  374.  
  375.         case cmd_options :
  376.         if (arg)
  377.         {
  378.             catfe(input_name,arg,"opt");
  379.             opt_file=fopen(input_name,"w");
  380.         }
  381.         else opt_file=stdout;
  382.         if (opt_file==NULL)
  383.         {
  384.             print("Cannot open file %s",arg);
  385.             break;
  386.         }
  387.         fprintf(opt_file,"%s = %ld\n",
  388.             cmd_name(cmd_hresolution),op.hres);
  389.         fprintf(opt_file,"%s = %ld\n",
  390.             cmd_name(cmd_vresolution),op.vres);
  391.         if (op.new_mag)
  392.             fprintf(opt_file,"%s = %ld\n",
  393.             cmd_name(cmd_magnification),op.new_mag);
  394.         if (*op.pk_path)
  395.             fprintf(opt_file,"%s = %s\n",
  396.             cmd_name(cmd_pkpath),op.pk_path);
  397.         if (*op.tfm_path)
  398.             fprintf(opt_file,"%s = %s\n",
  399.             cmd_name(cmd_tfmpath),op.tfm_path);
  400.         if (*op.dvi_path)
  401.             fprintf(opt_file,"%s = %s\n",
  402.             cmd_name(cmd_path),op.dvi_path);
  403.         if (*op.img_path)
  404.             fprintf(opt_file,"%s = %s\n",
  405.             cmd_name(cmd_imgpath),op.img_path);
  406.         if (*op.input_path)
  407.             fprintf(opt_file,"%s = %s\n",
  408.             cmd_name(cmd_grpath),op.input_path);
  409.         if (*op.vf_path)
  410.             fprintf(opt_file,"%s = %s\n",
  411.             cmd_name(cmd_vfpath),op.vf_path);
  412.         if (!is_default(lower))
  413.             fprintf(opt_file,"%s = %s\n",
  414.             cmd_name(cmd_lower), fmt_page(lower));
  415.         if (!is_default(upper))
  416.             fprintf(opt_file,"%s = %s\n",
  417.             cmd_name(cmd_upper), fmt_page(upper));
  418.         if (op.width)
  419.             fprintf(opt_file,"%s = %.3fin\n",
  420.             cmd_name(cmd_width),op.width);
  421.         if (op.height)
  422.             fprintf(opt_file,"%s = %.3fin\n",
  423.             cmd_name(cmd_height),op.height);
  424.         if (op.hoffset)
  425.             fprintf(opt_file,"%s = %.3fin\n",
  426.             cmd_name(cmd_hoffset),op.hoffset);
  427.         if (op.voffset)
  428.             fprintf(opt_file,"%s = %.3fin\n",
  429.             cmd_name(cmd_voffset),op.voffset);
  430.         if (op.hmargin)
  431.             fprintf(opt_file,"%s = %.3fin\n",
  432.             cmd_name(cmd_hmargin),op.hmargin);
  433.         if (op.vmargin)
  434.             fprintf(opt_file,"%s = %.3fin\n",
  435.             cmd_name(cmd_vmargin),op.vmargin);
  436.         if (op.hspread)
  437.             fprintf(opt_file,"%s = %.3fin\n",
  438.             cmd_name(cmd_hspread),op.hspread);
  439.         if (op.vspread)
  440.             fprintf(opt_file,"%s = %.3fin\n",
  441.             cmd_name(cmd_vspread),op.vspread);
  442.         fprintf(opt_file,"%s = %s\n",
  443.             cmd_name(cmd_eject), onoff(op.formfeed));
  444.         fprintf(opt_file,"%s = %s\n",
  445.             cmd_name(cmd_separate), onoff(op.separate));
  446.         if (op.showfonts)
  447.             fprintf(opt_file,"%s = %s\n",
  448.             cmd_name(cmd_showfonts), onoff(op.showfonts));
  449.         if (op.landscape)
  450.             fprintf(opt_file,"%s = %s\n",
  451.             cmd_name(cmd_landscape), onoff(op.landscape));
  452.         if (op.copies!=1)
  453.             fprintf(opt_file,"%s = %d\n",
  454.             cmd_name(cmd_copies),op.copies);
  455.         fprintf(opt_file,"%s = %s\n",
  456.             cmd_name(cmd_pictures), onoff(op.show_img));
  457.         fprintf(opt_file,"%s = %s\n",
  458.             cmd_name(cmd_memory), onoff(op.dvimemory));
  459.         fprintf(opt_file,"%s = %s\n",
  460.             cmd_name(cmd_tracemem), onoff(op.tracemem));
  461.         fprintf(opt_file,"%s = %s\n",
  462.             cmd_name(cmd_tracechars), onoff(op.tracechars));
  463.         fprintf(opt_file,"%s = %g\n",
  464.             cmd_name(cmd_density),op.density);
  465.         fprintf(opt_file,"%s = %s\n",
  466.             cmd_name(cmd_singlesheet), onoff(op.singlesheet));
  467.         fprintf(opt_file,"%s = %s\n",
  468.             cmd_name(cmd_thinout), onoff(op.thin_out));
  469.         for (k=0; out_rout[k]!=NULL; k++)
  470.             if (shipout==out_rout[k])
  471.             fprintf(opt_file,"%s = %s\n",
  472.                 cmd_name(cmd_output),out_list[k].cmd_name);
  473.         if (op.pixmem)
  474.             fprintf(opt_file,"%s = %ld\n",
  475.             cmd_name(cmd_pixmem),op.pixmem);
  476.         if (op.maxmem)
  477.             fprintf(opt_file,"%s = %ld\n",
  478.             cmd_name(cmd_maxmem),op.maxmem);
  479.         if (op.pathmem)
  480.             fprintf(opt_file,"%s = %ld\n",
  481.             cmd_name(cmd_pathmem),op.pathmem);
  482.         if (*op.redirect)
  483.             fprintf(opt_file,"%s = %s\n",
  484.             cmd_name(cmd_redirect),op.redirect);
  485. #ifdef IBMPC
  486.         if (op.biosdev!=0)
  487.             fprintf(opt_file,"%s = %d\n",
  488.             cmd_name(cmd_port),op.biosdev);
  489. #endif
  490.         if (opt_file!=stdout) fclose(opt_file);
  491.         break;
  492.  
  493.         case cmd_dvifile :
  494.         if (arg!=NULL) strcpy(dvi_name,arg);
  495.         break;
  496.  
  497.         case cmd_format :
  498.         if (*dvi_name)
  499.         {
  500.             int first, last, step;
  501.             if (arg==NULL)
  502.             {
  503.             first=1; last=9999; step=1;
  504.             }
  505.             else get_pages(arg,&first,&last,&step);
  506.             format_pages(first,last,step);
  507.         }
  508.         else print("No filename specified");
  509.         break;
  510.  
  511.         case cmd_exit :
  512.         return;
  513.  
  514.         case cmd_output :
  515.         if (arg) get_output(arg);
  516.         break;
  517.  
  518.         case cmd_vresolution :
  519.         if (arg==NULL) break;
  520.         r=atol(arg);
  521.         if (r<=0) print("Illegal resolution");
  522.         else op.vres=r;
  523.         break;
  524.  
  525.         case cmd_hoffset :
  526.         if (arg==NULL) op.hoffset=0.0;
  527.         else get_dimen(arg,&op.hoffset);
  528.         break;
  529.  
  530.         case cmd_voffset :
  531.         if (arg==NULL) op.voffset=0.0;
  532.         else get_dimen(arg,&op.voffset);
  533.         break;
  534.  
  535.         case cmd_hspread :
  536.         if (arg==NULL) op.hspread=0.0;
  537.         else get_dimen(arg,&op.hspread);
  538.         break;
  539.  
  540.         case cmd_vspread :
  541.         if (arg==NULL) op.vspread=0.0;
  542.         else get_dimen(arg,&op.vspread);
  543.         break;
  544.  
  545.         case cmd_eject :
  546.         if (arg==NULL) print(onoff(op.formfeed));
  547.         else op.formfeed = get_onoff(arg);
  548.         break;
  549.  
  550.         case cmd_separate :
  551.         if (arg==NULL) print(onoff(op.separate));
  552.         else
  553.         {
  554.             op.separate = get_onoff(arg);
  555.             if (op.separate) op.thin_out = 0;
  556.         }
  557.         break;
  558.  
  559.         case cmd_lower :
  560.         if (arg==NULL) break;
  561.         if (!get_bound(arg,lower))
  562.             print("Illegal page specification");
  563.         break;
  564.  
  565.         case cmd_upper :
  566.         if (arg==NULL) break;
  567.         if (!get_bound(arg,upper))
  568.             print("Illegal page specification");
  569.         break;
  570.  
  571.         case cmd_not_found : arg=cmd;
  572.         case cmd_input :
  573.         if (arg==NULL) break;
  574.         catfe(input_name,arg,"opt");
  575.         input_file = fopene(input_name,"r",path,NULL);
  576.         if (input_file==NULL)
  577.             print("Cannot open file %s",input_name);
  578.         break;
  579.  
  580.         case cmd_magnification :
  581.         if (arg) r=atol(arg);
  582.         else break;
  583.         if (r<0) print("Illegal magnification");
  584.         else op.new_mag=r;
  585.         break;
  586.  
  587.         case cmd_thinout :
  588.         if (arg==NULL) print(onoff(op.thin_out));
  589.         else
  590.         {
  591.             op.thin_out = get_onoff(arg);
  592.             if (op.thin_out) op.separate = 0;
  593.         }
  594.         break;
  595.  
  596.         case cmd_path :
  597.         if (arg!=NULL) strcpy(op.dvi_path,arg);
  598.         else *op.dvi_path=0;
  599.         break;
  600.  
  601.         case cmd_showfonts :
  602.         if (arg==NULL) print(onoff(op.showfonts));
  603.         else op.showfonts = get_onoff(arg);
  604.         break;
  605.  
  606.         case cmd_tracechars :
  607.         if (arg==NULL) print(onoff(op.tracechars));
  608.         else op.tracechars = get_onoff(arg);
  609.         break;
  610.  
  611.         case cmd_tracemem :
  612.         if (arg==NULL) print(onoff(op.tracemem));
  613.         else op.tracemem = get_onoff(arg);
  614.         break;
  615.  
  616.         case cmd_singlesheet :
  617.         if (arg==NULL) print(onoff(op.singlesheet));
  618.         else op.singlesheet = get_onoff(arg);
  619.         break;
  620.  
  621.         case cmd_memory :
  622.         if (arg==NULL) print(onoff(op.dvimemory));
  623.         else op.dvimemory = get_onoff(arg);
  624.         break;
  625.  
  626.         case cmd_pixmem :
  627.         if (arg==NULL) print("pixmem=%ld",op.pixmem);
  628.         else op.pixmem = atol(arg);
  629.         break;
  630.  
  631.         case cmd_maxmem :
  632.         if (arg==NULL) print("maxmem=%ld",op.maxmem);
  633.         else op.maxmem = atol(arg);
  634.         break;
  635.  
  636.         case cmd_pathmem :
  637.         if (arg==NULL) print("pathmem=%ld",op.pathmem);
  638.         else op.pathmem = atol(arg);
  639.         break;
  640.  
  641.         case cmd_landscape :
  642.         if (arg==NULL) print(onoff(op.landscape));
  643.         else op.landscape = get_onoff(arg);
  644.         break;
  645.  
  646.         case cmd_logfile :
  647.         if (arg==NULL) op.log_name[0]='\0';
  648.         else catfe(op.log_name,arg,"log");
  649.         break;
  650.  
  651.         case cmd_redirect :
  652.         if (arg==NULL) op.redirect[0]='\0';
  653.         else strcpy(op.redirect,arg);
  654.         break;
  655.  
  656.         case cmd_hmargin :
  657.         if (arg==NULL) op.hmargin=0.0;
  658.         else get_dimen(arg,&op.hmargin);
  659.         break;
  660.  
  661.         case cmd_vmargin :
  662.         if (arg==NULL) op.vmargin=0.0;
  663.         else get_dimen(arg,&op.vmargin);
  664.         break;
  665.  
  666.         case cmd_copies :
  667.         if (arg==NULL) op.copies=1;
  668.         else op.copies=atoi(arg);
  669.         break;
  670.  
  671.         case cmd_page :
  672.         if (arg==NULL) prbyte(12);
  673.         else
  674.         {
  675.             int k=atoi(arg);
  676.             while(k-- > 0) prbyte(12);
  677.         }
  678.         break;
  679.  
  680.         case cmd_pictures :
  681.         if (arg==NULL) print(onoff(op.show_img));
  682.         else op.show_img = get_onoff(arg);
  683.         break;
  684.  
  685.         case cmd_imgpath :
  686.         if (arg!=NULL) strcpy(op.img_path,arg);
  687.         else *op.img_path=0;
  688.         break;
  689.  
  690.         case cmd_density :
  691.         if (arg==NULL) print("Density = %g",op.density);
  692.         else
  693.         {
  694.             op.density = atof(arg);
  695.             if (op.density < 0.0) op.density = 0.0;
  696.             if (op.density > 1.0) op.density = 1.0;
  697.         }
  698.         break;
  699.  
  700.         case cmd_vfpath :
  701.         if (arg!=NULL) strcpy(op.vf_path,arg);
  702.         else *op.vf_path=0;
  703.         break;
  704.  
  705.         case cmd_grpath :
  706.         if (arg!=NULL) strcpy(op.input_path,arg);
  707.         else *op.input_path=0;
  708.         break;
  709.     }
  710.     switch(i)
  711.     {
  712.         case cmd_ambiguous:
  713.         case cmd_not_found:
  714.         case cmd_format:
  715.         case cmd_input:
  716.         break;
  717.  
  718.         default:
  719.         dvi_clean();
  720.     }
  721.     }
  722. }
  723.  
  724. static char *get_arg(int *argc, char ***argv)
  725. {
  726.     if ( (**argv)[2] ) return (**argv)+2;
  727.     else if (*argc>1)
  728.     {
  729.     if ( *(*argv)[1]!='-' )
  730.     {
  731.         (*argc)--;
  732.         return *++(*argv);
  733.     }
  734.     else return "";
  735.     }
  736.     else return "";
  737. }
  738.  
  739. int main(int argc,char **argv)
  740. {
  741.     int interactive=1;
  742.     int i, first, last, step;
  743.  
  744.     print( "This is DVI Version %s" , version_string );
  745.  
  746.     shipout = screen;
  747.     path = getenv("PATH");
  748.  
  749.     install();
  750.     if( !gr_install() )
  751.         exit(1);
  752.  
  753.     for (i=0; i<10; i++) lower[i] = upper[i] = ASTERISK;
  754.     for(argc--,argv++; argc; argc--, argv++)
  755.     {
  756.     if (**argv=='-')
  757.     {
  758.         switch(tolower((*argv)[1]))
  759.         {
  760.         case 'i' :
  761.             strcpy(input_name,get_arg(&argc,&argv));
  762.             break;
  763.         case 'f' :
  764.             get_pages(get_arg(&argc,&argv),&first,&last,&step);
  765.             interactive=0;
  766.             break;
  767.         case 'o' :
  768.             get_output(get_arg(&argc,&argv));
  769.             break;
  770.         case 'l' :
  771.             strcpy(op.log_name,get_arg(&argc,&argv));
  772.             break;
  773.         }
  774.     }
  775.     else
  776.     {
  777.         strcpy(dvi_name,*argv);
  778.     }
  779.     }
  780.  
  781.  
  782.     if (*op.log_name)
  783.     {
  784.     catfe(op.log_name,op.log_name,"log");
  785.     log_file = fopen(op.log_name,"w");
  786.     if (log_file==NULL) print("Cannot open logfile");
  787.     }
  788.  
  789.     if (*input_name)
  790.     {
  791.     input_file = fopen(input_name,"r");
  792.     if (input_file==NULL)
  793.     {
  794.         catfe(input_name,input_name,"opt");
  795.         if (path!=(char*)NULL)
  796.         input_file = fopene(input_name,"r",path,(char*)NULL);
  797.         else
  798.         input_file = fopen(input_name,"r");
  799.     }
  800.     if (input_file!=NULL)
  801.     {
  802.         if (!setjmp(halt_jump)) do_commands(1);
  803.     }
  804.     else print("Cannot open input file %s",input_name);
  805.     }
  806.  
  807.     if (interactive || *dvi_name=='\0')
  808.     {
  809.     setjmp(halt_jump);
  810.     do_commands(0);
  811.     }
  812.     else
  813.     {
  814.     if (!setjmp(halt_jump)) format_pages(first,last,step);
  815.     else print("Program aborted");
  816.     }
  817.  
  818.     dvi_clean();
  819.     if (log_file) fclose(log_file);
  820.     if (input_file) fclose(input_file);
  821.     close_missing();
  822.  
  823.     gr_destall();
  824.     destall();
  825.  
  826.     mem_test();
  827.  
  828.     return 0;
  829. }
  830.  
  831.