home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / contool2.2 / part02 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-26  |  4.6 KB  |  193 lines

  1. /************************************************************************/
  2. /*    Copyright 1988, 1989 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.  
  22. #include    "contool.h"
  23.  
  24. /************************************************************************/
  25. EXPORT    int    getline(stream, string, max)
  26.  
  27. FILE    *stream;
  28. char    *string;
  29. int    max;
  30.  
  31. {    register    int    i, j;
  32.  
  33.     i = (int) fgets(string, max, stream);
  34.     if (i == NULL)
  35.        return(EOF);
  36.     j = strlen(string);
  37.     if (j > 0 && string[j - 1] == '\n')
  38.        string[--j] = '\0';
  39.     return(j);
  40. }
  41.  
  42. /************************************************************************/
  43. PRIVATE    delarg(argc, argv)
  44.  
  45. int    *argc;
  46. char    **argv;
  47.  
  48. {    char    *p;
  49.  
  50.     while (*argv = *(argv+1))
  51.        argv++;
  52.     (*argc)--;
  53. }
  54.  
  55. /************************************************************************/
  56. EXPORT    char    getopt(argc, argv, opts, parm)
  57.  
  58. int    *argc;
  59. char    **argv;
  60. char    *opts;
  61. char    **parm;
  62.  
  63. {    char    c, *p, *strcpy(), *index();
  64.     int    killed;
  65.  
  66.     *parm = NULL;
  67.     while (*argv && ((**argv != '-') || (*(*argv+1) == '\0')))
  68.        argv++;
  69.     if (*argv == NULL)
  70.        return(EOF);
  71.     c = *(*argv+1);
  72.     *++(*argv) = '-';
  73.     if (killed = (*(*argv+1) == '\0'))
  74.        delarg(argc, argv);
  75.     if ((p = index(opts, c)) == NULL)
  76.        c = '\0';
  77.     else if (*(p+1) == ':') {
  78.        *parm = killed ? *argv : *argv+1;
  79.        delarg(argc, argv);
  80.        }
  81.     return(c);
  82. }
  83.  
  84. /************************************************************************/
  85. EXPORT    int    verify(source, valid)
  86.  
  87. char    *source;
  88. char    *valid;
  89.  
  90. {    register    char    *s;
  91.  
  92.     for ( ; *source; source++) {
  93.        for (s = valid; *s && *s != *source; s++)
  94.           ;
  95.        if (*s == '\0')
  96.           return(0);
  97.        }
  98.     return(1);
  99. }
  100.  
  101. /************************************************************************/
  102. EXPORT    char    **saveargs(argc, argv)
  103.  
  104. int    argc;
  105. char    **argv;
  106.  
  107. {    int    i;
  108.     char    **copy;
  109.  
  110.     copy = (char **) malloc((argc + 1) * sizeof(char *));
  111.     for (i = 0; i < argc; i++)
  112.        strcpy(copy[i] = (char *) malloc(strlen(argv[i]) + 1), argv[i]);
  113.     copy[i] = (char *) 0;
  114.     return(copy);
  115. }
  116.  
  117. #define        SIZE_INCREMENT        8
  118.  
  119. /************************************************************************/
  120. EXPORT    char    **tokenize(line, argc)
  121.  
  122. char    *line;
  123. int    *argc;
  124.  
  125. {    char    match, **argv, *buf, *p;
  126.     int    limit;
  127.  
  128.     buf = (char *) malloc(strlen(line) + 1);
  129.     *argc = 0;
  130.     argv = (char **) malloc((limit = SIZE_INCREMENT) * sizeof(char *));
  131.     while (*line) {
  132.        while (isspace(*line))
  133.           line++;
  134.        switch (*line) {
  135.           case '"'  :
  136.           case '\'' : match = *line++; /* remove the quote mark */
  137.                     for (p = buf; *line && (*line != match); )
  138.                        *p++ = *line++;
  139.                     if (*line)
  140.                        line++; /* wipe out quote mark */
  141.                     break;
  142.           default   : for (p = buf; *line && !isspace(*line) && (*line != '"') && (*line != '\''); )
  143.                        *p++ = *line++;
  144.                     break;
  145.           }
  146.        *p = '\0';
  147.        if (*buf) {
  148.           argv[(*argc)++] = strsave(buf);
  149.           if (*argc == limit)
  150.              argv = (char **) realloc(argv, (limit += SIZE_INCREMENT) * sizeof(char *));
  151.           }
  152.        }
  153.     free(buf);
  154.     argv[*argc] = (char *) 0;
  155.     return(argv);
  156. }
  157.  
  158. #define        P_POS        5
  159. #define        L_POS        8
  160. #define        D_POS        9
  161.  
  162. #define        PATH        "/dev/ptyp0"
  163. #define        LETTERS        "pqr"
  164. #define        DIGITS        "0123456789abcdef"
  165.  
  166. PRIVATE    char    path[12];
  167.  
  168. /************************************************************************/
  169. EXPORT    char    *open_psuedo_tty(master, m_mode, slave, s_mode)
  170.  
  171. FILE    **master;
  172. char    *m_mode;
  173. FILE    **slave;
  174. char    *s_mode;
  175.  
  176. {    char    *s, *t;
  177.  
  178.     strcpy(path, PATH);
  179.     for (s = LETTERS; *s && *master == NULL; s++) {
  180.        path[L_POS] = *s;
  181.        for (t = DIGITS; *t && *master == NULL; t++) {
  182.           path[D_POS] = *t;
  183.           *master = fopen(path, m_mode);
  184.           }
  185.        }
  186.     if (*master != NULL) {
  187.        path[P_POS] = 't';
  188.        *slave = fopen(path, s_mode);
  189.        path[P_POS] = 'p';
  190.        }
  191.     return(path);
  192. }
  193.