bool ParseCmdLine(int argc, char *argv[], int& r) { for (int a=1; a<argc; a++) { if (!STRCASECMP(argv[a], "-run")) { if (a == argc-1) { PrintCmdLine (); return false; } const char *scriptFile = argv[a+1]; //r = RunScript (binder, scriptFile); return false; } } return true; }
INT32 FindPid(char *szCmdLine, pid_t *aPid, UINT32 u32SizPid) { DIR *dirp; struct dirent *dp; char *sp; char szPidString[MAX_PID_STRING]; char szUserCmdLine[MAX_CMD_LINE]; char szProcCmdLine[MAX_CMD_LINE]; BOOL bNumeric; INT32 i32ProcCmdLen = 0; INT32 i32UserCmdLen = 0; UINT32 u32NumPid = 0; FILE *fp; #ifdef TEST_PROCESS_FUNCTIONS int n; #endif // Convert the command line i32UserCmdLen = ConvertCmdLine(szCmdLine, szUserCmdLine, MAX_CMD_LINE); #ifdef TEST_PROCESS_FUNCTIONS if (i32UserCmdLen < 0) printf("ERROR parsing command line \'%s\'\n", szCmdLine); else if (i32UserCmdLen == 0) printf("WARNING: empty command line \'%s\'\n", szCmdLine); else PrintCmdLine(szUserCmdLine, i32UserCmdLen); #endif if (i32UserCmdLen <= 0) return -1; // Open the /proc directory if ((dirp = opendir("/proc")) == NULL) { #ifdef TEST_PROCESS_FUNCTIONS printf("Can't open /proc\n"); #endif return -1; } // Search for matching PID entries while ((dp = readdir(dirp)) != NULL && dp->d_name != NULL) { // Process only numeric directory names bNumeric = TRUE; for (sp=dp->d_name; *sp != 0 && bNumeric; sp++) { if (!isdigit(*sp)) bNumeric = FALSE; } // Extract the command line for each PID // Compare it to the user command line // and build a list of matching PIDs if (bNumeric) { // Construct the file name strncpy(szPidString, "/proc/", MAX_PID_STRING); strncat(szPidString, dp->d_name, MAX_PID_STRING); strncat(szPidString, "/cmdline", MAX_PID_STRING); // Open and read the command line string from each file if ((fp = fopen(szPidString, "r")) != NULL) { i32ProcCmdLen = fread(szProcCmdLine, 1, MAX_CMD_LINE, fp); fclose(fp); if (i32ProcCmdLen == i32UserCmdLen && !memcmp(szProcCmdLine, szUserCmdLine, i32UserCmdLen)) { if (u32NumPid < u32SizPid) { aPid[u32NumPid] = (pid_t) atoi(dp->d_name); #ifdef TEST_PROCESS_FUNCTIONS printf("%d: ", (UINT32) aPid[u32NumPid]); PrintCmdLine(szProcCmdLine, i32ProcCmdLen); #endif u32NumPid++; } } } // if ((fp = fopen } // if (bNumeric } // while ((dp = readdir closedir(dirp); return u32NumPid; } // FindPid
void TOpts::PrintUsage(const TStringBuf& program, IOutputStream& osIn, const NColorizer::TColors& colors) const { TStringStream os; if (!Title.empty()) os << Title << "\n\n"; PrintCmdLine(program, os, colors); TVector<TString> leftColumn(Opts_.size()); TVector<size_t> leftColumnSizes(leftColumn.size()); size_t leftWidth = 0; size_t requiredOptionsCount = 0; NColorizer::TColors disabledColors(false); for (size_t i = 0; i < Opts_.size(); i++) { const TOpt* opt = Opts_[i].Get(); if (opt->IsHidden()) continue; leftColumn[i] = FormatOption(opt, colors); const size_t leftColumnSize = colors.IsTTY() ? FormatOption(opt, disabledColors).size() : leftColumn[i].size(); leftColumnSizes[i] = leftColumnSize; leftWidth = Max(leftWidth, leftColumnSize); if (opt->IsRequired()) requiredOptionsCount++; } const size_t kMaxLeftWidth = 25; leftWidth = Min(leftWidth, kMaxLeftWidth); const TString maxPadding(kMaxLeftWidth, ' '); const TString leftPadding(leftWidth, ' '); for (size_t sectionId = 0; sectionId <= 1; sectionId++) { bool requiredOptionsSection = (sectionId == 0); if (requiredOptionsSection) { if (requiredOptionsCount == 0) continue; os << Endl << colors.BoldColor() << "Required parameters" << colors.OldColor() << ":" << Endl; } else { if (requiredOptionsCount == Opts_.size()) continue; if (requiredOptionsCount == 0) os << Endl << colors.BoldColor() << "Options" << colors.OldColor() << ":" << Endl; else os << Endl << colors.BoldColor() << "Optional parameters" << colors.OldColor() << ":" << Endl; // optional options would be a tautology } for (size_t i = 0; i < Opts_.size(); i++) { const TOpt* opt = Opts_[i].Get(); if (opt->IsHidden()) continue; if (opt->IsRequired() != requiredOptionsSection) continue; if (leftColumnSizes[i] > leftWidth && !opt->GetHelp().empty()) { os << SPad << leftColumn[i] << Endl << SPad << maxPadding << ' '; } else { os << SPad << leftColumn[i] << ' '; if (leftColumnSizes[i] < leftWidth) os << TStringBuf(~leftPadding, leftWidth - leftColumnSizes[i]); } bool multiLineHelp = false; if (!opt->GetHelp().empty()) { TVector<TStringBuf> helpLines; Split(opt->GetHelp(), "\n", helpLines); multiLineHelp = (helpLines.size() > 1); os << helpLines[0]; for (size_t j = 1; j < helpLines.size(); ++j) { if (helpLines[j].empty()) continue; os << Endl << SPad << leftPadding << ' ' << helpLines[j]; } } if (opt->HasDefaultValue()) { TString quotedDef = QuoteForHelp(opt->GetDefaultValue()); if (multiLineHelp) os << Endl << SPad << leftPadding << " Default: " << colors.CyanColor() << quotedDef << colors.OldColor(); else if (opt->GetHelp().empty()) os << "Default: " << colors.CyanColor() << quotedDef << colors.OldColor(); else os << " (default: " << colors.CyanColor() << quotedDef << colors.OldColor() << ")"; } os << Endl; } } PrintFreeArgsDesc(os, colors); osIn << os.Str(); }
void TOpts::PrintUsage(const TStringBuf& program, TOutputStream& os) const { if (!Title.empty()) os << Title << "\n\n"; PrintCmdLine(program, os); yvector<Stroka> leftColumn(Opts_.size()); size_t leftWidth = 0; size_t requiredOptionsCount = 0; for (size_t i = 0; i < Opts_.size(); i++) { const TOpt* opt = Opts_[i].Get(); if (opt->IsHidden()) continue; leftColumn[i] = FormatOption(opt); leftWidth = Max(leftWidth, leftColumn[i].size()); if (opt->IsRequired()) requiredOptionsCount++; } const size_t kMaxLeftWidth = 25; leftWidth = Min(leftWidth, kMaxLeftWidth); const Stroka maxPadding(kMaxLeftWidth, ' '); const Stroka leftPadding(leftWidth, ' '); for (size_t sectionId = 0; sectionId <= 1; sectionId++) { bool requiredOptionsSection = (sectionId == 0); if (requiredOptionsSection) { if (requiredOptionsCount == 0) continue; os << Endl << "Required parameters:" << Endl; } else { if (requiredOptionsCount == Opts_.size()) continue; if (requiredOptionsCount == 0) os << Endl << "Options:" << Endl; else os << Endl << "Optional parameters:" << Endl; // optional options would be a tautology } for (size_t i = 0; i < Opts_.size(); i++) { const TOpt* opt = Opts_[i].Get(); if (opt->IsHidden()) continue; if (opt->IsRequired() != requiredOptionsSection) continue; if (leftColumn[i].size() > leftWidth && !opt->Help_.empty()) os << SPad << leftColumn[i] << Endl << SPad << maxPadding << ' '; else os << SPad << RightPad(leftColumn[i], leftWidth, ' ') << ' '; bool multiLineHelp = false; if (!opt->Help_.empty()) { yvector<TStringBuf> helpLines; Split(opt->Help_, "\n", helpLines); multiLineHelp = (helpLines.size() > 1); os << helpLines[0]; for (size_t j = 1; j < helpLines.size(); ++j) { if (helpLines[j].empty()) continue; os << Endl << SPad << leftPadding << ' ' << helpLines[j]; } } if (opt->HasDefaultValue()) { Stroka quotedDef = QuoteForHelp(opt->GetDefaultValue()); if (multiLineHelp) os << Endl << SPad << leftPadding << " Default: " << quotedDef; else if (opt->Help_.empty()) os << "Default: " << quotedDef; else os << " (default: " << quotedDef << ")"; } os << Endl; } } PrintFreeArgsDesc(os); }