Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
url: merge regexlib into urlhack.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
FauxFaux committed May 22, 2018
1 parent 8224973 commit 3be7260
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 73 deletions.
4 changes: 2 additions & 2 deletions Recipe
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ TERMINAL = terminal wcwidth ldiscucs logging tree234 minibidi
# GUI front end and terminal emulator (putty, puttytel).
GUITERM = TERMINAL window windlg winctrls sizetip winprint winutils
+ wincfg sercfg winhelp winjump
+ urlhack regex_lib urlhack_win
+ urlhack urlhack_win

# Same thing on Unix.
UXTERM = TERMINAL uxcfg sercfg uxucs uxprint timing callback miscucs
GTKTERM = UXTERM gtkwin gtkcfg gtkdlg gtkfont gtkcols gtkmisc xkeysym
+ x11misc gtkcomm
+ urlhack regex_lib urlhack_unix
+ urlhack urlhack_unix

# Non-SSH back ends (putty, puttytel, plink).
NONSSH = telnet raw rlogin ldisc pinger
Expand Down
38 changes: 0 additions & 38 deletions regex_lib.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions regex_lib.h

This file was deleted.

46 changes: 26 additions & 20 deletions urlhack.c → urlhack.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#include <string.h>
#include <assert.h>
#include <regex>

extern "C" {
#include "putty.h"
#include "urlhack.h"
#include "misc.h"
#include "puttymem.h"
#include "terminal.h"

#include "regex_lib.h"
// TODO: linking fails on mingw if putty.h is included in urlhack.h,
// TODO: so forward-declare (inside extern "C") this here, after `Terminal` is defined.
void urlhack_for_every_link(
Terminal *term,
void (*output)(Terminal *, void *, wchar_t *, int *, int, int));
}

int urlhack_mouse_old_x = -1, urlhack_mouse_old_y = -1,
urlhack_current_region = -1;
Expand All @@ -16,9 +21,6 @@ static text_region **link_regions;
static unsigned int link_regions_len;
static unsigned int link_regions_current_pos;

int urlhack_is_ctrl_pressed();
void rtfm(const char *error);

int urlhack_is_in_link_region(int x, int y)
{
unsigned int i = 0;
Expand Down Expand Up @@ -132,7 +134,7 @@ void urlhack_link_regions_clear()

static int urlhack_disabled = 0;
static int is_regexp_compiled = 0;
static struct regexp *urlhack_rx;
static std::regex urlhack_rx;

static char *window_text;
static int window_text_len;
Expand Down Expand Up @@ -203,11 +205,13 @@ void urlhack_set_regular_expression(int mode, const char *expression)
is_regexp_compiled = 0;
urlhack_disabled = 0;

set_regerror_func(rtfm);
urlhack_rx = regcomp((char *)(to_use));

if (urlhack_rx == 0) {
urlhack_disabled = 1;
try {
urlhack_rx = std::regex(
to_use, std::regex_constants::ECMAScript | std::regex_constants::icase);
urlhack_disabled = false;
} catch (std::regex_error &error) {
rtfm(error.what());
urlhack_disabled = true;
}

is_regexp_compiled = 1;
Expand All @@ -229,17 +233,21 @@ void urlhack_go_find_me_some_hyperlinks(int screen_width)

text_pos = window_text;

while (regexec(urlhack_rx, text_pos) == 1) {
char *start_pos;
char *end_pos;
reg_get_range(urlhack_rx, &start_pos, &end_pos);
// Comedy bad attempt at iterators from the C++ committee here?
auto begin = std::cregex_iterator(window_text, window_text + window_text_len, urlhack_rx);
auto end = std::cregex_iterator();

for (std::cregex_iterator it = begin; it != end; ++it) {
std::cmatch match = *it;

const char *start_pos = match[0].first;
const char *end_pos = match[0].second;

if (' ' == *start_pos)
++start_pos;

int max_brackets = 0, x0, y0, x1, y1;
char *c;
for (c = start_pos; c < end_pos; ++c) {
for (const char *c = start_pos; c < end_pos; ++c) {
switch (*c) {
case '(':
++max_brackets;
Expand All @@ -263,7 +271,5 @@ void urlhack_go_find_me_some_hyperlinks(int screen_width)
x1 = screen_width - 1;

urlhack_add_link_region(x0, y0, x1, y1);

text_pos = end_pos + 1;
}
}
7 changes: 7 additions & 0 deletions urlhack.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#ifndef _URLHACK_H
#define _URLHACK_H

#ifdef __cplusplus
extern "C" {
#endif
#include <wchar.h>

typedef struct {
Expand Down Expand Up @@ -29,4 +32,8 @@ void rtfm(const char *error);
void urlhack_init();
void urlhack_cleanup();

#ifdef __cplusplus
}
#endif

#endif // _URLHACK_H
1 change: 1 addition & 0 deletions windows/urlhack_win.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <winsock2.h>
#include <windows.h>
#include <string.h>
#include "misc.h"
Expand Down

0 comments on commit 3be7260

Please sign in to comment.