Changeset 3192 for trunk/src/kmk/kmkbuiltin/common-env-and-cwd-opt.c
- Timestamp:
- Mar 26, 2018, 10:25:56 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/common-env-and-cwd-opt.c
r3177 r3192 50 50 * 51 51 * @returns The duplicate enviornment. 52 * @param pCtx The built-in command context. 52 53 * @param papszEnv The read-only vector. 53 54 * @param cEnvVars The number of variables. … … 56 57 * @param cVerbosity The verbosity level. 57 58 */ 58 static char **kBuiltinOptEnvDuplicate(char **papszEnv, unsigned cEnvVars, unsigned *pcAllocatedEnvVars, int cVerbosity) 59 static char **kBuiltinOptEnvDuplicate(PKMKBUILTINCTX pCtx, char **papszEnv, unsigned cEnvVars, unsigned *pcAllocatedEnvVars, 60 int cVerbosity) 59 61 { 60 62 unsigned cAllocatedEnvVars = (cEnvVars + 2 + 0xf) & ~(unsigned)0xf; … … 72 74 free(papszEnvNew[i]); 73 75 free(papszEnvNew); 74 errx( 1, "out of memory for duplicating environment variables!", i);76 errx(pCtx, 1, "out of memory for duplicating environment variables!", i); 75 77 return NULL; 76 78 } … … 80 82 } 81 83 else 82 errx( 1, "out of memory for duplicating environment vector!");84 errx(pCtx, 1, "out of memory for duplicating environment vector!"); 83 85 return papszEnvNew; 84 86 } … … 90 92 * 91 93 * @returns 0 on success, non-zero exit code on error. 94 * @param pCtx The built-in command context. 92 95 * @param papszEnv The environment vector. 93 96 * @param pcEnvVars Pointer to the variable holding the number of … … 98 101 * @param pszValue The var=value string to apply. 99 102 */ 100 static int kBuiltinOptEnvAddVar( char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,103 static int kBuiltinOptEnvAddVar(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, 101 104 int cVerbosity, const char *pszValue) 102 105 { … … 109 112 papszEnv = (char **)realloc(papszEnv, *pcAllocatedEnvVars * sizeof(papszEnv[0])); 110 113 if (!papszEnv) 111 return errx( 1, "out of memory growing environment vector!");114 return errx(pCtx, 1, "out of memory growing environment vector!"); 112 115 *ppapszEnv = papszEnv; 113 116 } 114 117 papszEnv[cEnvVars] = strdup(pszValue); 115 118 if (!papszEnv[cEnvVars]) 116 return errx( 1, "out of memory adding environment variable!");119 return errx(pCtx, 1, "out of memory adding environment variable!"); 117 120 papszEnv[++cEnvVars] = NULL; 118 121 *pcEnvVars = cEnvVars; 119 122 if (cVerbosity > 0) 120 warnx( "added '%s'", papszEnv[cEnvVars - 1]);123 warnx(pCtx, "added '%s'", papszEnv[cEnvVars - 1]); 121 124 return 0; 122 125 } … … 128 131 * 129 132 * @returns 0 on success, non-zero exit code on error. 133 * @param pCtx The built-in command context. 130 134 * @param papszEnv The environment vector. 131 135 * @param cEnvVars Number of environment variables. … … 135 139 * @param iEnvVar Where to start searching after. 136 140 */ 137 static int kBuiltinOptEnvRemoveDuplicates( char **papszEnv, unsigned cEnvVars, int cVerbosity,141 static int kBuiltinOptEnvRemoveDuplicates(PKMKBUILTINCTX pCtx, char **papszEnv, unsigned cEnvVars, int cVerbosity, 138 142 const char *pszValue, size_t cchVar, unsigned iEnvVar) 139 143 { … … 143 147 { 144 148 if (cVerbosity > 0) 145 warnx( "removing duplicate '%s'", papszEnv[iEnvVar]);149 warnx(pCtx, "removing duplicate '%s'", papszEnv[iEnvVar]); 146 150 free(papszEnv[iEnvVar]); 147 151 cEnvVars--; … … 159 163 * 160 164 * @returns 0 on success, non-zero exit code on error. 165 * @param pCtx The built-in command context. 161 166 * @param ppapszEnv The environment vector pointer. 162 167 * @param pcEnvVars Pointer to the variable holding the number of … … 167 172 * @param pszValue The var=value string to apply. 168 173 */ 169 int kBuiltinOptEnvSet(char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, int cVerbosity, const char *pszValue) 174 int kBuiltinOptEnvSet(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, 175 int cVerbosity, const char *pszValue) 170 176 { 171 177 const char *pszEqual = strchr(pszValue, '='); … … 179 185 if (!*pcAllocatedEnvVars) 180 186 { 181 papszEnv = kBuiltinOptEnvDuplicate(p apszEnv, cEnvVars, pcAllocatedEnvVars, cVerbosity);187 papszEnv = kBuiltinOptEnvDuplicate(pCtx, papszEnv, cEnvVars, pcAllocatedEnvVars, cVerbosity); 182 188 if (!papszEnv) 183 return errx( 1, "out of memory duplicating enviornment (setenv)!");189 return errx(pCtx, 1, "out of memory duplicating enviornment (setenv)!"); 184 190 *ppapszEnv = papszEnv; 185 191 } … … 192 198 { 193 199 if (cVerbosity > 0) 194 warnx( "replacing '%s' with '%s'", papszEnv[iEnvVar], pszValue);200 warnx(pCtx, "replacing '%s' with '%s'", papszEnv[iEnvVar], pszValue); 195 201 free(papszEnv[iEnvVar]); 196 202 papszEnv[iEnvVar] = strdup(pszValue); 197 203 if (!papszEnv[iEnvVar]) 198 return errx( 1, "out of memory for modified environment variable!");199 200 return kBuiltinOptEnvRemoveDuplicates(p apszEnv, cEnvVars, cVerbosity, pszValue, cchVar, iEnvVar);204 return errx(pCtx, 1, "out of memory for modified environment variable!"); 205 206 return kBuiltinOptEnvRemoveDuplicates(pCtx, papszEnv, cEnvVars, cVerbosity, pszValue, cchVar, iEnvVar); 201 207 } 202 208 } 203 return kBuiltinOptEnvAddVar(p papszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue);204 } 205 return errx( 1, "Missing '=': -E %s", pszValue);209 return kBuiltinOptEnvAddVar(pCtx, ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue); 210 } 211 return errx(pCtx, 1, "Missing '=': -E %s", pszValue); 206 212 } 207 213 … … 211 217 * 212 218 * @returns 0 on success, non-zero exit code on error. 219 * @param pCtx The built-in command context. 213 220 * @param ppapszEnv The environment vector pointer. 214 221 * @param pcEnvVars Pointer to the variable holding the number of … … 219 226 * @param pszValue The var=value string to apply. 220 227 */ 221 static int kBuiltinOptEnvAppendPrepend( char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,228 static int kBuiltinOptEnvAppendPrepend(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, 222 229 int cVerbosity, const char *pszValue, int fAppend) 223 230 { … … 232 239 if (!*pcAllocatedEnvVars) 233 240 { 234 papszEnv = kBuiltinOptEnvDuplicate(p apszEnv, cEnvVars, pcAllocatedEnvVars, cVerbosity);241 papszEnv = kBuiltinOptEnvDuplicate(pCtx, papszEnv, cEnvVars, pcAllocatedEnvVars, cVerbosity); 235 242 if (!papszEnv) 236 return errx( 1, "out of memory duplicating environment (append)!");243 return errx(pCtx, 1, "out of memory duplicating environment (append)!"); 237 244 *ppapszEnv = papszEnv; 238 245 } … … 248 255 char *pszNew = malloc(cchVar + 1 + cchOldValue + cchNewValue + 1); 249 256 if (!pszNew) 250 return errx( 1, "out of memory appending to environment variable!");257 return errx(pCtx, 1, "out of memory appending to environment variable!"); 251 258 if (fAppend) 252 259 { … … 262 269 263 270 if (cVerbosity > 0) 264 warnx( "replacing '%s' with '%s'", pszCur, pszNew);271 warnx(pCtx, "replacing '%s' with '%s'", pszCur, pszNew); 265 272 free(pszCur); 266 273 papszEnv[iEnvVar] = pszNew; 267 274 268 return kBuiltinOptEnvRemoveDuplicates(p apszEnv, cEnvVars, cVerbosity, pszValue, cchVar, iEnvVar);275 return kBuiltinOptEnvRemoveDuplicates(pCtx, papszEnv, cEnvVars, cVerbosity, pszValue, cchVar, iEnvVar); 269 276 } 270 277 } 271 return kBuiltinOptEnvAddVar(p papszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue);272 } 273 return errx( 1, "Missing '=': -E %s", pszValue);278 return kBuiltinOptEnvAddVar(pCtx, ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue); 279 } 280 return errx(pCtx, 1, "Missing '=': -E %s", pszValue); 274 281 } 275 282 … … 279 286 * 280 287 * @returns 0 on success, non-zero exit code on error. 288 * @param pCtx The built-in command context. 281 289 * @param ppapszEnv The environment vector pointer. 282 290 * @param pcEnvVars Pointer to the variable holding the number of … … 287 295 * @param pszValue The var=value string to apply. 288 296 */ 289 int kBuiltinOptEnvAppend(char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, int cVerbosity, const char *pszValue) 290 { 291 return kBuiltinOptEnvAppendPrepend(ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue, 1 /*fAppend*/); 297 int kBuiltinOptEnvAppend(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, 298 int cVerbosity, const char *pszValue) 299 { 300 return kBuiltinOptEnvAppendPrepend(pCtx, ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue, 1 /*fAppend*/); 292 301 } 293 302 … … 297 306 * 298 307 * @returns 0 on success, non-zero exit code on error. 308 * @param pCtx The built-in command context. 299 309 * @param ppapszEnv The environment vector pointer. 300 310 * @param pcEnvVars Pointer to the variable holding the number of … … 305 315 * @param pszValue The var=value string to apply. 306 316 */ 307 int kBuiltinOptEnvPrepend(char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, int cVerbosity, const char *pszValue) 308 { 309 return kBuiltinOptEnvAppendPrepend(ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue, 0 /*fAppend*/); 317 int kBuiltinOptEnvPrepend(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, 318 int cVerbosity, const char *pszValue) 319 { 320 return kBuiltinOptEnvAppendPrepend(pCtx, ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue, 0 /*fAppend*/); 310 321 } 311 322 … … 315 326 * 316 327 * @returns 0 on success, non-zero exit code on error. 328 * @param pCtx The built-in command context. 317 329 * @param ppapszEnv The environment vector pointer. 318 330 * @param pcEnvVars Pointer to the variable holding the number of … … 324 336 * @param pszVarToRemove The name of the variable to remove. 325 337 */ 326 int kBuiltinOptEnvUnset(char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, int cVerbosity, const char *pszVarToRemove) 338 int kBuiltinOptEnvUnset(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, 339 int cVerbosity, const char *pszVarToRemove) 327 340 { 328 341 if (strchr(pszVarToRemove, '=') == NULL) … … 339 352 { 340 353 if (cVerbosity > 0) 341 warnx( !cRemoved ? "removing '%s'" : "removing duplicate '%s'", papszEnv[iEnvVar]);354 warnx(pCtx, !cRemoved ? "removing '%s'" : "removing duplicate '%s'", papszEnv[iEnvVar]); 342 355 343 356 if (!*pcAllocatedEnvVars) 344 357 { 345 papszEnv = kBuiltinOptEnvDuplicate(p apszEnv, cEnvVars, pcAllocatedEnvVars, cVerbosity);358 papszEnv = kBuiltinOptEnvDuplicate(pCtx, papszEnv, cEnvVars, pcAllocatedEnvVars, cVerbosity); 346 359 if (!papszEnv) 347 return errx( 1, "out of memory duplicating environment (unset)!");360 return errx(pCtx, 1, "out of memory duplicating environment (unset)!"); 348 361 *ppapszEnv = papszEnv; 349 362 } … … 360 373 361 374 if (cVerbosity > 0 && !cRemoved) 362 warnx( "not found '%s'", pszVarToRemove);375 warnx(pCtx, "not found '%s'", pszVarToRemove); 363 376 } 364 377 else 365 return errx( 1, "Found invalid variable name character '=' in: -U %s", pszVarToRemove);378 return errx(pCtx, 1, "Found invalid variable name character '=' in: -U %s", pszVarToRemove); 366 379 return 0; 367 380 } … … 372 385 * 373 386 * @returns 0 on success, non-zero exit code on error. 387 * @param pCtx The built-in command context. 374 388 * @param ppapszEnv The environment vector pointer. 375 389 * @param pcEnvVars Pointer to the variable holding the number of … … 380 394 * @param cVerbosity The verbosity level. 381 395 */ 382 int kBuiltinOptEnvZap( char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, int cVerbosity)396 int kBuiltinOptEnvZap(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, int cVerbosity) 383 397 { 384 398 if (*pcAllocatedEnvVars > 0) … … 396 410 char **papszEnv = calloc(4, sizeof(char *)); 397 411 if (!papszEnv) 398 return err( 1, "out of memory!");412 return err(pCtx, 1, "out of memory!"); 399 413 *ppapszEnv = papszEnv; 400 414 *pcAllocatedEnvVars = 4; … … 435 449 * 436 450 * @returns 0 on success, non-zero exit code on error. 451 * @param pCtx The built-in command context. 437 452 * @param pszCwd The CWD buffer. Contains current CWD on input, 438 453 * modified by @a pszValue on output. … … 440 455 * @param pszValue The --chdir value to apply. 441 456 */ 442 int kBuiltinOptChDir( char *pszCwd, size_t cbCwdBuf, const char *pszValue)457 int kBuiltinOptChDir(PKMKBUILTINCTX pCtx, char *pszCwd, size_t cbCwdBuf, const char *pszValue) 443 458 { 444 459 size_t cchNewCwd = strlen(pszValue); … … 454 469 offDst = 2; /* Take drive letter from CWD. */ 455 470 else 456 return errx( 1, "UNC relative CWD not implemented: cur='%s' new='%s'", pszCwd, pszValue);471 return errx(pCtx, 1, "UNC relative CWD not implemented: cur='%s' new='%s'", pszCwd, pszValue); 457 472 } 458 473 else if ( pszValue[1] == ':' … … 473 488 int iDrive = tolower(pszValue[0]) - 'a' + 1; 474 489 if (!_getdcwd(iDrive, pszCwd, cbCwdBuf)) 475 return err( 1, "_getdcwd(%d,,) failed", iDrive);490 return err(pCtx, 1, "_getdcwd(%d,,) failed", iDrive); 476 491 pszValue += 2; 477 492 cchNewCwd -= 2; … … 493 508 pszCwd[offDst++] = '/'; 494 509 if (offDst + cchNewCwd >= cbCwdBuf) 495 return errx( 1, "Too long CWD: %*.*s%s", offDst, offDst, pszCwd, pszValue);510 return errx(pCtx, 1, "Too long CWD: %*.*s%s", offDst, offDst, pszCwd, pszValue); 496 511 memcpy(&pszCwd[offDst], pszValue, cchNewCwd + 1); 497 512 }
Note:
See TracChangeset
for help on using the changeset viewer.