comparison src/t-builtins.cc @ 266:818be8915438

[project @ 1994-01-03 23:06:07 by jwe]
author jwe
date Mon, 03 Jan 1994 23:07:25 +0000
parents 0a1644ef9a0a
children 12010b8263ca
comparison
equal deleted inserted replaced
265:221a5fa8bf01 266:818be8915438
28 Software Foundation, Inc. 28 Software Foundation, Inc.
29 29
30 The function list_in_columns was adapted from a similar function from 30 The function list_in_columns was adapted from a similar function from
31 GNU ls, print_many_per_line, copyright (C) 1985, 1988, 1990, 1991 Free 31 GNU ls, print_many_per_line, copyright (C) 1985, 1988, 1990, 1991 Free
32 Software Foundation, Inc. 32 Software Foundation, Inc.
33
34 The function glob_pattern_p was taken from the file glob.c distributed
35 with GNU Bash, the Bourne Again SHell, copyright (C) 1985, 1988, 1989
36 Free Software Foundation, Inc.
33 37
34 */ 38 */
35 39
36 #ifdef HAVE_CONFIG_H 40 #ifdef HAVE_CONFIG_H
37 #include "config.h" 41 #include "config.h"
49 #include <sys/file.h> 53 #include <sys/file.h>
50 #include <sys/stat.h> 54 #include <sys/stat.h>
51 #include <time.h> 55 #include <time.h>
52 #include <errno.h> 56 #include <errno.h>
53 #include <signal.h> 57 #include <signal.h>
54 #include <String.h>
55 58
56 #include "procstream.h" 59 #include "procstream.h"
57 60
58 #include "variables.h" 61 #include "variables.h"
59 #include "symtab.h" 62 #include "symtab.h"
70 #include "pr-output.h" 73 #include "pr-output.h"
71 #include "defaults.h" 74 #include "defaults.h"
72 #include "tree.h" 75 #include "tree.h"
73 #include "help.h" 76 #include "help.h"
74 77
78 extern "C"
79 {
80 #include "fnmatch.h"
81 }
82
75 // May need replacement for this on some machines. 83 // May need replacement for this on some machines.
76 extern "C" 84 extern "C"
77 { 85 {
78 extern char *strerror (int); 86 extern char *strerror (int);
79 char *tilde_expand (char *s); /* From readline's tilde.c */ 87 char *tilde_expand (char *s); /* From readline's tilde.c */
276 while (--argc > 0) 284 while (--argc > 0)
277 { 285 {
278 argv++; 286 argv++;
279 if (*argv != (char *) NULL) 287 if (*argv != (char *) NULL)
280 { 288 {
281 Regex rx (*argv);
282
283 int i; 289 int i;
284 for (i = 0; i < lcount; i++) 290 for (i = 0; i < lcount; i++)
285 { 291 {
286 String nm (lvars[i]); 292 if (fnmatch (*argv, lvars[i], __FNM_FLAGS) == 0)
287 if (nm.matches (rx))
288 curr_sym_tab->clear (lvars[i]); 293 curr_sym_tab->clear (lvars[i]);
289 } 294 }
290 295
291 int count; 296 int count;
292 for (i = 0; i < gcount; i++) 297 for (i = 0; i < gcount; i++)
293 { 298 {
294 String nm (gvars[i]); 299 if (fnmatch (*argv, gvars[i], __FNM_FLAGS) == 0)
295 if (nm.matches (rx))
296 { 300 {
297 count = curr_sym_tab->clear (gvars[i]); 301 count = curr_sym_tab->clear (gvars[i]);
298 if (count > 0) 302 if (count > 0)
299 global_sym_tab->clear (gvars[i], clear_user_functions); 303 global_sym_tab->clear (gvars[i], clear_user_functions);
300 } 304 }
301 } 305 }
302 306
303 for (i = 0; i < fcount; i++) 307 for (i = 0; i < fcount; i++)
304 { 308 {
305 String nm (fcns[i]); 309 if (fnmatch (*argv, fcns[i], __FNM_FLAGS) == 0)
306 if (nm.matches (rx))
307 { 310 {
308 count = curr_sym_tab->clear (fcns[i]); 311 count = curr_sym_tab->clear (fcns[i]);
309 if (count > 0) 312 if (count > 0)
310 global_sym_tab->clear (fcns[i], clear_user_functions); 313 global_sym_tab->clear (fcns[i], clear_user_functions);
311 } 314 }
811 do_run_history (argc, argv); 814 do_run_history (argc, argv);
812 return retval; 815 return retval;
813 } 816 }
814 817
815 /* 818 /*
819 * Return nonzero if PATTERN has any special globbing chars in it.
820 */
821 static int
822 glob_pattern_p (char *pattern)
823 {
824 char *p = pattern;
825 char c;
826 int open = 0;
827
828 while ((c = *p++) != '\0')
829 {
830 switch (c)
831 {
832 case '?':
833 case '*':
834 return 1;
835
836 case '[': // Only accept an open brace if there is a close
837 open++; // brace to match it. Bracket expressions must be
838 continue; // complete, according to Posix.2
839
840 case ']':
841 if (open)
842 return 1;
843 continue;
844
845 case '\\':
846 if (*p++ == '\0')
847 return 0;
848
849 default:
850 continue;
851 }
852 }
853
854 return 0;
855 }
856
857 /*
816 * Write variables to an output stream. 858 * Write variables to an output stream.
817 */ 859 */
818 tree_constant 860 tree_constant
819 builtin_save (int argc, char **argv) 861 builtin_save (int argc, char **argv)
820 { 862 {
834 if (strcmp (*argv, "-") == 0) 876 if (strcmp (*argv, "-") == 0)
835 { 877 {
836 // XXX FIXME XXX -- things intended for the screen should end up in a 878 // XXX FIXME XXX -- things intended for the screen should end up in a
837 // tree_constant (string)? 879 // tree_constant (string)?
838 stream = cout; 880 stream = cout;
881 }
882 else if (argc == 1 && glob_pattern_p (*argv)) // Guard against things
883 { // like `save a*',
884 print_usage ("save"); // which are probably
885 return retval; // mistakes...
839 } 886 }
840 else 887 else
841 { 888 {
842 char *fname = tilde_expand (*argv); 889 char *fname = tilde_expand (*argv);
843 file.open (fname); 890 file.open (fname);
869 argv++; 916 argv++;
870 917
871 int count; 918 int count;
872 char **lvars = curr_sym_tab->list (count, 0, 919 char **lvars = curr_sym_tab->list (count, 0,
873 symbol_def::USER_VARIABLE); 920 symbol_def::USER_VARIABLE);
874 Regex rx (*argv);
875 921
876 int saved_or_error = 0; 922 int saved_or_error = 0;
877 int i; 923 int i;
878 for (i = 0; i < count; i++) 924 for (i = 0; i < count; i++)
879 { 925 {
880 String nm (lvars[i]); 926 if (fnmatch (*argv, lvars[i], __FNM_FLAGS) == 0
881 if (nm.matches (rx)
882 && curr_sym_tab->save (stream, lvars[i]) != 0) 927 && curr_sym_tab->save (stream, lvars[i]) != 0)
883 saved_or_error++; 928 saved_or_error++;
884 } 929 }
885 930
886 char **bvars = global_sym_tab->list (count, 0, 931 char **bvars = global_sym_tab->list (count, 0,
887 symbol_def::BUILTIN_VARIABLE); 932 symbol_def::BUILTIN_VARIABLE);
888 933
889 for (i = 0; i < count; i++) 934 for (i = 0; i < count; i++)
890 { 935 {
891 String nm (bvars[i]); 936 if (fnmatch (*argv, bvars[i], __FNM_FLAGS) == 0
892 if (nm.matches (rx)
893 && global_sym_tab->save (stream, bvars[i]) != 0) 937 && global_sym_tab->save (stream, bvars[i]) != 0)
894 saved_or_error++; 938 saved_or_error++;
895 } 939 }
896 940
897 delete [] lvars; 941 delete [] lvars;