home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume10 / contool / part02 / misc.c < prev    next >
C/C++ Source or Header  |  1990-10-30  |  6KB  |  267 lines

  1. /************************************************************************/
  2. /*    Copyright 1988-1990 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.  This     */
  15. /*    software may not be sold without the prior explicit permission    */
  16. /*    of Harris Corporation.                        */
  17. /************************************************************************/
  18.  
  19. #include    <stdio.h>
  20. #include    <ctype.h>
  21. #include    <pwd.h>
  22.  
  23. #include    "manifest.h"
  24. #include    "contool.h"
  25.  
  26. /************************************************************************/
  27. EXPORT    int    getline(stream, string, max)
  28.  
  29. FILE    *stream;
  30. char    *string;
  31. int    max;
  32.  
  33. {    register    int    i, j;
  34.  
  35.     i = (int) fgets(string, max, stream);
  36.     if (i == NULL)
  37.        return(EOF);
  38.     j = strlen(string);
  39.     if (j > 0 && string[j - 1] == '\n')
  40.        string[--j] = '\0';
  41.     return(j);
  42. }
  43.  
  44. /************************************************************************/
  45. PRIVATE    delarg(argc, argv)
  46.  
  47. int    *argc;
  48. char    **argv;
  49.  
  50. {    char    *p;
  51.  
  52.     while (*argv = *(argv+1))
  53.        argv++;
  54.     (*argc)--;
  55. }
  56.  
  57. /************************************************************************/
  58. EXPORT    char    getopt(argc, argv, opts, parm)
  59.  
  60. int    *argc;
  61. char    **argv;
  62. char    *opts;
  63. char    **parm;
  64.  
  65. {    char    c, *p, *strcpy(), *index();
  66.     int    killed;
  67.  
  68.     *parm = NULL;
  69.     while (*argv && ((**argv != '-') || (*(*argv+1) == '\0')))
  70.        argv++;
  71.     if (*argv == NULL)
  72.        return(EOF);
  73.     c = *(*argv+1);
  74.     *++(*argv) = '-';
  75.     if (killed = (*(*argv+1) == '\0'))
  76.        delarg(argc, argv);
  77.     if ((p = index(opts, c)) == NULL)
  78.        c = '\0';
  79.     else if (*(p+1) == ':') {
  80.        *parm = killed ? *argv : *argv+1;
  81.        delarg(argc, argv);
  82.        }
  83.     return(c);
  84. }
  85.  
  86. /************************************************************************/
  87. EXPORT    int    verify(source, valid)
  88.  
  89. char    *source;
  90. char    *valid;
  91.  
  92. {    register    char    *s;
  93.  
  94.     for ( ; *source; source++) {
  95.        for (s = valid; *s && *s != *source; s++)
  96.           ;
  97.        if (*s == '\0')
  98.           return(0);
  99.        }
  100.     return(1);
  101. }
  102.  
  103. /************************************************************************/
  104. EXPORT    char    **saveargs(argc, argv)
  105.  
  106. int    argc;
  107. char    **argv;
  108.  
  109. {    int    i;
  110.     char    **copy;
  111.  
  112.     copy = (char **) malloc((argc + 1) * sizeof(char *));
  113.     for (i = 0; i < argc; i++)
  114.        strcpy(copy[i] = (char *) malloc(strlen(argv[i]) + 1), argv[i]);
  115.     copy[i] = (char *) 0;
  116.     return(copy);
  117. }
  118.  
  119. #define        SIZE_INCREMENT        8
  120.  
  121. /************************************************************************/
  122. EXPORT    char    **tokenize(line, argc)
  123.  
  124. char    *line;
  125. int    *argc;
  126.  
  127. {    char    match, **argv, *buf, *p;
  128.     int    limit;
  129.  
  130.     buf = (char *) malloc(strlen(line) + 1);
  131.     *argc = 0;
  132.     argv = (char **) malloc((limit = SIZE_INCREMENT) * sizeof(char *));
  133.     while (*line) {
  134.        while (isspace(*line))
  135.           line++;
  136.        switch (*line) {
  137.           case '"'  :
  138.           case '\'' : match = *line++; /* remove the quote mark */
  139.                     for (p = buf; *line && (*line != match); ) {
  140.                        if (*line == '\\')
  141.                           line++;
  142.                        if (*line)
  143.                           *p++ = *line++;
  144.                        }
  145.                     if (*line)
  146.                        line++; /* wipe out quote mark */
  147.                     break;
  148.           default   : for (p = buf; *line && !isspace(*line) && (*line != '"') && (*line != '\''); )
  149.                        *p++ = *line++;
  150.                     break;
  151.           }
  152.        *p = '\0';
  153.        if (*buf) {
  154.           argv[(*argc)++] = strsave(buf);
  155.           if (*argc == limit)
  156.              argv = (char **) realloc(argv, (limit += SIZE_INCREMENT) * sizeof(char *));
  157.           }
  158.        }
  159.     free(buf);
  160.     argv[*argc] = (char *) 0;
  161.     return(argv);
  162. }
  163.  
  164. #define        P_POS        5
  165. #define        L_POS        8
  166. #define        D_POS        9
  167.  
  168. #define        PATH        "/dev/ptyp0"
  169. #define        LETTERS        "pqr"
  170. #define        DIGITS        "0123456789abcdef"
  171.  
  172. PRIVATE    char    path[12];
  173.  
  174. /************************************************************************/
  175. EXPORT    char    *open_psuedo_tty(master, m_mode, slave, s_mode)
  176.  
  177. FILE    **master;
  178. char    *m_mode;
  179. FILE    **slave;
  180. char    *s_mode;
  181.  
  182. {    char    *s, *t;
  183.  
  184.     strcpy(path, PATH);
  185.     for (s = LETTERS; *s && *master == NULL; s++) {
  186.        path[L_POS] = *s;
  187.        for (t = DIGITS; *t && *master == NULL; t++) {
  188.           path[D_POS] = *t;
  189.           *master = fopen(path, m_mode);
  190.           }
  191.        }
  192.     if (*master != NULL) {
  193.        path[P_POS] = 't';
  194.        *slave = fopen(path, s_mode);
  195.        path[P_POS] = 'p';
  196.        }
  197.     return(path);
  198. }
  199.  
  200. /************************************************************************/
  201. EXPORT    char    *expand_tilde(path)
  202.  
  203. char    *path;
  204.  
  205. {    char    s[1024], *p;
  206.     struct    passwd    *pw;
  207.  
  208.     if (*path == '~') {
  209.        if (path[1] == '/' || path[1] == '\0') {
  210.           strcpy(s, getenv("HOME"));
  211.           strcat(s, path + 1);
  212.           }
  213.        else {
  214.           if ((p = index(path, '/')) != NULL)
  215.              *p = '\0';
  216.           if ((pw = getpwnam(path + 1)) != NULL) {
  217.              strcpy(s, pw->pw_dir);
  218.              if (p != NULL) {
  219.                 strcat(s, "/");
  220.                 strcat(s, p + 1);
  221.                 }
  222.              }
  223.           else {
  224.              if (p != NULL)
  225.                 *p = '/';
  226.              strcpy(s, path);
  227.              }
  228.           }
  229.        return(strsave(s));
  230.        }
  231.     else
  232.        return(strsave(path));
  233. }
  234.  
  235. /************************************************************************/
  236. EXPORT    int    is_empty(s)
  237.  
  238. char    *s;
  239.  
  240. {
  241.     if (s == NULL)
  242.        return(TRUE);
  243.     for (; *s; s++)
  244.        if (!isspace(*s))
  245.           return(FALSE); 
  246.     return(TRUE);
  247. }
  248.  
  249. /************************************************************************/
  250. EXPORT    char    *check_escapes(s)
  251.  
  252. char    *s;
  253.  
  254. {    static    char    buf[1024];
  255.     char    *p;
  256.  
  257.     for (p = buf; *s; s++, p++)
  258.        if (*s == '\\' || *s == '"') {
  259.           *p++ = '\\';
  260.           *p = *s;
  261.           }
  262.        else
  263.           *p = *s;
  264.     *p = '\0';
  265.     return(buf);
  266. }
  267.