home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo1.zoo / demo / ify / setname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-02  |  3.3 KB  |  155 lines

  1. /* setname.c - specify the name to appear when window is iconified
  2.    Jim Blandy - Monday, July 17, 1989 - Bell Communications Research */
  3.  
  4. #include "term.h"
  5.  
  6. extern char *malloc( /* unsigned size */ ),
  7.             *realloc( /* char *ptr, unsigned size */ );
  8.  
  9. char *progname;            /* name of program */
  10.  
  11. #define MAXHOSTLEN (80)        /* maximum length of host name  */
  12. char *buf;            /* where we accumulate the name */
  13. int bufsize;            /* how many bytes it has allocated to it */
  14.  
  15. #define ADD(size) assure(bufsize+(size))
  16.  
  17. char usage[] = "\
  18. Usage: %s <window name>\n\
  19. changes the name of the window to <window name>.\n\
  20. <window name> may contain:\n\
  21.   %%m - current host name\n\
  22.   %%w - width of the window, in columns of text\n\
  23.   %%h - height of the window, in rows of text\n\
  24.   %%p - vague verbal description of the window's position\n\
  25.   %%%% - a single %%\n";
  26.  
  27. main(argc, argv)
  28.      int argc;
  29.      char **argv;
  30. {
  31.     int bufend;
  32.     int cols, rows;
  33.     char hostname[MAXHOSTLEN];
  34.     int disp_w, disp_h;
  35.     int wind_x, wind_y, wind_w, wind_h;
  36.     int eps_x, eps_y;
  37.  
  38.     progname = argv[0];
  39.  
  40.     if (argc == 1) {
  41.     fprintf(stderr, usage, progname);
  42.     exit(1);
  43.     }
  44.     
  45.     m_setup(M_FLUSH);
  46.     m_ttyset();
  47.     get_param(NULL, &disp_w, &disp_h, NULL);
  48.     gethostname(hostname, MAXHOSTLEN);
  49.     eps_x = disp_w>>3;
  50.     eps_y = disp_h>>3;
  51.     get_colrow(&cols, &rows);
  52.     get_size(&wind_x, &wind_y, &wind_w, &wind_h);
  53.     m_ttyreset();
  54.     assure(100);
  55.  
  56.     bufend = 0;
  57.     for (argc--, argv++; argc>0; argc--, argv++) {
  58.     char *p;
  59.  
  60.     assure(bufend+strlen(*argv));
  61.  
  62.     for (p = *argv; *p; p++)
  63.         if (*p == '%')
  64.           switch(p[1]) {
  65.         case '%':    /* an ordinary % */
  66.           buf[bufend++] = *++p;
  67.           break;
  68.         case 'm':    /* machine name */
  69.           {
  70.               int len;
  71.  
  72.               ADD(len = strlen(hostname));
  73.               strcpy(buf+bufend, hostname);
  74.               bufend+=len;
  75.               p++;
  76.           }
  77.           break;
  78.         case 'w':    /* columns in window */
  79.           ADD(3);
  80.           sprintf(buf+bufend, "%d", cols);
  81.           bufend+=strlen(buf+bufend);
  82.           p++;
  83.           break;
  84.         case 'h':    /* rows in window */
  85.           ADD(3);
  86.           sprintf(buf+bufend, "%d", rows);
  87.           bufend+=strlen(buf+bufend);
  88.           p++;
  89.           break;
  90.         case 'p':    /* vague description of window's */
  91.                 /* position*/
  92.           {
  93.               int h = wind_x - (disp_w - (wind_x+wind_w));
  94.               int v = wind_y - (disp_h - (wind_y+wind_h));
  95.  
  96.               ADD(15);
  97.  
  98.               if (v<-eps_y)
  99.             strcpy(buf+bufend, "upper");
  100.               else if (v>eps_y)
  101.             strcpy(buf+bufend, "lower");
  102.               else
  103.             strcpy(buf+bufend, "middle");
  104.  
  105.               bufend += strlen(buf+bufend);
  106.  
  107.               if (h<-eps_x)
  108.             strcpy(buf+bufend, " left");
  109.               else if (h>eps_x)
  110.             strcpy(buf+bufend, " right");
  111.               else if (v<-eps_x || v>eps_x)
  112.             strcpy(buf+bufend, " middle");
  113.  
  114.               bufend += strlen(buf+bufend);
  115.               p++;
  116.           }
  117.           break;
  118.         default:
  119.           buf[bufend++] = *p;
  120.           break;
  121.           }
  122.         else
  123.           buf[bufend++] = *p;
  124.  
  125.     if (argc>1)
  126.       buf[bufend++] = ' ';
  127.     }
  128.  
  129.     buf[bufend] = '\0';
  130.     m_setevent(NOTIFY, buf);
  131.     die(NULL);
  132. }
  133.  
  134.  
  135. die(mesg, err)
  136.      char *mesg;
  137.      int err;
  138. {
  139.     if (mesg && mesg[0] != '\0')
  140.       fprintf(stderr, "%s: %s\n", progname, mesg);
  141.     exit(err);
  142. }
  143.  
  144.  
  145. assure(size)
  146.      int size;
  147. {
  148.     if (size < bufsize)
  149.       return;
  150.     size = (size + 127L) & ~127L;
  151.     if ((buf == NULL && (buf = malloc(size)) == NULL)
  152.     || (buf = realloc(buf, size)) == NULL)
  153.       die("out of memory");
  154. }
  155.