home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / toswinsc.zoo / environ.c < prev    next >
C/C++ Source or Header  |  1992-10-27  |  6KB  |  255 lines

  1. /*
  2.  * Copyright 1992 Eric R. Smith. All rights reserved.
  3.  * Redistribution is permitted only if the distribution
  4.  * is not for profit, and only if all documentation
  5.  * (including, in particular, the file "copying")
  6.  * is included in the distribution in unmodified form.
  7.  * THIS PROGRAM COMES WITH ABSOLUTELY NO WARRANTY, NOT
  8.  * EVEN THE IMPLIED WARRANTIES OF MERCHANTIBILITY OR
  9.  * FITNESS FOR A PARTICULAR PURPOSE. USE AT YOUR OWN
  10.  * RISK.
  11.  */
  12. /* Routines for setting environment variables */
  13. #include <osbind.h>
  14. #include <basepage.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <mintbind.h>
  18. #include "xgem.h"
  19. #include "toswin.h"
  20. #include "twdefs.h"
  21. #include "twproto.h"
  22.  
  23. /* which variables do we put in the environment */
  24. int env_options = E_POSIX|E_TERMCAP|E_TERM|E_ARGV;
  25.  
  26. static char LINESprefix[] = "LINES=";
  27. static char COLSprefix[] = "COLUMNS=";
  28. static char TERMprefix[] = "TERM=";
  29. static char TERMCAPprefix[] = "TERMCAP=";
  30. static char ARGVprefix[] = "ARGV=";
  31.  
  32. /* templates for building a TERMCAP */
  33. static char *TC1 = "TERMCAP=tw|tw52|toswin window mgr\
  34. :al=\\EL:am:bl=^G:bs:cd=\\EJ:ce=\\EK:cl=\\EE:cm=\\EY%+ %+ :co#";
  35.  
  36. static char *TC2 =
  37.     ":dc=\\Ea:dl=\\EM:do=\\EB:ei=\\Ei:eo:ho=\\EH:ic=:im=\\Eh:it#8:le=^H:li#";
  38.  
  39. static char *TC3 = ":md=\\EyA:me=\\Ez_:mh=\\EzB:mi:mr=\\Ep:ms:nd=\\EC\
  40. :rc=\\Ek:rs=\\Ez_\\Eb@\\EcA:sc=\\Ej:se=\\Eq:sf=^J:sr=\\EI:so=\\Ep\
  41. :ta=^I:ti=\\Eq\\Ev\\Ee:ue=\\EzH:us=\\EyH:up=\\EA:ve=\\Ee:vi=\\Ef:";
  42.  
  43. struct ereserved {
  44.     char *prefix;
  45.     int len;
  46.     int bits;
  47. } evar[] = {
  48.     ARGVprefix, 6, E_ARGV,
  49.     LINESprefix, 10, E_POSIX,
  50.     COLSprefix, 12, E_POSIX,
  51.     TERMprefix, 10, E_TERM,
  52.     TERMCAPprefix, 512, E_TERMCAP,
  53.     0L, 0, 0
  54. };
  55.  
  56. /* returns 1 if "var" starts with the characters in "prefix", 0 otherwise */
  57.  
  58. static int
  59. strprefix(var, prefix)
  60.     char *var, *prefix;
  61. {
  62.     while (*var && *prefix) {
  63.         if (*var++ != *prefix++) return 0;
  64.     }
  65.     if (*prefix) return 0;
  66.     return 1;
  67. }
  68.  
  69. /* copies "src" to "dest"; returns a pointer to the trailing 0 in dest
  70.  * after the copy is finished
  71.  */
  72.  
  73. static char *
  74. copyprefix(dest, src)
  75.     char *dest, *src;
  76. {
  77.     while (*src)
  78.         *dest++ = *src++;
  79.     *dest = 0;
  80.     return dest;
  81. }
  82.  
  83. /* copy an ASCII representation of the decimal number 'd' (0-999 inclusive)
  84.  * into the string pointed to by 'to'; return a pointer to the new trailing
  85.  * '\0'
  86.  */
  87.  
  88. static char *
  89. putdec(to, d)
  90.     char *to;
  91.     int d;
  92. {
  93.     char *val = valdec2(d);
  94.  
  95.     return copyprefix(to, val);
  96. }
  97.  
  98. char *
  99. envstr(progname, progargs, progdir, cols, rows)
  100.     char *progname, *progargs, *progdir;
  101.     int cols, rows;
  102. {
  103.     static char *oldenv = 0;
  104.     char *from, *to;
  105.     long nbytes;
  106.     struct ereserved *ev;
  107.     char *newenv;
  108.  
  109.     if (!oldenv)
  110.         oldenv = _base->p_env;
  111.  
  112.     nbytes = 4;            /* for nulls and fluff */
  113.     from = oldenv;
  114.     while (from[0] || from[1]) {
  115.         from++;
  116.         nbytes++;
  117.     }
  118.     nbytes += 2*strlen(progargs) + strlen(progname);  /* ARGV=... */
  119.     for (ev = evar; ev->prefix; ev++) {
  120.         nbytes += ev->len;
  121.     }
  122.     newenv = malloc(nbytes);
  123.     if (!newenv) return 0;
  124.  
  125. /* OK, now copy the environment over into "newenv" */
  126. /* any environment variables that we're going to set, don't copy */
  127.  
  128.     from = oldenv;
  129.     to = newenv;
  130.     while (*from) {
  131.         for (ev = evar; ev->prefix; ev++) {
  132.             if ((env_options & ev->bits) && 
  133.                 strprefix(from, ev->prefix)) {
  134.                 while(*from) from++;
  135.                 from++;
  136.                 goto endloop;
  137.             }
  138.         }
  139.         while (*from)
  140.             *to++ = *from++;
  141.         *to++ = *from++;
  142.     endloop: ;
  143.     }
  144.  
  145. /* now we copy in our variables */
  146.     if (env_options & E_POSIX) {
  147.         to = copyprefix(to, LINESprefix);
  148.         to = putdec(to, rows) + 1;
  149.         to = copyprefix(to, COLSprefix);
  150.         to = putdec(to, cols) + 1;
  151.     }
  152.  
  153.     if (env_options & E_TERMCAP) {
  154.         to = copyprefix(to, "TERM=tw52")+1;
  155.         to = copyprefix(to, TC1);
  156.         to = putdec(to, cols);
  157.         to = copyprefix(to, TC2);
  158.         to = putdec(to, rows);
  159.         to = copyprefix(to, TC3)+1;
  160.     }
  161.     else if (env_options & E_TERM) {
  162.         to = copyprefix(to, "TERM=st52")+1;
  163.     }
  164.  
  165.     if (env_options & E_ARGV) {
  166.         to = copyprefix(to, ARGVprefix) + 1;
  167.         to = copyprefix(to, progname) + 1;
  168.         if (*progargs) {
  169.             while(*progargs) {
  170.                 from = nextword(&progargs);
  171.                 to = copyprefix(to, from) + 1;
  172.             }
  173.         }
  174.     }
  175.  
  176.     *to++ = 0;    /* extra trailing 0 to close environment */
  177.     return newenv;
  178. }
  179.  
  180. void
  181. setenvoptions()
  182. {
  183.     OBJECT *envdial;
  184.     int x, y, w, h, ret;
  185.  
  186.     rsrc_gaddr(0, ENVDIAL, &envdial);
  187.     form_center(envdial, &x, &y, &w, &h);
  188.  
  189.     envdial[POSIXBOX].ob_state = (env_options & E_POSIX) ?SELECTED : NORMAL;
  190.     envdial[ARGVBOX].ob_state = (env_options & E_ARGV) ? SELECTED : NORMAL;
  191.     envdial[TCAPBOX].ob_state = (env_options & E_TERMCAP) ? SELECTED : NORMAL;
  192.     if ( (env_options & E_TERM) && !(env_options & E_TERMCAP) )
  193.         envdial[TERMBOX].ob_state = SELECTED;
  194.     else
  195.         envdial[TERMBOX].ob_state = NORMAL;
  196.  
  197.     wind_update(1);
  198.     form_dial(FMD_START, 0, 0, 32, 32, x, y, w, h);
  199.     if (win_flourishes)
  200.         form_dial(FMD_GROW, 0, 0, 32, 32, x, y, w, h);
  201.  
  202.     objc_draw(envdial, 0, 2, x, y, w, h);
  203.  
  204.     ret = form_do(envdial, 0);
  205.     if (win_flourishes)
  206.         form_dial(FMD_SHRINK, 0, 0, 32, 32, x, y, w, h);
  207.  
  208.     form_dial(FMD_FINISH, 0, 0, 32, 32, x, y, w, h);
  209.     objc_change(envdial, ret, 0, x, y, w, h, NORMAL, 0);
  210.     wind_update(0);
  211.     if (ret == ENVCAN) return;
  212.     env_options = 0;
  213.     if (envdial[TCAPBOX].ob_state == SELECTED)
  214.         env_options |= E_TERMCAP|E_TERM;
  215.     else if (envdial[TERMBOX].ob_state == SELECTED)
  216.         env_options |= E_TERM;
  217.     if (envdial[ARGVBOX].ob_state == SELECTED)
  218.         env_options |= E_ARGV;
  219.     if (envdial[POSIXBOX].ob_state == SELECTED)
  220.         env_options |= E_POSIX;
  221. }
  222.  
  223. /*
  224.  * output a TERMCAP string to text window t, just as though
  225.  * the user typed it
  226.  */
  227.  
  228. static void
  229. sendstr(t, s)
  230.     TEXTWIN *t;
  231.     char *s;
  232. {
  233.     long c;
  234.  
  235.     if (t->fd > 0) {
  236.         while (*s) {
  237.             c = *((unsigned char *)s);
  238.             s++;
  239.             (void)Fputchar(t->fd, c, 0);
  240.         }
  241.     }
  242. }
  243.  
  244. void
  245. output_termcap(t)
  246.     TEXTWIN *t;
  247. {
  248.     sendstr(t, TC1);
  249.     sendstr(t, valdec2(t->maxx));
  250.     sendstr(t, TC2);
  251.     sendstr(t, valdec2(NROWS(t)));
  252.     sendstr(t, TC3);
  253.     sendstr(t, "\r");
  254. }
  255.