File: bitwise.h

package info (click to toggle)
bitwise 0.50-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 628 kB
  • sloc: sh: 4,449; ansic: 2,234; makefile: 31
file content (180 lines) | stat: -rw-r--r-- 3,914 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Copyright 2019
 * Ramon Fried <ramon.fried@gmail.com
 */

#ifndef BITWISE_H
#define BITWISE_H

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <form.h>
#include <string.h>
#include "config.h"
/* Readine checks */

#ifdef HAVE_LIBREADLINE
#  if defined(HAVE_READLINE_READLINE_H)
#    include <readline/readline.h>
#  elif defined(HAVE_READLINE_H)
#    include <readline.h>
#  else /* !defined(HAVE_READLINE_H) */
extern char *readline();
#  endif /* !defined(HAVE_READLINE_H) */
#else /* !defined(HAVE_READLINE_READLINE_H) */
  /* no readline */
#endif /* HAVE_LIBREADLINE */

#ifdef HAVE_READLINE_HISTORY
#  if defined(HAVE_READLINE_HISTORY_H)
#    include <readline/history.h>
#  elif defined(HAVE_HISTORY_H)
#    include <history.h>
#  else /* !defined(HAVE_HISTORY_H) */
extern void add_history();
extern int write_history();
extern int read_history();
#  endif /* defined(HAVE_READLINE_HISTORY_H) */
  /* no history */
#endif /* HAVE_READLINE_HISTORY */

#define MAX_HISTORY_LEN 100

#define FIELDS_WIN  0
#define BINARY_WIN  1
#define COMMAND_WIN 2

#define BIT(nr) (1ULL << (nr))
#define MASK(s) (~0ULL >> (64 - s))
#define ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))

typedef enum output_type {
	CMD_OUTPUT_DECIMAL,
	CMD_OUTPUT_HEXADECIMAL,
	CMD_OUTPUT_OCTAL,
	CMD_OUTPUT_BINARY,
	CMD_OUTPUT_ALL,
} output_type;

/* Misc */
int lltostr(uint64_t val, char *buf, int base);
int set_width(char width);
void set_width_by_val(uint64_t val);
void die(const char *fmt, ...);
int parse_input(const char *input, uint64_t *val);
int validate_input(int ch, int base);
int sprintf_type(uint64_t val, char *buf, output_type type);

void init_terminal(void);
void deinit_terminal(void);

/* Interactive */
extern FORM *form;

int sprintf_size(uint64_t val, char *buf, bool si);
int start_interactive(uint64_t start);
void set_active_field(bool none);
void set_fields_width(int width);
void position_binary_curser(int previous_pos, int next_pos);
void paint_screen(void);
void unpaint_screen(void);
void update_binary();
int update_fields(int index);

/* Command */
void process_cmd(int ch);
void init_readline(void);
void deinit_readline(void);
void readline_redisplay(void);

/* calc */
int calc(int argc, char *argv[]);

/* Help */
void show_help(void);

#ifdef TRACE
#define LOG(...) do { \
		fprintf(fd, __VA_ARGS__); \
		fflush(fd);\
		} while (0)
extern FILE *fd;
#else
#define LOG(...) do { } while (0)
#endif

/* Colors */
void init_colors(void);

extern char *color_green;
extern char *color_red;
extern char *color_blue;
extern char *color_magenta;
extern char *color_cyan;
extern char *color_white;
extern char *color_reset;

extern WINDOW *fields_win;
extern WINDOW *binary_win;
extern WINDOW *cmd_win;
extern int active_win;
extern int last_win;

extern int bit_pos;
extern int g_has_color;
extern int g_width;
extern int g_output;
extern bool g_input_avail;
extern int g_input;
extern bool g_leave_req;
extern uint64_t g_val;

/* History */
typedef enum history_type {
	TYPE_INPUT_COMMAND = 0,
	TYPE_INPUT_EXPRESSION,
	TYPE_OUTPUT_RESULT,
	TYPE_OUTPUT_ERROR,
} history_type;

struct history_entry {
	history_type type;
	char *line;
};

extern struct history_entry history[];
extern unsigned int history_pos;

void flush_history(void);
void update_history_win(void);

static inline void append_to_history(const char *str, history_type type) {
	char *new_line;

	new_line = strdup(str);
	if (!new_line) {
		LOG("No memory to allocate string\n");
		return;
	}

	history[history_pos % MAX_HISTORY_LEN].line = new_line;
	history[history_pos % MAX_HISTORY_LEN].type = type;
	history_pos++;

	update_history_win();
}

static inline WINDOW *get_win(int win)
{
	if (win == FIELDS_WIN)
		return fields_win;
	else if (win == BINARY_WIN)
		return binary_win;
	else if (win == COMMAND_WIN)
		return cmd_win;
	else
		return NULL;
}

#endif /* end of include guard: BITWISE_H */