aboutsummaryrefslogtreecommitdiffhomepage
path: root/libslang/slsh/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'libslang/slsh/scripts')
-rwxr-xr-xlibslang/slsh/scripts/badlinks78
-rwxr-xr-xlibslang/slsh/scripts/htmlstrip48
-rwxr-xr-xlibslang/slsh/scripts/ls333
-rwxr-xr-xlibslang/slsh/scripts/lsrpm85
-rwxr-xr-xlibslang/slsh/scripts/mv143
-rwxr-xr-xlibslang/slsh/scripts/purge65
6 files changed, 752 insertions, 0 deletions
diff --git a/libslang/slsh/scripts/badlinks b/libslang/slsh/scripts/badlinks
new file mode 100755
index 0000000..77409bd
--- /dev/null
+++ b/libslang/slsh/scripts/badlinks
@@ -0,0 +1,78 @@
+#!/usr/bin/env slsh
+% Find links that point to non-existent files
+
+static define warn ()
+{
+ variable args = __pop_args (_NARGS-1);
+ variable err = ();
+ variable msg;
+
+ msg = sprintf (__push_args (args));
+
+ if (err)
+ err = ": " + errno_string (err);
+ else
+ err == "";
+
+ () = fprintf (stderr, "%s: %s%s\n",
+ __argv[0], msg, err);
+}
+
+static define badlinks (dir)
+{
+ variable files, file;
+ variable st;
+
+ files = listdir (dir);
+ if (files == NULL)
+ {
+ warn (errno, dir);
+ return;
+ }
+
+ foreach (files)
+ {
+ file = ();
+
+ file = path_concat (dir, file);
+ st = stat_file (file);
+ if (st != NULL)
+ continue;
+
+ if (errno != ENOENT)
+ {
+ warn (errno, file);
+ continue;
+ }
+
+ if (-1 == fprintf (stdout, "%s\n", file))
+ break;
+ }
+}
+
+define main (argc, argv)
+{
+ if (argc > 1)
+ {
+ if (argv[1] == "--help")
+ {
+ () = fprintf (stdout, "Usage: %s [--help] [dirs....]\n",
+ __argv[0]);
+ exit (1);
+ }
+ }
+
+ if (argc == 1)
+ {
+ badlinks (".");
+ exit (0);
+ }
+
+ foreach (argv[[1:]])
+ badlinks ();
+
+ exit (0);
+}
+
+main (__argc, __argv);
+
diff --git a/libslang/slsh/scripts/htmlstrip b/libslang/slsh/scripts/htmlstrip
new file mode 100755
index 0000000..c6a6b36
--- /dev/null
+++ b/libslang/slsh/scripts/htmlstrip
@@ -0,0 +1,48 @@
+#! /usr/bin/env slsh
+% -*- mode: slang -*-
+%_debug_info = 1;
+
+% This file strips HTML tags from one or more html files and write the result
+% to stdout. It is very simple minded.
+
+define process_file (file)
+{
+ variable fp, l;
+
+ if (file != NULL)
+ {
+ fp = fopen (file, "r");
+ if (fp == NULL)
+ {
+ () = fputs (sprintf ("Unable to open %s\n", file), stderr);
+ return;
+ }
+ }
+ else fp = stdin;
+
+ foreach (fp)
+ {
+ l = ();
+
+ l = strtrim (str_uncomment_string (l, "<", ">"));
+ !if (strlen (l))
+ continue;
+
+ () = fputs (l, stdout);
+ () = fputs ("\n", stdout);
+ }
+}
+
+if (__argc == 1)
+{
+ if (isatty (stdin))
+ {
+ () = fprintf (stderr, "Usage: %s [files...]\n", __argv[0]);
+ exit (1);
+ }
+ process_file (NULL);
+ exit (0);
+}
+
+foreach (__argv[[1:]]) process_file ();
+exit (0);
diff --git a/libslang/slsh/scripts/ls b/libslang/slsh/scripts/ls
new file mode 100755
index 0000000..197d47d
--- /dev/null
+++ b/libslang/slsh/scripts/ls
@@ -0,0 +1,333 @@
+#!/usr/bin/env slsh
+%_debug_info = 1;
+% A simple ls designed primarily for windows.
+
+static variable Months =
+ ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
+ "Oct", "Nov", "Dec"];
+
+static variable Six_Months_Ago = _time () - 3600*24*30*6;
+
+
+static define ls_long (this_dir, file_list, st_list)
+{
+ _for (0, length(file_list)-1, 1)
+ {
+ variable i = ();
+ variable file = file_list[i];
+ variable st = st_list[i];
+
+ variable size, mode, owner, group, symlink, mtime;
+ variable mstring;
+ variable tm;
+
+ size = st.st_size;
+ mtime = st.st_mtime;
+ mode = st.st_mode;
+ owner = st.st_uid;
+ group = st.st_gid;
+#ifdef WIN32
+ variable attrs = st.st_opt_attrs;
+#endif
+ tm = localtime (mtime);
+#ifdef WIN32
+ if (tm == NULL)
+ mtime = "Jan 01 1980";
+ else
+#endif
+ if (mtime < Six_Months_Ago)
+ mtime = sprintf ("%s %2d %4d",
+ Months[tm.tm_mon],
+ tm.tm_mday,
+ 1900 + tm.tm_year);
+ else
+ mtime = sprintf ("%s %2d %2d:%02d",
+ Months[tm.tm_mon],
+ tm.tm_mday,
+ tm.tm_hour,
+ tm.tm_min);
+
+ symlink = "";
+#ifexists readlink
+ if (stat_is ("lnk", mode))
+ {
+ symlink = readlink (path_concat (this_dir, file));
+ if (symlink == NULL)
+ symlink = "??";
+
+ symlink = " -> " + symlink;
+ }
+#endif
+#ifdef WIN32
+ mstring = stat_mode_to_string (mode, attrs);
+#else
+ mstring = stat_mode_to_string (mode);
+#endif
+ () = fprintf (stdout,
+ "%8s %8S %8S %10S %s %s%s\n",
+ mstring, owner, group, size, mtime, file, symlink);
+ }
+}
+
+static variable
+ Use_Long_Form = 0,
+ Use_atime = 0,
+ Sort_By_Time = 0,
+ Sort_By_Size = 0,
+ Use_a_Option = 0,
+ Use_F_Option = 1,
+ Use_R_Option = 0,
+ Use_d_Option = 0;
+
+
+static define parse_args (args)
+{
+ variable ch;
+
+ foreach (args)
+ {
+ ch = ();
+ switch (ch)
+ { case 'l': Use_Long_Form = 1; }
+ { case 'u': Use_atime = 1; }
+ { case 't': Sort_By_Time = 1; }
+ { case 'S': Sort_By_Size = 1; }
+ { case 'd': Use_d_Option = 1; }
+ { case 'a': Use_a_Option = 1; }
+ { case 'R': Use_R_Option = 1; }
+ { case '-':} % ignore it
+ {
+ () = fprintf (stderr, "Option '%c' not supported.\n", ch);
+ }
+ }
+}
+
+define ls_short (dirs)
+{
+ variable max_len;
+ variable ncols;
+ variable num, num_per_row, num_rows;
+ variable stride;
+
+ num = length (dirs);
+ max_len = 0;
+ foreach (dirs)
+ {
+ variable dir;
+
+ dir = ();
+ if (strlen (dir) > max_len)
+ max_len = strlen (dir);
+ }
+
+ max_len += 2;
+
+ variable format = "%-" + string (max_len) + "s";
+
+ ncols = 80;
+ num_per_row = ncols / max_len;
+ if (num_per_row == 0)
+ num_per_row = 1;
+
+ num_rows = (num + num_per_row - 1) / num_per_row;
+ _for (0, num_rows-1, 1)
+ {
+ variable r = ();
+ _for (0, num_per_row-1, 1)
+ {
+ variable c = ();
+ variable i = r + num_rows * c;
+
+ if (i < num)
+ {
+ if (c + 1 < num_per_row)
+ () = fprintf (stdout, format, dirs[i]);
+ else
+ () = fputs (dirs[i], stdout);
+ }
+ }
+ () = fputs ("\n", stdout);
+ }
+}
+
+static define size_sort (a, b)
+{
+ b.st_size - a.st_size;
+}
+
+static define time_sort (a, b)
+{
+ b.st_mtime - a.st_mtime;
+}
+
+static define is_non_null_fun (a)
+{
+ a != NULL;
+}
+
+
+define sort_files (dirs, sts)
+{
+ variable st, i, non_null;
+
+ % Some of the sts structs may be NULL. Get rid of those
+ non_null = array_map (Char_Type, &is_non_null_fun, sts);
+
+ i = where (non_null);
+ dirs = dirs [i];
+ sts = sts [i];
+
+
+ if (Use_atime)
+ {
+ foreach (sts)
+ {
+ st = ();
+ st.st_mtime = st.st_atime;
+ }
+ }
+
+ if (Sort_By_Time)
+ i = array_sort (sts, &time_sort);
+ else if (Sort_By_Size)
+ i = array_sort (sts, &size_sort);
+ else
+ i = array_sort (dirs);
+
+ return dirs[i], sts[i];
+}
+
+static define isdir_fun (st)
+{
+ stat_is ("dir", st.st_mode);
+}
+
+static define list_dir (dir)
+{
+#ifndef UNIX
+ if (Use_a_Option)
+ listdir (dir, "");
+ else
+#endif
+ listdir (dir);
+}
+
+
+define do_ls ();
+define do_ls (dirs, this_dir, recurse, prune_hidden)
+{
+ variable i, len, st, sts, dir;
+
+ if (dirs == NULL)
+ return;
+
+ len = length(dirs);
+ sts = Struct_Type [len];
+
+
+ _for (0, len-1, 1)
+ {
+ i = ();
+ dir = dirs[i];
+
+ if (prune_hidden)
+ {
+ if ((dir[0] == '.') and (Use_a_Option == 0))
+ {
+ !if (is_substr (dir, "\\"))
+ continue;
+ }
+ }
+
+ st = lstat_file (path_concat (this_dir, dir));
+ if (st == NULL)
+ () = fprintf (stderr, "%s: %s: %s\n",
+ __argv[0],
+ path_concat (this_dir, dir),
+ errno_string (errno));
+ else
+ sts[i] = st;
+ }
+
+
+ (dirs, sts) = sort_files (dirs, sts);
+
+ variable isdir;
+
+ if (length (sts)) isdir = array_map (Char_Type, &isdir_fun, sts);
+ else isdir = Int_Type[0];
+
+ variable i_reg = where (isdir == 0);
+ variable i_dir = where (isdir);
+ variable dont_recurse;
+
+
+ if (Use_F_Option and length (i_dir))
+ dirs[i_dir] = array_map (String_Type, &path_concat, dirs[i_dir], "");
+
+ dont_recurse = (Use_d_Option or not recurse);
+
+ if (dont_recurse)
+ {
+ if (Use_Long_Form)
+ ls_long (this_dir, dirs, sts);
+ else
+ ls_short (dirs);
+ return;
+ }
+
+ if (Use_Long_Form)
+ ls_long (this_dir, dirs[i_reg], sts[i_reg]);
+ else
+ ls_short (dirs[i_reg]);
+
+
+ if (length(i_dir) == 1)
+ {
+ if (length (i_reg) == 0)
+ {
+ dir = dirs[i_dir][0];
+
+ do_ls (list_dir (dir), dir, Use_R_Option, 1);
+ return;
+ }
+ () = fputs ("\n", stdout);
+ }
+
+
+ foreach (dirs[i_dir])
+ {
+ dir = ();
+ dir = path_concat (this_dir, dir);
+ () = fprintf (stdout, "%s:\n", dir);
+ do_ls (list_dir (dir), dir, Use_R_Option, 1);
+ () = fprintf (stdout, "\n");
+ }
+}
+
+define main (argc, argv)
+{
+ variable dirs;
+
+ if (argc == 1)
+ return do_ls (list_dir("."), ".", 0, 1);
+ else if (__argv[1][0] == '-')
+ {
+ parse_args (__argv[1]);
+
+ if (Use_d_Option and Use_R_Option)
+ Use_R_Option = 0;
+
+ if (__argc > 2)
+ dirs = __argv[[2:]];
+ else
+ return do_ls (list_dir("."), ".", Use_R_Option, 1);
+ }
+ else
+ dirs = __argv[[1:]];
+
+ do_ls (dirs, ".", 1, 0);
+}
+
+
+main (__argc, __argv);
diff --git a/libslang/slsh/scripts/lsrpm b/libslang/slsh/scripts/lsrpm
new file mode 100755
index 0000000..dcd607a
--- /dev/null
+++ b/libslang/slsh/scripts/lsrpm
@@ -0,0 +1,85 @@
+#! /usr/bin/env slsh
+% Generate a listing of an RPM file
+
+static define pgm_usage ()
+{
+ vmessage ("Usage: lsrpm FILENAME");
+ exit (1);
+}
+
+static variable RPM_Command = "rpm -q -l --dump -p ";
+
+static define exit_error (msg)
+{
+ () = fprintf (stderr, "%s\n", msg);
+ exit (1);
+}
+
+
+static define run_rpm (file)
+{
+ variable fp;
+ variable lines;
+ variable months =
+ ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
+ "Oct", "Nov", "Dec"];
+ variable s;
+
+ fp = popen (RPM_Command + file, "r");
+ if (fp == NULL)
+ exit_error ("Failed to open RPM process");
+
+ % each line contains:
+ % path size mtime md5sum mode owner group isconfig isdoc rdev symlink
+
+ variable six_months_ago = _time () - 3600*24*30*6;
+
+ foreach (fp)
+ {
+ variable path, size, mode, owner, group, symlink, mtime;
+ variable mstring;
+ variable tm;
+
+ s = ();
+ s = strchop (strtrim_end (s, "\n"), ' ', 0);
+
+ path = s[0];
+ size = s[1];
+ mtime = integer (s[2]);
+ mode = integer (s[4]);
+ owner = s[5];
+ group = s[6];
+
+ tm = localtime (mtime);
+ if (mtime < six_months_ago)
+ mtime = sprintf ("%s %2d %4d",
+ months[tm.tm_mon],
+ tm.tm_mday,
+ 1900 + tm.tm_year);
+ else
+ mtime = sprintf ("%s %2d % 2d:%02d",
+ months[tm.tm_mon],
+ tm.tm_mday,
+ tm.tm_hour,
+ tm.tm_min);
+
+
+ symlink = "";
+ if (stat_is ("lnk", mode))
+ symlink = " -> " + s[10];
+
+ mstring = stat_mode_to_string (mode);
+
+ if (-1 == fprintf (stdout,
+ "%8s %8s %8s %10s %s %s%s\n",
+ mstring, owner, group, size, mtime, path, symlink))
+ exit_error (sprintf ("Write failed: %s", errno_string (errno)));
+ }
+ () = pclose (fp);
+}
+
+if (__argc != 2)
+ pgm_usage ();
+
+run_rpm (__argv[1]);
+exit (0);
diff --git a/libslang/slsh/scripts/mv b/libslang/slsh/scripts/mv
new file mode 100755
index 0000000..47bc25f
--- /dev/null
+++ b/libslang/slsh/scripts/mv
@@ -0,0 +1,143 @@
+#!/usr/bin/env slsh
+% -*- slang -*-
+
+static variable Confirm_Move = 0;
+
+static define get_yn ()
+{
+ variable args = __pop_args (_NARGS);
+ () = fprintf (stdout, __push_args (args));
+ () = fflush (stdout);
+
+ variable yn;
+ if (fgets (&yn, stdin) <= 0)
+ return -1;
+
+ "y" == strlow (strtrim (yn));
+}
+
+
+static define move_file (from, to)
+{
+ if (from == to)
+ {
+ () = fprintf (stderr, "%s: Cannot move a file to itself.\n", __argv[0]);
+ return -1;
+ }
+
+ if (0 == rename (from, to))
+ return 0;
+
+ variable st = stat_file (to);
+
+ if (st != NULL)
+ {
+ if (1 != get_yn ("%s exists. Overwrite? [y/n]", to))
+ {
+ () = fputs ("Not Confirmed\n", stdout);
+ return -1;
+ }
+ () = remove (to);
+ }
+
+ if (0 == rename (from, to))
+ return 0;
+
+ ()=fprintf (stderr, "Failed to rename %s to %s: %s\n",
+ from, to, errno_string (errno));
+
+ return -1;
+}
+
+define move_files (from_files, to)
+{
+ variable st = stat_file (to);
+ if (st == NULL)
+ {
+ if (length (from_files) != 1)
+ {
+ () = fprintf (stderr, "%s must be a directory\n", to);
+ exit (1);
+ }
+ if (-1 == move_file (from_files[0], to))
+ exit (1);
+ exit (0);
+ }
+
+ !if (stat_is ("dir", st.st_mode))
+ {
+ if (length (from_files) != 1)
+ {
+ () = fprintf (stderr, "%s must be a directory\n", to);
+ exit (1);
+ }
+ if (-1 == move_file (from_files[0], to))
+ exit (1);
+ exit (0);
+ }
+
+
+ foreach (from_files)
+ {
+ variable old = ();
+ variable new = path_concat (to, path_basename (old));
+
+ if (NULL == stat_file (old))
+ {
+ () = fprintf (stderr, "Unable to access %s\n", old);
+ continue;
+ }
+
+ if (Confirm_Move)
+ {
+ if (1 != get_yn ("Move %s to %s/? [y/n]", old, to))
+ {
+ () = fputs ("Not Confirmed\n", stdout);
+ continue;
+ }
+ }
+
+
+ () = move_file (old, new);
+ }
+}
+
+static define usage ()
+{
+ () = fprintf (stdout, "Usage: %s [-i] files ... dir\n", __argv[0]);
+ exit (1);
+}
+
+define main (argc, argv)
+{
+ argc--;
+ argv = argv[[1:]];
+
+ while (argc > 1)
+ {
+ if (argv[0] == "-i")
+ {
+ Confirm_Move = 1;
+ argc--;
+ argv = argv[[1:]];
+ continue;
+ }
+ break;
+ }
+
+ if (argc < 2)
+ usage ();
+
+ move_files (argv[[0:argc-2]], argv[argc-1]);
+}
+
+
+define slsh_main ()
+{
+ main (__argc, __argv);
+}
+
+
+
+
+
diff --git a/libslang/slsh/scripts/purge b/libslang/slsh/scripts/purge
new file mode 100755
index 0000000..fae0e20
--- /dev/null
+++ b/libslang/slsh/scripts/purge
@@ -0,0 +1,65 @@
+#! /usr/bin/env slsh
+% -*- mode: slang -*-
+_debug_info = 1;
+
+static define purge_file (file, age, print_option)
+{
+ variable st = stat_file (file);
+ if (st == NULL)
+ {
+ () = fprintf (stderr, "stat %s failed: %s\n", file, errno_string (errno));
+ return;
+ }
+
+ if (st.st_ctime >= age)
+ return;
+
+ if (print_option)
+ {
+ () = fprintf (stdout, "%s\n", file);
+ return;
+ }
+
+ if (-1 == remove (file))
+ () = fprintf (stderr, "remove %s failed: %s\n", file, errno_string (errno));
+}
+
+static define purge_usage ()
+{
+ () = fprintf (stderr, "Usage: %s [-n] NUM-DAYS-OLD files...\n", __argv[0]);
+ () = fprintf (stderr, " Files older than NUM-DAYS-OLD be deleted.\n");
+ () = fprintf (stderr, " -n ==> Just print the files to be removed but do not remove them.\n");
+ exit (1);
+}
+
+static define main (argc, argv)
+{
+ variable age, i, print_option, file;
+
+ if (argc < 3) purge_usage ();
+
+ i = 2;
+ print_option = 0;
+ if (argv[1] == "-n")
+ {
+ i++;
+ print_option = 1;
+ if (argc < 4)
+ purge_usage ();
+ }
+
+ age = __argv[i-1];
+ if (String_Type == _slang_guess_type (age))
+ purge_usage ();
+
+ age = _time() - atof(age) * 24 * 3600;
+
+ foreach (argv[[i:]])
+ {
+ file = ();
+ purge_file (file, age, print_option);
+ }
+ exit (0);
+}
+
+main (__argc, __argv);