home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lynx2.8.1dev.10.tar.gz / lynx2.8.1dev.10.tar / lynx2-8 / src / UCAuto.c < prev    next >
C/C++ Source or Header  |  1998-04-23  |  8KB  |  355 lines

  1. /*
  2. **  This file contains code for changing the Linux console mode.
  3. **  Currently some names for font files are hardwired in here.
  4. **  You have to change this code if it needs accomodation for your
  5. **  system (or get the required files...).
  6. **
  7. **  Depending on the Display Character Set switched to, and the previous
  8. **  one as far as it is known, system("setfont ...") and/or output of
  9. **  escape sequences to switch console mode are done.  Curses will be
  10. **  temporarily suspended while that happens.
  11. **
  12. **  NOTE that the setfont calls will also affect all other virtual consoles.
  13. **
  14. **  Any ideas how to do this for other systems?
  15. */
  16.  
  17. #include <HTUtils.h>
  18. #include <tcp.h>
  19.  
  20. #include <UCMap.h>
  21. #include <UCDefs.h>
  22. #include <UCAuto.h>
  23. #include <LYGlobalDefs.h>
  24. #include <LYClean.h>
  25. #include <LYUtils.h>
  26.  
  27. #ifdef EXP_CHARTRANS_AUTOSWITCH
  28.  
  29. #ifdef VMS
  30. #define DISPLAY "DECW$DISPLAY"
  31. #else
  32. #define DISPLAY "DISPLAY"
  33. #endif /* VMS */
  34.  
  35. #ifdef LINUX
  36. typedef enum {
  37.     Is_Unset, Is_Set, Dunno, Dont_Care
  38. } TGen_state_t;
  39. typedef enum {
  40.     G0, G1
  41. } TGNstate_t;
  42. typedef enum {
  43.     GN_Blat1, GN_0decgraf, GN_Ucp437, GN_Kuser, GN_dunno, GN_dontCare
  44. } TTransT_t;
  45.  
  46. static char T_font_fn[100] = "\0";
  47. static char T_umap_fn[100] = "\0";
  48. static char T_setfont_cmd[200] = "\0";
  49. #define SETFONT "setfont"
  50. #define NOOUTPUT "2>/dev/null >/dev/null"
  51.  
  52. PRIVATE void call_setfont ARGS3(
  53.     char *,     font,
  54.     char *,     fnsuffix,
  55.     char *,     umap)
  56. {
  57.     if (font && *font && umap && *umap &&
  58.     !strcmp(font, T_font_fn) && !strcmp(umap, T_umap_fn)) {
  59.     /*
  60.      *  No need to repeat.
  61.      */
  62.     return;
  63.     }
  64.     if (font)
  65.     strcpy(T_font_fn, font);
  66.     if (umap)
  67.     strcpy(T_umap_fn, umap);
  68.  
  69.     if (!*fnsuffix)
  70.     fnsuffix = "";
  71.  
  72.     if (umap &&*umap && font && *font) {
  73.     sprintf(T_setfont_cmd, "%s %s%s -u %s %s",
  74.         SETFONT, font, fnsuffix,    umap,    NOOUTPUT);
  75.     } else if (font && *font) {
  76.     sprintf(T_setfont_cmd, "%s %s%s %s",
  77.         SETFONT, font, fnsuffix,        NOOUTPUT);
  78.     } else if (umap && *umap) {
  79.     sprintf(T_setfont_cmd, "%s -u %s %s",
  80.         SETFONT,            umap,    NOOUTPUT);
  81.     } else {
  82.     *T_setfont_cmd = '\0';
  83.     }
  84.  
  85.     if (*T_setfont_cmd) {
  86.     CTRACE(tfp, "Executing setfont: '%s'\n", T_setfont_cmd);
  87.     system(T_setfont_cmd);
  88.     }
  89. }
  90.  
  91. PRIVATE void write_esc ARGS1(
  92.     CONST char *,    p)
  93. {
  94.     int fd = open("/dev/tty", O_WRONLY);
  95.  
  96.     if (fd >= 0) {
  97.     write(fd, p, strlen(p));
  98.     close(fd);
  99.     }
  100. }
  101.  
  102. PRIVATE int nonempty_file ARGS1(
  103.     CONST char *,    p)
  104. {
  105.     struct stat sb;
  106.  
  107.     return (stat(p, &sb) == 0 &&
  108.         (sb.st_mode & S_IFMT) == S_IFREG &&
  109.         (sb.st_size != 0));
  110. }
  111.  
  112. /*
  113.  *  This is the thing that actually gets called from display_page().
  114.  */
  115. PUBLIC void UCChangeTerminalCodepage ARGS2(
  116.     int,        newcs,
  117.     LYUCcharset *,    p)
  118. {
  119.     static int lastcs = -1;
  120.     static CONST char * lastname = NULL;
  121.     static TTransT_t lastTransT = GN_dunno;
  122.     static TGen_state_t lastUtf = Dunno;
  123.     static TGen_state_t lastHasUmap = Dunno;
  124.  
  125.     static char *old_font;
  126.     static char *old_umap;
  127.  
  128.     CONST char * name;
  129.     TTransT_t TransT = GN_dunno;
  130.     TGen_state_t Utf = Dunno;
  131.     TGen_state_t HasUmap = Dunno;
  132.  
  133.     char tmpbuf1[100], tmpbuf2[20];
  134.     char *cp;
  135.  
  136.     /*
  137.      *    Restore the original character set.
  138.      */
  139.     if (newcs < 0 || p == 0) {
  140.     if (old_font && *old_font &&
  141.         old_umap && *old_umap) {
  142.         int have_font = nonempty_file(old_font);
  143.         int have_umap = nonempty_file(old_umap);
  144.  
  145.         if (have_font) {
  146.         if (have_umap) {
  147.             sprintf(tmpbuf1, "%s %s -u %s %s",
  148.                 SETFONT, old_font, old_umap, NOOUTPUT);
  149.         } else {
  150.             sprintf(tmpbuf1, "%s %s %s",
  151.                 SETFONT, old_font, NOOUTPUT);
  152.         }
  153.         system(tmpbuf1);
  154.         }
  155.  
  156.         remove(old_font);
  157.         free(old_font);
  158.         old_font = 0;
  159.  
  160.         remove(old_umap);
  161.         free(old_umap);
  162.         old_umap = 0;
  163.     }
  164.     return;
  165.     } else if (lastcs < 0 && old_umap == 0 && old_font == 0) {
  166.     old_umap = tempnam((char *)0, "umap");
  167.     old_font = tempnam((char *)0, "font");
  168.     sprintf(tmpbuf1, "%s -o %s -ou %s %s",
  169.         SETFONT, old_font, old_umap, NOOUTPUT);
  170.     system(tmpbuf1);
  171.     }
  172.  
  173.     name = p->MIMEname;
  174.  
  175.     /*
  176.      *    Font sizes are currently hardwired here.
  177.      */
  178. #define SUFF1 ".f16"
  179. #define SUFF2 "-16.psf"
  180. #define SUFF3 "-8x16"
  181. #define SUFF4 "8x16"
  182.  
  183.     /*
  184.      *    Use this for output of escape sequences.
  185.      */
  186.     if ((display != NULL) ||
  187.     ((cp = getenv(DISPLAY)) != NULL && *cp != '\0')) {
  188.     /*
  189.      *  We won't do anything in an xterm.  Better that way...
  190.      */
  191.     return;
  192.     }
  193.  
  194.     if (!strcmp(name, "iso-8859-10")) {
  195.     call_setfont("iso10", SUFF1, "iso10.uni");
  196.     TransT = GN_Kuser;
  197.     HasUmap = Is_Set;
  198.     Utf = Is_Unset;
  199.     } else if (!strncmp(name, "iso-8859-1", 10)) {
  200.     if ((lastHasUmap == Is_Set) && !strcmp(lastname, "cp850")) {
  201.         /*
  202.          *    cp850 already contains all latin1 characters.
  203.          */
  204.         if (lastTransT != GN_Blat1) {
  205.         TransT = GN_Blat1;
  206.         }
  207.     } else {
  208.         /*
  209.          *    "setfont lat1u-16.psf -u lat1u.uni"
  210.          */
  211.         call_setfont("lat1u", SUFF2, "lat1u.uni");
  212.         HasUmap = Is_Set;
  213.         if (lastTransT != GN_Blat1) {
  214.         TransT = GN_Blat1;
  215.         }
  216.     }
  217.     Utf = Is_Unset;
  218.     } else if (!strcmp(name, "iso-8859-2")) {
  219. #ifdef NOTDEFINED
  220.     /*
  221.      *  "setfont lat2-16.psf -u lat2.uni"
  222.      */
  223.     call_setfont("lat2", SUFF2, "lat2.uni");  */
  224. #endif /* NOTDEFINED */
  225.     /*
  226.      *  "setfont iso02.f16 -u iso02.uni"
  227.      */
  228.     call_setfont("iso02", SUFF1, "iso02.uni");
  229.     TransT = GN_Kuser;
  230.     HasUmap = Is_Set;
  231.     Utf = Is_Unset;
  232.     } else if (!strncmp(name, "iso-8859-", 9)) {
  233.     sprintf(tmpbuf1, "iso0%s", &name[9]);
  234.     sprintf(tmpbuf2, "iso0%s%s", &name[9],".uni");
  235.     /*
  236.      *  "setfont iso0N.f16 -u iso0N.uni"
  237.      */
  238.     call_setfont(tmpbuf1, SUFF1, tmpbuf2);
  239.     TransT = GN_Kuser;
  240.     HasUmap = Is_Set;
  241.     Utf = Is_Unset;
  242.     } else if (!strcmp(name, "koi8-r")) {
  243.     /*
  244.      *  "setfont koi8-8x16"
  245.      */
  246.     call_setfont("koi8", SUFF3, NULL);
  247.     TransT = GN_Kuser;
  248.     HasUmap = Is_Unset;
  249.     Utf = Is_Unset;
  250.     } else if (!strcmp(name, "cp437")) {
  251.     /*
  252.      *  "setfont default8x16 -u cp437.uni"
  253.      */
  254.     call_setfont("default", SUFF4, "cp437.uni");
  255.     if (TransT == GN_Kuser || TransT == GN_Ucp437)
  256.         TransT = GN_dontCare;
  257.     else
  258.         TransT = GN_Ucp437;
  259.     HasUmap = Is_Set;
  260.     Utf = Is_Unset;
  261.     } else if (!strcmp(name, "cp850")) {
  262.     /*
  263.      *  "setfont cp850-8x16 -u cp850.uni"
  264.      */
  265.     call_setfont("cp850", SUFF3, "cp850.uni");
  266.     TransT = GN_Kuser;
  267.     HasUmap = Is_Set;
  268.     Utf = Is_Unset;
  269.     } else if (!strcmp(name, "x-transparent")) {
  270.     Utf = Dont_Care;
  271.     } else if (!strcmp(name, "us-ascii")) {
  272.     Utf = Dont_Care;
  273.     } else if (!strncmp(name, "mnem", 4)) {
  274.     Utf = Dont_Care;
  275.     }
  276.  
  277.     if (TransT != lastTransT) {
  278.     if (TransT == GN_Blat1) {
  279.         /*
  280.          *    Switch Linux console to lat1 table.
  281.          */
  282.         write_esc("\033(B");
  283.     } else if (TransT == GN_0decgraf) {
  284.         write_esc("\033(0");
  285.     } else if (TransT == GN_Ucp437) {
  286.          /*
  287.           *  Switch Linux console to 437 table?
  288.           */
  289.         write_esc("\033(U");
  290.     } else if (TransT == GN_Kuser) {
  291.          /*
  292.           *  Switch Linux console to user table.
  293.           */
  294.         write_esc("\033(K");
  295.     }
  296.     if (TransT != GN_dunno && TransT != GN_dontCare) {
  297.         lastTransT = TransT;
  298.     } else {
  299.         TransT = lastTransT;
  300.     }
  301.     }
  302.  
  303.     if (HasUmap != Dont_Care && HasUmap != Dunno)
  304.     lastHasUmap = HasUmap;
  305.  
  306.     if (p->enc == UCT_ENC_UTF8) {
  307.     if (lastUtf != Is_Set) {
  308.         Utf = Is_Set;
  309.         /*
  310.          *    Turn Linux console UTF8 mode ON.
  311.          */
  312.         write_esc("\033%G");
  313.         lastUtf = Utf;
  314.     }
  315.     return;
  316.     } else if (lastUtf == Is_Set && Utf != Dont_Care) {
  317.     Utf = Is_Unset;
  318.     /*
  319.      *  Turn Linux console UTF8 mode OFF.
  320.      */
  321.     write_esc("\033%@");
  322.     lastUtf = Utf;
  323.     }
  324.  
  325.     if (Utf != Dont_Care && Utf != Dunno)
  326.     lastUtf = Utf;
  327.  
  328.     lastcs = newcs;
  329.     lastname = name;
  330. }
  331.  
  332. #else /* Not LINUX: */
  333. /*
  334.  *  This is the thing that actually gets called from display_page().
  335.  */
  336. PUBLIC void UCChangeTerminalCodepage ARGS2(
  337.     int,        newcs,
  338.     LYUCcharset *,    p)
  339. {
  340.     CTRACE(tfp, "UCChangeTerminalCodepage: Called, but not implemented!");
  341. }
  342. #endif /* LINUX */
  343.  
  344. #else /* EXP_CHARTRANS_AUTOSWITCH not defined: */
  345. /*
  346.  *  This is the thing that actually gets called from display_page().
  347.  */
  348. PUBLIC void UCChangeTerminalCodepage ARGS2(
  349.     int,        newcs GCC_UNUSED,
  350.     LYUCcharset *,    p GCC_UNUSED)
  351. {
  352.     CTRACE(tfp, "UCChangeTerminalCodepage: Called, but not implemented!");
  353. }
  354. #endif /* EXP_CHARTRANS_AUTOSWITCH */
  355.