чување f029ec3f42a9263bc796fb067af266b936495324
родитељ 1c80682fac169dc33a81344684bd788103d98a5e
Аутор: Страхиња Радић <sr@strahinja.org>
Датум: Sat, 24 Jan 2026 21:34:50 +0100
Add -B/-b; change consts in config.def.h to not const
Diffstat:
измењених датотека: 5, додавања: 56(+), брисања: 27(-)
diff --git a/README b/README
@@ -52,7 +52,9 @@ A: Most likely because it is not essential to editing text files. However, you
Q: How can I configure sled if I installed it (from AUR|through my distro's
package manager)?
A: You don't. If you want customization/configuration, clone the git repo,
- customize and compile it from source, just like with suckless software.
+ customize and compile it from source, just like with suckless software. Some
+ options can be set/overridden per session on the command line, for example -B
+ turns off backups for that session, and -b turns them on.
Q: Why am I getting a "Permission denied" when trying to edit a file?
A: You either can't access the directory for writing and search (execute bit)
diff --git a/TODO b/TODO
@@ -7,7 +7,7 @@ TODO
that termbox has a function for getting characters from the display
buffer
-[ ] Make backup creation able to be disabled through a command-line
+[x] Make backup creation able to be disabled through a command-line
parameter?
- Rationale: usage in specific privilege-restricted scenarios
diff --git a/config.def.h b/config.def.h
@@ -30,29 +30,29 @@ static const struct Color colors[] = {
};
/* clang-format on */
-extern const int backup_fail;
-extern const char* backup_suffix;
-extern const int create_backups;
-extern const int enable_wrap;
-extern const int line_numbers;
-extern const int line_numbers_width;
-extern const int open_binary;
-extern const int wrap_width;
+extern int backup_fail;
+extern char* backup_suffix;
+extern int create_backups;
+extern int enable_wrap;
+extern int line_numbers;
+extern int line_numbers_width;
+extern int open_binary;
+extern int wrap_width;
#ifdef SLED_C_GLOBALS
/* clang-format off */
/* Fail with error if creating backups is not possible due to file and
* directory permissions? */
-const int backup_fail = 1;
-const char* backup_suffix = "~";
-const int create_backups = 1; /* comment out to not create backups */
-const int enable_wrap = 1;
-const int line_numbers = 0; /* draw line numbers? initial value */
-const int open_binary = 0; /* open binary files? */
+int backup_fail = 1;
+char* backup_suffix = "~";
+int create_backups = 1; /* comment out to not create backups */
+int enable_wrap = 1;
+int line_numbers = 0; /* draw line numbers? initial value */
+int open_binary = 0; /* open binary files? */
/* - ideally, wrap_width + line_numbers_width (automatic) <= 80 */
/* - wrap width of 72 is recommended on https://useplaintext.email/ */
-const int wrap_width = 72;
+int wrap_width = 72;
/* Always processed */
static const struct Key top_level_keys[] = {
diff --git a/sled.1.in b/sled.1.in
@@ -14,11 +14,21 @@
.Nm
.Fl h | \-help | Fl V | \-full\-version | Fl v | \-version
.Nm
+.Op Fl B | Fl \-no\-backup
+.Op Fl b | Fl \-backup
.Ar filename
.Sh DESCRIPTION
.Nm
is a simple text editor with a terminal UI.
.Bl -tag -width Ds
+.It Fl B , \-no\-backup
+Disables backups.
+This option overrides the setting in
+.Pa config.h .
+.It Fl b , \-backup
+Enables backups.
+This option overrides the setting in
+.Pa config.h .
.It Fl h , \-help
Show option summary.
.It Fl V , \-full\-version
diff --git a/sled.c b/sled.c
@@ -2870,6 +2870,7 @@ main(int argc, char** argv)
int dir_access_errno;
int saved_errno;
int tbires;
+ int optind = 1;
*prompt = 0;
@@ -2886,25 +2887,27 @@ main(int argc, char** argv)
if (argc == 1)
return print_error(1, "No filename given");
- if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
+main_options_loop:
+ if (!strcmp(argv[optind], "-h") || !strcmp(argv[optind], "--help"))
{
printf("Usage:\t%s -h | --help | -V | --full-version | "
- "-v |"
- " --version\n"
- "\t%s filename\n",
+ "-v | --version\n"
+ "\t%s [-B | --no-backup] [-b | --backup] filename\n",
PROGRAM_NAME, PROGRAM_NAME);
return 0;
}
- else if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))
+ else if (!strcmp(argv[optind], "-v")
+ || !strcmp(argv[optind], "--version"))
{
printf("%s %s, committed on %s\n", PROGRAM_NAME, VERSION, DATE);
return 0;
}
- else if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--full-version"))
+ else if (!strcmp(argv[optind], "-V")
+ || !strcmp(argv[optind], "--full-version"))
{
printf("%s %s, committed on %s\n", PROGRAM_NAME, VERSION, DATE);
puts(COPYRIGHT);
- printf("Features set in config.h:\n");
+ printf("Features set in config.h and on command line:\n");
printf(" backup_fail:\t\t%s\n", backup_fail ? "set" : "unset");
printf(" create_backups:\t%s\n",
create_backups ? "set" : "unset");
@@ -2915,6 +2918,20 @@ main(int argc, char** argv)
printf(" wrap_width:\t\t%d\n", wrap_width);
return 0;
}
+ else if (!strcmp(argv[optind], "-B")
+ || !strcmp(argv[optind], "--no-backup"))
+ {
+ create_backups = 0;
+ optind++;
+ goto main_options_loop;
+ }
+ else if (!strcmp(argv[optind], "-b")
+ || !strcmp(argv[optind], "--backup"))
+ {
+ create_backups = 1;
+ optind++;
+ goto main_options_loop;
+ }
/*
* Chicken and egg problem: to get fpathconf(dirfd, _PC_PATH_MAX)
@@ -2922,11 +2939,11 @@ main(int argc, char** argv)
* need to know the value returned by fpathconf(dirfd, _PC_PATH_MAX)...
*/
/* argv[1] is the filename; BTW, "filename" is a misnomer */
- filename_size = strlen(argv[1]) + 1;
+ filename_size = strlen(argv[optind]) + 1;
if (!(dir = malloc(filename_size)))
return print_error(1, "Allocation failed (out of memory?)");
- (void)dirname(dir, argv[1], filename_size);
+ (void)dirname(dir, argv[optind], filename_size);
if (init_limits(dir) < 0)
{
perror(PROGRAM_NAME ": init_limits");
@@ -2942,7 +2959,7 @@ main(int argc, char** argv)
}
*filename = 0;
- MEMCCPY(filename, argv[1], limits[L_PATH_MAX], temp);
+ MEMCCPY(filename, argv[optind], limits[L_PATH_MAX], temp);
if (!*filename)
{
free(filename);