aboutsummaryrefslogtreecommitdiffstats
path: root/src/pattern.c
blob: c766d8362cff457bf14bef03366d55f244c32ab9 (plain) (blame)
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
/**
 * scrotty — Screenshot program for Linux's TTY
 * 
 * Copyright © 2014, 2015  Mattias Andrée (maandree@member.fsf.org)
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */
#include "common.h"
#include "pattern.h"



/**
 * Duplicate all '%':s in a buffer until the first occurrence of a zero byte.
 * 
 * @param   buf  The buffer.
 * @param   n    The size of the buffer.
 * @return       -1 if the buffer is too small, otherwise.
 *               the new position of the first zero byte.
 */
static ssize_t
duplicate_percents (char *restrict buf, size_t n)
{
  size_t p = 0, pi, pc = 0, i;
  
  /* Count number of '%':s. */
  for (i = 0; buf[i]; i++)
    if (buf[i] == '%')
      pc++;
  
  /* Check whether the string will overflow. */
  if (i + pc > n)
    return -1;
  
  /* Duplicate all '%':s. */
  for (pi = 0; pi < pc; pi++)
    {
      p = (size_t)(strchr (buf + p, '%') - buf);
      memmove (buf + p + 1, buf + p, (i - (p - pi)) * sizeof (char));
      p += 2;
    }
  
  return (ssize_t)(i + pi);
}


/**
 * Parse and evaluate a --exec argument or filename pattern.
 * 
 * If `path != NULL` than all non-escaped spaces in
 * `pattern` will be stored as 255-bytes in `buf`.
 * 
 * @param   buf      The output buffer.
 * @param   n        The size of `buf`.
 * @param   pattern  The pattern to evaluate.
 * @param   fbno     The index of the framebuffer.
 * @param   width    The width of the image/framebuffer.
 * @param   height   The height of the image/framebuffer.
 * @param   path     The filename of the saved image, `NULL`
 *                   during the evaluation of the filename pattern.
 * @return           Zero on success, -1 on error.
 */
int
evaluate (char *restrict buf, size_t n, const char *restrict pattern,
	  int fbno, long width, long height, const char *restrict path)
{
#define P(format, value)  r = snprintf (buf + i, n - i, format "%zn", value, &j)
  
  size_t i = 0;
  ssize_t j = 0;
  int percent = 0, backslash = 0, dollar = 0, r;
  char c;
  char *fmt;
  time_t t;
  struct tm *tm;
  
  /* Expand '$' and '\'. */
  while ((i < n) && ((c = *pattern++)))
    if (dollar)
      {
	dollar = 0;
	if (path == NULL)
	  if ((c == 'f') || (c == 'n'))
	    continue;
	if      (c == 'i')  P ("%i", fbno);
	else if (c == 'f')  P ("%s", path);
	else if (c == 'n')  P ("%s", strrchr (path, '/') ? (strrchr (path, '/') + 1) : path);
	else if (c == 'p')  P ("%ju", (uintmax_t)width * (uintmax_t)height);
	else if (c == 'w')  P ("%li", width);
	else if (c == 'h')  P ("%li", height);
	else if (c == '$')  r = 0, j = 1, buf[i] = '$';
	else if ((r < 0) || (j <= 0))
	  return -1;
	else if ((c == 'f') || (c == 'n'))
	  if (j = duplicate_percents (buf + i, n - i), j < 0)
	    goto enametoolong;
	i += (size_t)j;
      }
    else if (backslash)  buf[i++] = (c == 'n' ? '\n' : c), backslash = 0;
    else if (percent)    buf[i++] = c, percent = 0;
    else if (c == '%')   buf[i++] = c, percent = 1;
    else if (c == '\\')  backslash = 1;
    else if (c == '$')   dollar = 1;
    else if (c == ' ')   buf[i++] = path == NULL ? ' ' : (char)255; /* 255 is not valid in UTF-8. */
    else                 buf[i++] = c;
  if (i >= n)
    goto enametoolong;
  buf[i] = '\0';
  
  /* Check whether there are any '%' to expand. */
  if (strchr (buf, '%') == NULL)
    return 0;
  
  /* Copy the buffer so we can reuse the buffer and use its old content for the format. */
  fmt = alloca ((strlen (buf) + 1) * sizeof (char));
  memcpy (fmt, buf, (strlen (buf) + 1) * sizeof (char));
  
  /* Expand '%'. */
  t = time (NULL);
  tm = localtime (&t);
  if (tm == NULL)
    goto fail;
  
#ifdef __GNUC__
# pragma GCC diagnostic ignored "-Wformat-nonliteral"
#endif
  if (strftime (buf, n, fmt, tm) == 0)
    goto enametoolong; /* No errors are defined for `strftime`. What else can we do? */
  
  return 0;
  
 enametoolong:
  return errno = ENAMETOOLONG, -1;
  
 fail:
  return -1;
}