summaryrefslogtreecommitdiff
path: root/mini-commander/src/macro.c
blob: e6108d02e662503e184d21e7cd825d4361fb3ca5 (plain)
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
/*
 * Mini-Commander Applet
 * Copyright (C) 1998 Oliver Maruhn <oliver@maruhn.com>
 *
 * Author: Oliver Maruhn <oliver@maruhn.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>

#include "macro.h"
#include "preferences.h"

/* search for the longest matching prefix */
static MCMacro *
mc_macro_with_prefix (MCData *mc,
		      char   *command)
{
    MCMacro *retval = NULL;
    GSList  *l;
    int      prefix_len_found = 0;

    for (l = mc->preferences.macros; l; l = l->next) {
	MCMacro *macro = l->data;
	int      pattern_len;

	pattern_len = strlen (macro->pattern);

	if (pattern_len > prefix_len_found &&
	    !strncmp (command, macro->pattern, pattern_len) &&
	    (strstr (macro->command, "$1") || pattern_len == strlen (command))) {
		/* found a matching prefix;
		 * if macro does not contain "$1" then the prefix has
		 * to to have the same length as the command
		 */
		prefix_len_found = pattern_len;
		retval = macro;
	}
    }

    return retval;
}

int
mc_macro_prefix_len (MCData *mc,
		     char   *command)
{
    MCMacro *macro;

    macro = mc_macro_with_prefix (mc, command);
    if (!macro)
	return 0;

    return strlen (macro->pattern);
}

int
mc_macro_prefix_len_wspace (MCData *mc,
			    char   *command)
{
    char *c_ptr;

    c_ptr = command + mc_macro_prefix_len (mc, command);

    while (*c_ptr != '\0' && *c_ptr == ' ')
	c_ptr++;

    return c_ptr - command;
}

char *
mc_macro_get_prefix (MCData *mc,
		     char   *command)
{
    MCMacro *macro;

    macro = mc_macro_with_prefix (mc, command);
    if (!macro)
	return NULL;

    return macro->pattern;
}

void
mc_macro_expand_command (MCData *mc,
			 char   *command)
{
    GSList *l;
    char    command_exec [1000];

    command_exec [0] = '\0';

    for (l = mc->preferences.macros; l; l = l->next) {
	regmatch_t  regex_matches [MC_MAX_NUM_MACRO_PARAMETERS];
	MCMacro    *macro = l->data;

	if (regexec (&macro->regex, command, MC_MAX_NUM_MACRO_PARAMETERS, regex_matches, 0) != REG_NOMATCH) {
	    char  placeholder [100];
	    char *char_p;
	    int   inside_placeholder;
	    int   parameter_number;

	    inside_placeholder = 0;

	    for (char_p = macro->command; *char_p != '\0'; ++char_p) {
		if (inside_placeholder == 0 &&
		    *char_p == '\\' &&
		    *(char_p + 1) >= '0' &&
		    *(char_p + 1) <= '9') {
			strcpy (placeholder, "");
			inside_placeholder = 1;
		} else if (inside_placeholder &&
			   (*(char_p + 1) < '0' || *(char_p + 1) > '9'))
			inside_placeholder = 2;
			
		if (inside_placeholder == 0)
		    strncat (command_exec, char_p, 1);
		else
		    strncat (placeholder, char_p, 1);

		if (inside_placeholder == 2) {
		    parameter_number = atoi (placeholder + 1);

		    if (parameter_number <= MC_MAX_NUM_MACRO_PARAMETERS &&
			regex_matches [parameter_number].rm_eo > 0)
			strncat (command_exec,
				 command + regex_matches [parameter_number].rm_so,
				 regex_matches [parameter_number].rm_eo - regex_matches [parameter_number].rm_so);

		     inside_placeholder = 0;
		}
	    }
	}
    }

    if (command_exec [0] != '\0')
	strcpy (command, command_exec);
}