int decode_state( pbs_attribute *pattr, /* I (modified) */ char *name, /* pbs_attribute name */ char *rescn, /* resource name, unused here */ char *val, /* pbs_attribute value */ int perm) /* only used for resources */ { int rc = 0; /*return code; 0==success*/ short flag, currflag; char *str; char strbuf[512]; /*should handle most vals*/ char *sbufp; char *ptr = NULL; int slen; if (val == NULL) { return(PBSE_BADNDATVAL); } /* * determine string storage requirement and copy the string "val" * to a work buffer area */ slen = strlen(val); /*bufr either on stack or heap*/ if (slen - 512 < 0) { sbufp = strbuf; } else { if (!(sbufp = (char *)calloc(1, slen + 1))) { return(PBSE_SYSTEM); } } strcpy(sbufp, val); if ((str = parse_comma_string(sbufp,&ptr)) == NULL) { if (slen >= 512) free(sbufp); return(rc); } flag = 0; if ((rc = set_nodeflag(str, &flag)) != 0) { if (slen >= 512) free(sbufp); return(rc); } currflag = flag; /*calling parse_comma_string with a null ptr continues where*/ /*last call left off. The initial comma separated string */ /*copy pointed to by sbufp is modified with each func call */ while ((str = parse_comma_string(NULL,&ptr)) != NULL) { if ((rc = set_nodeflag(str, &flag)) != 0) break; if (((currflag == 0) && flag) || (currflag && (flag == 0))) { rc = PBSE_MUTUALEX; /*free is mutually exclusive*/ break; } currflag = flag; } if (!rc) { pattr->at_val.at_short = flag; pattr->at_flags |= ATR_VFLAG_SET | ATR_VFLAG_MODIFY; } if (slen >= 512) /* buffer on heap, not stack */ free(sbufp); return(rc); } /* END decode_state() */
/** * @brief * decode a comma string into an attribute of type ATR_TYPE_ARST * * @par Functionality: * 1. Call count_substrings to find out the number of sub strings separated by comma * 2. Call parse_comma_string function to parse the value of the attribute * * @see * * @param[in/out] patr - Pointer to attribute structure * @param[in] val - Value of the attribute as comma separated string. This parameter's value * cannot be modified by any of the functions that are called inside this function. * * @return int * @retval 0 - Success * @retval >0 - Failure * * */ static int decode_arst_direct(struct attribute *patr, char *val) { unsigned long bksize; int j; int ns; char *pbuf = NULL; char *pc; char *pstr; struct array_strings *stp = NULL; int rc; char strbuf[BUF_SIZE]; /* Should handle most values */ char *sbufp = NULL; size_t slen; if (!patr || !val) return (PBSE_INTERNAL); /* * determine number of sub strings, each sub string is terminated * by a non-escaped comma or a new-line, the whole string is terminated * by a null */ if ((rc = count_substrings(val, &ns)) != 0) return (rc); slen = strlen(val); pbuf = (char *)malloc(slen + 1); if (pbuf == (char *)0) return (PBSE_SYSTEM); bksize = ((ns-1) * sizeof(char *)) + sizeof(struct array_strings); stp = (struct array_strings *)malloc(bksize); if (!stp) { free(pbuf); return (PBSE_SYSTEM); } /* number of slots (sub strings) */ stp->as_npointers = ns; stp->as_usedptr = 0; /* for the strings themselves */ stp->as_buf = pbuf; stp->as_next = pbuf; stp->as_bufsize = slen + 1; /* * determine string storage requirement and copy the string "val" * to a work buffer area */ if (slen < BUF_SIZE) /* buffer on stack */ sbufp = strbuf; else { /* buffer on heap */ if ((sbufp = (char *)malloc(slen + 1)) == NULL) { free(pbuf); free(stp); return (PBSE_SYSTEM); } } strncpy(sbufp, val, slen); sbufp[slen] = '\0'; /* now copy in substrings and set pointers */ pc = pbuf; j = 0; pstr = parse_comma_string(sbufp); while ((pstr != (char *)0) && (j < ns)) { stp->as_string[j] = pc; while (*pstr) { *pc++ = *pstr++; } *pc++ = '\0'; pstr = parse_comma_string((char *)0); j++; } stp->as_usedptr = j; stp->as_next = pc; patr->at_flags |= ATR_VFLAG_SET | ATR_VFLAG_MODIFY | ATR_VFLAG_MODCACHE; patr->at_val.at_arst = stp; if (sbufp != strbuf) /* buffer on heap, not stack */ free(sbufp); return (0); }
int decode_arst_direct( pbs_attribute *patr, /* I (modified) */ char *val) /* I */ { unsigned long bksize; int j; int ns; char *pbuf = NULL; char *pc = NULL; char *pstr = NULL; int ssize; char *tmpval = NULL; char *tmp = NULL; struct array_strings *stp = NULL; /* * determine number of sub strings, each sub string is terminated * by a non-escaped comma or a new-line, the whole string is terminated * by a null */ ns = 1; ssize = strlen(val) + 1; if ((tmpval =(char *) calloc(1, ssize)) == NULL) { /* FAILURE */ return(PBSE_SYSTEM); } strcpy(tmpval,val); for (pc = tmpval;*pc;pc++) { if (*pc == '\\') { if (*++pc == '\0') break; } else if ((*pc == ',') || (*pc == '\n')) { ++ns; } } pc--; if (((*pc == '\n') || (*pc == ',')) && (*(pc - 1) != '\\')) { ns--; /* strip any trailing null string */ *pc = '\0'; ssize--; } if ((pbuf = (char *)calloc(1, (unsigned)ssize)) == NULL) { /* FAILURE */ free(tmpval); return(PBSE_SYSTEM); } memset(pbuf, 0, ssize); bksize = (ns - 1) * sizeof(char *) + sizeof(struct array_strings); if ((stp = (struct array_strings *)calloc(1, bksize)) == NULL) { /* FAILURE */ free(tmpval); free(pbuf); return(PBSE_SYSTEM); } stp->as_npointers = ns; /* number of slots (sub strings) */ /* for the strings themselves */ stp->as_buf = pbuf; stp->as_next = pbuf; stp->as_bufsize = (unsigned int)ssize; /* now copy in substrings and set pointers */ pc = pbuf; j = 0; pstr = parse_comma_string(tmpval,&tmp); while ((pstr != NULL) && (j < ns)) { stp->as_string[j] = pc; while (*pstr) { if (*pstr == '\\') if (*++pstr == '\0') break; *pc++ = *pstr++; } *pc++ = '\0'; j++; pstr = parse_comma_string(NULL,&tmp); } /* END while ((pstr != NULL) && (j < ns)) */ stp->as_usedptr = j; stp->as_next = pc; patr->at_flags |= ATR_VFLAG_SET | ATR_VFLAG_MODIFY; patr->at_val.at_arst = stp; free(tmpval); /* SUCCESS */ return(0); } /* END decode_arst_direct() */
int decode_state(attribute *pattr, char *name, char *rescn, char *val) { int rc = 0; /*return code; 0==success*/ unsigned long flag, currflag; char *str; char strbuf[512]; /*should handle most vals*/ char *sbufp; int slen; if (val == NULL) return (PBSE_BADNDATVAL); /* * determine string storage requirement and copy the string "val" * to a work buffer area */ slen = strlen(val); /*bufr either on stack or heap*/ if (slen - 512 < 0) sbufp = strbuf; else { if (!(sbufp = (char *)malloc(slen + 1))) return (PBSE_SYSTEM); } strcpy(sbufp, val); if ((str = parse_comma_string(sbufp)) == NULL) { if (slen >= 512) free(sbufp); return rc; } flag = 0; if ((rc = set_nodeflag(str, &flag)) != 0) { if (slen >= 512) free(sbufp); return rc; } currflag = flag; /*calling parse_comma_string with a null ptr continues where*/ /*last call left off. The initial comma separated string */ /*copy pointed to by sbufp is modified with each func call */ while ((str = parse_comma_string(NULL)) != 0) { if ((rc = set_nodeflag(str, &flag)) != 0) break; if ((currflag == 0 && flag) || (currflag && flag == 0)) { rc = PBSE_MUTUALEX; /*free is mutually exclusive*/ break; } currflag = flag; } if (!rc) { pattr->at_val.at_long = flag; pattr->at_flags |= ATR_VFLAG_SET|ATR_VFLAG_MODIFY|ATR_VFLAG_MODCACHE; } if (slen >= 512) /*buffer on heap, not stack*/ free(sbufp); return rc; }