home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / lpqtool / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-20  |  4.3 KB  |  180 lines

  1. /************************************************************************/
  2. /*    Copyright 1988 by Chuck Musciano and Harris Corporation        */
  3. /*                                    */
  4. /*    Permission to use, copy, modify, and distribute this software    */
  5. /*    and its documentation for any purpose and without fee is    */
  6. /*    hereby granted, provided that the above copyright notice    */
  7. /*    appear in all copies and that both that copyright notice and    */
  8. /*    this permission notice appear in supporting documentation, and    */
  9. /*    that the name of Chuck Musciano and Harris Corporation not be    */
  10. /*    used in advertising or publicity pertaining to distribution    */
  11. /*    of the software without specific, written prior permission.    */
  12. /*    Chuck Musciano and Harris Corporation make no representations    */
  13. /*    about the suitability of this software for any purpose.  It is    */
  14. /*    provided "as is" without express or implied warranty.        */
  15. /************************************************************************/
  16.  
  17.  
  18. /************************************************************************/
  19. /*    Some miscellaneous support routines                */
  20. /************************************************************************/
  21.  
  22. #include    <stdio.h>
  23. #include    <pwd.h>
  24.  
  25. #include    <suntool/sunview.h>
  26. #include    <suntool/panel.h>
  27.  
  28. static    short    cross_bits[] = {0x4000, 0xe000, 0x4000};
  29. mpr_static(better_button_cross, 3, 3, 1, cross_bits);
  30.  
  31. extern    char    *strcpy();
  32.  
  33. text_width(text, font)
  34.  
  35. char    *text;
  36. struct    pixfont    *font;
  37.  
  38. {    int    width;
  39.  
  40.     for (width = 0; *text; text++)
  41.        width += font->pf_char[*text].pc_adv.x;
  42.     return(width);
  43. }
  44.  
  45. struct    pixrect    *better_button_image(panel, text, chars, pixels, font)
  46.  
  47. Panel    panel;
  48. char    *text;
  49. int    chars;
  50. int    pixels;
  51. struct    pixfont    *font;
  52.  
  53. {    struct    pixrect    *pr;
  54.     struct    pr_prpos    pos;
  55.     int    width, true_width, height;
  56.  
  57.     if (font == NULL)
  58.        font = (struct pixfont *) window_get(panel, WIN_FONT);
  59.     width = chars * font->pf_char['0'].pc_adv.x + pixels;
  60.     true_width = text_width(text, font);
  61.     if (width < true_width)
  62.        width = true_width;
  63.     pr = mem_create(width + 8, height = font->pf_defaultsize.y + 6, 1);
  64.     pr_rop(pr, 3, 0, width + 2, 2, PIX_SRC | PIX_COLOR(1), NULL, 0, 0);
  65.     pr_rop(pr, 3, height - 2, width + 2, 2, PIX_SRC | PIX_COLOR(1), NULL, 0, 0);
  66.     pr_rop(pr, 0, 3, 2, height - 6, PIX_SRC | PIX_COLOR(1), NULL, 0, 0);
  67.     pr_rop(pr, width + 6, 3, 2, height - 6, PIX_SRC | PIX_COLOR(1), NULL, 0, 0);
  68.     pr_rop(pr, 1, 1, 3, 3, PIX_SRC | PIX_DST, &better_button_cross, 0, 0);
  69.     pr_rop(pr, width + 4, 1, 3, 3, PIX_SRC | PIX_DST, &better_button_cross, 0, 0);
  70.     pr_rop(pr, width + 4, height - 4, 3, 3, PIX_SRC | PIX_DST, &better_button_cross, 0, 0);
  71.     pr_rop(pr, 1, height - 4, 3, 3, PIX_SRC | PIX_DST, &better_button_cross, 0, 0);
  72.     pos.pr = pr;
  73.     pos.pos.x = 4 + (width - true_width) / 2;
  74.     pos.pos.y = 4 - font->pf_char['T'].pc_home.y;
  75.     pf_ttext(pos, PIX_SRC | PIX_COLOR(1), font, text);
  76.     return(pr);
  77. }
  78.  
  79. char    *user_name()
  80.  
  81. {    struct passwd *pp;
  82.  
  83.     pp = getpwuid(getuid());
  84.     return(pp? pp->pw_name : NULL);
  85. }
  86.  
  87. abend(s1, s2, s3, s4, s5, s6, s7, s8)
  88.  
  89. char    *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8;
  90.  
  91. {
  92.     fprintf(stderr, s1, s2, s3, s4, s5, s6, s7, s8);
  93.     exit(1);
  94. }
  95.  
  96. char    *strindex(source, target)
  97.  
  98. char    *source;
  99. char    *target;
  100.  
  101. {    register    int    len;
  102.  
  103.     len = strlen(target);
  104.     for (; *source; source++)
  105.        if (strncmp(source, target, len) == 0)
  106.           return(source);
  107.     return(0);
  108. }
  109.  
  110. verify(source, valid)
  111.  
  112. char    *source;
  113. char    *valid;
  114.  
  115. {    register    char    *s;
  116.  
  117.     for ( ; *source; source++) {
  118.        for (s = valid; *s && *s != *source; s++)
  119.           ;
  120.        if (*s == '\0')
  121.           return(0);
  122.        }
  123.     return(1);
  124. }
  125.  
  126. char    **saveargs(argc, argv)
  127.  
  128. int    argc;
  129. char    **argv;
  130.  
  131. {    int    i;
  132.     char    **copy;
  133.  
  134.     copy = (char **) malloc((argc + 1) * sizeof(char *));
  135.     for (i = 0; i < argc; i++)
  136.        strcpy(copy[i] = (char *) malloc(strlen(argv[i]) + 1), argv[i]);
  137.     copy[i] = (char *) 0;
  138.     return(copy);
  139. }
  140.  
  141. static    delarg(argc, argv)
  142.  
  143. int    *argc;
  144. char    **argv;
  145.  
  146. {    char    *p;
  147.  
  148.     while (*argv = *(argv+1))
  149.        argv++;
  150.     (*argc)--;
  151. }
  152.  
  153. char    getopt(argc, argv, opts, parm)
  154.  
  155. int    *argc;
  156. char    **argv;
  157. char    *opts;
  158. char    **parm;
  159.  
  160. {    char    c, *p, *index();
  161.     int    killed;
  162.  
  163.     *parm = NULL;
  164.     while (*argv && ((**argv != '-') || (*(*argv+1) == '\0')))
  165.        argv++;
  166.     if (*argv == NULL)
  167.        return(EOF);
  168.     c = *(*argv+1);
  169.     *++(*argv) = '-';
  170.     if (killed = (*(*argv+1) == '\0'))
  171.        delarg(argc, argv);
  172.     if ((p = index(opts, c)) == NULL)
  173.        c = '\0';
  174.     else if (*(p+1) == ':') {
  175.        *parm = killed ? *argv : *argv+1;
  176.        delarg(argc, argv);
  177.        }
  178.     return(c);
  179. }
  180.