/* * Returns: * 0 Success * 1 Incorrect usage (i.e., command-line syntax error). * 2 No such parameter or node. Error message written. * 3 System error. Error message written. */ int main( int argc, char* argv[]) { int status; const char* const progname = basename(argv[0]); (void)log_init(progname); if ((status = sb_new(&_valuePath, 80))) { log_error("Couldn't initialize utility"); status = SYSTEM_ERROR; } else { enum { UNKNOWN, CREATE, PRINT, PUT_BOOL, PUT_STRING, PUT_UINT, PUT_SIGNATURE, PUT_TIME, RESET, REMOVE } action = UNKNOWN; const char* string; signaturet signature; timestampt timestamp; unsigned long uint; int boolean; int ch; int quiet = 0; opterr = 0; /* supress getopt(3) error messages */ while (0 == status && (ch = getopt(argc, argv, ":b:cd:h:qRrs:t:u:vx")) != -1) { switch (ch) { case 'b': { if (strcasecmp(optarg, "TRUE") == 0) { boolean = 1; } else if (strcasecmp(optarg, "FALSE") == 0) { boolean = 0; } else { log_add("Not a boolean value: \"%s\"", optarg); status = COMMAND_SYNTAX; } if (status == 0) { if (CREATE == action) { log_error("Create option ignored"); } action = PUT_BOOL; } break; } case 'c': { if (UNKNOWN != action) { log_add("Can't mix create action with other actions"); status = COMMAND_SYNTAX; } else { action = CREATE; } break; } case 'd': { if ((status = reg_setDirectory(optarg))) status = SYSTEM_ERROR; break; } case 'h': { status = sigParse(optarg, &signature); if (0 > status || 0 != optarg[status]) { log_add("Not a signature: \"%s\"", optarg); status = COMMAND_SYNTAX; } else { if (CREATE == action) { log_info("Create action ignored"); } action = PUT_SIGNATURE; status = 0; } break; } case 'q': { quiet = 1; break; } case 'R': { if (UNKNOWN != action) { log_add("Can't mix reset action with other actions"); status = COMMAND_SYNTAX; } else { action = RESET; } break; } case 'r': { if (UNKNOWN != action) { log_add("Can't mix remove action with other actions"); status = COMMAND_SYNTAX; } else { action = REMOVE; } break; } case 's': { if (CREATE == action) { log_info("Create action ignored"); } string = optarg; action = PUT_STRING; break; } case 't': { status = tsParse(optarg, ×tamp); if (0 > status || 0 != optarg[status]) { log_add("Not a timestamp: \"%s\"", optarg); status = COMMAND_SYNTAX; } else { if (CREATE == action) { log_info("Create action ignored"); } action = PUT_TIME; status = 0; } break; } case 'u': { char* end; errno = 0; uint = strtoul(optarg, &end, 0); if (0 != *end || (0 == uint && 0 != errno)) { log_add("Not an unsigned integer: \"%s\"", optarg); status = COMMAND_SYNTAX; } else { if (CREATE == action) { log_info("Create option ignored"); } action = PUT_UINT; } break; } case 'v': { if (!log_is_enabled_info) (void)log_set_level(LOG_LEVEL_INFO); break; } case 'x': { (void)log_set_level(LOG_LEVEL_DEBUG); break; } case ':': { log_add("Option \"-%c\" requires an operand", optopt); status = COMMAND_SYNTAX; break; } default: log_add("Unknown option: \"%c\"", optopt); status = COMMAND_SYNTAX; /* no break */ } } /* options loop */ if (status) { log_flush_error(); if (COMMAND_SYNTAX == status) usage(progname); } else { const int argCount = argc - optind; if (UNKNOWN == action) action = PRINT; switch (action) { case CREATE: { if (0 < argCount) { log_error("Too many arguments"); usage(progname); status = COMMAND_SYNTAX; } else { status = createRegistry(); } break; } case RESET: { if (0 < argCount) { log_error("Too many arguments"); usage(progname); status = COMMAND_SYNTAX; } else { status = resetRegistry(); } break; } case REMOVE: { if (0 == argCount) { log_error( "Removal action requires absolute pathname(s)"); usage(progname); status = COMMAND_SYNTAX; } else { log_debug("Removing registry"); status = actUponPathList(argv + optind, deletePath, quiet); } break; } case PRINT: { log_debug("Printing registry"); status = (0 == argCount) ? printPath("/", quiet) : actUponPathList(argv + optind, printPath, quiet); break; } default: { /* * Must be some kind of "put". */ if (0 == argCount) { log_error("Put action requires value pathname"); usage(progname); status = COMMAND_SYNTAX; } else { switch (action) { case PUT_BOOL: status = reg_putBool(argv[optind], boolean); break; case PUT_UINT: status = reg_putUint(argv[optind], uint); break; case PUT_STRING: status = reg_putString(argv[optind], string); break; case PUT_TIME: status = reg_putTime(argv[optind], ×tamp); break; case PUT_SIGNATURE: status = reg_putSignature(argv[optind], signature); break; default: abort(); } if (status) { log_flush_error(); status = SYSTEM_ERROR; } } } /* put switch */ /* no break */ } /* "action" switch */ } /* decoded options */ sb_free(_valuePath); } /* "_valuePath" allocated */ return status; }
int main( const int argc, const char* const* argv) { int exitCode = EXIT_FAILURE; char cmd[80]; char* regPath = "/tmp/testRegistry"; if (log_init(argv[0])) { (void)fprintf(stderr, "Couldn't open logging system\n"); } else { if (0 != system(strcat(strcpy(cmd, "rm -rf "), regPath))) { (void)fprintf(stderr, "Couldn't remove test-directory \"%s\": %s", regPath, strerror(errno)); } else { if (-1 == mkdir(regPath, S_IRWXU)) { (void)fprintf(stderr, "Couldn't create test directory \"%s\": %s\n", regPath, strerror(errno)); } else { if (CUE_SUCCESS == CU_initialize_registry()) { CU_Suite* testSuite = CU_add_suite(__FILE__, setup, teardown); if (NULL != testSuite) { const char* progname = strrchr(argv[0], '/'); progname = (NULL == progname) ? argv[0] : progname + 1; CU_ADD_TEST(testSuite, test_regMissing); CU_ADD_TEST(testSuite, test_keyWithSpace); CU_ADD_TEST(testSuite, test_regString); CU_ADD_TEST(testSuite, test_regBool); CU_ADD_TEST(testSuite, test_regInt); CU_ADD_TEST(testSuite, test_regTime); CU_ADD_TEST(testSuite, test_regSignature); CU_ADD_TEST(testSuite, test_regSubkeys); CU_ADD_TEST(testSuite, test_regDelete); CU_ADD_TEST(testSuite, test_regNode); CU_ADD_TEST(testSuite, test_regReset); #if 0 CU_ADD_TEST(testSuite, test_regRemove); #endif reg_setDirectory(regPath); if (CU_basic_run_tests() == CUE_SUCCESS) { if (0 == CU_get_number_of_failures()) exitCode = EXIT_SUCCESS; } } CU_cleanup_registry(); } /* CUnit registery allocated */ #if 0 if (-1 == rmdir(regPath)) (void)fprintf(stderr, "Couldn't delete test directory \"%s\": %s\n", regPath, strerror(errno)); #endif } /* test-directory created */ } /* test-directory removed */ } /* logging initialized */ return exitCode; }