Thread: [Cryptar-cvs] cryptar prefs.c,1.23,1.24
Status: Beta
Brought to you by:
jma2
From: Jeff A. <jm...@us...> - 2004-05-29 04:29:44
|
Update of /cvsroot/cryptar/cryptar In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22057 Modified Files: prefs.c Log Message: Preference values are stored in database. Remove old config file code Index: prefs.c =================================================================== RCS file: /cvsroot/cryptar/cryptar/prefs.c,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** prefs.c 28 May 2004 18:12:46 -0000 1.23 --- prefs.c 29 May 2004 04:29:35 -0000 1.24 *************** *** 25,28 **** --- 25,29 ---- #include "constants.h" + #include "db_misc.h" #include "option.h" #include "options.h" *************** *** 61,64 **** --- 62,66 ---- + #define YESNO_SIZE 10 void do_create() { *************** *** 68,76 **** char *daemon_command, *to_archive, *block_length; int n_block_length; ! char buf[11]; /* used in scanf below, length below ! must not exceed one less than this. ! */ /* ### Good target for I18N ### */ daemon_command = get_pref_from_user("daemon command", "This is the local command to use to talk to the remote cryptar server.\nTypically it will be \"ssh hostname cryptar --daemon\", where you replace hostname\nwith the name of the host with which you're communicating.\n", "ssh host cryptar --daemon"); to_archive = get_pref_from_user("files to archive", "This is a comma-separated list of files to archive.", getenv("HOME")); --- 70,78 ---- char *daemon_command, *to_archive, *block_length; int n_block_length; ! char buf[YESNO_SIZE]; /* ### Good target for I18N ### */ + printf("\nThis will lead you through the steps to create a new archive.\n"); + daemon_command = get_pref_from_user("daemon command", "This is the local command to use to talk to the remote cryptar server.\nTypically it will be \"ssh hostname cryptar --daemon\", where you replace hostname\nwith the name of the host with which you're communicating.\n", "ssh host cryptar --daemon"); to_archive = get_pref_from_user("files to archive", "This is a comma-separated list of files to archive.", getenv("HOME")); *************** *** 89,94 **** printf("block length = %d\n", n_block_length); printf("\nOK? [Y]es/no "); ! scanf("%10s", buf); ! if(buf[0] == 'y' || buf[0] == 'Y') { /* We could be more polite, we could offer to edit, etc. */ printf("Sorry, try again when you know what you want.\n"); --- 91,96 ---- printf("block length = %d\n", n_block_length); printf("\nOK? [Y]es/no "); ! fgets(buf, YESNO_SIZE, stdin); ! if(buf[0] != 'y' && buf[0] != 'Y') { /* We could be more polite, we could offer to edit, etc. */ printf("Sorry, try again when you know what you want.\n"); *************** *** 96,99 **** --- 98,102 ---- } + db_open(kConstants); const_put_string(PREF_DAEMON_COMMAND, daemon_command); const_put_string(PREF_TO_ARCHIVE, to_archive); *************** *** 108,124 **** static char *get_pref_from_user(const char *name, const char *description, const char *default_val) { ! char buf[10241]; /* length is repeated in scanf spec ! below and nowhere else, must be one ! more here than below due to ! terminating NULL. */ ! g_assert(name); printf("\n\n"); if(description) printf("%s\n", description); ! printf("%s [%s]:", name, default_val); ! scanf("%10240s", buf); return g_strdup(buf); } --- 111,128 ---- + #define BUFSIZE 10240 static char *get_pref_from_user(const char *name, const char *description, const char *default_val) { ! char buf[BUFSIZE]; ! g_assert(name); printf("\n\n"); if(description) printf("%s\n", description); ! printf("%s [%s]: ", name, default_val); ! fgets(buf, BUFSIZE, stdin); ! g_strchomp(buf); ! if(buf[0] == '\0') ! return g_strdup(default_val); return g_strdup(buf); } *************** *** 183,291 **** - - - #if 0 - /* Functions to read preferences from the preference file. - */ - - - /* Determine the name of the preferences file, then read preferences - into the pref tree. - - This function is a major hack, see notes at pref_parse_prefs. - */ - void pref_get_prefs() - { - char *file; - FileContents cont; - - file = g_strconcat(getenv("HOME"), "/.cryptar", NULL); - cont = pref_read_pref_file(file); - prefs = g_tree_new(PrefCompare); - #if 0 - pref_parse_prefs(cont); - #endif - return; - } - - - - /* Tree ordering callback */ - static gint PrefCompare(gconstpointer a, gconstpointer b) - { - return strcmp(a, b); - } - - - - /* Read file to a buffer so we can parse it. Buffer should not be - freed, we just point the parse tree into the cont.buf buffer. - */ - static FileContents pref_read_pref_file(const char *file) - { - FILE *fp; - struct stat sbuf; - FileContents ret; - g_assert(file); - - if(stat(file, &sbuf)) { - perror("stat of pref file failed"); - g_error(" ==> Can't read file %s", file); - } - ret.len = sbuf.st_size; - fp = fopen(file, "r"); - g_assert(fp); - ret.buf = g_malloc(ret.len); - if(fread(ret.buf, 1, ret.len, fp) < ret.len) - g_error("Failed to read preference file '%s'\n", file); - return ret; - } - - - - /* Records are delimited by '\n', fields by the first '=' in the - record. Write a '\0' to the buffer to terminate the strings rather - than copying things about. - - This should either be moved to the local db or else expanded to - allow comments and backslash escapes. We should trim - spaces. Probably more. - - In addition, we need to support preferences for multiple archive - here, keyed on the archive name the user specifies on the command - line. - */ - static void pref_parse_prefs(FileContents cont) - { - guint i; - gchar *key; - gchar *value; - - g_assert(prefs); - key = &cont.buf[0]; - value = NULL; - for(i = 0; i < cont.len; i++) { - if(cont.buf[i] == '=' && !value) { - cont.buf[i] = '\0'; - value = &cont.buf[i+1]; - } else if(cont.buf[i] == '\n') { - if(!value) - g_error("Error in pref file, character %d: no '=' found in line.\n", i); - if((value == key) || (&cont.buf[i] == value)) - g_error("Error in pref file, character %d: key or value is empty.\n", i); - g_assert(key); - g_assert(value); - cont.buf[i] = '\0'; - g_tree_insert(prefs, key, value); - #if 0 - /* debug */ - g_message("Inserted in tree: key = '%s', value = '%s'", - key, value); - #endif - key = &cont.buf[i + 1]; - value = NULL; - } - } - } - #endif /* 0 */ - --- 187,188 ---- |