home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / tooltool2.1c / part05 / func.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-06  |  13.5 KB  |  556 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    <pwd.h>
  27. #include    <grp.h>
  28.  
  29. #include    <sys/types.h>
  30. #include    <sys/stat.h>
  31. #include    <sys/file.h>
  32.  
  33. #include    "tooltool.h"
  34.  
  35. PUBLIC    char    *getenv(), *rindex();
  36.  
  37. PRIVATE    v_ptr    do_cardinality(),
  38.         do_cd(),
  39.         do_executable(),
  40.         do_exists(),
  41.         do_format(),
  42.         do_getenv(),
  43.         do_group(),
  44.         do_head(),
  45.         do_index(),
  46.         do_is_open(),
  47.         do_length(),
  48.         do_output_of(),
  49.         do_pwd(),
  50.         do_readable(),
  51.         do_root(),
  52.         do_selection(),
  53.         do_stat(),
  54.         do_substr(),
  55.         do_suffix(),
  56.         do_system(),
  57.         do_tail(),
  58.         do_tokenize(),
  59.         do_user(),
  60.         do_verify(),
  61.         do_writable();
  62.  
  63. PRIVATE    struct    {char    *name;
  64.          f_ptr    func;
  65.         } func[] = {{"cardinality", do_cardinality},
  66.                 {"cd",          do_cd},
  67.                 {"executable",  do_executable},
  68.                 {"exists",      do_exists},
  69.                 {"format",      do_format},
  70.                 {"getenv",      do_getenv},
  71.                 {"group",       do_group},
  72.                 {"head",        do_head},
  73.                 {"index",       do_index},
  74.                 {"is_open",     do_is_open},
  75.                 {"length",      do_length},
  76.                 {"output_of",   do_output_of},
  77.                 {"pwd",         do_pwd},
  78.                 {"readable",    do_readable},
  79.                 {"root",        do_root},
  80.                 {"selection",   do_selection},
  81.                 {"stat",        do_stat},
  82.                 {"substr",      do_substr},
  83.                 {"suffix",      do_suffix},
  84.                 {"system",      do_system},
  85.                 {"tail",        do_tail},
  86.                 {"tokenize",    do_tokenize},
  87.                 {"user",        do_user},
  88.                 {"verify",      do_verify},
  89.                 {"writable",    do_writable},
  90.                 {NULL,          NULL}};
  91.  
  92. /************************************************************************/
  93. EXPORT    f_ptr    tt_is_function(s)
  94.  
  95. char    *s;
  96.  
  97. {    int    i;
  98.  
  99.     for (i = 0; func[i].name; i++)
  100.        if (strcmp(func[i].name, s) == 0)
  101.           return(func[i].func);
  102.     return(NULL);
  103. }
  104.  
  105. /************************************************************************/
  106. PRIVATE    char    *fix_ctime(time)
  107.  
  108. int    *time;
  109.  
  110. {    char    *p;
  111.  
  112.     p = ctime(time);
  113.     p[24] = '\0';
  114.     return(p);
  115. }
  116.  
  117. /************************************************************************/
  118. PRIVATE    e_ptr    get_parm(e, n)
  119.  
  120. e_ptr    e;
  121. int    n;
  122.  
  123. {    e_ptr    e1;
  124.     int    i, depth;
  125.  
  126.     if (e == NULL)
  127.        return(NULL);
  128.     for (e1 = e, depth = 1; e1->op == E_COMMA; e1 = e1->left)
  129.        depth++;
  130.     if (n > depth)
  131.        return(NULL);
  132.     else if (depth == 1)
  133.        return(e);
  134.     else {
  135.        for (i = depth - n; i; i--)
  136.           e = e->left;
  137.        return((n == 1)? e : e->right);
  138.        }
  139. }
  140.  
  141. /************************************************************************/
  142. PRIVATE    int    child_count(v)
  143.  
  144. v_ptr    v;
  145.  
  146. {
  147.     return(v? child_count(v->left) + child_count(v->right) + 1 : 0);
  148. }
  149.  
  150. /************************************************************************/
  151. PRIVATE    v_ptr    do_cardinality(e)
  152.  
  153. e_ptr    e;
  154.  
  155. {    v_ptr    v;
  156.  
  157.     v = tt_eval(e);
  158.     if (is_array(v))
  159.        return(tt_int_result(child_count(v->value)));
  160.     else
  161.        return(tt_int_result(0));
  162. }
  163.  
  164. /************************************************************************/
  165. PRIVATE    v_ptr    do_cd(e)
  166.  
  167. e_ptr    e;
  168.  
  169. {
  170.     return(tt_int_result(chdir(tt_string_of(tt_eval(e)))? 0 : 1));
  171. }
  172.  
  173. /************************************************************************/
  174. PRIVATE    v_ptr    do_executable(e)
  175.  
  176. e_ptr    e;
  177.  
  178. {    struct    stat    buf;
  179.     int    result;
  180.     char    *p;
  181.  
  182.     if (stat(p = tt_string_of(tt_eval(e)), &buf) == 0 && access(p, X_OK) == 0)
  183.        result = 1;
  184.     else
  185.        result = 0;
  186.     return(tt_int_result(result));
  187. }
  188.  
  189. /************************************************************************/
  190. PRIVATE    v_ptr    do_exists(e)
  191.  
  192. e_ptr    e;
  193.  
  194. {    struct    stat    buf;
  195.  
  196.     return(tt_int_result((stat(tt_string_of(tt_eval(e)), &buf) == -1)? 0 : 1));
  197. }
  198.  
  199. /************************************************************************/
  200. PRIVATE    v_ptr    do_format(e)
  201.  
  202. e_ptr    e;
  203.  
  204. {    char    fmt[1024], result[1024], *p, *q, *r, *format;
  205.     int    parm;
  206.     e_ptr    e1;
  207.  
  208.     format = tt_string_of(tt_eval(get_parm(e, 1)));
  209.     for (parm = 1, q = result, p = fmt; *format; format++) {
  210.        *p++ = *format;
  211.        if (*format == '%') {
  212.           for (format++; index("0123456789.-+ #", *format); )
  213.              *p++ = *format++;
  214.           *p++ = *format;
  215.           *p = '\0';
  216.           if (index("eEfgG", *format)) { /* print as a double */
  217.              if ((e1 = get_parm(e, ++parm)) == NULL)
  218.                 abend("too few parameters supplied to 'format'");
  219.              sprintf(q, fmt, tt_eval(e1)->number);
  220.              }
  221.           else if (index("cdloxXu", *format)) { /* print as integer */
  222.              if ((e1 = get_parm(e, ++parm)) == NULL)
  223.                 abend("too few parameters supplied to 'format'");
  224.              sprintf(q, fmt, (int) tt_eval(e1)->number);
  225.              }
  226.           else if (*format == 's') { /* a string */
  227.              if ((e1 = get_parm(e, ++parm)) == NULL)
  228.                 abend("too few parameters supplied to 'format'");
  229.              sprintf(q, fmt, tt_string_of(tt_eval(e1)));
  230.              }
  231.           else if (*format == '%')
  232.              sprintf(q, fmt);
  233.           else
  234.              abend("invalid format character passed to 'format': %c", *format);
  235.           q += strlen(q);
  236.           p = fmt;
  237.           }
  238.        }
  239.     *p = '\0';
  240.     sprintf(q, fmt);
  241.     return(tt_string_result(result));
  242. }
  243.  
  244. /************************************************************************/
  245. PRIVATE    v_ptr    do_getenv(e)
  246.  
  247. e_ptr    e;
  248.  
  249. {    register    char    *p;
  250.  
  251.     p = getenv(tt_string_of(tt_eval(e)));
  252.     return(tt_string_result(p? p : ""));
  253. }
  254.  
  255. /************************************************************************/
  256. PRIVATE    v_ptr    do_group()
  257.  
  258. {    register    struct    group    *gp;
  259.     register    int    gid;
  260.  
  261.     if (gp = getgrgid(gid = getgid()))
  262.        return(tt_string_result(gp->gr_name));
  263.     else
  264.        return(tt_int_result(gid));
  265. }
  266.  
  267. /************************************************************************/
  268. PRIVATE    v_ptr    do_head(e)
  269.  
  270. e_ptr    e;
  271.  
  272. {    char    *p, *s;
  273.  
  274.     p = tt_string_of(tt_eval(e));
  275.     if (s = rindex(p, '/'))
  276.        *s = '\0';
  277.     return(tt_string_result(p));
  278. }
  279.  
  280. /************************************************************************/
  281. PRIVATE    v_ptr    do_index(e)
  282.  
  283. {    char    *source, *target;
  284.     int    i;
  285.  
  286.     source = tt_string_of(tt_eval(get_parm(e, 1)));
  287.     target = tt_string_of(tt_eval(get_parm(e, 2)));
  288.     if (source == NULL || target == NULL)
  289.        abend("too few parameters supplied to 'index'");
  290.     for (i = 1; *source; source++, i++) {
  291.        for ( ; *source && *source != *target; source++, i++)
  292.           ;
  293.        if (strncmp(source, target, strlen(target)) == 0)
  294.           return(tt_int_result(i));
  295.        }
  296.     return(tt_int_result(0));
  297. }
  298.  
  299. /************************************************************************/
  300. PRIVATE    v_ptr    do_is_open()
  301.  
  302. {
  303.     return(tt_int_result(window_get(tt_base_window->frame, FRAME_CLOSED)? 0 : 1));
  304. }
  305.  
  306. /************************************************************************/
  307. PRIVATE    v_ptr    do_length(e)
  308.  
  309. e_ptr    e;
  310.  
  311. {
  312.     return(tt_int_result(strlen(tt_string_of(tt_eval(e)))));
  313. }
  314.  
  315. /************************************************************************/
  316. PRIVATE    v_ptr    do_output_of(e)
  317.  
  318. e_ptr    e;
  319.  
  320. {    FILE    *f;
  321.     char    *buf, *p;
  322.     int    amt, size;
  323.  
  324.     if ((f = popen(tt_string_of(tt_eval(e)), "r")) == NULL)
  325.        return(tt_int_result(-1));
  326.     for (buf = p = tt_emalloc(65536), size = 65536; size > 0 && (amt = fread(p, sizeof(char), 1024, f)); p += amt, size -= amt)
  327.        ;
  328.     *p = '\0';
  329.     pclose(f);
  330.     return(tt_string_result(buf));
  331. }
  332.  
  333. /************************************************************************/
  334. PRIVATE    v_ptr    do_pwd()
  335.  
  336. {    char    buf[1024];
  337.  
  338.     getwd(buf);
  339.     return(tt_string_result(buf));
  340. }
  341.  
  342. /************************************************************************/
  343. PRIVATE    v_ptr    do_readable(e)
  344.  
  345. e_ptr    e;
  346.  
  347. {    struct    stat    buf;
  348.     int    result;
  349.     char    *p;
  350.  
  351.     if (stat(p = tt_string_of(tt_eval(e)), &buf) == 0 && access(p, R_OK) == 0)
  352.        result = 1;
  353.     else
  354.        result = 0;
  355.     return(tt_int_result(result));
  356. }
  357.  
  358. /************************************************************************/
  359. PRIVATE    v_ptr    do_root(e)
  360.  
  361. e_ptr    e;
  362.  
  363. {    char    *s, *p, *q;
  364.  
  365.     p = tt_string_of(tt_eval(e));
  366.     s = rindex(p, '/');
  367.     q = rindex(p, '.');
  368.     if (s) {
  369.        if (q > s)
  370.           *q = '\0';
  371.        }
  372.     else if (q)
  373.        *q = '\0';
  374.     return(tt_string_result(p));
  375. }
  376.  
  377. /************************************************************************/
  378. PRIVATE    v_ptr    do_selection(e)
  379.  
  380. e_ptr    e;
  381.  
  382. {
  383.     return(tt_string_result(tt_get_selection((int) tt_eval(e)->number)));
  384. }
  385.  
  386. /************************************************************************/
  387. PRIVATE    v_ptr    do_stat(e)
  388.  
  389. e_ptr    e;
  390.  
  391. {    register    v_ptr    v;
  392.     struct    stat    buf;
  393.     register    struct    passwd    *pp;
  394.     register    struct    group    *gp;
  395.  
  396.     v = (v_ptr) tt_emalloc(sizeof(v_data));
  397.     v->kind = V_ARRAY;
  398.     v->index = NULL;
  399.     v->value = NULL;
  400.     v->left = NULL;
  401.     v->right = NULL;
  402.     if (stat(tt_string_of(tt_eval(e)), &buf) == 0) {
  403.        tt_insert_array(&(v->value), estrsave("mode"), tt_int_result(buf.st_mode));
  404.        if (pp = getpwuid(buf.st_uid))
  405.           tt_insert_array(&(v->value), estrsave("uid"), tt_string_result(pp->pw_name));
  406.        else
  407.           tt_insert_array(&(v->value), estrsave("uid"), tt_int_result(buf.st_uid));
  408.        if (gp = getgrgid(buf.st_gid))
  409.           tt_insert_array(&(v->value), estrsave("gid"), tt_string_result(gp->gr_name));
  410.        else
  411.           tt_insert_array(&(v->value), estrsave("gid"), tt_int_result(buf.st_gid));
  412.        tt_insert_array(&(v->value), estrsave("size"), tt_int_result(buf.st_size));
  413.        tt_insert_array(&(v->value), estrsave("atime"), tt_string_result(fix_ctime(&(buf.st_atime))));
  414.        tt_insert_array(&(v->value), estrsave("mtime"), tt_string_result(fix_ctime(&(buf.st_mtime))));
  415.        tt_insert_array(&(v->value), estrsave("ctime"), tt_string_result(fix_ctime(&(buf.st_ctime))));
  416.        }
  417.     return(v);
  418. }
  419.  
  420. /************************************************************************/
  421. PRIVATE    v_ptr    do_substr(e)
  422.  
  423. e_ptr    e;
  424.  
  425. {    e_ptr    string, start, length;
  426.     char    *s;
  427.     int    st, l;
  428.  
  429.     string = get_parm(e, 1);
  430.     start = get_parm(e, 2);
  431.     length = get_parm(e, 3);
  432.     if (get_parm(e, 4))
  433.        abend("too many arguments passed to 'substr'");
  434.     s = estrsave(tt_string_of(tt_eval(string)));
  435.     if ((st = start? tt_eval(start)->number - 1 : 0) < 0)
  436.        abend("negative starting position passed to 'substr': %d", st);
  437.     if ((l = length? tt_eval(length)->number : 0x7fffffff) < 0)
  438.        abend("negative length passed to 'substr': %d", l);
  439.     if (st > strlen(s))
  440.        *s = '\0';
  441.     else
  442.        s += st;
  443.     if (l <= strlen(s))
  444.        *(s + l) = '\0';
  445.     return(tt_string_result(s));
  446. }
  447.  
  448. /************************************************************************/
  449. PRIVATE    v_ptr    do_suffix(e)
  450.  
  451. e_ptr    e;
  452.  
  453. {    char    *s, *p, *q;
  454.  
  455.     p = tt_string_of(tt_eval(e));
  456.     s = rindex(p, '/');
  457.     q = rindex(p, '.');
  458.     if (s)
  459.        p = (q > s)? q + 1 : "";
  460.     else
  461.        p = q? q + 1 : "";
  462.     return(tt_string_result(p));
  463. }
  464.  
  465. /************************************************************************/
  466. PRIVATE    v_ptr    do_system(e)
  467.  
  468. e_ptr    e;
  469.  
  470. {
  471.     return(tt_int_result(system(tt_string_of(tt_eval(e)))));
  472. }
  473.  
  474. /************************************************************************/
  475. PRIVATE    v_ptr    do_tail(e)
  476.  
  477. e_ptr    e;
  478.  
  479. {    char    *p, *s;
  480.  
  481.     p = tt_string_of(tt_eval(e));
  482.     if (s = rindex(p, '/'))
  483.        p = s + 1;
  484.     return(tt_string_result(p));
  485. }
  486.  
  487. /************************************************************************/
  488. PRIVATE    v_ptr    do_tokenize(e)
  489.  
  490. e_ptr    e;
  491.  
  492. {    register    char    **tokens, *line;
  493.     register    int    i, max_count;
  494.     register    v_ptr    v;
  495.     char    buf[20];
  496.     int    count;
  497.  
  498.     line = tt_string_of(tt_eval(e));
  499.     tokens = (char **) tt_emalloc((max_count = strlen(line) / 2 + 2) * sizeof(char *));
  500.     tokenize(line, &count, tokens, max_count);
  501.     v = (v_ptr) tt_emalloc(sizeof(v_data));
  502.     v->kind = V_ARRAY;
  503.     v->index = NULL;
  504.     v->value = NULL;
  505.     v->left = NULL;
  506.     v->right = NULL;
  507.     for (i = 0; i < count; i++) {
  508.        sprintf(buf, "%d", i);
  509.        tt_insert_array(&(v->value), estrsave(buf), tt_string_result(tokens[i]));
  510.        }
  511.     return(v);
  512. }
  513.  
  514. /************************************************************************/
  515. PRIVATE    v_ptr    do_user()
  516.  
  517. {    register    struct    passwd    *pp;
  518.     register    int    uid;
  519.  
  520.     if (pp = getpwuid(uid = getuid()))
  521.        return(tt_string_result(pp->pw_name));
  522.     else
  523.        return(tt_int_result(uid));
  524. }
  525.  
  526. /************************************************************************/
  527. PRIVATE    v_ptr    do_verify(e)
  528.  
  529. {    char    *source, *valid;
  530.  
  531.     source = tt_string_of(tt_eval(get_parm(e, 1)));
  532.     valid = tt_string_of(tt_eval(get_parm(e, 2)));
  533.     if (source == NULL || valid == NULL)
  534.        abend("too few parameters supplied to 'verify'");
  535.     for ( ; *source; source++)
  536.        if (index(valid, *source) == NULL)
  537.           return(tt_int_result(0));
  538.     return(tt_int_result(1));
  539. }
  540.  
  541. /************************************************************************/
  542. PRIVATE    v_ptr    do_writable(e)
  543.  
  544. e_ptr    e;
  545.  
  546. {    struct    stat    buf;
  547.     int    result;
  548.     char    *p;
  549.  
  550.     if (stat(p = tt_string_of(tt_eval(e)), &buf) == 0 && access(p, W_OK) == 0)
  551.        result = 1;
  552.     else
  553.        result = 0;
  554.     return(tt_int_result(result));
  555. }
  556.