home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / contool2.1 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-21  |  6.4 KB  |  258 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. /*                                    */
  20. /*    misc:    miscellaneous support routines for contool        */
  21. /*                                    */
  22. /************************************************************************/
  23.  
  24. #include    <stdio.h>
  25. #include    <ctype.h>
  26.  
  27. /************************************************************************/
  28. /*                                    */
  29. /*    getline        read a line from a stream, removing the newline    */
  30. /*            and returning the length of the line.        */
  31. /*                                    */
  32. /************************************************************************/
  33.  
  34. int    getline(stream, string, max)
  35.  
  36. FILE    *stream;
  37. char    *string;
  38. int    max;
  39.  
  40. {    register    int    i, j;
  41.  
  42.     i = (int) fgets(string, max, stream);
  43.     if (i == NULL)
  44.        return(EOF);
  45.     j = strlen(string);
  46.     if (j > 0 && string[j - 1] == '\n')
  47.        string[--j] = '\0';
  48.     return(j);
  49. }
  50.  
  51. /************************************************************************/
  52. /*                                    */
  53. /*    getopt        extract options and their parameters from a    */
  54. /*            list of strings (most likely the command line    */
  55. /*            arguments.                    */
  56. /*                                    */
  57. /*            The 'opts' descriptor is a string of letters,    */
  58. /*            indicating which options are valid.  If a    */
  59. /*            letter is followed by a ':', that option is    */
  60. /*            assumed to have a parameter following it.    */
  61. /*            Getopt returns the discovered option, NULL when    */
  62. /*            an illegal option is found, and EOF when no    */
  63. /*            more options are found.  Options and their    */
  64. /*            parameters are removed from the string list.    */
  65. /*                                    */
  66. /************************************************************************/
  67.  
  68. static    delarg(argc, argv)
  69.  
  70. int    *argc;
  71. char    **argv;
  72.  
  73. {    char    *p;
  74.  
  75.     while (*argv = *(argv+1))
  76.        argv++;
  77.     (*argc)--;
  78. }
  79.  
  80. char    getopt(argc, argv, opts, parm)
  81.  
  82. int    *argc;
  83. char    **argv;
  84. char    *opts;
  85. char    **parm;
  86.  
  87. {    char    c, *p, *strcpy(), *index();
  88.     int    killed;
  89.  
  90.     *parm = NULL;
  91.     while (*argv && ((**argv != '-') || (*(*argv+1) == '\0')))
  92.        argv++;
  93.     if (*argv == NULL)
  94.        return(EOF);
  95.     c = *(*argv+1);
  96.     *++(*argv) = '-';
  97.     if (killed = (*(*argv+1) == '\0'))
  98.        delarg(argc, argv);
  99.     if ((p = index(opts, c)) == NULL)
  100.        c = '\0';
  101.     else if (*(p+1) == ':') {
  102.        *parm = killed ? *argv : *argv+1;
  103.        delarg(argc, argv);
  104.        }
  105.     return(c);
  106. }
  107.  
  108. /************************************************************************/
  109. /*                                    */
  110. /*    lower        convert a string to lower case            */
  111. /*                                    */
  112. /************************************************************************/
  113.  
  114. char    *lower(s)
  115.  
  116. char    *s;
  117.  
  118. {    char    *p;
  119.  
  120.     p = s;
  121.     while (*s) {
  122.        if (isupper(*s))
  123.           *s = tolower(*s);
  124.        s++;
  125.        }
  126.     return(p);
  127. }
  128.  
  129. /************************************************************************/
  130. /*                                    */
  131. /*    verify(source, valid)                        */
  132. /*                                    */
  133. /*    char    *source;                        */
  134. /*    char    *valid;                            */
  135. /*                                    */
  136. /*    This routine verifies that every character in source is also in    */
  137. /*    valid.                                */
  138. /*                                    */
  139. /************************************************************************/
  140.  
  141. verify(source, valid)
  142.  
  143. char    *source;
  144. char    *valid;
  145.  
  146. {    register    char    *s;
  147.  
  148.     for ( ; *source; source++) {
  149.        for (s = valid; *s && *s != *source; s++)
  150.           ;
  151.        if (*s == '\0')
  152.           return(0);
  153.        }
  154.     return(1);
  155. }
  156.  
  157. /************************************************************************/
  158. /*                                    */
  159. /*    saveargs    Replicate and return a pointer to the argument    */
  160. /*            list passed in.                    */
  161. /*                                    */
  162. /************************************************************************/
  163.  
  164. char    **saveargs(argc, argv)
  165.  
  166. int    argc;
  167. char    **argv;
  168.  
  169. {    int    i;
  170.     char    **copy;
  171.  
  172.     copy = (char **) malloc((argc + 1) * sizeof(char *));
  173.     for (i = 0; i < argc; i++)
  174.        strcpy(copy[i] = (char *) malloc(strlen(argv[i]) + 1), argv[i]);
  175.     copy[i] = (char *) 0;
  176.     return(copy);
  177. }
  178.  
  179. /************************************************************************/
  180. /*                                    */
  181. /*    tokenize    break a line into tokens            */
  182. /*                                    */
  183. /************************************************************************/
  184.  
  185. tokenize(line, argc, argv, max)
  186.  
  187. char    *line;
  188. int    *argc;
  189. char    *argv[];
  190. int    max;
  191.  
  192. {    char    *buf, match;
  193.  
  194.     *argc = 0;
  195.     buf = (char *) malloc(strlen(line) * 2 + 1);
  196.     while (*line && (*argc < (max-1))) {
  197.        while (isspace(*line))
  198.           line++;
  199.        argv[(*argc)++] = buf;
  200.        switch (*line) {
  201.           case '"'  :
  202.           case '\'' : match = *line++; /* remove the quote mark */
  203.                     while (*line && (*line != match))
  204.                        *buf++ = *line++;
  205.                     if (*line)
  206.                        line++; /* wipe out quote mark */
  207.                     break;
  208.           default   : while (*line && !isspace(*line) && (*line != '"') && (*line != '\''))
  209.                        *buf++ = *line++;
  210.                     break;
  211.           }
  212.        *buf++ = '\0';
  213.        }
  214.     *buf = '\0';
  215.     argv[*argc] = (char *) 0;
  216. }
  217.  
  218. /************************************************************************/
  219. /*                                    */
  220. /*    open_psuedo_tty    opens the first available psuedo terminal    */
  221. /*                                    */
  222. /************************************************************************/
  223.  
  224. #define        P_POS        5
  225. #define        L_POS        8
  226. #define        D_POS        9
  227.  
  228. #define        PATH        "/dev/ptyp0"
  229. #define        LETTERS        "pqr"
  230. #define        DIGITS        "0123456789abcdef"
  231.  
  232. static    char    path[12];
  233.  
  234. char    *open_psuedo_tty(master, m_mode, slave, s_mode)
  235.  
  236. FILE    **master;
  237. char    *m_mode;
  238. FILE    **slave;
  239. char    *s_mode;
  240.  
  241. {    char    *s, *t;
  242.  
  243.     strcpy(path, PATH);
  244.     for (s = LETTERS; *s && *master == NULL; s++) {
  245.        path[L_POS] = *s;
  246.        for (t = DIGITS; *t && *master == NULL; t++) {
  247.           path[D_POS] = *t;
  248.           *master = fopen(path, m_mode);
  249.           }
  250.        }
  251.     if (*master != NULL) {
  252.        path[P_POS] = 't';
  253.        *slave = fopen(path, s_mode);
  254.        path[P_POS] = 'p';
  255.        }
  256.     return(path);
  257. }
  258.