home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************/
- /* Copyright 1988 by Chuck Musciano and Harris Corporation */
- /* */
- /* Permission to use, copy, modify, and distribute this software */
- /* and its documentation for any purpose and without fee is */
- /* hereby granted, provided that the above copyright notice */
- /* appear in all copies and that both that copyright notice and */
- /* this permission notice appear in supporting documentation, and */
- /* that the name of Chuck Musciano and Harris Corporation not be */
- /* used in advertising or publicity pertaining to distribution */
- /* of the software without specific, written prior permission. */
- /* Chuck Musciano and Harris Corporation make no representations */
- /* about the suitability of this software for any purpose. It is */
- /* provided "as is" without express or implied warranty. */
- /************************************************************************/
-
-
- /************************************************************************/
- /* */
- /* misc: miscellaneous support routines for contool */
- /* */
- /************************************************************************/
-
- #include <stdio.h>
- #include <ctype.h>
-
- /************************************************************************/
- /* */
- /* getline read a line from a stream, removing the newline */
- /* and returning the length of the line. */
- /* */
- /************************************************************************/
-
- int getline(stream, string, max)
-
- FILE *stream;
- char *string;
- int max;
-
- { register int i, j;
-
- i = (int) fgets(string, max, stream);
- if (i == NULL)
- return(EOF);
- j = strlen(string);
- if (j > 0 && string[j - 1] == '\n')
- string[--j] = '\0';
- return(j);
- }
-
- /************************************************************************/
- /* */
- /* getopt extract options and their parameters from a */
- /* list of strings (most likely the command line */
- /* arguments. */
- /* */
- /* The 'opts' descriptor is a string of letters, */
- /* indicating which options are valid. If a */
- /* letter is followed by a ':', that option is */
- /* assumed to have a parameter following it. */
- /* Getopt returns the discovered option, NULL when */
- /* an illegal option is found, and EOF when no */
- /* more options are found. Options and their */
- /* parameters are removed from the string list. */
- /* */
- /************************************************************************/
-
- static delarg(argc, argv)
-
- int *argc;
- char **argv;
-
- { char *p;
-
- while (*argv = *(argv+1))
- argv++;
- (*argc)--;
- }
-
- char getopt(argc, argv, opts, parm)
-
- int *argc;
- char **argv;
- char *opts;
- char **parm;
-
- { char c, *p, *strcpy(), *index();
- int killed;
-
- *parm = NULL;
- while (*argv && ((**argv != '-') || (*(*argv+1) == '\0')))
- argv++;
- if (*argv == NULL)
- return(EOF);
- c = *(*argv+1);
- *++(*argv) = '-';
- if (killed = (*(*argv+1) == '\0'))
- delarg(argc, argv);
- if ((p = index(opts, c)) == NULL)
- c = '\0';
- else if (*(p+1) == ':') {
- *parm = killed ? *argv : *argv+1;
- delarg(argc, argv);
- }
- return(c);
- }
-
- /************************************************************************/
- /* */
- /* lower convert a string to lower case */
- /* */
- /************************************************************************/
-
- char *lower(s)
-
- char *s;
-
- { char *p;
-
- p = s;
- while (*s) {
- if (isupper(*s))
- *s = tolower(*s);
- s++;
- }
- return(p);
- }
-
- /************************************************************************/
- /* */
- /* verify(source, valid) */
- /* */
- /* char *source; */
- /* char *valid; */
- /* */
- /* This routine verifies that every character in source is also in */
- /* valid. */
- /* */
- /************************************************************************/
-
- verify(source, valid)
-
- char *source;
- char *valid;
-
- { register char *s;
-
- for ( ; *source; source++) {
- for (s = valid; *s && *s != *source; s++)
- ;
- if (*s == '\0')
- return(0);
- }
- return(1);
- }
-
- /************************************************************************/
- /* */
- /* saveargs Replicate and return a pointer to the argument */
- /* list passed in. */
- /* */
- /************************************************************************/
-
- char **saveargs(argc, argv)
-
- int argc;
- char **argv;
-
- { int i;
- char **copy;
-
- copy = (char **) malloc((argc + 1) * sizeof(char *));
- for (i = 0; i < argc; i++)
- strcpy(copy[i] = (char *) malloc(strlen(argv[i]) + 1), argv[i]);
- copy[i] = (char *) 0;
- return(copy);
- }
-
- /************************************************************************/
- /* */
- /* tokenize break a line into tokens */
- /* */
- /************************************************************************/
-
- tokenize(line, argc, argv, max)
-
- char *line;
- int *argc;
- char *argv[];
- int max;
-
- { char *buf, match;
-
- *argc = 0;
- buf = (char *) malloc(strlen(line) * 2 + 1);
- while (*line && (*argc < (max-1))) {
- while (isspace(*line))
- line++;
- argv[(*argc)++] = buf;
- switch (*line) {
- case '"' :
- case '\'' : match = *line++; /* remove the quote mark */
- while (*line && (*line != match))
- *buf++ = *line++;
- if (*line)
- line++; /* wipe out quote mark */
- break;
- default : while (*line && !isspace(*line) && (*line != '"') && (*line != '\''))
- *buf++ = *line++;
- break;
- }
- *buf++ = '\0';
- }
- *buf = '\0';
- argv[*argc] = (char *) 0;
- }
-
- /************************************************************************/
- /* */
- /* open_psuedo_tty opens the first available psuedo terminal */
- /* */
- /************************************************************************/
-
- #define P_POS 5
- #define L_POS 8
- #define D_POS 9
-
- #define PATH "/dev/ptyp0"
- #define LETTERS "pqr"
- #define DIGITS "0123456789abcdef"
-
- static char path[12];
-
- char *open_psuedo_tty(master, m_mode, slave, s_mode)
-
- FILE **master;
- char *m_mode;
- FILE **slave;
- char *s_mode;
-
- { char *s, *t;
-
- strcpy(path, PATH);
- for (s = LETTERS; *s && *master == NULL; s++) {
- path[L_POS] = *s;
- for (t = DIGITS; *t && *master == NULL; t++) {
- path[D_POS] = *t;
- *master = fopen(path, m_mode);
- }
- }
- if (*master != NULL) {
- path[P_POS] = 't';
- *slave = fopen(path, s_mode);
- path[P_POS] = 'p';
- }
- return(path);
- }
-