static void ParseArguments(int argc, char **argv) // Parses our command line arguments into the global variables // listed above. { int ch; // Set gProgramName to the last path component of argv[0] gProgramName = strrchr(argv[0], '/'); if (gProgramName == NULL) { gProgramName = argv[0]; } else { gProgramName += 1; } // Parse command line options using getopt. do { ch = getopt(argc, argv, "v:t:d:"); if (ch != -1) { switch (ch) { case 'v': gMDNSPlatformPosixVerboseLevel = atoi(optarg); if (gMDNSPlatformPosixVerboseLevel < 0 || gMDNSPlatformPosixVerboseLevel > 2) { fprintf(stderr, "%s: Verbose mode must be in the range 0..2\n", gProgramName); exit(1); } break; case 't': gServiceType = optarg; if ( ! CheckThatServiceTypeIsUsable(gServiceType, mDNStrue) ) { exit(1); } break; case 'd': gServiceDomain = optarg; break; case '?': default: PrintUsage(); exit(1); break; } } } while (ch != -1); // Check for any left over command line arguments. if (optind != argc) { fprintf(stderr, "%s: Unexpected argument '%s'\n", gProgramName, argv[optind]); exit(1); } }
static mStatus RegisterServicesInFile(const char *filePath) { mStatus status = mStatus_NoError; FILE * fp = fopen(filePath, "r"); int junk; if (fp == NULL) { status = mStatus_UnknownErr; } if (status == mStatus_NoError) { mDNSBool good = mDNStrue; do { int ch; char name[256]; char type[256]; const char *dom = kDefaultServiceDomain; char rawText[1024]; mDNSu8 text[sizeof(RDataBody)]; unsigned int textLen = 0; char port[256]; // Skip over any blank lines. do ch = fgetc(fp); while ( ch == '\n' || ch == '\r' ); if (ch != EOF) good = (ungetc(ch, fp) == ch); // Read three lines, check them for validity, and register the service. good = ReadALine(name, sizeof(name), fp); if (good) { good = ReadALine(type, sizeof(type), fp); } if (good) { char *p = type; while (*p && *p != ' ') p++; if (*p) { *p = 0; dom = p+1; } } if (good) { good = ReadALine(port, sizeof(port), fp); } if (good) { good = CheckThatRichTextNameIsUsable(name, mDNSfalse) && CheckThatServiceTypeIsUsable(type, mDNSfalse) && CheckThatPortNumberIsUsable(atol(port), mDNSfalse); } if (good) { while (1) { int len; if (!ReadALine(rawText, sizeof(rawText), fp)) break; len = strlen(rawText); if (len <= 255) { unsigned int newlen = textLen + 1 + len; if (len == 0 || newlen >= sizeof(text)) break; text[textLen] = len; memcpy(text + textLen + 1, rawText, len); textLen = newlen; } else fprintf(stderr, "%s: TXT attribute too long for name = %s, type = %s, port = %s\n", gProgramName, name, type, port); } } if (good) { status = RegisterOneService(name, type, dom, text, textLen, atol(port)); if (status != mStatus_NoError) { fprintf(stderr, "%s: Failed to register service, name = %s, type = %s, port = %s\n", gProgramName, name, type, port); status = mStatus_NoError; // keep reading } } } while (good && !feof(fp)); if ( ! good ) { fprintf(stderr, "%s: Error reading service file %s\n", gProgramName, filePath); } } if (fp != NULL) { junk = fclose(fp); assert(junk == 0); } return status; }
static void ParseArguments(int argc, char **argv) // Parses our command line arguments into the global variables // listed above. { int ch; // Set gProgramName to the last path component of argv[0] gProgramName = strrchr(argv[0], '/'); if (gProgramName == NULL) { gProgramName = argv[0]; } else { gProgramName += 1; } // Parse command line options using getopt. do { ch = getopt(argc, argv, "v:rn:t:d:p:f:dP:bx"); if (ch != -1) { switch (ch) { case 'v': gMDNSPlatformPosixVerboseLevel = atoi(optarg); if (gMDNSPlatformPosixVerboseLevel < 0 || gMDNSPlatformPosixVerboseLevel > 2) { fprintf(stderr, "%s: Verbose mode must be in the range 0..2\n", gProgramName); exit(1); } break; case 'r': gAvoidPort53 = mDNSfalse; break; case 'n': gServiceName = optarg; if ( ! CheckThatRichTextNameIsUsable(gServiceName, mDNStrue) ) { exit(1); } break; case 't': gServiceType = optarg; if ( ! CheckThatServiceTypeIsUsable(gServiceType, mDNStrue) ) { exit(1); } break; case 'd': gServiceDomain = optarg; break; case 'p': gPortNumber = atol(optarg); if ( ! CheckThatPortNumberIsUsable(gPortNumber, mDNStrue) ) { exit(1); } break; case 'f': gServiceFile = optarg; break; case 'b': gDaemon = mDNStrue; break; case 'P': gPIDFile = optarg; break; case 'x': while (optind < argc) { gServiceText[gServiceTextLen] = strlen(argv[optind]); memcpy(gServiceText+gServiceTextLen+1, argv[optind], gServiceText[gServiceTextLen]); gServiceTextLen += 1 + gServiceText[gServiceTextLen]; optind++; } ch = -1; break; case '?': default: PrintUsage(); exit(1); break; } } } while (ch != -1); // Check for any left over command line arguments. if (optind != argc) { PrintUsage(); fprintf(stderr, "%s: Unexpected argument '%s'\n", gProgramName, argv[optind]); exit(1); } // Check for inconsistency between the arguments. if ( (gServiceName[0] == 0) && (gServiceFile[0] == 0) ) { PrintUsage(); fprintf(stderr, "%s: You must specify a service name to register (-n) or a service file (-f).\n", gProgramName); exit(1); } }