aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias Andrée <maandree@member.fsf.org>2015-12-10 22:57:51 +0100
committerMattias Andrée <maandree@member.fsf.org>2015-12-10 22:57:51 +0100
commit6a4cbfccbd649b6e9dd92be74d106f7a8336a264 (patch)
tree4f9f38fe21853c92dc3622db5857d6507613cfd6 /src
parentmostly splitting the source (diff)
downloadscrotty-6a4cbfccbd649b6e9dd92be74d106f7a8336a264.tar.gz
scrotty-6a4cbfccbd649b6e9dd92be74d106f7a8336a264.tar.bz2
scrotty-6a4cbfccbd649b6e9dd92be74d106f7a8336a264.tar.xz
soon, convert will not be used
Signed-off-by: Mattias Andrée <maandree@member.fsf.org>
Diffstat (limited to 'src')
-rw-r--r--src/info.c12
-rw-r--r--src/info.h2
-rw-r--r--src/kern-linux.c11
-rw-r--r--src/kern.h7
-rw-r--r--src/pattern.c2
-rw-r--r--src/scrotty.c132
6 files changed, 75 insertions, 91 deletions
diff --git a/src/info.c b/src/info.c
index 9eff7cb..36ab8f5 100644
--- a/src/info.c
+++ b/src/info.c
@@ -30,14 +30,14 @@ int
print_help (void)
{
return printf (_("SYNOPSIS\n"
- "\t%s [options...] [filename-pattern] [-- options-for-convert...]\n"
+ "\t%s [OPTIONS...] [--] [FILENAME-PATTERN]\n"
"\n"
"OPTIONS\n"
- "\t--help Print usage information.\n"
- "\t--version Print program name and version.\n"
- "\t--copyright Print copyright information.\n"
- "\t--raw Save in PNM rather than in PNG.\n"
- "\t--exec CMD Command to run for each saved image.\n"
+ "\t-h, --help Print usage information.\n"
+ "\t-v, --version Print program name and version.\n"
+ "\t-c, --copyright Print copyright information.\n"
+ "\t-r, --raw Save in PNM rather than in PNG.\n"
+ "\t-e, --exec CMD Command to run for each saved image.\n"
"\n"
"SPECIAL STRINGS\n"
"\tBoth the --exec and filename-pattern parameters can take format specifiers\n"
diff --git a/src/info.h b/src/info.h
index 1d2c652..cba9e05 100644
--- a/src/info.h
+++ b/src/info.h
@@ -20,7 +20,7 @@
# define PROGRAM_NAME "scrotty"
#endif
#ifndef PROGRAM_VERSION
-# define PROGRAM_VERSION "1.1"
+# define PROGRAM_VERSION "development-version"
#endif
diff --git a/src/kern-linux.c b/src/kern-linux.c
index af17822..28b4d4e 100644
--- a/src/kern-linux.c
+++ b/src/kern-linux.c
@@ -54,7 +54,7 @@ print_not_found_help (void)
* @param fbno The index of the framebuffer.
*/
void
-get_fbpath (char *pathbuf, int altpath, int fbno)
+get_fbpath (char *restrict pathbuf, int altpath, int fbno)
{
sprintf (pathbuf, "%s/fb%s%i", DEVDIR, (altpath ? "/" : ""), fbno);
}
@@ -70,7 +70,7 @@ get_fbpath (char *pathbuf, int altpath, int fbno)
* @return Zero on success, -1 on error.
*/
int
-measure (int fbno, char *fbpath, long *width, long *height)
+measure (int fbno, char *restrict fbpath, long *restrict width, long *restrict height)
{
static char buf[PATH_MAX];
char *delim;
@@ -124,9 +124,10 @@ measure (int fbno, char *fbpath, long *width, long *height)
* @return Zero on success, -1 on error.
*/
int
-convert_fb (FILE *file, char *buf, size_t n, size_t *adjustment)
+convert_fb (FILE *restrict file, const char *restrict buf,
+ size_t n, size_t *restrict adjustment)
{
- uint32_t *pixel;
+ const uint32_t *restrict pixel;
int r, g, b;
size_t off;
@@ -134,7 +135,7 @@ convert_fb (FILE *file, char *buf, size_t n, size_t *adjustment)
{
/* A pixel in the framebuffer is formatted as `%{blue}%{green}%{red}%{x}`
in big-endian binary, or `%{x}%{red}%{green}%{blue}` in little-endian binary. */
- pixel = (uint32_t *)(buf + off);
+ pixel = (const uint32_t *)(buf + off);
r = (*pixel >> 16) & 255;
g = (*pixel >> 8) & 255;
b = (*pixel >> 0) & 255;
diff --git a/src/kern.h b/src/kern.h
index 69eaf0b..624564c 100644
--- a/src/kern.h
+++ b/src/kern.h
@@ -39,7 +39,7 @@ void print_not_found_help (void);
* @param altpath The index of the alternative path-pattern to use.
* @param fbno The index of the framebuffer.
*/
-void get_fbpath (char *pathbuf, int altpath, int fbno);
+void get_fbpath (char *restrict pathbuf, int altpath, int fbno);
/**
* Get the dimensions of a framebuffer.
@@ -50,7 +50,7 @@ void get_fbpath (char *pathbuf, int altpath, int fbno);
* @param height Output parameter for the height of the image.
* @return Zero on success, -1 on error.
*/
-int measure (int fbno, char *fbpath, long *width, long *height);
+int measure (int fbno, char *restrict fbpath, long *restrict width, long *restrict height);
/**
* Convert read data from a framebuffer to PNM pixel data.
@@ -64,5 +64,6 @@ int measure (int fbno, char *fbpath, long *width, long *height);
* pixel is encoded.
* @return Zero on success, -1 on error.
*/
-int convert_fb (FILE *file, char *buf, size_t n, size_t *adjustment);
+int convert_fb (FILE *restrict file, const char *restrict buf,
+ size_t n, size_t *restrict adjustment);
diff --git a/src/pattern.c b/src/pattern.c
index 6912731..c766d83 100644
--- a/src/pattern.c
+++ b/src/pattern.c
@@ -145,7 +145,5 @@ evaluate (char *restrict buf, size_t n, const char *restrict pattern,
fail:
return -1;
-
-#undef P
}
diff --git a/src/scrotty.c b/src/scrotty.c
index c6cb3fc..da69cc9 100644
--- a/src/scrotty.c
+++ b/src/scrotty.c
@@ -16,12 +16,14 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#define _GNU_SOURCE /* For getopt_long. */
#include "common.h"
#include "kern.h"
#include "info.h"
#include "pnm.h"
#include "pattern.h"
+#include <getopt.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
@@ -64,6 +66,7 @@ const char* inttable[] =
};
+
/**
* `argv[0]` from `main`.
*/
@@ -77,13 +80,6 @@ const char *execname;
const char *failure_file = NULL;
/**
- * Arguments for `convert`,
- * the output file should be printed into `convert_args[2]`.
- * `NULL` is `convert` shall not be used.
- */
-static char **convert_args = NULL;
-
-/**
* The index of the alternative path-pattern,
* for the framebuffers, to try.
*/
@@ -92,13 +88,13 @@ static int try_alt_fbpath = 0;
/**
- * Create an PNM-file that is sent to `convert` for convertion
- * to a compressed format, or directly to a file.
+ * Create an PNM-file that is sent to a conversion process,
+ * or directly to a file.
*
* @param fbname The framebuffer device.
* @param width The width of the image.
* @param height The height of the image.
- * @param fd The file descriptor connected to `convert`'s stdin.
+ * @param fd The file descriptor connected to conversion process's stdin.
* @return Zero on success, -1 on error.
*/
static int
@@ -116,7 +112,7 @@ save_pnm (const char *fbpath, long width, long height, int fd)
if (fbfd == -1)
goto fail;
- /* Create a FILE*, for writing, for the image file. */
+ /* Create a FILE *, for writing, for the image file. */
file = fdopen (fd, "w");
if (file == NULL)
goto fail;
@@ -177,10 +173,11 @@ save_pnm (const char *fbpath, long width, long height, int fd)
* @param imgname The pathname of the output image.
* @param width The width of the image.
* @param height The height of the image.
+ * @param raw Save in PNM?
* @return Zero on success, -1 on error.
*/
static int
-save (const char *fbpath, const char *imgpath, long width, long height)
+save (const char *fbpath, const char *imgpath, long width, long height, int raw)
{
int pipe_rw[2] = { -1, -1 };
pid_t pid;
@@ -188,14 +185,14 @@ save (const char *fbpath, const char *imgpath, long width, long height)
int fd = -1;
int saved_errno;
- if (convert_args == NULL)
+ if (raw)
goto no_convert;
- /* Create a pipe that for sending data into the `convert` program. */
+ /* Create a pipe that for sending data into the conversion process program. */
if (pipe (pipe_rw) < 0)
goto fail;
- /* Fork the process, the child will exec. `convert`. */
+ /* Fork the process, the child will become the conversion process. */
pid = fork ();
if (pid == -1)
goto fail;
@@ -206,7 +203,7 @@ save (const char *fbpath, const char *imgpath, long width, long height)
{
/* Close the write-end of the pipe. */
close (pipe_rw[1]);
- /* Turn the read-end of the into stdin. */
+ /* Turn the read-end of the pipe into stdin. */
if (pipe_rw[0] != STDIN_FILENO)
{
close (STDIN_FILENO);
@@ -215,8 +212,7 @@ save (const char *fbpath, const char *imgpath, long width, long height)
close (pipe_rw[0]);
}
/* Exec. `convert` to convert the PNM-image we create to a compressed image. */
- sprintf (convert_args[2], "%s", imgpath);
- execvp ("convert", convert_args);
+ execlp ("convert", "convert", DEVDIR "/stdin", imgpath, NULL);
child_fail:
perror(execname);
@@ -236,15 +232,15 @@ save (const char *fbpath, const char *imgpath, long width, long height)
/* Close the write-end of the pipe. */
close (pipe_rw[1]), pipe_rw[1] = -1;
- /* Wait for `convert` to exit. */
+ /* Wait for conversion process to exit. */
if (waitpid (pid, &status, 0) < 0)
goto fail;
- /* Return successfully if and only if `convert` did. */
+ /* Return successfully if and only if conversion did. */
return status == 0 ? 0 : -1;
- /* `convert` shall not be used: */
+ /* Conversion shall not take place: */
no_convert:
/* Open output file. */
@@ -372,7 +368,7 @@ save_fb (int fbno, int raw, const char *filepattern, const char *execpattern)
return -1;
/* Take a screenshot of the current framebuffer. */
- if (save (fbpath, imgpath, width, height) < 0)
+ if (save (fbpath, imgpath, width, height, raw) < 0)
goto fail;
fprintf (stderr, _("Saved framebuffer %i to %s.\n"), fbno, imgpath);
@@ -433,15 +429,22 @@ int
main (int argc, char *argv[])
{
#define EXIT_USAGE(MSG) \
- return fprintf (stderr, _("%s: %s. Type '%s --help' for help.\n"), execname, MSG, execname), 1
-
- int fbno, r, i, dash = argc, exec = -1, help = 0, found = 0;
- int raw = 0, version = 0, copyright = 0, filepattern = -1;
- static char convert_args_0[] = "convert";
- static char convert_args_1[] = DEVDIR "/stdin";
- static char convert_args_2[PATH_MAX];
-
- execname = argc ? *argv : "scrotty";
+ return fprintf (stderr, _("%s: %s. Type '%s --help' for help.\n"), execname, MSG, execname), 2
+#define USAGE_ASSERT(ASSERTION, MSG) \
+ do { if (!(ASSERTION)) EXIT_USAGE (MSG); } while (0)
+
+ int fbno, r, found = 0, raw = 0;
+ char *exec = NULL;
+ char *filepattern = NULL;
+ struct option long_options[] =
+ {
+ {"help", no_argument, NULL, 'h'},
+ {"version", no_argument, NULL, 'v'},
+ {"copyright", no_argument, NULL, 'c'},
+ {"raw", no_argument, NULL, 'r'},
+ {"exec", required_argument, NULL, 'e'},
+ {NULL, 0, NULL, 0 }
+ };
/* Set up for internationalisation. */
#if defined(USE_GETTEXT) && defined(PACKAGE) && defined(LOCALEDIR)
@@ -451,62 +454,44 @@ main (int argc, char *argv[])
#endif
/* Parse command line. */
- for (i = 1; i < argc; i++)
+ execname = argc ? *argv : "scrotty";
+ for (;;)
{
- if (!strcmp (argv[i], "--help")) help = 1;
- else if (!strcmp (argv[i], "--version")) version = 1;
- else if (!strcmp (argv[i], "--copyright")) copyright = 1;
- else if (!strcmp (argv[i], "--raw")) raw = 1;
- else if (!strcmp (argv[i], "--exec")) exec = ++i;
- else if (!strcmp (argv[i], "--"))
+ r = getopt_long (argc, argv, "hvcre:", long_options, NULL);
+ if (r == -1) break;
+ else if (r == 'h') return -(print_help ());
+ else if (r == 'v') return -(print_version ());
+ else if (r == 'c') return -(print_copyright ());
+ else if (r == 'r') raw = 1;
+ else if (r == 'e')
{
- dash = i + 1;
- break;
+ USAGE_ASSERT (exec == NULL, _("--exec is used twice."));
+ exec = optarg;
}
- else if ((argv[i][0] == '-') || (filepattern != -1))
- EXIT_USAGE (_("Unrecognised option."));
+ else if (r == '?')
+ EXIT_USAGE (_("Invalid input."));
else
- filepattern = i;
+ abort ();
}
-
- /* Check that --exec is valid. */
- if (exec == argc)
- EXIT_USAGE (_("--exec has no argument."));
-
- /* Was --help, --version, or --copyright used? */
- if (help) return -(print_help ());
- if (version) return -(print_version ());
- if (copyright) return -(print_copyright ());
-
- /* Create arguments for `convert`. */
- if ((!raw) || (dash < argc))
+ while (optind < argc)
{
- convert_args = alloca ((size_t)(4 + (argc - dash)) * sizeof (char*));
- convert_args[0] = convert_args_0;
- convert_args[1] = convert_args_1;
- convert_args[2] = convert_args_2;
- for (i = dash; i < argc; i++)
- convert_args[i - dash + 2] = argv[i];
- convert_args[3 + (argc - dash)] = NULL;
+ USAGE_ASSERT (filepattern == NULL, _("FILENAME-PATTERN is used twice."));
+ filepattern = argv[optind++];
}
retry:
/* Take a screenshot of each framebuffer. */
for (fbno = 0;; fbno++)
{
- r = save_fb (fbno, raw,
- (filepattern < 0 ? NULL : argv[filepattern]),
- (exec < 0 ? NULL : argv[exec]));
+ r = save_fb (fbno, raw, filepattern, exec);
if (r < 0)
goto fail;
- if (r > 0)
- {
- if (fbno) /* Perhaps framebuffer 1 is the first. */
- break;
- else
- continue;
- }
- found = 1;
+ else if (r == 0)
+ found = 1;
+ else if (fbno > 0)
+ break;
+ else
+ continue; /* Perhaps framebuffer 1 is the first. */
}
/* Did not find any framebuffer? */
@@ -535,6 +520,5 @@ main (int argc, char *argv[])
execname, strerror (errno), failure_file);
return 1;
-#undef EXIT_USAGE
}