/* The main program. */ int main (int argc, char **argv) { identify_program(argc, argv); process_options(argc, argv); process_parameters(argc, argv); assign_defaults(); compose_tunes(); monitor_locks(foreground_task, poll_interval); }
/*============================================================================== ROUTINE upstst_pass1 - Do a first pass through argv[], weeding out all known switches specified in argTable[]. CALL: (static int) upstst_pass1 (int *argcPtr, char **argv, upstst_argt *argTable) argcPtr - Pointer to the address of argument count argv - Command line argument array argTable - Table of known arguments DESCRIPTION: upstst_pass1() performs a first pass through the command line arguments specified in argv[]. As it goes thru it's motions, it strips out all known arguments. Known arguments are specified in argTable[]. As an argument is recognized and processed, it is removed from argv[]. *argcPtr will be decremented to account for this. Only switches are weeded out in this pass. Positional parameters are searched for in the next pass. Example - Consider the following command line argument: myCommand reg1 20 -debugLevel 3 -useMalloc -delay 10 -o Also consider that argTable[] knows about the following arguments <reg1> - Positional parameter [<numCols>] - Optional positional parameter -debugLevel - Switch that takes an argument -useMalloc - Switch that takes no argument After a call to this function, argv[] will look like: argv[0] ---> myCommand argv[1] ---> reg1 argv[2] ---> 20 argv[3] ---> -delay argv[4] ---> 10 argv[5] ---> -o argcPtr will have been modified to contain 6. RETURNS: UPSTST_SUCCESS : if command-line options parsed successfully UPSTST_BADSYNTAX : on error. Reason for error is put in interp->result ============================================================================= */ static int upstst_pass1(int * const argcPtr, char ** const argv, upstst_argt * const argTable, const unsigned int options) { upstst_argt *infoPtr; /* Pointer to the current entry in the * table of argument descriptions. */ upstst_argt *matchPtr; /* Descriptor that matches current argument. */ char *curArg; /* Current argument */ int srcIndex; /* Location from which to read next argument from argv*/ int dstIndex; /* Index into argv to which next unused * argument should be copied (never greater * than srcIndex). */ size_t length; /* Number of characters in current argument. */ char tmpKeyBuf[30]; #define upstst_missing_arg(myarg) { \ (void) fprintf(stderr,"Syntax Error: %s option requires an additional argument\n",\ myarg); \ return UPSTST_BADSYNTAX; \ } if (assign_defaults(argTable) == 0) return UPSTST_BADSYNTAX; /* * Do the real parsing, skipping over first arg (ie command) */ for (srcIndex = dstIndex = 1; srcIndex < *argcPtr; ) { curArg = argv[srcIndex]; srcIndex++; length = strlen(curArg); if (length == 0) { /* * Someone passed a NULL string. Just move on. */ argv[dstIndex] = curArg; dstIndex++; continue; } /* * Loop throught the argument descriptors searching for one with * the matching key string. If found, leave a pointer to it in * matchPtr. */ if (!strcmp(curArg,"-usage")) return UPSTST_USAGE; matchPtr = NULL; for (infoPtr = argTable; infoPtr->type != UPSTST_ARGV_END; infoPtr++) { if (infoPtr->key == NULL) continue; if (options && UPSTST_PARSE_EXACTMATCH) { if (strcmp(infoPtr->key, curArg)) continue; } else { if (strncmp(infoPtr->key, curArg, length)) continue; } if (infoPtr->key[length] == 0) { matchPtr = infoPtr; goto gotMatch; } if (matchPtr != NULL) { (void) fprintf (stderr, "Syntax Error: ambiguous option %s ", curArg); return UPSTST_BADSYNTAX; } matchPtr = infoPtr; } if (matchPtr == NULL) { /* * Unrecognized argument. Just copy it down */ argv[dstIndex] = curArg; dstIndex++; continue; } /* * Take the appropriate action based on the option type */ gotMatch: infoPtr = matchPtr; switch (infoPtr->type) { case UPSTST_ARGV_CONSTANT: *((int *) infoPtr->dst) = (int) infoPtr->src; (void) sprintf(tmpKeyBuf, "%s,", infoPtr->key); (void) strcat(g_Specified_args, tmpKeyBuf); break; case UPSTST_ARGV_INT: { char *endPtr; if (srcIndex == *argcPtr) upstst_missing_arg(curArg); *((int *) infoPtr->dst) = (int) strtoul(argv[srcIndex], &endPtr, 0); if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) { (void) fprintf (stderr,"Syntax Error: expected integer argument " "for %s but got %s \n", infoPtr->key,argv[srcIndex]); return UPSTST_BADSYNTAX; } srcIndex++; (void) sprintf(tmpKeyBuf, "%s,", infoPtr->key); (void) strcat(g_Specified_args, tmpKeyBuf); break; } case UPSTST_ARGV_STRING: if (srcIndex == *argcPtr) upstst_missing_arg(curArg); *((char **)infoPtr->dst) = argv[srcIndex]; srcIndex++; (void) sprintf(tmpKeyBuf, "%s,", infoPtr->key); (void) strcat(g_Specified_args, tmpKeyBuf); break; case UPSTST_ARGV_DOUBLE : { char *endPtr; if (srcIndex == *argcPtr) upstst_missing_arg(curArg); *((double *) infoPtr->dst) = strtod(argv[srcIndex], &endPtr); if ((endPtr == argv[srcIndex]) || (*endPtr != 0)) { (void) fprintf(stderr,"Syntax Error: expected floating-point argument "); (void) fprintf(stderr," for %s but got %s\n",infoPtr->key, argv[srcIndex]); return UPSTST_BADSYNTAX; } srcIndex++; (void) sprintf(tmpKeyBuf, "%s,", infoPtr->key); (void) strcat(g_Specified_args, tmpKeyBuf); break; } default: (void) fprintf(stderr,"Programmer Error: bad argument type %d in arg table", infoPtr->type); return UPSTST_BADSYNTAX; } } /* * Copy the remaining arguments down. */ for (;srcIndex < *argcPtr; argv[dstIndex++] = argv[srcIndex++]); argv[dstIndex] = (char *) NULL; *argcPtr = dstIndex; return UPSTST_SUCCESS; }
SLPInternalError reset_slpd( int argc, char *pcArgv[], struct sockaddr_in *psin, SAState* psa ) { const char * pcFile; #ifdef ENABLE_SLP_LOGGING SLP_LOG( SLP_LOG_STATE, "*** slpd reset ***" ); #endif #ifdef EXTRA_MSGS if ((psa->pvMutex = SDGetMutex(MSLP_SERVER)) == NULL) { LOG_SLP_ERROR_AND_RETURN(SLP_LOG_ERR,"slpd: could not init mutex",SLP_INTERNAL_SYSTEM_ERROR); } global_resources.pvMutex = psa->pvMutex; // to free on exit! SDLock(psa->pvMutex); #endif if ( argc > 4 || (argc > 1 && pcArgv[1][0] == '?') ) { fprintf( stderr, "Usage: %s [<filename>] [-f <config-file>]\n" " <filename> is a serialized registration file. (optional)\n" " <config-file> is a configuration file. (optional)\n", pcArgv[0] ); return SLP_PARSE_ERROR; } if ( assign_defaults( psa, psin, argc, pcArgv ) != SLP_OK ) { LOG_SLP_ERROR_AND_RETURN(SLP_LOG_ERR,"Could not assign defaults - abort",SLP_INTERNAL_SYSTEM_ERROR); return SLP_INTERNAL_SYSTEM_ERROR; } InitializeSLPdSystemConfigurator(); DeleteRegFileIfFirstStartupSinceBoot(); /* * process the registration file, indicated on the command line or param */ if ((pcFile = SLPGetProperty("net.slp.serializedRegURL")) != NULL) { pcFile = &pcFile[strlen("file:")]; /* skip past the 'file:' scheme */ } if (pcFile) { if (process_regfile(&(psa->store), pcFile) < 0) { SLP_LOG(SLP_LOG_ERR,"mslpd: could not process regfile",SLP_INTERNAL_SYSTEM_ERROR); exit(-2); } } #ifdef EXTRA_MSGS /* * Copy the value of the registration file to file used by both * mslpd and all services which advertise themselves using libslp. */ #ifdef ENABLE_SLP_LOGGING if ( SLPGetProperty("com.sun.slp.regfile") ) SLP_LOG(SLP_LOG_DEBUG, "reset_slpd, handling regfile: %s", SLPGetProperty("com.sun.slp.regfile")); else SLP_LOG(SLP_LOG_DEBUG, "reset_slpd, regfile property is NULL!"); #endif if ( pcFile ) { fcopy(pcFile,SLPGetProperty("com.sun.slp.regfile")); } else { FILE *fp = fopen(SLPGetProperty("com.sun.slp.regfile"),"r+"); if (!fp) fp = fopen(SLPGetProperty("com.sun.slp.regfile"),"w+"); // the file may not exist, so create it if (!fp) { SLP_LOG(SLP_LOG_ERR,"mslpd: could not initialize regfile %s",strerror(errno)); return SLP_PREFERENCES_ERROR; } else { if ( getc(fp) == EOF ) // is this a new file? { if (fprintf(fp,"# initial reg file automatically generated\n") < 0) { fclose(fp); SLP_LOG(SLP_LOG_ERR,"mslpd: could not write to regfile %s",strerror(errno)); return SLP_PREFERENCES_ERROR; } if (SDchmod_writable(SLPGetProperty("com.sun.slp.regfile")) < 0) { fclose(fp); SLP_LOG(SLP_LOG_ERR,"mslpd: could not change file permissions",strerror(errno)); return SLP_PREFERENCES_ERROR; } } else if (process_regfile(&(psa->store), SLPGetProperty("com.sun.slp.regfile")) < 0) // This file was already there, we should process it { fclose(fp); LOG_SLP_ERROR_AND_RETURN(SLP_LOG_ERR,"mslpd: could not process regfile",SLP_INTERNAL_SYSTEM_ERROR); } if (fclose(fp) < 0) { SLP_LOG(SLP_LOG_ERR,"mslpd: could not close reg file",strerror(errno)); return SLP_PREFERENCES_ERROR; } } } SDUnlock(psa->pvMutex); #endif /* EXTRA_MSGS */ #ifdef ENABLE_SLP_LOGGING SLP_LOG( SLP_LOG_DEBUG, "reset_slpd finished" ); #endif return SLP_OK; }