home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / git-4.3 / git-4 / git-4.3.7 / src / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-10  |  11.9 KB  |  553 lines

  1. /* misc.c -- miscelaneous functions used in git/gitps/gitview. I'll add here
  2.    anything that won't fit anywhere else. */
  3.  
  4. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Written by Tudor Hulubei and Andrei Pitis.  */
  21.  
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include <stdio.h>
  28.  
  29. #ifdef HAVE_STDLIB_H
  30. #include <stdlib.h>
  31. #else /* !HAVE_STDLIB_H */
  32. #include "ansi_stdlib.h"
  33. #endif /* !HAVE_STDLIB_H */
  34.  
  35. #include <signal.h>
  36.  
  37. #include <sys/types.h>
  38. #include <pwd.h>
  39.  
  40. #ifdef HAVE_UNISTD_H
  41. #include <unistd.h>
  42. #endif /* HAVE_UNISTD_H */
  43.  
  44. #include "xstring.h"
  45. #include "xmalloc.h"
  46. #include "configure.h"
  47. #include "file.h"
  48. #include "tty.h"
  49. #include "misc.h"
  50.  
  51.  
  52. static char CONFIGFILE_PREFIX[] = "/.gitrc.";
  53. static char term[] = TERM_DIRECTORY;
  54.  
  55. char  *tty_name;
  56. size_t tty_name_len;
  57. char  *login_name;
  58. size_t login_name_len;
  59.  
  60.  
  61. char *day_name[] =
  62. {
  63.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  64. };
  65.  
  66.  
  67. char *month_name[] =
  68. {
  69.     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  70.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  71. };
  72.  
  73.  
  74. /* The pointer to the head of the file type attributes list.  */
  75. file_type_info_t *fti_head = NULL;
  76.  
  77.  
  78. RETSIGTYPE
  79. fatal_signal(signum)
  80.     int signum;
  81. {
  82.     extern void clean_up __P(());
  83.  
  84.     clean_up();
  85.  
  86.     switch (signum)
  87.     {
  88.         case SIGTERM:
  89.         case SIGQUIT:
  90.  
  91.             display_exit_message((signum == SIGTERM) ? "TERM" : "QUIT");
  92.             break;
  93.  
  94.         case SIGHUP:
  95.         case SIGINT:
  96.  
  97.             display_exit_message((signum == SIGHUP) ? "HUP" : "INT");
  98.             break;
  99.  
  100.         case SIGSEGV:
  101.  
  102.             display_exit_message("SEGV");
  103.             goto ask_report;
  104.             break;
  105.  
  106.         default:
  107.  
  108.             fprintf(stderr,
  109.                     "%s: got a stupid signal (%d). Unless it was a joke ...\n",
  110.                     program, signum);
  111.           ask_report:
  112.             fprintf(stderr, "%s: please report to tudor@chang.pub.ro\n",
  113.                     program);
  114.             break;
  115.     }
  116.  
  117.     exit(signum);
  118. }
  119.  
  120.  
  121. void
  122. display_exit_message(signame)
  123.     char *signame;
  124. {
  125.     struct tm *time = get_local_time();
  126.  
  127.     fprintf(stderr, "%s %d %2d:%02d:%02d %s[%d]: exiting on %s signal\n",
  128.             month_name[time->tm_mon], time->tm_mday, time->tm_hour,
  129.             time->tm_min, time->tm_sec, program, (int)pid, signame);
  130. }
  131.  
  132.  
  133. void
  134. configuration_fatal_error(configfile)
  135.     char *configfile;
  136. {
  137.     fprintf(stderr, "%s: installation problem: \n", program);
  138.     fprintf(stderr, "%s: cannot find configuration file '%s'.\n\n", program, configfile);
  139. }
  140.  
  141.  
  142. void
  143. configuration_warning(configfile)
  144.     char *configfile;
  145. {
  146.     fprintf(stderr, "\n%s: cannot open configuration file '%s'.\n",
  147.             program, configfile);
  148.     fprintf(stderr, "%s: See the manual page for details.\n", program);
  149.     fprintf(stderr, "%s: If the TERM environment variable is, let's say, vt102, your\n",
  150.             program);
  151.     fprintf(stderr, "%s: configuration file name should be '.gitrc.vt102'.\n",
  152.             program);
  153.     fprintf(stderr, "%s: You can copy a configuration file in your home directory to\n",
  154.             program);
  155.     fprintf(stderr, "%s: overwrite the default one. \n", program);
  156.     fprintf(stderr, "%s: Try modifying '.gitrc.generic'...\n\n", program);
  157. }
  158.  
  159.  
  160. void
  161. common_configuration_init()
  162. {
  163.     int config_ok;
  164.  
  165.     char *configfile = xmalloc(strlen(term) + 1 + strlen(CONFIGFILE_PREFIX) +
  166.                                strlen(tty_type) + 1);
  167.     strcpy(configfile, term);
  168.     strcat(configfile, CONFIGFILE_PREFIX);
  169.     strcat(configfile, "common");
  170.  
  171.     config_ok = configuration_init(configfile);
  172.  
  173.     if (!config_ok)
  174.     {
  175.         configuration_fatal_error(configfile);
  176.         exit(1);
  177.     }
  178. }
  179.  
  180.  
  181. void
  182. specific_configuration_init()
  183. {
  184.     int config_ok;
  185.  
  186.     char *configfile = xmalloc(strlen(home) + 1 + strlen(CONFIGFILE_PREFIX) +
  187.                                strlen(tty_type) + 1);
  188.     strcpy(configfile, home);
  189.     strcat(configfile, CONFIGFILE_PREFIX);
  190.     strcat(configfile, tty_type);
  191.  
  192.     config_ok = configuration_init(configfile);
  193.  
  194.     if (!config_ok)
  195.     {
  196.         xfree(configfile);
  197.         configfile = xmalloc(strlen(term) + 1 + strlen(CONFIGFILE_PREFIX) +
  198.                              strlen(tty_type) + 1);
  199.         strcpy(configfile, term);
  200.         strcat(configfile, CONFIGFILE_PREFIX);
  201.         strcat(configfile, tty_type);
  202.  
  203.         config_ok = configuration_init(configfile);
  204.  
  205.         if (!config_ok)
  206.         {
  207.             configuration_warning(configfile);
  208.  
  209.             xfree(configfile);
  210.             configfile = xmalloc(strlen(term) + 1 + strlen(CONFIGFILE_PREFIX) +
  211.                                  10 + 1);
  212.             strcpy(configfile, term);
  213.             strcat(configfile, CONFIGFILE_PREFIX);
  214.             strcat(configfile, "generic");
  215.  
  216.             config_ok = configuration_init(configfile);
  217.  
  218.             if (!config_ok)
  219.             {
  220.                 configuration_fatal_error(configfile);
  221.                 exit(1);
  222.             }
  223.         }
  224.     }
  225. }
  226.  
  227.  
  228. void
  229. use_section(section)
  230.     char *section;
  231. {
  232.     if (configuration_section(section) == -1)
  233.     {
  234.         fprintf(stderr,"%s: can't find section %s in the configuration file.\n",
  235.                 program, section);
  236.         exit(1);
  237.     }
  238. }
  239.  
  240.  
  241. int
  242. get_int_var(var_name, default_value)
  243.     char *var_name;
  244.     int default_value;
  245. {
  246.     char *data;
  247.  
  248.     configuration_getvarinfo(var_name, &data, 1, DO_SEEK);
  249.  
  250.     return data ? atoi(data) : default_value;
  251. }
  252.  
  253.  
  254. int
  255. get_const_var(var_name, options, options_no, default_value)
  256.     char *var_name, *options[];
  257.     int options_no, default_value;
  258. {
  259.     int i;
  260.     char *data;
  261.  
  262.     configuration_getvarinfo(var_name, &data, 1, DO_SEEK);
  263.  
  264.     if (data)
  265.     {
  266.         for (i = 0; i < options_no; i++)
  267.             if (strcmp(data, options[i]) == 0)
  268.                 break;
  269.  
  270.         if (i == options_no)
  271.             fprintf(stderr, "%s: invalid %s (%s).\n", program, var_name, data);
  272.         else
  273.             return i;
  274.     }
  275.  
  276.     return default_value;
  277. }
  278.  
  279.  
  280. int
  281. get_flag_var(var_name, default_value)
  282.     char *var_name;
  283.     int default_value;
  284. {
  285.     char *data;
  286.  
  287.     configuration_getvarinfo(var_name, &data, 1, DO_SEEK);
  288.  
  289.     if (data)
  290.     {
  291.         if (strcmp(data, "ON")  == 0) return 1;
  292.         if (strcmp(data, "OFF") == 0) return 0;
  293.         fprintf(stderr, "%s: invalid %s (%s).\n", program, var_name, data);
  294.         return default_value;
  295.     }
  296.  
  297.     return default_value;
  298. }
  299.  
  300.  
  301. char
  302. *get_string_var(var_name, default_value)
  303.     char *var_name, *default_value;
  304. {
  305.     char *data;
  306.  
  307.     configuration_getvarinfo(var_name, &data, 1, DO_SEEK);
  308.  
  309.     if (data)
  310.         return xstrdup(data);
  311.  
  312.     return default_value;
  313. }
  314.  
  315.  
  316. void
  317. get_colorset_var(charset, colorset_name, fields_no)
  318.     int *charset;
  319.     char *colorset_name[];
  320.     int fields_no;
  321. {
  322.     int i, index;
  323.     char *data;
  324.  
  325.     for (i = 0; i < fields_no; i++)
  326.     {
  327.         configuration_getvarinfo(colorset_name[i], &data, 1, DO_SEEK);
  328.  
  329.         if (data)
  330.         {
  331.             index = tty_get_color_index(data);
  332.             if (index == -1)
  333.                 fprintf(stderr, "%s: invalid %s (%s).\n",
  334.                         program, colorset_name[i], data);
  335.             else
  336.                 charset[i] = index;
  337.         }
  338.     }
  339. }
  340.  
  341.  
  342. char
  343. *clear_path(path)
  344.     char *path;
  345. {
  346.     char *cpath = path;
  347.     char *opath = path;
  348.  
  349.     if (*opath == 0) return path;
  350.     if (*opath == '/') *cpath++ = *opath++;
  351.  
  352.     while (*opath)
  353.     {
  354.         while (*opath == '/' || 
  355.                (*opath == '.' && (*(opath + 1) == '/' || *(opath + 1) == 0)))
  356.             opath++;
  357.         while (*opath && *opath != '/') *cpath++ = *opath++;
  358.         if (*opath) *cpath++ = '/';
  359.     }
  360.     if (*(cpath - 1) == '/' && cpath - path > 1) cpath--;
  361.     *cpath = 0;
  362.     return path;
  363. }
  364.  
  365.  
  366. void
  367. get_tty_name()
  368. {
  369.     if ((tty_name = ttyname(1)) == NULL)
  370.     {
  371.         fprintf(stderr, "%s: can't get terminal name.\n", program);
  372.         exit(1);
  373.     }
  374.  
  375.     tty_name_len = strlen(tty_name);
  376. }
  377.  
  378.  
  379. void
  380. get_login_name()
  381. {
  382.     struct passwd *pwd;
  383.     int euid = geteuid();
  384.  
  385.     if ((pwd = getpwuid(euid)) == NULL)
  386.     {
  387.         fprintf(stderr,
  388.                 "%s: OOOPS, I can't get your user name (euid = %d) !\n",
  389.                 program, euid);
  390.         fprintf(stderr,
  391.                 "%s: Your account no longer exists or you are a SYSTEM CRACKER !! :-)\n",
  392.                 program);
  393.         fprintf(stderr, "%s: Correct the problem and try again.\n", program);
  394.         exit(1);
  395.     }
  396.  
  397.     login_name     = xstrdup(pwd->pw_name);
  398.     login_name_len = strlen(login_name);
  399. }
  400.  
  401.  
  402. void
  403. truncate_long_name(name, dest, len)
  404.     char *name, *dest;
  405.     size_t len;
  406. {
  407.     size_t name_len;
  408.  
  409.     if ((name_len = strlen(name)) > len)
  410.     {
  411.         dest[0] = dest[1] = dest[2] = '.';
  412.         memcpy(dest + 3, name + name_len - len + 3, len - 3);
  413.     }
  414.     else
  415.         memcpy(dest, name, name_len);
  416. }
  417.  
  418.  
  419. char *
  420. truncate_string(path, temppath, len)
  421.     char *path;
  422.     char *temppath;
  423.     size_t len;
  424. {
  425.     truncate_long_name(path, temppath, len);
  426.     temppath[min(len, strlen(path))] = 0;
  427.     return temppath;
  428. }
  429.  
  430.  
  431. int
  432. get_file_length(handle)
  433.     int handle;
  434. {
  435.     int tmp, length;
  436.     
  437.     tmp    = lseek(handle, 0, SEEK_CUR);
  438.     length = lseek(handle, 0, SEEK_END);
  439.     lseek(handle, tmp, SEEK_SET);
  440.     return length;
  441. }
  442.  
  443.  
  444. struct tm *
  445. get_local_time()
  446. {
  447.     time_t __time;
  448.  
  449.     /* Get the broken-down time representation.  */
  450.     __time = time(NULL);
  451.     return localtime(&__time);
  452. }
  453.  
  454.  
  455. char *
  456. xbasename(name)
  457.     char *name;
  458. {
  459.     char *base;
  460.     size_t len = strlen(name);
  461.  
  462.     if (name[len - 1] == '/')
  463.         name[len - 1] = '\0';
  464.  
  465.     base = strrchr(name, '/');
  466.     return base ? base + 1 : name;
  467. }
  468.  
  469.  
  470. int
  471. is_a_bg_command(cmd)
  472.     char *cmd;
  473. {
  474.     int i;
  475.  
  476.     /* Check if it is a background command.  We are doing this by looking
  477.        for a '&' at the end of the command string.  */
  478.  
  479.     for (i = strlen(cmd) - 1; i >= 0; i--)
  480.     {
  481.         if (cmd[i] == '&')
  482.             return 1;
  483.  
  484.         /* Skip spaces and tabs.  */
  485.         if (cmd[i] != ' ' && cmd[i] != key_TAB)
  486.             return 0;
  487.     }
  488.  
  489.     /* No '&' found.  */
  490.     return 0;
  491. }
  492.  
  493.  
  494. void
  495. get_file_type_info()
  496. {
  497.     char *contents[3];
  498.     unsigned char regexp[80];
  499.     int brightness, foreground, background;
  500.     file_type_info_t *previous = NULL, *fti;
  501.     char *error_message = "%s: '%s': invalid or missing %s.\n";
  502.  
  503.  
  504.     for (;;)
  505.     {
  506.         configuration_getvarinfo((char *)regexp, contents, 3, NO_SEEK);
  507.  
  508.         if (*regexp == 0)
  509.             break;
  510.  
  511.         if (contents[0])
  512.             foreground = tty_get_color_index(contents[0]);
  513.         else
  514.         {
  515.         fprintf(stderr, error_message, program, regexp,"foreground color");
  516.             continue;
  517.         }
  518.  
  519.         if (contents[1])
  520.             background = tty_get_color_index(contents[1]);
  521.         else
  522.         {
  523.         fprintf(stderr, error_message, program, regexp,"background color");
  524.             continue;
  525.         }
  526.  
  527.         if (contents[2])
  528.             brightness = tty_get_color_index(contents[2]);
  529.         else
  530.         {
  531.         fprintf(stderr, error_message, program, regexp,"brightness");
  532.             continue;
  533.         }
  534.  
  535.     /* Insert the just obtained file type entry into the list.  */
  536.  
  537.     fti = (file_type_info_t *)xmalloc(sizeof(file_type_info_t));
  538.  
  539.     if (fti_head == NULL)
  540.         fti_head = previous = fti;
  541.     else
  542.         previous->next = fti;
  543.  
  544.     fti->regexp     = xstrdup(regexp);
  545.     fti->foreground = foreground;
  546.     fti->background = background;
  547.     fti->brightness = brightness;
  548.     fti->next       = NULL;
  549.  
  550.         previous = fti;
  551.     }
  552. }
  553.