]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/getdate.*: Reimplement in pure C
authorAlejandro Colomar <alx@kernel.org>
Tue, 18 Feb 2025 13:52:04 +0000 (14:52 +0100)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Mon, 2 Jun 2025 07:59:51 +0000 (09:59 +0200)
This removes all yacc(1) code from this project.  Add copyright and
license, since there remains nothing of the original code.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
.github/actions/install-dependencies/action.yml
.gitignore
configure.ac
lib/Makefile.am
lib/getdate.c [new file with mode: 0644]
lib/getdate.y [deleted file]
share/ansible/roles/ci_run/tasks/alpine.yml
share/ansible/roles/ci_run/tasks/opensuse.yml

index 15c0ba352b89625e93300eaaa88b45a2b9faf53f..7dd52a71c09945230e780898253cac27e275c2bc 100644 (file)
@@ -22,5 +22,5 @@ runs:
       sudo apt-mark hold grub-efi-amd64-bin grub-efi-amd64-signed
       sudo apt-get update
       sudo apt-get -y dist-upgrade
-      sudo apt-get -y install ubuntu-dev-tools automake autopoint xsltproc gettext expect byacc libtool libbsd-dev libltdl-dev pkgconf
+      sudo apt-get -y install ubuntu-dev-tools automake autopoint xsltproc gettext expect libtool libbsd-dev libltdl-dev pkgconf
       sudo apt-get -y build-dep shadow
index 3d6d91c80c4edb34557c2b507f5d00664a8ef092..56693605a87d9c29ca8ec457c18c95656776f146 100644 (file)
@@ -52,7 +52,6 @@ test-driver
 
 /shadow.spec
 /shadow-*.tar.*
-/lib/getdate.c
 /libsubid/subid.h
 
 .venv
index 283097fe81ab3f241d3e163c34c37f25ea380161..186c1ea9a12c1efc4169df895122254df96d3f94 100644 (file)
@@ -30,7 +30,6 @@ AM_MAINTAINER_MODE
 dnl Checks for programs.
 AC_PROG_CC
 AC_PROG_LN_S
-AC_PROG_YACC
 LT_INIT
 LT_LIB_DLLOAD
 
index 279971bdabd48f539009c33e559d28c9382febe6..e6a2cf2df4f6d4eef49fc2f5dbd2b89a990280ca 100644 (file)
@@ -104,8 +104,8 @@ libshadow_la_SOURCES = \
        fs/readlink/readlinknul.c \
        fs/readlink/readlinknul.h \
        get_pid.c \
+       getdate.c \
        getdate.h \
-       getdate.y \
        getdef.c \
        getdef.h \
        getgr_nam_gid.c \
diff --git a/lib/getdate.c b/lib/getdate.c
new file mode 100644 (file)
index 0000000..3d08bd4
--- /dev/null
@@ -0,0 +1,80 @@
+// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <string.h>
+#include <time.h>
+
+#include "atoi/a2i/a2s.h"
+#include "getdate.h"
+#include "string/strchr/strchrcnt.h"
+#include "string/strcmp/streq.h"
+#include "string/strcmp/strprefix.h"
+#include "string/strcmp/strsuffix.h"
+#include "string/strspn/stpspn.h"
+
+
+#define TM_YEAR_ORIGIN 1900
+
+
+static long  yyDay;
+static long  yyMonth;
+static long  yyYear;
+
+
+static int parse_date(const char *s);
+
+
+time_t get_date (const char *p, const time_t *now)
+{
+  struct tm  tm;
+
+  if (parse_date(p) == -1)
+    return -1;
+
+  tm.tm_year = yyYear - TM_YEAR_ORIGIN;
+  tm.tm_mon = yyMonth - 1;
+  tm.tm_mday = yyDay;
+  tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
+  tm.tm_isdst = 0;
+
+  return timegm(&tm);
+}
+
+
+static int
+parse_date(const char *s)
+{
+       long  n;
+
+       if (!streq(stpspn(s, "0123456789-"), "")
+        || strchrcnt(s, '-') != 2
+        || strprefix(s, "-")
+        || strsuffix(s, "-")
+        || strstr(s, "--"))
+       {
+               return -1;
+       }
+
+       if (a2sl(&yyYear, s, &s, 10, LONG_MIN, LONG_MAX) == -1 && errno != ENOTSUP)
+               return -1;
+
+       if (!strprefix(s++, "-"))
+               return -1;
+
+       if (a2sl(&yyMonth, s, &s, 10, LONG_MIN, LONG_MAX) == -1 && errno != ENOTSUP)
+               return -1;
+
+       if (!strprefix(s++, "-"))
+               return -1;
+
+       if (a2sl(&yyDay, s, NULL, 10, LONG_MIN, LONG_MAX) == -1)
+               return -1;
+
+       return 0;
+}
diff --git a/lib/getdate.y b/lib/getdate.y
deleted file mode 100644 (file)
index c0df434..0000000
+++ /dev/null
@@ -1,248 +0,0 @@
-%{
-/*
-**  Originally written by Steven M. Bellovin <smb@research.att.com> while
-**  at the University of North Carolina at Chapel Hill.  Later tweaked by
-**  a couple of people on Usenet.  Completely overhauled by Rich $alz
-**  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
-**
-**  This grammar has 13 shift/reduce conflicts.
-**
-**  This code is in the public domain and has no copyright.
-*/
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-/* Since the code of getdate.y is not included in the Emacs executable
-   itself, there is no need to #define static in this file.  Even if
-   the code were included in the Emacs executable, it probably
-   wouldn't do any harm to #undef it here; this will only cause
-   problems if we try to write to a static variable, which I don't
-   think this code needs to do.  */
-#ifdef emacs
-# undef static
-#endif
-
-#include <ctype.h>
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-
-#include "attr.h"
-#include "getdate.h"
-#include "string/strspn/stpspn.h"
-
-
-/* Some old versions of bison generate parsers that use bcopy.
-   That loses on systems that don't provide the function, so we have
-   to redefine it here.  */
-#if !defined (HAVE_BCOPY) && !defined (bcopy)
-# define bcopy(from, to, len) memcpy ((to), (from), (len))
-#endif
-
-/* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
-   as well as gratuitously global symbol names, so we can have multiple
-   yacc generated parsers in the same program.  Note that these are only
-   the variables produced by yacc.  If other parser generators (bison,
-   byacc, etc) produce additional global names that conflict at link time,
-   then those parser generators need to be fixed instead of adding those
-   names to this list. */
-
-#define yymaxdepth gd_maxdepth
-#define yyparse gd_parse
-#define yylex   gd_lex
-#define yyerror gd_error
-#define yylval  gd_lval
-#define yychar  gd_char
-#define yydebug gd_debug
-#define yypact  gd_pact
-#define yyr1    gd_r1
-#define yyr2    gd_r2
-#define yydef   gd_def
-#define yychk   gd_chk
-#define yypgo   gd_pgo
-#define yyact   gd_act
-#define yyexca  gd_exca
-#define yyerrflag gd_errflag
-#define yynerrs gd_nerrs
-#define yyps    gd_ps
-#define yypv    gd_pv
-#define yys     gd_s
-#define yy_yys  gd_yys
-#define yystate gd_state
-#define yytmp   gd_tmp
-#define yyv     gd_v
-#define yy_yyv  gd_yyv
-#define yyval   gd_val
-#define yylloc  gd_lloc
-#define yyreds  gd_reds          /* With YYDEBUG defined */
-#define yytoks  gd_toks          /* With YYDEBUG defined */
-#define yylhs   gd_yylhs
-#define yylen   gd_yylen
-#define yydefred gd_yydefred
-#define yydgoto gd_yydgoto
-#define yysindex gd_yysindex
-#define yyrindex gd_yyrindex
-#define yygindex gd_yygindex
-#define yytable  gd_yytable
-#define yycheck  gd_yycheck
-
-static int yylex (void);
-static int yyerror (const char *s);
-
-
-#define MAX_BUFF_LEN    128   /* size of buffer to read the date into */
-
-/*
-**  An entry in the lexical lookup table.
-*/
-typedef struct _TABLE {
-    const char *name;
-    int                type;
-    int                value;
-} TABLE;
-
-
-/*
-**  Global variables.  We could get rid of most of these by using a good
-**  union as the yacc stack.  (This routine was originally written before
-**  yacc had the %union construct.)  Maybe someday; right now we only use
-**  the %union very rarely.
-*/
-static const char      *yyInput;
-static int     yyDay;
-static int     yyMonth;
-static int     yyYear;
-
-%}
-
-%union {
-    int                        Number;
-}
-
-%token tSNUMBER tUNUMBER
-
-%type  <Number>        tSNUMBER tUNUMBER
-
-%%
-
-spec   : /* NULL */
-       | spec item
-       ;
-
-item   : date
-       ;
-
-date   : tUNUMBER tSNUMBER tSNUMBER {
-           /* ISO 8601 format.  yyyy-mm-dd.  */
-           yyYear = $1;
-           yyMonth = -$2;
-           yyDay = -$3;
-       }
-       ;
-
-%%
-
-\f
-
-
-static int yyerror (MAYBE_UNUSED const char *s)
-{
-  return 0;
-}
-
-static int
-yylex (void)
-{
-  register char c;
-  register char *p;
-  char buff[20];
-  int Count;
-  int sign;
-
-  for (;;)
-    {
-      yyInput = stpspn(yyInput, " \t");
-
-      if (isdigit (c = *yyInput) || c == '-' || c == '+')
-       {
-         if (c == '-' || c == '+')
-           {
-             sign = c == '-' ? -1 : 1;
-             if (!isdigit (*++yyInput))
-               /* skip the '-' sign */
-               continue;
-           }
-         else
-           sign = 0;
-         for (yylval.Number = 0; isdigit (c = *yyInput++);)
-           yylval.Number = 10 * yylval.Number + c - '0';
-         yyInput--;
-         if (sign < 0)
-           yylval.Number = -yylval.Number;
-         return (0 != sign) ? tSNUMBER : tUNUMBER;
-       }
-      if (c != '(')
-       return *yyInput++;
-      Count = 0;
-      do
-       {
-         c = *yyInput++;
-         if (c == '\0')
-           return c;
-         if (c == '(')
-           Count++;
-         else if (c == ')')
-           Count--;
-       }
-      while (Count > 0);
-    }
-}
-
-#define TM_YEAR_ORIGIN 1900
-
-time_t get_date (const char *p, const time_t *now)
-{
-  struct tm  tm;
-
-  yyInput = p;
-
-  if (yyparse())
-    return -1;
-
-  tm.tm_year = yyYear - TM_YEAR_ORIGIN;
-  tm.tm_mon = yyMonth - 1;
-  tm.tm_mday = yyDay;
-  tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
-  tm.tm_isdst = 0;
-
-  return timegm(&tm);
-}
-
-#if    defined (TEST)
-
-int
-main(void)
-{
-  char buff[MAX_BUFF_LEN + 1];
-  time_t d;
-
-  (void) printf ("Enter date, or blank line to exit.\n\t> ");
-  (void) fflush (stdout);
-
-  buff[MAX_BUFF_LEN] = 0;
-  while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0])
-    {
-      d = get_date(buff, NULL);
-      if (d == -1)
-       (void) printf ("Bad format - couldn't convert.\n");
-      else
-       (void) printf ("%s", ctime (&d));
-      (void) printf ("\t> ");
-      (void) fflush (stdout);
-    }
-  exit (0);
-  /* NOTREACHED */
-}
-#endif /* defined (TEST) */
index b76b639b22608b66a5b4e0a64d8479acedcaef98..abd0f2502c0f16caf9f846c0a8da02ca51e5ea3b 100644 (file)
@@ -10,7 +10,6 @@
       - automake
       - bash
       - build-base
-      - byacc
       - cmocka-dev
       - coreutils
       - expect
index ab16580f8280bf6d5580a41a46ca2d5792ea96a0..92a779b984b5ec9e0e332c23a9d69b0f55abc235 100644 (file)
@@ -8,7 +8,6 @@
     name:
       - autoconf
       - automake
-      - byacc
       - diffutils
       - gawk
       - gcc