1
0

hmain.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef H_MAIN_H_
  2. #define H_MAIN_H_
  3. #include "hplatform.h"
  4. #include "hdef.h"
  5. #include "hstring.h"
  6. typedef struct main_ctx_s {
  7. pid_t oldpid; // getpid_from_pidfile
  8. pid_t pid; // getpid
  9. char run_path[MAX_PATH];
  10. char program_name[MAX_PATH];
  11. int argc;
  12. int arg_len;
  13. char** os_argv;
  14. char** save_argv;
  15. char* cmdline;
  16. int envc;
  17. int env_len;
  18. char** os_envp;
  19. char** save_envp;
  20. char confile[MAX_PATH]; // default etc/${program}.conf
  21. char pidfile[MAX_PATH]; // default logs/${program}.pid
  22. char logfile[MAX_PATH]; // default logs/${program}.log
  23. keyval_t arg_kv;
  24. StringList arg_list;
  25. keyval_t env_kv;
  26. void* confile_parser; // deprecated
  27. } main_ctx_t;
  28. extern main_ctx_t g_main_ctx;
  29. // arg_type
  30. #define NO_ARGUMENT 0
  31. #define REQUIRED_ARGUMENT 1
  32. #define OPTIONAL_ARGUMENT 2
  33. // option define
  34. #define OPTION_PREFIX '-'
  35. #define OPTION_DELIM '='
  36. #define OPTION_ENABLE "on"
  37. #define OPTION_DISABLE "off"
  38. typedef struct option_s {
  39. char short_opt;
  40. const char* long_opt;
  41. int arg_type;
  42. } option_t;
  43. int main_ctx_init(int argc, char** argv);
  44. // ls -a -l
  45. // ls -al
  46. // watch -n 10 ls
  47. // watch -n10 ls
  48. int parse_opt(int argc, char** argv, const char* opt);
  49. // gcc -g -Wall -O3 -std=cpp main.c
  50. int parse_opt_long(int argc, char** argv, const option_t* long_options, int size);
  51. const char* get_arg(const char* key);
  52. const char* get_env(const char* key);
  53. #ifdef OS_UNIX
  54. void setproctitle(const char* title);
  55. #endif
  56. int create_pidfile();
  57. void delete_pidfile();
  58. pid_t getpid_from_pidfile();
  59. #endif // H_MAIN_H_