a simple cmdline implementation method--structure define

来源:互联网 发布:淘宝开店赚钱吗 编辑:程序博客网 时间:2024/06/10 20:15

structure define:

the major structure  consist of cmdline_param_t and cmdline_instance_t.

//parameter of cmdline

typedef struct
{
    // --- Command line
    unsigned int   line_len;       // Maximum length of the command line

    // --- I/O
    int            non_blocking;   // !0, if I/O in non blocking mode
    int            fd_in;          // Input file descriptor
    int            fd_out;         // Output file descriptor

    // --- History
    unsigned int   histo_len;      // History size
    int            auto_or_sp;     // Auto-completion or spaces for TAB
#define CMDLINE_TAB_AUTO_COMPLETE   0  // Provide 'auto_complete' callback (may be NUL)
#define CMDLINE_TAB_SPACES          1  // Provide the number of 'spaces' for a TAB (may be 0)

    union
    {
        void         (*auto_complete)(void                *ctx,                // User context
                                  const unsigned char *cmd_line,           // Current displayed command line
                                  unsigned int        *cursor,             // IN: Current cursor position
                                                                           // OUT: New cursor position
                                  unsigned char      **completed_cmd_line);// New command line if any (NULL otherwise)
                                 // Callback called upon TAB keystroke to trigger auto-completion
        unsigned int spaces;         // Number of spaces to display for a TAB
    } tab;
    char           histo_shortcut; // Character to call an history entry (0 if not activated)

    void          *ctx;            // Any user private information for future reference

} cmdline_param_t;


// Instance descriptor of cmdline
typedef struct
{
  cmdline_param_t  user;               // User parameters
  int              dbg;                // Debug level
  unsigned char   *cmd_line;           // Command line
  unsigned char   *saved_cmd_line;     // Saved command line
  int              echo_on;            // !0, if echo activated
  struct termios   orig_term_settings; // Saved terminal settings
  struct termios   new_term_settings;  // Saved terminal settings
  int              in_flag;            // Flags of input descriptor

  // Command line mngt
  unsigned char    stored_ungetc;
  int              state;              // Current state of the FSM
  int              prev_state;         // Previous FSM state
  int              cursor;             // Cursor's position in the line
  unsigned int     line_sz;            // Number of chars in the line
  char             eol;                // Character for 'end of line'

  // History mngt
  int             histo_on;            // !0, if echo activated
  unsigned char  *histo;               // History
  int             histo_cur;           // Current display index
  unsigned int    histo_sz;            // Number of occupied entries
  unsigned int    histo_insert;        // Insertion index

  // Function key
  cmdline_fn_key_t function_key;       // Callback called upon function keystroke

} cmdline_instance_t;