wd
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
wd.c
Go to the documentation of this file.
1 /*
2  Copyright 2013 John Bailey
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #include "cmdln.h"
18 #include "dir_list.h"
19 
20 #include <stdlib.h>
21 #include <stdio.h>
22 
23 #if defined _WIN32
24 #include <io.h>
25 #include <fcntl.h>
26 #endif
27 
28 static int do_remove( const config_container_t* const p_config, const char* cmd, dir_list_t p_dir_list )
29 {
30  int success = 0;
31  size_t index = 0;
32  int ret_val = 0;
33 
34  if( p_config->wd_prompt )
35  {
36  dump_dir_list( p_dir_list );
37  fprintf(stdout,"Enter number to remove: ");
38 
39  /* Flush the output to ensure that the user knows that we're waiting for
40  some input. This is necessary when, for example, running the Win32
41  version of this app within MinTTy on Cygwin */
42  fflush(stdout);
43 
44  success = fscanf(stdin,"%u",&index);
45  if( success )
46  {
47  success = remove_dir_by_index( p_dir_list, index );
48  }
49  }
50  else
51  {
52  success = remove_dir( p_dir_list, p_config->wd_oper_dir );
53  }
54 
55  if( success )
56  {
57  ret_val = 1;
58  }
59  else
60  {
61  if( p_config->wd_prompt )
62  {
63  fprintf(stderr,
64  "%s: Error: Invalid index '%d'\n",
65  cmd, index);
66  }
67  else
68  {
69  fprintf(stderr,
70  "%s: Warning: Directory not in list: '%s'\n",
71  cmd, p_config->wd_oper_dir);
72  }
73  }
74 
75  return ret_val;
76 }
77 
78 int main( int argc, char* argv[] )
79 {
80  int ret_code = 0;
81 
83 
84  init_cmdln( &cfg );
85 
86  if( process_env( &cfg ) &&
87  process_cmdln( &cfg, argc, argv ) ) {
88 
89  DEBUG_OUT("command line processed");
90 
91  if( cfg.wd_oper != WD_OPER_NONE ) {
92 
93  int dir_list_needs_save = 0;
94 
95  dir_list_t dir_list = NULL;
96 
97  DEBUG_OUT("loading bookmark file %s", cfg.list_fn);
98 
99  dir_list = load_dir_list( &cfg, cfg.list_fn );
100 
101  if( dir_list == NULL ) {
102  fprintf(stderr,"%s: Warning: Unable to load list file '%s'\n",
103  argv[0], cfg.list_fn);
104  dir_list = new_dir_list();
105  }
106 
107  DEBUG_OUT("loaded bookmark file");
108 
109  switch( cfg.wd_oper ) {
110  case WD_OPER_REMOVE:
111  dir_list_needs_save = do_remove( &cfg, argv[0], dir_list );
112  break;
113  case WD_OPER_GET:
114  DEBUG_OUT("WD_OPER_GET: %s",cfg.wd_bookmark_name);
115 
116  if(( dump_dir_with_name( dir_list, cfg.wd_bookmark_name ) ||
117  dump_dir_if_exists( dir_list, cfg.wd_bookmark_name )) && cfg.wd_store_access ) {
118  /* We're updating access times, so flag that the list needs
119  saving */
120  dir_list_needs_save = 1;
121  }
122  break;
124  DEBUG_OUT("WD_OPER_GET_BY_BM_NAME: %s",cfg.wd_bookmark_name);
125 
126  if( dump_dir_with_name( dir_list, cfg.wd_bookmark_name ) && cfg.wd_store_access ) {
127  /* We're updating access times, so flag that the list needs
128  saving */
129  dir_list_needs_save = 1;
130  }
131  break;
132  case WD_OPER_ADD:
133  /* TODO: Consider allowing directory to be added twice with
134  different bookmark name? */
135  if( dir_in_list( dir_list, cfg.wd_oper_dir )) {
136  fprintf(stderr,
137  "%s: Warning: Directory already in list: '%s'\n",
138  argv[0], cfg.wd_oper_dir);
139  } else if( (cfg.wd_bookmark_name != NULL) &&
140  bookmark_in_list( dir_list, cfg.wd_bookmark_name )) {
141  fprintf(stderr,
142  "%s: Warning: Bookmark name already in list: '%s'\n",
143  argv[0], cfg.wd_bookmark_name);
144  } else {
145  time_t a_time = -1;
146  if( cfg.wd_store_access ) {
147  a_time = cfg.wd_now_time;
148  }
149  add_dir( dir_list,
150  cfg.wd_oper_dir,
151  cfg.wd_bookmark_name,
152  cfg.wd_now_time,
153  a_time,
155  dir_list_needs_save = 1;
156  }
157  break;
158  case WD_OPER_DUMP:
159  dump_dir_list( dir_list );
160  break;
161  case WD_OPER_LIST:
162 #if defined _WIN32
163  _setmode(1,_O_BINARY);
164 #endif
165  /* TODO: This won't update the access time - don't
166  necessarily want to because we might be listing out a lot
167  of directories. Probably want a call-back from the shell
168  script */
169  list_dirs( dir_list );
170  break;
171  default:
172  fprintf(stderr,"Unhandled operation type\n");
173  break;
174  }
175  if( dir_list_needs_save ) {
176  save_dir_list( dir_list, cfg.list_fn );
177  }
178  }
179  /* TODO: be more selective about setting this */
180  ret_code = EXIT_SUCCESS;
181  } else {
182  ret_code = EXIT_FAILURE;
183  }
184 
185  DEBUG_OUT("all done");
186  return ret_code;
187 }
int dir_in_list(dir_list_t p_list, const char *const p_dir)
Definition: dir_list.c:467
char * wd_bookmark_name
Name of a bookmark read from the command line on which operations should be performed.
Definition: cmdln.h:59
void list_dirs(const dir_list_t p_list)
Definition: dir_list.c:635
#define DEBUG_OUT(...)
Definition: cmdln.h:86
int dump_dir_with_name(const dir_list_t p_list, const char *const p_name)
Definition: dir_list.c:580
time_t wd_now_time
Time to use as the current time when manipulating datestamps.
Definition: cmdln.h:68
struct dir_list_item * dir_list
Definition: dir_list.c:67
The dir_list module provides functions to manipulate and search the list of bookmarks.
wd_oper_t wd_oper
Type of operation which the command line has instructed should be performed.
Definition: cmdln.h:49
void init_cmdln(config_container_t *const p_config)
Initialise the specified config with default values.
Definition: cmdln.c:86
int dump_dir_if_exists(const dir_list_t p_list, const char *const p_dir)
Definition: dir_list.c:547
char * list_fn
Directory containing list of bookmarks.
Definition: cmdln.h:53
int process_cmdln(config_container_t *const p_config, const int argc, char *const argv[])
Definition: cmdln.c:378
char wd_oper_dir[MAXPATHLEN]
Directory read from the command line upon which operations should be performed.
Definition: cmdln.h:56
dir_list_t load_dir_list(const config_container_t *const p_config, const char *const p_fn)
Load a set of bookmarks from the specified file.
Definition: dir_list.c:213
int bookmark_in_list(dir_list_t p_list, const char *const p_name)
Definition: dir_list.c:399
const config_container_t * cfg
Definition: dir_list.c:72
int save_dir_list(const dir_list_t p_list, const char *p_fn)
Definition: dir_list.c:822
int remove_dir_by_index(dir_list_t p_list, const size_t p_dir)
Definition: dir_list.c:437
int add_dir(dir_list_t p_list, const char *const p_dir, const char *const p_name, const time_t p_t_added, const time_t p_t_accessed, const wd_entity_t p_type)
Definition: dir_list.c:96
int wd_store_access
Indicate whether or not access times should be stored in the bookmarks.
Definition: cmdln.h:64
void dump_dir_list(const dir_list_t p_list)
Definition: dir_list.c:734
int wd_prompt
Indicate whether or not to run in &quot;interactive&quot; mode.
Definition: cmdln.h:61
int main(int argc, char *argv[])
Definition: wd.c:78
int process_env(config_container_t *const p_config)
Definition: cmdln.c:297
int remove_dir(dir_list_t p_list, const char *const p_dir)
Definition: dir_list.c:455
dir_list_t new_dir_list(void)
Create a new directory list structure.
Definition: dir_list.c:156