home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 446.lha / parseargs / arglist.c < prev    next >
C/C++ Source or Header  |  1990-12-05  |  2KB  |  117 lines

  1. #include <parseargs.h>
  2. #include <ctype.h>
  3.  
  4. #ifdef __STDC__
  5. typedef void *pointer;
  6. #else
  7. typedef char *pointer;
  8. #endif
  9.  
  10. extern pointer malloc();
  11.  
  12. #define ALL_AD        ad = argd; ad->ad_name != '\0'; ad++
  13. #define ALL_DEFS    ad = _DefaultArgs; ad->ad_name != '\0'; ad++
  14.  
  15. extern char    *ProgName;
  16.  
  17. /* Argument list utility routines. After processing, parseargs calls
  18.  * cleanup_lists to reverse all argument lists so they stay in order.
  19.  */
  20.  
  21. /* Reverse a list */
  22. struct arglist *reverselist(from)
  23. struct arglist *from;
  24. {
  25.     struct arglist *to, *tmp;
  26.  
  27.     to = NULL;
  28.     while(from) {
  29.         tmp = from; /* remove top from old list */
  30.         from = from->nl_next;
  31.         tmp->nl_next = to; /* insert top in new list */
  32.         to = tmp;
  33.     }
  34.     return to;
  35. }
  36.  
  37. /* Reverse all arglists in argd */
  38. cleanup_lists(argd)
  39. ARGDESC *argd;
  40. {
  41.     ARGDESC *ad;
  42.  
  43.     for(ALL_AD) {
  44.         if( (ad->ad_flags & ARGLIST) &&
  45.             *(struct arglist **)ad->ad_valp) {
  46.             *(struct arglist **)ad->ad_valp =
  47.                 reverselist( *(struct arglist **)ad->ad_valp );
  48.         }
  49.     }
  50. }
  51.  
  52. /*
  53. **  ARGlist -- list argument translation routines.
  54. **
  55. **    Each of these converts a parameter value to the internal form,
  56. **    including validity checking.  Their parameters and return values
  57. **    all behave identically. These are the routines for dealing with
  58. **    lists...
  59. **
  60. **    Parameters:
  61. **        ad -- the argument descriptor for this parameter.
  62. **        vp -- a pointer to the string input value.
  63. **        copyf -- if TRUE, the value will be destroyed later,
  64. **            and so should be copied if it will be retained
  65. **            (as for a string).
  66. **
  67. **    Returns:
  68. **        TRUE -- if the conversion was successful.  The actual
  69. **            value should be added to the list stored in the
  70. **            location indicated by ad->ad_valp.
  71. **        FALSE -- if the conversion failed.  The reason for failure
  72. **            should be diagnosed using usrerr().
  73. **
  74. **    Side Effects:
  75. **        The value should be stored through ad->ad_valp.
  76. */
  77.  
  78. BOOL
  79. listStr(ad, vp, copyf)
  80.     register ARGDESC *ad;
  81.     register char *vp;
  82.     BOOL copyf;
  83. {
  84.     char *cp;
  85.     struct arglist *nl;
  86.  
  87.     if (copyf)
  88.     {
  89.         register int i;
  90.  
  91.         i = strlen(vp) + 1;
  92.         cp = (char *) malloc(i);
  93.         if(!cp) {
  94.             usrerr("out of memory saving string %s", ad->ad_prompt);
  95.             return FALSE;
  96.         }
  97.         bcopy(vp, cp, i);
  98.     }
  99.     else
  100.     {
  101.         cp = vp;
  102.     }
  103.  
  104.     nl = (struct arglist *) malloc(sizeof *nl);
  105.     if(!nl) {
  106.         usrerr("out of memory saving arg %s", ad->ad_prompt);
  107.         if(copyf) free(cp);
  108.         return FALSE;
  109.     }
  110.  
  111.     nl->nl_next = *(struct arglist **) ad->ad_valp;
  112.     nl->nl_val = (ARBPTR)cp;
  113.     *(struct arglist **) ad->ad_valp = nl;
  114.     return (TRUE);
  115. }
  116.  
  117.