C Version: add the help message, and make it work by default with ~/.tt
authorviric <viriketo@gmail.com>
Mon, 15 Mar 2010 23:22:50 +0100
changeset 10 29610784a302
parent 9 d571ba4d952c
child 11 4c3c80ec2180
C Version: add the help message, and make it work by default with ~/.tt
tt.c
--- a/tt.c	Mon Mar 15 23:08:57 2010 +0100
+++ b/tt.c	Mon Mar 15 23:22:50 2010 +0100
@@ -10,11 +10,27 @@
 static FILE *
 open_file(const char *attr)
 {
-    const char *name = getenv("TT_PROJECT");
-    if (!name)
+    char *name = getenv("TT_PROJECT");
+    if (name)
+    {
+        name = strdup(name);
+    }
+    else
     {
-        fprintf(stderr, "You have to point TT_PROJECT to a file\n");
-        exit(1);
+        const char *home = getenv("HOME");
+        const char *filename = "/.tt";
+        int len;
+
+        if (!home)
+        {
+            fprintf(stderr, "You should have $HOME or $TT_PROJECT set.\n");
+            exit(1);
+        }
+
+        len = strlen(filename) + strlen(home) + 1;
+        name = malloc(len);
+
+        snprintf(name, len, "%s%s", home, filename);
     }
 
     FILE *f;
@@ -23,9 +39,12 @@
     {
         fprintf(stderr, "Could not open the file: %s (%s)", name,
                 strerror(errno));
+        free(name);
         exit(1);
     }
 
+    free(name);
+
     return f;
 }
 
@@ -156,7 +175,7 @@
             memcpy(buf, buf+ret, n);
         }
     }
-    while(n > 0);
+    while(!feof(f));
     fclose(f);
 }
 
@@ -318,6 +337,21 @@
     free(str_hms);
 }
 
+void show_help(const char *prog)
+{
+    printf(
+	    "Usage: %s [-d] [-l] [-t]\n"
+        "       %s <task>\n"
+        "Manual:\n"
+        "  Switch to a task         :  tt mytask\n"
+        "  List task switches       :  tt -l\n"
+        "  List time spent per task :  tt -t\n"
+        "  Sum times given with -t  :  tt -t | grep ... | tt -s\n"
+        "  List what is tracked     :  tt\n"
+        "  $TT_PROJECT or ~/.tt stores the tracking.\n"
+        , prog, prog);
+}
+
 int main(int argc, char **argv)
 {
     if (argc >= 2)
@@ -325,6 +359,8 @@
             list_task_times();
         else if (strcmp(argv[1], "-s") == 0)
             sum_times();
+        else if (strcmp(argv[1], "-h") == 0)
+            show_help(argv[0]);
         else
             new_task(argv[1]);
     else