| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <getopt.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- //7-segment digital tube
- typedef struct {
- char *f_arg;
- int r_flag;
- int d_flag;
- char *str_arg;
- } Options;
- int flash_flag = 0;
- int read_flag = 0;
- int dp_flag = 0;
- long int display_value = 0;
- void print_usage(const char *program_name) {
- printf("Usage: %s [options] <string>\n", program_name);
- printf("Options:\n");
- printf(" -f <arg> Specify an argument for -f option\n");
- printf(" -r Enable r option (no argument)\n");
- printf(" -d Enable d option (no argument)\n");
- printf(" -h Show this help message\n");
- printf("\nExample:\n");
- printf(" %s -f config.txt -r -d hello\n", program_name);
- printf(" %s -r -f data.txt world\n", program_name);
- }
- int parse_arguments(int argc, char *argv[], Options *opts) {
- int opt;
- int ret = 0;
- int f_option_count = 0;
-
- memset(opts, 0, sizeof(Options));
-
- while ((opt = getopt(argc, argv, "f:rdh")) != -1) {
- switch (opt) {
- case 'f':
- flash_flag = atoi(optarg);
- opts->f_arg = optarg;
- f_option_count++;
- printf("[DEBUG] -f option with argument: %s\n", optarg);
- break;
-
- case 'r':
- read_flag = 1;
- opts->r_flag = 1;
- printf("[DEBUG] -r option enabled\n");
- break;
-
- case 'd':
- dp_flag = 1;
- opts->d_flag = 1;
- printf("[DEBUG] -d option enabled\n");
- break;
-
- case 'h':
- print_usage(argv[0]);
- return 0;
-
- case '?':
- return -1;
-
- default:
- fprintf(stderr, "Unexpected option: %c\n", opt);
- return -1;
- }
- }
-
- if (f_option_count > 1) {
- fprintf(stderr, "Error: -f option can only be used once\n");
- return -1;
- }
-
- if (optind < argc) {
- if (argc - optind > 1) {
- fprintf(stderr, "Warning: Extra arguments after string will be ignored\n");
- }
- opts->str_arg = argv[optind];
- char *endptr;
- display_value = strtol(opts->str_arg, &endptr, 16);
- if(*endptr != '\0')
- {
- printf("Error: Invalid string argument\n");
- return -1;
- }
- else
- {
- printf("display_value=%lx\n", display_value);
- }
- printf("[DEBUG] String argument: %s\n", opts->str_arg);
- } else {
- fprintf(stderr, "Error: Missing required string argument\n");
- print_usage(argv[0]);
- return -1;
- }
-
- return 1;
- }
- void show_results(Options *opts) {
- printf("\n=== Parsing Results ===\n");
-
- if (opts->f_arg) {
- printf("-f option argument: %s\n", opts->f_arg);
- } else {
- printf("-f option not specified\n");
- }
-
- printf("-r option: %s\n", opts->r_flag ? "enabled" : "disabled");
- printf("-d option: %s\n", opts->d_flag ? "enabled" : "disabled");
-
- if (opts->str_arg) {
- printf("String argument: %s\n", opts->str_arg);
- }
-
- printf("======================\n");
- }
- int main(int argc, char *argv[]) {
- Options opts;
- int ret;
- unsigned int cmd = 0;
- int fd = 0;
-
- printf("Argument Parser Demo\n");
- printf("====================\n");
- printf("Total arguments: %d\n", argc);
-
- printf("Raw arguments:");
- for (int i = 0; i < argc; i++) {
- printf(" [%d]%s", i, argv[i]);
- }
- printf("\n\n");
-
- ret = parse_arguments(argc, argv, &opts);
-
- if (ret == 0) {
- return 0;
- } else if (ret < 0) {
- fprintf(stderr, "\nArgument parsing failed!\n");
- return 1;
- }
-
- show_results(&opts);
- fd = open("/dev/ssegment", O_RDWR);
- if(fd < 0)
- {
- printf("open led device failed\n");
- return -1;
- }
-
- cmd = 0;
- if(dp_flag == 1)
- {
- cmd = cmd | 0x0200;
- }
- if(flash_flag == 1)
- {
- cmd = cmd | 0x8000;
- }
- cmd = cmd | (display_value & 0xff);
- if(read_flag == 1)
- {
- cmd = 0;
- ret = read(fd, &cmd, 4);
- if(ret < 0)
- {
- printf("read led device failed\n");
- return -1;
- }
- printf("read led device success, cmd=%x\n", cmd);
- }
- else
- {
- ret = write(fd, &cmd, 4);
- if(ret < 0)
- {
- printf("write led device failed\n");
- return -1;
- }
- }
-
- return 0;
- }
|