<commit_before>/* SPDX-License-Identifier: BSD-2-Clause */

#define DEFINE_TERM_ACTION_0(name)                                          \
   static void                                                              \
   term_action_##name(term *_t, ulong __u1, ulong __u2, ulong __u3) {       \
      struct vterm *const t = _t;                                           \
      (void)t; /* silence the unused variable warning */                    \
      __term_action_##name(t);                                              \
   }

#define DEFINE_TERM_ACTION_1(name, t1)                                      \
   static void                                                              \
   term_action_##name(term *_t, ulong __a1, ulong __u1, ulong __u2) {       \
      struct vterm *const t = _t;                                           \
      __term_action_##name(t, (t1)__a1);                                    \
   }

#define DEFINE_TERM_ACTION_2(name, t1, t2)                                  \
   static void                                                              \
   term_action_##name(term *_t, ulong __a1, ulong __a2, ulong __u1) {       \
      struct vterm *const t = _t;                                           \
      __term_action_##name(t, (t1)__a1, (t2)__a2);                          \
   }

#define DEFINE_TERM_ACTION_3(name, t1, a1, t2, a2, t3, a3, ...)             \
   static void                                                              \
   term_action_##name(term *_t, ulong __##a1, ulong __##a2, ulong __##a3) { \
      struct vterm *const t = _t;                                           \
      t1 a1 = (t1)__##a1;                                                   \
      t2 a2 = (t2)__##a2;                                                   \
      t3 a3 = (t3)__##a3;                                                   \
      __VA_ARGS__                                                           \
   }

#define CALL_ACTION_FUNC_0(func, t)                                \
   (func)((t), 0, 0, 0)

#define CALL_ACTION_FUNC_1(func, t, a1)                            \
   (func)((t), (ulong)(a1), 0, 0)

#define CALL_ACTION_FUNC_2(func, t, a1, a2)                        \
   (func)((t), (ulong)(a1), (ulong)(a2), 0)

#define CALL_ACTION_FUNC_3(func, t, a1, a2, a3)                    \
   (func)((t), (ulong)(a1), (ulong)(a2), (ulong)(a3))

static void
__term_action_none(struct vterm *const t)
{
   /* do nothing */
}

DEFINE_TERM_ACTION_0(none)

static void
__term_action_enable_cursor(struct vterm *const t, u16 val)
{
   term_int_enable_cursor(t, val);
}

DEFINE_TERM_ACTION_1(enable_cursor, u16)

static void
__term_action_scroll(struct vterm *const t,
                     u32 lines, enum term_scroll_type st)
{
   if (st == term_scroll_up) {
      term_int_scroll_up(t, lines);
   } else {
      ASSERT(st == term_scroll_down);
      term_int_scroll_down(t, lines);
   }
}

DEFINE_TERM_ACTION_2(scroll, u32, enum term_scroll_type)

static void
__term_action_non_buf_scroll(struct vterm *const t, u16 n, u16 dir)
{
   if (dir == term_scroll_up) {

      term_internal_non_buf_scroll_up(t, n);

   } else {

      ASSERT(dir == term_scroll_down);
      term_internal_non_buf_scroll_down(t, n);
   }
}

DEFINE_TERM_ACTION_2(non_buf_scroll, u16, u16)

static void
__term_action_move_ch_and_cur(struct vterm *const t, int row, int col)
{
   term_int_move_ch_and_cur(t, row, col);
}

DEFINE_TERM_ACTION_2(move_ch_and_cur, int, int)

DEFINE_TERM_ACTION_3(write, char *, buf, u32, len, u8, color,
{
   const struct video_interface *const vi = t->vi;

   ts_scroll_to_bottom(t);
   vi->enable_cursor();

   for (u32 i = 0; i < len; i++) {

      if (UNLIKELY(t->filter == NULL)) {
         /* Early term use by printk(), before tty has been initialized */
         term_internal_write_char2(t, buf[i], color);
         continue;
      }

      /*
       * NOTE: We MUST store buf[i] in a local variable because the filter
       * function is absolutely allowed to modify its contents!!
       *
       * (Of course, buf is *not* required to point to writable memory.)
       */
      char c = buf[i];
      struct term_action a = { .type1 = a_none };
      enum term_fret r = t->filter((u8 *)&c, &color, &a, t->filter_ctx);

      if (LIKELY(r == TERM_FILTER_WRITE_C))
         term_internal_write_char2(t, c, color);

      term_execute_action(t, &a);
   }

   if (t->cursor_enabled)
      vi->move_cursor(t->r, t->c, get_curr_cell_fg_color(t));
})

/* Direct write without any filter nor move_cursor/flush */
DEFINE_TERM_ACTION_3(direct_write, char *, buf, u32, len, u8, color,
{
   for (u32 i = 0; i < len; i++)
      term_internal_write_char2(t, buf[i], color);
})


static void
__term_action_set_col_offset(struct vterm *const t, u16 off)
{
   t->col_offset = off;
}

DEFINE_TERM_ACTION_1(set_col_offset, u16)

static void
__term_action_move_ch_and_cur_rel(struct vterm *const t, s8 dr, s8 dc)
{
   if (!t->buffer)
      return;

   t->r = (u16) CLAMP((int)t->r + dr, 0, t->rows - 1);
   t->c = (u16) CLAMP((int)t->c + dc, 0, t->cols - 1);

   if (t->cursor_enabled)
      t->vi->move_cursor(t->r, t->c, get_curr_cell_fg_color(t));
}

DEFINE_TERM_ACTION_2(move_ch_and_cur_rel, s8, s8)

static void __term_action_reset(struct vterm *const t)
{
   t->vi->enable_cursor();
   term_int_move_ch_and_cur(t, 0, 0);
   t->scroll = t->max_scroll = 0;

   for (u16 i = 0; i < t->rows; i++)<commit_msg>[term] Make DEFINE_TERM_ACTION_* generate just wrappers [4/4]<commit_after>/* SPDX-License-Identifier: BSD-2-Clause */

#define DEFINE_TERM_ACTION_0(name)                                          \
   static void                                                              \
   term_action_##name(term *_t, ulong __u1, ulong __u2, ulong __u3) {       \
      struct vterm *const t = _t;                                           \
      (void)t; /* silence the unused variable warning */                    \
      __term_action_##name(t);                                              \
   }

#define DEFINE_TERM_ACTION_1(name, t1)                                      \
   static void                                                              \
   term_action_##name(term *_t, ulong __a1, ulong __u1, ulong __u2) {       \
      struct vterm *const t = _t;                                           \
      __term_action_##name(t, (t1)__a1);                                    \
   }

#define DEFINE_TERM_ACTION_2(name, t1, t2)                                  \
   static void                                                              \
   term_action_##name(term *_t, ulong __a1, ulong __a2, ulong __u1) {       \
      struct vterm *const t = _t;                                           \
      __term_action_##name(t, (t1)__a1, (t2)__a2);                          \
   }

#define DEFINE_TERM_ACTION_3(name, t1, t2, t3)                              \
   static void                                                              \
   term_action_##name(term *_t, ulong __a1, ulong __a2, ulong __a3) {       \
      struct vterm *const t = _t;                                           \
      __term_action_##name(t, (t1)__a1, (t2)__a2, (t3)__a3);                \
   }

#define CALL_ACTION_FUNC_0(func, t)                                \
   (func)((t), 0, 0, 0)

#define CALL_ACTION_FUNC_1(func, t, a1)                            \
   (func)((t), (ulong)(a1), 0, 0)

#define CALL_ACTION_FUNC_2(func, t, a1, a2)                        \
   (func)((t), (ulong)(a1), (ulong)(a2), 0)

#define CALL_ACTION_FUNC_3(func, t, a1, a2, a3)                    \
   (func)((t), (ulong)(a1), (ulong)(a2), (ulong)(a3))

static void
__term_action_none(struct vterm *const t)
{
   /* do nothing */
}

DEFINE_TERM_ACTION_0(none)

static void
__term_action_enable_cursor(struct vterm *const t, u16 val)
{
   term_int_enable_cursor(t, val);
}

DEFINE_TERM_ACTION_1(enable_cursor, u16)

static void
__term_action_scroll(struct vterm *const t,
                     u32 lines, enum term_scroll_type st)
{
   if (st == term_scroll_up) {
      term_int_scroll_up(t, lines);
   } else {
      ASSERT(st == term_scroll_down);
      term_int_scroll_down(t, lines);
   }
}

DEFINE_TERM_ACTION_2(scroll, u32, enum term_scroll_type)

static void
__term_action_non_buf_scroll(struct vterm *const t, u16 n, u16 dir)
{
   if (dir == term_scroll_up) {

      term_internal_non_buf_scroll_up(t, n);

   } else {

      ASSERT(dir == term_scroll_down);
      term_internal_non_buf_scroll_down(t, n);
   }
}

DEFINE_TERM_ACTION_2(non_buf_scroll, u16, u16)

static void
__term_action_move_ch_and_cur(struct vterm *const t, int row, int col)
{
   term_int_move_ch_and_cur(t, row, col);
}

DEFINE_TERM_ACTION_2(move_ch_and_cur, int, int)

static void
__term_action_write(struct vterm *const t, char * buf, u32 len, u8 color)
{
   const struct video_interface *const vi = t->vi;

   ts_scroll_to_bottom(t);
   vi->enable_cursor();

   for (u32 i = 0; i < len; i++) {

      if (UNLIKELY(t->filter == NULL)) {
         /* Early term use by printk(), before tty has been initialized */
         term_internal_write_char2(t, buf[i], color);
         continue;
      }

      /*
       * NOTE: We MUST store buf[i] in a local variable because the filter
       * function is absolutely allowed to modify its contents!!
       *
       * (Of course, buf is *not* required to point to writable memory.)
       */
      char c = buf[i];
      struct term_action a = { .type1 = a_none };
      enum term_fret r = t->filter((u8 *)&c, &color, &a, t->filter_ctx);

      if (LIKELY(r == TERM_FILTER_WRITE_C))
         term_internal_write_char2(t, c, color);

      term_execute_action(t, &a);
   }

   if (t->cursor_enabled)
      vi->move_cursor(t->r, t->c, get_curr_cell_fg_color(t));
}

DEFINE_TERM_ACTION_3(write, char *, u32, u8)

/* Direct write without any filter nor move_cursor/flush */
static void
__term_action_direct_write(struct vterm *const t, char *buf, u32 len, u8 color)
{
   for (u32 i = 0; i < len; i++)
      term_internal_write_char2(t, buf[i], color);
}

DEFINE_TERM_ACTION_3(direct_write, char *, u32, u8)

static void
__term_action_set_col_offset(struct vterm *const t, u16 off)
{
   t->col_offset = off;
}

DEFINE_TERM_ACTION_1(set_col_offset, u16)

static void
__term_action_move_ch_and_cur_rel(struct vterm *const t, s8 dr, s8 dc)
{
   if (!t->buffer)
      return;

   t->r = (u16) CLAMP((int)t->r + dr, 0, t->rows - 1);
   t->c = (u16) CLAMP((int)t->c + dc, 0, t->cols - 1);

   if (t->cursor_enabled)
      t->vi->move_cursor(t->r, t->c, get_curr_cell_fg_color(t));
}

DEFINE_TERM_ACTION_2(move_ch_and_cur_rel, s8, s8)

static void __term_action_reset(struct vterm *const t)
{
   t->vi->enable_cursor();
   term_int_move_ch_and_cur(t, 0, 0);
   t->scroll = t->max_scroll = 0;

   for (u16 i = 0; i < t->rows; i++)
{'fraction_non_alphanumeric': 0.11853627062402065, 'fraction_numerical': 0.023873168033920176, 'mean_word_length': 1.8007227671657202, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 56, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5607223', 'n_tokens_mistral': 3877, 'n_tokens_neox': 3728, 'n_words': 836}