home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / tooltool2.1c / part04 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-06  |  10.7 KB  |  466 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. /*    The sale of any product based wholely or in part upon the     */
  17. /*    technology provided by tooltool is strictly forbidden without    */
  18. /*    specific, prior written permission from Harris Corporation.    */
  19. /*    Tooltool technology includes, but is not limited to, the source    */
  20. /*    code, executable binary files, specification language, and    */
  21. /*    sample specification files.                    */
  22. /************************************************************************/
  23.  
  24. #include    <stdio.h>
  25. #include    <ctype.h>
  26. #include    <sgtty.h>
  27. #include    <pwd.h>
  28.  
  29. #include    <sys/types.h>
  30. #include    <sys/stat.h>
  31. #include    <sys/dir.h>
  32. #include    <sys/file.h>
  33.  
  34. #include    <suntool/sunview.h>
  35. #include    <suntool/icon_load.h>
  36.  
  37. #include    "tooltool.h"
  38.  
  39. PUBLIC    char    *index(), *rindex(), *getenv();
  40.  
  41. typedef    struct    pc_rec    pc_data, *pc_ptr;
  42. typedef    struct    fc_rec    fc_data, *fc_ptr;
  43.  
  44. struct    pc_rec    {char    *path;
  45.          struct    pixrect    *image;
  46.          pc_ptr    next;
  47.         };
  48.  
  49. struct    fc_rec    {char    *path;
  50.          struct    pixfont    *font;
  51.          fc_ptr    next;
  52.         };
  53.  
  54. PRIVATE    pc_ptr    pix_cache = NULL;
  55. PRIVATE    fc_ptr    font_cache = NULL;
  56.  
  57. /************************************************************************/
  58. EXPORT    char    *safe_malloc(size)
  59.  
  60. int    size;
  61.  
  62. {    char    *p;
  63.  
  64.     if (p = (char *) malloc(size))
  65.        return(p);
  66.     else
  67.        abend("insufficient memory available");
  68. }
  69.  
  70. /************************************************************************/
  71. EXPORT    safe_free(p)
  72.  
  73. char    *p;
  74.  
  75. {
  76.     free(p);
  77. }
  78.  
  79. /************************************************************************/
  80. EXPORT    tokenize(line, argc, argv, maxv)
  81.  
  82. char    *line;
  83. int    *argc;
  84. char    *argv[];
  85. int    maxv;
  86.  
  87. {    char    *buf, match, *delimiters;
  88.  
  89.     *argc = 0;
  90.     buf = (char *) tt_emalloc(strlen(line) * 2 + 1);
  91.     if ((delimiters = tt_string_of(tt_delimiters->value)) == NULL)
  92.        delimiters = " \t\n\r\"'";
  93.     while (*line && (*argc < (maxv - 1))) {
  94.        while (*line && index(delimiters, *line))
  95.           if (*line == '"' || *line == '\'')
  96.              break;
  97.           else
  98.              line++;
  99.        if (*line == '\0')
  100.           break;
  101.        argv[(*argc)++] = buf;
  102.        if (index(delimiters, *line)) { /* scanning a quoted string */
  103.           match = *line++; /* remove the quote mark */
  104.           while (*line && (*line != match))
  105.              *buf++ = *line++;
  106.           if (*line)
  107.              line++; /* wipe out quote mark */
  108.           }
  109.        else {
  110.           while (*line && !index(delimiters, *line))
  111.              *buf++ = *line++;
  112.           }
  113.        *buf++ = '\0';
  114.        }
  115.     *buf = '\0';
  116.     argv[*argc] = (char *) 0;
  117. }
  118.  
  119. /************************************************************************/
  120. EXPORT    struct    pixrect    *tt_load_icon(path)
  121.  
  122. char    *path;
  123.  
  124. {    char    msg[IL_ERRORMSG_SIZE];
  125.     struct    pixrect    *p;
  126.     FILE    *f;
  127.     pc_ptr    pc;
  128.  
  129.     for (pc = pix_cache; pc; pc = pc->next)
  130.        if (strcmp(path, pc->path) == 0)
  131.           return(pc->image);
  132.     if ((p = icon_load_mpr(path, msg)) == NULL) { /* not in icon format */
  133.        if ((f = fopen(path, "r")) == NULL)
  134.           abend("could not open %s", path);
  135.        if ((p = pr_load(f, NULL)) == NULL)
  136.           abend("%s must be in either icon or raster file format", path);
  137.        fclose(f);
  138.        }
  139.     pc = (pc_ptr) safe_malloc(sizeof(pc_data));
  140.     pc->path = strsave(path);
  141.     pc->image = p;
  142.     pc->next = pix_cache;
  143.     pix_cache = pc;
  144.     return(p);
  145. }
  146.  
  147. /************************************************************************/
  148. EXPORT    struct    pixfont    *tt_open_font(path)
  149.  
  150. char    *path;
  151.  
  152. {    struct    pixfont    *p;
  153.     fc_ptr    pf;
  154.  
  155.     for (pf = font_cache; pf; pf = pf->next)
  156.        if (strcmp(path, pf->path) == 0)
  157.           return(pf->font);
  158.     if ((p = pf_open(path)) == NULL)
  159.        abend("cannot open font: %s", path);
  160.     pf = (fc_ptr) safe_malloc(sizeof(fc_data));
  161.     pf->path = strsave(path);
  162.     pf->font = p;
  163.     pf->next = font_cache;
  164.     font_cache = pf;
  165.     return(p);
  166. }
  167.  
  168. /************************************************************************/
  169. EXPORT    int    text_width(text, font)
  170.  
  171. unsigned    char    *text;
  172. struct    pixfont    *font;
  173.  
  174. {    int    width;
  175.  
  176.     for (width = 0; *text; text++)
  177.        width += font->pf_char[*text].pc_adv.x;
  178.     return(width);
  179. }
  180.  
  181. /************************************************************************/
  182. PRIVATE    char    *root_path(path)
  183.  
  184. char    *path;
  185.  
  186. {    char    *p;
  187.  
  188.     if (p = rindex(path, '/'))
  189.        if (p == path)
  190.           p[1] = '\0';
  191.        else
  192.           *p = '\0';
  193.     else
  194.        *path = '\0';
  195.     return(path);
  196. }
  197.  
  198. /************************************************************************/
  199. PRIVATE    char    *last_node(path)
  200.  
  201. char    *path;
  202.  
  203. {    char    *p;
  204.  
  205.     return((p = rindex(path, '/'))? p + 1 : path);
  206. }
  207.  
  208. /************************************************************************/
  209. EXPORT    char    *expand_filename(path)
  210.  
  211. char    *path;
  212.  
  213. {    static    char    s[1024];
  214.     char    pattern[1024], candidate[1024], *p,*q;
  215.     DIR    *dir;
  216.     struct    direct *dp;
  217.     struct    passwd    *pw;
  218.  
  219.     strcpy(s, path);
  220.     if (*path == '~')
  221.        if (path[1] == '/' || path[1] == '\0') {
  222.           strcpy(s, getenv("HOME"));
  223.           strcat(s, path + 1);
  224.           }
  225.        else {
  226.           if ((p = index(path, '/')) != NULL)
  227.              *p = '\0';
  228.           if ((pw = getpwnam(path + 1)) != NULL) {
  229.              strcpy(s, pw->pw_dir);
  230.              if (p != NULL) {
  231.                 strcat(s, "/");
  232.                 strcat(s, p + 1);
  233.                 }
  234.              }
  235.           else
  236.              return(NULL);
  237.           }
  238.     strcpy(pattern, last_node(s));
  239.     if (*pattern == '\0')
  240.        return(s);
  241.     root_path(s);
  242.     candidate[0] = '\0';
  243.     if (*s == '\0')
  244.        strcpy(s, ".");
  245.     if ((dir = opendir(s)) == NULL) {
  246.        strcpy(s, path);
  247.        return(s);
  248.        }
  249.     while ((dp = readdir(dir)) != NULL)
  250.        if (strncmp(dp->d_name, pattern, strlen(pattern)) == 0)
  251.           if (*candidate == '\0')
  252.              strcpy(candidate, dp->d_name);
  253.           else {
  254.              for (p = candidate, q = dp->d_name; *p == *q; p++, q++)
  255.                 ;
  256.              *p = '\0';
  257.              }
  258.     closedir(dir);
  259.     if (*candidate == '\0')
  260.        return(NULL);
  261.     else {
  262.        if (strcmp(s, ".") == 0)
  263.           *s = '\0';
  264.        else if (s[strlen(s) - 1] != '/')
  265.           strcat(s, "/");
  266.        strcat(s, candidate);
  267.        }
  268.     return(s);
  269. }
  270.  
  271. /************************************************************************/
  272. EXPORT    tt_construct_label(l)
  273.  
  274. l_ptr    l;
  275.  
  276. {    struct    pr_prpos    where;
  277.  
  278.     if (!l->is_icon && l->image == NULL) {
  279.        l->image = mem_create(text_width(l->label, l->font), l->font->pf_defaultsize.y, 1);
  280.        where.pr = l->image;
  281.        where.pos.x = 0;
  282.        where.pos.y = baseline_of(l->font);
  283.        pf_text(where, PIX_SRC, l->font, l->label);
  284.        }
  285. }
  286.  
  287. /************************************************************************/
  288. EXPORT    l_ptr    tt_make_label(is_icon, label, font, image)
  289.  
  290. int    is_icon;
  291. char    *label;
  292. Pixfont    *font;
  293. struct    pixrect    *image;
  294.  
  295. {    l_ptr    l;
  296.  
  297.     l = (l_ptr) safe_malloc(sizeof(l_data));
  298.     l->is_icon = is_icon;
  299.     l->label = label;
  300.     l->font = font;
  301.     l->image = image;
  302.     return(l);
  303. }
  304.  
  305. /************************************************************************/
  306. EXPORT    d_ptr    tt_make_base_window()
  307.  
  308. {    d_ptr    d;
  309.  
  310.     d = (d_ptr) safe_malloc(sizeof(d_data));
  311.     d->frame = NULL;
  312.     d->panel = NULL;
  313.     d->is_base_frame = FALSE;
  314.     d->is_open = TRUE;
  315.     d->is_popup = FALSE;
  316.     d->rows = 24;
  317.     d->columns = 80;
  318.     d->win_x = -1;
  319.     d->win_y = -1;
  320.     d->is_chars = TRUE;
  321.     d->g_align = NO_ALIGN;
  322.     d->proportional = FALSE;
  323.     d->justified = TRUE;
  324.     d->text_items_exist = FALSE;
  325.     d->gadget_pos = G_NOPOS;
  326.     d->gadgets = NULL;
  327.     d->label = NULL;
  328.     d->open_action = NULL;
  329.     d->close_action = NULL;
  330.     d->g_font = tt_default_font;
  331.     d->next = NULL;
  332.     return(d);
  333. }
  334.  
  335. /************************************************************************/
  336. EXPORT    abend(s1, s2, s3, s4, s5, s6, s7, s8, s9)
  337.  
  338. char    *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
  339.  
  340. {
  341.     fprintf(stderr, "%s: ", tt_program);
  342.     fprintf(stderr, s1, s2, s3, s4, s5, s6, s7, s8, s9);
  343.     fprintf(stderr, "\n");
  344.     exit(1);
  345. }
  346.  
  347. /************************************************************************/
  348. EXPORT    int    tt_is_number(s)
  349.  
  350. register    char    *s;
  351.  
  352. {    register    int    saw_digit = FALSE;
  353.  
  354.     if (*s == '-' || *s == '+')
  355.        s++;
  356.     for ( ; isdigit(*s); s++)
  357.        saw_digit = TRUE;
  358.     if (*s == '.')
  359.        for (s++; isdigit(*s); s++)
  360.           saw_digit = TRUE;
  361.     if (*s == 'e' || *s == 'E') {
  362.        if (*++s == '-' || *s == '+')
  363.           s++;
  364.        for ( ; isdigit(*s); s++)
  365.           saw_digit = TRUE;
  366.        }
  367.     return(saw_digit && *s == '\0');
  368. }
  369.  
  370. /************************************************************************/
  371. EXPORT    int    tt_dict_compare(l, r)
  372.  
  373. char    *l;
  374. char    *r;
  375.  
  376. {    register    char    *p, *q;
  377.  
  378.     for (p = l; isdigit(*p); p++)
  379.        ;
  380.     for (q = r; isdigit(*q); q++)
  381.        ;
  382.     if (*p || *q)
  383.        return(strcmp(l, r));
  384.     else
  385.        return(atoi(l) - atoi(r));
  386. }
  387.  
  388. /************************************************************************/
  389. EXPORT    char    *tt_expand_ranges(s)
  390.  
  391. unsigned    char    *s;
  392.  
  393. {    unsigned    char    *p, buf[1024];
  394.     int    c;
  395.  
  396.     for (p = buf; *s; s) {
  397.        *p++ = *s++;
  398.        if (*s == '-' && *(s + 1) != '\0') {
  399.           for (c = *(p - 1), s++; c <= *s; )
  400.              *p++ = c++;
  401.           s++;
  402.           }
  403.        }
  404.     *p = '\0';
  405.     return(strsave(buf));
  406. }
  407.  
  408. /************************************************************************/
  409. EXPORT    wait_for_window_size(width, height, timeout)
  410.  
  411. int    width;
  412. int    height;
  413. int    timeout; /* in milliseconds */
  414.  
  415. {    struct    winsize    win;
  416.  
  417.     if (width > 0 && height > 0)
  418.        do {
  419.           if (ioctl(fileno(stderr), TIOCGWINSZ, &win) < 0)
  420.              abend("could not obtain the window size");
  421.           usleep(50000);
  422.           } while ((win.ws_row != height || win.ws_col != width) && (timeout -= 50) > 0);
  423. }
  424.  
  425. /************************************************************************/
  426. EXPORT    int    tt_perm_of(st)
  427.  
  428. struct    stat    *st;
  429.  
  430. {
  431.     if (getuid() == st->st_uid)
  432.        return((st->st_mode & 0700) >> 6);
  433.     else if (getgid() == st->st_gid)
  434.        return((st->st_mode & 070) >> 3);
  435.     else
  436.        return((st->st_mode & 07));
  437. }
  438.  
  439. /************************************************************************/
  440. EXPORT    char    *tt_full_path_of(s, mode)
  441.  
  442. char    *s;
  443. int    mode;
  444.  
  445. {    char    *path, *p, *q;
  446.     static    char    full_path[1024];
  447.     struct    stat    buf;
  448.  
  449.     if (*s == '/')
  450.        return(s);
  451.     path = getenv("PATH");
  452.     for (p = path, q = full_path; TRUE; )
  453.        if (*p == ':' || *p == '\0') {
  454.           *q = '\0';
  455.           strcat(full_path, "/");
  456.           strcat(full_path, s);
  457.           if (stat(full_path, &buf) == 0 && ((buf.st_mode & S_IFMT) == S_IFREG) && access(full_path, mode) == 0)
  458.              return(full_path);
  459.           q = full_path;
  460.           if (*p++ == '\0')
  461.              return(s);
  462.           }
  463.        else
  464.           *q++ = *p++;
  465. }
  466.