Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use separate arguments instead of colon for option delimiting in -l and -m #203

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions redshift.1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
redshift \- Set color temperature of display according to time of day.
.SH SYNOPSIS
.B redshift
\fI[\-l LAT:LON | \-l PROVIDER:OPTIONS] [\-t DAY:NIGHT] \fR[\fIOPTIONS\fR...]
\fI[\-l LAT:LON | \-l PROVIDER OPTIONS] [\-t DAY:NIGHT] \fR[\fIOPTIONS\fR...]
.SH DESCRIPTION
.B redshift
adjusts the color temperature of your screen according to your
Expand Down Expand Up @@ -46,11 +46,11 @@ Your current location, in degrees, given as floating point numbers,
towards north and east, with negative numbers representing south and
west, respectively.
.TP
\fB\-l\fR PROVIDER[:OPTIONS]
\fB\-l\fR PROVIDER [OPTIONS]
Select provider for automatic location updates
(Use `-l list' to see available providers)
.TP
\fB\-m\fR METHOD[:OPTIONS]
\fB\-m\fR METHOD [OPTIONS]
Method to use to set color temperature
(Use `-m list' to see available methods)
.TP
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ redshift_SOURCES = \
location-manual.c location-manual.h \
solar.c solar.h \
systemtime.c systemtime.h \
opt-parser.c opt-parser.h \
hooks.c hooks.h \
gamma-dummy.c gamma-dummy.h

Expand Down
79 changes: 79 additions & 0 deletions src/opt-parser.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* opt-parser.c -- getopt wrapper source
This file is part of Redshift.

Redshift 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.

Redshift 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 Redshift. If not, see <http://www.gnu.org/licenses/>.

Copyright (c) 2014 Mattias Andrée <maandree@member.fsf.org>
*/
#include "opt-parser.h"

#include <unistd.h>
#include <stdlib.h>
#include <string.h>


int
parseopt(int argc, char *const *argv, const char *shortopts, const char **args, int *args_count)
{
int opt;
char *p;

*args_count = 0;
opt = getopt(argc, argv, shortopts);
if (opt < 0)
return opt;

p = strchr(shortopts, opt);
if ((p == NULL) || (p[1] != ':'))
return opt;

args[(*args_count)++] = optarg;
while (optind < argc && argv[optind][0] != '-') {
args[(*args_count)++] = argv[optind++];
}

return opt;
}


char *
coalesce_args(const char *const *args, int args_count, char delimiter, char final)
{
size_t n = args_count ? (args_count + 1) : 2;
size_t i;
char *rc;
char *rc_;

for (i = 0; i < args_count; i++)
n += strlen(args[i]);

rc = rc_ = malloc(n * sizeof(char));
if (rc == NULL)
return NULL;

for (i = 0; i < args_count; i++) {
n = strlen(args[i]);
memcpy(rc_, args[i], n * sizeof(char));
rc_ += n;
*rc_++ = delimiter;
}
if (args_count > 0)
rc_--;

*rc_++ = final;
*rc_++ = '\0';

return rc;
}

28 changes: 28 additions & 0 deletions src/opt-parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* opt-parser.h -- getopt wrapper header
This file is part of Redshift.

Redshift 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.

Redshift 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 Redshift. If not, see <http://www.gnu.org/licenses/>.

Copyright (c) 2014 Mattias Andrée <maandree@member.fsf.org>
*/
#ifndef REDSHIFT_OPT_PARSER_H
#define REDSHIFT_OPT_PARSER_H


int parseopt(int argc, char *const *argv, const char *shortopts, const char **args, int *args_count);

char *coalesce_args(const char *const *args, int args_count, char delimiter, char final);


#endif /* ! REDSHIFT_OPT_PARSER_H */
Loading