Junbo-Zheng commented on code in PR #3741:
URL: https://github.com/apache/nuttx-apps/pull/3741#discussion_r3828758331


##########
system/grep/grep_main.c:
##########
@@ -0,0 +1,299 @@
+/****************************************************************************
+ * apps/system/grep/grep_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <errno.h>
+#include <regex.h>
+
+#if !defined(CONFIG_SYSTEM_GREP_PROGNAME)
+#define CONFIG_SYSTEM_GREP_PROGNAME "grep"
+#endif
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define GREP_F_INVERT  (1u << 0)   /* -v: invert match               */
+#define GREP_F_ICASE   (1u << 1)   /* -i: case-insensitive           */
+#define GREP_F_LINENO  (1u << 2)   /* -n: print line number          */
+#define GREP_F_RECURSE (1u << 3)   /* -r: recurse into directories   */
+#define GREP_F_NAME    (1u << 4)   /* -H: always print filename      */
+#define GREP_F_NONAME  (1u << 5)   /* -h: never print filename       */
+
+#define GREP_LINE_MAX  512
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int grep_stream(FILE *in, FAR const char *label,
+                       FAR const regex_t *re, unsigned int flags)
+{
+  char line[GREP_LINE_MAX];
+  unsigned long lineno = 0;
+  int matched = 0;
+
+  while (fgets(line, sizeof(line), in) != NULL)
+    {
+      size_t n = strlen(line);
+      if (n > 0 && line[n - 1] == '\n')
+        {
+          line[n - 1] = '\0';
+        }
+
+      lineno++;
+
+      int hit = (regexec(re, line, 0, NULL, 0) == 0);
+      if (flags & GREP_F_INVERT)
+        {
+          hit = !hit;
+        }
+
+      if (hit)
+        {
+          matched = 1;
+          if (label != NULL && !(flags & GREP_F_NONAME))
+            {
+              printf("%s:", label);
+            }
+
+          if (flags & GREP_F_LINENO)
+            {
+              printf("%lu:", lineno);
+            }
+
+          printf("%s\n", line);
+        }
+    }
+
+  return matched ? 0 : 1;
+}
+
+static int grep_file(FAR const char *path, FAR const regex_t *re,

Review Comment:
   @xiaoxiang781216 All addressed, please take a look.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to