home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / b / bmh02src.zip / MISC.C < prev    next >
C/C++ Source or Header  |  1992-08-16  |  3KB  |  154 lines

  1. /*
  2.    misc.c : Copyright Paul Healy, EI9GL, 1992.
  3.  
  4.    Derived from bm.
  5.  
  6.    Copyright 1986 Bdale Garbee, All Rights Reserved.
  7.    Permission granted for non-commercial copying and use, provided
  8.    this notice is retained.
  9.    Copyright 1987 1988 Dave Trulli NN2Z, All Rights Reserved.
  10.    Permission granted for non-commercial copying and use, provided
  11.    this notice is retained.
  12.  
  13.    920603 : Created.
  14.    920802 : Cleaned up parse a little.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20. #include <io.h>
  21. #include "misc.h"
  22. #include "rc.h"
  23.  
  24. /*
  25.  * parse a line into argv array. Return argc, -1 on error.
  26.  */
  27. int
  28. parse(char *line, char *argv[], int maxargs)
  29. {
  30.    int argc = 0;
  31.  
  32.    while ( argc < maxargs ) {
  33.       int qflag = 0;
  34.  
  35.       while (isspace(*line) || (*line == ',') )  /* Skip leading white space */
  36.          line++;
  37.       if(*line == '\0')
  38.          break;
  39.  
  40.       if(*line == '"') {  /* Check for quoted token */
  41.          line++;          /* Suppress quote */
  42.          qflag = 1;
  43.          }
  44.       argv[argc++] = line;   /* Beginning of token */
  45.  
  46.       /*
  47.        * Find terminating delimiter
  48.        */
  49.  
  50.       if (qflag) {
  51.          if ( (line = strchr(line, '"')) == NULL )
  52.             return -1;
  53.          *line++ = '\0';
  54.          }
  55.       else {
  56.          char *cp;
  57.  
  58.          /* Find space or tab. If not present,
  59.           * then we've already found the last
  60.           * token.
  61.           * 920802 : note that bm would wrongly leave trailing tab chars
  62.           *          after a token
  63.           */
  64.          if( (cp = strpbrk(line, " \t,\n")) == NULL)
  65.             break;
  66.          *cp++ = '\0';
  67.          line = cp;
  68.       }
  69.    }
  70.    if (argc == maxargs)
  71.       return -1;
  72.  
  73.    argv[argc] = NULL;
  74.  
  75.    return argc;
  76. }
  77.  
  78. /*
  79.  * replace terminating end of line marker with null
  80.  */
  81. char *
  82. rip(char *s)
  83. {
  84.    char *p = s;
  85.  
  86.    for (; *s; s++)
  87.       if (*s == '\n') {
  88.          *s = '\0';
  89.          break;
  90.       }
  91.    return p;
  92. }
  93.  
  94. int
  95. wanted(int msg, int argc, char *argv[], int default_msg)
  96. {
  97.    int i, m;
  98.    char *p;
  99.  
  100.    if (argc == 1)
  101.       return msg == default_msg;
  102.  
  103.    for (i=1; i<argc; i++)
  104.       if (isdigit(argv[i][0])) {
  105.          if ((m=atoi(argv[i])) == msg)
  106.             return 1;
  107.          if ( (p=strchr(argv[i], '-')) != NULL)
  108.             if ( (msg>=m) && (msg<=atoi(p+1)) )
  109.                return 1;
  110.          }
  111.    return 0;
  112. }
  113.  
  114. int
  115. setupbm(void)
  116. {
  117.    static int done = 0;
  118.  
  119.    if (done)             /* don't keep loading config in bmh */
  120.       return 0;
  121.    else
  122.       done = 1;
  123.  
  124.    if (loadconfig()==-1)
  125.       return -1;
  126.  
  127.    if (/* isatty(fileno(stdout)) && */ (setvbuf(stdout, NULL, _IOFBF, 2048) != 0)) {
  128.       fprintf(stderr,"bm: can't buffer stdout\n");
  129.       return -1;
  130.       }
  131.  
  132.    return 0;
  133. }
  134.  
  135. #ifndef DEBUG
  136. /*
  137.  * null checking taken from ka9q NOS
  138.  */
  139. static int oldNull;
  140.  
  141. void
  142. null_init(void)
  143. {
  144.     oldNull = *(unsigned short *)NULL;
  145. }
  146.  
  147. void
  148. null_check(char *s)
  149. {
  150.     if(*(unsigned short *)NULL != oldNull)
  151.       fprintf(stderr, "null: location smashed at %s\n", s);
  152. }
  153. #endif
  154.