home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume3 / pcmail / part06 / params.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  2.9 KB  |  123 lines

  1. /*++
  2. /* NAME
  3. /*    params 3
  4. /* SUMMARY
  5. /*    communication parameter access
  6. /* PROJECT
  7. /*    pc-mail
  8. /* PACKAGE
  9. /*    library
  10. /* SYNOPSIS
  11. /*    #include "params.h"
  12. /*
  13. /*    Info *getparams();
  14. /* DESCRIPTION
  15. /*    getparams() returns a pointer to a table with communications
  16. /*    parameters. Usually communications parameters are set with the
  17. /*    "setup" option in the main menu of the interactive mail program.
  18. /*
  19. /*    First getparams() attempts to read from the setup file.
  20. /*    If that fails it creates an empty parameter table with
  21. /*    null string pointers as parameter values.
  22. /* FUNCTIONS AND MACROS
  23. /*    myalloc()
  24. /* BUGS
  25. /*    getparams() silently ignores any information in the
  26. /*    parameter file that it does not recognize.
  27. /*    getparams() will read the parameter file upon each call, even
  28. /*    if nothing has changed since the last read. Let us say that
  29. /*    it anticipates on multi-user environments.
  30. /* AUTHOR(S)
  31. /*    W.Z. Venema
  32. /*    Eindhoven University of Technology
  33. /*    Department of Mathematics and Computer Science
  34. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  35. /* CREATION DATE
  36. /*    Wed Apr  8 15:39:23 GMT+1:00 1987
  37. /* LAST MODIFICATION
  38. /*    Mon Apr  4 23:46:51 MET 1988
  39. /* VERSION/RELEASE
  40. /*    1.3
  41. /*--*/
  42.  
  43. #include <ctype.h>
  44. #include "defs.h"
  45. #include "path.h"
  46. #include "params.h"
  47.  
  48. /* Storage area for setup parameters */
  49.  
  50. hidden Info params[] = {
  51.     /* name */    /* name length */    /* string value */
  52.     PORT,    sizeof(PORT)-1,        0,
  53.     BAUD,    sizeof(BAUD)-1,        0,
  54.     HOST,    sizeof(HOST)-1,        0,
  55.     LOGIN,    sizeof(LOGIN)-1,    0,
  56.     DIAL,    sizeof(DIAL)-1,        0,
  57.     DISC,    sizeof(DISC)-1,        0,
  58.     0,        0,            0,
  59. };
  60.  
  61. hidden char *hackstr();                /* forward declaration */
  62.  
  63. /* getparams - try to get info from file, else make empty table */
  64.  
  65. public Info *getparams()
  66. {
  67.     char line[BUFSIZ];
  68.     register Info *ip;
  69.     FILE *fp;
  70.  
  71.     /* for cleanliness, we first clear all table entries */
  72.  
  73.     for (ip = params; ip->ident; ip++) {
  74.     if (ip->strval)
  75.         free(ip->strval);
  76.     ip->strval = NULL;
  77.     }
  78.  
  79.     /* then, try to copy parameter file info to the table */
  80.  
  81.     if (fp = fopen(parm_file(),"r")) {
  82.     while (fgets(line,BUFSIZ,fp)) {
  83.         for(ip = params; ip->ident; ip++) {
  84.         if (strncmp(ip->ident,line,ip->length) == 0) {
  85.             ip->strval = hackstr(line+ip->length);
  86.             break;
  87.         }
  88.         }
  89.     }
  90.     fclose(fp);
  91.     }
  92.     return(params);
  93. }
  94.  
  95. /* hackstr - cut away blanks around string and make copy */
  96.  
  97. hidden char *hackstr(s)
  98. register char *s;
  99. {
  100.     register char *r;
  101.     int len;
  102.  
  103.     while (*s && isspace(*s))                /* trim leading blks */
  104.     s++;
  105.     for (r = s+strlen(s); r > s && isspace(r[-1]); r--)    /* trim trailing blks */
  106.     /* void */ ;
  107.  
  108.     /*
  109.     * s is at the terminator or first non-blank char.
  110.     * r is at the terminator or first blank after the last non-blank char.
  111.     * Thus, the actual string length is r-s. We add one for the terminator.
  112.     * We don't allocate memory if the string is empty.
  113.     */
  114.  
  115.     if (len = r-s) {
  116.     char *cp = strncpy(myalloc(len+1),s,len);
  117.     cp[len] = '\0';
  118.     return(cp);
  119.     } else {
  120.     return(NULL);
  121.     }
  122. }
  123.